Repository navigation
Fix MULTIPLE_EXPRESSIONS_FOR_ALIAS for distributed JOIN with same-named ALIAS columns - #108025
yakov-olkhovskiy wants to merge 4 commits into
Conversation
…ed ALIAS columns
When a Distributed query JOINs two tables that each define an `ALIAS` column with the same name and
the name is referenced in an expression clause (`WHERE`/`GROUP BY`/`ORDER BY`/`HAVING`/`JOIN ON`),
inlining the `ALIAS` columns for shard transport re-applied the bare column name as an alias on each
inlined expression. After unique table aliases are assigned the two different expressions
(`__table1.x` and `__table2.x`) ended up sharing one alias in the same scope, which the analyzer
rejects with `MULTIPLE_EXPRESSIONS_FOR_ALIAS`, e.g.:
SELECT l.id FROM a_dist AS l INNER JOIN b_dist AS r ON l.id = r.id
WHERE l.foo > 0 AND r.foo > 0; -- a.foo and b.foo are both `ALIAS x`
The inlined alias only needs to preserve the logical column name for top-level projection items (so
the mergeable-state output column keeps its name). Inside expression clauses and nested expressions
the alias is redundant and only creates the conflicting scope entry. The inliner now sets the alias
only for top-level projection items, and clones each inlined expression so per-occurrence aliasing
does not mutate shared nodes.
This is new-analyzer only.
Closes: #107990
Co-authored-by: Yakov Olkhovskiy <[email protected]>
|
|
|
Workflow [PR], commit [71bb389] Summary: ✅
AI ReviewSummaryThis PR consolidates the distributed Findings
Tests
Final VerdictStatus: Minimum required action: clear aliases recursively on cloned inlined expressions for non-projection use and add the focused nested-alias regression test. |
…join-same-name-alias-3628 Co-authored-by: Yakov Olkhovskiy <[email protected]>
… helper After merging master (which brought in #107913), there were two copies of the ALIAS-column inliner: the shard-transport inliner in `StorageDistributed` (introduced by this PR for #107990) and the name-computation inliner `InlineAliasColumnsForNamingVisitor` added by #107913 in `buildQueryTreeForShard`. Unify them: the projection-aware inliner now lives in `buildQueryTreeForShard` and is exposed as `inlineAliasColumnsForDistributed`. `StorageDistributed::buildQueryTreeDistributed` calls it, and the name-computation path (`actionNameAfterAliasInlining`) reuses the same no-alias inliner. Aliasing is irrelevant for name computation — action node names come from the column identifier / expression, not the SQL alias — so the shared inliner yields the same name the shard assigns. No behavior change: the duplicate-ALIAS column-count fix (#107913) and the same-named JOIN ALIAS collision fix (#107990) both keep working. Co-authored-by: Yakov Olkhovskiy <[email protected]>
| || column_source->getNodeType() == QueryTreeNodeType::ARRAY_JOIN) | ||
| return nullptr; | ||
|
|
||
| return column_node->getExpression()->clone(); |
There was a problem hiding this comment.
This still preserves aliases that were already present in the stored ALIAS expression. ClickHouse accepts definitions like foo UInt64 ALIAS (x AS v); after this clone, a non-projection use such as WHERE l.foo > 0 AND r.foo > 0 or JOIN ON l.foo = r.foo can still put two different expressions with alias v into the same remote-analysis scope, so the original MULTIPLE_EXPRESSIONS_FOR_ALIAS exception is still possible. Please clear aliases on the cloned expression before using it in expression clauses (recursively, with the same nested-scope boundaries as the analyzer alias cleanup), while still setting the output alias for the top-level projection case.
There was a problem hiding this comment.
Such aliases don't survive resolution - they only serve as references inside the same column's defining expression and don't leak into the outer scope. By the time the inliner clones getExpression() it's already the resolved, alias-free expression ((x AS v) -> COLUMN x, and even (x AS v) + v -> plus(x, x)), so there's no alias left to collide.
The PR covers same-named ALIAS columns referenced in JOIN ON, but the test only referenced them in WHERE/GROUP BY/ORDER BY/projection. Add a focused case (ON l.id = r.id AND l.foo > 0 AND r.foo > 0) so the join-tree branch of the inliner is exercised. Co-authored-by: Yakov Olkhovskiy <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 41/43 (95.35%) | Lost baseline coverage: none · Uncovered code |
Inlining an `ALIAS` column by mutating its stored expression and stamping the column's name on it puts
the same alias on two different bodies whenever two `JOIN` sides declare a same-named `ALIAS` column,
and the remote side then throws `MULTIPLE_EXPRESSIONS_FOR_ALIAS`. With the shipped query converted back
to SQL this surfaces through `ORDER BY`, which keeps both references:
ORDER BY __table1.v * 2 AS a_v DESC, __table2.v * 2 AS a_v DESC
Clone the defining expression per occurrence and set the alias only on top-level projection items, where
it is needed to keep the mergeable-state output column's name. References inside `WHERE` / `GROUP BY` /
`ORDER BY` / `HAVING` / `JOIN ON` are inlined bare. Nested subqueries and unions are recursed into as
queries so their own projections keep their names. This is the inliner from
#108025, which becomes redundant once this lands.
Closes: #107990
Co-authored-by: Yakov Olkhovskiy <[email protected]>
`04757` grows the shapes it was missing relative to #113364's own matrix: two `ALIAS` columns with identical bodies, `ARRAY JOIN` on either side, `GLOBAL RIGHT JOIN`, and `GLOBAL JOIN ... USING` both selecting the key and selecting an `ALIAS` column beside it. `04758` is the #107990 regression test from #108025, covering the same-named `ALIAS` column referenced from `JOIN ON` / `WHERE` / `GROUP BY` / `ORDER BY` / a projection expression / a `GLOBAL JOIN` subquery over real `Distributed` tables. It reports six `MULTIPLE_EXPRESSIONS_FOR_ALIAS` on master. `04759` is the laziness test from #113364: an `ALIAS` column of a shipped table expression must be computed once per row on every dispatch path. Inlining keeps that property, so the reference is identical to that pull request's. On master the `parallel_replicas_local_plan=1` arm throws instead of reporting a count. Co-authored-by: Yakov Olkhovskiy <[email protected]>
|
closing in favor of #107700 |
Closes: #107990
When a
Distributedquery JOINs two tables that each define anALIAScolumn with the same name, and that name is referenced in an expression clause (WHERE/GROUP BY/ORDER BY/HAVING/JOIN ON), the query failed during analysis withMULTIPLE_EXPRESSIONS_FOR_ALIAS:Root cause
For shard transport,
ALIAScolumns are inlined into their defining expressions and the bare column name was re-applied as an alias on every inlined expression. AftercreateUniqueAliasesIfNecessaryqualifies the table sources, the two structurally different expressions__table1.xand__table2.xboth carry the aliasfooin one scope, which the analyzer rejects.Fix
The inlined alias only needs to preserve the logical column name for top-level projection items (so the mergeable-state output column keeps its name). Inside expression clauses and nested expressions the alias is redundant and only creates the conflicting scope entry, so the inliner now sets the alias only for top-level projection items. Grouping/sorting/filtering still match the projection by expression (action node name), independent of the SQL alias. Each inlined expression is cloned so per-occurrence aliasing does not mutate shared nodes.
This is new-analyzer only.
Inliner consolidation
This PR also consolidates the ALIAS-column inliner into a single shared helper
inlineAliasColumnsForDistributedinbuildQueryTreeForShard. Previously the shard-transport inliner lived inStorageDistributedwhile a second copy (InlineAliasColumnsForNamingVisitor, added by #107913 for shard-header name computation) lived inbuildQueryTreeForShard. Both now use the one implementation; name computation reuses the no-alias inliner (aliasing is irrelevant for action node names). The duplicate-ALIAS column-count fix (#107913) keeps working — verified by04357_distributed_alias_same_expression.Tests
04401_distributed_alias_name_collisioncovers same-namedALIAScolumns referenced inWHERE,GROUP BY,ORDER BY, a projection expression, and aGLOBAL JOINsubquery (the original report), each checked for correct results.Related: #107913
Related: #79916
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
MULTIPLE_EXPRESSIONS_FOR_ALIASerror when querying aDistributedtable with aJOINwhere both tables have anALIAScolumn of the same name referenced inWHERE/GROUP BY/ORDER BY/JOIN ON.