Repository navigation
Keep a set operation after an unaliased derived table outside it - #18
Merged
Merged
Conversation
In `SELECT 1 AS s, * FROM (SELECT x FROM a) UNION ALL SELECT 2, x FROM b` the parser folded the UNION ALL (and any trailing LIMIT) into the derived table, changing the query: the outer projection then applied to the whole union. qualify_tables' set-operation normalization undid it only for a bare `SELECT *` outer query. A set operation after the closing paren belongs to the enclosing query; nested parens (`FROM ((SELECT 1) UNION (SELECT 2))`) are still parsed by the nested-paren branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017CxHoCFbTWJA9bcZWC9ydu
The nested-paren branch now parses the set operation itself, so wrap it in the Subquery the inner paren branch used to return. The ORDER BY/LIMIT/OFFSET handling after it relies on that shape: without it, SELECT * FROM (((SELECT 1) UNION SELECT 2) ORDER BY x LIMIT 1 OFFSET 1) moved the modifiers outside the outer parens (sqlglot_identity). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017CxHoCFbTWJA9bcZWC9ydu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After a parenthesized query in
FROM, the parser folded any followingUNION/INTERSECT/EXCEPT(and a trailingLIMIT) into the derived table:That changes the query: the outer projection is applied to the whole union. Found in production as
TYPE_MISMATCH: UNION query has different number of fields: 6, 7.qualify_tables' set-operation-subquery normalization undid the wrapper, but only when the outer query is a bareSELECT *, which hid the bug in the common case. Upstream 0.14.1 has the same behaviour.Change
FROM ((SELECT 1) UNION (SELECT 2))), the nested-paren branch already parsed the set operation. It now wraps it in the sameSubquerythe inner branch used to return. ItsORDER BY/LIMIT/OFFSEThandling relies on that shape: without it,SELECT * FROM (((SELECT 1) UNION SELECT 2) ORDER BY x LIMIT 1 OFFSET 1)moved the modifiers outside the outer parens (caught bysqlglot_identity).qualify_tables::test_can_disable_set_operation_wrapper_normalizationasserted the misparsed shape with the normalization turned off. It now expects the correct one, with no wrapper left to normalize.Tests
tests/unaliased_derived_table_set_operation.rs(new):LIMITwith an outerLIMIT, a comma join, andEXCEPT, in Trino and generic;ORDER BY … LIMIT … OFFSETone above.sqlglot_transpilation,sqlglot_identity,sqlglot_dialect_identity,sqlglot_parserandsqlglot_transpileall pass in full.cargo test --lib(1343),identity_roundtripandquery_analysispass.query-rewritersuite, including its column and lineage fixtures, passes againstmainwith this PR, Reprint Trino/Presto TIMESTAMP(x) as CAST(x AS TIMESTAMP) #16 and Keep Presto/Trino two-argument MAX(x, n)/MIN(x, n) as aggregates #17 combined.🤖 Generated with Claude Code
https://claude.ai/code/session_017CxHoCFbTWJA9bcZWC9ydu