alamb commented on code in PR #6800: URL: https://github.com/apache/arrow-datafusion/pull/6800#discussion_r1251141255
########## datafusion/physical-expr/src/aggregate/groups_accumulator/adapter.rs: ########## @@ -0,0 +1,293 @@ +// 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. + +//! Adapter that makes [`GroupsAccumulator`] out of [`Accumulator`] + +use super::GroupsAccumulator; +use arrow::{ + array::{AsArray, UInt32Builder}, + compute, + datatypes::UInt32Type, +}; +use arrow_array::{ArrayRef, BooleanArray, PrimitiveArray}; +use datafusion_common::{utils::get_arrayref_at_indices, DataFusionError, Result}; +use datafusion_expr::Accumulator; + +/// An adpater that implements [`GroupsAccumulator`] for any [`Accumulator`] +/// +/// While [`Accumulator`] are simpler to implement and can support +/// more general calculations (like retractable), but are not as fast +/// as `GroupsAccumulator`. This interface bridges the gap. +pub struct GroupsAccumulatorAdapter { + factory: Box<dyn Fn() -> Result<Box<dyn Accumulator>> + Send>, + + /// [`Accumulators`] for each group, stored in group_index order + states: Vec<AccumulatorState>, +} + +struct AccumulatorState { + /// [`Accumulators`] + accumulator: Box<dyn Accumulator>, + + // scratch space for holding the indexes in the input array that + // will be fed to this accumulator. Use u32 to match take kernel + // input + indices: Vec<u32>, +} + +impl AccumulatorState { + fn new(accumulator: Box<dyn Accumulator>) -> Self { + Self { + accumulator, + indices: vec![], + } + } + + fn size(&self) -> usize { + self.accumulator.size() + + std::mem::size_of_val(self) + + std::mem::size_of::<u32>() * self.indices.capacity() + } +} + +impl GroupsAccumulatorAdapter { + /// Create a new adapter that will create a new [`Accumulator`] + /// for each group, using the specified factory function + pub fn new<F>(factory: F) -> Self + where + F: Fn() -> Result<Box<dyn Accumulator>> + Send + 'static, + { + Self { + factory: Box::new(factory), + states: vec![], + } + } + + /// Ensure that self.accumulators has total_num_groups + fn make_accumulators_if_needed(&mut self, total_num_groups: usize) -> Result<()> { + // can't shrink + assert!(total_num_groups >= self.states.len()); + let new_accumulators = total_num_groups - self.states.len(); + for _ in 0..new_accumulators { + let accumulator = (self.factory)()?; + // todo update allocation + self.states.push(AccumulatorState::new(accumulator)); + } + Ok(()) + } + + /// invokes f(accumulator, values) for the correct slices of the + /// input values of this array. + /// + /// This first reorders the input and filter so that values for group_indexes Review Comment: After some study of the exising group by code, it turns out this is how it was invoking accumulators, which is clever but non trivially complicated -- 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]
