Skip to content

Fix CANNOT_CONVERT_TYPE for Merge over Merge over Distributed with distributed_group_by_no_merge=1 - #105330

Merged
azat merged 2 commits into
ClickHouse:masterfrom
azat:fix-merge-distributed-group-by-no-merge-nested
Jun 17, 2026
Merged

azat merged 2 commits into
ClickHouse:masterfrom
azat:fix-merge-distributed-group-by-no-merge-nested

Conversation

@azat

@azat azat commented May 19, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix CANNOT_CONVERT_TYPE for Merge over Merge over Distributed with distributed_group_by_no_merge=1

#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.

Version info

  • Merged into: 26.6.1.922

@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cbfb4a1]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug, targeted, old_compatibility) FAIL
Logical error: Not-ready Set is passed as the second argument for function 'A (STID: 0250-41a5) FAIL cidb, issue
Stateless tests (amd_msan, WasmEdge, parallel, 2/2) FAIL
04337_quorum_insert_honors_time_limit FAIL cidb

AI Review

Summary

This PR fixes stage negotiation for nested Merge over Distributed queries with distributed_group_by_no_merge=1, so an outer Merge expecting AggregateFunction states is not fed finalized values from a nested single-table StorageMerge. The current patch also preserves the later aggregation stages requested by callers of StorageDistributed and adds a focused stateless regression for state-bearing aggregates. I did not find any unresolved correctness, safety, or coverage issue that warrants an inline review comment.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 19, 2026
@antonio2368 antonio2368 self-assigned this Jun 2, 2026
@azat
azat enabled auto-merge June 4, 2026 12:06
Comment thread src/Storages/StorageDistributed.cpp Resolved
azat and others added 2 commits June 16, 2026 17:52
…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]>
@azat
azat force-pushed the fix-merge-distributed-group-by-no-merge-nested branch from 3d005b1 to cbfb4a1 Compare June 16, 2026 16:38
@clickhouse-gh

clickhouse-gh Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.40% 77.40% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 6/6 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@azat

azat commented Jun 17, 2026

Copy link
Copy Markdown
Member Author

@azat
azat added this pull request to the merge queue Jun 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 17, 2026
@azat
azat added this pull request to the merge queue Jun 17, 2026
Merged via the queue into ClickHouse:master with commit 66c94e6 Jun 17, 2026
163 of 166 checks passed
@azat
azat deleted the fix-merge-distributed-group-by-no-merge-nested branch June 17, 2026 14:07
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 17, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — could this be a backport candidate for 26.5, 26.4? No backport label was found, and the affected code appears to exist in those branches based on a file/function presence check.

Affected code: StorageMerge::getQueryProcessingStage and StorageDistributed::getQueryProcessingStage — introduced in 26.4.

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 (26.3, 26.2, 26.1) predate 26.4 and do not contain this code.

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 pr-must-backport or a version label like v26.5-must-backport.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants