Repository navigation
Fix MULTIPLE_EXPRESSIONS_FOR_ALIAS on SELECT * over JOIN under parallel replicas - #104303
devcrafter wants to merge 15 commits into
Conversation
…llel replicas Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Workflow [PR], commit [184113a] Summary: ❌
AI ReviewSummaryThis PR targets Findings❌ Blockers
Final VerdictStatus:
|
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Address reviewer feedback by moving the `PREWHERE` half of the fix out of `queryNodeToDistributedSelectQuery` and into the analyzer itself. The original PR postprocessed the AST after `queryNodeToDistributedSelectQuery` to clear aliases from constraint clauses. Reviewers (ClickHouse#103806 review threads at `Utils.cpp:214`, `:223`, `:267`) flagged the approach as a "mechanical fixup of something already broken" and asked for a deeper fix in the query tree. This commit implements that. Root cause (traced in `QueryAnalyzer::resolveQuery`): * When resolving `PREWHERE cond` where `cond` is a projection alias, the analyzer deep-clones the alias body and registers the clone's root in `scope.aliases.node_to_remove_aliases` (see line ~1135 of `QueryAnalyzer.cpp`). The clone's *descendants*, which carry the inner aliases (e.g. `cond1`, `cond2`) from the original projection body, are not registered. * `PREWHERE` then deep-clones the tree a second time so that `ReplaceColumnsVisitor` can rewrite types for `JOIN USING` without leaking back into the projection. After this clone the originally- registered node is no longer in the query tree, while the new clone's root carries the `cond` alias and is unregistered. * The cleanup loop at line ~5840 calls `removeAlias` on each registered node non-recursively, so the projection's inner aliases get cleared while `PREWHERE`'s do not. When the AST is shipped to a remote replica under `parallel_replicas_local_plan = 0` the receiver sees the same alias bound to two different bodies and throws `MULTIPLE_EXPRESSIONS_FOR_ALIAS`. Fix: 1. Make alias removal recursive in `QueryAnalyzer.cpp`: walk each registered node top-down and clear aliases on every descendant, stopping at `QUERY` / `UNION` / `LAMBDA` boundaries (those are separate scopes that handle their own cleanup). 2. Register the post-clone `prewhere_node` in `node_to_remove_aliases` so the recursive walk reaches its inner aliases. `Utils.cpp` no longer touches `PREWHERE` / `WHERE` / `HAVING` / `QUALIFY`. The empirical check in the issue thread showed only `PREWHERE` ever triggered the asymmetry; the rest of the constraint clauses were dead code in the postprocess and are dropped together with it. The duplicate-projection-alias dedup remains in `queryNodeToDistributedSelectQuery` for now: it covers case 2 of the regression test (`SELECT *` over a self-join with overlapping column names) and a different root cause is being addressed in ClickHouse#104303 by @devcrafter. Once that lands the dedup here can be removed. Verified locally with the new binary against the regression test `04145_multiple_expressions_for_alias_parallel_replicas` and the original repro from ClickHouse#74324; both pass. Existing alias tests (`00057_join_aliases`, `00138_table_aliases`, `00157_aliases_and_lambda_formal_parameters`) remain green. Refs ClickHouse#74324 Refs ClickHouse#103806 (comment) Refs ClickHouse#104303 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
LLVM Coverage Report
Changed lines: 95.00% (19/20) · Uncovered code |
novikd
left a comment
There was a problem hiding this comment.
It'd be better to find out the reason for such kind of clushes.
| auto [it, inserted] = first_column_by_name.try_emplace(column_name, current_column); | ||
| if (!inserted && it->second->getColumnSourceOrNull() != current_column->getColumnSourceOrNull()) | ||
| continue; |
There was a problem hiding this comment.
It does not look like a good solution because it does not fix the query sent, but it "supresses" an error on the shard.
What is the reason that different expressions have the same aliases?
There was a problem hiding this comment.
It doesn't suppress error on the shard, it doesn't allow generating the same aliases for different columns.
There was a problem hiding this comment.
The another question - why should we generate here aliases?
There was a problem hiding this comment.
projection_columns aliases are currently dropped for every query here because the whole alias-assignment block is disabled.
That changes more than the SELECT */JOIN collision case. projection_columns is also where override aliases are stored (for example, subquery column alias syntax via setProjectionAliasesToOverride). Those names are not guaranteed to exist as node aliases, so without this block they are lost during QueryNode -> AST serialization.
As a result, distributed/parallel-replica re-analysis can observe different projection names than the analyzed query tree contract.
Can we keep alias emission and only suppress it for the conflicting duplicate-source case, instead of disabling aliases globally?
| ast_with_alias->setAlias(projection_columns[i].name); | ||
| } | ||
| } | ||
| // if (!projection_columns.empty()) |
There was a problem hiding this comment.
projection_columns alias emission is disabled for all projections here because the entire block is commented out.
That changes behavior beyond the SELECT */JOIN collision case: alias override paths (for example setProjectionAliasesToOverride used for subquery column alias lists and generated unique aliases) still populate projection_columns, but those names are no longer serialized into the dispatched AST. Remote re-analysis can then observe different projection names than the analyzed query tree.
Can we restore alias emission and only suppress AS <name> for the narrow conflicting duplicate-source ColumnNode case?
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
MULTIPLE_EXPRESSIONS_FOR_ALIASwhen serializing aQueryNodewhose projection has duplicated column names (e.g.SELECT *over a JOIN) for distributed dispatch.Partial progress on #74324.
QueryNode::toASTImplalways emitsAS <name>for every projection. AftercreateUniqueAliasesIfNecessaryqualifies tables as__tableN, aSELECT *over a JOIN produces__table1.a AS a, __table3.a AS a— duplicate alias, two different expressions — which the analyzer rejects when re-resolving the dispatched AST on a remote replica.Fix: skip the alias only when the projection column name is duplicated within the same projection.
Verification (debug build, parallel replicas enabled):
04209_pr_join_using_alias_collision: 100/10000597_push_down_predicate_long: 100/100 (removed fromparallel_replicas_blacklist.txt)02559_multiple_read_steps_in_prewherewas also listed under #74324 but fails on a differentPREWHEREalias-substitution pattern; left blacklisted.