Skip to content

stable-25.8: correlated subquery over MergeTree crashes under parallel replicas — Cannot clone ReadFromPreparedSource (NOT_IMPLEMENTED) #2051

Closed

Description

@CarlosFelipeOR

Type of problem

Bug report — latent 25.8 bug (present upstream 25.8, fixed on master) exposed by the ParallelReplicas CI config.

Describe the situation

Test 04299_constraint_subst_correlated_subquery_root fails consistently on stable-25.8 in the job Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel) (7/7 failures in that config; all other configs OK).

The cause: constraint-optimization decorrelation of a correlated subquery clones the query plan (PlannerCorrelatedSubqueries.cpp:273, context.query_plan.clone()). Under parallel replicas the source read is a ReadFromPreparedSource step, whose clone() is not implemented, so the query aborts with Code: 48 ... Cannot clone ReadFromPreparedSource plan step (NOT_IMPLEMENTED).

This is not a regression and not Altinity-specific: the identical code exists in upstream 25.8. It never fails in upstream's 25.8 CI because that branch's stateless matrix runs only amd_asan, distributed plan — it does not run the ParallelReplicas config (confirmed: 0 of ~1.95M rows). Altinity MasterCI runs the broader matrix (incl. ParallelReplicas), so it surfaces. It is fixed on upstream master by commit dc187196cd5 ("Implement subplan reference materialization as plan optimization"), which removes the plan clone.

How to reproduce

CREATE TABLE t (a Nullable(String), b String, c LowCardinality(String), d String,
    CONSTRAINT c1 ASSUME (a <= b) AND (b <= c) AND (c <= d) AND (d <= a))
ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t VALUES ('1','2','3','4');
-- with parallel replicas enabled:
SELECT count() FROM t WHERE (b < d) OR (SELECT a < c)
SETTINGS enable_analyzer=1, convert_query_to_cnf=1, optimize_substitute_columns=1, optimize_using_constraints=1;

Expected behavior

Query returns a result (as it does without parallel replicas / on master).

Actual behavior

Code: 48. DB::Exception: Cannot clone ReadFromPreparedSource plan step. (NOT_IMPLEMENTED)
  ... IQueryPlanStep::clone() ... QueryPlan::clone()
  ... decorrelateQueryPlan ... buildQueryPlanForCorrelatedSubquery
  ... executeQueryWithParallelReplicas

Suggested fix

  • Proper fix: upstream master commit dc187196cd5 ("Implement subplan reference materialization as plan optimization") removes the plan clone in decorrelation. Note: this fix is not in upstream 25.8 either, and it's a large, non-trivial backport — 14 files (+265/-71) introducing new plan infrastructure (SaveSubqueryResultToBufferStep, ReadFromCommonBufferStep, a new materializeQueryPlanReferences optimization pass), not an isolated cherry-pick.
  • CI mitigation (separate PR): tag 04299_constraint_subst_correlated_subquery_root with no-parallel-replicas.

Activity

  1. added
    bug
    25.8
    on Jul 15, 2026
  2. CarlosFelipeOR commented on Jul 27, 2026

    @CarlosFelipeOR
    CollaboratorAuthor

    Manual reproduction

    Reproduced with a local 3-replica cluster (all replicas on localhost) so parallel replicas engages on a non-replicated MergeTree.

    Query:

    CREATE TABLE t_constraint_corr_root
    (
        a Nullable(String), b String, c LowCardinality(String), d String,
        CONSTRAINT c1 ASSUME (a <= b) AND (b <= c) AND (c <= d) AND (d <= a)
    )
    ENGINE = MergeTree ORDER BY tuple();
    INSERT INTO t_constraint_corr_root VALUES ('1','2','3','4');
    
    SELECT count() FROM t_constraint_corr_root
    WHERE (b < d) OR (SELECT a < c)
    SETTINGS
        enable_analyzer = 1, convert_query_to_cnf = 1,
        optimize_substitute_columns = 1, optimize_using_constraints = 1,
        enable_parallel_replicas = 2,
        cluster_for_parallel_replicas = 'test_cluster_one_shard_three_replicas_localhost',
        parallel_replicas_for_non_replicated_merge_tree = 1,
        max_parallel_replicas = 3, parallel_replicas_local_plan = 1;

    On 25.8 it fails with Code 48:

    Received exception from server (version 25.8.28):
    Code: 48. DB::Exception: Received from localhost:9000.
    DB::Exception: Cannot clone ReadFromPreparedSource plan step. (NOT_IMPLEMENTED)
    

    Cross-version results (same query)

    Version Result
    upstream 25.8.26 Code: 48 — Cannot clone ReadFromPreparedSource (NOT_IMPLEMENTED)
    Altinity 25.8.28 same — Code: 48 NOT_IMPLEMENTED
    upstream 26.1.12 💥 segfault (ColumnNode::getColumnSource)
    Altinity antalya-26.1 💥 segfault — identical to upstream
    upstream 26.3.17 ✅ works (fixed)
    Altinity 26.3.16 ✅ works (fixed)

    On 26.1 it fails even worse: instead of the clean NOT_IMPLEMENTED, the query crashes the server (segfault in the correlated-subquery planner — a different, worse code path). But the antalya-26.1 crash is identical to upstream 26.1, and we do not plan to ship a new antalya-26.1 release, so no fix is needed there.

    On 25.8, the failure matches upstream 25.8 and is fixed from 26.3 onward (both upstream and Altinity).

    Decision: no backport to 25.8. Mark 04299_constraint_subst_correlated_subquery_root as broken on both stable-25.8 and antalya-25.8.

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

Metadata

Metadata

Labels

25.8bugstablestable-25.8

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions