Repository navigation
Do not treat an equated INNER JOIN key as an ambiguous identifier - #109366
Conversation
An unqualified column that resolves to both sides of an `INNER JOIN` was reported as `AMBIGUOUS_IDENTIFIER` whenever `single_join_prefer_left_table` could not silently pick a side - for example with that setting disabled, or in a multi-way join. But when the two columns are equated by a top-level `equals` conjunct of the `ON` condition, every joined row carries the same value on both sides, so an unqualified reference is not actually ambiguous. `tryResolveIdentifierFromJoin` now resolves such a reference to the left side, exactly as `single_join_prefer_left_table` already did, so this only turns a previously-thrown ambiguity into the same result the default already produced. The relaxation is limited to `INNER` joins (an outer join may leave the non-preserved side default or NULL on unmatched rows) and to equalities reachable through a chain of `and` (an equality under `or` does not guarantee equal values). Existing tests `02372_analyzer_join`, `02374_analyzer_join_using` and `03716_multiple_joins_using_top_level_identifier` documented the old behavior and are updated accordingly; `04501_inner_join_key_column_not_ambiguous` is added for the new behavior and its guard rails. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [65b641f] Summary: ✅
AI ReviewSummaryThis PR narrows Final Verdict✅ No new findings. The implementation matches the narrowed contract, the focused regression coverage is adequate for that scope, and current CI is green. |
The flaky check for the changed test `02374_analyzer_join_using` failed
with "Test runs too long (> 180s)": across 173 randomized runs the average
was ~58s but a tail reached ~197s, i.e. specific randomized MergeTree
storage settings (direct I/O thresholds, uncompressed cache, part storage,
prewarm, etc.) pushed individual debug runs past the 180s limit.
The test only exercises analyzer `JOIN` / `USING` identifier resolution and
join value semantics; the storage engine is incidental. Switching the three
tables from `MergeTree` to `Memory` removes the MergeTree-settings
randomization that drives the slow tail while keeping the output identical:
the `CREATE TABLE` statements sit before the `{ echoOn }` marker so they are
never echoed into the reference, and every query uses `ORDER BY ALL`, so row
sets and ordering are unchanged (verified: `MergeTree` and `Memory` produce
byte-identical results for the test queries). The `.reference` is therefore
unchanged.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109366&sha=823a06f7ab937f8bfd09e1024d11a33e5dbd4be9&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ui-key-not-ambiguous
The new test `04501_inner_join_key_column_not_ambiguous` documents behavior
that is specific to the new analyzer (an equated `INNER JOIN` key is not an
`AMBIGUOUS_IDENTIFIER`, while non-equated columns, `OR` conditions, and outer
joins still are). The "old analyzer" stateless job runs with
`enable_analyzer = 0`, where these queries resolve differently and do not throw
`AMBIGUOUS_IDENTIFIER`, so the `-- { serverError AMBIGUOUS_IDENTIFIER }`
expectations failed (3 out of 3 reruns).
Add `SET enable_analyzer = 1;` at the top of the test, matching the sibling
analyzer tests this PR already touches (`02372_analyzer_join`,
`02374_analyzer_join_using`), so the test always exercises the new analyzer
regardless of the job's default. The reference output is unchanged.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109366&sha=5f98e8723f258ea37d813e553b7299664a560dd7
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The template expands to ~180 queries; under sanitizers with the flaky
check's 50 parallel reruns a single run can exceed the 180 second limit
("Test runs too long"). The sibling 02372_analyzer_join, with the same
4x4 join-matrix shape, already carries the `long` tag for this reason.
The flaky check exempts `long`-tagged tests from the run-time limit
(tests/clickhouse-test, FailureReason.TOO_LONG), so this fixes the only
PR-specific CI failure while preserving all coverage and the reference.
#109366
The review asked for a focused mixed-type regression that proves the resulting identifier semantics. The semantics are deliberate: for mixed-type equi-keys the unqualified reference resolves to the raw left column - the same value and type that the default `single_join_prefer_left_table = 1` has always produced for a single join - and not a USING-like common-type projection. The new queries pin `toTypeName(id)` to the left type (`UInt8`) for the relaxation path, show it is identical to the pre-existing single-join default, cover the multi-way join where `single_join_prefer_left_table` never applied, and contrast with `USING (id)`, which keeps exposing the common-type (`UInt16`) projection. #109366
The `various_checks.sh` style check now rejects the phrases "new analyzer" and "new query analyzer" in comments and docs (the analyzer is enabled by default since ClickHouse 24.3 and is no longer new). The comment in `04501_inner_join_key_column_not_ambiguous.sql` used "the new analyzer"; reword it to "the analyzer". "the old analyzer" is left as-is (the check only forbids the "new" forms). This surfaced on the `CH Inc sync` job (private sync PR ClickHouse/clickhouse-private#63276), whose merge with private master brought in the wording rule that the public PR's older merge-base predated. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: This PR only relaxes the analyzer's AMBIGUOUS_IDENTIFIER handling so an unqualified column that is an equated key of an INNER JOIN resolves to the left side instead of erroring — a pure identifier-resolution change with no effect on the execution hot path, and it only runs for JOIN queries. The single flagged item, ClickBench Q4, has no join at all, so the diff cannot plausibly move its timing; the -24.7% delta falls within master's current variance band (201.5-294.0 ms) and was correctly held at no_change as run-to-run noise. The deterministic envelope suppression is consistent with the diff, and no cross-query pattern applies. clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
…d identifiers
The equi-key relaxation in `innerJoinKeyColumnsAreEquated` was applied to every
ambiguous JOIN identifier lookup, not just the unqualified (one-part) column
references it is meant for. For a storage that exposes subcolumns as `ColumnNode`
(`supportsSubcolumns` is true), a compound identifier could bind to a left
subcolumn and a right column at the same time; when the `ON` condition equated
those two candidates, the relaxation silently resolved the ambiguity to the left
side instead of raising `AMBIGUOUS_IDENTIFIER`, changing qualified/subcolumn name
resolution semantics.
Gate the branch on `identifier_lookup.identifier.isShort()` so it only fires for
truly unqualified identifiers, matching the advertised behavior.
Reproducer (with `single_join_prefer_left_table = 0`), previously resolved to the
left subcolumn, now correctly raises `AMBIGUOUS_IDENTIFIER`:
CREATE TABLE t_sub (p Tuple(q Int64)) ENGINE = Memory;
CREATE TABLE p (q Int32) ENGINE = Memory;
SELECT p.q FROM t_sub INNER JOIN p AS pr ON t_sub.p.q = pr.q;
Covered by a new case in `04501_inner_join_key_column_not_ambiguous`.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ated INNER JOIN key relaxation
The equated `INNER JOIN` key relaxation (`innerJoinKeyColumnsAreEquated`)
matches a direct `equals` of the two resolved columns in the nearest join's
`ON`; it does not chase a transitive equivalence class across the left
subtree. So a multi-way chain whose later `ON` equates a key that is not the
exposed left representative - e.g.
SELECT id FROM t1
INNER JOIN t2 ON t1.id = t2.id
INNER JOIN t3 ON t2.id = t3.id
with `single_join_prefer_left_table = 0`, where the left subtree exposes `id`
as `t1.id` but the second `ON` names `t2.id` - still (correctly) raises
`AMBIGUOUS_IDENTIFIER`, because `t1.id = t3.id` is not directly present.
This narrows the PR's claim to the implemented scope rather than expanding the
feature to full equivalence-class tracking (which would be a much larger,
riskier change). No source behavior changes: this commit only adds regression
coverage to `04501_inner_join_key_column_not_ambiguous` for the transitive
chain (stays ambiguous) and for the sibling shape whose later `ON` equates the
exposed representative `t1.id` directly (resolves), and rewords the PR
description accordingly.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
@groeneai, please investigate these two CI failures on this PR and provide fixes in separate PRs. Both are unrelated to this change (it only touches
If a fix for either is already in progress, please link it here. The remaining two reds — |
|
Both are already owned, so no new PRs from me. 1. The culprit is the randomized #111502 pins 2.
That query is Fixes in progress: #110048 (yours, read consistency for renamed or exchanged tables - the root cause), #109747 (guard in partial-result evaluation), #111708 (the reporter misattribution itself, so the label stops hiding the real message). Do not conflate this STID with 5217-5b3d / 5217-505b on the ParallelReplicas coverage job: different root cause (unknown-stream throw), issue #110518, fix #111689. |
Address review: `innerJoinKeyColumnsAreEquated` only recognized `equals`, but a null-safe equality `<=>` (`isNotDistinctFrom`) is a join-key carrier for the planner as well - see the `equals`/`isNotDistinctFrom` handling in `PlannerJoins.cpp`. As a result `SELECT a FROM t1 INNER JOIN t2 ON t1.a <=> t2.a` still threw `AMBIGUOUS_IDENTIFIER` with `single_join_prefer_left_table = 0`, even though every surviving row carries the same value on both sides. `isNotDistinctFrom` is in fact a stronger guarantee than `equals` here: a surviving row has either the same non-NULL value or `NULL` on both sides, so resolving the unqualified reference to the left side is exactly as safe. Adds regressions to `04501_inner_join_key_column_not_ambiguous`: a direct `<=>` key, the same inside an `and` chain mixed with a plain `equals` conjunct, and a negative case pinning that a non-key column stays ambiguous under a null-safe join key.
…pe as ambiguous `innerJoinKeyColumnsAreEquated` matches an `equals`/`isNotDistinctFrom` of the two already resolved columns, so a reference written inside the very `ON` that equates it, as in `... INNER JOIN t3 ON id = t3.id`, is not covered: at that point that side of the equality is still the identifier being resolved, and picking a side would change the join condition itself (resolved to the right side it becomes trivially true), so the "both sides carry the same value" justification does not apply. Confirmed by experiment that this shape throws `AMBIGUOUS_IDENTIFIER` both in the multi-way form (regardless of `single_join_prefer_left_table`) and in the single-join form with `single_join_prefer_left_table = 0`, exactly as before this change; the single-join form with `single_join_prefer_left_table = 1` keeps working. All four cases are now pinned in `04501_inner_join_key_column_not_ambiguous`.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 31/33 (93.94%) · Uncovered code |
Resolves an unqualified column reference that binds to both sides of an
INNER JOINwhen the two columns are equated by a top-levelequalsconjunct of theONcondition, instead of throwingAMBIGUOUS_IDENTIFIER.Motivation: for
SELECT id FROM a INNER JOIN b ON a.id = b.idthe join guaranteesa.id = b.idon every output row, soidis not really ambiguous. Previously this only "worked" by accident whensingle_join_prefer_left_table = 1silently picked the left side for a single join; with the setting disabled, or in a multi-way join whose laterONequates the exposed column by name on both sides - e.g.... t1 INNER JOIN t2 ON t1.id = t2.id INNER JOIN t3 ON t1.id = t3.id, or a top-level identifier consumed by a laterUSING- the same query failed. The reference is now resolved to the left side - exactly the valuesingle_join_prefer_left_tablealready produced - so the change only turns a previously-thrown ambiguity into that same result and never changes a query that already succeeded.The relaxation is deliberately narrow and value-safe:
INNERjoins (an outer join can leave the non-preserved side default/NULL on unmatched rows);and(an equality underordoes not guarantee equal values);equalsof the two resolved columns in that join'sON(nota.id = b.id + 1, etc.). It does not chase a transitive equivalence class across the left subtree, so a multi-way chain whose laterONequates a key that is not the exposed left representative - e.g.... t1 INNER JOIN t2 ON t1.id = t2.id INNER JOIN t3 ON t2.id = t3.id, where the left subtree exposesidast1.idbut the secondONnamest2.id- stays ambiguous by design.ONthat equates it - e.g.... INNER JOIN t3 ON id = t3.id- is not covered and stays ambiguous as before: picking a side there would change the join condition rather than only the projected column (resolved to the right side the condition becomes trivially true). Such a reference keeps working only in the single-join case throughsingle_join_prefer_left_table = 1, which is untouched.02372_analyzer_join,02374_analyzer_join_usingand03716_multiple_joins_using_top_level_identifierdocumented the old behavior and are updated.04501_inner_join_key_column_not_ambiguousis added covering the new behavior, each guard rail (non-key columns,OR,LEFT JOIN, qualified/subcolumn identifiers, the transitive multi-way chain above, and a reference inside its ownONstill raiseAMBIGUOUS_IDENTIFIER), and the mixed-type equi-key semantics: the reference keeps the raw left type, identical to whatsingle_join_prefer_left_table = 1has always produced, in contrast to the common-type projection ofUSING.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
An unqualified reference to an
INNER JOINkey that is equated in theONcondition (e.g.SELECT id FROM a INNER JOIN b ON a.id = b.id) is no longer reported as an ambiguous identifier, since both sides are guaranteed to carry the same value.Documentation entry for user-facing changes
Version info
26.8.1.559(included in26.8and later)