datafusion-expr-common 56.0.0

Logical plan and expression representation for DataFusion query engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

//! Vectorized [`GroupsAccumulator`]

use arrow::array::{ArrayRef, BooleanArray};
use datafusion_common::{Result, exec_err, not_impl_err, utils::split_vec_min_alloc};
use std::sync::Arc;

use crate::accumulator::AggregateMetrics;

/// Describes how many rows should be emitted during grouping.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmitTo {
    /// Emit all groups
    All,
    /// Emit only the first `n` groups and shift all existing group
    /// indexes down by `n`.
    ///
    /// For example, if `n=10`, group_index `0, 1, ... 9` are emitted
    /// and group indexes `10, 11, 12, ...` become `0, 1, 2, ...`.
    First(usize),
}

impl EmitTo {
    /// Removes the number of rows from `v` required to emit the right
    /// number of rows, returning a `Vec` with elements taken, and the
    /// remaining values in `v`.
    ///
    /// This avoids copying if Self::All
    pub fn take_needed<T>(&self, v: &mut Vec<T>) -> Vec<T> {
        match self {
            Self::All => {
                // Take the entire vector, leave new (empty) vector
                std::mem::take(v)
            }
            Self::First(n) => split_vec_min_alloc(v, *n),
        }
    }
}

/// Selects groups for a non-destructive grouped aggregation read.
///
/// Unlike [`EmitTo`], this selection does not remove groups or change their
/// indices. Selections created by [`Self::try_from_indices`] preserve the
/// requested order and support duplicate indices.
///
/// A selection is validated once when it is constructed and can then be reused
/// for the group values and accumulators participating in the same snapshot.
/// Construct a new selection if the number or indexing of those groups changes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GroupSelection<'a> {
    total_num_groups: usize,
    indices: Option<&'a [usize]>,
}

impl<'a> GroupSelection<'a> {
    /// Selects all `total_num_groups` groups in group-index order.
    pub fn all(total_num_groups: usize) -> Self {
        Self {
            total_num_groups,
            indices: None,
        }
    }

    /// Selects groups in the order specified by `indices`.
    ///
    /// Returns an error if an index is not less than `total_num_groups`. Empty
    /// selections are valid, and duplicate indices are preserved.
    pub fn try_from_indices(
        indices: &'a [usize],
        total_num_groups: usize,
    ) -> Result<Self> {
        if let Some(index) = indices.iter().find(|&&index| index >= total_num_groups) {
            return exec_err!(
                "Group index {index} is out of bounds for {total_num_groups} groups"
            );
        }
        Ok(Self {
            total_num_groups,
            indices: Some(indices),
        })
    }

    /// Returns the group count against which this selection was constructed.
    pub fn total_num_groups(self) -> usize {
        self.total_num_groups
    }

    /// Ensures this selection is being applied to the same number of groups
    /// against which it was constructed.
    ///
    /// Preserving-read implementations should call this method with their
    /// stored group count before using [`Self::iter`]. This check is `O(1)`;
    /// the selected indices were already checked by [`Self::try_from_indices`].
    pub fn validate_num_groups(self, actual_num_groups: usize) -> Result<()> {
        if actual_num_groups != self.total_num_groups {
            return exec_err!(
                "Group selection was constructed for {} groups but applied to {actual_num_groups} groups",
                self.total_num_groups
            );
        }
        Ok(())
    }

    /// Returns the number of selected groups.
    pub fn len(self) -> usize {
        self.indices
            .map_or(self.total_num_groups, |indices| indices.len())
    }

    /// Returns `true` if no groups are selected.
    pub fn is_empty(self) -> bool {
        self.len() == 0
    }

    /// Returns the selected group indices in output order.
    pub fn iter(self) -> impl Iterator<Item = usize> + 'a {
        let (all, indices): (_, &'a [usize]) = match self.indices {
            None => (0..self.total_num_groups, &[]),
            Some(indices) => (0..0, indices),
        };
        all.chain(indices.iter().copied())
    }
}

