parsing bug with intersect, dialect: postgres
Author: laravioliCreated Sep 18, 2026Updated Sep 18, 2026
postgres: there is a bug while parsing a chain of union/intersect
postgres doc (18.6) state:
without parentheses, UNION and EXCEPT associate left-to-right, but INTERSECT binds more tightly than those two operators. Thus query1 UNION query2 INTERSECT query3 means query1 UNION (query2 INTERSECT query3)
from sqlglot import parse_one
select = """select 1 union select 2 intersect select 3"""
parse_one(select, dialect= "postgres")show -> (query1 UNION query2) INTERSECT query3 instead of -> query1 UNION (query2 INTERSECT query3)
Source: tobymao/sqlglot