Skip to content

Fix logical error in automatic parallel replicas with min_number_of_rows_per_replica - #109011

Merged
nickitat merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-autopr-min-rows-analyzed-result-assertion
Jul 4, 2026
Merged

nickitat merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-autopr-min-rows-analyzed-result-assertion

Conversation

@groeneai

@groeneai groeneai commented Jul 1, 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):

Fixed a LOGICAL_ERROR (local_replica_plan_reading_step->getAnalyzedResult() == nullptr) in the automatic parallel replicas planner that could occur when automatic_parallel_replicas_mode is enabled together with parallel_replicas_min_number_of_rows_per_replica greater than 0.

Description

considerEnablingParallelReplicas() asserted that the local parallel-replicas reading step carried no index analysis yet before transplanting the single-replica analysis into it:

chassert(local_replica_plan_reading_step->getAnalyzedResult() == nullptr);

This holds when parallel_replicas_min_number_of_rows_per_replica == 0, but not otherwise. With that setting greater than 0, the planner (PlannerJoinTree.cpp, canUseParallelReplicasOnInitiator() branch) runs index analysis on the reading step to estimate how many replicas to use, and that same step is then moved into the parallel-replicas plan. So it already carries an analysis by the time automatic parallel replicas wants to reuse the single-replica one, and the assertion fails. This is an exception that is handled in release builds, but it aborts the server in debug and sanitizer builds.

The pre-existing analysis is legitimate, so instead of asserting we overwrite it with the single-replica analysis, which is exactly what the parallel_replicas_min_number_of_rows_per_replica == 0 path already does. Both analyses are selectRangesToRead() over the same table and query, so the result is identical.