/// `GroupsAccumulator` implements a single aggregate (e.g. AVG) and
/// stores the state for *all* groups internally.
///
/// Logically, a [`GroupsAccumulator`] stores a mapping from each group index to
/// the state of the aggregate for that group. For example an implementation for
/// `min` might look like
///
/// ```text
///    ┌─────┐
///    │  0  │───────────▶   100
///    ├─────┤
///    │  1  │───────────▶   200
///    └─────┘
///      ...                 ...
///    ┌─────┐
///    │ N-2 │───────────▶    50
///    ├─────┤
///    │ N-1 │───────────▶   200
///    └─────┘
///
///
///  Logical group      Current Min
///     number          value for that
///                     group
/// ```
///
/// # Notes on Implementing `GroupsAccumulator`
///
/// All aggregates must first implement the simpler [`Accumulator`] trait, which
/// handles state for a single group. Implementing `GroupsAccumulator` is
/// optional and is harder to implement than `Accumulator`, but can be much
/// faster for queries with many group values.  See the [Aggregating Millions of
/// Groups Fast blog] for more background.
/// For more background, please also see the [Aggregating Millions of Groups Fast in Apache Arrow DataFusion 28.0.0 blog]
///
/// [Aggregating Millions of Groups Fast in Apache Arrow DataFusion 28.0.0 blog]: https://datafusion.apache.org/blog/2023/08/05/datafusion_fast_grouping
///
/// [`NullState`] can help keep the state for groups that have not seen any
/// values and produce the correct output for those groups.
///
/// [`NullState`]: https://docs.rs/datafusion/latest/datafusion/physical_expr/struct.NullState.html
///
/// # Details
/// Each group is assigned a `group_index` by the hash table and each
/// accumulator manages the specific state, one per `group_index`.
///
/// `group_index`es are contiguous (there aren't gaps), and thus it is
/// expected that each `GroupsAccumulator` will use something like `Vec<..>`
/// to store the group states.
///
/// [`Accumulator`]: crate::accumulator::Accumulator
/// [Aggregating Millions of Groups Fast blog]: https://arrow.apache.org/blog/2023/08/05/datafusion_fast_grouping/
pub trait GroupsAccumulator: Send + std::any::Any {
    /// Supplies optional metrics owned by this aggregate expression.
    ///
    /// Call this immediately after construction and before the accumulator is
    /// used; replacing metrics after use is unsupported. The default preserves
    /// compatibility for accumulators without internal submetrics.
    fn set_metrics(&mut self, _metrics: Arc<dyn AggregateMetrics>) {}

    /// Updates the accumulator's state from its arguments, encoded as
    /// a vector of [`ArrayRef`]s.
    ///
    /// * `values`: the input arguments to the accumulator
    ///
    /// * `group_indices`: The group indices to which each row in `values` belongs.
    ///
    /// * `opt_filter`: if present, only update aggregate state using
    ///   `values[i]` if `opt_filter[i]` is true
    ///
    /// * `total_num_groups`: the number of groups (the largest
    ///   group_index is thus `total_num_groups - 1`).
    ///
    /// Note that subsequent calls to update_batch may have larger
    /// total_num_groups as new groups are seen.
    ///
    /// See [`NullState`] to help keep the state for groups that have not seen any
    /// values and produce the correct output for those groups.
    ///
    /// [`NullState`]: https://docs.rs/datafusion/latest/datafusion/physical_expr/struct.NullState.html
    fn update_batch(
        &mut self,
        values: &[ArrayRef],
        group_indices: &[usize],
        opt_filter: Option<&BooleanArray>,
        total_num_groups: usize,
    ) -> Result<()>;

    /// Returns the final aggregate value for each group as a single
    /// `RecordBatch`, resetting the internal state.
    ///
    /// The rows returned *must* be in group_index order: The value
    /// for group_index 0, followed by 1, etc.  Any group_index that
    /// did not have values, should be null.
    ///
    /// For example, a `SUM` accumulator maintains a running sum for
    /// each group, and `evaluate` will produce that running sum as
    /// its output for all groups, in group_index order
    ///
    /// If `emit_to` is [`EmitTo::All`], the accumulator should
    /// return all groups and release / reset its internal state
    /// equivalent to when it was first created.
    ///
    /// If `emit_to` is [`EmitTo::First`], only the first `n` groups
    /// should be emitted and the state for those first groups
    /// removed. State for the remaining groups must be retained for
    /// future use. The group_indices on subsequent calls to
    /// `update_batch` or `merge_batch` will be shifted down by
    /// `n`. See [`EmitTo::First`] for more details.
    fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef>;

