Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use crate::aggregates::{

/// Marker for raw rows -> partial state aggregation.
pub(in crate::aggregates) struct PartialMarker;
/// Marker for raw rows -> final value aggregation.
pub(in crate::aggregates) struct SingleMarker;
/// Marker for partial state -> partial state aggregation.
pub(in crate::aggregates) struct PartialReduceMarker;
/// Marker for raw rows -> partial state conversion without aggregation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ mod ordered_final_table;
mod ordered_partial_table;
mod partial_reduce_table;
mod partial_table;
mod single_table;

pub(super) use common::{
AggregateHashTable, FinalMarker, PartialMarker, PartialReduceMarker,
PartialSkipMarker,
PartialSkipMarker, SingleMarker,
};
pub(super) use common_ordered::OrderedAggregateTable;
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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 arrow::datatypes::SchemaRef;
use arrow::record_batch::RecordBatch;
use datafusion_common::Result;

use crate::aggregates::AggregateExec;

use super::common::{AggregateHashTable, HashAggregateAccumulator, SingleMarker};

/// Implementation specific to single aggregation, where the table stores final
/// aggregate values and the input rows are raw rows.
///
/// Example: `AVG(x) GROUP BY k`
///
/// - Aggregate table stores: `k, avg(x)`
/// - Input rows: `k, x`
impl AggregateHashTable<SingleMarker> {
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,
agg.filter_expr.iter().cloned().collect(),
)
}

/// Emits the next batch of aggregated group keys and final aggregate values.
///
/// 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>> {
self.next_output_batch_inner(HashAggregateAccumulator::evaluate_to_columns)
}

/// Single aggregation consumes raw input rows and updates the table's
/// final-value accumulators.
pub(in crate::aggregates) fn aggregate_batch(
&mut self,
batch: &RecordBatch,
) -> Result<()> {
self.aggregate_batch_inner(batch, HashAggregateAccumulator::update_batch)
}

pub(in crate::aggregates) fn start_output(&mut self) -> Result<()> {
self.start_outputting();
Ok(())
}
}
101 changes: 101 additions & 0 deletions datafusion/physical-plan/src/aggregates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::aggregates::{
ordered_final_stream::OrderedFinalAggregateStream,
ordered_partial_stream::OrderedPartialAggregateStream,
partial_reduce_stream::PartialReduceHashAggregateStream,
single_stream::SingleHashAggregateStream,
};
use crate::execution_plan::{CardinalityEffect, EmissionType};
use crate::filter_pushdown::{
Expand Down Expand Up @@ -85,6 +86,7 @@ pub mod order;
mod ordered_final_stream;
mod ordered_partial_stream;
mod partial_reduce_stream;
mod single_stream;
mod skip_partial;
mod topk;

Expand Down Expand Up @@ -539,6 +541,9 @@ enum StreamType {
/// Final stage of the hash aggregation
/// Input output scheme: partial state -> final result
FinalHash(FinalHashAggregateStream),
/// Single stage of the hash aggregation
/// Input output scheme: initial input -> final result
SingleHash(SingleHashAggregateStream),
/// Partial stage of aggregation for ordered input.
OrderedPartialAggregate(OrderedPartialAggregateStream),
/// Final stage of aggregation for ordered input.
Expand Down Expand Up @@ -567,6 +572,7 @@ impl From<StreamType> for SendableRecordBatchStream {
StreamType::PartialHash(stream) => Box::pin(stream),
StreamType::PartialReduceHash(stream) => Box::pin(stream),
StreamType::FinalHash(stream) => Box::pin(stream),
StreamType::SingleHash(stream) => Box::pin(stream),
StreamType::OrderedPartialAggregate(stream) => Box::pin(stream),
StreamType::OrderedFinalAggregate(stream) => Box::pin(stream),
StreamType::GroupedHash(stream) => Box::pin(stream),
Expand Down Expand Up @@ -1071,6 +1077,12 @@ impl AggregateExec {
self, context, partition,
)?));
}

