Repository navigation
Fix "Not-ready Set" exception by making in-place set build non-destructive - #107924
Conversation
…ctive `FutureSetFromSubquery::buildOrderedSetInplace` is a speculative build run during primary key / skip index analysis for an `IN` subquery. It built the set through the destructive `build`, which moves the subquery `source` plan out of the future-set; building a pipeline then consumes it. If the in-place pipeline stops without creating the set (e.g. a subquery timeout with `overflow_mode = 'break'`, or any early stop that skips `CreatingSetsTransform::generate` and therefore `Set::finishInsert`), `buildOrderedSetInplace` returns `nullptr`, but `source` has already been consumed. The authoritative deferred build (`DelayedCreatingSetsStep::makePlansForSets`) then finds `source == nullptr`, attaches no set-building step to the pipeline, and `FunctionIn` throws "Not-ready Set is passed as the second argument" when the query runs. Fix: run the in-place pipeline against a clone of `source` (via the existing `QueryPlan::clone`) and keep the original intact, resetting it only after the set is actually created. If a step in the plan does not support `clone` (`NOT_IMPLEMENTED`), skip the in-place optimization and return `nullptr` *without* consuming `source` — fail-close, so the deferred build can always rebuild the set. This makes the exception impossible for every plan shape; losing primary key analysis for a non-clonable subquery only costs performance. The speculative build no longer shares its result through `PreparedSetsCache`, so a silent failure cannot poison the key for the deferred build. Trivial `clone` overrides are added for the common query plan steps so the optimization is preserved for ordinary subquery shapes. The physical `JoinStep` is deliberately left without `clone`: stored source plans are logical (joins are `JoinStepLogical`, already clonable), so join subqueries take the safe fallback instead of a fragile deep-clone of `IJoin`. A `ONCE` failpoint `prepared_sets_build_ordered_set_inplace_fail` is added to `CreatingSetsTransform::generate` (skips `finishInsert`) so the regression test `04357_not_ready_set_inplace_build` exercises the silent-failure path deterministically. This is an alternative to #102192 that follows the fail-close principle (its `NOT_IMPLEMENTED` fallback consumed `source`, leaving the bug latent) and drops the risky `JoinStep` deep-clone. Related: #102192 Related: #107619 Related: #104130 Related: #102803 Related: #102981 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [9685248] Summary: ✅
AI ReviewSummaryThis PR fixes the silent-failure Findings
Final VerdictStatus: |
… to destructive build for non-clonable sources The previous version skipped the in-place optimization entirely (returning `nullptr`) whenever a source step did not support `clone`. That regressed primary key / skip index pruning for every subquery whose source is not clonable — most notably `ReadFromPreparedSource`, which wraps an already-materialized, single-use `Pipe` (dictionary, many system table, and remote reads go through it). It broke the EXPLAIN-based pruning tests `03302_analyzer_distributed_filter_push_down`, `04202_inverse_dictionary_lookup_pruning_key_condition`, `03928_materialized_cte_index`, `03928_materialized_cte_index_2`, `01700_system_zookeeper_path_in`, `02859_replicated_db_name_zookeeper`, and `01064_incremental_streaming_from_2_src_with_feedback`. For a non-clonable source, fall back to the original destructive `build` instead, so primary key analysis is still performed for such subqueries exactly as before; only the rare silent-failure case stays unrecoverable there, which is no worse than the previous behavior (the deferred build throws `Not-ready Set is passed as the second argument`, it does not reuse a partial set). Clonable sources still take the safe non-destructive path. Also address the review comment that the speculative build mutated the canonical `set_and_key->set` before the build was committed: with `overflow_mode = 'break'` the pipeline can stop after `CreatingSetsTransform::consume` has inserted a prefix of rows but before `Set::finishInsert`, leaving partial rows in the set that the deferred build reuses — corrupting the result of a non-deterministic subquery such as one using `rand`. The non-destructive path now builds into a temporary `Set` and publishes it into `set_and_key` only after `isCreated()`, so an interrupted build never leaks partial rows into the canonical set. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…committed speculative build The non-destructive path gave the speculative `CreatingSetsTransform` the real `GLOBAL IN` `external_table`. `CreatingSetsTransform::consume` writes each block to that table before `generate` / `finishInsert`, so a pass stopped by `timeout_overflow_mode = 'break'` (or the failpoint) would leave a prefix of rows in the temporary table; the deferred build then appends a second evaluation, and remote shards would see the failed prefix plus the real build while the local `Set` contains only the real build. Keep `external_table` null on the temporary `SetAndKey` so the speculative pipeline never touches the real table, and populate it from the committed set afterwards via `buildExternalTableFromInplaceSet` (the same path `setExternalTable` uses), only once `isCreated()` is true. The deferred build is then skipped, so this is the single, atomic population of the table. On the destructive fallback the external table is still written by `build`'s `CreatingSetsTransform`, exactly as before. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The non-destructive in-place set build added by this PR (cloning `source` instead of consuming it) left the `GLOBAL IN` / `GLOBAL JOIN` external table untouched during `FutureSetFromSubquery::buildOrderedSetInplace` and repopulated it from the committed set afterwards via `buildExternalTableFromInplaceSet`. That broke two things: - The network transfer limits `max_rows_to_transfer` / `max_bytes_to_transfer` are enforced in `CreatingSetsTransform::consume` only while streaming rows into the external table. With the table left empty during the speculative build the check never fired, so queries that must fail with `SET_SIZE_LIMIT_EXCEEDED` succeeded silently. This is the regression caught by `04204_global_in_join_max_rows_to_transfer_103333` under the old analyzer. - `Set::insertFromColumns` drops the explicit set elements once `use_index_for_in_with_subqueries_max_values` is exceeded, so rebuilding the external table from the set elements would lose rows. Restrict the non-destructive path to subqueries without an external table. The `GLOBAL IN` / `GLOBAL JOIN` case now uses the destructive `build`, exactly as before this PR: it streams the subquery output into the external table during the pipeline run and enforces the transfer limits there. The `Not-ready Set` fix is retained for the common local `IN` case. Also document why `DelayedCreatingSetsStep` (added to the source plan by a nested `IN`) is intentionally left non-clonable: it holds the inner subqueries by shared pointer and building it consumes their `source`, so a shallow clone would consume the inner sources and mutate the canonical inner sets anyway, giving no real preservation; that shape takes the destructive fallback, no worse than before. Tests: renumber `04357_not_ready_set_inplace_build` to `04401` (the `04357` prefix now collides with a test merged to master), extend it with a nested `IN` case, and add `04402_global_in_set_max_values_external_table` covering `GLOBAL IN` with a small `use_index_for_in_with_subqueries_max_values`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Pushed d3cc487 (on top of a clean master merge) to address the AI Review ❌ Block and the failing Root cause. The non-destructive in-place build left the Fix. Take the non-destructive (clone) path only when there is no external table. The Nested Tests.
Verified locally against a server built from this branch: |
…llision After merging master, many other tests now use the 04401 and 04402 prefixes. Move both regression tests added by this PR (`not_ready_set_inplace_build` and `global_in_set_max_values_external_table`) to the next free numbers. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Merged a clean `master` (the branch was 226 commits behind and the only red checks were flakes). The merge had no conflicts and the net diff is unchanged (34 files, +346/-13); test numbers `04489`/`04490` still do not collide with master (which tops out at `04488`). Re-checked `PreparedSets.cpp`, `CreatingSetsTransform.cpp`, `FailPoint.cpp`, `SortingStep.cpp`, and `TotalsHavingStep.cpp` against the merged tree with The two non-green checks are both |
…eline On the non-destructive (clone) path of `FutureSetFromSubquery::buildOrderedSetInplace`, the speculative pipeline is built from `source->clone()`. `QueryPlan::clone` copies the plan nodes only, not the `QueryPlanResourceHolder`, so the cloned plan — and therefore the pipeline built from it — carries an empty resource holder. The interpreter contexts, storage holders, and table locks that processors may use implicitly (including in their destructors) are held only by the original `source`. Previously `source.reset()` ran while `executor` and `pipeline` were still alive, releasing those resources before the processors were destroyed. Run the pipeline in its own scope so the executor and pipeline are destroyed first, and reset `source` only afterwards. On the destructive fallback `build` moved the resources into `plan`, which already outlives the scope, so the ordering is correct there too. Also document that a `MATERIALIZED` CTE referenced by a local `IN` subquery contains a non-clonable `DelayedMaterializingCTEsStep` and therefore takes the destructive fallback, identical to the pre-PR behavior for that shape. This addresses the AI Review lifetime finding on the cloned speculative pipeline. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…t-inplace-build Fix "Not-ready Set" exception by making in-place set build non-destructive
`prepared_sets_build_ordered_set_inplace_fail` was added in ClickHouse#107924 to simulate a *speculative* in-place set build that stops without creating its set, so that the runtime deferred build has to recover it. The injection lives in `CreatingSetsTransform::generate()`, but that transform serves both builds: the in-place one (`FutureSetFromSubquery::buildSetInplace` / `::buildOrderedSetInplace`) and the ordinary runtime one under `DelayedCreatingSetsStep`. The failpoint's own comment asserted the narrow scope, and nothing enforced it: it held only while an in-place build happened to be the first `generate()` call after the arm. `SYSTEM ENABLE FAILPOINT` is server-wide and a `ONCE` arm survives until the first `generate()` call anywhere in the process. When that call belongs to an unrelated query's runtime build, `generate()` returns before `Set::finishInsert()`, so `Set::isCreated()` stays false, `FutureSetFromSubquery::get()` returns nullptr, and `FunctionIn::executeImpl` raises LOGICAL_ERROR from inside an already-running `FilterTransform`, aborting a debug or sanitizer server. Only the in-place path has a recovery, so nothing rescues the runtime build. In CI this is armed on purpose by BuzzHouse and replayed verbatim by the AST fuzzer out of the corpora of 04489_not_ready_set_inplace_build.sql and 04697_shared_set_error_not_inherited.sh, which is why the abort lands on queries that never asked for a set at all. Carry the discriminator on the individual set build: `CreatingSetStep` gains `speculative_build`, threaded through `FutureSetFromSubquery::build()` and set true only at the three in-place construction sites, and `CreatingSetsTransform::generate()` consults it before `fiu_do_on`. The check precedes the injection rather than sitting inside it, because `fiu_fail` on a `FIU_ONETIME` point consumes the shot: an inside check would let a runtime build spend the shot and leave the speculative build it was armed for running unfaulted. The flag deliberately travels on the step and not on `BuildQueryPipelineSettings`. One settings object serves an entire plan, and `addPlansForSets` splices runtime set plans in as children of that same plan, so a pipeline-wide flag would also mark runtime `CreatingSetStep`s nested inside an in-place build's plan, including lazy FINAL's internal set. That is a narrower version of the same bug. For the same reason the nested `DelayedCreatingSetsStep` an in-place destructive fallback can carry expands through `makePlansForSets` with the flag false, so its inner canonical sets stay classified as runtime and the shot cannot abandon them. Every new parameter is defaulted, and all five `build()` call sites state their value explicitly so a future caller has to make the choice rather than inherit it. The injected state is otherwise byte-identical, which is why 04489 and 04697 pass unchanged; they remain the live proof that the in-place injection still fires. Related: ClickHouse#118722 Co-Authored-By: Claude Opus 5 <[email protected]>
…ause `master` has since merged and repeatedly extended its own fix for the lost subquery plan in `FutureSetFromSubquery::buildOrderedSetInplace` (ClickHouse#107924 and follow-ups: clone-based speculative build, `in_place_build_failure`, `inplace_build_mutex`, the recoverable-build scope of the `prepared_sets_build_ordered_set_inplace_fail` failpoint, distributed-plan support). This branch's alternative implementation (query-plan rebuild callbacks, the `GLOBAL IN` transfer-limit deferral and the `hasUnbuiltSubquerySet` guards for synchronous consumers) conflicts with all of it and can no longer be reconciled without re-implementing those changes. Conflict resolution: take `master`'s implementation of the in-place set build (`PreparedSets.{h,cpp}`, `QueryPlan.cpp`, `IQueryPlanStep`, the call sites of `addFromSubquery`, `MergeTreeData.cpp`, `VirtualColumnUtils`, `03269_explain_unique_ids.reference`, `ci/defs/darwin.skip`) and drop the tests that only covered the dropped machinery (`04093` duplicates `master`'s `04489`; `04401` covered the transfer-limit deferral). The part `master` still lacks, the `GLOBAL IN` external-table case, is ported on top in the next commit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Split out of ClickHouse#102192. `ExtractColumnsStep` did not implement `clone`, so an `IN` subquery whose source plan contains it made `FutureSetFromSubquery::buildOrderedSetInplace` take the destructive fallback introduced in ClickHouse#107924. Also add the regression tests for the "Not-ready Set is passed as the second argument" exception fixed by ClickHouse#107924, using the `prepared_sets_build_ordered_set_inplace_fail` failpoint: the original parallel replicas reproducer and a `WITH FILL` subquery source plan. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Split out of ClickHouse#102192. With `query_cache_for_subqueries = 1` an `IN` subquery plan contains `StreamInQueryResultCacheStep` (write path) or is replaced by `ReadFromQueryResultCacheStep` (cache hit). Neither implemented `clone`, so `FutureSetFromSubquery::buildOrderedSetInplace` took the destructive fallback (see ClickHouse#107924), where a silent in-place build failure still leads to the "Not-ready Set is passed as the second argument" exception. - `StreamInQueryResultCacheStep::clone` gives the clone its own copy of the `QueryResultCacheWriter`. - The `QueryResultCacheWriter` copy now carries over `query_start_time`, so `query_cache_min_query_duration` is not measured from the clone point. On the successful in-place build path the copy is the only writer that is finalized. - `ReadFromQueryResultCacheStep` keeps its `SourceFromChunks` and clones them via the new `SourceFromChunks::clone`, which shares the immutable columns. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Split out of ClickHouse#102192. `IN` subqueries whose plans contain a `JoinStep` took the destructive fallback of `FutureSetFromSubquery::buildOrderedSetInplace` (see ClickHouse#107924), where a silent in-place build failure still leads to the "Not-ready Set is passed as the second argument" exception. `JoinStep::clone` deep-clones the `IJoin` (respecting `swap_streams`) for the algorithms that support `IJoin::clone`: `HashJoin`, `ConcurrentHashJoin`, `ConstantJoin` and `FullSortingMergeJoin`. Other algorithms throw `NOT_IMPLEMENTED` and keep the fallback; widening `IJoin::isCloneSupported` would also enable join swapping and outer-to-inner conversion for them. Document why `FilledJoinStep` and `JoinStepLogicalLookup` stay non-clonable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #102192
Related: #107619
Related: #104130
Related: #102803
Related: #102981
FutureSetFromSubquery::buildOrderedSetInplaceis a speculative build run during primary key / skip index analysis for anINsubquery (KeyCondition,ReadFromMergeTree::selectRangesToRead, including theconsiderEnablingParallelReplicasestimation path). It built the set through the destructivebuild, which moves the subquerysourceplan out of the future-set; constructing a pipeline then consumes it.If the in-place pipeline stops without creating the set — a subquery timeout with
overflow_mode = 'break', or any early stop that skipsCreatingSetsTransform::generateand thereforeSet::finishInsert—buildOrderedSetInplacereturnsnullptr, butsourcehas already been consumed. The authoritative deferred build (DelayedCreatingSetsStep::makePlansForSets) then findssource == nullptr, attaches no set-building step to the pipeline, andFunctionInthrowsNot-ready Set is passed as the second argumentwhen the query runs.Fix. Run the in-place pipeline against a clone of
source(via the existingQueryPlan::clone) and keep the original intact, resetting it only after the set is actually created. The speculative pipeline builds into a temporarySetthat is published into the canonicalset_and_keyonly once it is fully created, so a partial prefix of rows from an interrupted build never leaks into the set that the deferred build reuses (which would otherwise corrupt the result of a non-deterministic subquery such as one usingrand). The speculative build no longer shares its result throughPreparedSetsCache, so a silent failure cannot poison the key for the deferred build.Some source steps cannot be cloned — most notably
ReadFromPreparedSource, which wraps an already-materialized, single-usePipe(dictionary, many system table, and remote reads go through it). For a non-clonable source the code falls back to the original destructive build, so primary key analysis is still performed for such subqueries exactly as before; only the rare silent-failure case stays unrecoverable there, which is no worse than the previous behavior (the deferred build throws, it does not reuse a partial set). Trivialcloneoverrides are added for the common query plan steps so the safe non-destructive path is taken for ordinary subquery shapes. The physicalJoinStepis deliberately left withoutclone: stored source plans are logical (joins areJoinStepLogical, already clonable), so join subqueries take the destructive fallback instead of a fragile deep-clone ofIJoin.A
ONCEfailpointprepared_sets_build_ordered_set_inplace_failis added toCreatingSetsTransform::generate(it skipsfinishInsert), so the regression test04489_not_ready_set_inplace_buildexercises the silent-failure path deterministically.This is an alternative to #102192. That PR adds a 33-line
JoinStep::clonethat deep-clonesIJoinand changes behavior for the othercloneconsumers; this PR drops that deep-clone, builds the speculative set into a temporary set so an interrupted build cannot corrupt the canonical set, and keeps the speculative build out ofPreparedSetsCache.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix the
Not-ready Set is passed as the second argumentexception that could occur when building anINsubquery set during primary key analysis failed silently (for example, a subquery timeout withoverflow_mode = 'break'), leaving the set permanently unbuilt for the query pipeline.Documentation entry for user-facing changes
Version info
26.7.1.802(included in26.7and later)