Repository navigation
Resolve JOIN USING identifier from aliases defined in WITH and other clauses - #121975
Conversation
With `analyzer_compatibility_join_using_top_level_identifier = 1` the `USING` identifier was looked up among `SELECT` list aliases only, so an alias defined in the `WITH` clause was not found and the query failed with `UNKNOWN_IDENTIFIER`. The `WITH` section is removed from the query node before the join tree is resolved; keep its nodes in the scope and include their aliases in the lookup. Such a key is treated like a nested `SELECT` list alias for parallel replicas and remote shipping, since the rendered query loses it. Closes: #121401 Co-Authored-By: Claude Fable 5.1 <[email protected]>
The alias lookup for `analyzer_compatibility_join_using_top_level_identifier` refused an alias with several definitions even when all of them are the same expression, while the old analyzer accepted such queries, for example `WITH x + 1 AS id SELECT sum(x + 1 AS id) FROM t1 JOIN t2 USING (id)`. Compare the definitions and refuse only conflicting ones. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…py of the `WITH` section The scope already holds an unresolved clone of every aliased expression of the query, the `WITH` section included: identifiers are resolved through fresh clones of these entries, so they stay unresolved while the join tree is resolved. Use it for the `analyzer_compatibility_join_using_top_level_identifier` lookup instead of keeping the `WITH` nodes in the scope and re-collecting the `SELECT` list aliases per join. As a consequence an alias defined in any clause, not only in `WITH` or the `SELECT` list, is considered, which is what the old analyzer did: for example `SELECT sum(x) FROM t1 JOIN t2 USING (id) WHERE (x + 1 AS id) > 0` joins by `x + 1`. Test 04603 pinned the opposite for a `WHERE` alias and is updated. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [672ecfe] Summary: ✅
AI ReviewSummaryThis PR expands Findings❌ Blockers
Tests
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit 672ecfe.
Changed lines: Changed C/C++ lines covered: 60/61 (98.36%) · Uncovered code |
| /// A lambda alias must not become a USING column. | ||
| auto it = select_list_aliases->alias_name_to_expression_node.find(identifier_full_name_); | ||
| if (it == select_list_aliases->alias_name_to_expression_node.end()) | ||
| const auto & aliases = scope.aliases; |
There was a problem hiding this comment.
Pulling an unresolved node straight out of scope.aliases is only half of the fix here. The matched body is still re-resolved inside the synthetic subquery created a few lines below with parent_scope = nullptr, so chained query aliases are still lost. A query like WITH x + 1 AS a, a AS id SELECT sum(x) FROM (SELECT 1 AS x) t1 JOIN (SELECT 2 AS id) t2 USING (id) reaches this branch, clones a AS id, and then the synthetic subquery no longer has alias a, so it throws UNKNOWN_IDENTIFIER (or silently binds to a real a column if one exists). Since the PR now says aliases from WITH and other clauses are supported, I think this needs the current alias environment carried into that synthetic resolution path, plus a regression test for alias chaining.
| for (const auto & duplicated_node : aliases.nodes_with_duplicated_aliases) | ||
| { | ||
| if (duplicated_node->hasAlias() && duplicated_node->getAlias() == identifier_full_name_) | ||
| if (duplicated_node->hasAlias() && duplicated_node->getAlias() == identifier_full_name_ && !duplicated_node->isEqual(*it->second)) |
There was a problem hiding this comment.
This duplicate check is stricter than the normal duplicate-alias validation later in resolveQuery, because here we compare the unresolved trees directly. A case like WITH x + 1 AS id, x + 1 AS a, a AS id SELECT sum(x) FROM (SELECT 1 AS x) t1 JOIN (SELECT 2 AS id) t2 USING (id) will pick the first id, see the duplicate a AS id, and reject it here even though the end-of-query duplicate pass would resolve a to x + 1 and accept the definitions as identical. That contradicts the new "identical duplicated aliases are accepted" behavior from the PR description and tests, so this path needs to normalize/resolve both sides the same way as the canonical duplicate check.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 38 s compile time in total, 9 of them have a recent master baseline. |
# Conflicts: # src/Core/Settings.cpp
b451812 to
672ecfe
Compare
| for (const auto & duplicated_node : aliases.nodes_with_duplicated_aliases) | ||
| { | ||
| if (duplicated_node->hasAlias() && duplicated_node->getAlias() == identifier_full_name_) | ||
| if (duplicated_node->hasAlias() && duplicated_node->getAlias() == identifier_full_name_ && !duplicated_node->isEqual(*it->second)) |
There was a problem hiding this comment.
The duplicate-alias guard only returns nullptr, so this path can still fall through to the plain left-column lookup below. With analyzer_compatibility_join_using_top_level_identifier = 1, WITH x + 1 AS id, x + 2 AS id SELECT sum(x) FROM (SELECT 1 AS x, 5 AS id) t1 JOIN (SELECT 2 AS id) t2 USING (id) now joins by t1.id instead of refusing the ambiguous alias, even though the PR contract says differing duplicate aliases should still be rejected. This needs to suppress the left-table fallback (or throw immediately) once a conflicting non-top-level alias was found, and it needs a regression test with a real id column on the left side.
Closes: #121401
Related: #110739
With
analyzer_compatibility_join_using_top_level_identifier = 1theUSINGidentifier was looked up among the aliases of theSELECTlist only: top-level aliases and, since #110739, aliases nested inSELECTlist expressions. An alias defined in theWITHclause was not found, so the query from the issue failed withUNKNOWN_IDENTIFIERwhile the old analyzer accepted it:The
WITHsection is removed from the query node before the join tree is resolved, but the scope alias table already holds an unresolved clone of every aliased expression of the query,WITHincluded: identifiers are resolved through fresh clones of these entries, so they stay unresolved at that point. The lookup now uses that table after the top-levelSELECTlist check, instead of re-collecting theSELECTlist aliases per join. As a consequence an alias defined in any clause is considered, which is what the old analyzer did:SELECT sum(x) FROM t1 JOIN t2 USING (id) WHERE (x + 1 AS id) > 0joins byx + 1. Test 04603 pinned the opposite for aWHEREalias and is updated.Duplicated aliases are now refused only when their definitions differ. Identical definitions, for example
WITH x + 1 AS id SELECT sum(x + 1 AS id) FROM t1 JOIN t2 USING (id), were refused before and are accepted by the old analyzer.A key resolved this way is treated like a nested
SELECTlist alias for shipping: parallel replicas are disabled for the query, andDistributedtables andremotereject it on the initiator unless the key is also a top-levelSELECTlist column. The behaviour was compared against the old analyzer (26.3 withenable_analyzer = 0). The remaining known difference isprefer_column_name_to_alias = 1, which the old analyzer honoured insideUSINGand the compatibility setting never did, forSELECTlist aliases as well.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 now also resolved from an alias defined in theWITHclause or in another clause of the query, not only from theSELECTlist, and duplicated aliases with identical definitions no longer prevent the resolution. Closes #121401.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/121975]
Version info
26.10.1.1149-master(included in26.10and later)