if self.should_use_single_hash_stream(context) {
return Ok(StreamType::SingleHash(SingleHashAggregateStream::new(
self, context, partition,
)?));
}
}

// Execution paths that have not been migrated use the fallback implementation
Expand Down Expand Up @@ -1133,6 +1145,21 @@ impl AggregateExec {
&& self.group_by.is_single()
}

fn should_use_single_hash_stream(&self, context: &TaskContext) -> bool {
// TODO: implement memory-limited path and remove this limitation
if matches!(context.memory_pool().memory_limit(), MemoryLimit::Finite(_)) {
return false;
}

matches!(
self.mode,
AggregateMode::Single | AggregateMode::SinglePartitioned
) && self.limit_options.is_none()
&& self.input_order_mode == InputOrderMode::Linear
&& !self.group_by.is_true_no_grouping()
&& self.group_by.is_single()
}

fn should_use_ordered_final_aggregate_stream(&self, context: &TaskContext) -> bool {
// TODO: implement memory-limited path and remove this limitation
if matches!(context.memory_pool().memory_limit(), MemoryLimit::Finite(_)) {
Expand Down Expand Up @@ -3506,6 +3533,80 @@ mod tests {
Ok(())
}

fn single_test_aggregate() -> Result<AggregateExec> {
let schema = Arc::new(Schema::new(vec![
Field::new("a", DataType::UInt32, false),
Field::new("b", DataType::Float64, false),
]));
let input_batch = RecordBatch::try_new(
Arc::clone(&schema),
vec![
Arc::new(UInt32Array::from(vec![1, 2, 1, 3])),
Arc::new(Float64Array::from(vec![10.0, 20.0, 40.0, 30.0])),
],
)?;
let input = TestMemoryExec::try_new_exec(
&[vec![input_batch]],
Arc::clone(&schema),
None,
)?;
let group_by =
PhysicalGroupBy::new_single(vec![(col("a", &schema)?, "a".to_string())]);
let aggregates: Vec<Arc<AggregateFunctionExpr>> = vec![Arc::new(
AggregateExprBuilder::new(sum_udaf(), vec![col("b", &schema)?])
.schema(Arc::clone(&schema))
.alias("SUM(b)")
.build()?,
)];

AggregateExec::try_new(
AggregateMode::Single,
group_by,
aggregates,
vec![None],
input,
schema,
)
}

/// For single aggregation, ensures `SingleHashAggregateStream` is used when
/// enabled by migration config.
#[tokio::test]
async fn single_aggregate_planning() -> Result<()> {
let single = single_test_aggregate()?;
let task_ctx = new_migrated_hash_ctx(2);

let stream = single.execute_typed(0, &task_ctx)?;
assert!(matches!(stream, StreamType::SingleHash(_)));
let stream: SendableRecordBatchStream = stream.into();
let output = collect(stream).await?;
assert_eq!(output.iter().map(RecordBatch::num_rows).sum::<usize>(), 3);
assert_snapshot!(batches_to_sort_string(&output), @r"
+---+--------+
| a | SUM(b) |
+---+--------+
| 1 | 50.0 |
| 2 | 20.0 |
| 3 | 30.0 |
+---+--------+
");

Ok(())
}

/// Spilling behavior is not implemented for single hash stream yet, so fall
/// back to the existing `GroupedHashAggregateStream`.
#[tokio::test]
async fn single_aggregate_with_memory_limit_planning() -> Result<()> {
let single = single_test_aggregate()?;
let task_ctx = new_finite_memory_migrated_hash_ctx(2, 1024 * 1024)?;

let stream = single.execute_typed(0, &task_ctx)?;
assert!(matches!(stream, StreamType::GroupedHash(_)));

Ok(())
}

fn partial_reduce_test_aggregate() -> Result<AggregateExec> {
let schema = Arc::new(Schema::new(vec![
Field::new("a", DataType::UInt32, false),
Expand Down
Loading
Loading