Skip to main content

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