/Users/andrewlamb/Software/datafusion/datafusion/functions-aggregate/src/macros.rs
Line | Count | Source (jump to first uncovered line) |
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_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 | 0 | pub fn $EXPR_FN( |
23 | 0 | $($arg: datafusion_expr::Expr,)* |
24 | 0 | ) -> datafusion_expr::Expr { |
25 | 0 | datafusion_expr::Expr::AggregateFunction(datafusion_expr::expr::AggregateFunction::new_udf( |
26 | 0 | $AGGREGATE_UDF_FN(), |
27 | 0 | vec![$($arg),*], |
28 | 0 | false, |
29 | 0 | None, |
30 | 0 | None, |
31 | 0 | None, |
32 | 0 | )) |
33 | 0 | } |
34 | | }; |
35 | | } |
36 | | |
37 | | macro_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 | 0 | pub fn $EXPR_FN( |
46 | 0 | args: Vec<datafusion_expr::Expr>, |
47 | 0 | ) -> datafusion_expr::Expr { |
48 | 0 | datafusion_expr::Expr::AggregateFunction(datafusion_expr::expr::AggregateFunction::new_udf( |
49 | 0 | $AGGREGATE_UDF_FN(), |
50 | 0 | args, |
51 | 0 | false, |
52 | 0 | None, |
53 | 0 | None, |
54 | 0 | None, |
55 | 0 | )) |
56 | 0 | } |
57 | | |
58 | | create_func!($UDAF, $AGGREGATE_UDF_FN); |
59 | | }; |
60 | | } |
61 | | |
62 | | macro_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 | | /// Singleton instance of [$UDAF], ensures the UDAF is only created once |
69 | | /// named STATIC_$(UDAF). For example `STATIC_FirstValue` |
70 | | #[allow(non_upper_case_globals)] |
71 | | static [< STATIC_ $UDAF >]: std::sync::OnceLock<std::sync::Arc<datafusion_expr::AggregateUDF>> = |
72 | | std::sync::OnceLock::new(); |
73 | | |
74 | | #[doc = concat!("AggregateFunction that returns a [`AggregateUDF`](datafusion_expr::AggregateUDF) for [`", stringify!($UDAF), "`]")] |
75 | 36 | pub fn $AGGREGATE_UDF_FN() -> std::sync::Arc<datafusion_expr::AggregateUDF> { |
76 | 36 | [< STATIC_ $UDAF >] |
77 | 36 | .get_or_init(|| { |
78 | 7 | std::sync::Arc::new(datafusion_expr::AggregateUDF::from($CREATE)) |
79 | 36 | }) |
80 | 36 | .clone() |
81 | 36 | } |
82 | | } |
83 | | } |
84 | | } |