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::LexOrdering;
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)]
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    ///
54    /// If no `ORDER BY` is specified, `ordering_req` will be empty.
55    pub ordering_req: &'a LexOrdering,
56
57    /// Whether the aggregation is running in reverse order
58    pub is_reversed: bool,
59
60    /// The name of the aggregate expression
61    pub name: &'a str,
62
63    /// Whether the aggregate function is distinct.
64    ///
65    /// ```sql
66    /// SELECT COUNT(DISTINCT column1) FROM t;
67    /// ```
68    pub is_distinct: bool,
69
70    /// The physical expression of arguments the aggregate function takes.
71    pub exprs: &'a [Arc<dyn PhysicalExpr>],
72}
73
74impl AccumulatorArgs<'_> {
75    /// Returns the return type of the aggregate function.
76    pub fn return_type(&self) -> &DataType {
77        self.return_field.data_type()
78    }
79}
80
81/// Factory that returns an accumulator for the given aggregate function.
82pub type AccumulatorFactoryFunction =
83    Arc<dyn Fn(AccumulatorArgs) -> Result<Box<dyn Accumulator>> + Send + Sync>;
84
85/// [`StateFieldsArgs`] contains information about the fields that an
86/// aggregate function's accumulator should have. Used for `AggregateUDFImpl::state_fields`.
87pub struct StateFieldsArgs<'a> {
88    /// The name of the aggregate function.
89    pub name: &'a str,
90
91    /// The input fields of the aggregate function.
92    pub input_fields: &'a [FieldRef],
93
94    /// The return fields of the aggregate function.
95    pub return_field: FieldRef,
96
97    /// The ordering fields of the aggregate function.
98    pub ordering_fields: &'a [FieldRef],
99
100    /// Whether the aggregate function is distinct.
101    pub is_distinct: bool,
102}
103
104impl StateFieldsArgs<'_> {
105    /// The return type of the aggregate function.
106    pub fn return_type(&self) -> &DataType {
107        self.return_field.data_type()
108    }
109}