-
Notifications
You must be signed in to change notification settings - Fork 2.2k
refactor: Split hash aggregation logic into separated streams #22729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
2010YOUY01
wants to merge
5
commits into
apache:main
Choose a base branch
from
2010YOUY01:split-aggr-refactor-only
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,186
−6
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
345 changes: 345 additions & 0 deletions
345
datafusion/physical-plan/src/aggregates/hash_aggregate.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,345 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! 2-stage hash aggregation stream implementation. | ||
| //! | ||
| //! See comments in [`PartialHashAggregateStream`] and [`FinalHashAggregateStream`] | ||
| //! for details. | ||
| //! | ||
| //! Note these streams are an incremental migration of the existing | ||
| //! [`crate::aggregates::row_hash::GroupedHashAggregateStream`]. | ||
| //! | ||
| //! See issue for details: <https://github.com/apache/datafusion/issues/22710> | ||
|
|
||
| use std::sync::Arc; | ||
| use std::task::{Context, Poll}; | ||
|
|
||
| use arrow::datatypes::SchemaRef; | ||
| use arrow::record_batch::RecordBatch; | ||
| use datafusion_common::Result; | ||
| use datafusion_execution::TaskContext; | ||
| use datafusion_execution::memory_pool::{MemoryConsumer, MemoryReservation}; | ||
| use futures::stream::{Stream, StreamExt}; | ||
|
|
||
| use super::AggregateExec; | ||
| use super::hash_table::{AggregateHashTable, Final, Partial}; | ||
| use crate::metrics::{BaselineMetrics, MetricBuilder, RecordOutput, SpillMetrics}; | ||
| use crate::stream::EmptyRecordBatchStream; | ||
| use crate::{InputOrderMode, RecordBatchStream, SendableRecordBatchStream, metrics}; | ||
|
|
||
| /// Hash aggregation uses a 2-stage (partial and final) hash aggregation, this stream | ||
| /// is for the partial stage. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// select k, avg(v) from t group by k; | ||
| /// | ||
| /// ## Plan | ||
| /// AggregateExec(stage=final) | ||
| /// -- RepartitionExec(hash(k)) | ||
| /// ---- AggregateExec(stage=partial) | ||
| /// | ||
| /// ## Partial Stage Behavior | ||
| /// Input: raw rows | ||
| /// Output: partial states for all groups (e.g. for avg(x), it's sum(x), count(x)) | ||
| /// | ||
| /// ## Final Stage Behavior | ||
| /// Input: partial states | ||
| /// Output: results for all groups (e.g. for avg(x), it's avg(x) calculated from the state) | ||
| pub(crate) struct PartialHashAggregateStream { | ||
| /// Output schema: group columns followed by partial aggregate state columns. | ||
| schema: SchemaRef, | ||
|
|
||
| /// Input batches containing raw rows, not partial aggregate state. | ||
| input: SendableRecordBatchStream, | ||
|
|
||
| /// Hash table state for this aggregate stream. | ||
| hash_table: AggregateHashTable<Partial>, | ||
|
|
||
| /// Memory reservation for group keys and accumulators. | ||
| reservation: MemoryReservation, | ||
|
|
||
| /// Execution metrics shared with the aggregate plan node. | ||
| baseline_metrics: BaselineMetrics, | ||
|
|
||
| /// Tracks partial aggregation row reduction, matching `GroupedHashAggregateStream`. | ||
| reduction_factor: metrics::RatioMetrics, | ||
| } | ||
|
|
||
| /// Hash aggregation uses a 2-stage (partial and final) hash aggregation, this stream | ||
| /// is for the final stage. | ||
| /// | ||
| /// See [`PartialHashAggregateStream`] for details. | ||
| pub(crate) struct FinalHashAggregateStream { | ||
| /// Output schema: group columns followed by final aggregate value columns. | ||
| schema: SchemaRef, | ||
|
|
||
| /// Input batches containing partial aggregate state rows. | ||
| input: SendableRecordBatchStream, | ||
|
|
||
| /// Hash table state for this aggregate stream. | ||
| hash_table: AggregateHashTable<Final>, | ||
|
|
||
| /// Execution metrics shared with the aggregate plan node. | ||
| baseline_metrics: BaselineMetrics, | ||
|
|
||
| /// Memory reservation for group keys and accumulators. | ||
| reservation: MemoryReservation, | ||
| } | ||
|
|
||
| impl PartialHashAggregateStream { | ||
| pub fn new( | ||
| agg: &AggregateExec, | ||
| context: &Arc<TaskContext>, | ||
| partition: usize, | ||
| ) -> Result<Self> { | ||
| debug_assert_eq!(agg.mode, super::AggregateMode::Partial); | ||
| debug_assert_eq!(agg.input_order_mode, InputOrderMode::Linear); | ||
|
|
||
| let schema = Arc::clone(&agg.schema); | ||
| let input = agg.input.execute(partition, Arc::clone(context))?; | ||
| let batch_size = context.session_config().batch_size(); | ||
| let baseline_metrics = BaselineMetrics::new(&agg.metrics, partition); | ||
|
|
||
| // Preserve the existing aggregate metric surface for this plan node. | ||
| let _spill_metrics = SpillMetrics::new(&agg.metrics, partition); | ||
| let reduction_factor = MetricBuilder::new(&agg.metrics) | ||
| .with_type(metrics::MetricType::Summary) | ||
| .ratio_metrics("reduction_factor", partition); | ||
|
|
||
| let hash_table = AggregateHashTable::<Partial>::new( | ||
| agg, | ||
| partition, | ||
| Arc::clone(&schema), | ||
| batch_size, | ||
| )?; | ||
|
|
||
| let reservation = | ||
| MemoryConsumer::new(format!("PartialHashAggregateStream[{partition}]")) | ||
| .register(context.memory_pool()); | ||
|
|
||
| Ok(Self { | ||
| schema, | ||
| input, | ||
| hash_table, | ||
| baseline_metrics, | ||
| reservation, | ||
| reduction_factor, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Stream for PartialHashAggregateStream { | ||
| type Item = Result<RecordBatch>; | ||
|
|
||
| fn poll_next( | ||
| mut self: std::pin::Pin<&mut Self>, | ||
| cx: &mut Context<'_>, | ||
| ) -> Poll<Option<Self::Item>> { | ||
| let elapsed_compute = self.baseline_metrics.elapsed_compute().clone(); | ||
|
|
||
| loop { | ||
| if self.hash_table.is_done() { | ||
| let _ = self.reservation.try_resize(0); | ||
| return Poll::Ready(None); | ||
| } else if self.hash_table.is_building() { | ||
| match self.input.poll_next_unpin(cx) { | ||
| Poll::Pending => return Poll::Pending, | ||
| Poll::Ready(Some(Ok(batch))) => { | ||
| let timer = elapsed_compute.timer(); | ||
| self.reduction_factor.add_total(batch.num_rows()); | ||
| let result = self.hash_table.aggregate_batch(&batch); | ||
| timer.done(); | ||
|
|
||
| if let Err(e) = result { | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
|
|
||
| // TODO: impl memory-limited aggr, when OOM directly send | ||
| // partial state to final aggregate stage | ||
| if let Err(e) = | ||
| self.reservation.try_resize(self.hash_table.memory_size()) | ||
| { | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
| } | ||
| Poll::Ready(Some(Err(e))) => { | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
| Poll::Ready(None) => { | ||
| let input_schema = self.input.schema(); | ||
| self.input = Box::pin(EmptyRecordBatchStream::new(input_schema)); | ||
|
|
||
| let timer = elapsed_compute.timer(); | ||
| let result = self.hash_table.start_output(); | ||
| timer.done(); | ||
|
|
||
| if let Err(e) = result { | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| let timer = elapsed_compute.timer(); | ||
| let result = self.hash_table.next_output_batch(); | ||
| timer.done(); | ||
|
|
||
| match result { | ||
| Ok(Some(batch)) => { | ||
| let _ = | ||
| self.reservation.try_resize(self.hash_table.memory_size()); | ||
| self.reduction_factor.add_part(batch.num_rows()); | ||
| debug_assert!(batch.num_rows() > 0); | ||
| return Poll::Ready(Some(Ok( | ||
| batch.record_output(&self.baseline_metrics) | ||
| ))); | ||
| } | ||
| Ok(None) => { | ||
| let _ = self.reservation.try_resize(0); | ||
| return Poll::Ready(None); | ||
| } | ||
| Err(e) => return Poll::Ready(Some(Err(e))), | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl RecordBatchStream for PartialHashAggregateStream { | ||
| fn schema(&self) -> SchemaRef { | ||
| Arc::clone(&self.schema) | ||
| } | ||
| } | ||
|
|
||
| impl FinalHashAggregateStream { | ||
| pub fn new( | ||
| agg: &AggregateExec, | ||
| context: &Arc<TaskContext>, | ||
| partition: usize, | ||
| ) -> Result<Self> { | ||
| debug_assert!(matches!( | ||
| agg.mode, | ||
| super::AggregateMode::Final | super::AggregateMode::FinalPartitioned | ||
| )); | ||
| debug_assert_eq!(agg.input_order_mode, InputOrderMode::Linear); | ||
|
|
||
| let schema = Arc::clone(&agg.schema); | ||
| let input = agg.input.execute(partition, Arc::clone(context))?; | ||
| let batch_size = context.session_config().batch_size(); | ||
| let baseline_metrics = BaselineMetrics::new(&agg.metrics, partition); | ||
|
|
||
| // Preserve the existing aggregate metric surface for this plan node. | ||
| let _spill_metrics = SpillMetrics::new(&agg.metrics, partition); | ||
|
|
||
| let hash_table = AggregateHashTable::<Final>::new( | ||
| agg, | ||
| partition, | ||
| Arc::clone(&schema), | ||
| batch_size, | ||
| )?; | ||
|
|
||
| let reservation = | ||
| MemoryConsumer::new(format!("FinalHashAggregateStream[{partition}]")) | ||
| .register(context.memory_pool()); | ||
|
|
||
| Ok(Self { | ||
| schema, | ||
| input, | ||
| hash_table, | ||
| baseline_metrics, | ||
| reservation, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Stream for FinalHashAggregateStream { | ||
| type Item = Result<RecordBatch>; | ||
|
|
||
| fn poll_next( | ||
| mut self: std::pin::Pin<&mut Self>, | ||
| cx: &mut Context<'_>, | ||
| ) -> Poll<Option<Self::Item>> { | ||
| let elapsed_compute = self.baseline_metrics.elapsed_compute().clone(); | ||
|
|
||
| loop { | ||
| if self.hash_table.is_done() { | ||
| let _ = self.reservation.try_resize(0); | ||
| return Poll::Ready(None); | ||
| } else if self.hash_table.is_building() { | ||
| match self.input.poll_next_unpin(cx) { | ||
| Poll::Pending => return Poll::Pending, | ||
| Poll::Ready(Some(Ok(batch))) => { | ||
| let timer = elapsed_compute.timer(); | ||
| let result = self.hash_table.aggregate_batch(&batch); | ||
| timer.done(); | ||
|
|
||
| if let Err(e) = result { | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
|
|
||
| if let Err(e) = | ||
| self.reservation.try_resize(self.hash_table.memory_size()) | ||
| { | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
| } | ||
| Poll::Ready(Some(Err(e))) => { | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
| Poll::Ready(None) => { | ||
| let input_schema = self.input.schema(); | ||
| self.input = Box::pin(EmptyRecordBatchStream::new(input_schema)); | ||
|
|
||
| let timer = elapsed_compute.timer(); | ||
| let result = self.hash_table.start_output(); | ||
| timer.done(); | ||
|
|
||
| if let Err(e) = result { | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| let timer = elapsed_compute.timer(); | ||
| let result = self.hash_table.next_output_batch(); | ||
| timer.done(); | ||
|
|
||
| match result { | ||
| Ok(Some(batch)) => { | ||
| let _ = | ||
| self.reservation.try_resize(self.hash_table.memory_size()); | ||
| debug_assert!(batch.num_rows() > 0); | ||
| return Poll::Ready(Some(Ok( | ||
| batch.record_output(&self.baseline_metrics) | ||
| ))); | ||
| } | ||
| Ok(None) => { | ||
| let _ = self.reservation.try_resize(0); | ||
| return Poll::Ready(None); | ||
| } | ||
| Err(e) => return Poll::Ready(Some(Err(e))), | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl RecordBatchStream for FinalHashAggregateStream { | ||
| fn schema(&self) -> SchemaRef { | ||
| Arc::clone(&self.schema) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The state machines are identical for now, but in follow-up work, such as skipping partial aggregation for high-cardinality inputs, their control flows will diverge. I think separating them improves clarity, as discussed in #22710.
Some duplication is inevitable, but that is the trade-off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given these are two separate structs, what is the rationale for keepign them in the same module?
datafusion/physical-plan/src/aggregates/hash_aggregate.rs
Perhaps it would be clearer if we put them in their own modules (could be a follow on PR)
like
datafusion/physical-plan/src/aggregates/streams/partial_final.rs
datafusion/physical-plan/src/aggregates/streams/initial_partial.rs