Repository navigation
Conversation
Under parallel_replicas_plan_based=1 with serialize_query_plan=1, a query with an IN-subquery aborted with LOGICAL_ERROR 'Cannot serialize FutureSetFromSubquery with no query plan' (and 'Method serialize is not implemented for ParallelReplicasSplit'). Introduced by ClickHouse#108504; found by the AST fuzzer (STID 2678-3c21). The plan-based path ships the initiator's optimized fragment to replicas, sharing FutureSetFromSubquery objects with the local plan. The deferred set build consumes each set's subquery source plan during local execution, so lazy send-time serialization found no plan. Additionally the IN-subquery's own plan carried a transient ParallelReplicasSplitStep, which has no serialization. Serialize the remote fragment in createRemotePlanFragmentForParallelReplicas while the shared sets still own their source plans (mirroring the classic serialize_query_plan path), and skip the ParallelReplicasSplitStep marker in QueryPlan::serialize like the other transient pass-through markers. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260717-131800 |
|
cc @devcrafter @KochetovNicolai — could you review this? It fixes a LOGICAL_ERROR under plan-based parallel replicas ( |
|
Workflow [PR], commit [342bdf2] Summary: ⏳
AI ReviewSummaryThis PR fixes the original plan-based parallel-replicas Findings
Final VerdictRequest changes: narrow the retained |
CI finish ledger — 4dbd9b9bdabe7cbaea464a906ed05770Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens.
Session id: cron:our-pr-ci-monitor:20260717-230000 |
…rialized After master moved the parallel-replicas split into the plan optimizer, the remote fragment is now built during optimizeTreeSecondPass, i.e. AFTER primary key analysis has already built the IN-subquery's set in place. The in-place build discarded the set's subquery plan unconditionally, so serializing the fragment threw Cannot serialize FutureSetFromSubquery with no query plan which reddened this PR's own regression test in Fast test. Two things are fixed: 1. buildOrderedSetInplace no longer drops `source` on its non-destructive path. That path deliberately runs the speculative pipeline against a clone, so the canonical plan is still valid and unconsumed when it finishes; it was simply being thrown away. Serializing the set by value is not an alternative, because once the set exceeds use_index_for_in_with_subqueries_max_values it clears its materialized elements and keeps only the hash table, so the subquery plan is the only shippable description of it. On the destructive fallback (a non-clonable source, e.g. a nested IN) `build` really did move the plan out, so the reset is kept there. 2. The eager serialization in createRemotePlanFragmentForParallelReplicas is no longer gated on serialize_query_plan. ReadFromParallelReplicasStep always hands the fragment to its RemoteQueryExecutor, which always sends it, so this path serializes regardless of that setting; with the gate, a fragment whose set is consumed only by the local pipeline still failed at send time. Each change is covered separately by the extended test: item 1 by the key-column IN (only a key column reaches index analysis) plus the max_values shape, item 2 by the serialize_query_plan = 0 queries with index analysis disabled. Verified on a 3-replica cluster: the test fails on the pre-fix build and passes on the fixed one, and reverting either change alone reddens it again. Every result matches non-parallel execution. A nested IN subquery still hits the same error through the destructive fallback; that is pre-existing and tracked separately. Co-Authored-By: Claude Fable 5 <[email protected]>
…ss silently Every asserted result equals what plain local execution returns, so if the distributed split or the index-time set build silently stopped happening the value assertions would still be green and the test would no longer cover the serialization path at all. Assert both preconditions explicitly: the plan contains a ReadFromParallelReplicas step, and index analysis reports granule pruning (i.e. it really built the set). Both are written as SELECT over EXPLAIN filtered with ILIKE, which is robust to plan formatting changes. Verified load-bearing: setting parallel_replicas_plan_based = 0 reddens the first guard and use_index_for_in_with_subqueries = 0 reddens the second. Co-Authored-By: Claude Fable 5 <[email protected]>
| set_and_key->set = speculative_set; | ||
| source.reset(); | ||
|
|
||
| /// Keep the preserved `source`: plan serialization ships a set as a subquery plan and cannot rebuild one |
There was a problem hiding this comment.
Keeping source whenever the clone path succeeds changes the common local-query path too, not just the serialized remote one. source owns the subquery plan's QueryPlanResourceHolder (table locks, storage holders, interpreter contexts), and before this patch a successful in-place build released all of that here. Now a plain local ... IN (subquery) that hit key analysis keeps the entire RHS plan and its locks alive until the outer query finishes even though build will never consume source again once set_and_key->set is created.
For long-running queries this can hold share locks on the subquery tables and extra plan memory for no benefit. Can we preserve source only when the set may actually need plan serialization, or replace it with a compact serialized form once the remote fragment has been eagerly cached?
There was a problem hiding this comment.
Agreed, and fixed in 342bdf2e2da1c0.
The blast radius is real: buildOrderedSetInplace has around fifteen callers with nothing to do with parallel replicas (key analysis, the skip-index conditions, VirtualColumnUtils, KVStorageUtils, StorageSystemUsers, StorageSystemZooKeeper, ConditionSelectivityEstimator, evaluateConstantExpression), and the retention was ungated, so it was not confined to the experimental setting the rest of the change is scoped to.
The plan is now retained only when a setting that enables a query-plan-serializing consumer is on: serialize_query_plan for the classic parallel-replicas and distributed readers, parallel_replicas_plan_based, and make_distributed_plan. Those are all the consumers, and the predicate fails closed, so a new one keeps the plan only once its setting is listed. A plain local IN (subquery) releases the plan exactly as before, which I verified: it returns the right result, still prunes granules, and the subquery table can be dropped immediately afterwards.
That is your first suggestion. The second one is not available in general: past use_index_for_in_with_subqueries_max_values the set clears its materialized elements and keeps only the hash table (Set.cpp:216-220), so hasExplicitSetElements() is false and there is no compact value form to substitute. The retained plan is the only shippable description of such a set.
The regression test gained the shape that pins this: serialize_query_plan = 0 with index analysis still on. Keying the retention on serialize_query_plan alone still aborts there, and no previously covered shape detected it.
There was a problem hiding this comment.
I don't think this is fully fixed yet. The current predicate is still session-wide: maySerializeQueryPlan returns true for any query with serialize_query_plan = 1 (src/Interpreters/PreparedSets.cpp:434-437), and that is enough to skip the source.reset at src/Interpreters/PreparedSets.cpp:623.
But serialize_query_plan only creates a remote query_plan when a remote shard stream is actually being built (src/Interpreters/ClusterProxy/SelectStreamFactory.cpp:177-183). A plain local SELECT ... WHERE key IN (subquery) over a local MergeTree can have serialize_query_plan = 1 and never hit any plan serialization at all, so the clone-path buildOrderedSetInplace still keeps the subquery plan's QueryPlanResourceHolder / table locks alive until the outer query ends.
So the original local-query retention issue is still present; the predicate needs to depend on an actual serializing consumer, not just the session setting.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 14/14 (100.00%) · Uncovered code |
Addresses the automated review on the previous revision. `buildOrderedSetInplace` kept the preserved `source` plan on every non-destructive build, but that plan is only needed by `QueryPlan::serializeSets`, which ships a prepared set to another server as its subquery plan and cannot rebuild one from an already-built set. On a plain local query nothing serializes, so the retained plan is unreachable (`build` returns early once the set is created) while still owning the subquery's table locks, storage holders and interpreter contexts: after a clone-path build it is their sole owner, because `QueryPlan::clone` copies the plan nodes and not the resource holder. That function has around fifteen callers across key analysis, skip indexes, virtual columns and system tables, none of which involve parallel replicas, so the retention was not confined to the experimental setting the rest of this change is scoped to. Release the plan as before unless one of the three settings that enable a query-plan-serializing consumer is set: the classic and plan-based parallel-replicas readers and the distributed-plan executor. The predicate fails closed, so a future consumer keeps the plan only once its setting is listed. The `ParallelReplicasSplitStep` serialization skip is dropped: master has since moved split insertion into the plan optimizer, and the fragment builder clones the subtree below the marker, so no such step can enter a serialized plan. The set's own plan cannot carry one either, being built unoptimized with only its clone optimized. Removing the branch leaves the test green and no plan-based shape reaches the corresponding error, which is what identifies it as dead rather than untested. `Serialization.cpp` is therefore unchanged. Three test fixes. The index-analysis precondition asserted only that `Granules:` appears, which it does whenever any index analysis runs, so it could not detect the set dropping out of key analysis; it now asserts the `IN` set reached the primary key condition. The `serialize_query_plan = 0` cases used `parallel_replicas_local_plan = 0` instead of the process-global one-shot `parallel_replicas_wait_for_unused_replicas` failpoint, which a concurrent query could consume. A case with `serialize_query_plan = 0` and index analysis on was added, the only shape that shows the retention must key on `parallel_replicas_plan_based` rather than on `serialize_query_plan` alone. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Close in favor of #112443 |
|
Closing the loop on the one ledger line above that was owned by a fix task rather than a PR: |
Related: #108504
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
Fixes a
LOGICAL_ERRORunder the experimental plan-based parallel replicas feature (parallel_replicas_plan_based = 1, added by #108504). Found by the AST fuzzer (STID 2678-3c21); not reachable in a stable release, as the setting is experimental and off by default.That path ships the initiator's already-optimized plan fragment to the replicas, sharing
FutureSetFromSubqueryobjects with the local plan. ForSELECT ... WHERE a IN (SELECT k FROM src)the shared set loses its subquery plan before the fragment is serialized, andQueryPlan::serializeSetsthrows. Two causes, each fixed here:FutureSetFromSubquery::buildOrderedSetInplacediscarded the subquery plan even on its non-destructive path, where the speculative pipeline runs against a clone so the canonical plan is still valid. Primary key analysis takes that path, and the fragment is now built after index analysis. Serializing the set by value is no alternative: pastuse_index_for_in_with_subqueries_max_valuesthe set keeps only its hash table. The plan is retained only when a setting enabling a plan-serializing consumer is on, so a plain localIN (subquery)still releases it and its table locks.createRemotePlanFragmentForParallelReplicaswas gated onserialize_query_plan, butReadFromParallelReplicasStepalways hands the fragment to itsRemoteQueryExecutor, which always sends it.Validated on a 3-replica cluster: the test fails before the fix, passes after, matches non-parallel results, and reverting either change reddens it. It asserts its own preconditions, so silently losing the distributed read or the set build cannot leave it green.
Three shapes are still broken and tracked separately, all reproducing on master. A nested
INand a non-keyPREWHEREtake the destructive analysis-time build, which moves the plan out unconditionally. ALowCardinality(Nullable)key hits this same error on master and becomes a catchableCode: 117from the function type round trip here, which is a separate defect outside these files.