Skip to content

Plan-based parallel replicas: run IN (subquery) locally instead of failing - #112443

Merged
devcrafter merged 5 commits into
masterfrom
fix-111876-pr-plan-based-disable-in-subquery
Jul 30, 2026
Merged

devcrafter merged 5 commits into
masterfrom
fix-111876-pr-plan-based-disable-in-subquery

Conversation

@devcrafter

@devcrafter devcrafter commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

Description

Under plan-based parallel replicas (parallel_replicas_plan_based = 1), a WHERE x IN (SELECT ...) predicate made the query fail: with parallel_replicas_local_plan = 0 it threw a LOGICAL_ERROR Cannot serialize FutureSetFromSubquery with no query plan, and with parallel_replicas_local_plan = 1 it hit Next task callback is not set (#111677).

The shipped fragment is captured (as a clone) during optimizeTreeSecondPass while the FutureSetFromSubquery still holds its subquery query plan, but the later addStepsToBuildSets pass calls FutureSetFromSubquery::build, which moves that plan out. Serializing the fragment at execution time then finds a null plan and throws.

Properly shipping the subquery set requires serializing it while its plan is still populated. This is a feature under active development, so until that is done this change disables distribution for any query that builds a set from a subquery and runs it as the plain local plan (correct results, no parallel replicas) — mirroring the existing FINAL handling in insertParallelReplicasSplit. The still-intact DelayedCreatingSetsStep is the signal; it carries only FutureSetFromSubquery sets, so a plain tuple IN (1, 2, 3) keeps being distributed. A TODO(#111876) records the follow-up.

Closes: #111876
Related: #111677

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Not for changelog: fixes behavior of the experimental parallel_replicas_plan_based feature, which is under active development.

Version info

  • Merged into: 26.8.1.392 (included in 26.8 and later)

…iling

Under plan-based parallel replicas (`parallel_replicas_plan_based = 1`), a
`WHERE x IN (SELECT ...)` predicate made the query fail: with
`parallel_replicas_local_plan = 0` it threw a `LOGICAL_ERROR` "Cannot serialize
FutureSetFromSubquery with no query plan", and with `parallel_replicas_local_plan = 1`
it hit "Next task callback is not set" (#111677).

The shipped fragment is captured (as a clone) during `optimizeTreeSecondPass` while
the `FutureSetFromSubquery` still holds its subquery query plan, but the later
`addStepsToBuildSets` pass calls `FutureSetFromSubquery::build`, which moves that plan
out. Serializing the fragment at execution time then finds a null plan and throws.

Properly shipping the subquery set requires serializing it while its plan is still
populated; until that is done, disable distribution for any query that builds a set
from a subquery and run it as the plain local plan (correct results, no parallel
replicas) - mirroring the existing FINAL handling in `insertParallelReplicasSplit`.
The still-intact `DelayedCreatingSetsStep` (present before `addStepsToBuildSets`
converts it) is the signal; it carries only `FutureSetFromSubquery` sets, so a plain
tuple `IN (1, 2, 3)` keeps being distributed. A `TODO(#111876)` records the follow-up.

Closes: #111876
Related: #111677

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2aa1567]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, parallel) FAIL
04367_distributed_plan_merge_scatter_multishard FAIL cidb, issue
Scraping system tables FAIL cidb, issue

AI Review

Summary

This PR avoids the FutureSetFromSubquery serialization/Next task callback is not set failures by keeping plan-based parallel-replica queries with subquery-built sets on the local plan, and the new stateless test now pins that stays-local behavior. The main gap is that the new fallback still bypasses the existing contract of enforced parallel replicas: when allow_experimental_parallel_reading_from_replicas >= 2, unsupported shapes are supposed to throw, not silently run locally.

Findings

⚠️ Majors

  • [src/Processors/QueryPlan/Optimizations/applyParallelReplicas.cpp:339] planHasSubquerySet now returns before any split is inserted, so parallel_replicas_plan_based = 1 quietly degrades IN (subquery) to a local plan even when the user explicitly set allow_experimental_parallel_reading_from_replicas >= 2 to require parallel replicas. That breaks the same unsupported-shape contract already enforced for FINAL and for parallel_replicas_allow_in_with_subquery = 0: this mode must reject unsupported plans with SUPPORT_IS_DISABLED, not ignore the requirement and succeed differently. Suggested fix: keep the local fallback for the soft mode, but add the same throw path for enforced mode, either here with access to the setting or earlier in the planner/interpreter alongside the existing guards.
Final Verdict

Changes requested: the local fallback is fine for the default mode, but the enforce-parallel-replicas contract still needs to be preserved.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 29, 2026
Address review feedback: the regression previously proved only that the query
returns the right rows, not that plan-based parallel replicas keeps the outer
`IN (subquery)` read local. A later change could distribute it again, still
return the same rows on this self-subquery, and stay green while re-introducing
the known-unsafe plan shape.

Add EXPLAIN optimize = 1 assertions (both parallel_replicas_local_plan modes)
that the outer read is not distributed: no `ReadFromParallelReplicas`, still a
local `ReadFromMergeTree`. The subquery appears as a reference in the plan, so
the count reflects only the outer read. A tuple `IN (1, 2, 3)` guard confirms
distribution is still applied where it is safe. Verified against a local server.
The TODO now points at flipping these assertions once #111876 is fixed.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
if (planHasFinalMergeTreeRead(root))
return;

if (planHasSubquerySet(root))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow_experimental_parallel_reading_from_replicas >= 2 is the "enforce parallel replicas" mode, and every other unsupported shape handles that by throwing instead of silently falling back to a local plan (FINAL in InterpreterSelectQuery, parallel_replicas_allow_in_with_subquery = 0 in Planner). This unconditional return bypasses that contract for the new unsupported IN (subquery) case: the query will now succeed locally even though the user explicitly required parallel replicas. If this combination must stay unsupported until #111876, it needs the same disable-or-throw handling as the existing guards instead of quietly ignoring the request.

@devcrafter devcrafter Jul 29, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point. In enforce mode (allow_experimental_parallel_reading_from_replicas >= 2) this should throw SUPPORT_IS_DISABLED like the existing IN with subquery (Planner.cpp, parallel_replicas_allow_in_with_subquery = 0) and FINAL guards, rather than silently running locally when the user required parallel replicas. Since that edits the planHasSubquerySet guard introduced in this PR, I'll add it in a follow-up PR off master once this one merges — keeping it out of a stacked PR that CI wouldn't run.

@devcrafter
devcrafter enabled auto-merge July 29, 2026 20:55
…-in-subquery' into fix-111876-pr-plan-based-disable-in-subquery
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.40% 86.40% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.50% 78.50% +0.00%

Changed lines: Changed C/C++ lines covered: 15/17 (88.24%) · Uncovered code

Full report · Diff report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plan-based parallel replicas: IN (subquery) throws LOGICAL_ERROR Cannot serialize FutureSetFromSubquery with no query plan

2 participants