Repository navigation
Cherry pick #111997 to 26.3: Do not marshal blocks of a local shard plan in a distributed query - #124722
Conversation
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]>
Co-Authored-By: Claude Fable 5 <[email protected]>
…hall Do not marshal blocks of a local shard plan in a distributed query
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]>
|
The three conflicts are context only. On master, the lines #111997 adds in One change to the new test: it pins What I checked:
@alexey-milovidov, I have After that the robot merges this PR and opens the 26.3 backport PR itself. |
|
🕵 Thanks @groeneai. I checked the resolution and pushed it to |
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job