Skip to content

Commit 8af7cb1

Browse files
2010YOUY01Omega359
authored andcommitted
perf: Improve output materializing speed for OrderedFinalAggregateStream (apache#25639)
# perf: Avoid copying when materializing output in OrderedFinalAggregateStream ## Which issue does this PR close? - Closes [apache#25157](apache#25157). - Follow-up to [apache#25312](apache#25312). ## Rationale for this change See [apache#25312](apache#25312) for the rationale and implementation details. This PR mechanically applies the same pattern to `OrderedFinalAggregateStream`. I repeated the original 10-million-row experiment on an M4 Pro MacBook Pro, with 14 partitions and `batch_size = 8192`: ```text main (already includes apache#25312): 450.82 ms this PR: 329.25 ms legacy aggregation (migration disabled): 336.10 ms ``` ## What changes are included in this PR? 1. A clean-up refactor for `OrderedFinalAggregateStream` implementation, see apache#23974 for idea. 2. Materialize all completed final results once and emit slices, retaining the output buffers' reservation until the last slice is handed off. If those buffers cannot be reserved, hand off the whole batch. 3. Preserve spill replay and count its final output once in the operator metrics. To read this PR, start at `OrderedFinalAggregateStream::into_stream()`. Its stage diagram describes the execution flow, and the incremental-output notes link to the explanation in the partial stream. The diff is not easy to read directly due to the clean-up refactor ## What is the testing strategy for this PR? For correctness, existing tests have covered it. To prevent similar perf regression, we can do - apache#25310 ## Are there any user-facing changes? Faster ordered final aggregation. Query results and public APIs are unchanged.
1 parent a11f858 commit 8af7cb1

4 files changed

Lines changed: 269 additions & 561 deletions

File tree

‎datafusion/physical-plan/src/aggregates/aggregate_hash_table/ordered_final_table.rs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,24 @@ impl OrderedAggregateTable<FinalMarker> {
8080
)
8181
}
8282

83-
/// See comments in `ordered_partial_stream::next_output_batch`
84-
pub(in crate::aggregates) fn next_output_batch(
83+
/// Materializes final results for all groups proven complete by the input
84+
/// ordering, leaving the active ordered-key range in the table.
85+
///
86+
/// Returns None if there are no completed groups.
87+
pub(in crate::aggregates) fn take_completed_result_batch(
8588
&mut self,
8689
) -> Result<Option<RecordBatch>> {
87-
self.next_output_batch_inner(
90+
if self.is_empty() {
91+
return Ok(None);
92+
}
93+
let Some(emit_to) = self.group_ordering().emit_to() else {
94+
return Ok(None);
95+
};
96+
self.materialize_groups(
97+
emit_to,
8898
HashAggregateAccumulator::evaluate_to_columns,
8999
AccumulatorPhase::Evaluate,
90100
)
101+
.map(Some)
91102
}
92103
}

‎datafusion/physical-plan/src/aggregates/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ impl From<StreamType> for SendableRecordBatchStream {
719719
StreamType::FinalHash(stream) => stream.into_stream(),
720720
StreamType::SingleHash(stream) => Box::pin(stream),
721721
StreamType::OrderedPartialAggregate(stream) => stream.into_stream(),
722-
StreamType::OrderedFinalAggregate(stream) => Box::pin(stream),
722+
StreamType::OrderedFinalAggregate(stream) => stream.into_stream(),
723723
StreamType::OrderedSingleAggregate(stream) => Box::pin(stream),
724724
StreamType::GroupedHash(stream) => Box::pin(stream),
725725
StreamType::GroupedPriorityQueue(stream) => Box::pin(stream),

0 commit comments

Comments
 (0)