Skip to main content

datafusion_functions_aggregate_common/
noop_accumulator.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use arrow::array::ArrayRef;
6use datafusion_common::{Result, ScalarValue};
7use datafusion_expr_common::accumulator::Accumulator;
8
9/// [`Accumulator`] that does no work and always returns a fixed value (default
10/// of `NULL` but can be customized).
11///
12/// Useful for aggregate functions that need to handle an input of [`DataType::Null`]
13/// that does no work.
14///
15/// [`DataType::Null`]: arrow::datatypes::DataType::Null
16#[derive(Debug)]
17pub struct NoopAccumulator {
18    evaluate_value: ScalarValue,
19}
20
21impl NoopAccumulator {
22    pub fn new(evaluate_value: ScalarValue) -> Self {
23        Self { evaluate_value }
24    }
25}
26
27impl Default for NoopAccumulator {
28    fn default() -> Self {
29        Self {
30            evaluate_value: ScalarValue::Null,
31        }
32    }
33}
34
35impl Accumulator for NoopAccumulator {
36    fn update_batch(&mut self, _values: &[ArrayRef]) -> Result<()> {
37        Ok(())
38    }
39
40    fn evaluate(&mut self) -> Result<ScalarValue> {
41        Ok(self.evaluate_value.clone())
42    }
43
44    fn size(&self) -> usize {
45        size_of_val(self)
46    }
47
48    fn state(&mut self) -> Result<Vec<ScalarValue>> {
49        // We ensure we return a state field even if unused otherwise we run into
50        // issues with queries like `SELECT agg_fn(NULL) FROM table`
51        Ok(vec![ScalarValue::Null])
52    }
53
54    fn merge_batch(&mut self, _states: &[ArrayRef]) -> Result<()> {
55        Ok(())
56    }
57}