Reproduction (deterministic on a debug build): an aggregation with automatic_parallel_replicas_mode = 1 and parallel_replicas_min_number_of_rows_per_replica > 0, run twice so the dataflow statistics cache is populated. The default of the setting is 0, which is why the existing test 04034_autopr_dataflow_cache_reuse_between_different_queries does not hit it; CI randomizes the setting (see #71028), which is how the fuzzer found it.

The assertion was reported by the AST fuzzer (AST fuzzer (amd_debug), STID 2508-5243). Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100752&sha=7af449dad33f6e6f34c5e583129fe1652fd54436&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29 . It is a pre-existing planner bug, unrelated to #100752 where it happened to surface; over 60 days it also appeared under #71028, #104513 and #106073.

A regression test 04034_autopr_min_rows_per_replica_reuses_index_analysis is added that pins parallel_replicas_min_number_of_rows_per_replica to deterministically exercise the fixed path and asserts that automatic parallel replicas still engages.

Version info

  • Merged into: 26.7.1.522 (included in 26.7 and later)

…ows_per_replica

considerEnablingParallelReplicas() asserted that the local parallel-replicas
reading step had no index analysis yet before transplanting the single-replica
analysis into it:

    chassert(local_replica_plan_reading_step->getAnalyzedResult() == nullptr);

This holds when parallel_replicas_min_number_of_rows_per_replica == 0, but not
otherwise: with that setting > 0, the planner
(PlannerJoinTree.cpp, canUseParallelReplicasOnInitiator branch) runs index
analysis on the reading step to estimate how many replicas to use, and that same
step is then moved into the parallel-replicas plan. So it already carries an
analysis by the time automatic parallel replicas wants to reuse the
single-replica one, and the assertion aborts the server in debug and sanitizer
builds.

The pre-existing analysis is legitimate, so instead of asserting we simply
overwrite it with the single-replica analysis, which is exactly what the
min_number_of_rows_per_replica == 0 path already does. Both analyses are
selectRangesToRead() over the same table and query, so the result is identical.

Reproduced by an aggregation with automatic_parallel_replicas_mode = 1 and
parallel_replicas_min_number_of_rows_per_replica > 0, once the dataflow
statistics cache is populated. The default of the setting is 0, which is why the
existing test 04034_autopr_dataflow_cache_reuse_between_different_queries does not
hit it; CI randomizes the setting, which is how the fuzzer found it.

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

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Debug build, single server + a 3-replica parallel_replicas cluster: create the table, run the aggregation with automatic_parallel_replicas_mode=1 and parallel_replicas_min_number_of_rows_per_replica=1000 twice. The second run (stats cache populated) aborts on the assertion, every time.
b Root cause explained? Yes. canUseParallelReplicasOnInitiator() requires automatic_parallel_replicas_mode==0, so it is false during the user's single-node build (mode=1) and the reading step stays fresh. The auto-PR plan is built with mode forced to 0, so PlannerJoinTree.cpp enters the initiator branch and, when parallel_replicas_min_number_of_rows_per_replica>0, calls selectRangesToRead() on the reading step (to estimate the replica count), caching its analyzed_result_ptr. That step is moved into the auto-PR plan, so it reaches line 359 already analyzed and chassert(getAnalyzedResult()==nullptr) fails.
c Fix matches root cause? Yes. The non-null analysis is legitimate (it is the estimation result), so the fix removes the over-strict assertion and keeps setAnalyzedResult(analysis), which overwrites it with the single-replica analysis exactly as the min_rows==0 path already does.
d Test intent preserved / new tests added? Added regression test 04034_autopr_min_rows_per_replica_reuses_index_analysis (pins parallel_replicas_min_number_of_rows_per_replica to exercise the path). It asserts auto-PR still engages (ParallelReplicasUsedCount>0), so it catches both the abort and a silent disable. Existing 04034_... test still passes.
e Both directions demonstrated? Yes. Pre-fix binary (Build ID 539f6587...): repro aborts on run 2, stack matches CIDB. Post-fix binary (f686be11...): same repro 6/6 OK, server alive, and auto-PR engages (pr_used=1 on the initiator rows).
f Fix is general across code paths? Yes. grep confirms this is the only getAnalyzedResult()==nullptr assertion in src/. Both callers that populate the step's analysis (planner estimation, and the auto-PR reuse) now converge on the same single-replica analysis.
g Fix generalizes across inputs? Yes. The fix is not input-shape specific: analysis and the step's own analysis are both selectRangesToRead() over the same table/query/filters, computed before the step is wrapped as parallel (is_parallel_reading_from_replicas=false), so they are identical for any column types / values. The trigger is a settings combination, not a datatype.
h Backward compatible? Yes. No setting default, on-disk/wire format, or validation changed. The fix only relaxes a debug/sanitizer assertion into the already-correct release-build behavior.
i Invariants and contracts preserved? Yes. The invariant "the auto-PR local reading step uses the single-replica index analysis" is preserved: previously it held only when the step was un-analyzed; now it holds unconditionally by overwriting. setAnalyzedResult replaces the ranges the step will read, and both analyses cover the same parts/ranges, so the coordinator handout is unaffected.

Session id: cron:clickhouse-worker-slot-3:20260701-034200

@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

cc @nickitat @KochetovNicolai — could you review this? It fixes the auto-parallel-replicas getAnalyzedResult() == nullptr logical error: when parallel_replicas_min_number_of_rows_per_replica > 0, the planner already ran index analysis on the auto-PR local reading step, so considerEnablingParallelReplicas finds it non-null instead of fresh. The fix reuses the single-replica analysis unconditionally (same as the min_rows==0 path).

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

clickhouse-gh Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4531517]

Summary: ✅


AI Review

Summary

This PR fixes an invalid chassert in automatic parallel replicas when parallel_replicas_min_number_of_rows_per_replica has already run index analysis on the local replica ReadFromMergeTree step, and adds a focused stateless regression test for the cached-statistics path. I did not find a correctness, safety, or PR-metadata issue in the changed planner path.

Missing context / blind spots
  • ⚠️ CI was still pending for commit 4531517ba0a51a968862bc9d6e085733de02e91b at review time (Bugfix validation, Fast test, Build (arm_tidy), and Code Review were in progress). A completed passing CI run, especially the debug/stateless coverage that exercises chassert, would close this gap.
Final Verdict

✅ No findings to post inline. The change looks reasonable pending completion of CI.

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

