alamb commented on code in PR #23055: URL: https://github.com/apache/datafusion/pull/23055#discussion_r3452489318
########## datafusion/physical-plan/src/aggregates/aggregate_hash_table/partial_table.rs: ########## @@ -0,0 +1,270 @@ +// 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::collections::HashMap; +use std::marker::PhantomData; +use std::sync::Arc; + +use arrow::array::{ArrayRef, BooleanArray, new_null_array}; +use arrow::datatypes::SchemaRef; +use arrow::record_batch::RecordBatch; +use datafusion_common::{Result, assert_eq_or_internal_err, internal_err}; + +use crate::aggregates::group_values::new_group_values; +use crate::aggregates::order::GroupOrdering; +use crate::aggregates::{AggregateExec, group_id_array, max_duplicate_ordinal}; + +use super::common::{ + AggregateHashTable, AggregateHashTableState, BuildingHashTableState, + EvaluatedHashAggregateAccumulator, HashAggregateAccumulator, Partial, PartialSkip, + emit_to_for_batch_size, +}; + +/// Methods specific to the aggregate hash table used in the partial aggregation stage. +impl AggregateHashTable<Partial> { + 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 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; + match &mut self.state { + AggregateHashTableState::Outputting(state) => { + if state.group_values.is_empty() { + self.state = AggregateHashTableState::Done; + return Ok(None); + } + + let emit_to = + emit_to_for_batch_size(batch_size, state.group_values.len()); + let timer = self.group_by_metrics.emitting_time.timer(); + let mut output = state.group_values.emit(emit_to)?; + + for acc in state.accumulators.iter_mut() { + output.extend(acc.state(emit_to)?); + } + let done = state.group_values.is_empty(); + drop(timer); + + let batch = RecordBatch::try_new(output_schema, output)?; + debug_assert!(batch.num_rows() > 0); + if done { + self.state = AggregateHashTableState::Done; + } + Ok(Some(batch)) + } + AggregateHashTableState::Done => Ok(None), + AggregateHashTableState::Building(_) => { + internal_err!("next_output_batch must be called in the outputting state") + } + } + } + + pub(in crate::aggregates) fn can_skip_aggregation(&self) -> bool { + self.state + .building() + .accumulators + .iter() + .all(|acc| acc.supports_convert_to_state()) Review Comment: I think we should try and remove this "supports_convert_to_state" API (as a follow on PR / project) to simplify the hash aggregate code and ensure all our groups accumulators have the high performance APIs. I filed a ticket - https://github.com/apache/datafusion/issues/23081 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
