Skip to content

Fix coordination mode mismatch for parallel replicas with aggregation in order - #98467

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-parallel-replicas-coordination-mode
Mar 3, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-parallel-replicas-coordination-mode

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 2, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix a logical error exception ("Replica decided to read in WithOrder mode, not in ReverseOrder") when using parallel replicas with optimize_aggregation_in_order and parallel_replicas_local_plan.
  • The initiator replica (with 0 parts) used stale result.read_type (Default) to compute coordination mode, incorrectly mapping to ReverseOrder, while remote replicas correctly computed WithOrder from input_order_info->direction.
  • Fix uses query_info.input_order_info->direction directly, which is always consistent regardless of whether range analysis ran.

Fixes #95524

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=03c7c3d6f2c843cd5f0f681e5032bf968e97e88a&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_debug%29

Test plan

  • Existing test 03810_pr_aggr_in_order_read_mode passes with the fix
  • Added test case with parallel_replicas_filter_pushdown = 1 enabled (previously had a TODO to enable after fix)

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 exception "Replica decided to read in WithOrder mode, not in ReverseOrder" when using parallel replicas with optimize_aggregation_in_order.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.3.1.339

… in order

When the initiator replica has no parts to read, `result.read_type`
remains `Default` (never updated to `InOrder`), but
`query_info.input_order_info` is set with direction=1 by the
`optimizeAggregationInOrder` optimization. The coordination mode
calculation incorrectly used `result.read_type` to determine the mode,
falling into the `ReverseOrder` branch for non-`InOrder` read types.
Meanwhile, the remote replica with actual data correctly computed
`WithOrder` (direction=1), causing the coordinator to throw:
"Replica 0 decided to read in WithOrder mode, not in ReverseOrder."

Fix by using `query_info.input_order_info->direction` directly, which
is always consistent regardless of whether `result.read_type` was
updated.

Also enable `parallel_replicas_filter_pushdown` in the existing test
for this issue.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=03c7c3d6f2c843cd5f0f681e5032bf968e97e88a&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_debug%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3be5b07]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 2, 2026
alexey-milovidov and others added 2 commits March 2, 2026 05:30
Revert modification to the existing test `03810_pr_aggr_in_order_read_mode`
and add a new test `04009_pr_aggr_in_order_coordination_mode` that covers the
fix with `parallel_replicas_filter_pushdown` enabled.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@devcrafter devcrafter self-assigned this Mar 2, 2026
@alexey-milovidov
alexey-milovidov merged commit 8f8e0d3 into master Mar 3, 2026
145 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-parallel-replicas-coordination-mode branch March 3, 2026 17:36
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 3, 2026
nickitat added a commit that referenced this pull request Aug 31, 2026
`parallel_replicas_filter_pushdown` gates two different things. On the remote half of the
read it splices the predicate into the query AST shipped to the replicas
(`ReadFromParallelRemoteReplicasStep::initializePipeline` -> `addFilters`). On the local half it
only moves a `Filter` into the plan fragment the initiator runs itself, which is an ordinary
in-process plan optimization - the fragment is then optimized like any other plan, so the
predicate becomes a `PREWHERE` and prunes the index.

Stop gating the local half. `ReadFromLocalParallelReplicaStep` exists only when
`canUseLocalPlanForParallelReplicas` accepted the query, so this is exactly "ignore the setting
in local mode"; the setting keeps governing the remote query, where the AST surgery lives.

Nothing is filtered on the initiator only: the union push-down clones the `Filter` into both
branches and pushing into a `SourceStepWithFilterBase` keeps the step, so the remote branch
still filters what the replicas send back. The replicas also need not agree on which ranges the
local plan reads - each announces its own and the coordinator reconciles them.

The push-down used to be unconditional; the setting was added in #95510 to mitigate
#95524, where a filter reaching the local plan
let `optimize_aggregation_in_order` pick a coordination mode the replicas did not use
("Replica 1 decided to read in WithOrder mode, not in ReverseOrder"). That was fixed at its root
in #98467, and the local plan now inherits the read-in-order gates from the subquery's own
settings, so the mitigation is no longer needed. `03787_pr_group_by_read_mode_error`,
`03810_pr_aggr_in_order_read_mode` and `04009_pr_aggr_in_order_coordination_mode` cover it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01WCAGFHu72JG9die3EDo8t4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

PR: Replica 1 decided to read in WithOrder mode, not in ReverseOrder

3 participants