Repository navigation
Fix wrong ORDER BY results and pushed bucket twice with offset based parallel replicas - #128865
Conversation
…nless no consumer needs stream order With `parallel_replicas_mode = 'sampling_key'` on any cluster, or a custom key mode on a cluster with more than one shard, `ReadFromRemote::addPipe` reads a shard with one `RemoteQueryExecutor` in `PoolMode::GET_MANY`. Every replica gets its own `parallel_replica_offset` and returns its own part of the result of the shard, and all parts arrive interleaved through one `RemoteSource` port. The initiator's order-dependent merges assume one ordered stream per port: - `GroupingAggregatedTransform` treats a port as past bucket b once any replica sent b + 1, pushes b, and the slower replica's chunk of b recreates it: `pushed bucket b twice` (a `LOGICAL_ERROR`; duplicated groups before ClickHouse#114523); - the sorted merge for `ORDER BY` merges ports that are not sorted, and with one shard there is no merge at all, so `ORDER BY ... LIMIT` returns rows of one replica. As decided by the owner in ClickHouse#115986 (fail closed, decided from the final plan), a new last pass of `optimizeTreeSecondPass`, `allowReadingShardsFromSeveralReplicas`, walks from every `ReadFromRemote` to its first consumer that is not `Expression`/`Filter`/`Union`, and allows several replicas only for a consumer that ignores the order of its input streams: a `MergingAggregated` that is not memory bound and either not memory efficient or without keys, an `Aggregating` that is not in order, a `Full` sort. Anything else, including the plan root (whose consumer is outside the plan: an upper initiator, the parent of a `Merge` child plan, a set), and any plan that is never optimized, read every shard from one replica (`GET_ONE` sends no `parallel_replicas_count`, so that replica reads the whole shard). `addPipe` runs in `initializePipeline`, after the optimization, so the pool mode follows the final plan. The single-shard custom key case already reads every replica through its own `GET_ONE` executor and is unchanged. The price is the parallelism inside a shard for order-dependent offset-mode reads. Related: ClickHouse#115986 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…replicas keeps the replicas The `system.query_log` decision check covered only an aggregation without keys. Add a merge of aggregation that is not memory efficient, a full sort and an aggregation that is not in order, each expecting three secondary queries, so turning any of these consumers to one replica fails the test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…fset parallel replicas test With aggregation in order, which the test runner can randomize on, the replicas send single-level results, so the GROUP BY cases miss the bucket-by-bucket merge. Pin it off for them. The replica decision check had no expression, filter or union between the read and the consumer. Add one case for each, so treating any of these steps as order-dependent fails the test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ard from one replica Every case of the replica decision check reached a consumer that is classified explicitly, so making the generic fallback or the default for the plan root allow several replicas kept the test green. Add a read under LIMIT and a read at the plan root, both expected to use one replica. Also drop the per-query optimize_aggregation_in_order = 0 of the plain merge case: the session SET already pins it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ancelled With LIMIT 3 the initiator stops reading before the secondary query sends EndOfStream and cancels it. A secondary that sees the Cancel is logged as ExceptionWhileProcessing, which the QueryFinish-only check does not count, so the case could read 0 instead of 1 with the fix and 1 instead of 3 without it. Use a limit above the row count, so the initiator reads to the end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A merge of aggregation states in order (memory bound merging over a local in-order aggregation) needs one ordered stream per port, so the remote shard must be read from one replica. Add a top-level GROUP BY that reaches FinishAggregatingInOrderTransform and check that its remote shard is read by one secondary query (three without the fix). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the pass and addPipe comments to one line each and drop the member comment that only pointed at the pass. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (6 rounds; the last re-review found nothing)
|
|
cc @devcrafter @KochetovNicolai, could you review this and add the |
|
Workflow [PR], commit [3774155] Summary: ✅
AI ReviewSummaryPR #128865 fixes incorrect Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes6 object files changed (+39.35 KiB total), 1 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units84 translation units recompiled, 700 s compile time in total, 83 of them have a recent master baseline. Translation units without a recent master baseline:
|
The distributed case checks that on a two shard cluster every replica processed the query with the custom key filter. Its `GROUP BY` is merged memory efficiently by default, so a shard is now read from one replica and the check fails. Pin `distributed_aggregation_memory_efficient = 0`: the plain merge ignores the order of the streams, so the replicas of a shard are still used together and the check still covers that path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Antalya 26.8 Backport of ClickHouse#128865 - Fix wrong `ORDER BY` results and `pushed bucket twice` with offset based parallel replicas
Antalya 26.6 Backport of ClickHouse#128865 - Fix wrong `ORDER BY` results and `pushed bucket twice` with offset based parallel replicas
Related: #115986
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of
ORDER BY(unsorted rows, wrong rows underLIMIT) and theGroupingAggregatedTransform pushed bucket ... twiceerror (duplicated groups in older versions) of queries withparallel_replicas_mode = 'sampling_key', or a custom key mode on a cluster with more than one shard. Such a query now reads a shard from one replica unless the initiator only aggregates or fully sorts the parts returned by the replicas.Description
On 26.10.2.103,
SELECT k FROM remote('127.0.0.{1|2|3}', db, t) ORDER BY k LIMIT 10 SETTINGS enable_parallel_replicas = 1, max_parallel_replicas = 3, parallel_replicas_mode = 'sampling_key'returns the ten smallest keys of one replica, not of the table (same on 26.8.34.13 and 26.3.60.2). WithGROUP BYsuch reads fail withGroupingAggregatedTransform pushed bucket N twice(26.3: duplicated groups). Found by the AST fuzzer (report).In these modes
ReadFromRemotereads a shard with one executor inGET_MANY: every replica returns its own part of the shard's result, and all parts arrive interleaved on one port. The memory-efficient merge of aggregation and the sorted merge forORDER BYneed one ordered stream per port.As decided in #115986 (fail closed, from the final plan), a new last pass of
optimizeTreeSecondPassallowsGET_MANYonly when every consumer of the read ignores the order of its streams: a merge of aggregation states that has no keys or is neither memory-efficient nor in order, an aggregation not in order, a full sort (expressions, filters and unions are looked through). Otherwise, including the plan root, the shard is read from one replica. The price is the parallelism inside a shard for such reads; aGROUP BYkeeps it withdistributed_aggregation_memory_efficient = 0, unless its states are merged in order. The single-shard custom key case already reads every replica separately. A custom key read that now goes to one replica per shard no longer needsparallel_replicas_custom_key; a shard split among replicas still fails withBAD_ARGUMENTSwithout it.Validation (debug build, local 2 shards x 3 replicas): the new test fails 20/20 on master, passes 50/50 plus 50 randomized runs;
ORDER BYcases: 80/80 wrong on master, 80/80 correct with the fix; ~890 distributed and parallel replicas tests show no difference.test_parallel_replicas_custom_keypinsdistributed_aggregation_memory_efficient = 0to keep using every replica.Workflow [PR]
Sync PR [sync-upstream/pr/128865]
Version info
26.11.1.1246-master(included in26.11and later)