Repository navigation
Do not apply parallel replicas to the child plans of a Merge table - #112849
Conversation
`ReadFromMerge` materializes the plan of every underlying table lazily, while the outer plan is already being executed. Plan-based parallel replicas (`parallel_replicas_plan_based = 1`) was still applied to those child plans, and the shipped fragment was wrong in two ways: * The filters pushed down into the child plan were lost, so any query with a `WHERE` over a `Merge` table silently returned no rows. With `parallel_replicas_local_plan = 0`, `SELECT count() FROM merge(currentDatabase(), 't') WHERE name = '1'` returned `0` instead of `1`. * When the filter referenced a subquery set, the outer plan's `addStepsToBuildSets` had already moved the source plan out of the `FutureSetFromSubquery`, so serializing the fragment threw `Cannot serialize FutureSetFromSubquery with no query plan`. This is the shape the AST fuzzer hit with `SELECT count() FROM merge(currentDatabase(), 'test_local') WHERE name GLOBAL IN (SELECT name FROM test_distributed)`. Disable parallel replicas for `Merge` child plans, both in the optimization settings and in the child context - the conversion re-checks `canUseParallelReplicasOnInitiator` against the context captured by the reading step, not only the optimization settings. This restores the documented behavior that queries over `Merge` tables run on a single replica (#67770); proper support is being added separately by #110972 behind `parallel_replicas_allow_merge_tables`. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [f05e878] Summary: ✅
AI ReviewSummaryThis PR stops Final Verdict✅ No new findings in the current diff. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 34/35 (97.14%) · Uncovered code |
…the regression test The test switched to `parallel_replicas_local_plan = 1` but only re-ran two `count()` queries there. The structural check above it matches only `ReadFromParallelReplicas`, so a regression that distributes the `Merge` child plan in local-plan mode - where the read is wrapped in `ReadFromLocalReplica` / `ReadFromRemoteParallelReplicas` - would not have been caught. Add, for `parallel_replicas_local_plan = 1`: the `GLOBAL IN` reproducer that used to throw `Cannot serialize FutureSetFromSubquery with no query plan`, an `EXPLAIN` assertion that the `Merge` child read carries none of the three distributed-read wrappers, and the matching positive control on a plain table. Verified against a binary with the `StorageMerge.cpp` fix reverted: the new `EXPLAIN` assertion returns `1` instead of `0` and the new `GLOBAL IN` query throws the logical error, so neither assertion is vacuous. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 14/14 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
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 units7 translation units recompiled, 19 s compile time in total, 7 of them have a recent master baseline. |
…ns of a `Merge` table `ReadFromMerge::buildPipeline` built the child pipeline with raw `QueryPlanOptimizationSettings(context)` from the outer query context, and `QueryPlan::buildQueryPipeline` runs `convertToDistributed` whenever `make_distributed_plan` is set, regardless of `optimize_plan`. So with `make_distributed_plan = 1`, a `Merge` child plan was converted to a distributed plan mid-execution, failing with `Unknown query plan step: GatherReceive` (and `Cannot serialize FutureSetFromSubquery with no query plan` when the query had subquery sets already consumed by the outer plan's `addStepsToBuildSets`). Clear `make_distributed_plan` in `getChildPlanOptimizationSettings`, use that helper in `buildPipeline`, and clear the setting in the child context as well (mirroring `enable_parallel_replicas`, for nested interpreters). Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112849&sha=bc9bb1384ce09061a1573db7b3ac6691766e1cbf&name_0=PR&name_1=Stress%20test%20%28arm_debug%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The |
|
Confirmed unrelated to this PR, and I agree it is accounting rather than real memory pressure. Sharpening the mechanism: the refused request is 0 bytes ( The refusal site is On tracking: I did not open a separate issue, I posted the full analysis as a comment on 113408: #113408 (comment). It covers the counter path, the candidate unmatched addition ( There is no fix PR, and I do not want to imply one is on the way, because what is left is a taxonomy choice rather than a local fix: either fix the counter (the unsaturated background-task-end and free paths), or give the stress harness a terminal status for "alive but refusing all requests" so this stops being reported as a deadlock. Two questions at the end of that comment are waiting on your call; either answer unblocks me and I will implement it. Breadth, so the scope is not overstated. Keyed on |
…the query has subquery sets Fast test on the previous commit caught that clearing \`make_distributed_plan\` unconditionally breaks 04367_distributed_plan_merge_scatter_multishard: distributing the child plans of a \`Merge\` table is a supported feature (the double-application of the transforms is fenced by \`planContainsLogicalExchange\` in \`optimizeTree.cpp\`, and \`ReadFromMerge::buildPipeline\`'s conversion is the run that materializes the logical exchanges inserted at child-plan creation). The \`Unknown query plan step: GatherReceive\` failure that motivated the blanket clearing was an artifact of reproducing with \`clickhouse local\`, which never calls \`QueryPlanStepRegistry::registerPlanSteps\` (only \`Server.cpp\` does), so any distributed child plan fails to deserialize there; on a server that path works. The genuine bug is narrower: when the query has subquery sets (\`IN (SELECT ...)\`), the outer plan's \`addStepsToBuildSets\` has already moved the source plan out of every \`FutureSetFromSubquery\` by the time the lazily materialized child plans are optimized, so a distributed child fragment referencing such a set fails to serialize with the logical error \`Cannot serialize FutureSetFromSubquery with no query plan\`. Clear \`make_distributed_plan\` for the child plans only in that case, detected via the prepared sets of the outer query's planner context. The test now asserts both directions on a 100k-row \`Merge\` child: without subquery sets the child plan contains exchanges and executes correctly (positive control that the feature still applies), and with \`GLOBAL IN\` the child plan has no exchanges and the query succeeds. Plain \`IN\` and aggregation/ORDER BY shapes are excluded: converting an outer plan whose worker stage would carry an unserializable step (\`ReadFromMerge\`, the EXPLAIN wrapper's \`ReadFromStorage\`) is rejected upfront with \`SUPPORT_IS_DISABLED\` - a pre-existing limitation unrelated to the child plans. Fast test failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112849&sha=425692c16c6deb6481fa26a0617ebb9efbda5ae6&name_0=PR&name_1=Fast%20test (#112849) Co-Authored-By: Claude Fable 5 <[email protected]>
alexey-milovidov
left a comment
There was a problem hiding this comment.
Ok. Half of this change is related to the fact that the distributed plan does not work.
…-as-database Pulls in the merged fix #112849 for the unrelated AST fuzzer failure #112848 (`Cannot serialize FutureSetFromSubquery with no query plan`) seen in https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110976&sha=561948e08e41b94066e8b88d5cb43cc1bf4c8d6e&name_0=PR
…Merge` read `Merge` table support for parallel replicas needs the opposite of what the master merge brought in from #112849: a replica reading for an initiator builds its child plans through `createChildrenPlans`, and every child reading step must read only the mark ranges the coordinator hands out to it - which is exactly what `enable_parallel_replicas` in the child context enables. Clearing it made every replica read every child table in full, so every coordinated `Merge` read returned its rows multiplied by the number of replicas (`04666`, `04667`, `04828`, `04829` all returned three times their rows locally). Clear the setting only when this `Merge` read is not itself a part of coordinated reading with parallel replicas; the gate additionally requires `parallel_replicas_allow_merge_tables`, so the behavior #112849 restored for the default configuration is untouched. The initiator's own local plan does not depend on the setting - its child reading steps are replaced with local parallel replicas reading steps right after they are created. The plan-level flag stays cleared for every child plan (`getChildPlanOptimizationSettings`): plan-based parallel replicas must not distribute a lazily built child plan, which is what #112849 fixed.
…rom a self-matching merge() child PR ClickHouse#112849 fixed the coordinator guard trips (`Initiator received more initial requests than there are replicas`, `Duplicate announcement received for replica number N`) that fired when a `merge()` regex matched the outer table itself, but its test 04665 never reaches the announcement guards on the pre-fix code: its fixture aborts earlier with `Cannot serialize FutureSetFromSubquery with no query plan` and uses the default index granularity, at which the initiator claims every mark range and the followers are cancelled before announcing. This fixture (from the AST fuzzer signature, STIDs 8765-7cc2/8765-7341) reddens 6/6 without the `enable_parallel_replicas` clear in `ReadFromMerge::createChildrenPlans` and passes with it, in both `parallel_replicas_local_plan` modes. Related: ClickHouse#112849 Related: ClickHouse#110972 Co-Authored-By: Claude Fable 5 <[email protected]>
…ing a read
With parallel_replicas_plan_based = 1, a query over a system log table or a
SQL SECURITY DEFINER view failed on the initiator with
LOGICAL_ERROR: Next task callback is not set for query with id: <uuid>
applyParallelReplicas marks the coordinated read with
enableParallelReadingFromReplicasForSerialization, which deliberately attaches
no callbacks: the step is only serialized and shipped to replicas, where
deserialize rebuilds it and resolves the callbacks from the context.
createParallelReplicasPlan then clones that fragment once more to build the
initiator's local arm, and ReadFromMergeTree::clone() passed the
marked-but-callback-less state back through the constructor. The constructor
cannot distinguish "marked for serialization" from "replica side, resolve from
the context", so it took its context fallback, and an initiator holds no
coordinator callback.
Ordinary plan-based queries survive that fallback only because Context's copy
constructor carries the callbacks TCPHandler installed. Instrumenting the
constructor showed the fallback is taken on the ordinary path too, and merely
happens to find a callback there. The failing shapes read under a context built
fresh from the global context, which never holds them: a system log storage, or
a DEFINER view.
clone() now copies the flag and both callback optionals directly, so a copy no
longer re-derives state from the context. All three fields move together:
restoring only the flag would drop the callbacks of a marked read that has them,
which getParallelReadingExtension asserts on. The constructor's context fallback
is unchanged, since deserialize, StorageMergeTree::read and
StorageReplicatedMergeTree::readLocalImpl rely on it on a follower.
Both branches of that fallback were reachable with the same state: with the
first one made non-throwing, the same query threw the sibling message from
Context::getMergeTreeReadTaskCallback, so copying both optionals is required.
Still reproduced with both in-flight Merge route fixes applied (ClickHouse#112849,
ClickHouse#113788), so this is a separate defect rather than one of those routes.
Related: ClickHouse#111677
Cherry pick #112849 to 26.7: Do not apply parallel replicas to the child plans of a Merge table
The only conflict was an unrelated `ProcessList.h` include that master carries next to the new `PreparedSets.h` and 26.7 does not have. Everything else applies verbatim. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… plans of a Merge table
Backport #112849 to 26.7: Do not apply parallel replicas to the child plans of a Merge table
04665 is claimed by two live open PRs (ClickHouse#112846, ClickHouse#112849). The collision is silent: the filenames differ so git reports no conflict, and check_gaps_in_tests_numbers only rejects gaps of 100 or more, never collisions. 04666, 04670, 04674 and 04675 are claimed too, by ClickHouse#112816, ClickHouse#112458 and unpublished branches respectively. Co-Authored-By: Claude Fable 5 <[email protected]>
ReadFromMergematerializes the plan of every underlying table lazily, while the outer plan is already being executed. Plan-based parallel replicas (parallel_replicas_plan_based = 1) was still applied to those child plans, and the shipped fragment was wrong in two ways:parallel_replicas_local_plan = 0any query with aWHEREover aMergetable silently returned no rows —SELECT count() FROM merge(currentDatabase(), 't') WHERE name = '1'returned0instead of1.addStepsToBuildSetshad already moved the source plan out of theFutureSetFromSubquery, so serializing the fragment threwLOGICAL_ERRORCannot serialize FutureSetFromSubquery with no query plan. This is the shape the AST fuzzer keeps hitting:SELECT count() FROM merge(currentDatabase(), 'test_local') WHERE name GLOBAL IN (SELECT name FROM test_distributed).The guard added by #112443 (
planHasSubquerySet) only looks for aDelayedCreatingSetsStepin the plan being optimized, which aMergechild plan never has.Parallel replicas are now disabled for
Mergechild plans, both in the optimization settings and in the child context — the conversion re-checkscanUseParallelReplicasOnInitiatoragainst the context captured by the reading step, not only the optimization settings. This restores the documented behavior that queries overMergetables run on a single replica (#67770); proper support is being added separately by #110972 behindparallel_replicas_allow_merge_tables.New test
04665_merge_table_parallel_replicas_child_plancovers the wrong-result shapes, both subquery shapes, and asserts that a plain table read is still distributed so parallel replicas are not over-disabled.Follow-up from review:
make_distributed_planalso leaked into the child pipelines throughReadFromMerge::buildPipeline. Distributing the child plans of aMergetable is a supported feature (see04367_distributed_plan_merge_scatter_multishard), so it stays enabled — except when the query has subquery sets, whose source plans the outer plan'saddStepsToBuildSetshas already consumed: a distributed child fragment referencing such a set failed to serialize with the sameCannot serialize FutureSetFromSubquery with no query planlogical error. Such child plans now stay local, covered by the new test04827_merge_table_make_distributed_plan_child_plan.Closes: #112848
Related: #112443
Related: #110972
Related: #67770
CI report where the AST fuzzer hit this: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110968&sha=67c1d446e4881a0bebbf05446dc39948f79e6222&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29 (#110968)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results (the
WHEREclause was ignored) and a logical errorCannot serialize FutureSetFromSubquery with no query planfor queries overMergetables and themergetable function when plan-based parallel replicas were enabled.Documentation entry for user-facing changes
Version info
26.8.1.1055(included in26.8and later)26.7.13.6