Repository navigation
Fix wrong results of a subquery that enables parallel replicas in its own SETTINGS when a projection answers part of it - #131618
Conversation
…ections with parallel replicas The aggregate and normal projection optimizers decided whether a coordinated read is the initiator's own share (and so must return what a projection or a prepared source answers) from `QueryPlanOptimizationSettings::is_parallel_replicas_initiator_with_projection_support`, computed once from the context the whole plan is optimized with. The initiator's local plan is optimized with a copy of the OUTER settings, so when parallel replicas are enabled only in a subquery's SETTINGS the flag is false there: the initiator emitted a NullSource for the `_minmax_count_projection`, `_statistics_projection` and `_exact_count_projection` blocks and skipped the projection read beside parts without the projection, while the followers do the same by design. `min`/`max`/`count` came back as 0 (since ClickHouse#82807), `sum`/`avg` as 0/nan (since ClickHouse#123950), and projection-answered rows were dropped. Use `ReadFromMergeTree::isParallelReplicasLocalPlanForInitiator()`, which reads the read's own context and is already used by the same code for the empty-ranges announcement (and its follower twin for the follower failpoints), and remove the plan-wide field. For a query whose outer and subquery contexts agree the answer is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Assert in `system.query_log` that every query of the test is answered by the expected projection or prepared source, not only that parallel replicas ran it. Pin `auto_statistics_types = 'basic'` on the table and clamp `materialize_statistics_on_insert` to 1, so that the `sum`/`avg` query is answered from the per-part statistics in every randomized run (without `basic` statistics or with statistics not materialized on insert it read the parts and passed on master too). Move the parallel replicas settings that only matter in the subquery to the session, so each subquery enables parallel replicas with one setting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (2 rounds, head 390072d): 1 finding, fixed; round 2 clean
Round 2 (test-only delta): no findings. |
|
Workflow [PR], commit [390072d] Summary: ✅
AI ReviewSummaryReviewed #131618 at Final VerdictStatus: ✅ Approve |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261114-115800 |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units161 translation units recompiled, 1439 s compile time in total, 161 of them have a recent master baseline. |
5c48ad1
|
The release branches still have this bug. The repro in #134035 (a subquery with parallel replicas in its own This PR has no backport, so these branches would need |
Related: #115916
Related: #123950
Related: #82807
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of a subquery that enables parallel replicas in its own
SETTINGSclause when the result comes from a projection, themin/max/count()part metadata or an exact count from the primary key. For example,SELECT * FROM (SELECT max(x) FROM t SETTINGS enable_parallel_replicas = 1, ...)returned 0.Description
On 26.3.33.24 and 26.10.2.103,
SELECT * FROM (SELECT max(k) FROM t SETTINGS enable_parallel_replicas = 1, max_parallel_replicas = 3, cluster_for_parallel_replicas = '...', parallel_replicas_for_non_replicated_merge_tree = 1)returns 0 instead of the maximum; with the same settings on the top-level query it is correct. On mastersum()returns 0 the same way, which is the red05033_recursive_cte_parallel_replicasin the Fast test of #115916.The projection optimizers decided whether a coordinated read is the initiator's own share from
is_parallel_replicas_initiator_with_projection_support, computed once from the context the plan is optimized with. The initiator's local plan is optimized with the outer query's settings, so with parallel replicas enabled only in the subquery the initiator took the follower branch: it returned an empty source instead of the_minmax_count_projection,_statistics_projectionor_exact_count_projectionblock and skipped the projection read beside parts that lack the projection, which the followers skip by design.The decision now uses
ReadFromMergeTree::isParallelReplicasLocalPlanForInitiator(), which reads the read's own context and already answers the same question for the empty-ranges announcement in the same code; the plan-wide field is removed. Queries whose outer and subquery settings agree are unchanged.The
sum/avghalf came with #123950, the rest with #82807, so reverting #123950 alone would not fix it.The test covers the five sources and checks that parallel replicas and the expected source served each query. It fails on master and passes 50/50 with random settings.
Workflow [PR]
Sync PR [sync-upstream/pr/131618]
Version info
26.11.1.2543-master(included in26.11and later)