|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Vectorized [`GroupsAccumulator`] |
| 19 | +
|
| 20 | +use arrow_array::{ArrayRef, BooleanArray}; |
| 21 | +use datafusion_common::Result; |
| 22 | + |
| 23 | +/// An implementation of GroupAccumulator is for a single aggregate |
| 24 | +/// (e.g. AVG) and stores the state for *all* groups internally |
| 25 | +/// |
| 26 | +/// The logical model is that each group is given a `group_index` |
| 27 | +/// assigned and maintained by the hash table. |
| 28 | +/// |
| 29 | +/// group_indexes are contiguous (there aren't gaps), and thus it is |
| 30 | +/// expected that each GroupAccumulator will use something like `Vec<..>` |
| 31 | +/// to store the group states. |
| 32 | +pub trait GroupsAccumulator: Send { |
| 33 | + /// updates the accumulator's state from a vector of arrays: |
| 34 | + /// |
| 35 | + /// * `values`: the input arguments to the accumulator |
| 36 | + /// * `group_indices`: To which groups do the rows in `values` belong, group id) |
| 37 | + /// * `opt_filter`: if present, only update aggregate state using values[i] if opt_filter[i] is true |
| 38 | + fn update_batch( |
| 39 | + &mut self, |
| 40 | + values: &[ArrayRef], |
| 41 | + group_indicies: &[usize], |
| 42 | + opt_filter: Option<&BooleanArray>, |
| 43 | + ) -> Result<usize>; |
| 44 | + |
| 45 | + /// Returns the final aggregate value for each group as a single |
| 46 | + /// `RecordBatch` |
| 47 | + /// |
| 48 | + /// OPEN QUESTION: Should this method take a "batch_size: usize" |
| 49 | + /// and produce a Vec<RecordBatch> as output to avoid 1) requiring |
| 50 | + /// one giant intermediate buffer? |
| 51 | + /// |
| 52 | + /// For example, the `SUM` accumulator maintains a running sum, |
| 53 | + /// and `evaluate` will produce that running sum as its output for |
| 54 | + /// all groups, in group_index order |
| 55 | + /// |
| 56 | + /// This call should be treated as consuming (takes `self`, but it |
| 57 | + /// can not be due to keeping it object save) the accumulator is |
| 58 | + /// free to release / reset it is internal state after this call |
| 59 | + /// and error on any subsequent call. |
| 60 | + fn evaluate(&mut self) -> Result<ArrayRef>; |
| 61 | + |
| 62 | + /// Returns any intermediate aggregate state used for multi-phase grouping |
| 63 | + /// |
| 64 | + /// For example, AVG returns two arrays: `SUM` and `COUNT`. |
| 65 | + /// |
| 66 | + /// This call should be treated as consuming (takes `self`, but it |
| 67 | + /// can not be due to keeping it object save) the accumulator is |
| 68 | + /// free to release / reset it is internal state after this call |
| 69 | + /// and error on any subsequent call. |
| 70 | + /// |
| 71 | + /// TODO: consider returning a single Array (which could be a |
| 72 | + /// StructArray) instead |
| 73 | + fn state(&mut self) -> Result<Vec<ArrayRef>>; |
| 74 | + |
| 75 | + /// merges intermediate state (from `state()`) into this accumulators values |
| 76 | + /// |
| 77 | + /// For some aggregates (such as `SUM`), merge_batch is the same |
| 78 | + /// as `update_batch`, but for some aggregrates (such as `COUNT`) |
| 79 | + /// the operations differ. See [`Self::state`] for more details on how |
| 80 | + /// state is used and merged. |
| 81 | + /// |
| 82 | + /// * `values`: arrays produced from calling `state` previously to the accumulator |
| 83 | + /// * `group_indices`: To which groups do the rows in `values` belong, group id) |
| 84 | + /// * `opt_filter`: if present, only update aggregate state using values[i] if opt_filter[i] is true |
| 85 | + fn merge_batch( |
| 86 | + &mut self, |
| 87 | + values: &[ArrayRef], |
| 88 | + group_indicies: &[usize], |
| 89 | + opt_filter: Option<&BooleanArray>, |
| 90 | + ) -> Result<()>; |
| 91 | + |
| 92 | + /// Amount of memory used to store the state of this |
| 93 | + /// accumulator. This function is called once per batch, so it |
| 94 | + /// should be O(n) to compute |
| 95 | + fn size(&self) -> usize; |
| 96 | +} |
0 commit comments