Repository navigation
Resolve a JOIN USING key from an alias only for the outermost JOIN - #123103
Conversation
|
Workflow [PR], commit [6f6ce01] Summary: ✅
AI ReviewSummaryThis PR narrows Findings❌ Blockers
Final Verdict
LLVM Coverage ReportMeasured on commit 6f6ce01.
Changed lines: Changed C/C++ lines covered: 68/70 (97.14%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes5 object files changed (+33.02 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units8 translation units recompiled, 31 s compile time in total, 8 of them have a recent master baseline. |
5a0c275 to
8dd6861
Compare
| const auto & resolved_nodes = left_subquery->getProjection().getNodes(); | ||
| if (resolved_nodes.size() == 1) | ||
| auto & projection_node = left_subquery->getProjection().getNodes().front(); | ||
| QueryExpressionsAliasVisitor alias_visitor(left_subquery_scope.aliases); |
There was a problem hiding this comment.
alias_visitor only collects aliases nested inside projection_node, but this fresh left_subquery_scope never inherits the rest of the query's alias tables. That breaks chained aliases that the setting says should work from "the query", for example WITH x + 1 AS a, a + 1 AS b SELECT count() FROM (SELECT 0 AS x) t1 JOIN (SELECT 2 AS b) t2 USING (b) or SELECT x + 1 AS a, a + 1 AS b FROM (SELECT 0 AS x) t1 JOIN (SELECT 2 AS b) t2 USING (b): the cloned b expression still contains identifier a, and resolveExpressionNode here can only see the left join tree, so it throws UNKNOWN_IDENTIFIER.
I think the probe still needs to inherit the enclosing alias / WITH environment (or resolve through a child scope of the original query) before resolving the cloned expression.
With `analyzer_compatibility_join_using_top_level_identifier` the alias lookup ran for every `JOIN` of the query. A projection alias that refers to the right side of an inner `JOIN`, such as `if(m.symbol = '', b.symbol, m.symbol) AS symbol` over `FROM m FULL JOIN b USING (symbol) FULL JOIN dttm ON 1 = 1`, therefore failed with `UNKNOWN_IDENTIFIER`. The old analyzer rewrote a query with several joins into nested subqueries, so an inner `JOIN` never saw such an alias and resolved the key from its left table; only the outermost `JOIN` does now. The alias probe also re-resolved the already resolved left join tree, so an inner `USING` join below an aliased outermost `USING` key failed with `USING clause expected identifier`; the `USING` branch of `resolveJoin` returns early for a list that is already resolved. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…tree again `try_resolve_identifier_from_query_projection` built a query whose join tree was the already resolved left side of the `JOIN` and resolved that query. An inner `USING` join below an aliased outermost `USING` key therefore failed with `USING clause expected identifier`: its `USING` list held columns by then. The probe now keeps a fresh scope over the left side, takes over the table expression data of its leaves from the current scope and resolves only the aliased expression with `resolveExpressionNode`. Nothing is resolved twice, so the early return for an already resolved `USING` list is gone. The query node of the probe stays as the scope of the resolution: an unqualified matcher inside the expression, such as `tuple(*) AS k`, finds the left side only through the nearest query scope, which the test now pins. The outermost `JOIN` check is evaluated on demand, and the comments no longer claim the old analyzer used an alias for the outermost `JOIN` of a multi-join query: it rejected that shape altogether, so only an inner `JOIN` resolving its key from the left table is old-analyzer behaviour. Co-Authored-By: Claude Fable 5.1 <[email protected]>
8296ff4 to
590de1f
Compare
…d resolve it as in `PREWHERE` The probe of `try_resolve_identifier_from_query_projection` resolves the aliased expression in a fresh scope over the already resolved left side, but the scope held only the table expression data of the leaves. A qualified matcher finds its table only through `alias_name_to_table_expression_node`, so `tuple(t1.*) AS k ... USING (k)` failed with `Qualified matcher t1.* does not find table`, even for a single `JOIN`. The scope now also registers the aliases of the leaves and counts `joins_count` and `using_joins_count` over the left side, as `resolveQuery` does for a join tree. The synthesized key is an ALIAS column of its source table and is computed right after that table is read, before the joins of the left side. A column of a `USING` key inside the left side therefore has to keep the type it is read with: resolving `t1.a` with the supertype of an inner `USING (a)` raised a `LOGICAL_ERROR` (`Unexpected return type from tuple`) when the alias action ran on the read column. The probe scope sets `allow_resolve_from_using = false`, the mechanism `PREWHERE` uses for the same reason, next to the existing `join_use_nulls = false`. As in `PREWHERE`, an unqualified matcher that expands the key column of the inner `USING` join from both of its tables has no single source table and does not become a key. Co-Authored-By: Claude Fable 5.1 <[email protected]>
`CollectPrewhereTableExpressionVisitor` did not descend into `LAMBDA`, so a column read only inside a lambda body escaped the check that PREWHERE reads one table. Once an unqualified matcher in a lambda expands like the direct one, `CROSS JOIN`, `JOIN ... ON` and `PASTE JOIN` put the right table's columns there, and the query failed later with `NOT_FOUND_COLUMN_IN_BLOCK` or a `Cannot capture column` logical error instead of `ILLEGAL_PREWHERE`. Lambda parameters are skipped by their `LAMBDA_ARGS` source, as `CollectSourceColumnsVisitor` does. 04647 section 12e compared a lambda matcher in PREWHERE with explicit columns of the merged USING key, which the lambda form matched only because of the scope bug fixed in this PR. Its oracle is now the same matcher written directly in PREWHERE. In 04683 the expansion under `analyzer_compatibility_join_using_top_level_identifier` follows master after ClickHouse#123103. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #123103 to 26.3: Resolve a JOIN USING key from an alias only for the outermost JOIN
… for the outermost JOIN
Cherry pick #123103 to 26.8: Resolve a JOIN USING key from an alias only for the outermost JOIN
… for the outermost JOIN
Cherry pick #123103 to 26.9: Resolve a JOIN USING key from an alias only for the outermost JOIN
… for the outermost JOIN
Backport #123103 to 26.8: Resolve a JOIN USING key from an alias only for the outermost JOIN
Backport #123103 to 26.9: Resolve a JOIN USING key from an alias only for the outermost JOIN
Backport #123103 to 26.3: Resolve a JOIN USING key from an alias only for the outermost JOIN
Related: #121975
Stacked on #121975, which this branch is based on.
With
analyzer_compatibility_join_using_top_level_identifier = 1the alias lookup for aJOIN USINGidentifier ran for everyJOINof the query. When a projection alias coincides with theUSINGkey of an innerJOINand its expression refers to the right side, the key could not be resolved:The old analyzer, which the setting emulates, rewrote a query with several joins into nested subqueries (
JoinToSubqueryTransformVisitor). An innerJOINtherefore never saw an alias of the outer query and resolved itsUSINGkey from the left table; the query above returns a row there (checked on 26.3 withenable_analyzer = 0). An aliasedUSINGkey on the outermostJOINof such a query was an error in the old analyzer, while the new analyzer has supported it since03716_multiple_joins_using_top_level_identifier; that stays as is.resolveJoinnow consults the aliases only for theJOINat the root of the query's join tree (ARRAY JOINnodes above it do not count), and innerJOINs resolve the key from their left table. The hint about the setting in theUNKNOWN_IDENTIFIERmessage is limited to the same case. An innerJOINwhoseUSINGkey currently resolves from an alias will resolve from the left column instead, or fail when there is none, which is what the old analyzer did.The alias probe also re-resolves the already resolved left join tree, so an inner
USINGjoin below an aliased outermostUSINGkey failed on master as well:The
USINGbranch ofresolveJoinreturns early for a list that already holds columns, the same way theONbranch is idempotent through resolved expressions.Not changed: with an
ARRAY JOINon the left side of theJOINthe alias is not applied at all (04002_join_using_top_level_array_join,04075_array_join_using_compatibility_setting), although the old analyzer applied it there.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
With
analyzer_compatibility_join_using_top_level_identifier = 1, aJOIN USINGidentifier is resolved from an alias of the query only for the outermostJOIN; an innerJOINof a query with several joins resolves it from its left table, as the old analyzer did. This fixesUNKNOWN_IDENTIFIERfor queries such asSELECT if(m.symbol = '', b.symbol, m.symbol) AS symbol FROM m FULL JOIN b USING (symbol) FULL JOIN dttm ON 1 = 1, and aUSING clause expected identifiererror when an innerUSINGjoin is placed below an aliased outermostUSINGkey.Close #121591
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123103]
Version info
26.10.1.1665-master(included in26.10and later)26.9.30.8,26.8.38.11,26.3.66.4