Skip to content

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

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.

Metadata

Metadata

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions