Skip to content

Fix coordinator mode mismatch when subqueries override in-order settings - #106243

Merged
alexey-milovidov merged 7 commits into
masterfrom
coordinator-mode-mismatch
Jun 3, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
coordinator-mode-mismatch

Conversation

@nickitat

@nickitat nickitat commented Jun 1, 2026 •

Copy link
Copy Markdown
Member

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 coordinator mode mismatch when subqueries override in-order settings.


Closes: #106039
Related: #105369, #106043

@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [376e52a]

Summary: ✅


AI Review

Summary

This PR fixes the parallel-replicas local-plan optimizer so read-in-order decisions use the subquery context for the settings that can change query_info.input_order_info, while preserving the outer optimizer contracts that are needed for automatic parallel replicas. The added stateless tests cover the original aggregation-in-order collision plus distinct/read-in-order regressions, and the PR metadata matches a real bug fix. I found no new blocking or major issues.

Missing context / blind spots
  • ⚠️ I could not run the new stateless tests locally because this checkout has no ClickHouse build directory or binaries. The PR CI report currently shows no failed checks.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 1, 2026
nickitat and others added 2 commits June 1, 2026 16:15
The parallel-replicas local plan must agree with the remote replicas only on
the coordination mode announced to the shared coordinator. That mode is a pure
function of `query_info.input_order_info`, which is produced solely by the
read-in-order optimizations; every other per-subquery setting divergence stays
local (each replica announces its own ranges and the coordinator reconciles
them).

Rebuilding the whole `QueryPlanOptimizationSettings` from the subquery context
reset the flags that callers set imperatively after construction — deferred set
building and reused index/PK analysis in `buildQueryPlanForAutomaticParallelReplicas`
(`build_sets`, `materialize_ctes`, `query_plan_optimize_primary_key`,
`optimize_projection`, `force_use_projection`), and the EXPLAIN-introspection
state. A `ReadFromLocalParallelReplicaStep` inside such a candidate would then
build sets / re-run primary-key and projection optimization despite the outer
optimizer disabling them.

Keep the outer `optimization_settings` (which carries those contracts) and
override only the read-in-order family — `read_in_order`, `aggregation_in_order`,
`read_in_order_through_join` — with the subquery's values. Drop the now-unused
`inheritExplainSettingsFrom`.

Add `04303_parallel_replicas_projection_scope_collision`: a non-mode setting
(`optimize_use_projections`) enabled in the outer scope and disabled in the
subquery, verifying the initiator-uses-projection / replicas-read-raw split
still returns the correct result.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
nickitat and others added 2 commits June 1, 2026 18:57
The coordination mode is set only by `ReadFromMergeTree::requestReadingInOrder`,
but four independent optimizations call it, not two: `optimizeReadInOrder`
(`read_in_order`, `read_in_order_through_join`), `optimizeAggregationInOrder`
(`aggregation_in_order`), `optimizeDistinctInOrder` (`distinct_in_order`) and
`tryReuseStorageOrderingForWindowFunctions`
(`reuse_storage_ordering_for_window_functions`).

The previous graft only carried the first three, so a query like

  SELECT count() FROM (SELECT DISTINCT a FROM t SETTINGS optimize_distinct_in_order = 0)
  SETTINGS optimize_distinct_in_order = 1

still let the initiator's local plan announce `WithOrder` while the remote
replicas announced `Default` for the same stream, throwing `Coordination mode
mismatch`. Override `distinct_in_order` and `reuse_storage_ordering_for_window_functions`
from the subquery context as well, and add `04304_parallel_replicas_input_order_scope_collision`
covering the distinct, read-in-order and window paths.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The window query `sum(b) OVER (PARTITION BY a ORDER BY a)` over 600k rows made
the test run > 180s in the debug-build flaky check (many reruns over many tiny
granules from randomized `index_granularity`). The distinct-in-order and
read-in-order cases reproduce the coordination-mode mismatch on their own and
are as cheap as 04278, so drop the window query. The window path is still
covered by the fix itself (`reuse_storage_ordering_for_window_functions` is
copied from the subquery context in optimizeTree.cpp).
@nickitat
nickitat marked this pull request as ready for review June 2, 2026 12:19
@nickitat
nickitat requested a review from devcrafter June 2, 2026 12:19
@clickhouse-gh

clickhouse-gh Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.50% 84.40% -0.10%
Functions 92.40% 92.40% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 30/33 (90.91%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 4 line(s) · Uncovered code

Full report · Diff report

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.

Logical error: Coordination mode mismatch for stream A: got B, expected C (STID: 6117-68e9)

3 participants