Repository navigation
Fix coordinator mode mismatch when subqueries override in-order settings - #106243
Merged
Merged
Conversation
Contributor
|
Workflow [PR], commit [376e52a] Summary: ✅
AI ReviewSummaryThis PR fixes the parallel-replicas local-plan optimizer so read-in-order decisions use the subquery context for the settings that can change Missing context / blind spots
Final VerdictStatus: ✅ Approve |
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]>
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
marked this pull request as ready for review
June 2, 2026 12:19
Contributor
LLVM Coverage Report
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 |
alexey-milovidov
approved these changes
Jun 3, 2026
This was referenced Jun 5, 2026
7 tasks
This was referenced Aug 11, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
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