Repository navigation
Do not use parallel replicas for a correlated subquery in a mergeTreeAnalyzeIndexes predicate - #119860
Conversation
…AnalyzeIndexes predicate With parallel replicas enabled, a mergeTreeAnalyzeIndexes predicate whose IN subquery contains a correlated subquery tripped a LOGICAL_ERROR in the parallel replicas coordinator: "Initiator received more initial requests than there are replicas: replica_num=N", or its sibling "Duplicate announcement received for replica number N" when the initiator has a local plan. A release build returns Code: 49; a debug build aborts. Found by the AST fuzzer. mergeTreeAnalyzeIndexes keeps its predicate as an AST and turns it into a query tree at execution time, resolving it with QueryAnalyzer::resolveConstantExpression only. addQueryTreePasses does not run on that tree, so DisableParallelReplicasPass never saw the predicate and the IN subquery was hand-planned and shipped to the replicas with parallel replicas still enabled. Each follower re-plans that SQL; decorrelation references the outer subplan, and when correlated_subqueries_use_in_memory_buffer is off, materializeQueryPlanReferences replaces the reference with a clone of that subplan, ReadFromMergeTree included. Two reads of one table share one stream_id, so getOrCreateCoordinator hands both the same coordinator and the replica announces twice. Until 2825975 this function disabled parallel replicas for its own predicate directly. That commit moved the disable into distributedIndexAnalysisOnReplicas, which does not cover a directly invoked table function. Rather than re-adding a blanket disable, run the missing pass on the resolved predicate tree: the rule then lives in one place, and the pass covers nested QueryNode/UnionNode contexts, which the planner reads separately. Running it on the resolved tree also means a SETTINGS clause on the subquery itself cannot re-enable parallel replicas, since QueryTreeBuilder has already applied that clause to the subquery's own context. The pass fires only when a correlated subquery is present, so an uncorrelated IN (SELECT ...) predicate keeps parallel replicas and the deliberate move in 2825975 stands. The regression test asserts both directions. The sibling site in MergeTreeData.cpp that resolves a predicate the same way is unaffected: getPartitionIdsPrunedByPredicate returns nullopt for any predicate containing an ASTSubquery before it plans one. Model: us.anthropic.claude-opus-5 Co-Authored-By: Claude Opus 5 <[email protected]>
The tag's stated reason was that the test queries `system.text_log`, but every read there is keyed on a `query_id` built from the test's own database name, so a test running concurrently cannot perturb an assertion. Verified with 50 runs across 8 workers, and with the test interleaved with four other `system.text_log` readers. `no-parallel-replicas` stays, because `tests/clickhouse-test` reads it three times and this test manages the parallel replicas settings itself: it keeps `enable_parallel_replicas` off unless the test turns it on, it excludes the test from the `automatic_parallel_replicas_mode = 2` bundle that would otherwise repoint `cluster_for_parallel_replicas` at a cluster with no local replica, and it skips the test in the run that reports which tests are unsupported with parallel replicas. Narrow the comment above the new `DisableParallelReplicasPass` call, in the source and in the test header. Both said that `resolveConstantExpression` does not run `addQueryTreePasses`, which is too wide a claim about that function: it hands a query or union node to `evaluateScalarSubqueryIfNeeded`, which builds a `QueryTreePassManager` and does run the passes. What holds is the tree-scoped statement, so the comment now says the passes do not run on this tree. No behaviour changes: the binary is bit-identical to the previous commit's. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=95761&sha=2e03d4382b8fa296098f36e8897e581397ea0d37&name_0=PR&name_1=AST%20fuzzer%20(amd_debug,%20targeted,%20old_compatibility) Related: ClickHouse#95761 Related: ClickHouse#117610 Model: us.anthropic.claude-opus-5 Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings), plus my own cold read of
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260913-185300 |
|
Workflow [PR], commit [ec90cc6] Summary: ✅
AI ReviewSummaryThis PR restores the missing Final Verdict
LLVM Coverage ReportMeasured on commit ec90cc6.
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 15 s compile time in total, 7 of them have a recent master baseline. |
d58fa23
Related: #95761
Related: #117610
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a
LOGICAL_ERROR(Initiator received more initial requests than there are replicas, orDuplicate announcement received for replica number N) when amergeTreeAnalyzeIndexespredicate contained a correlated subquery and parallel replicas were enabled.Description
With parallel replicas enabled, a
mergeTreeAnalyzeIndexespredicate whoseINsubquery contains acorrelated subquery fails:
A release build returns
Code: 49 LOGICAL_ERROR; a debug build aborts. Found by the AST fuzzer:https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=95761&sha=2e03d4382b8fa296098f36e8897e581397ea0d37&name_0=PR&name_1=AST%20fuzzer%20(amd_debug,%20targeted,%20old_compatibility)
mergeTreeAnalyzeIndexeskeeps its predicate as an AST and turns it into a query tree at executiontime, resolving it with
resolveConstantExpressiononly.addQueryTreePassesdoes not run on thattree, so
DisableParallelReplicasPassnever saw the predicate and theINsubquery was shipped tothe replicas with parallel replicas still enabled. Decorrelating it on a replica materializes the
referenced subplan a second time, so one table is read twice in one plan, both reads share a
stream_id, and the replica announces twice on the same coordinator.Until
2825975ac13a5this function disabled parallel replicas for its own predicate; that commitmoved the disable into the distributed-index-analysis driver, which does not cover a directly invoked
table function. Running the missing pass instead keeps the rule in one place. It fires only when a
correlated subquery is present: an uncorrelated
IN (SELECT ...)predicate still uses parallelreplicas, which the new test asserts.
Results are unchanged, which the test also asserts. The sibling site in
MergeTreeData.cppisunaffected: it bails out on any predicate containing a subquery before planning one.
Validation: the failure reproduces deterministically and is gone with the fix, each assertion of the
new test flipping individually; 50/50 randomized runs; the 21 existing tests calling
mergeTreeAnalyzeIndexespass.Workflow [PR]
Sync PR [sync-upstream/pr/119860]
Version info
26.10.1.217(included in26.10and later)