Skip to content

Do not run distributed-plan rewrites on unsplit plans - #114753

Merged
davenger merged 3 commits into
masterfrom
continue-pr-113636
Aug 14, 2026
Merged

davenger merged 3 commits into
masterfrom
continue-pr-113636

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

Supersedes #113636 and ports Alexey Milovidov's fix onto current master.

Avoid materializing set-operation constants in distributed-plan candidates without logical exchanges, and normalize a Buffer read when its distributed destination materializes constants. This preserves matching headers for Merge children.

Related: #113636

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 a logical error (Block structure mismatch) when reading a Merge table with make_distributed_plan enabled over children with internal set operations.

alexey-milovidov and others added 3 commits August 14, 2026 03:33
A child plan of `ReadFromMerge` is united into the parent pipeline in the
same process and is never serialized as a distributed fragment. With
`make_distributed_plan` enabled, `QueryPlan::buildQueryPipeline` (and the
`optimize` calls on the child plans) still ran distributed-plan-only
rewrites on it — `materializeConstantsForSetOperationBranches` materialized
the constants of the `Union` inside a `Buffer` child (destination table +
buffers), while a sibling child kept them const, breaking the equal-headers
invariant across the children of `ReadFromMerge`:

    Logical error: Block structure mismatch in Pipe stream: different columns:
    9223372036854775806_UInt64 UInt64 UInt64(size = 0)
    9223372036854775806_UInt64 UInt64 Const(size = 0, UInt64(size = 1))

Found by the AST fuzzer on #100173:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100173&sha=22dee0374690f4c09727a81023ef38fe4e515785&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29

Co-Authored-By: Claude Fable 5 <[email protected]>
The previous commit cleared `make_distributed_plan` in the optimization
settings of the child plans of `ReadFromMerge`, but that disabled the whole
distributed planning of the children, and a `Merge` table over `Distributed`
tables intentionally converts each child plan to a distributed plan on its
own (see #108401). This failed the `Fast test` on
04367_distributed_plan_merge_scatter_multishard:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113636&sha=d2b378e20bfae32ba1ace67846ff6c59f235f8be&name_0=PR&name_1=Fast%20test

Fix it at the actual defect instead: `materializeConstantsForSetOperationBranches`
exists because plan serialization stores only names and types, so constness is
re-derived per step after a fragment is deserialized. A plan with no logical
exchange steps is never split into serialized fragments (`convertToDistributed`
keeps it as a single stage executed in this process), so the rewrite must not
touch it. Gate the rewrite on `planContainsLogicalExchange`, and restore the
plain optimization settings for the child plans of `ReadFromMerge`.

Verified locally: 04812_merge_children_distributed_plan_const_mismatch fails
with `Block structure mismatch` on the unpatched binary and passes with this
change; 04367_distributed_plan_merge_scatter_multishard,
04613_distributed_plan_merge_broadcast_join and
04319_distributed_plan_set_operation_const_columns (and the rest of the
distributed_plan family, 22 tests) pass.

Co-Authored-By: Claude Fable 5 <[email protected]>
…rns full columns

Reading the destination table of a `Buffer` table can return a full column
where the plan over the buffers keeps it constant: constants come back
materialized from a `Distributed` destination. `StorageBuffer::read`
converted the destination plan to the buffers header by name, and a full
column cannot be converted back to a constant, so such reads failed with
`ILLEGAL_COLUMN` ("Cannot convert column ... because it is non constant in
source stream but must be constant in result") - with or without
`make_distributed_plan`, with or without a `Merge` table on top.

Materialize such constants in the buffers branch instead and unite the
branches of the `Buffer` read on the materialized header. This also covers
the mixed local/distributed child shape of `ReadFromMerge` raised in the
review of #113636: a `Buffer`
child over a `Distributed` destination next to a non-distributed sibling.
@clickhouse-gh

clickhouse-gh Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cef900b]

Summary: ✅


AI Review

Summary

This PR narrows the distributed-plan constant-materialization rewrite to plans that actually contain logical exchanges and normalizes StorageBuffer reads when a Distributed destination returns full columns, with focused stateless repros for both cases. I did not find any remaining correctness, safety, or evidence gap on the current head.

Final Verdict

✅ No blocking or major issues found.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.10% 79.20% +0.10%

Changed lines: Changed C/C++ lines covered: 26/26 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing cef900b71 with master 0eb658995 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 692.32 MiB 689.31 MiB -3.01 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

8 translation units recompiled, 33 s compile time in total, 8 of them have a recent master baseline.

Job report

@davenger
davenger added this pull request to the merge queue Aug 14, 2026
Merged via the queue into master with commit e6dab88 Aug 14, 2026
182 checks passed
@davenger
davenger deleted the continue-pr-113636 branch August 14, 2026 10:49
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 14, 2026
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.

3 participants