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.
1718#[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]
23pub 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(),
28vec![$($arg),*],
29false,
30None,
31None,
32None,
33 ))
34 }
35 };
36}
3738#[macro_export]
39macro_rules! make_udaf_expr_and_func {
40 ($UDAF:ty, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $AGGREGATE_UDF_FN:ident) => {
41make_udaf_expr!($EXPR_FN, $($arg)*, $DOC, $AGGREGATE_UDF_FN);
42create_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]
47pub 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,
53false,
54None,
55None,
56None,
57 ))
58 }
5960create_func!($UDAF, $AGGREGATE_UDF_FN);
61 };
62}
6364#[macro_export]
65macro_rules! create_func {
66 ($UDAF:ty, $AGGREGATE_UDF_FN:ident) => {
67create_func!($UDAF, $AGGREGATE_UDF_FN, <$UDAF>::default());
68 };
69 ($UDAF:ty, $AGGREGATE_UDF_FN:ident, $CREATE:expr) => {
70paste::paste! {
71#[doc = concat!("AggregateFunction that returns a [`AggregateUDF`](datafusion_expr::AggregateUDF) for [`", stringify!($UDAF), "`]")]
72pub fn $AGGREGATE_UDF_FN() -> std::sync::Arc<datafusion_expr::AggregateUDF> {
73// Singleton instance of [$UDAF], ensures the UDAF is only created once
74static 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}