datafusion_functions_aggregate/
macros.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
18#[macro_export]
19macro_rules! make_udaf_expr {
20    ($EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $AGGREGATE_UDF_FN:ident) => {
21        // "fluent expr_fn" style function
22        #[doc = $DOC]
23        pub fn $EXPR_FN(
24            $($arg: datafusion_expr::Expr,)*
25        ) -> datafusion_expr::Expr {
26            datafusion_expr::Expr::AggregateFunction(datafusion_expr::expr::AggregateFunction::new_udf(
27                $AGGREGATE_UDF_FN(),
28                vec![$($arg),*],
29                false,
30                None,
31                None,
32                None,
33            ))
34        }
35    };
36}
37
38#[macro_export]
39macro_rules! make_udaf_expr_and_func {
40    ($UDAF:ty, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $AGGREGATE_UDF_FN:ident) => {
41        make_udaf_expr!($EXPR_FN, $($arg)*, $DOC, $AGGREGATE_UDF_FN);
42        create_func!($UDAF, $AGGREGATE_UDF_FN);
43    };
44    ($UDAF:ty, $EXPR_FN:ident, $DOC:expr, $AGGREGATE_UDF_FN:ident) => {
45        // "fluent expr_fn" style function
46        #[doc = $DOC]
47        pub fn $EXPR_FN(
48            args: Vec<datafusion_expr::Expr>,
49        ) -> datafusion_expr::Expr {
50            datafusion_expr::Expr::AggregateFunction(datafusion_expr::expr::AggregateFunction::new_udf(
51                $AGGREGATE_UDF_FN(),
52                args,
53                false,
54                None,
55                None,
56                None,
57            ))
58        }
59
60        create_func!($UDAF, $AGGREGATE_UDF_FN);
61    };
62}
63
64#[macro_export]
65macro_rules! create_func {
66    ($UDAF:ty, $AGGREGATE_UDF_FN:ident) => {
67        create_func!($UDAF, $AGGREGATE_UDF_FN, <$UDAF>::default());
68    };
69    ($UDAF:ty, $AGGREGATE_UDF_FN:ident, $CREATE:expr) => {
70        paste::paste! {
71            #[doc = concat!("AggregateFunction that returns a [`AggregateUDF`](datafusion_expr::AggregateUDF) for [`", stringify!($UDAF), "`]")]
72            pub fn $AGGREGATE_UDF_FN() -> std::sync::Arc<datafusion_expr::AggregateUDF> {
73                // Singleton instance of [$UDAF], ensures the UDAF is only created once
74                static INSTANCE: std::sync::LazyLock<std::sync::Arc<datafusion_expr::AggregateUDF>> =
75                    std::sync::LazyLock::new(|| {
76                        std::sync::Arc::new(datafusion_expr::AggregateUDF::from($CREATE))
77                    });
78                std::sync::Arc::clone(&INSTANCE)
79            }
80        }
81    }
82}