Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing queries involving UNION in statements #478

Open
kkadu opened this issue Apr 4, 2024 · 2 comments
Open

Parsing queries involving UNION in statements #478

kkadu opened this issue Apr 4, 2024 · 2 comments
Labels

Comments

@kkadu
Copy link

kkadu commented Apr 4, 2024

This may not be a bug but a new feature but I have experienced that queries that have UNION or INTERSECT / MINUS (less common) are not parsed correctly. Especially when you have multiple union all, it becomes important to have columns fetched as table.column however, in the case of union, we do not get table appended to column when you extract parser.column.

(SELECT a.abc, a.bcg, a.sdf, b.cnt FROM (SELECT abc, bcg, sdf FROM xxx.table1 UNION SELECT abc, NULL, sdf FROM yyy.table2 UNION SELECT abc, bcg, NULL FROM zzz.table3 WHERE rxy IN ('test')) AS a FULL OUTER JOIN (SELECT abc, count(r) AS cnt FROM (SELECT abc, r FROM xxx.table1 UNION SELECT abc, r FROM yyy.table2) GROUP BY abc) AS b ON a.abc = b.abc) UNION SELECT abc, NULL, NULL, sdf, 0 FROM xxx.table2 WHERE abc IS NOT NULL AND xyz IS NULL AND mmr IS NULL

when we parse the above statement for parser.tables we get ['xxx.table1', 'yyy.table2', 'zzz.table3', 'xxx.table2'] which is correct, however for parser.columns we get ['abc', 'bcg', 'sdf', 'r', 'NULL', 'rxy', 'xyz', 'mmr'] where we would expect to have table.column appended, so that it is clear which tables those columns are coming from i.e. ['xxx.table1.abc', 'yyy.table2.abc', 'zzz.table3.abc', 'xxx.table2.abc', 'xxx.table1.bcg', 'zzz.table3.bcg', 'xxx.table1.sdf', 'yyy.table2.sdf', 'xxx.table2.sdf', 'xxx.table1.r', 'yyy.table2.r', 'zzz.table3.rxy', 'xxx.table2.xyz', 'xxx.table2.mmr']

@kkadu
Copy link
Author

kkadu commented May 6, 2024

Hi @macbre - Let me know if this is a valid scenario for sql parser to handle. Is there any current workaround for this?

@macbre
Copy link
Owner

macbre commented May 8, 2024

@kkadu - can you try to provide a bit shorter SQL query that is still affected by the bug? It will make fixing it a bit easier (and also be a nice candidate for the unit tests).

@macbre macbre added the bug label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants