Skip to content

Parse the full 3x3 set-operator matrix (UNION/EXCEPT/INTERSECT x bare/ALL/DISTINCT)#2

Merged
odemkovych merged 1 commit into
mainfrom
feat/high_order_queries
Jun 3, 2026
Merged

Parse the full 3x3 set-operator matrix (UNION/EXCEPT/INTERSECT x bare/ALL/DISTINCT)#2
odemkovych merged 1 commit into
mainfrom
feat/high_order_queries

Conversation

@odemkovych

Copy link
Copy Markdown

Summary

  • Fills in the four missing surface forms (bare UNION, EXCEPT ALL, EXCEPT DISTINCT, and all three INTERSECT variants) so the parser now covers the full 3×3 matrix of {UNION, EXCEPT, INTERSECT} × {bare, ALL, DISTINCT} between SELECT queries.
  • Refactors SelectQuery's set-operator slots to one pointer + a typed mode discriminator per operator (Union/UnionMode, Except/ExceptMode, Intersect/IntersectMode), mirroring the existing OrderDirection precedent. Breaking AST API change: removes UnionAll/UnionDistinct.
  • Migrates parseSelectQuery, walker, Accept, and formatter together; adds KeywordIntersect. New formatter is three parallel arms with an inner switch on the mode.
  • Adds two scoped fixes in parser/parser_column.go so the in-tree EXCEPT-bare-ident SELECT-modifier feature (#ac97d1d) doesn't greedily consume EXCEPT SELECT as a column modifier, and so INTERSECT isn't taken as a bare alias.
  • Migrates .golangci.yml from v1 to v2 format (via golangci-lint migrate); config verify passes.

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 INTERSECT b UNION ALL c mis-associates per ClickHouse's rule that INTERSECT binds tighter. A follow-up will address this — see Decision 8 in design.md.

Footprint

  • 5 source files in parser/ (ast.go, walk.go, format.go, parser_query.go, parser_column.go) and keyword.go.
  • 90 pre-existing JSON goldens regenerate by a 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).
  • Format and beautify goldens for the four pre-existing set-op fixtures stay byte-identical.
  • 6 new .sql fixtures × 3 goldens each = 18 new goldens.

Test plan

  • go build ./parser/...
  • go vet ./parser/... — only the pre-existing WriteByte warning, no new ones
  • go 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 pass
  • TestParser_InvalidSyntax — passes (error-message change documented in Decision 7)
  • Format/beautify goldens for select_with_union_distinct, select_with_multi_union, select_with_multi_union_distinct, select_with_multi_except — byte-identical
  • Three-spot diff inspection of regenerated JSON goldens (UNION subtree migration, EXCEPT mode addition, non-set-op pure rename + addition)
  • openspec validate set-operator-modes --type spec — valid
  • golangci-lint config verify — passes

🤖 Generated with Claude Code

@odemkovych odemkovych self-assigned this Jun 2, 2026
@odemkovych odemkovych force-pushed the feat/high_order_queries branch 2 times, most recently from 32edd2a to 34206ab Compare June 2, 2026 19:22
…/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>
@odemkovych odemkovych force-pushed the feat/high_order_queries branch from 34206ab to 06efabd Compare June 2, 2026 19:24
@odemkovych odemkovych merged commit 703b291 into main Jun 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant