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, Field, 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 type of the aggregate function.
31 pub return_type: &'a DataType,
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
74/// Factory that returns an accumulator for the given aggregate function.
75pub type AccumulatorFactoryFunction =
76 Arc<dyn Fn(AccumulatorArgs) -> Result<Box<dyn Accumulator>> + Send + Sync>;
77
78/// [`StateFieldsArgs`] contains information about the fields that an
79/// aggregate function's accumulator should have. Used for `AggregateUDFImpl::state_fields`.
80pub struct StateFieldsArgs<'a> {
81 /// The name of the aggregate function.
82 pub name: &'a str,
83
84 /// The input types of the aggregate function.
85 pub input_types: &'a [DataType],
86
87 /// The return type of the aggregate function.
88 pub return_type: &'a DataType,
89
90 /// The ordering fields of the aggregate function.
91 pub ordering_fields: &'a [Field],
92
93 /// Whether the aggregate function is distinct.
94 pub is_distinct: bool,
95}