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