Skip to content

Cherry pick #111997 to 26.3: Do not marshal blocks of a local shard plan in a distributed query - #124722

Merged
robot-ch-test-poll merged 9 commits into
backport/26.3/111997from
cherrypick/26.3/111997
Oct 9, 2026
Merged

robot-ch-test-poll merged 9 commits into
backport/26.3/111997from
cherrypick/26.3/111997

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Original pull-request #111997

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 8 commits July 26, 2026 18:43
A distributed query with GROUP BY GROUPING SETS executed on a shard could
fail with a logical error:

  Expected UInt64 column for __grouping_set, got BLOB

BLOB there is ColumnBLOB::getFamilyName(), the enable_parallel_blocks_marshalling
wrapper, so the column was at the right header position with the right logical
type: it was a marshalled UInt64 that nothing had unmarshalled.

The gate that adds BlocksMarshallingStep in Planner::buildPlanForQueryNode()
keys on properties of the context (query_kind == SECONDARY_QUERY,
distributed_depth, to_stage) rather than on whether the plan's blocks will be
written to a socket. When a query runs on a secondary context and reads a
cluster that has a local replica, the local branch is planned in-process by
createLocalPlan at a non-Complete stage on that same secondary context. The
gate fired and appended BlocksMarshallingStep to a plan that is then united
straight into the parent pipeline, where no UnmarshallBlocksTransform exists
(that transform lives only on remote pipes), so the ColumnBLOB reached the
parent operators as is.

Add SelectQueryOptions::is_local_plan_for_distributed_query, set it in the
local-plan builders, and add it to the gate. In createLocalPlan it is
conditioned on !build_logical_plan, because a logical plan is serialized and
shipped to a shard which sends its blocks back over the network and must keep
marshalling. A shard's own top-level plan is unaffected: its blocks do go to
the socket, so the gate still fires there.

Three carriers are covered: the local shard stream in SelectStreamFactory,
the stale-replica local fallback in ReadFromRemote, and
createLocalPlanForParallelReplicas.
createLocalPlanFragmentForParallelReplicas is not a carrier, it rewrites an
existing plan fragment and never runs the Planner.

Only GROUP BY GROUPING SETS with two or more sets produces this particular
message, because MergingAggregatedTransform::addChunk returns early for a
single set before type-checking the column. The stray ColumnBLOB reached
every other consumer on that path too, which is why the earlier fixes in this
area addressed individual consumers rather than the gate.

Removing the step for local plans is also a small win: it used to serialize
and compress every block for data that never leaves the process.

Found by the AST fuzzer on #102192, unrelated to that PR's own changes.
The previous commit's carrier enumeration was incomplete: it said three
carriers, all of them local shard/replica plans built through
createLocalPlan.

Two storage layers build a nested child plan of their own, with a fresh
SelectQueryOptions at a non-Complete stage on the caller's context, and
then unite that plan into the current pipeline in this process with no
unmarshalling:

  ReadFromMerge::createPlanForTable, united by ReadFromMerge::buildPipeline
  which adds only a MaterializingTransform,

  StorageBuffer::read, whose buffers plan is united with the table plan.

So the gate still fired for them and produced the same stray ColumnBLOB.
For the Merge table it surfaces as a different message, because the
column is caught by columnMatchesType inside
ExpressionActions::executeOnColumns rather than by
MergingAggregatedTransform::addChunk:

  ColumnBLOB should be converted to a regular column before usage

This is what a Merge table spanning a local MergeTree and a Remote or
Distributed table hits when it is read through remote(), reported as
issue #111945.

Setting the flag on the nested options cannot remove marshalling that
anything depends on: when the outer query is itself a shard plan, the
outer plan carries its own BlocksMarshallingStep, so the socket boundary
is still marshalled exactly once at the right level.

The Merge case is added to the regression test. There is no Buffer case:
the code path is a carrier by inspection, but I could not construct a
query that reaches it with a marshalled block, and a test that may not be
asserting anything is worse than none.

The test also pins enable_analyzer, because only the analyzer adds the
step and CI runs variants with the old analyzer, and it now asserts the
plan shape for three configurations instead of one: remote(), parallel
replicas, and a plan serialized to be shipped to a shard. The last one is
what pins the !build_logical_plan condition, which was previously
unexercised.

Closes: #111945
The `distributed plan` CI variants set `serialize_query_plan = 1` in the default
profile, and a plan serialized and shipped to a shard legitimately still carries
`BlocksMarshallingStep`. That step is not registered in `QueryPlanStepRegistry`
on master, so the shard fails to deserialize the plan with
`Unknown query plan step: BlocksMarshalling` - a pre-existing limitation
unrelated to this fix, which the test would otherwise import as a deterministic
failure in four PR-blocking jobs.

Pin the setting on the clients rather than skipping the test, so the coverage is
kept everywhere. The plan-shape assertion for the shipped plan keeps its own
query-level `serialize_query_plan = 1` and stays EXPLAIN-only.

Co-Authored-By: Claude Fable 5 <[email protected]>
The regression test covered the `Merge` carrier but never read a `Buffer`
table, so the assignment on the buffers `SelectQueryOptions` in
`StorageBuffer::read` was only correct by inspection and a later refactor
could have dropped it silently.

Reaching that code needs a stage above `FetchColumns`, which a `Buffer`
inherits from its destination table, so the destination is a `Distributed`
one and the flush thresholds are large enough to keep rows in the buffer at
read time. Reverting only that one assignment now aborts the test with
`Expected UInt64 column for __grouping_set, got BLOB`, and the added plan
assertion goes from two `BlocksMarshalling` steps to four.

