Repository navigation
Resolve the 26.7 cherry-pick conflicts of #112942 - #121593
PedroTadim merged 16 commits into
Conversation
…enabled `applyParallelReplicas` created the `MergingAggregatedStep` which merges the partially aggregated data of the replicas with a hardcoded `memory_efficient_aggregation = false`, while the path it replaced, `Planner::addMergingAggregatedStep`, obeys `distributed_aggregation_memory_efficient` and lists `parallel_replicas_from_merge_tree` among the cases where it applies. It is not only about the memory. The ordinary `MergingAggregatedTransform` reads all the buckets before it returns anything, and it returns them in the order produced by `Aggregator::convertToChunks`, where the buckets of a two level result are converted by a thread pool, every thread collects the buckets it took into its own list, and the lists are spliced together at the end. So the buckets come out in an arbitrary order. A shard of a `Distributed` table can read its local table with parallel replicas (`ClusterProxy::executeQuery` keeps them enabled for the shards of a cluster which has more than one replica per shard), and then it sends the merged result to the initiator, which merges it memory efficiently. Buckets arriving out of order there make the initiator merge and output the same bucket twice, and the result contains duplicated keys. The bug does not need the buckets to be produced out of order, so `enable_producing_buckets_out_of_order_in_aggregation` is disabled in the test. The thread pool in `Aggregator::prepareChunksAndFillTwoLevelImpl` is used only for more than 100000 keys, so the test has 200000 of them. Without the fix the test reports about 46000 duplicated keys and fails in every run. Memory efficient merging is not supported for grouping sets, so it stays disabled for them. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
…not applicable
`ReadFromRemote` establishes a connection per replica of a shard (`PoolMode::GET_MANY`)
unless parallel reading from replicas is used or the `enable_parallel_replicas`
setting is 0. But a replica executes the whole query unless it is given a part of
the data to read, and only the offset based modes (`SAMPLING_KEY`,
`CUSTOM_KEY_SAMPLING`, `CUSTOM_KEY_RANGE`) do that, by sending a filter along with
the query. `ParallelReplicasMode::READ_TASKS` does it with the coordinator, and for
that a single connection per shard is needed, which is why it is handled separately.
`Context::canUseTaskBasedParallelReplicas` returns false not only when the setting
is 0, but also because of `automatic_parallel_replicas_mode`,
`parallel_replicas_only_with_analyzer` and `max_parallel_replicas`. In those cases
the query was still sent to every replica of a shard, every one of them read all of
its data, and the result of the shard was multiplied by the number of the replicas.
SELECT count(), sum(y) FROM d SETTINGS enable_parallel_replicas = 1,
max_parallel_replicas = 3, automatic_parallel_replicas_mode = 2
returned 6000, 2997000 instead of 2000, 999000 for a cluster of 2 shards holding
1000 rows each. With `distributed_aggregation_memory_efficient` the duplicated
partially aggregated results also arrive at the initiator as several complete
sequences of two-level buckets on a single input port, which the memory efficient
merging cannot handle, so the keys were duplicated as well.
Now several connections to a shard are used only when they read different parts of
the data, i.e. only for the offset based modes.
`automatic_parallel_replicas_mode` no longer has to be pinned in
04606_parallel_replicas_merging_aggregated_bucket_order.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
`04606` wrote the result of the aggregation into a table, because such a query could not be used as a subquery: it failed with `Chunk should have AggregatedChunkInfo in MergingAggregatedTransform`. That is fixed in master, which is merged here, so the query is a subquery again and the table is gone. Both tests drop their tables in the prologue and in the epilogue now. Every test gets its own database (`tests/clickhouse-test` creates `test_<random>` per test and drops it afterwards), so this is not needed for the isolation, but it is what most of the tests around do. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
`04603_pr_plan_based_view_no_view_flag`, `04604_pr_plan_based_force_index_view` and `04605_pr_plan_based_count_distinct_serialized` are named after the feature they test, and this test belongs to the same group. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
`parallel_replicas_mode` can ask for the custom key filtering while `parallel_replicas_custom_key` is not set. Every replica reads only the part of the data its filter selects, so without the key every replica reads everything and the result of the shard is multiplied by the number of the replicas. The initiator builds the filter itself only for a cluster with a single shard, and it already failed there, because `parseCustomKeyForTable` cannot parse the empty key. Otherwise the filter is built by every replica, and `buildCustomKeyFilterIfNeeded` returned no filter silently for the empty key, so a query to a cluster with several shards returned every row as many times as there are replicas. Check the key in `parseCustomKeyForTable`, so that every caller fails the same way, and with the message about the missing custom key instead of a syntax error. An empty filter means a missing key and nothing else: a key that parses always produces a filter, and `parseCustomKeyForTable` throws rather than returns nothing for a key that does not parse. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT Assert that the custom key is parsed into something `parseCustomKeyForTable` either parses the key or throws, so the check for the missing result was never true and its message about the missing custom key was unreachable. The message is reported by `parseCustomKeyForTable` itself now, keep the expectation as an assert. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
`parallel_replicas_for_non_replicated_merge_tree` is about the task based parallel replicas: the replicas read the parts the coordinator assigns to them, and that needs the same parts on every replica. The old analyzer disabled the parallel replicas for a plain `MergeTree` table whenever the setting is not set, no matter the mode, and that also disabled building the filter by the custom key, which is built by the same interpreter. The initiator has already sent the query to every replica of the shard by then, so every replica read the whole table and the result of the shard was multiplied by the number of the replicas. A query without the custom key did not report the missing key either, because the code that reports it was not reached. Check the mode, the same way the check for FINAL right above it does. The analyzer does not disable the filtering here, so both of them behave the same way now. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
`parseCustomKeyForTable` either parses the key or throws, it never returns nothing, so the branch that reports the missing custom key when it returns nothing was never taken. It reported the key of a replica with an offset only, which left the first replica reading the whole table silently. The missing key is reported by `parseCustomKeyForTable` itself now, for every replica, keep the expectation as an assert. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
JOINs are not supported with the custom key, and the old analyzer dropped the custom key for them, promising in the log message that the query is executed without the parallel replicas. It is not what dropping the key does: the parallel replicas stay enabled, the initiator still sends the query to every replica of the shard, and the replicas are left without anything to split the data by, so every one of them reads the whole table and the result of the shard is multiplied by the number of the replicas. Disable the parallel replicas instead, the way the checks for FINAL and for a plain `MergeTree` next to it do. The analyzer executes such queries without the parallel replicas as well, and both of them return the same result now. Reported by review of ClickHouse#112942. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
The table of the test is a plain `MergeTree`, and the parallel replicas are not used for it unless `parallel_replicas_for_non_replicated_merge_tree` is set. The initiator still sends the query to every replica of the shard, so the missing custom key has to be reported, but the test depended on the replicas keeping the parallel replicas enabled to report it. Ask for them explicitly, so that the test checks the missing key on its own. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
The setting says that the query is executed locally when the analyzer is disabled, and it is set by default, but it does not stop the initiator from sending the query to every replica of the shard. Only the filter by the custom key keeps the result of such a query correct, and the replicas dropped that filter for a plain `MergeTree` table, so the result was multiplied by the number of the replicas with either value of the setting. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
The replicas of the cluster of the test are the same server, so the other queries return the same numbers whether a shard is read by one replica or by all of them, and they check the result alone. The query without the custom key is the only one that needs the parallel replicas to be used to get its result, and it relied on them being used for a plain `MergeTree` without `parallel_replicas_for_non_replicated_merge_tree`. Ask for them, so that the check does not depend on that. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BAZjq1jL3wNACqVQzocPoT
…licas-duplicate-keys Fix a few cases when parallel replicas might return duplicate rows in the output
The robot cherry-pick of ClickHouse#112942 into 26.7 (ClickHouse#113810) came out conflicting and was auto-closed after seven idle days without anyone resolving it. Eight hours later the 26.7 backport of ClickHouse#112794 landed the "Bucket N is pushed twice" assert and test 04605 on the branch, so 26.7 now asserts an invariant whose fix it never received. ClickHouse#112942 makes the per-shard replica merge that `applyParallelReplicas` inserts obey `distributed_aggregation_memory_efficient`. Without it that merge always takes the `!memory_efficient_aggregation` branch of `MergingAggregatedStep::transformPipeline`, which resizes the pipeline to one stream and uses the plain `MergingAggregatedTransform`. That transform emits two-level buckets in `Aggregator::convertToChunks` order and never consults `should_produce_results_in_order_of_bucket_number`, so a shard that promised the initiator ascending bucket order breaks its own promise. The initiator's memory-efficient merge then trips `chassert(bucket < 0 || pushed_buckets.insert(bucket).second)` in debug and sanitizer builds, and returns duplicated aggregation keys in release builds. Merging `backport/26.7/112942` into `cherrypick/26.7/112942` conflicts in one file, `applyParallelReplicas.cpp`, in one six-line region: master passes the new `settings` argument to the visitor and calls `insertParallelReplicasSplit(query_plan, nodes)` before it, a function added to master after 26.7 was cut. The resolution keeps the `settings` argument and drops the `insertParallelReplicasSplit` call. The resolved file is byte-identical to master's own copy at the fix commit 7a902ae (blob 74365cc), because 26.7's copy equals master's copy at that commit's parent. The resolution therefore diverges from upstream by zero bytes. The other four source files and all three new tests auto-merge clean. (cherry picked from commit 0793ca6)
04606 asserts that a shard of a `Distributed` table which merges the partially
aggregated results of its parallel replicas hands the initiator the two-level
buckets in ascending bucket id order, so the initiator's memory-efficient merge
does not merge and output the same bucket twice. It measures that as
`count() - uniqExact(x)` over 200000 groups, which has to be `0`.
Upstream computes the aggregation in a subquery. On this branch the same query
kills the server before it reaches the assertion:
Logical error: 'Chunk should have AggregatedChunkInfo in
MergingAggregatedTransform.' (MergingAggregatedTransform.cpp:239)
Logical error: 'Chunk should have AggregatedChunkInfo/ChunkInfoWithAllocatedBytes
in GroupingAggregatedTransform.' (MergingAggregatedMemoryEfficientTransform.cpp:369)
on run 1, against a pristine 26.7 tip with none of this branch applied. The
outer query consumes only `x`, so `sum(y)` is pruned from the shard-side
fragment and its header carries no aggregate state column.
`ReadFromParallelReplicas.cpp:197` hard-codes `add_agg_info = false`, and the
only other way that flag is set is the auto-detection in `RemoteSource`'s
constructor (`RemoteSource.cpp:29-33`), which looks for a
`DataTypeAggregateFunction` column in the header. So no `AggregatedChunkInfo` is
attached and the merge throws. Master fixed that in ClickHouse#111063
(`36966297cd1696a`), merged five days after this branch was cut.
The test is healthy upstream: 12348 runs and 0 failures on master over the last
30 days. It is this branch that cannot run the shape, not the test that is
flaky.
Upstream itself wrote the test this way first. `7a902aec5b1f6` introduced it
writing the result into a `Memory` table, with the comment "at the moment such a
query cannot be used as a subquery", and `bcd47d843620` converted it to a
subquery only once master had the fix: "it failed with `Chunk should have
AggregatedChunkInfo in MergingAggregatedTransform`. That is fixed in master,
which is merged here, so the query is a subquery again and the table is gone."
This commit reverts that one transformation and keeps the rest of it, including
the drop hygiene.
The coverage is unchanged. 200000 keys keep the buckets of the two-level result
being converted by a thread pool, which is what returns them in an arbitrary
order, and `enable_producing_buckets_out_of_order_in_aggregation = 0` keeps the
producer from reporting them as delayed. The reference stays `0`.
Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model reviewAn independent model reviewed the resolved tree cold against the base ❌ Strict mode for
💡 Noted, not changed: the comment blocks in the source diff are upstream's own text, so they stay |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260913-221100 |
|
Ok, let's merge to finish the backport |
|
Thanks. The robot will not open the 26.7 PR from here: it only does that after the cherry-pick PR from So the last step is manual: squash On today's |
Related: #112942
Related: #113810
Related: #118802
Related: #119753
Related: #115986
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed several cases when parallel replicas returned duplicated rows:
distributed_aggregation_memory_efficientwas ignored by the plan-based parallel replicas, so the buckets of the memory efficient merging arrived out of order and were merged twice; and a shard was read from all of its replicas although the data was not split between them, when the parallel replicas are not applicable to the query or whenparallel_replicas_modeasks for the custom key filtering withoutparallel_replicas_custom_key.Description
Conflict resolution for the auto-closed robot cherry-pick #113810, done the way
cherry_pick.pydoes it, hence the base. One six-line region: keep the newsettingsargument, dropinsertParallelReplicasSplit, added after the 26.7 cut.Why 26.7 needs it. #118802 landed the bucket-order half of #114523, but alone it is inert:
applyParallelReplicas.cppstill hard-codesmemory_efficient_aggregation = false, because that visitor cannot see the optimization settings #112942 hands it. Oncbe5200ad319the test reds 7 of 20 runs withGroupingAggregatedTransform pushed bucket N twice; with the port, 20/20 clean.Onto 26.7. Merging this alone opens no 26.7-facing PR:
cherry_pick.pydrops a closed cherry-pick PR before it would create one. Preferred, a fast-forward:then reopen #113810; merging this PR first empties its diff and blocks it. The squash onto 26.7 conflicts in that region again, since #118802 rewrote it: keep its
setProduceResultsInBucketOrdercall and take the derivedmemory_efficient_aggregationfrom here, so 26.7 gets both. I built and tested that result; the numbers below are from it.Commit 2 restores 04606's own earlier table-based body, because its subquery form dies on run 1 on the pristine tip: the pruned
sum(y)leaves the shard fragment with no aggregate-state column, which the parallel-replicas read never adds. Upstream used that form until #111063 fixed it in master, five days after the cut; the 200000 keys and the out-of-order setting are preserved. If 26.7 should have #111063's+7/-1hunk, that is av26.7-must-backportlabel away.No CI here: this base matches neither workflow's branch filter. The 26.7-facing PR does.
Validation, all local, per-arm Build-ID and server identity asserted (click to expand)
The resolved
applyParallelReplicas.cppis byte-identical to master's own copy at #112942's fix commit7a902aec5b1f6, because 26.7's copy equals master's at that commit's parent, so the resolution diverges from upstream by zero bytes. The other four source files and all three tests auto-merge clean.cherry_pick.py:300-306discards a closed cherry-pick PR beforecreate_backport();ReadFromParallelReplicas.cpp:197hard-codes theadd_agg_info = falsethat costs 04606 its subquery form.Arms are
git commit-trees on the 26.7 tipcbe5200ad319, soHEAD^{tree}certifies each. The last row isgit merge-tree --write-tree --merge-base fafdfd835ab9c cbe5200ad319 <this head>with its single conflict resolved, i.e. what the release step produces.Every failure above is
Logical error: 'GroupingAggregatedTransform pushed bucket N twice', N varying per run. The dying query is pinned by query id: initialINSERT INTO res SELECT x, sum(y) FROM d GROUP BY xat stage Complete, shard fragment at stage WithMergeableState, and zero Shuffle/Scatter/Exchange lines for it, so this is the parallel-replicas route, not the distributed-plan one #116446 fixed.Randomization was live throughout (87 to 187 settings per query), and 21 of the 71 recorded runs on the last arm used the runner's
automatic_parallel_replicas_mode = 2bundle, the route the partial arm still failed on; all green. That middle row is why the port has to go in whole: 04607 and 04669 need the four source files besides the conflicted one.