1
Fork 0

add with expressions, all tpch appear to parse correct, some of the later ones pretty print badly though

This commit is contained in:
Jake Wheat 2013-12-14 00:58:12 +02:00
parent 15c83555ff
commit 931272d1db
5 changed files with 60 additions and 28 deletions

View file

@ -539,10 +539,20 @@ attempt to fix the precedence and associativity. Doesn't work
> offset :: P (Maybe ScalarExpr) > offset :: P (Maybe ScalarExpr)
> offset = optionalScalarExpr "offset" > offset = optionalScalarExpr "offset"
> with :: P QueryExpr
> with = try (keyword_ "with") >>
> With <$> commaSep1 withQuery
> <*> queryExpr
> where
> withQuery = (,) <$> (identifierString
> <* optional (try $ keyword_ "as"))
> <*> parens queryExpr
> queryExpr :: P QueryExpr > queryExpr :: P QueryExpr
> queryExpr = > queryExpr =
> (try (keyword_ "select") >> > choice [select >>= queryExprSuffix, with]
> where
> select = try (keyword_ "select") >>
> Select > Select
> <$> (fromMaybe All <$> duplicates) > <$> (fromMaybe All <$> duplicates)
> <*> selectList > <*> selectList
@ -552,12 +562,11 @@ attempt to fix the precedence and associativity. Doesn't work
> <*> having > <*> having
> <*> option [] orderBy > <*> option [] orderBy
> <*> limit > <*> limit
> <*> offset) > <*> offset
> >>= queryExprSuffix
> queryExprSuffix :: QueryExpr -> P QueryExpr > queryExprSuffix :: QueryExpr -> P QueryExpr
> queryExprSuffix qe = > queryExprSuffix qe =
> choice [CombineQueryExpr qe > choice [(CombineQueryExpr qe
> <$> try (choice > <$> try (choice
> [Union <$ keyword_ "union" > [Union <$ keyword_ "union"
> ,Intersect <$ keyword_ "intersect" > ,Intersect <$ keyword_ "intersect"
@ -566,7 +575,8 @@ attempt to fix the precedence and associativity. Doesn't work
> <*> (option Respectively > <*> (option Respectively
> $ try (Corresponding > $ try (Corresponding
> <$ keyword_ "corresponding")) > <$ keyword_ "corresponding"))
> <*> queryExpr > <*> queryExpr)
> >>= queryExprSuffix
> ,return qe] > ,return qe]
> queryExprs :: P [QueryExpr] > queryExprs :: P [QueryExpr]

View file

@ -143,6 +143,12 @@ back into SQL source text. It attempts to format the output nicely.
> Corresponding -> text "corresponding" > Corresponding -> text "corresponding"
> Respectively -> empty > Respectively -> empty
> ,queryExpr q2] > ,queryExpr q2]
> queryExpr (With withs qe) =
> text "with"
> <+> vcat [nest 4
> (vcat $ punctuate comma $ flip map withs $ \(n,q) ->
> text n <+> text "as" <+> parens (queryExpr q))
> ,queryExpr qe]
> selectList :: [(Maybe String, ScalarExpr)] -> Doc > selectList :: [(Maybe String, ScalarExpr)] -> Doc
> selectList is = commaSep $ map si is > selectList is = commaSep $ map si is

View file

@ -77,6 +77,7 @@
> ,qeCorresponding :: Corresponding > ,qeCorresponding :: Corresponding
> ,qe2 :: QueryExpr > ,qe2 :: QueryExpr
> } > }
> | With [(String,QueryExpr)] QueryExpr
> deriving (Eq,Show) > deriving (Eq,Show)
> data Duplicates = Distinct | All deriving (Eq,Show) > data Duplicates = Distinct | All deriving (Eq,Show)

View file

@ -221,6 +221,7 @@
> ,orderBy > ,orderBy
> ,limit > ,limit
> ,combos > ,combos
> ,withQueries
> ,fullQueries > ,fullQueries
> ] > ]
@ -388,6 +389,25 @@
> {qeSelectList = [(Nothing,Iden "b")] > {qeSelectList = [(Nothing,Iden "b")]
> ,qeFrom = [SimpleTableRef "u"]} > ,qeFrom = [SimpleTableRef "u"]}
> withQueries :: TestItem
> withQueries = Group "with queries" $ map (uncurry TestQueryExpr)
> [("with u as (select a from t) select a from u"
> ,With [("u", ms1)] ms2)
> ,("with x as (select a from t),\n\
> \ u as (select a from x)\n\
> \select a from u"
> ,With [("x", ms1), ("u",ms3)] ms2)
> ]
> where
> ms c t = makeSelect
> {qeSelectList = [(Nothing,Iden c)]
> ,qeFrom = [SimpleTableRef t]}
> ms1 = ms "a" "t"
> ms2 = ms "a" "u"
> ms3 = ms "a" "x"
> fullQueries :: TestItem > fullQueries :: TestItem
> fullQueries = Group "queries" $ map (uncurry TestQueryExpr) > fullQueries = Group "queries" $ map (uncurry TestQueryExpr)
> [("select count(*) from t" > [("select count(*) from t"

View file

@ -360,8 +360,6 @@ order by
-- q13 -- q13
-- needs full table alias
/*
select select
c_count, c_count,
count(*) as custdist count(*) as custdist
@ -382,7 +380,6 @@ group by
order by order by
custdist desc, custdist desc,
c_count desc; c_count desc;
*/
-- q14 -- q14
@ -401,7 +398,6 @@ where
and l_shipdate < date '1994-12-01' + interval '1' month; and l_shipdate < date '1994-12-01' + interval '1' month;
-- q15 -- q15
-- needs cte
/*create view revenue0 (supplier_no, total_revenue) as /*create view revenue0 (supplier_no, total_revenue) as
select select
l_suppkey, l_suppkey,
@ -413,7 +409,7 @@ where
and l_shipdate < date '1995-06-01' + interval '3' month and l_shipdate < date '1995-06-01' + interval '3' month
group by group by
l_suppkey;*/ l_suppkey;*/
/*
with with
revenue0 as revenue0 as
(select (select
@ -445,7 +441,6 @@ where
) )
order by order by
s_suppkey; s_suppkey;
*/
-- q16 -- q16