Home > Back-end >  Disambiguate a SQL set operation (UNION|INTERSECT|EXCEPT)
Disambiguate a SQL set operation (UNION|INTERSECT|EXCEPT)

Time:08-24

I am working on a SQL grammar similar (at a high level) to BigQuery, which has the following enter image description here

However, I believe this grammar may be ambiguous (also pointed out in the answer from here: enter image description here

You can make the set_operator right-associative like so:


query_statement
   : query_expr # simplequery
   | <assoc=right> query_statement set_operator query_statement # setQuery
   ;

And get this parse tree: enter image description here

This has no effect on which SELECT the LIMIT binds to.

Off hand, I'm not sure how you would get

SELECT 1 UNION SELECT 1 LIMIT 1

interpreted as:

(SELECT 1 UNION (SELECT 1) LIMIT 1)

(it also seems as though it would be a most unintuitive interpretation of the input, but that's just my opinion)

  • Related