    /// Returns final aggregate values without changing the logical state or
    /// group indices.
    ///
    /// Rows are returned in the order specified by `selection`. An empty
    /// selection returns a correctly typed array with no rows.
    ///
    /// This method requires exclusive access because implementations may mutate
    /// internal caches or builders. However, repeated calls and later updates
    /// must observe the same logical accumulator state.
    fn evaluate_preserving(
        &mut self,
        _selection: GroupSelection<'_>,
    ) -> Result<ArrayRef> {
        not_impl_err!("Preserving grouped evaluation is not implemented")
    }

    /// Returns `true` if [`Self::evaluate_preserving`] is implemented.
    fn supports_evaluate_preserving(&self) -> bool {
        false
    }

    /// Returns the intermediate aggregate state for this accumulator,
    /// used for multi-phase grouping, resetting its internal state.
    ///
    /// See [`Accumulator::state`] for more information on multi-phase
    /// aggregation.
    ///
    /// For example, `AVG` might return two arrays: `SUM` and `COUNT`
    /// but the `MIN` aggregate would just return a single array.
    ///
    /// Note more sophisticated internal state can be passed as
    /// single `StructArray` rather than multiple arrays.
    ///
    /// See [`Self::evaluate`] for details on the required output
    /// order and `emit_to`.
    ///
    /// [`Accumulator::state`]: crate::accumulator::Accumulator::state
    fn state(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>>;

    /// Returns intermediate aggregate state without changing the logical state
    /// or group indices.
    ///
    /// Each returned array has one row per selected group, in the order
    /// specified by `selection`. An empty selection returns the normal number
    /// of correctly typed state arrays, each with no rows.
    ///
    /// This method requires exclusive access because implementations may mutate
    /// internal caches or builders. However, repeated calls and later updates
    /// must observe the same logical accumulator state.
    fn state_preserving(
        &mut self,
        _selection: GroupSelection<'_>,
    ) -> Result<Vec<ArrayRef>> {
        not_impl_err!("Preserving grouped state is not implemented")
    }

    /// Returns `true` if [`Self::state_preserving`] is implemented.
    fn supports_state_preserving(&self) -> bool {
        false
    }

    /// Merges intermediate state (the output from [`Self::state`])
    /// into this accumulator's current state.
    ///
    /// For some aggregates (such as `SUM`), `merge_batch` is the same
    /// as `update_batch`, but for some aggregates (such as `COUNT`,
    /// where the partial counts must be summed) the operations
    /// differ. See [`Self::state`] for more details on how state is
    /// used and merged.
    ///
    /// * `values`: arrays produced from previously calling `state` on other accumulators.
    ///
    /// Other arguments are the same as for [`Self::update_batch`], except that
    /// there is no `opt_filter` — aggregate filters are applied during the
    /// partial (update) phase, so by the time intermediate states are merged
    /// no per-row filtering is needed.
    fn merge_batch(
        &mut self,
        values: &[ArrayRef],
        group_indices: &[usize],
        total_num_groups: usize,
    ) -> Result<()>;

    /// Converts an input batch directly to the intermediate aggregate state.
    ///
    /// This is the equivalent of treating each input row as its own group. It
    /// is invoked when the Partial phase of a multi-phase aggregation is not
    /// reducing the cardinality enough to warrant spending more effort on
    /// pre-aggregation (see `Background` section below), and switches to
    /// passing intermediate state directly on to the next aggregation phase.
    ///
    /// Examples:
    /// * `COUNT`: an array of 1s for each row in the input batch.
    /// * `SUM/MIN/MAX`: the input values themselves.
    ///
    /// # Arguments
    /// * `values`: the input arguments to the accumulator
    /// * `opt_filter`: if present, any row where `opt_filter[i]` is false should be ignored
    ///
    /// # Background
    ///
    /// In a multi-phase aggregation (see [`Accumulator::state`]), the initial
    /// Partial phase reduces the cardinality of the input data as soon as
    /// possible in the plan.
    ///
    /// This strategy is very effective for queries with a small number of
    /// groups, as most of the data is aggregated immediately and only a small
    /// amount of data must be repartitioned (see [`Accumulator::state`] for
    /// background)
    ///
    /// However, for queries with a large number of groups, the Partial phase
    /// often does not reduce the cardinality enough to warrant the memory and
    /// CPU cost of actually performing the aggregation. For such cases, the
    /// HashAggregate operator will dynamically switch to passing intermediate
    /// state directly to the next aggregation phase with minimal processing
    /// using this method.
    ///
    /// [`Accumulator::state`]: crate::accumulator::Accumulator::state
    fn convert_to_state(
        &self,
        values: &[ArrayRef],
        opt_filter: Option<&BooleanArray>,
    ) -> Result<Vec<ArrayRef>>;

    /// Amount of memory used to store the state of this accumulator,
    /// in bytes.
    ///
    /// This function is called once per batch, so it should be `O(n)` to
    /// compute, not `O(num_groups)`
    ///
    /// May be expensive; check the implementation before calling on hot paths.
    fn size(&self) -> usize;

    /// Keeps the stored groups but releases capacity they do not need, such as
    /// per-batch scratch buffers, so that [`Self::size`] reflects the stored
    /// groups. For example, after [`EmitTo::First`] removes most groups, an
    /// accumulator may keep a buffer it sized for the last input batch.
    ///
    /// The default does nothing.
    fn shrink_to_fit(&mut self) {}
}

#[cfg(test)]
mod tests {
    use super::{EmitTo, GroupSelection};

