Repository navigation
Fix LOCAL in a join being taken as an implicit alias of the left table - #120222
PedroTadim merged 4 commits into
Conversation
`ParserAlias::parseImpl` refuses a keyword as an implicit alias only when the keyword is listed in `restricted_keywords`. That array carries `GLOBAL` but not `LOCAL`, so after a table expression the alias parser consumed `LOCAL` before `ParserTablesInSelectQueryElement` could read it as `JoinLocality::Local`. Two consequences. In the plain spelling the qualifier is silently dropped: `SELECT * FROM ta LOCAL CROSS JOIN tb` formats back as `SELECT * FROM ta AS LOCAL CROSS JOIN tb`, and the qualifier is not cosmetic, since `EXPLAIN` shows `Locality: LOCAL`, selects `SpillingHashJoin(ConcurrentHashJoin)` and drops cost and NDV estimation only when it survives. And on assertions-enabled builds the query aborts: a parenthesized join and a pipe operator each desugar into a plain `SELECT` whose left table has no alias, which re-opens the implicit-alias slot on the second parse, so the same text parses into a different tree. `ASTTableJoin::updateTreeHashImpl` hashes `locality` while `getID` does not print it, which is why the two trees the error message dumps look identical while their hashes differ. Refuse `LOCAL` as an implicit alias only when the next token is one of the 14 keywords the join grammar admits between a locality qualifier and `JOIN`, read off `ParserTablesInSelectQueryElement::parseImpl` and `parseJoinStrictness`. The peek uses `IParser::checkWithoutMoving`, which takes its position by value, so the cursor cannot move, and a private `Expected` keeps the error-message variants unpolluted. `ARRAY`, the comma of a comma join, and `OUTER` are deliberately excluded so they keep their current permissive parse. The first two are tested before the locality keywords, so they can never follow a locality. `OUTER` is consumed only after a join kind and every kind keyword is already restricted, so no tree can carry a swallowed `OUTER` back into text that spells a keyword. Adding `"LOCAL"` to `restricted_keywords` would be a single line, but it would reserve the word for every implicit alias in the language and turn `SELECT 1 local` into a syntax error, which this bug does not require. The same narrow choice was made for `COMMENT` in the same function, and `tests/queries/0_stateless/04299_at_time_zone.sql` pins the compatibility of `local` as a column name and an alias as a contract. Related: ClickHouse#119190 Co-Authored-By: Claude Opus 5 <[email protected]>
The previous commit refuses `LOCAL` as an implicit alias whenever one of the
14 keywords the join grammar admits between a locality qualifier and `JOIN`
follows it. `ARRAY JOIN` is a sibling of the join clause, tried first in the
element parser, and it accepts an optional `LEFT` or `INNER` prefix, so a
one-token peek cannot tell `local LEFT ARRAY JOIN [1]` from
`local LEFT JOIN tb`. Both were refused, the token was then read as the
locality, and the mandatory `JOIN` never arrived: two previously valid
queries became SYNTAX_ERROR.
Skip the refusal when an `ARRAY JOIN` starts at the peek position, in any of
its three spellings. `ASTArrayJoin` carries no locality, so nothing is lost
by leaving the alias in front of it, and the collision set is exactly
`{LEFT, INNER} x ARRAY JOIN`: `LEFT OUTER ARRAY JOIN` is a syntax error
before and after this change.
Both prefixed spellings are pinned as compatibility rows, alongside a
`local LEFT JOIN ... ON` discriminator so they cannot be satisfied by
dropping `LEFT` and `INNER` from the peek set instead. Two further rows
cover the remaining join-grammar positions that no earlier row reached: a
bare `JOIN` with the kind defaulted, and a leading `NATURAL`.
Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Two rounds of pre-publication review by an independent model (engine: codex; 2 findings) plus my own
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20260915-145000 |
|
Workflow [PR], commit [7f83cf6] Summary: ✅
AI ReviewSummaryThe PR preserves Final VerdictStatus: ✅ Approve |
CI finish ledger - 12d984eEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
Session id: cron:our-pr-ci-monitor:20260915-213215 |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline. |
|
@groeneai @PedroTadim — this PR looks backward incompatible but is categorized as Why: The new Suggested category: |
|
Agreed: |
|
Yes, it is backward incompatible, but a very edge case |
Related: #119190
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
LOCALbetween a table and a join, as inSELECT * FROM a LOCAL JOIN b ON a.k = b.k, is now parsed as the join locality qualifier, likeGLOBAL, instead of being silently taken as an implicit alias ofa. A query that relies on such an implicitlocalalias, for exampleSELECT * FROM t local JOIN u ON local.id = u.id, now fails; writet AS localinstead. Stored view and table definitions are not affected, because they are saved with an explicitAS.Description
ParserAlias::parseImplrefuses a keyword as an implicit alias only if it is inrestricted_keywords.GLOBALis;LOCALwas not, so the alias parser consumed it beforeParserTablesInSelectQueryElementcould read the locality. Two consequences:The qualifier is silently ignored.
SELECT * FROM ta LOCAL CROSS JOIN tbformats back as... FROM ta AS LOCAL CROSS JOIN tb. Not cosmetic: with itEXPLAINshowsLocality: LOCAL, switches toSpillingHashJoin(ConcurrentHashJoin)and drops cost/NDV estimation.Assertions-enabled builds abort. A parenthesized join and a pipe operator desugar into a plain
SELECTwhose left table has no alias, re-opening the implicit-alias slot on the second parse.ASTTableJoin::updateTreeHashImplhasheslocality, so the round trip is unstable andexecuteQuery.cpp's debug-only check raisesInconsistent AST formatting:SELECT * FROM (ta LOCAL CROSS JOIN tb)aborts a debug server;GLOBALthere is fine.The fix refuses
LOCALas an implicit alias only when the next token is one of the 14 keywords the join grammar admits between a locality qualifier andJOIN, unless those tokens start anARRAY JOIN, whose optionalLEFT/INNERprefix reuses two of them and carries no locality.localstays a valid alias, column name and table name wherever a join clause cannot follow it, which04299_at_time_zone.sqlpins as a contract. Where one can,FROM t local JOIN unow reads the locality instead of aliasingt, likeGLOBAL; the repository has no such alias. Adding"LOCAL"torestricted_keywordswould be one line, but would makeSELECT 1 locala syntax error; #110372 made the same narrow choice forCOMMENT. Say the word if you prefer it.OUTER, the only other gap, is left alone: it is read only after a join kind, so no AST can carry it back into keyword text.Tests extend
04501_format_local_join, which already stated this invariant for explicitAS.Workflow [PR]
Sync PR [sync-upstream/pr/120222]
Version info
26.11.1.1375-master(included in26.11and later)