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 /// Input schema to the aggregate function. If you need to check data type, nullability
34 /// or metadata of input arguments then you should use `expr_fields` below instead.
35 pub schema: &'a Schema,
36
37 /// Whether to ignore nulls.
38 ///
39 /// SQL allows the user to specify `IGNORE NULLS`, for example:
40 ///
41 /// ```sql
42 /// SELECT FIRST_VALUE(column1) IGNORE NULLS FROM t;
43 /// ```
44 pub ignore_nulls: bool,
45
46 /// The expressions in the `ORDER BY` clause passed to this aggregator.
47 ///
48 /// SQL allows the user to specify the ordering of arguments to the
49 /// aggregate using an `ORDER BY`. For example:
50 ///
51 /// ```sql
52 /// SELECT FIRST_VALUE(column1 ORDER BY column2) FROM t;
53 /// ```
54 pub order_bys: &'a [PhysicalSortExpr],
55
56 /// Whether the aggregation is running in reverse order
57 pub is_reversed: bool,
58
59 /// The name of the aggregate expression
60 pub name: &'a str,
61
62 /// Whether the aggregate function is distinct.
63 ///
64 /// ```sql
65 /// SELECT COUNT(DISTINCT column1) FROM t;
66 /// ```
67 pub is_distinct: bool,
68
69 /// The physical expression of arguments the aggregate function takes.
70 pub exprs: &'a [Arc<dyn PhysicalExpr>],
71
72 /// Fields corresponding to each expr (same order & length).
73 pub expr_fields: &'a [FieldRef],
74}
75
76impl AccumulatorArgs<'_> {
77 /// Returns the return type of the aggregate function.
78 pub fn return_type(&self) -> &DataType {
79 self.return_field.data_type()
80 }
81}
82
83/// Factory that returns an accumulator for the given aggregate function.
84pub type AccumulatorFactoryFunction =
85 Arc<dyn Fn(AccumulatorArgs) -> Result<Box<dyn Accumulator>> + Send + Sync>;
86
87/// [`StateFieldsArgs`] contains information about the fields that an
88/// aggregate function's accumulator should have. Used for `AggregateUDFImpl::state_fields`.
89pub struct StateFieldsArgs<'a> {
90 /// The name of the aggregate function.
91 pub name: &'a str,
92
93 /// The input fields of the aggregate function.
94 pub input_fields: &'a [FieldRef],
95
96 /// The return fields of the aggregate function.
97 pub return_field: FieldRef,
98
99 /// The ordering fields of the aggregate function.
100 pub ordering_fields: &'a [FieldRef],
101
102 /// Whether the aggregate function is distinct.
103 pub is_distinct: bool,
104}
105
106impl StateFieldsArgs<'_> {
107 /// The return type of the aggregate function.
108 pub fn return_type(&self) -> &DataType {
109 self.return_field.data_type()
110 }
111}