clickhouse-gh Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.50% +0.10%
Functions 92.60% 92.60% +0.00%
Branches 77.70% 77.70% +0.00%

Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 4531517

CI is fully finished (Finish Workflow succeeded; only CH Inc sync remains, which is exempt). Every failure below has an owner: a fixing PR, or a full-effort fix task whose fixing-PR link will be posted here when it opens. This PR (drop an over-strict chassert in considerEnablingParallelReplicas + an AutoPR regression test) touches no MergeTree / format / backup / union code path, so none of these are PR-caused.

Check / test Reason Owner / fixing PR
AST fuzzer (amd_debug) / Block structure mismatch in UnionStep (STID 0993-27f0) chronic trunk fuzzer bug (20 master + 24 unrelated PRs / 30d) #107719 (ours, open)
Stateless tests (amd_msan, WasmEdge, parallel, 1/2) / 00612_pk_in_tuple_perf transient flake, not reproducible (CI --diagnose-random-settings: 4/4 reruns passed); 3 unrelated PRs / 0 master a fix task is created (investigating at full effort; fixing-PR link to follow here)
Upgrade check (amd_release) / Error message in clickhouse-server.log (CANNOT_RESTORE_TO_NONENCRYPTED_DISK) upgrade error-leak: encrypted backup restored to a non-encrypted disk; 1 unrelated PR / 0 master a fix task is created (investigating at full effort; fixing-PR link to follow here)
CH Inc sync private fork-sync mirror CH Inc sync (private, not actionable by us)

Session id: cron:our-pr-ci-monitor:20260701-093000

@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the 00612_pk_in_tuple_perf transient flake: #109039

@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the Upgrade-check CANNOT_RESTORE_TO_NONENCRYPTED_DISK error leak (and the co-occurring FUNCTION_THROW_IF_VALUE_IS_NON_ZERO mutation leak on the same check): #109042

@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

Correction/split on the Upgrade check (amd_release) error-leak line: it had two independent benign leftover-state signatures. The FUNCTION_THROW_IF_VALUE_IS_NON_ZERO mutation-retry noise is fixed by #109009; the CANNOT_RESTORE_TO_NONENCRYPTED_DISK encrypted Backup-engine restore leak is fixed by #109042 (this replaces my earlier single-PR link).

@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 4531517

Every failure below has an owner. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
AST fuzzer (amd_debug) / Logical error: Block structure mismatch in stream (STID 0993-27f0) logical error (chronic trunk UnionStep block-structure-mismatch, 140 PRs / 13 master in 30d) #107719 (ours, open) / tracked under #94517
Stateless tests (amd_msan, WasmEdge, parallel, 1/2) / 00612_pk_in_tuple_perf flaky (CI's own randomized-settings diagnosis: 4/4 reruns passed, "not reproducible, likely transient"; 3 unrelated PRs / 0 master / 1 hit each in 30d) a fix task is created (investigating at full effort; fixing-PR link to follow here)
Upgrade check (amd_release) / Error message in clickhouse-server.log (upgrade_error_messages.txt) infra (benign encrypted Backup-engine restore + throwIf leftover-state error leak) #109042 (ours, in review) / #109009 (ours, merged, throwIf)
Mergeable Check / PR rollup of the above owned via the above
CH Inc sync — CH Inc sync (private, not actionable)

No PR-caused failure on this SHA. #109011's diff only drops an over-strict chassert in considerEnablingParallelReplicas.cpp + adds a parallel-replicas stateless test; it cannot produce a UnionStep block-structure mismatch, a PK-in-tuple perf-test flake, or an encrypted-backup-restore log leak.

Session id: cron:our-pr-ci-monitor:20260703-013000

@nickitat nickitat self-assigned this Jul 4, 2026
@nickitat
nickitat enabled auto-merge July 4, 2026 18:57
@nickitat
nickitat added this pull request to the merge queue Jul 4, 2026
Merged via the queue into ClickHouse:master with commit 04fe85d Jul 4, 2026
342 of 345 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 4, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…s-analyzed-result-assertion

Fix logical error in automatic parallel replicas with min_number_of_rows_per_replica
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.

4 participants