Repository navigation
Shuffle partial aggregation states instead of input rows in distributed aggregation - #123537
Conversation
…ed aggregation Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tion-shuffle-partial-states # Conflicts: # src/Core/Settings.cpp # src/Core/SettingsChangesHistory.cpp
|
Workflow [PR], commit [23984ab] Summary: ✅
AI ReviewSummaryThe PR adds partial aggregation before a shuffle and per-bucket merging to both distributed planners. Request changes: differing reader/shuffle bucket counts introduce an extra exchange of the entire raw input, and the previously reported loss of spilling remains in the current code. Findings
Missing context / blind spots
Tests
Final Verdict |
…d plan tests Co-Authored-By: Claude Opus 5.5 <[email protected]>
…reams Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rows in the rule-based planner Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tion-shuffle-partial-states # Conflicts: # tests/queries/0_stateless/04926_cascades_aggregation_pushdown.reference
Build profile diff (arm_release)Commit See the job log for details. |
…stem table merges Co-Authored-By: Claude Opus 5.5 <[email protected]>
…stage split Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tion-shuffle-partial-states Co-Authored-By: Claude Opus 5.5 <[email protected]>
| /// which the memory-efficient merge cannot consume. | ||
| auto & merge_node = nodes.emplace_back(); | ||
| merge_node.step = makeMergeOfPartialStates( | ||
| *aggregating_step, exchange_scatter_node.step->getOutputHeader(), /*memory_efficient_merge=*/false, |
There was a problem hiding this comment.
Passing memory_efficient_merge = false selects MergingAggregatedTransform, which retains all incoming state chunks in bucket_to_chunks until input finishes. Neither that accumulation nor Aggregator::mergeBlocks spills according to max_bytes_before_external_group_by. The old row-shuffle path uses AggregatingTransform, which combines and spills while receiving. Consequently, overlapping keys from several readers can accumulate multiple copies of the same keys/states at a skewed receiver and exceed max_memory_usage, even though the previous plan completes with external aggregation enabled. The Cascades alternative has the same issue through cloneWithoutMemoryEfficientAggregation.
I reproduced this with the CI binary for 83b074ec52e8f428ac6a560a349f0b1db154e228: four reader/shuffle tasks, Streaming exchanges, and 2,000,000 rows containing 100,000 distinct 264-byte String keys repeated across readers and routed to one shuffle partition. For this query:
SELECT count(), sum(c)
FROM (SELECT k, count() AS c FROM t_skew GROUP BY k)
SETTINGS max_bytes_before_external_group_by = 2000000,
max_bytes_ratio_before_external_group_by = 0,
max_memory_usage = 120000000;With distributed_plan_partial_aggregation_before_shuffle = 0, it returns 100000, 2000000 and the aggregation receiver writes 58 spill files. With the setting enabled, the partial tasks spill too, but the merge receiver throws MEMORY_LIMIT_EXCEEDED while deserializing the accumulated states (StreamingExchangeDeserializingTransform → SerializationAggregateFunction::deserializeBinaryBulk → Arena::addMemoryChunk).
Please use a merge that can combine/spill unordered input incrementally, or retain a plan that honors external aggregation when this merge cannot, and add focused memory-limit coverage for both planners.
There was a problem hiding this comment.
Spilling can be handled in follow on PR.
554634a to
83b074e
Compare
…tion-shuffle-partial-states # Conflicts: # tests/queries/0_stateless/03394_distributed_shuffle_join_with_aggregation.reference # tests/queries/0_stateless/03394_distributed_shuffle_join_with_in.reference
…tion-shuffle-partial-states # Conflicts: # tests/queries/0_stateless/05218_distributed_plan_exchange_compression_codec.sql
…n NDV hint Co-Authored-By: Claude Opus 5.5 <[email protected]>
| /// ShuffleExchange (by hash(keys)) | ||
| /// Aggregating (partial) | ||
| /// <source> | ||
| auto & partial_aggregation_node = addPartialAggregation(*aggregating_step, source, nodes, bucket_count, /*produce_results_in_bucket_order=*/false); |
There was a problem hiding this comment.
This passes the destination shuffle count to addPartialAggregation, so its any scatter redistributes the raw input before aggregating whenever the reader and shuffle counts differ. optimizeExchanges only removes that scatter/shuffle when the counts match. For example, with distributed_plan_default_reader_bucket_count = 8 and distributed_plan_default_shuffle_join_bucket_count = 2, the new plan has a raw-row ShuffleExchange (any) below the partial aggregation and a second keyed shuffle above it. The updated 04878_distributed_plan_shuffle_bucket_order_promise.reference:83-90 already shows this shape.
The previous plan needed only the keyed raw-row shuffle. The new plan still transfers every input row, then additionally transfers the states and performs another aggregation pass, defeating the promised reduction before the exchange for these supported settings.
Keep the partial aggregation at the source bucket count and use a single reader-to-merge shuffle for its states. Please cover unequal reader/shuffle counts with a plan assertion that no raw-row exchange remains below the partial aggregation.
…ault The test checks that the streaming exchange compresses, i.e. that it writes fewer bytes to the sockets than the Native blocks it serializes. Since ClickHouse#123537 a distributed aggregation without NDV statistics shuffles partial aggregation states. With a randomized `group_by_two_level_threshold = 1` or `group_by_two_level_threshold_bytes = 1` the partial aggregation is two-level, so the states cross the exchange as ~1000 packets of ~110 bytes, and the per-packet framing, written for every codec, outweighs what the codec saves: LZ4 sends 133 KB for 112 KB serialized (NONE sends 156 KB). Force the shuffle of the input rows, as 478d701 did for the sibling 05218: the exchange then carries ~4 MB in 14 packets whatever the aggregation settings, and LZ4 sends 11% of it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With
make_distributed_plan, the rule-based planner distributes aGROUP BYwith many groups by shuffling every input row by the group keys and aggregating each bucket after the shuffle. Nothing reduces the rows before the exchange, so a query that sees each key many times on each reader serializes, sends and deserializes all of its input. The Cascades optimizer can aggregate before the exchange, but it can only gather the partial states to one node and merge them there, which becomes the bottleneck when there are many groups.This PR adds the plan shape that partially aggregates where the data is read, shuffles the partial states by the group keys and merges them per bucket:
ScatterByPartitionTransformkeepsAggregatedChunkInfoon each part of a split chunk, which the merge on the other side needs.distributed_plan_partial_aggregation_before_shuffle(defaulttrue) turns the new shape off for both planners.A
GROUP BYwhere almost every row is a separate group and the planner has no statistics now does an extra hash table pass on the readers. A run-time bypass of partial aggregation that does not reduce the data is planned as a follow-up.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A distributed query plan (
make_distributed_plan) aggregates partially before it shuffles aGROUP BYby its keys, and merges the partial states per bucket, instead of shuffling every input row. Controlled by the new settingdistributed_plan_partial_aggregation_before_shuffle.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123537]
Version info
26.11.1.407-master(included in26.11and later)