1
Fork 0

add support for with recursive and column aliases in cte

This commit is contained in:
Jake Wheat 2013-12-17 13:41:06 +02:00
parent c52334943f
commit 4308acb982
7 changed files with 57 additions and 38 deletions

View file

@ -149,14 +149,14 @@ queries section
> \WHERE region IN (SELECT region FROM top_regions)\n\
> \GROUP BY region, product;"
> {-,"WITH RECURSIVE t(n) AS (\n\
> ,"WITH RECURSIVE t(n) AS (\n\
> \ VALUES (1)\n\
> \ UNION ALL\n\
> \ SELECT n+1 FROM t WHERE n < 100\n\
> \)\n\
> \SELECT sum(n) FROM t"-} -- full alias in cte
> \SELECT sum(n) FROM t"
> {-,"WITH RECURSIVE included_parts(sub_part, part, quantity) AS (\n\
> ,"WITH RECURSIVE included_parts(sub_part, part, quantity) AS (\n\
> \ SELECT sub_part, part, quantity FROM parts WHERE part = 'our_product'\n\
> \ UNION ALL\n\
> \ SELECT p.sub_part, p.part, p.quantity\n\
@ -177,7 +177,7 @@ queries section
> \)\n\
> \SELECT * FROM search_graph;"
> ,"WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (\n\
> {-,"WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (\n\
> \ SELECT g.id, g.link, g.data, 1,\n\
> \ ARRAY[g.id],\n\
> \ false\n\
@ -189,9 +189,9 @@ queries section
> \ FROM graph g, search_graph sg\n\
> \ WHERE g.id = sg.link AND NOT cycle\n\
> \)\n\
> \SELECT * FROM search_graph;"
> \SELECT * FROM search_graph;"-} -- ARRAY
> ,"WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (\n\
> {-,"WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (\n\
> \ SELECT g.id, g.link, g.data, 1,\n\
> \ ARRAY[ROW(g.f1, g.f2)],\n\
> \ false\n\
@ -203,14 +203,14 @@ queries section
> \ FROM graph g, search_graph sg\n\
> \ WHERE g.id = sg.link AND NOT cycle\n\
> \)\n\
> \SELECT * FROM search_graph;"
> \SELECT * FROM search_graph;"-} -- ARRAY
> ,"WITH RECURSIVE t(n) AS (\n\
> \ SELECT 1\n\
> \ UNION ALL\n\
> \ SELECT n+1 FROM t\n\
> \)\n\
> \SELECT n FROM t LIMIT 100;"-}
> \SELECT n FROM t LIMIT 100;"
select page reference
@ -241,7 +241,7 @@ select page reference
> \UNION ALL\n\
> \SELECT * FROM t"
> {-,"WITH RECURSIVE employee_recursive(distance, employee_name, manager_name) AS (\n\
> ,"WITH RECURSIVE employee_recursive(distance, employee_name, manager_name) AS (\n\
> \ SELECT 1, employee_name, manager_name\n\
> \ FROM employee\n\
> \ WHERE manager_name = 'Mary'\n\
@ -250,7 +250,7 @@ select page reference
> \ FROM employee_recursive er, employee e\n\
> \ WHERE er.employee_name = e.manager_name\n\
> \ )\n\
> \SELECT distance, employee_name FROM employee_recursive;"-} -- with recursive, full cte alias
> \SELECT distance, employee_name FROM employee_recursive;"
> ,"SELECT m.name AS mname, pname\n\
> \FROM manufacturers m, LATERAL get_product_names(m.id) pname;"

View file

@ -172,12 +172,18 @@ These are a few misc tests which don't fit anywhere else.
> 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 False [(Alias "u" Nothing, ms1)] ms2)
> ,("with u(b) as (select a from t) select a from u"
> ,With False [(Alias "u" (Just ["b"]), 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)
> ,With False [(Alias "x" Nothing, ms1), (Alias "u" Nothing,ms3)] ms2)
> ,("with recursive u as (select a from t) select a from u"
> ,With True [(Alias "u" Nothing, ms1)] ms2)
> ]
> where
> ms c t = makeSelect

View file

@ -80,18 +80,18 @@ these lateral queries make no sense but the syntax is valid
> ,ms [TRQueryExpr $ ms [TRSimple "t"]])
> ,("select a from t as u"
> ,ms [TRAlias (TRSimple "t") "u" Nothing])
> ,ms [TRAlias (TRSimple "t") (Alias "u" Nothing)])
> ,("select a from t u"
> ,ms [TRAlias (TRSimple "t") "u" Nothing])
> ,ms [TRAlias (TRSimple "t") (Alias "u" Nothing)])
> ,("select a from t u(b)"
> ,ms [TRAlias (TRSimple "t") "u" $ Just ["b"]])
> ,ms [TRAlias (TRSimple "t") (Alias "u" $ Just ["b"])])
> ,("select a from (t cross join u) as u"
> ,ms [TRAlias (TRParens $
> TRJoin (TRSimple "t") JCross (TRSimple "u") Nothing)
> "u" Nothing])
> (Alias "u" Nothing)])
> -- todo: not sure if the associativity is correct
> ,("select a from t cross join u cross join v",