    /// When `n` is small relative to `len`, the old `split_off(n) + swap` pattern had
    /// two allocation problems:
    ///
    /// 1. The returned Vec kept the original large backing allocation even though it
    ///    only contains `n` elements (wasted capacity on a short-lived value).
    /// 2. `split_off` allocated a fresh Vec for the `len - n` remaining elements,
    ///    even though that side is much larger than `n` — the expensive side to
    ///    allocate.
    ///
    /// `split_vec_min_alloc` fixes both: when `n * 2 <= len` it uses
    /// `drain(0..n).collect()`, allocating only `n` elements for the emitted prefix
    /// and keeping the original large backing in the remaining accumulator.
    #[test]
    fn take_needed_first_small_n_allocates_minimally() {
        let mut v: Vec<i32> = Vec::with_capacity(128);
        v.extend(0..20i32);
        let original_capacity = v.capacity(); // 128

        // n=4, n*2=8 <= len=20 -> drain branch in split_vec_min_alloc
        let emitted = EmitTo::First(4).take_needed(&mut v);

        assert_eq!(emitted, vec![0, 1, 2, 3]);
        assert_eq!(v, (4..20i32).collect::<Vec<_>>());

        // The emitted prefix must NOT carry the original large allocation.
        // Old split_off+swap returned a Vec with capacity=128 for only 4 elements.
        assert!(
            emitted.capacity() <= 4,
            "emitted prefix capacity {} should be ~n=4, not the original {}",
            emitted.capacity(),
            original_capacity,
        );

        // The remaining accumulator must retain the original large allocation so
        // that incoming groups don't immediately force a realloc.
        // Old split_off+swap left the remaining vec with a small fresh allocation.
        assert_eq!(
            v.capacity(),
            original_capacity,
            "remaining vec capacity {} should equal original {}",
            v.capacity(),
            original_capacity,
        );
    }

    #[test]
    fn group_selection_is_validated_once_and_reusable() {
        let values = [10, 20, 30, 40];
        let selected =
            GroupSelection::try_from_indices(&[3, 1, 3], values.len()).unwrap();
        assert_eq!(selected.total_num_groups(), values.len());
        assert_eq!(selected.len(), 3);
        assert_eq!(
            selected
                .iter()
                .map(|index| values[index])
                .collect::<Vec<_>>(),
            vec![40, 20, 40]
        );
        selected.validate_num_groups(values.len()).unwrap();
        let error = selected.validate_num_groups(values.len() - 1).unwrap_err();
        assert!(error.to_string().contains("constructed for 4 groups"));

        let all = GroupSelection::all(values.len());
        assert_eq!(
            all.iter().map(|index| values[index]).collect::<Vec<_>>(),
            values
        );

        let empty = GroupSelection::try_from_indices(&[], values.len()).unwrap();
        assert!(empty.is_empty());
        assert!(empty.iter().next().is_none());

        let error = GroupSelection::try_from_indices(&[4], values.len()).unwrap_err();
        assert!(error.to_string().contains("out of bounds"));
    }
}