Open hanjoosten opened 4 days ago
Thanks for the report. I've done a little bit of looking into this.
The following two statements are valid in Postgres (I think at least the first one is ANSI SQL as well):
select * from t except (select * from u except select * from v);
(select * from t);
Neither of these currently parse with this library.
I propose to add an explicit QueryExprParens or similar to query expressions. Then you would have to use this to get parentheses around a nested CTE in your case. This sort of follows the approach with ScalarExprs and TableRefs. The alternative would be to have the parentheses implicit when the are mandatory for a CTE, and implicit when pretty printing this also.
Edit: as is usual, I thought of something to check only after posting this comment: the TRQueryExpr - which also has to have parentheses around it, has implicit parens in the syntax and pretty printing, so to be consistent with this would mean making the parentheses implicit in CTEs inside QueryExprSetOp also. So now I'm leaning towards that option (with the explicit QueryExprParens for the other cases).
Hi Jake, Thanks for looking in to this. I am happy with the idea of adding an explicit QueryExprParens constructor for cases where it isn't obvious that parentheses are required. However, in the current cases where implicit parens are used, I wouldn't make them explicit too. That would likely cause breakage of applications that currently use your nice library to generate SQL.
I use this library to programmatically generate SQL. Recently, I am adding a feature in which I need to use recursive common table expressions. Fortunately, you support this with the
With
constructor of data typeQueryExpr
. However, if there are more than one elements in theqeViews
list, the generated sql forgets to put brackets when used as subquery in aQueryExprSetOp
.Currently, the query is output as:
The brackets should be placed at the subquery that is the right hand side of the Union, like this: