-
Notifications
You must be signed in to change notification settings - Fork 2.3k
refactor(hash-aggr): Migrate partial-reduce hash aggregation #23233
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ddd519
refactor(hash-aggr): Migrate partial-reduce hash aggregation
2010YOUY01 8f77c61
Merge remote-tracking branch 'apache/main' into split-aggr-partial-re…
alamb 205b31f
review: disable spilling for partial reduce stream
2010YOUY01 08558c6
Merge remote-tracking branch 'origin/split-aggr-partial-reduce' into …
2010YOUY01 2d62fb4
Merge upstream/main into split-aggr-partial-reduce
2010YOUY01 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
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
149 changes: 149 additions & 0 deletions
149
datafusion/physical-plan/src/aggregates/aggregate_hash_table/partial_reduce_table.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,149 @@ | ||
| // 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. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use arrow::datatypes::SchemaRef; | ||
| use arrow::record_batch::RecordBatch; | ||
| use datafusion_common::{Result, internal_err}; | ||
| use datafusion_expr::EmitTo; | ||
|
|
||
| use crate::aggregates::AggregateExec; | ||
|
|
||
| use super::common::{ | ||
| AggregateHashTable, AggregateHashTableBuffer, AggregateHashTableState, | ||
| MaterializedOutput, PartialReduceMarker, | ||
| }; | ||
|
|
||
| /// Methods specific to the aggregate hash table used in the partial-reduce stage. | ||
| impl AggregateHashTable<PartialReduceMarker> { | ||
| pub(in crate::aggregates) fn new( | ||
| agg: &AggregateExec, | ||
| partition: usize, | ||
| output_schema: SchemaRef, | ||
| batch_size: usize, | ||
| ) -> Result<Self> { | ||
| Self::new_with_filters( | ||
| agg, | ||
| partition, | ||
| output_schema, | ||
| batch_size, | ||
| vec![None; agg.aggr_expr.len()], | ||
| ) | ||
| } | ||
|
|
||
| /// Emits the next batch of aggregated group keys and aggregate states. | ||
| /// | ||
| /// The output batch size is determined by `self.batch_size`. | ||
| /// | ||
| /// Returns `Some(batch)` for each emitted batch, `None` when output is | ||
| /// exhausted, and an internal error if polled in the `Building` state. | ||
| pub(in crate::aggregates) fn next_output_batch( | ||
| &mut self, | ||
| ) -> Result<Option<RecordBatch>> { | ||
| let output_schema = Arc::clone(&self.output_schema); | ||
| let batch_size = self.batch_size; | ||
| // Take ownership of the output state. Note `emit_next_materialized_batch` | ||
| // updates state after it emits a materialized slice. | ||
| match std::mem::replace(&mut self.state, AggregateHashTableState::Done) { | ||
| AggregateHashTableState::Outputting(state) => { | ||
| if state.group_values.is_empty() { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| let output = | ||
| self.materialize_partial_reduce_output(state, output_schema)?; | ||
| Ok(self.emit_next_materialized_batch(output, batch_size)) | ||
| } | ||
| AggregateHashTableState::OutputtingMaterialized(output) => { | ||
| Ok(self.emit_next_materialized_batch(output, batch_size)) | ||
| } | ||
| AggregateHashTableState::Done => Ok(None), | ||
| AggregateHashTableState::Building(_) => { | ||
| internal_err!("next_output_batch must be called in the outputting state") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn materialize_partial_reduce_output( | ||
| &self, | ||
| mut state: AggregateHashTableBuffer, | ||
| output_schema: SchemaRef, | ||
| ) -> Result<MaterializedOutput> { | ||
| // `state(EmitTo::All)` consumes accumulator state. Emit all groups once, | ||
| // then slice the materialized batch on subsequent polls. | ||
| let emit_to_all = EmitTo::All; | ||
| let timer = self.group_by_metrics.emitting_time.timer(); | ||
| let mut output = state.group_values.emit(emit_to_all)?; | ||
|
|
||
| for acc in state.accumulators.iter_mut() { | ||
| output.extend(acc.state(emit_to_all)?); | ||
| } | ||
| drop(timer); | ||
|
|
||
| let batch = RecordBatch::try_new(output_schema, output)?; | ||
| debug_assert!(batch.num_rows() > 0); | ||
| Ok(MaterializedOutput::new(batch)) | ||
| } | ||
|
|
||
| fn emit_next_materialized_batch( | ||
| &mut self, | ||
| mut output: MaterializedOutput, | ||
| batch_size: usize, | ||
| ) -> Option<RecordBatch> { | ||
| let batch = output.next_batch(batch_size); | ||
| if output.is_exhausted() { | ||
| self.state = AggregateHashTableState::Done; | ||
| } else { | ||
| self.state = AggregateHashTableState::OutputtingMaterialized(output); | ||
| } | ||
| batch | ||
| } | ||
|
|
||
| pub(in crate::aggregates) fn aggregate_batch( | ||
| &mut self, | ||
| batch: &RecordBatch, | ||
| ) -> Result<()> { | ||
| let evaluated_batch = self.evaluate_batch(batch)?; | ||
| let state = self.state.building_mut(); | ||
|
|
||
| let timer = self.group_by_metrics.aggregation_time.timer(); | ||
| for group_values in &evaluated_batch.grouping_set_args { | ||
| state | ||
| .group_values | ||
| .intern(group_values, &mut state.batch_group_indices)?; | ||
| let group_indices = &state.batch_group_indices; | ||
| let total_num_groups = state.group_values.len(); | ||
|
|
||
| for (acc, values) in state | ||
| .accumulators | ||
| .iter_mut() | ||
| .zip(evaluated_batch.accumulator_args.iter()) | ||
| { | ||
| acc.merge_batch(values, group_indices, total_num_groups)?; | ||
| } | ||
| } | ||
| drop(timer); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub(in crate::aggregates) fn start_output(&mut self) -> Result<()> { | ||
| self.start_outputting(); | ||
| Ok(()) | ||
| } | ||
| } | ||
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
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.
I verified that this code is covered by existing tests: