Open icacho opened 2 years ago
Stumbled upon this problem as well and discovered several other set-operations related problems.
FYI: INTERSECT
should bind more strongly that UNION
, while EXCEPT
should bind at the same level as UNION
.
Additionally discovered that UNION
operator binding is incorrectly. For example parsing this SQL:
select * from foo UNION select * from bar UNION select * from baz
produces the following AST:
[{
"type": "union",
"left": {
"type": "select",
"columns": [ { "expr": { "type": "ref", "name": "*" } } ],
"from": [ { "type": "table", "name": { "name": "foo" } } ]
},
"right": {
"type": "union",
"left": {
"type": "select",
"columns": [ { "expr": { "type": "ref", "name": "*" } } ],
"from": [ { "type": "table", "name": { "name": "bar" } } ]
},
"right": {
"type": "select",
"columns": [ { "expr": { "type": "ref", "name": "*" } } ],
"from": [ { "type": "table", "name": { "name": "baz" } } ]
}
}
}]
It should bind from left to right, treating the SQL as if it were parenthesized like so:
(select * from foo UNION select * from bar) UNION select * from baz
But instead it treats the SQL as if it were parenthesized like so:
select * from foo UNION (select * from bar UNION select * from baz)
Parsing the following valid PostgreSQL code throws an error:
(select * from foo) UNION (select * from bar) ORDER BY name LIMIT 5
The following code parses just fine:
WITH p AS (select * from persons) (select * from p) UNION (select * from p)
But the following throws parse error:
WITH p AS (select * from persons) (select * from p UNION select * from p)
Though the root problem here seems to be that a parenthesized SELECT statement is not considered valid. This throws error:
(select * from p)
Using
Fails with
Expected
https://onecompiler.com/postgresql/3ygqmf7we