Skip to content

Commit 4445f34

Browse files
committed
review: exract MaterializeAccumulatorFn to a separate type
1 parent 32691af commit 4445f34

4 files changed

Lines changed: 24 additions & 20 deletions

File tree

datafusion/physical-plan/src/aggregates/aggregate_hash_table/common.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,14 @@ impl<AggrMode> AggregateHashTable<AggrMode> {
210210
///
211211
/// Each aggregation mode chooses a different `materialize_accumulator_fn`
212212
/// according to its semantics. For example, partial aggregation emits
213-
/// partial states to feed the final stage, so it us [`GroupsAccumulator::state`].
213+
/// partial states to feed the final stage, so it uses [`GroupsAccumulator::state`].
214214
///
215215
/// This is a temporary solution until blocked state management is implemented:
216216
/// Issue: <https://github.com/apache/datafusion/issues/7065>
217-
pub(super) fn next_output_batch_inner<F>(
217+
pub(super) fn next_output_batch_inner(
218218
&mut self,
219-
mut materialize_accumulator_fn: F,
220-
) -> Result<Option<RecordBatch>>
221-
where
222-
F: FnMut(&mut HashAggregateAccumulator, EmitTo, &mut Vec<ArrayRef>) -> Result<()>,
223-
{
219+
materialize_accumulator_fn: MaterializeAccumulatorFn,
220+
) -> Result<Option<RecordBatch>> {
224221
let output_schema = Arc::clone(&self.output_schema);
225222
let batch_size = self.batch_size;
226223

@@ -237,7 +234,7 @@ impl<AggrMode> AggregateHashTable<AggrMode> {
237234
let timer = self.group_by_metrics.emitting_time.timer();
238235
let mut columns = state.group_values.emit(emit_to)?;
239236
for acc in state.accumulators.iter_mut() {
240-
materialize_accumulator_fn(acc, emit_to, &mut columns)?;
237+
columns.extend(materialize_accumulator_fn(acc, emit_to)?);
241238
}
242239
drop(timer);
243240

@@ -349,6 +346,15 @@ pub(super) type AggregateBatchFn = fn(
349346
usize,
350347
) -> Result<()>;
351348

349+
/// Function used by [`AggregateHashTable::next_output_batch_inner`] to
350+
/// materialize one accumulator's output columns.
351+
///
352+
/// Arguments:
353+
/// * accumulator to materialize.
354+
/// * group range to emit from the accumulator.
355+
pub(super) type MaterializeAccumulatorFn =
356+
fn(&mut AggregateAccumulator, EmitTo) -> Result<Vec<ArrayRef>>;
357+
352358
/// Evaluated aggregate arguments and filter for one input batch.
353359
///
354360
/// For example, `AVG(x + 1) FILTER (WHERE x > 0)` evaluates both `x + 1`
@@ -547,6 +553,13 @@ impl HashAggregateAccumulator {
547553
self.accumulator.evaluate(emit_to)
548554
}
549555

556+
pub(super) fn evaluate_to_columns(
557+
&mut self,
558+
emit_to: EmitTo,
559+
) -> Result<Vec<ArrayRef>> {
560+
Ok(vec![self.evaluate(emit_to)?])
561+
}
562+
550563
/// Evaluating partial aggregate results according to `EmitTo`, and reset inner
551564
/// states. (e.g. after `state(EmitTo::All)`, it returns all accumulated groups
552565
/// , and clear the inner buffers)

datafusion/physical-plan/src/aggregates/aggregate_hash_table/final_table.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ impl AggregateHashTable<FinalMarker> {
5555
pub(in crate::aggregates) fn next_output_batch(
5656
&mut self,
5757
) -> Result<Option<RecordBatch>> {
58-
self.next_output_batch_inner(|acc, emit_to, output| {
59-
output.push(acc.evaluate(emit_to)?);
60-
Ok(())
61-
})
58+
self.next_output_batch_inner(HashAggregateAccumulator::evaluate_to_columns)
6259
}
6360

6461
/// Final aggregation consumes partial aggregate states and merges them into

datafusion/physical-plan/src/aggregates/aggregate_hash_table/partial_reduce_table.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ impl AggregateHashTable<PartialReduceMarker> {
4949
pub(in crate::aggregates) fn next_output_batch(
5050
&mut self,
5151
) -> Result<Option<RecordBatch>> {
52-
self.next_output_batch_inner(|acc, emit_to, output| {
53-
output.extend(acc.state(emit_to)?);
54-
Ok(())
55-
})
52+
self.next_output_batch_inner(HashAggregateAccumulator::state)
5653
}
5754

5855
/// Partial-reduce aggregation consumes partial aggregate states and merges

datafusion/physical-plan/src/aggregates/aggregate_hash_table/partial_table.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ impl AggregateHashTable<PartialMarker> {
6565
pub(in crate::aggregates) fn next_output_batch(
6666
&mut self,
6767
) -> Result<Option<RecordBatch>> {
68-
self.next_output_batch_inner(|acc, emit_to, output| {
69-
output.extend(acc.state(emit_to)?);
70-
Ok(())
71-
})
68+
self.next_output_batch_inner(HashAggregateAccumulator::state)
7269
}
7370

7471
pub(in crate::aggregates) fn can_skip_aggregation(&self) -> bool {

0 commit comments

Comments
 (0)