Repository navigation
Fix crash when Merge table wraps Distributed with distributed_group_by_no_merge=1 - #100859
alexey-milovidov merged 4 commits into
Conversation
…y_no_merge=1 When a Merge table contains multiple underlying tables and one is a Distributed table, StorageMerge::getQueryProcessingStage() caps the processing stage to WithMergeableState. It then passes this capped stage to StorageDistributed::getQueryProcessingStage() which had an assertion requiring to_stage == Complete when distributed_group_by_no_merge=1. The assertion was too strict. With distributed_group_by_no_merge=1, each shard processes the full query independently and the initiator concatenates results, so StorageDistributed can always return Complete regardless of the requested to_stage. The caller (StorageMerge) correctly handles storage_stage > processed_stage. Closes ClickHouse#75604 Co-Authored-By: Claude Opus 4.6 <[email protected]>
Pre-PR Validation Gate (Session a42ef9e6)a) Deterministic repro? ✅ Yes — b) Root cause explained? ✅ c) Fix matches root cause? ✅ Removes the overly-strict assertion. StorageDistributed now unconditionally returns d) Test intent preserved? ✅ This is a source code fix. The regression test ( e) Both directions demonstrated? ✅
|
|
cc @azat @alexey-milovidov — could you review this? It fixes a crash when a Merge table wraps a Distributed table with distributed_group_by_no_merge=1. |
|
Workflow [PR], commit [7839867] Summary: ✅ AI ReviewSummaryThis PR fixes an exception in Missing context
ClickHouse Rules
Final Verdict
|
|
@azat Thanks for the feedback — noted for the future. In this case the merge was to pick up PR #100867 (fix for Could you take a look at the actual fix when you have a chance? The core change is small: removing the overly-strict assertion in |
LLVM Coverage Report
Changed lines: 100.00% (2/2) | lost baseline coverage: 3 line(s) · Uncovered code |
| /// The caller may request a lower stage (e.g. StorageMerge passes | ||
| /// WithMergeableState when it wraps multiple tables), but that's fine — | ||
| /// the caller handles storage_stage > processed_stage correctly. | ||
| return QueryProcessingStage::Complete; |
There was a problem hiding this comment.
This is not enough, Merge also should be aware - #105330
There was a problem hiding this comment.
Agreed, my fix only covered same-type aggregates (count, sum) where the WithMergeableState and Complete column types coincide. Your follow-up correctly handles the remaining nested-Merge case where state-bearing aggregates (uniqExact, avg, groupArray, ...) hit CANNOT_CONVERT_TYPE.
Both halves look right:
- StorageMerge single-table branch capping at WithMergeableState mirrors the multi-table cap on the same line.
- std::max(to_stage, Complete) in StorageDistributed avoids silently downgrading WithMergeableStateAfterAggregation(/AndLimit) callers, which my original Complete return swallowed.
Test 04256 is a strict superset of 04064. Happy to help review #105330.
FYI: 01227_distributed_merge_global_in_primary_key just failed on your distributed-plan CI jobs. It looks like a pre-existing flake on that build family: 5 hits on #102192, 2 hits on #100226 over 30 days, 0 on master. Probably not yours.
…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.
When a
Mergetable (or themerge()table function) references multiple underlying tables, and one of them is aDistributedtable, queries crash with aLOGICAL_ERRORifdistributed_group_by_no_merge=1is set:Root cause:
StorageMerge::getQueryProcessingStage()caps the processing stage toWithMergeableStatewhen there are multiple underlying tables (line 415). WhencreatePlanForTable()then callsStorageDistributed::getQueryProcessingStage()with this cappedto_stage=WithMergeableState, the assertion at line 516 fires because it requiresto_stage == Complete.Fix: The assertion was too strict. With
distributed_group_by_no_merge=1, each shard processes the full query independently and the initiator concatenates results.StorageDistributedcan always returnCompleteregardless of the requestedto_stage— the caller (StorageMerge) correctly handlesstorage_stage > processed_stagevia the condition at line 1273.Reproducer (from issue):
Closes #75604
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix crash (
LOGICAL_ERROR) when querying aMergetable (ormerge()table function) that wraps multiple tables including aDistributedtable, withdistributed_group_by_no_merge=1enabled.Documentation entry for user-facing changes
Version info
26.4.1.671