Skip to content

Apache DataFusion SQL Query Engine

Unify Row hash and hash implementation (#4924)

Mustafa Akur96cf046be57b
diff --git a/datafusion/core/src/physical_plan/aggregates/hash.rs b/datafusion/core/src/physical_plan/aggregates/hash.rs
deleted file mode 100644
index 7c43270146..0000000000
--- a/datafusion/core/src/physical_plan/aggregates/hash.rs
+++ /dev/null
@@ -1,563 +0,0 @@
-// 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.
-
-//! Defines the execution plan for the hash aggregate operation
-
-use std::collections::VecDeque;
-use std::sync::Arc;
-use std::task::{Context, Poll};
-use std::vec;
-
-use ahash::RandomState;
-use datafusion_expr::Accumulator;
-use futures::stream::BoxStream;
-use futures::stream::{Stream, StreamExt};
-
-use crate::error::Result;
-use crate::execution::context::TaskContext;
-use crate::execution::memory_pool::proxy::{RawTableAllocExt, VecAllocExt};
-use crate::physical_plan::aggregates::{
- evaluate_group_by, evaluate_many, AccumulatorItem, AggregateMode, PhysicalGroupBy,
-};
-use crate::physical_plan::hash_utils::create_hashes;
-use crate::physical_plan::metrics::{BaselineMetrics, RecordOutput};
-use crate::physical_plan::{aggregates, AggregateExpr, PhysicalExpr};
-use crate::physical_plan::{RecordBatchStream, SendableRecordBatchStream};
-use crate::scalar::ScalarValue;
-
-use crate::execution::memory_pool::{MemoryConsumer, MemoryReservation};
-use arrow::{array::ArrayRef, compute, compute::cast};
-use arrow::{
- array::{Array, UInt32Builder},
- error::{ArrowError, Result as ArrowResult},
-};
-use arrow::{
- datatypes::{Schema, SchemaRef},
- record_batch::RecordBatch,
-};
-use hashbrown::raw::RawTable;
-
-/*
-The architecture is the following:
-
-1. An accumulator has state that is updated on each batch.
-2. At the end of the aggregation (e.g. end of batches in a partition), the accumulator converts its state to a RecordBatch of a single row
-3. The RecordBatches of all accumulators are merged (`concatenate` in `rust/arrow`) together to a single RecordBatch.
-4. The state's RecordBatch is `merge`d to a new state
-5. The state is mapped to the final value
-
-Why:
-
-* Accumulators' state can be statically typed, but it is more efficient to transmit data from the accumulators via `Array`
-* The `merge` operation must have access to the state of the aggregators because it uses it to correctly merge
-* It uses Arrow's native dynamically typed object, `Array`.
-* Arrow shines in batch operations and both `merge` and `concatenate` of uniform types are very performant.
-
-Example: average
-
-* the state is `n: u32` and `sum: f64`
-* For every batch, we update them accordingly.
-* At the end of the accumulation (of a partition), we convert `n` and `sum` to a RecordBatch of 1 row and two columns: `[n, sum]`
-* The RecordBatch is (sent back / transmitted over network)
-* Once all N record batches arrive, `merge` is performed, which builds a RecordBatch with N rows and 2 columns.
-* Finally, `get_value` returns an array with one entry computed from the state
-*/
-pub(crate) struct GroupedHashAggregateStream {
- stream: BoxStream<'static, ArrowResult<RecordBatch>>,
- schema: SchemaRef,
-}
-
-/// Actual implementation of [`GroupedHashAggregateStream`].
-///
-/// This is wrapped into yet another struct because we need to interact with the async memory management subsystem
-/// during poll. To have as little code "weirdness" as possible, we chose to just use [`BoxStream`] together with
-/// [`futures::stream::unfold`]. The latter requires a state object, which is [`GroupedHashAggregateStreamV2`].
-struct GroupedHashAggregateStreamInner {
- schema: SchemaRef,
- input: SendableRecordBatchStream,
- mode: AggregateMode,
- accumulators: Option<Accumulators>,
- aggregate_expressions: Vec<Vec<Arc<dyn PhysicalExpr>>>,
-
- aggr_expr: Vec<Arc<dyn AggregateExpr>>,
- group_by: PhysicalGroupBy,
-
- baseline_metrics: BaselineMetrics,
- random_state: RandomState,
- finished: bool,
-}
-
-impl GroupedHashAggregateStream {
- /// Create a new GroupedHashAggregateStream
- #[allow(clippy::too_many_arguments)]
- pub fn new(
- mode: AggregateMode,
- schema: SchemaRef,
- group_by: PhysicalGroupBy,
- aggr_expr: Vec<Arc<dyn AggregateExpr>>,
- input: SendableRecordBatchStream,
- baseline_metrics: BaselineMetrics,
- context: Arc<TaskContext>,
- partition: usize,
- ) -> Result<Self> {
- let timer = baseline_metrics.elapsed_compute().timer();
-
- // The expressions to evaluate the batch, one vec of expressions per aggregation.
- // Assume create_schema() always put group columns in front of aggr columns, we set
- // col_idx_base to group expression count.
- let aggregate_expressions =
- aggregates::aggregate_expressions(&aggr_expr, &mode, group_by.expr.len())?;
-
- timer.done();
-
- let reservation =
- MemoryConsumer::new(format!("GroupedHashAggregateStream[{partition}]"))
- .register(context.memory_pool());
-
- let inner = GroupedHashAggregateStreamInner {
- schema: Arc::clone(&schema),
- mode,
- input,
- aggr_expr,
- group_by,
- baseline_metrics,
- aggregate_expressions,
- accumulators: Some(Accumulators {
- reservation,
- map: RawTable::with_capacity(0),
- group_states: Vec::with_capacity(0),
- }),
- random_state: Default::default(),
- finished: false,
- };
-
- let stream = futures::stream::unfold(inner, |mut this| async move {
- if this.finished {
- return None;
- }
-
- let elapsed_compute = this.baseline_metrics.elapsed_compute();
-
- loop {
- let result = match this.input.next().await {
- Some(Ok(batch)) => {
- let timer = elapsed_compute.timer();
- let accumulators =
- this.accumulators.as_mut().expect("not yet finished");
- let result = group_aggregate_batch(
- &this.mode,
- &this.random_state,
- &this.group_by,
- &this.aggr_expr,
- batch,
- accumulators,
- &this.aggregate_expressions,
- );
-
- timer.done();
-
- // allocate memory
- // This happens AFTER we actually used the memory, but simplifies the whole accounting and we are OK with
- // overshooting a bit. Also this means we either store the whole record batch or not.
- match result.and_then(|allocated| {
- accumulators.reservation.try_grow(allocated)
- }) {
- Ok(_) => continue,
- Err(e) => Err(ArrowError::ExternalError(Box::new(e))),
- }
- }
- Some(Err(e)) => Err(e),
- None => {
- this.finished = true;
- let timer = this.baseline_metrics.elapsed_compute().timer();
- let result = create_batch_from_map(
- &this.mode,
- std::mem::take(&mut this.accumulators)
- .expect("not yet finished"),
- this.group_by.expr.len(),
- &this.schema,
- )
- .record_output(&this.baseline_metrics);
-
- timer.done();
- result
- }
- };
-
- this.finished = true;
- return Some((result, this));
- }
- });
-
- // seems like some consumers call this stream even after it returned `None`, so let's fuse the stream.
- let stream = stream.fuse();
- let stream = Box::pin(stream);
-
- Ok(Self { schema, stream })
- }
-}
-
-impl Stream for GroupedHashAggregateStream {
- type Item = ArrowResult<RecordBatch>;
-
- fn poll_next(
- mut self: std::pin::Pin<&mut Self>,
- cx: &mut Context<'_>,
- ) -> Poll<Option<Self::Item>> {
- let this = &mut *self;
- this.stream.poll_next_unpin(cx)
- }
-}
-
-impl RecordBatchStream for GroupedHashAggregateStream {
- fn schema(&self) -> SchemaRef {
- self.schema.clone()
- }
-}
-
-/// Perform group-by aggregation for the given [`RecordBatch`].
-///
-/// If successfull, this returns the additional number of bytes that were allocated during this process.
-///
-/// TODO: Make this a member function of [`GroupedHashAggregateStream`]
-fn group_aggregate_batch(
- mode: &AggregateMode,
- random_state: &RandomState,
- group_by: &PhysicalGroupBy,
- aggr_expr: &[Arc<dyn AggregateExpr>],
- batch: RecordBatch,
- accumulators: &mut Accumulators,
- aggregate_expressions: &[Vec<Arc<dyn PhysicalExpr>>],
-) -> Result<usize> {
- // evaluate the grouping expressions
- let group_by_values = evaluate_group_by(group_by, &batch)?;
-
- // evaluate the aggregation expressions.
- // We could evaluate them after the `take`, but since we need to evaluate all
- // of them anyways, it is more performant to do it while they are together.
- let aggr_input_values = evaluate_many(aggregate_expressions, &batch)?;
-
- // track memory allocations
- let mut allocated = 0usize;
-
- for grouping_set_values in group_by_values {
- // 1.1 construct the key from the group values
- // 1.2 construct the mapping key if it does not exist
- // 1.3 add the row' index to `indices`
-
- // track which entries in `accumulators` have rows in this batch to aggregate
- let mut groups_with_rows = vec![];
-
- // 1.1 Calculate the group keys for the group values
- let mut batch_hashes = vec![0; batch.num_rows()];
- create_hashes(&grouping_set_values, random_state, &mut batch_hashes)?;
-
- for (row, hash) in batch_hashes.into_iter().enumerate() {
- let Accumulators {
- map, group_states, ..
- } = accumulators;
-
- let entry = map.get_mut(hash, |(_hash, group_idx)| {
- // verify that a group that we are inserting with hash is
- // actually the same key value as the group in
- // existing_idx (aka group_values @ row)
- let group_state = &group_states[*group_idx];
- grouping_set_values
- .iter()
- .zip(group_state.group_by_values.iter())
- .all(|(array, scalar)| scalar.eq_array(array, row))
- });
-
- match entry {
- // Existing entry for this group value
- Some((_hash, group_idx)) => {
- let group_state = &mut group_states[*group_idx];
- // 1.3
- if group_state.indices.is_empty() {
- groups_with_rows.push(*group_idx);
- };
- group_state
- .indices
- .push_accounted(row as u32, &mut allocated); // remember this row
- }
- // 1.2 Need to create new entry
- None => {
- let accumulator_set = aggregates::create_accumulators(aggr_expr)?;
-
- // Copy group values out of arrays into `ScalarValue`s
- let group_by_values = grouping_set_values
- .iter()
- .map(|col| ScalarValue::try_from_array(col, row))
- .collect::<Result<Vec<_>>>()?;
-
- // Add new entry to group_states and save newly created index
- let group_state = GroupState {
- group_by_values: group_by_values.into_boxed_slice(),
- accumulator_set,
- indices: vec![row as u32], // 1.3
- };
- // NOTE: do NOT include the `GroupState` struct size in here because this is captured by
- // `group_states` (see allocation down below)
- allocated += group_state
- .group_by_values
- .iter()
- .map(|sv| sv.size())
- .sum::<usize>()
- + (std::mem::size_of::<Box<dyn Accumulator>>()
- * group_state.accumulator_set.capacity())
- + group_state
- .accumulator_set
- .iter()
- .map(|accu| accu.size())
- .sum::<usize>()
- + (std::mem::size_of::<u32>() * group_state.indices.capacity());
-
- let group_idx = group_states.len();
- group_states.push_accounted(group_state, &mut allocated);
- groups_with_rows.push(group_idx);
-
- // for hasher function, use precomputed hash value
- map.insert_accounted(
- (hash, group_idx),
- |(hash, _group_idx)| *hash,
- &mut allocated,
- );
- }
- };
- }
-
- // Collect all indices + offsets based on keys in this vec
- let mut batch_indices: UInt32Builder = UInt32Builder::with_capacity(0);
- let mut offsets = vec![0];
- let mut offset_so_far = 0;
- for group_idx in groups_with_rows.iter() {
- let indices = &accumulators.group_states[*group_idx].indices;
- batch_indices.append_slice(indices);
- offset_so_far += indices.len();
- offsets.push(offset_so_far);
- }
- let batch_indices = batch_indices.finish();
-
- // `Take` all values based on indices into Arrays
- let values: Vec<Vec<Arc<dyn Array>>> = aggr_input_values
- .iter()
- .map(|array| {
- array
- .iter()
- .map(|array| {
- compute::take(
- array.as_ref(),
- &batch_indices,
- None, // None: no index check
- )
- .unwrap()
- })
- .collect()
- // 2.3
- })
- .collect();
-
- // 2.1 for each key in this batch
- // 2.2 for each aggregation
- // 2.3 `slice` from each of its arrays the keys' values
- // 2.4 update / merge the accumulator with the values
- // 2.5 clear indices
- groups_with_rows
- .iter()
- .zip(offsets.windows(2))
- .try_for_each(|(group_idx, offsets)| {
- let group_state = &mut accumulators.group_states[*group_idx];
- // 2.2
- group_state
- .accumulator_set
- .iter_mut()
- .zip(values.iter())
- .map(|(accumulator, aggr_array)| {
- (
- accumulator,
- aggr_array
- .iter()
- .map(|array| {
- // 2.3
- array.slice(offsets[0], offsets[1] - offsets[0])
- })
- .collect::<Vec<ArrayRef>>(),
- )
- })
- .try_for_each(|(accumulator, values)| {
- let size_pre = accumulator.size();
- let res = match mode {
- AggregateMode::Partial => accumulator.update_batch(&values),
- AggregateMode::FinalPartitioned | AggregateMode::Final => {
- // note: the aggregation here is over states, not values, thus the merge
- accumulator.merge_batch(&values)
- }
- };
- let size_post = accumulator.size();
- allocated += size_post.saturating_sub(size_pre);
- res
- })
- // 2.5
- .and({
- group_state.indices.clear();
- Ok(())
- })
- })?;
- }
-
- Ok(allocated)
-}
-
-/// The state that is built for each output group.
-#[derive(Debug)]
-struct GroupState {
- /// The actual group by values, one for each group column
- group_by_values: Box<[ScalarValue]>,
-
- // Accumulator state, one for each aggregate
- accumulator_set: Vec<AccumulatorItem>,
-
- /// scratch space used to collect indices for input rows in a
- /// bach that have values to aggregate. Reset on each batch
- indices: Vec<u32>,
-}
-
-/// The state of all the groups
-struct Accumulators {
- reservation: MemoryReservation,
-
- /// Logically maps group values to an index in `group_states`
- ///
- /// Uses the raw API of hashbrown to avoid actually storing the
- /// keys in the table
- ///
- /// keys: u64 hashes of the GroupValue
- /// values: (hash, index into `group_states`)
- map: RawTable<(u64, usize)>,
-
- /// State for each group
- group_states: Vec<GroupState>,
-}
-
-impl std::fmt::Debug for Accumulators {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- // hashes are not store inline, so could only get values
- let map_string = "RawTable";
- f.debug_struct("Accumulators")
- .field("map", &map_string)
- .field("group_states", &self.group_states)
- .finish()
- }
-}
-
-/// Create a RecordBatch with all group keys and accumulator' states or values.
-///
-/// The output looks like
-/// ```text
-/// gby_expr1, gby_expr2, ... agg_1, agg2, ...
-/// ```
-fn create_batch_from_map(
- mode: &AggregateMode,
- accumulators: Accumulators,
- num_group_expr: usize,
- output_schema: &Schema,
-) -> ArrowResult<RecordBatch> {
- if accumulators.group_states.is_empty() {
- return Ok(RecordBatch::new_empty(Arc::new(output_schema.to_owned())));
- }
- let accs = &accumulators.group_states[0].accumulator_set;
- let mut acc_data_types: Vec<usize> = vec![];
-
- // Calculate number/shape of state arrays
- match mode {
- AggregateMode::Partial => {
- for acc in accs.iter() {
- let state = acc.state()?;
- acc_data_types.push(state.len());
- }
- }
- AggregateMode::Final | AggregateMode::FinalPartitioned => {
- acc_data_types = vec![1; accs.len()];
- }
- }
-
- // make group states mutable
- let (mut group_by_values_vec, mut accumulator_set_vec): (Vec<_>, Vec<_>) =
- accumulators
- .group_states
- .into_iter()
- .map(|group_state| {
- (
- VecDeque::from(group_state.group_by_values.to_vec()),
- VecDeque::from(group_state.accumulator_set),
- )
- })
- .unzip();
-
- // First, output all group by exprs
- let mut columns = (0..num_group_expr)
- .map(|_| {
- ScalarValue::iter_to_array(
- group_by_values_vec
- .iter_mut()
- .map(|x| x.pop_front().expect("invalid group_by_values")),
- )
- })
- .collect::<Result<Vec<_>>>()?;
-
- // next, output aggregates: either intermediate state or final output
- for (x, &state_len) in acc_data_types.iter().enumerate() {
- for y in 0..state_len {
- match mode {
- AggregateMode::Partial => {
- let res = ScalarValue::iter_to_array(
- accumulator_set_vec.iter().map(|accumulator_set| {
- accumulator_set[x]
- .state()
- .map(|x| x[y].clone())
- .expect("unexpected accumulator state in hash aggregate")
- }),
- )?;
-
- columns.push(res);
- }
- AggregateMode::Final | AggregateMode::FinalPartitioned => {
- let res = ScalarValue::iter_to_array(
- accumulator_set_vec.iter_mut().map(|x| {
- x.pop_front()
- .expect("invalid accumulator_set")
- .evaluate()
- .unwrap()
- }),
- )?;
- columns.push(res);
- }
- }
- }
- }
-
- // cast output if needed (e.g. for types like Dictionary where
- // the intermediate GroupByScalar type was not the same as the
- // output
- let columns = columns
- .iter()
- .zip(output_schema.fields().iter())
- .map(|(col, desired_field)| cast(col, desired_field.data_type()))
- .collect::<ArrowResult<Vec<_>>>()?;
-
- RecordBatch::try_new(Arc::new(output_schema.to_owned()), columns)
-}
diff --git a/datafusion/core/src/physical_plan/aggregates/mod.rs b/datafusion/core/src/physical_plan/aggregates/mod.rs
index 7556eadfbb..0acbef9a5a 100644
--- a/datafusion/core/src/physical_plan/aggregates/mod.rs
+++ b/datafusion/core/src/physical_plan/aggregates/mod.rs
@@ -18,7 +18,6 @@
//! Aggregates functionalities
use crate::execution::context::TaskContext;
-use crate::physical_plan::aggregates::hash::GroupedHashAggregateStream;
use crate::physical_plan::aggregates::no_grouping::AggregateStream;
use crate::physical_plan::metrics::{
BaselineMetrics, ExecutionPlanMetricsSet, MetricsSet,
@@ -41,11 +40,10 @@ use std::collections::HashMap;
use std::sync::Arc;
-mod hash;
mod no_grouping;
mod row_hash;
-use crate::physical_plan::aggregates::row_hash::GroupedHashAggregateStreamV2;
+use crate::physical_plan::aggregates::row_hash::GroupedHashAggregateStream;
use crate::physical_plan::EquivalenceProperties;
pub use datafusion_expr::AggregateFunction;
use datafusion_physical_expr::aggregate::row_accumulator::RowAccumulator;
@@ -151,7 +149,6 @@ impl PhysicalGroupBy {
enum StreamType {
AggregateStream(AggregateStream),
- GroupedHashAggregateStreamV2(GroupedHashAggregateStreamV2),
GroupedHashAggregateStream(GroupedHashAggregateStream),
}
@@ -159,7 +156,6 @@ impl From<StreamType> for SendableRecordBatchStream {
fn from(stream: StreamType) -> Self {
match stream {
StreamType::AggregateStream(stream) => Box::pin(stream),
- StreamType::GroupedHashAggregateStreamV2(stream) => Box::pin(stream),
StreamType::GroupedHashAggregateStream(stream) => Box::pin(stream),
}
}
@@ -271,10 +267,6 @@ impl AggregateExec {
self.input_schema.clone()
}
- fn row_aggregate_supported(&self) -> bool {
- accumulator_v2_supported(&self.aggr_expr)
- }
-
fn execute_typed(
&self,
partition: usize,
@@ -282,9 +274,7 @@ impl AggregateExec {
) -> Result<StreamType> {
let batch_size = context.session_config().batch_size();
let input = self.input.execute(partition, Arc::clone(&context))?;
-
let baseline_metrics = BaselineMetrics::new(&self.metrics, partition);
-
if self.group_by.expr.is_empty() {
Ok(StreamType::AggregateStream(AggregateStream::new(
self.mode,
@@ -295,20 +285,6 @@ impl AggregateExec {
context,
partition,
)?))
- } else if self.row_aggregate_supported() {
- Ok(StreamType::GroupedHashAggregateStreamV2(
- GroupedHashAggregateStreamV2::new(
- self.mode,
- self.schema.clone(),
- self.group_by.clone(),
- self.aggr_expr.clone(),
- input,
- baseline_metrics,
- batch_size,
- context,
- partition,
- )?,
- ))
} else {
Ok(StreamType::GroupedHashAggregateStream(
GroupedHashAggregateStream::new(
@@ -318,6 +294,7 @@ impl AggregateExec {
self.aggr_expr.clone(),
input,
baseline_metrics,
+ batch_size,
context,
partition,
)?,
@@ -614,7 +591,7 @@ fn merge_expressions(
}
pub(crate) type AccumulatorItem = Box<dyn Accumulator>;
-pub(crate) type AccumulatorItemV2 = Box<dyn RowAccumulator>;
+pub(crate) type RowAccumulatorItem = Box<dyn RowAccumulator>;
fn create_accumulators(
aggr_expr: &[Arc<dyn AggregateExpr>],
@@ -625,15 +602,9 @@ fn create_accumulators(
.collect::<datafusion_common::Result<Vec<_>>>()
}
-fn accumulator_v2_supported(aggr_expr: &[Arc<dyn AggregateExpr>]) -> bool {
- aggr_expr
- .iter()
- .all(|expr| expr.row_accumulator_supported())
-}
-
-fn create_accumulators_v2(
+fn create_row_accumulators(
aggr_expr: &[Arc<dyn AggregateExpr>],
-) -> datafusion_common::Result<Vec<AccumulatorItemV2>> {
+) -> datafusion_common::Result<Vec<RowAccumulatorItem>> {
let mut state_index = 0;
aggr_expr
.iter()
@@ -1200,10 +1171,7 @@ mod tests {
assert!(matches!(stream, StreamType::GroupedHashAggregateStream(_)));
}
2 => {
- assert!(matches!(
- stream,
- StreamType::GroupedHashAggregateStreamV2(_)
- ));
+ assert!(matches!(stream, StreamType::GroupedHashAggregateStream(_)));
}
_ => panic!("Unknown version: {version}"),
}
diff --git a/datafusion/core/src/physical_plan/aggregates/row_hash.rs b/datafusion/core/src/physical_plan/aggregates/row_hash.rs
index b27723dbd4..13d3a61dac 100644
--- a/datafusion/core/src/physical_plan/aggregates/row_hash.rs
+++ b/datafusion/core/src/physical_plan/aggregates/row_hash.rs
@@ -17,13 +17,15 @@
//! Hash aggregation through row format
+use std::cmp::min;
+use std::ops::Range;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::vec;
use ahash::RandomState;
use arrow::row::{OwnedRow, RowConverter, SortField};
-use datafusion_physical_expr::hash_utils::create_row_hashes_v2;
+use datafusion_physical_expr::hash_utils::create_hashes;
use futures::stream::BoxStream;
use futures::stream::{Stream, StreamExt};
@@ -31,23 +33,25 @@ use crate::error::Result;
use crate::execution::context::TaskContext;
use crate::execution::memory_pool::proxy::{RawTableAllocExt, VecAllocExt};
use crate::physical_plan::aggregates::{
- evaluate_group_by, evaluate_many, group_schema, AccumulatorItemV2, AggregateMode,
- PhysicalGroupBy,
+ evaluate_group_by, evaluate_many, group_schema, AccumulatorItem, AggregateMode,
+ PhysicalGroupBy, RowAccumulatorItem,
};
use crate::physical_plan::metrics::{BaselineMetrics, RecordOutput};
use crate::physical_plan::{aggregates, AggregateExpr, PhysicalExpr};
use crate::physical_plan::{RecordBatchStream, SendableRecordBatchStream};
use crate::execution::memory_pool::{MemoryConsumer, MemoryReservation};
+use arrow::array::{new_null_array, PrimitiveArray};
use arrow::compute::cast;
-use arrow::datatypes::Schema;
+use arrow::datatypes::{DataType, Schema, UInt32Type};
use arrow::{array::ArrayRef, compute};
use arrow::{
array::{Array, UInt32Builder},
error::{ArrowError, Result as ArrowResult},
};
use arrow::{datatypes::SchemaRef, record_batch::RecordBatch};
-use datafusion_common::ScalarValue;
+use datafusion_common::{DataFusionError, ScalarValue};
+use datafusion_expr::Accumulator;
use datafusion_row::accessor::RowAccessor;
use datafusion_row::layout::RowLayout;
use datafusion_row::reader::{read_row, RowReader};
@@ -70,29 +74,33 @@ use hashbrown::raw::RawTable;
///
/// [Compact]: datafusion_row::layout::RowType::Compact
/// [WordAligned]: datafusion_row::layout::RowType::WordAligned
-pub(crate) struct GroupedHashAggregateStreamV2 {
+pub(crate) struct GroupedHashAggregateStream {
stream: BoxStream<'static, ArrowResult<RecordBatch>>,
schema: SchemaRef,
}
-/// Actual implementation of [`GroupedHashAggregateStreamV2`].
+/// Actual implementation of [`GroupedHashAggregateStream`].
///
/// This is wrapped into yet another struct because we need to interact with the async memory management subsystem
/// during poll. To have as little code "weirdness" as possible, we chose to just use [`BoxStream`] together with
-/// [`futures::stream::unfold`]. The latter requires a state object, which is [`GroupedHashAggregateStreamV2Inner`].
-struct GroupedHashAggregateStreamV2Inner {
+/// [`futures::stream::unfold`]. The latter requires a state object, which is [`GroupedHashAggregateStreamInner`].
+struct GroupedHashAggregateStreamInner {
schema: SchemaRef,
input: SendableRecordBatchStream,
mode: AggregateMode,
- aggr_state: AggregationState,
- aggregate_expressions: Vec<Vec<Arc<dyn PhysicalExpr>>>,
+ normal_aggr_expr: Vec<Arc<dyn AggregateExpr>>,
+ row_aggr_state: RowAggregationState,
+ /// Aggregate expressions not supporting row accumulation
+ normal_aggregate_expressions: Vec<Vec<Arc<dyn PhysicalExpr>>>,
+ /// Aggregate expressions supporting row accumulation
+ row_aggregate_expressions: Vec<Vec<Arc<dyn PhysicalExpr>>>,
group_by: PhysicalGroupBy,
- accumulators: Vec<AccumulatorItemV2>,
+ row_accumulators: Vec<RowAccumulatorItem>,
row_converter: RowConverter,
- aggr_schema: SchemaRef,
- aggr_layout: Arc<RowLayout>,
+ row_aggr_schema: SchemaRef,
+ row_aggr_layout: Arc<RowLayout>,
baseline_metrics: BaselineMetrics,
random_state: RandomState,
@@ -101,6 +109,10 @@ struct GroupedHashAggregateStreamV2Inner {
/// if the result is chunked into batches,
/// last offset is preserved for continuation.
row_group_skip_position: usize,
+ /// keeps range for each accumulator in the field
+ /// first element in the array corresponds to normal accumulators
+ /// second element in the array corresponds to row accumulators
+ indices: [Vec<Range<usize>>; 2],
}
fn aggr_state_schema(aggr_expr: &[Arc<dyn AggregateExpr>]) -> Result<SchemaRef> {
@@ -111,8 +123,8 @@ fn aggr_state_schema(aggr_expr: &[Arc<dyn AggregateExpr>]) -> Result<SchemaRef>
Ok(Arc::new(Schema::new(fields)))
}
-impl GroupedHashAggregateStreamV2 {
- /// Create a new GroupedRowHashAggregateStream
+impl GroupedHashAggregateStream {
+ /// Create a new GroupedHashAggregateStream
#[allow(clippy::too_many_arguments)]
pub fn new(
mode: AggregateMode,
@@ -127,13 +139,46 @@ impl GroupedHashAggregateStreamV2 {
) -> Result<Self> {
let timer = baseline_metrics.elapsed_compute().timer();
+ let mut start_idx = group_by.expr.len();
+ let mut row_aggr_expr = vec![];
+ let mut row_agg_indices = vec![];
+ let mut row_aggregate_expressions = vec![];
+ let mut normal_aggr_expr = vec![];
+ let mut normal_agg_indices = vec![];
+ let mut normal_aggregate_expressions = vec![];
// The expressions to evaluate the batch, one vec of expressions per aggregation.
- // Assume create_schema() always put group columns in front of aggr columns, we set
- // col_idx_base to group expression count.
- let aggregate_expressions =
- aggregates::aggregate_expressions(&aggr_expr, &mode, group_by.expr.len())?;
+ // Assuming create_schema() always puts group columns in front of aggregation columns, we set
+ // col_idx_base to the group expression count.
+ let all_aggregate_expressions =
+ aggregates::aggregate_expressions(&aggr_expr, &mode, start_idx)?;
+ for (expr, others) in aggr_expr.iter().zip(all_aggregate_expressions.into_iter())
+ {
+ let n_fields = match mode {
+ // In partial aggregation, we keep additional fields in order to successfully
+ // merge aggregation results downstream.
+ AggregateMode::Partial => expr.state_fields()?.len(),
+ _ => 1,
+ };
+ // Stores range of each expression:
+ let aggr_range = Range {
+ start: start_idx,
+ end: start_idx + n_fields,
+ };
+ if expr.row_accumulator_supported() {
+ row_aggregate_expressions.push(others);
+ row_agg_indices.push(aggr_range);
+ row_aggr_expr.push(expr.clone());
+ } else {
+ normal_aggregate_expressions.push(others);
+ normal_agg_indices.push(aggr_range);
+ normal_aggr_expr.push(expr.clone());
+ }
+ start_idx += n_fields;
+ }
- let accumulators = aggregates::create_accumulators_v2(&aggr_expr)?;
+ let row_accumulators = aggregates::create_row_accumulators(&row_aggr_expr)?;
+
+ let row_aggr_schema = aggr_state_schema(&row_aggr_expr)?;
let group_schema = group_schema(&schema, group_by.expr.len());
let row_converter = RowConverter::new(
@@ -143,36 +188,37 @@ impl GroupedHashAggregateStreamV2 {
.map(|f| SortField::new(f.data_type().clone()))
.collect(),
)?;
- let aggr_schema = aggr_state_schema(&aggr_expr)?;
- let aggr_layout = Arc::new(RowLayout::new(&aggr_schema, RowType::WordAligned));
- let reservation =
- MemoryConsumer::new(format!("GroupedHashAggregateStreamV2[{partition}]"))
- .register(context.memory_pool());
+ let row_aggr_layout =
+ Arc::new(RowLayout::new(&row_aggr_schema, RowType::WordAligned));
- let aggr_state = AggregationState {
- reservation,
+ let name = format!("GroupedHashAggregateStream[{}]", partition);
+ let row_aggr_state = RowAggregationState {
+ reservation: MemoryConsumer::new(name).register(context.memory_pool()),
map: RawTable::with_capacity(0),
group_states: Vec::with_capacity(0),
};
timer.done();
- let inner = GroupedHashAggregateStreamV2Inner {
+ let inner = GroupedHashAggregateStreamInner {
schema: Arc::clone(&schema),
mode,
input,
group_by,
- accumulators,
+ normal_aggr_expr,
+ row_accumulators,
row_converter,
- aggr_schema,
- aggr_layout,
+ row_aggr_schema,
+ row_aggr_layout,
baseline_metrics,
- aggregate_expressions,
- aggr_state,
+ normal_aggregate_expressions,
+ row_aggregate_expressions,
+ row_aggr_state,
random_state: Default::default(),
batch_size,
row_group_skip_position: 0,
+ indices: [normal_agg_indices, row_agg_indices],
};
let stream = futures::stream::unfold(inner, |mut this| async move {
@@ -187,12 +233,14 @@ impl GroupedHashAggregateStreamV2 {
&this.mode,
&this.random_state,
&this.group_by,
- &mut this.accumulators,
+ &this.normal_aggr_expr,
+ &mut this.row_accumulators,
&mut this.row_converter,
- this.aggr_layout.clone(),
+ this.row_aggr_layout.clone(),
batch,
- &mut this.aggr_state,
- &this.aggregate_expressions,
+ &mut this.row_aggr_state,
+ &this.normal_aggregate_expressions,
+ &this.row_aggregate_expressions,
);
timer.done();
@@ -201,7 +249,7 @@ impl GroupedHashAggregateStreamV2 {
// This happens AFTER we actually used the memory, but simplifies the whole accounting and we are OK with
// overshooting a bit. Also this means we either store the whole record batch or not.
match result.and_then(|allocated| {
- this.aggr_state.reservation.try_grow(allocated)
+ this.row_aggr_state.reservation.try_grow(allocated)
}) {
Ok(_) => continue,
Err(e) => Err(ArrowError::ExternalError(Box::new(e))),
@@ -213,12 +261,13 @@ impl GroupedHashAggregateStreamV2 {
let result = create_batch_from_map(
&this.mode,
&this.row_converter,
- &this.aggr_schema,
+ &this.row_aggr_schema,
this.batch_size,
this.row_group_skip_position,
- &mut this.aggr_state,
- &mut this.accumulators,
+ &mut this.row_aggr_state,
+ &mut this.row_accumulators,
&this.schema,
+ &this.indices,
);
timer.done();
@@ -227,16 +276,14 @@ impl GroupedHashAggregateStreamV2 {
};
this.row_group_skip_position += this.batch_size;
- match result {
+ return match result {
Ok(Some(result)) => {
- return Some((
- Ok(result.record_output(&this.baseline_metrics)),
- this,
- ));
+ let batch = result.record_output(&this.baseline_metrics);
+ Some((Ok(batch), this))
}
- Ok(None) => return None,
- Err(error) => return Some((Err(error), this)),
- }
+ Ok(None) => None,
+ Err(error) => Some((Err(error), this)),
+ };
}
});
@@ -248,7 +295,7 @@ impl GroupedHashAggregateStreamV2 {
}
}
-impl Stream for GroupedHashAggregateStreamV2 {
+impl Stream for GroupedHashAggregateStream {
type Item = ArrowResult<RecordBatch>;
fn poll_next(
@@ -260,7 +307,7 @@ impl Stream for GroupedHashAggregateStreamV2 {
}
}
-impl RecordBatchStream for GroupedHashAggregateStreamV2 {
+impl RecordBatchStream for GroupedHashAggregateStream {
fn schema(&self) -> SchemaRef {
self.schema.clone()
}
@@ -270,35 +317,40 @@ impl RecordBatchStream for GroupedHashAggregateStreamV2 {
///
/// If successfull, this returns the additional number of bytes that were allocated during this process.
///
-/// TODO: Make this a member function of [`GroupedHashAggregateStreamV2`]
+/// TODO: Make this a member function of [`GroupedHashAggregateStream`]
#[allow(clippy::too_many_arguments)]
fn group_aggregate_batch(
mode: &AggregateMode,
random_state: &RandomState,
grouping_set: &PhysicalGroupBy,
- accumulators: &mut [AccumulatorItemV2],
+ normal_aggr_expr: &[Arc<dyn AggregateExpr>],
+ row_accumulators: &mut [RowAccumulatorItem],
row_converter: &mut RowConverter,
state_layout: Arc<RowLayout>,
batch: RecordBatch,
- aggr_state: &mut AggregationState,
- aggregate_expressions: &[Vec<Arc<dyn PhysicalExpr>>],
+ aggr_state: &mut RowAggregationState,
+ normal_aggregate_expressions: &[Vec<Arc<dyn PhysicalExpr>>],
+ row_aggregate_expressions: &[Vec<Arc<dyn PhysicalExpr>>],
) -> Result<usize> {
- // evaluate the grouping expressions
- let grouping_by_values = evaluate_group_by(grouping_set, &batch)?;
-
- let AggregationState {
- map, group_states, ..
- } = aggr_state;
+ // Evaluate the grouping expressions:
+ let group_by_values = evaluate_group_by(grouping_set, &batch)?;
+ // Keep track of memory allocated:
let mut allocated = 0usize;
- let row_converter_size_pre = row_converter.size();
+ let RowAggregationState {
+ map: row_map,
+ group_states: row_group_states,
+ ..
+ } = aggr_state;
- for group_values in grouping_by_values {
- let group_rows = row_converter.convert_columns(&group_values)?;
+ // Evaluate the aggregation expressions.
+ // We could evaluate them after the `take`, but since we need to evaluate all
+ // of them anyways, it is more performant to do it while they are together.
+ let row_aggr_input_values = evaluate_many(row_aggregate_expressions, &batch)?;
+ let normal_aggr_input_values = evaluate_many(normal_aggregate_expressions, &batch)?;
- // evaluate the aggregation expressions.
- // We could evaluate them after the `take`, but since we need to evaluate all
- // of them anyways, it is more performant to do it while they are together.
- let aggr_input_values = evaluate_many(aggregate_expressions, &batch)?;
+ let row_converter_size_pre = row_converter.size();
+ for group_values in &group_by_values {
+ let group_rows = row_converter.convert_columns(group_values)?;
// 1.1 construct the key from the group values
// 1.2 construct the mapping key if it does not exist
@@ -309,21 +361,21 @@ fn group_aggregate_batch(
// 1.1 Calculate the group keys for the group values
let mut batch_hashes = vec![0; batch.num_rows()];
- create_row_hashes_v2(&group_rows, random_state, &mut batch_hashes)?;
+ create_hashes(group_values, random_state, &mut batch_hashes)?;
for (row, hash) in batch_hashes.into_iter().enumerate() {
- let entry = map.get_mut(hash, |(_hash, group_idx)| {
+ let entry = row_map.get_mut(hash, |(_hash, group_idx)| {
// verify that a group that we are inserting with hash is
// actually the same key value as the group in
// existing_idx (aka group_values @ row)
- let group_state = &group_states[*group_idx];
+ let group_state = &row_group_states[*group_idx];
group_rows.row(row) == group_state.group_by_values.row()
});
match entry {
// Existing entry for this group value
Some((_hash, group_idx)) => {
- let group_state = &mut group_states[*group_idx];
+ let group_state = &mut row_group_states[*group_idx];
// 1.3
if group_state.indices.is_empty() {
@@ -336,13 +388,16 @@ fn group_aggregate_batch(
}
// 1.2 Need to create new entry
None => {
+ let accumulator_set =
+ aggregates::create_accumulators(normal_aggr_expr)?;
// Add new entry to group_states and save newly created index
let group_state = RowGroupState {
group_by_values: group_rows.row(row).owned(),
aggregation_buffer: vec![0; state_layout.fixed_part_width()],
+ accumulator_set,
indices: vec![row as u32], // 1.3
};
- let group_idx = group_states.len();
+ let group_idx = row_group_states.len();
// NOTE: do NOT include the `RowGroupState` struct size in here because this is captured by
// `group_states` (see allocation down below)
@@ -352,14 +407,23 @@ fn group_aggregate_batch(
* group_state.aggregation_buffer.capacity())
+ (std::mem::size_of::<u32>() * group_state.indices.capacity());
+ // Allocation done by normal accumulators
+ allocated += (std::mem::size_of::<Box<dyn Accumulator>>()
+ * group_state.accumulator_set.capacity())
+ + group_state
+ .accumulator_set
+ .iter()
+ .map(|accu| accu.size())
+ .sum::<usize>();
+
// for hasher function, use precomputed hash value
- map.insert_accounted(
+ row_map.insert_accounted(
(hash, group_idx),
|(hash, _group_index)| *hash,
&mut allocated,
);
- group_states.push_accounted(group_state, &mut allocated);
+ row_group_states.push_accounted(group_state, &mut allocated);
groups_with_rows.push(group_idx);
}
@@ -370,32 +434,16 @@ fn group_aggregate_batch(
let mut batch_indices: UInt32Builder = UInt32Builder::with_capacity(0);
let mut offsets = vec![0];
let mut offset_so_far = 0;
- for group_idx in groups_with_rows.iter() {
- let indices = &group_states[*group_idx].indices;
+ for &group_idx in groups_with_rows.iter() {
+ let indices = &row_group_states[group_idx].indices;
batch_indices.append_slice(indices);
offset_so_far += indices.len();
offsets.push(offset_so_far);
}
let batch_indices = batch_indices.finish();
- // `Take` all values based on indices into Arrays
- let values: Vec<Vec<Arc<dyn Array>>> = aggr_input_values
- .iter()
- .map(|array| {
- array
- .iter()
- .map(|array| {
- compute::take(
- array.as_ref(),
- &batch_indices,
- None, // None: no index check
- )
- .unwrap()
- })
- .collect()
- // 2.3
- })
- .collect();
+ let row_values = get_at_indices(&row_aggr_input_values, &batch_indices);
+ let normal_values = get_at_indices(&normal_aggr_input_values, &batch_indices);
// 2.1 for each key in this batch
// 2.2 for each aggregation
@@ -406,11 +454,11 @@ fn group_aggregate_batch(
.iter()
.zip(offsets.windows(2))
.try_for_each(|(group_idx, offsets)| {
- let group_state = &mut group_states[*group_idx];
+ let group_state = &mut row_group_states[*group_idx];
// 2.2
- accumulators
+ row_accumulators
.iter_mut()
- .zip(values.iter())
+ .zip(row_values.iter())
.map(|(accumulator, aggr_array)| {
(
accumulator,
@@ -439,35 +487,70 @@ fn group_aggregate_batch(
}
})
// 2.5
+ .and(Ok(()))?;
+ // normal accumulators
+ group_state
+ .accumulator_set
+ .iter_mut()
+ .zip(normal_values.iter())
+ .map(|(accumulator, aggr_array)| {
+ (
+ accumulator,
+ aggr_array
+ .iter()
+ .map(|array| {
+ // 2.3
+ array.slice(offsets[0], offsets[1] - offsets[0])
+ })
+ .collect::<Vec<ArrayRef>>(),
+ )
+ })
+ .try_for_each(|(accumulator, values)| {
+ let size_pre = accumulator.size();
+ let res = match mode {
+ AggregateMode::Partial => accumulator.update_batch(&values),
+ AggregateMode::FinalPartitioned | AggregateMode::Final => {
+ // note: the aggregation here is over states, not values, thus the merge
+ accumulator.merge_batch(&values)
+ }
+ };
+ let size_post = accumulator.size();
+ allocated += size_post.saturating_sub(size_pre);
+ res
+ })
+ // 2.5
.and({
group_state.indices.clear();
Ok(())
- })
+ })?;
+
+ Ok::<(), DataFusionError>(())
})?;
}
-
allocated += row_converter.size().saturating_sub(row_converter_size_pre);
-
Ok(allocated)
}
/// The state that is built for each output group.
#[derive(Debug)]
-struct RowGroupState {
- // Group key.
+pub struct RowGroupState {
+ /// The actual group by values, stored sequentially
group_by_values: OwnedRow,
// Accumulator state, stored sequentially
- aggregation_buffer: Vec<u8>,
+ pub aggregation_buffer: Vec<u8>,
+
+ // Accumulator state, one for each aggregate that doesn't support row accumulation
+ pub accumulator_set: Vec<AccumulatorItem>,
/// scratch space used to collect indices for input rows in a
/// bach that have values to aggregate. Reset on each batch
- indices: Vec<u32>,
+ pub indices: Vec<u32>,
}
/// The state of all the groups
-struct AggregationState {
- reservation: MemoryReservation,
+pub struct RowAggregationState {
+ pub reservation: MemoryReservation,
/// Logically maps group values to an index in `group_states`
///
@@ -476,13 +559,13 @@ struct AggregationState {
///
/// keys: u64 hashes of the GroupValue
/// values: (hash, index into `group_states`)
- map: RawTable<(u64, usize)>,
+ pub map: RawTable<(u64, usize)>,
/// State for each group
- group_states: Vec<RowGroupState>,
+ pub group_states: Vec<RowGroupState>,
}
-impl std::fmt::Debug for AggregationState {
+impl std::fmt::Debug for RowAggregationState {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
// hashes are not store inline, so could only get values
let map_string = "RawTable";
@@ -501,71 +584,130 @@ fn create_batch_from_map(
aggr_schema: &Schema,
batch_size: usize,
skip_items: usize,
- aggr_state: &mut AggregationState,
- accumulators: &mut [AccumulatorItemV2],
+ row_aggr_state: &mut RowAggregationState,
+ row_accumulators: &mut [RowAccumulatorItem],
output_schema: &Schema,
+ // Stores the location of each accumulator in the output_schema
+ indices: &[Vec<Range<usize>>],
) -> ArrowResult<Option<RecordBatch>> {
- if skip_items > aggr_state.group_states.len() {
+ if skip_items > row_aggr_state.group_states.len() {
return Ok(None);
}
-
- if aggr_state.group_states.is_empty() {
- return Ok(Some(RecordBatch::new_empty(Arc::new(
- output_schema.to_owned(),
- ))));
+ if row_aggr_state.group_states.is_empty() {
+ let schema = Arc::new(output_schema.to_owned());
+ return Ok(Some(RecordBatch::new_empty(schema)));
}
- let mut state_accessor = RowAccessor::new(aggr_schema, RowType::WordAligned);
+ let end_idx = min(skip_items + batch_size, row_aggr_state.group_states.len());
+ let group_state_chunk = &row_aggr_state.group_states[skip_items..end_idx];
+
+ if group_state_chunk.is_empty() {
+ let schema = Arc::new(output_schema.to_owned());
+ return Ok(Some(RecordBatch::new_empty(schema)));
+ }
- let (group_buffers, mut state_buffers): (Vec<_>, Vec<_>) = aggr_state
- .group_states
+ // Buffers for each distinct group (i.e. row accumulator memories)
+ let mut state_buffers = group_state_chunk
.iter()
- .skip(skip_items)
- .take(batch_size)
- .map(|gs| (gs.group_by_values.row(), gs.aggregation_buffer.clone()))
- .unzip();
-
- let mut columns: Vec<ArrayRef> = converter.convert_rows(group_buffers)?;
-
- match mode {
- AggregateMode::Partial => columns.extend(read_as_batch(
- &state_buffers,
- aggr_schema,
- RowType::WordAligned,
- )),
+ .map(|gs| gs.aggregation_buffer.clone())
+ .collect::<Vec<_>>();
+
+ let output_fields = output_schema.fields();
+ // Store row accumulator results (either final output or intermediate state):
+ let row_columns = match mode {
+ AggregateMode::Partial => {
+ read_as_batch(&state_buffers, aggr_schema, RowType::WordAligned)
+ }
AggregateMode::Final | AggregateMode::FinalPartitioned => {
- let mut results: Vec<Vec<ScalarValue>> = vec![vec![]; accumulators.len()];
- for buffer in state_buffers.iter_mut() {
- state_accessor.point_to(0, buffer);
- for (i, acc) in accumulators.iter().enumerate() {
- results[i].push(acc.evaluate(&state_accessor).unwrap());
- }
- }
- // We skip over the first `columns.len()` elements.
- //
- // This shouldn't panic if the `output_schema` has enough fields.
- let remaining_field_iterator = output_schema.fields()[columns.len()..].iter();
-
- for (scalars, field) in results.into_iter().zip(remaining_field_iterator) {
- if !scalars.is_empty() {
- columns.push(ScalarValue::iter_to_array(scalars)?);
+ let mut results = vec![];
+ for (idx, acc) in row_accumulators.iter().enumerate() {
+ let mut state_accessor =
+ RowAccessor::new(aggr_schema, RowType::WordAligned);
+ let current = state_buffers
+ .iter_mut()
+ .map(|buffer| {
+ state_accessor.point_to(0, buffer);
+ acc.evaluate(&state_accessor)
+ })
+ .collect::<Result<Vec<_>>>()?;
+ // Get corresponding field for row accumulator
+ let field = &output_fields[indices[1][idx].start];
+ let result = if current.is_empty() {
+ Ok(arrow::array::new_empty_array(field.data_type()))
} else {
- columns.push(arrow::array::new_empty_array(field.data_type()))
- }
+ let item = ScalarValue::iter_to_array(current)?;
+ // cast output if needed (e.g. for types like Dictionary where
+ // the intermediate GroupByScalar type was not the same as the
+ // output
+ cast(&item, field.data_type()).map_err(DataFusionError::ArrowError)
+ }?;
+ results.push(result);
}
+ results
+ }
+ };
+
+ // Store normal accumulator results (either final output or intermediate state):
+ let mut columns = vec![];
+ for (idx, &Range { start, end }) in indices[0].iter().enumerate() {
+ for (field_idx, field) in output_fields[start..end].iter().enumerate() {
+ let current = match mode {
+ AggregateMode::Partial => ScalarValue::iter_to_array(
+ group_state_chunk.iter().map(|row_group_state| {
+ row_group_state.accumulator_set[idx]
+ .state()
+ .map(|v| v[field_idx].clone())
+ .expect("Unexpected accumulator state in hash aggregate")
+ }),
+ ),
+ AggregateMode::Final | AggregateMode::FinalPartitioned => {
+ ScalarValue::iter_to_array(group_state_chunk.iter().map(
+ |row_group_state| {
+ row_group_state.accumulator_set[idx]
+ .evaluate()
+ .expect("Unexpected accumulator state in hash aggregate")
+ },
+ ))
+ }
+ }?;
+ // Cast output if needed (e.g. for types like Dictionary where
+ // the intermediate GroupByScalar type was not the same as the
+ // output
+ let result = cast(&current, field.data_type())?;
+ columns.push(result);
}
}
- // cast output if needed (e.g. for types like Dictionary where
- // the intermediate GroupByScalar type was not the same as the
- // output
- let columns = columns
+ // Stores the group by fields
+ let group_buffers = group_state_chunk
.iter()
- .zip(output_schema.fields().iter())
- .map(|(col, desired_field)| cast(col, desired_field.data_type()))
- .collect::<ArrowResult<Vec<_>>>()?;
+ .map(|gs| gs.group_by_values.row())
+ .collect::<Vec<_>>();
+ let mut output: Vec<ArrayRef> = converter.convert_rows(group_buffers)?;
- RecordBatch::try_new(Arc::new(output_schema.to_owned()), columns).map(Some)
+ // The size of the place occupied by row and normal accumulators
+ let extra: usize = indices
+ .iter()
+ .flatten()
+ .map(|Range { start, end }| end - start)
+ .sum();
+ let empty_arr = new_null_array(&DataType::Null, 1);
+ output.extend(std::iter::repeat(empty_arr).take(extra));
+
+ // Write results of both accumulator types to the corresponding location in
+ // the output schema:
+ let results = [columns.into_iter(), row_columns.into_iter()];
+ for (outer, mut current) in results.into_iter().enumerate() {
+ for &Range { start, end } in indices[outer].iter() {
+ for item in output.iter_mut().take(end).skip(start) {
+ *item = current.next().expect("Columns cannot be empty");
+ }
+ }
+ }
+ Ok(Some(RecordBatch::try_new(
+ Arc::new(output_schema.to_owned()),
+ output,
+ )?))
}
fn read_as_batch(rows: &[Vec<u8>], schema: &Schema, row_type: RowType) -> Vec<ArrayRef> {
@@ -580,3 +722,25 @@ fn read_as_batch(rows: &[Vec<u8>], schema: &Schema, row_type: RowType) -> Vec<Ar
output.output_as_columns()
}
+
+fn get_at_indices(
+ input_values: &[Vec<Arc<dyn Array>>],
+ batch_indices: &PrimitiveArray<UInt32Type>,
+) -> Vec<Vec<Arc<dyn Array>>> {
+ input_values
+ .iter()
+ .map(|array| {
+ array
+ .iter()
+ .map(|array| {
+ compute::take(
+ array.as_ref(),
+ batch_indices,
+ None, // None: no index check
+ )
+ .unwrap()
+ })
+ .collect()
+ })
+ .collect()
+}
diff --git a/datafusion/core/tests/sql/aggregates.rs b/datafusion/core/tests/sql/aggregates.rs
index 9911df9c7f..47bbffaefc 100644
--- a/datafusion/core/tests/sql/aggregates.rs
+++ b/datafusion/core/tests/sql/aggregates.rs
@@ -1058,3 +1058,32 @@ async fn aggregate_with_alias() -> Result<()> {
);
Ok(())
}
+
+#[tokio::test]
+async fn test_accumulator_row_accumulator() -> Result<()> {
+ let config = SessionConfig::new();
+ let ctx = SessionContext::with_config(config);
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c1, c2, MIN(c13) as min1, MIN(c9) as min2, MAX(c13) as max1, MAX(c9) as max2, AVG(c9) as avg1, MIN(c13) as min3, COUNT(C9) as cnt1, 0.5*SUM(c9-c8) as sum1
+ FROM aggregate_test_100
+ GROUP BY c1, c2
+ ORDER BY c1, c2
+ LIMIT 5";
+
+ let actual = execute_to_batches(&ctx, sql).await;
+ let expected = vec![
+ "+----+----+--------------------------------+-----------+--------------------------------+------------+--------------------+--------------------------------+------+--------------+",
+ "| c1 | c2 | min1 | min2 | max1 | max2 | avg1 | min3 | cnt1 | sum1 |",
+ "+----+----+--------------------------------+-----------+--------------------------------+------------+--------------------+--------------------------------+------+--------------+",
+ "| a | 1 | 0keZ5G8BffGwgF2RwQD59TFzMStxCB | 774637006 | waIGbOGl1PM6gnzZ4uuZt4E2yDWRHs | 4015442341 | 2437927011 | 0keZ5G8BffGwgF2RwQD59TFzMStxCB | 5 | 6094771121.5 |",
+ "| a | 2 | b3b9esRhTzFEawbs6XhpKnD9ojutHB | 145294611 | ukyD7b0Efj7tNlFSRmzZ0IqkEzg2a8 | 3717551163 | 2267588664 | b3b9esRhTzFEawbs6XhpKnD9ojutHB | 3 | 3401364777 |",
+ "| a | 3 | Amn2K87Db5Es3dFQO9cw9cvpAM6h35 | 431948861 | oLZ21P2JEDooxV1pU31cIxQHEeeoLu | 3998790955 | 2225685115.1666665 | Amn2K87Db5Es3dFQO9cw9cvpAM6h35 | 6 | 6676994872.5 |",
+ "| a | 4 | KJFcmTVjdkCMv94wYCtfHMFhzyRsmH | 466439833 | ydkwycaISlYSlEq3TlkS2m15I2pcp8 | 2502326480 | 1655431654 | KJFcmTVjdkCMv94wYCtfHMFhzyRsmH | 4 | 3310812222.5 |",
+ "| a | 5 | MeSTAXq8gVxVjbEjgkvU9YLte0X9uE | 141047417 | QJYm7YRA3YetcBHI5wkMZeLXVmfuNy | 2496054700 | 1216992989.6666667 | MeSTAXq8gVxVjbEjgkvU9YLte0X9uE | 3 | 1825431770 |",
+ "+----+----+--------------------------------+-----------+--------------------------------+------------+--------------------+--------------------------------+------+--------------+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ Ok(())
+}
diff --git a/datafusion/physical-expr/src/aggregate/count.rs b/datafusion/physical-expr/src/aggregate/count.rs
index 8ccf87ac2b..38b193ebf2 100644
--- a/datafusion/physical-expr/src/aggregate/count.rs
+++ b/datafusion/physical-expr/src/aggregate/count.rs
@@ -202,7 +202,9 @@ impl RowAccumulator for CountRowAccumulator {
}
fn evaluate(&self, accessor: &RowAccessor) -> Result<ScalarValue> {
- Ok(accessor.get_as_scalar(&DataType::Int64, self.state_index))
+ Ok(ScalarValue::Int64(Some(
+ accessor.get_u64_opt(self.state_index()).unwrap_or(0) as i64,
+ )))
}
#[inline(always)]