Parse the full 3x3 set-operator matrix (UNION/EXCEPT/INTERSECT x bare/ALL/DISTINCT)#2
Merged
Merged
Conversation
32edd2a to
34206ab
Compare
…/ALL/DISTINCT) ClickHouse SQL supports three set operators between SELECT queries, each with an optional ALL or DISTINCT modifier (nine surface forms total). The parser only handled five: UNION ALL, UNION DISTINCT, and bare EXCEPT. Bare UNION errored, EXCEPT ALL/DISTINCT errored, and INTERSECT wasn't a keyword. Refactor SelectQuery from per-mode pointer fields to one pointer + a typed mode discriminator per operator, mirroring the existing OrderDirection precedent: - REMOVE UnionAll *SelectQuery and UnionDistinct *SelectQuery. - ADD Union *SelectQuery + UnionMode UnionMode, Except *SelectQuery + ExceptMode ExceptMode (Except itself kept), Intersect *SelectQuery + IntersectMode IntersectMode. - New typed aliases UnionMode/ExceptMode/IntersectMode each with *None/*All/*Distinct constants. Walker, Accept, formatter, and parseSelectQuery migrate together. The formatter becomes three parallel arms with an inner switch on the mode. parseSelectQuery shares a consumeOptionalSetOpModifier helper across all three operator branches and drops the prior "expected ALL or DISTINCT" error on bare UNION. KeywordIntersect is added alphabetically to keyword.go. Bare EXCEPT and INTERSECT between top-level SELECTs surface two latent ambiguities with the in-tree SELECT-modifier feature (add-select-except-bare-ident) that did not exist when only EXCEPT-after- FROM was supported: - parseSelectItem's modifier loop greedily consumed `EXCEPT SELECT` as a column modifier because matchTokenKind(TokenKindIdent) treats keywords as identifiers. Guard with peekTokenKind so EXCEPT is only treated as a modifier when followed by `(` (parens form) or a true ident token (bare-ident form). - INTERSECT-as-bare-alias is now possible since INTERSECT is a keyword; add it to isSelectItemTerminatorKeyword alongside UNION and EXCEPT. Also restore parseExceptExpr in parser_column.go: the prior HOQ commit removed its definition while leaving the call site, which left the branch in a non-compiling state. The restored function is identical to its pre-HOQ form from ac97d1d. 90 pre-existing JSON goldens regenerate with the documented per-rendering diff (UnionAll/UnionDistinct lines removed; Union/UnionMode/ExceptMode/ Intersect/IntersectMode lines added; populated subtrees migrate to the new field name for the four set-op-populated fixtures). All parser/testdata/**/format/** and format/beautify/** goldens for pre-existing set-op fixtures remain byte-identical. Six new fixtures cover the newly-unlocked surface forms, each with parse/format/beautify goldens: - select_with_bare_union.sql - select_with_union_settings.sql (per-leg SETTINGS + bare UNION) - select_with_except_all.sql - select_with_except_distinct.sql - select_with_intersect.sql - select_with_intersect_modifiers.sql (chained ALL + DISTINCT) Tracked as openspec change add-set-operator-modes, archived under openspec/changes/archive/2026-06-02-add-set-operator-modes/. Canonical contract at openspec/specs/set-operator-modes/spec.md. Out of scope: mixed-operator precedence. The parser stays right- recursive, so `a UNION ALL b INTERSECT c` happens to associate correctly by luck but `a INTERSECT b UNION ALL c` mis-associates. ClickHouse's "INTERSECT binds tighter than UNION/EXCEPT; UNION/EXCEPT are left-to- right at equal precedence" rule is not enforced here. A follow-up change will address this; see Decision 8 in design.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
34206ab to
06efabd
Compare
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.
Summary
UNION,EXCEPT ALL,EXCEPT DISTINCT, and all threeINTERSECTvariants) so the parser now covers the full 3×3 matrix of{UNION, EXCEPT, INTERSECT} × {bare, ALL, DISTINCT}between SELECT queries.SelectQuery's set-operator slots to one pointer + a typed mode discriminator per operator (Union/UnionMode,Except/ExceptMode,Intersect/IntersectMode), mirroring the existingOrderDirectionprecedent. Breaking AST API change: removesUnionAll/UnionDistinct.parseSelectQuery, walker,Accept, and formatter together; addsKeywordIntersect. New formatter is three parallel arms with an inner switch on the mode.parser/parser_column.goso the in-tree EXCEPT-bare-ident SELECT-modifier feature (#ac97d1d) doesn't greedily consumeEXCEPT SELECTas a column modifier, and soINTERSECTisn't taken as a bare alias..golangci.ymlfrom v1 to v2 format (viagolangci-lint migrate);config verifypasses.Tracked as openspec change
add-set-operator-modes, archived underopenspec/changes/archive/2026-06-02-add-set-operator-modes/. Canonical contract atopenspec/specs/set-operator-modes/spec.md.Out of scope: mixed-operator precedence. The parser stays right-recursive, so
a INTERSECT b UNION ALL cmis-associates per ClickHouse's rule that INTERSECT binds tighter. A follow-up will address this — see Decision 8 indesign.md.Footprint
parser/(ast.go,walk.go,format.go,parser_query.go,parser_column.go) andkeyword.go..sqlfixtures × 3 goldens each = 18 new goldens.Test plan
go build ./parser/...go vet ./parser/...— only the pre-existingWriteBytewarning, no new onesgo test ./parser/... -count=1— 838 passing subtests, 0 failures (baseline was 814)TestParser_With_SetOperators— all 11 SQLs across the 3×3 matrix + 2 SETTINGS combinations passTestParser_InvalidSyntax— passes (error-message change documented in Decision 7)select_with_union_distinct,select_with_multi_union,select_with_multi_union_distinct,select_with_multi_except— byte-identicalopenspec validate set-operator-modes --type spec— validgolangci-lint config verify— passes🤖 Generated with Claude Code