Co-Authored-By: Claude Fable 5 <[email protected]>
The test exceeded the 180s flaky-check limit on the `amd_asan_ubsan` lane
(194.3s and 183.5s out of 50 runs, the rest between 51s and 146s), which
`tests/clickhouse-test` reports as `Test runs too long (> 180s)`.

Its runtime was dominated by starting client processes rather than by any
query. The statements were spread over 17 separate `clickhouse client`
invocations, and none of the queries is heavy: measured per statement on a
debug build, the whole test took 1.95s spread almost uniformly at about
0.08s per invocation, while running 19 trivial queries as 19 processes cost
1.32s against 0.10s for the same 19 queries inside one process.

That cost is about five times higher under a sanitizer: a single
`clickhouse local -q "SELECT 1"` takes about 0.75s with ASan against 0.14s
on a debug build. The flaky check also runs every selected test many times
concurrently with `nproc - 1` workers, and process startup is exactly the
resource that contention makes more expensive, so the per-run time grew with
the load until the tail crossed the limit.

Group the statements into as few invocations as possible (17 down to 6) and
emit the section headers as `SELECT` constants inside those batches instead
of from the shell. Driving the test with an ASan client against the same
server, this takes it from 9.0s to 3.7s when run alone, and the gain grows
with contention, which is the regime that actually failed: with copies
pinned to 8 CPUs the slowest copy goes from 8.6s to 3.4s at 8 copies and
from 16.9s to 6.0s at 16 copies, so 2.4x alone, 2.5x at 8 copies and 2.8x
at 16.

No assertion is changed, added or removed, and the `.reference` file is
untouched, so the output is byte for byte the same. The relevant behaviour of
a batch was checked first: per-statement `SETTINGS` are still honoured,
`rowNumberInAllBlocks` restarts for every statement, and
`--query_kind=secondary_query` applies to every statement, so the marshalling
gate still fires after the first one.

The `long` tag would also silence the failure, but it cuts the flaky-check
repeats to a tenth, and this test relies on those repeats to cover two
separate carriers.

Co-Authored-By: Claude Fable 5 <[email protected]>
…ob-unmarshall

Refresh the CI base. The branch was based on master of 2026-07-26 and
`Planner.cpp`, `StorageMerge.cpp` and `StorageBuffer.cpp` have all moved
since, so the previous green run no longer says anything about current
master. The fix itself is unchanged: all five `is_local_plan_for_distributed_query`
sites survive the merge intact.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…hall

Do not marshal blocks of a local shard plan in a distributed query
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Oct 8, 2026
Resolve the cherry-pick of #111997 onto 26.3.

The three conflicts are context only: on master the new assignments sit next to
is_local_shard_plan (35cff6a), which 26.3 does not have, so the resolution
keeps only the lines #111997 adds and does not bring is_local_shard_plan along.

The new test drops SETTINGS explain_query_plan_default = 'legacy': the setting
does not exist on 26.3, where the legacy EXPLAIN format is the only one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

The three conflicts are context only. On master, the lines #111997 adds in SelectQueryOptions.h, DistributedCreateLocalPlan.cpp and ParallelReplicasLocalPlan.cpp sit next to is_local_shard_plan, which #110136 added after the 26.3 cut. #110136 has no backport label, so this cherry-pick will not clear on its own, and that member is unrelated to marshalling. I kept 26.3's code as it is and added only #111997's lines, so nothing from #110136 comes along. The other five files merge cleanly.

One change to the new test: it pins explain_query_plan_default = 'legacy', a setting that does not exist on 26.3, where the legacy EXPLAIN format is the only one. I dropped those two SETTINGS clauses.

What I checked:

  • git diff backport/26.3/111997 <resolution> touches exactly the 8 files of Do not marshal blocks of a local shard plan in a distributed query #111997 (243 insertions, 4 deletions). In each source file the added and removed lines are the same as in Do not marshal blocks of a local shard plan in a distributed query #111997's own diff, and the test differs from master only by the two dropped clauses.
  • A Debug build of the resolved tree with clang-21, the 26.3 toolset, compiles. Run through the 26.3 clickhouse-test with randomized settings, 04647_no_blocks_marshalling_for_local_shard_plan passes 5/5 on it and fails 3/3 on the official 26.3.42.3 with Expected UInt64 column for __grouping_set, got BLOB.
  • The resolution's first parent is a6827f5554f9, this PR's head, so applying it is a fast-forward.

@alexey-milovidov, I have triage but not push on this repository, so I cannot update cherrypick/26.3/111997 myself. Applying it takes two commands:

git fetch https://github.com/groeneai/ClickHouse resolve-26.3-cherrypick-111997
git push origin FETCH_HEAD:cherrypick/26.3/111997

After that the robot merges this PR and opens the 26.3 backport PR itself.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Thanks @groeneai. I checked the resolution and pushed it to cherrypick/26.3/111997 (d2ddfc8c97e, a fast-forward from a6827f5554f). Its diff against backport/26.3/111997 has the same added and removed lines as #111997 on master. The only difference is the two SETTINGS explain_query_plan_default = 'legacy' clauses dropped from the test, because that setting does not exist on 26.3. None of the resolved files reference is_local_shard_plan, and there are no conflict markers.

@robot-ch-test-poll
robot-ch-test-poll merged commit 306aa7a into backport/26.3/111997 Oct 9, 2026
1 check passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.3/111997 branch October 9, 2026 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants