Repository navigation
Fix an ALIAS column read as a default through Merge over Distributed - #119561
Conversation
…uted`
An `ALIAS` column whose expression is a bare reference to another column read
back as the default value of its type through a `Merge` table over a
`Distributed` table with a remote shard:
CREATE TABLE t (a UInt64, b UInt64 ALIAS a, c UInt64 ALIAS b, d UInt8 ALIAS a > 0) ENGINE = MergeTree ORDER BY tuple();
CREATE TABLE t_dist AS t ENGINE = Distributed(test_cluster_two_shards, currentDatabase(), t);
CREATE TABLE t_merge AS t ENGINE = Merge(currentDatabase(), '^t_dist$');
SELECT a, b, c, d FROM t_dist; -- 1 1 1 1
SELECT a, b, c, d FROM t_merge; -- 1 0 0 0
The whole query is delegated to the shards there, and `Distributed` inlines the
`ALIAS` columns of the child table into their expressions before sending it, so
`b UInt64 ALIAS a` selected next to `a` becomes the same expression as `a` and
the child's `ActionsDAG` deduplicates the two into one column. The name of the
collapsed column is gone from the child's header, and neither the name nor the
position matching in `convertAndFilterSourceStream` can rebuild it, so
`addMissingDefaults` filled the column `Merge` was still expecting with the
default value of its type. An `ALIAS` column with a function in it survived,
because its expression has an action name of its own and nothing to collapse
onto - until a bare alias was selected next to it and the whole reconciliation
fell back to defaults.
Fan the collapsed columns back out before the reconciliation, with
`buildShardCollapseFanOut` - the same helper the planner already uses for a
direct read of a `Distributed` table, which is why reading `t_dist` itself was
correct all along.
Closes: #119374
|
Workflow [PR], commit [577d1e0] Summary: ✅
AI ReviewSummaryThis PR fixes the header-reconciliation bug for plain Findings❌ Blockers
Tests
Final VerdictChanges requested: the patch fixes the projection/header mismatch, but grouped LLVM Coverage ReportMeasured on commit 577d1e0.
Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code |
| /// The translation between an `ALIAS` column's own name and its inlined expression is read from | ||
| /// the outer query tree and its planner context: those are what the expected `header` is named | ||
| /// after, and the child's derived planner context holds no column identifiers of its own. | ||
| if (auto fan_out_actions_dag = buildShardCollapseFanOut( |
There was a problem hiding this comment.
This only repairs the header shape. The direct Distributed path also feeds the duplicate -> representative map from buildShardCollapseFanOut into addMergingAggregatedStep, because once the shard has collapsed GROUP BY a, b with b ALIAS a to one key it also buckets its partial states by that collapsed key set.
Here ReadFromMerge fans b back out, but the aggregation merge above this step still sees both keys, so under two-level aggregation it can bucket the same logical group differently from the shard and never merge the states. Unsafe GROUPING SETS shapes stay affected for the same reason. The new test only exercises the single-key GROUP BY b case, which does not hit this.
I think this needs the same metadata flow as the plain Distributed path (shard_collapse_duplicate_keys in Planner.cpp), or shipped aggregation has to be disabled for this shape. A focused repro would be a Merge-over-Distributed copy of the two-level GROUP BY a, b / GROUPING SETS cases from 04357_distributed_alias_same_expression.sql.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units16 translation units recompiled, 123 s compile time in total, 16 of them have a recent master baseline. |
With `optimize_respect_aliases = 0` the old analyzer refuses a `Merge` table over a `Distributed` table with `ALIAS` columns outright - `Missing columns: 'b' 'c' 'd'` - because it asks the `Merge` table for the source column alone while the query still references the alias columns. That happens on master as well, so the old-analyzer control of this test pins the setting instead of tripping over it when the randomized-settings run turns it off.
Reproducer-- on a server with the standard test clusters (test_cluster_two_shards has a remote shard)
CREATE TABLE t (a UInt64, b UInt64 ALIAS a) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t VALUES (1);
CREATE TABLE t_dist AS t ENGINE = Distributed(test_cluster_two_shards, currentDatabase(), t);
CREATE TABLE t_merge AS t ENGINE = Merge(currentDatabase(), '^t_dist$');
SELECT a, b FROM t_dist ORDER BY a LIMIT 1; -- 1 1
SELECT a, b FROM t_merge ORDER BY a LIMIT 1; -- 1 0 <-- ALIAS column b reads back as the defaultResults:
Backport the fix to CC component owner: @alexey-milovidov @CurtizJ Analysis metadata
Introducing change: not identified
|
… EXPLAIN or a subquery A correlated subquery is planned with its own `GlobalPlannerContext`, and `collectTableExpressionDataForCorrelatedColumns` stores raw pointers to the enclosing query's `TableExpressionData` in it, without keeping the enclosing `PlannerContext` alive. Every context derived from the subquery falls back to that map when it looks up the correlated column's source. `ReadFromMerge` plans its source tables lazily (from `applyFilters` during `QueryPlan::optimize`, or from `initializePipeline`). For a non-empty source table narrower than the `Merge` header, `convertAndFilterSourceStream` builds the fan-out added in ClickHouse#119561 and names the correlated column through that map. `EXPLAIN`, a subquery in `FROM`, a view and an `IN` subquery destroy the enclosing `Planner` before that point, so the lookup reads freed memory (AST fuzzer, arm_asan_ubsan, STID 4877-28b1). `GlobalPlannerContext` now holds a `PlannerContextPtr` to every planner context whose data it points into. It is the only writer of the map, so this covers all readers and every place that drops the enclosing planner early, without changing each of them. No reference cycle: the subquery's plan is united into the enclosing `QueryPlan`, never stored in the enclosing `PlannerContext` (correlated subqueries are neither `IN` set sources nor materialized CTEs). Related: ClickHouse#115238 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
An
ALIAScolumn whose expression is a bare reference to another column read back as the default value of its type through aMergetable over aDistributedtable with a remote shard:The whole query is delegated to the shards there, and
Distributedinlines theALIAScolumns of the child table into their expressions before sending it, sob UInt64 ALIAS aselected next toabecomes the same expression asaand the child'sActionsDAGdeduplicates the two into one column. The name of the collapsed column is gone from the child's header, and neither the name nor the position matching inconvertAndFilterSourceStreamcan rebuild it, soaddMissingDefaultsfilled the columnMergewas still expecting with the default value of its type. AnALIAScolumn with a function in it (d UInt8 ALIAS a > 0) survived, because its expression has an action name of its own and nothing to collapse onto — until a bare alias was selected next to it and the whole reconciliation fell back to defaults.The fix fans the collapsed columns back out before the reconciliation, with
buildShardCollapseFanOut— the same helper the planner already uses for a direct read of aDistributedtable, which is why readingt_distitself was correct all along.One divergence in this area is older than this change and not addressed by it: when a child table defines the alias differently from the
Mergetable (b ALIAS a * 100in the child,b ALIAS ain theMergetable), a delegated stage evaluates theMergetable's definition, while reading the same tables throughMergeat theFetchColumnsstage evaluates each child's own. Before this change that shape returned the default value as well.Closes: #119374
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix an
ALIAScolumn that references another column (b UInt64 ALIAS a) reading back as the default value of its type when the table is read through aMergetable over aDistributedtable with a remote shard.Workflow [PR]
Sync PR [sync-upstream/pr/119561]
Version info
26.9.1.1276(included in26.9and later)