Repository navigation
Fix CANNOT_CONVERT_TYPE for Merge over Merge over Distributed with distributed_group_by_no_merge=1 - #105330
Conversation
|
Workflow [PR], commit [cbfb4a1] Summary: ❌
AI ReviewSummaryThis PR fixes stage negotiation for nested Final VerdictStatus: ✅ Approve |
…stributed_group_by_no_merge=1 ClickHouse#100859 made multi-table `Merge` over `Distributed` with `distributed_group_by_no_merge=1` work for all aggregates: the `processed_stage <= storage_stage` rescue in `ReadFromMerge::createPlanForTable` takes the if-branch and calls `storage->read` with `processed_stage = WithMergeableState`, so the shards return states and they line up with the Merge's `common_header`. But that rescue is bypassed when the Merge child is itself a `StorageMerge` (the `must_return_interpreter_select_query_plan` flag forces the else-branch unconditionally). With the shape: outer Merge (multi-table - caps its own stage at WithMergeableState) -> inner Merge (single Distributed child - no cap on single-table branch) -> Distributed with distributed_group_by_no_merge=1 (now returns Complete instead of throwing) the inner `Merge` propagated `Complete` upward, the nested interpreter emitted finalized columns, and `convertAndFilterSourceStream` then tried to CAST e.g. `UInt64` to `AggregateFunction(uniqExact, Int)`, which the generic CAST does not support - failing with `CANNOT_CONVERT_TYPE` for state-bearing aggregates (`uniqExact`, `avg`, `groupArray`, `quantile`, ...). Two fixes: - `StorageMerge::getQueryProcessingStage`: cap the single-table branch at `WithMergeableState` when the caller asked for `WithMergeableState` or less. Mirrors the multi-table cap already present on the same line. - `StorageDistributed::getQueryProcessingStage`: return `std::max(to_stage, Complete)` instead of plain `Complete`, so a caller asking for `WithMergeableStateAfterAggregation(/AndLimit)` is not silently downgraded. Test 04256 covers the nested-Merge case and the state-bearing aggregates that 04064 missed.
…State The previous commit capped the single-table branch of `StorageMerge::getQueryProcessingStage` at `WithMergeableState` whenever `to_stage <= WithMergeableState`. That `<=` also catches `FetchColumns`. For single-table `Merge` over a single-shard `Distributed`, the inner `StorageDistributed::getQueryProcessingStage` returns `Complete` for any `to_stage != WithMergeableState` (the `nodes == 1` branch). So when the query-plan path asks the leaf `Merge` for `FetchColumns`, the old cap rewrote the stage up to `WithMergeableState` instead of leaving it at `Complete`. With `serialize_query_plan=1` that routes the child onto the path that keeps the analyzer-qualified `__table1.name` in the output header, and the converting step then looks up `name` and throws: Cannot find column `name` in source stream, there are only columns: [__table1.name]. (THERE_IS_NO_COLUMN) This broke 01227_distributed_merge_global_in_primary_key under the distributed-plan test variant, e.g. the per-shard subquery: SELECT `__table1`.`name` AS `name` FROM `test_distributed_merge` AS `__table1` The cap is only needed when the caller asked for exactly `WithMergeableState` (the nested-Merge case from 04256, where the outer multi-table `Merge` reads its children at `WithMergeableState` and a `distributed_group_by_no_merge=1` child reports `Complete`). For `FetchColumns` the caller wants raw columns and the child's `Complete` is fine - the previous (pre-cap) 01227 behavior. Narrow `<=` to `==`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
3d005b1 to
cbfb4a1
Compare
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 6/6 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
|
66c94e6
|
Hi — could this be a backport candidate for Affected code: Why this might apply: The CANNOT_CONVERT_TYPE failure is reachable only after #100859 relaxed the gbnm=1 assertion so Distributed returns Complete; #100859 merged 2026-04-08 (git show bc855b9), so only release branches cut after that date (26.4 released 2026-05-05, 26.5 released 2026-05-21) contain the affected code path. Branches 26.1-26.3 predate #100859 and are not affected. Other supported branches ( Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
CANNOT_CONVERT_TYPEforMergeoverMergeoverDistributedwithdistributed_group_by_no_merge=1#100859 made multi-table
MergeoverDistributedwithdistributed_group_by_no_merge=1work for all aggregates: theprocessed_stage <= storage_stagerescue inReadFromMerge::createPlanForTabletakes the if-branch and callsstorage->readwithprocessed_stage = WithMergeableState, so the shards return states and they line up with the Merge'scommon_header.But that rescue is bypassed when the Merge child is itself a
StorageMerge(themust_return_interpreter_select_query_planflag forces the else-branch unconditionally). With the shape:outer Merge (multi-table - caps its own stage at WithMergeableState)
-> inner Merge (single Distributed child - no cap on single-table branch)
-> Distributed with distributed_group_by_no_merge=1
(now returns Complete instead of throwing)
the inner
MergepropagatedCompleteupward, the nested interpreter emitted finalized columns, andconvertAndFilterSourceStreamthen tried to CAST e.g.UInt64toAggregateFunction(uniqExact, Int), which the generic CAST does not support - failing withCANNOT_CONVERT_TYPEfor state-bearing aggregates (uniqExact,avg,groupArray,quantile, ...).Two fixes:
StorageMerge::getQueryProcessingStage: cap the single-table branch atWithMergeableStatewhen the caller asked forWithMergeableStateor less. Mirrors the multi-table cap already present on the same line.StorageDistributed::getQueryProcessingStage: returnstd::max(to_stage, Complete)instead of plainComplete, so a caller asking forWithMergeableStateAfterAggregation(/AndLimit)is not silently downgraded.Test 04256 covers the nested-Merge case and the state-bearing aggregates that 04064 missed.
Version info
26.6.1.922