Skip to content

Disable parallel replicas in nested scalar subquery contexts - #107381

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-scalar-subquery-parallel-replicas-leak
Jun 13, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-scalar-subquery-parallel-replicas-leak

Conversation

@groeneai

@groeneai groeneai commented Jun 13, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix Logical error: 'Duplicate announcement received for replica number N' that could occur with parallel replicas when a scalar subquery contained nested subqueries reading the same table.

Description

A scalar subquery must never use parallel replicas. evaluateScalarSubqueryIfNeeded clones the scalar subquery tree and sets allow_experimental_parallel_reading_from_replicas = 0 on the subquery context, but it assigns that context only to the top QueryNode/UnionNode of the clone. Nested QueryNode/UnionNode children keep their original, parallel-replicas-enabled context.

The Planner reads the context from every node, so the scalar's plan is built with parallel replicas during analysis and creates ParallelReplicasReadingCoordinator instances. When the same table is read twice in one such plan (for example a correlated scalar that decorrelates into a self-join), both reads announce on the same coordinator with the same replica number, and the second announcement trips the assertion Duplicate announcement received for replica number N in ParallelReplicasReadingCoordinator.cpp.

The fix walks the whole cloned subquery tree and disables parallel replicas on every nested QueryNode/UnionNode context, mirroring the existing recursive walk in createLocalPlanForParallelReplicas.

Found by: AST fuzzer (AST fuzzer (amd_debug)), two occurrences on unrelated PRs:

No related open issue found. The two PRs above are unrelated to parallel replicas; this is a trunk bug surfaced by the fuzzer.

Regression test 04337_parallel_replicas_scalar_subquery_no_leak asserts (via system.text_log) that a scalar subquery over a parallel-replicas table creates no coordinator, while a top-level query over the same data still does. It fails before the fix (1 and 2 coordinators created during scalar analysis) and passes after.

Version info

  • Merged into: 26.6.1.757

evaluateScalarSubqueryIfNeeded clones the scalar subquery tree and sets
allow_experimental_parallel_reading_from_replicas = 0 on the subquery
context, but assigns that context only to the top QueryNode/UnionNode of
the clone. Nested QueryNode/UnionNode children keep their original
parallel-replicas-enabled context.

The Planner reads the context from every node, so the scalar's plan is
built with parallel replicas during analysis and creates
ParallelReplicasReadingCoordinator instances. When the same table is read
twice in one such plan (e.g. a correlated scalar that decorrelates into a
self-join), both reads announce on the same coordinator with the same
replica number and the second announcement trips the assertion
"Duplicate announcement received for replica number N"
(ParallelReplicasReadingCoordinator.cpp). Found by the AST fuzzer.

Walk the whole cloned tree and disable parallel replicas on every nested
QueryNode/UnionNode context, mirroring the existing recursive walk in
createLocalPlanForParallelReplicas (a scalar subquery must never use
parallel replicas by construction).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. On a master debug build with parallel replicas enabled (test_cluster_one_shard_three_replicas_localhost, parallel_replicas_local_plan=1), SELECT (SELECT count() FROM (SELECT a FROM t UNION ALL SELECT a FROM t)) deterministically creates 2 coordinators during scalar analysis (1 for the single-nested variant), observable in system.text_log (Creating parallel replicas coordinator). The duplicate-announcement crash itself is the rarer symptom (needs two same-table reads on one coordinator), but the underlying leak it stems from is 100% reproducible.
b Root cause explained? Yes. evaluateScalarSubqueryIfNeeded sets allow_experimental_parallel_reading_from_replicas=0 on the subquery context but assigns it only to the top cloned QueryNode/UnionNode; nested children keep the parallel-replicas-enabled context. The Planner reads each node's context, so the scalar's plan is built with parallel replicas during analysis, creating ParallelReplicasReadingCoordinator instances; two same-table reads in one plan announce twice on the same coordinator -> Duplicate announcement received for replica number N.
c Fix matches root cause? Yes. The fix recursively disables parallel replicas on every nested QueryNode/UnionNode context of the cloned scalar tree, closing the leak at its source (a scalar subquery must never use parallel replicas). It mirrors the existing proven recursive walk in createLocalPlanForParallelReplicas, not a symptom guard at the assertion.
d Test intent preserved / new tests added? New regression test 04337_parallel_replicas_scalar_subquery_no_leak. It asserts a scalar subquery creates 0 coordinators while a top-level query over the same data still creates them (parallel replicas keeps working) and the scalar results are unchanged.
e Both directions demonstrated? Yes. Against the same data: unfixed build creates 1 and 2 coordinators (test fails), fixed build creates 0 and 0 (test passes). The control line (top-level query uses parallel replicas) and the result-equality line are identical on both builds, confirming the fix is targeted.
f Fix is general, not a narrow patch? Yes. The fix is at the source (the context propagation in evaluateScalarSubqueryIfNeeded), covering all nested subquery shapes (UNION, EXCEPT, nested SELECT, correlated self-join), not the single fuzzer query. It does not add a defensive check at the coordinator assertion. The sibling path createLocalPlanForParallelReplicas already performs the same recursive walk, so no other site needs the same change.

Session id: cron:clickhouse-worker-slot-0:20260613-083100

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @nickitat @novikd — could you review this? A scalar subquery kept parallel replicas enabled in its nested QueryNode/UnionNode contexts (evaluateScalarSubqueryIfNeeded set the flag only on the top node), so the scalar's plan was built with parallel replicas during analysis and two same-table reads announced twice on one coordinator -> "Duplicate announcement received for replica number N" (AST fuzzer finding). The fix applies the same recursive context walk already used by createLocalPlanForParallelReplicas.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [591768e]

Summary: ✅


AI Review

Summary

This PR disables allow_experimental_parallel_reading_from_replicas recursively on cloned scalar-subquery QueryNode and UnionNode contexts, so nested scalar subquery planning cannot create ParallelReplicasReadingCoordinator instances. The focused stateless regression covers both nested subquery and nested UNION ALL shapes, and keeps a top-level parallel-replicas control. I found no material correctness, safety, or test-coverage issues.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.70% 84.70% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.30% 77.30% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 22/22 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fix.

@alexey-milovidov alexey-milovidov self-assigned this Jun 13, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 13, 2026
Merged via the queue into ClickHouse:master with commit 55fb419 Jun 13, 2026
166 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default 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.

3 participants