datafusion_functions_aggregate/
macros.rs1#[macro_export]
19macro_rules! make_udaf_expr {
20 ($EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $AGGREGATE_UDF_FN:ident) => {
21 #[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 #[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 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}