datafusion_functions_aggregate_common/
accumulator.rs

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
18use arrow::datatypes::{DataType, FieldRef, Schema};
19use datafusion_common::Result;
20use datafusion_expr_common::accumulator::Accumulator;
21use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
22use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr;
23use std::sync::Arc;
24
25/// [`AccumulatorArgs`] contains information about how an aggregate
26/// function was called, including the types of its arguments and any optional
27/// ordering expressions.
28#[derive(Debug, Clone)]
29pub struct AccumulatorArgs<'a> {
30    /// The return field of the aggregate function.
31    pub return_field: FieldRef,
32
33    /// The schema of the input arguments
34    pub schema: &'a Schema,
35
36    /// Whether to ignore nulls.
37    ///
38    /// SQL allows the user to specify `IGNORE NULLS`, for example:
39    ///
40    /// ```sql
41    /// SELECT FIRST_VALUE(column1) IGNORE NULLS FROM t;
42    /// ```
43    pub ignore_nulls: bool,
44
45    /// The expressions in the `ORDER BY` clause passed to this aggregator.
46    ///
47    /// SQL allows the user to specify the ordering of arguments to the
48    /// aggregate using an `ORDER BY`. For example:
49    ///
50    /// ```sql
51    /// SELECT FIRST_VALUE(column1 ORDER BY column2) FROM t;
52    /// ```
53    pub order_bys: &'a [PhysicalSortExpr],
54
55    /// Whether the aggregation is running in reverse order
56    pub is_reversed: bool,
57
58    /// The name of the aggregate expression
59    pub name: &'a str,
60
61    /// Whether the aggregate function is distinct.
62    ///
63    /// ```sql
64    /// SELECT COUNT(DISTINCT column1) FROM t;
65    /// ```
66    pub is_distinct: bool,
67
68    /// The physical expression of arguments the aggregate function takes.
69    pub exprs: &'a [Arc<dyn PhysicalExpr>],
70}
71
72impl AccumulatorArgs<'_> {
73    /// Returns the return type of the aggregate function.
74    pub fn return_type(&self) -> &DataType {
75        self.return_field.data_type()
76    }
77}
78
79/// Factory that returns an accumulator for the given aggregate function.
80pub type AccumulatorFactoryFunction =
81    Arc<dyn Fn(AccumulatorArgs) -> Result<Box<dyn Accumulator>> + Send + Sync>;
82
83/// [`StateFieldsArgs`] contains information about the fields that an
84/// aggregate function's accumulator should have. Used for `AggregateUDFImpl::state_fields`.
85pub struct StateFieldsArgs<'a> {
86    /// The name of the aggregate function.
87    pub name: &'a str,
88
89    /// The input fields of the aggregate function.
90    pub input_fields: &'a [FieldRef],
91
92    /// The return fields of the aggregate function.
93    pub return_field: FieldRef,
94
95    /// The ordering fields of the aggregate function.
96    pub ordering_fields: &'a [FieldRef],
97
98    /// Whether the aggregate function is distinct.
99    pub is_distinct: bool,
100}
101
102impl StateFieldsArgs<'_> {
103    /// The return type of the aggregate function.
104    pub fn return_type(&self) -> &DataType {
105        self.return_field.data_type()
106    }
107}