Repository navigation
fix: keep set operations after subquery operands at the outer query; build set-operation scopes in linear time - #32
Merged
Conversation
A parenthesized query used as an expression operand, such as the scalar subquery in `WHERE x < (SELECT ...) UNION ALL SELECT ...`, absorbed the following set operation, so the UNION/EXCEPT/INTERSECT became part of the predicate instead of combining the outer SELECTs. The same happened in HAVING, JOIN ON, BETWEEN and doubly parenthesized operands. A parenthesized query now continues into a set operation only where it starts the contents of an enclosing parenthesis, as in `x IN ((SELECT 1) UNION (SELECT 2))`, or where CREATE TABLE AS parses its parenthesized query. Statement-level parenthesized set operations are unchanged.
Each set-operation branch scope copied its whole operand, so a chain of N UNION/INTERSECT/EXCEPT branches copied every earlier branch at each level and scope building, validation and query analysis grew quadratically. A branch that is itself an unmodified set operation now stores its operator and clauses with NULL placeholders for set-operation operands, which are the expressions of its own branch scopes. Lineage and query-analysis nullability borrow each branch's complete query from its parent instead, and Scope::output_columns follows the leftmost branch scope. The scope tree shape and all validation, analysis and lineage results are unchanged. Add set_operation_branch_queries for consumers that need a branch's complete query.
This was referenced Oct 2, 2026
Merged
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.
Why
WHERE a.x < (SELECT …) UNION ALL SELECT …parsed asa.x < ((SELECT …) UNION ALL SELECT …). The same happened in HAVING, JOIN ON, BETWEEN and the SELECT list, so downstream tools reported false column-count errors and mis-formatted the query.Changes
fix:a bracketed query takes a following set operation only when it is the first operand inside another bracket (x IN ((SELECT 1) UNION (SELECT 2))) or when CTAS opts in.perf:nested plain set-operation branch scopes store a shallow node. Lineage and nullability get the full branch query through the newset_operation_branch_queries, andScope::output_columnsfollows the left branch.Verification