Skip to main content

datafusion_functions_nested/
macros_lambda.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/// Creates external API functions for an array UDF. Specifically, creates
19///
20/// 1. Single `HigherOrderUDF` instance
21///
22/// Creates a singleton `HigherOrderUDF` of the `$UDF` function named `STATIC_$(UDF)` and a
23/// function named `$HIGHER_ORDER_UDF_FUNC` which returns that function named `STATIC_$(UDF)`.
24///
25/// This is used to ensure creating the list of `HigherOrderUDF` only happens once.
26///
27/// # 2. `expr_fn` style function
28///
29/// These are functions that create an `Expr` that invokes the UDF, used
30/// primarily to programmatically create expressions.
31///
32/// For example:
33/// ```text
34/// pub fn array_to_string(delimiter: Expr) -> Expr {
35/// ...
36/// }
37/// ```
38/// # Arguments
39/// * `UDF`: name of the [`HigherOrderUDF`]
40/// * `EXPR_FN`: name of the expr_fn function to be created
41/// * `arg`: 0 or more named arguments for the function
42/// * `DOC`: documentation string for the function
43/// * `HIGHER_ORDER_UDF_FUNC`: name of the function to create (just) the `HigherOrderUDF`
44/// * (optional) `$CTOR`: Pass a custom constructor. When omitted it
45///   automatically resolves to `$UDF::new()`.
46///
47/// [`HigherOrderUDF`]: datafusion_expr::HigherOrderUDF
48macro_rules! make_higher_order_function_expr_and_func {
49    ($UDF:ident, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $HIGHER_ORDER_UDF_FN:ident) => {
50        make_higher_order_function_expr_and_func!($UDF, $EXPR_FN, $($arg)*, $DOC, $HIGHER_ORDER_UDF_FN, $UDF::new);
51    };
52    ($UDF:ident, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $HIGHER_ORDER_UDF_FN:ident, $CTOR:path) => {
53        // "fluent expr_fn" style function
54        #[doc = $DOC]
55        pub fn $EXPR_FN($($arg: datafusion_expr::Expr),*) -> datafusion_expr::Expr {
56            datafusion_expr::Expr::HigherOrderFunction(datafusion_expr::expr::HigherOrderFunction::new(
57                $HIGHER_ORDER_UDF_FN(),
58                vec![$($arg),*],
59            ))
60        }
61        create_higher_order!($UDF, $HIGHER_ORDER_UDF_FN, $CTOR);
62    };
63    ($UDF:ident, $EXPR_FN:ident, $DOC:expr, $HIGHER_ORDER_UDF_FN:ident) => {
64        make_higher_order_function_expr_and_func!($UDF, $EXPR_FN, $DOC, $HIGHER_ORDER_UDF_FN, $UDF::new);
65    };
66    ($UDF:ident, $EXPR_FN:ident, $DOC:expr, $HIGHER_ORDER_UDF_FN:ident, $CTOR:path) => {
67        // "fluent expr_fn" style function
68        #[doc = $DOC]
69        pub fn $EXPR_FN(arg: Vec<datafusion_expr::Expr>) -> datafusion_expr::Expr {
70            datafusion_expr::Expr::HigherOrderFunction(datafusion_expr::expr::HigherOrderFunction::new(
71                $HIGHER_ORDER_UDF_FN(),
72                arg,
73            ))
74        }
75        create_higher_order!($UDF, $HIGHER_ORDER_UDF_FN, $CTOR);
76    };
77}
78
79/// Creates a singleton `HigherOrderUDF` of the `$UDF` function named `STATIC_$(UDF)` and a
80/// function named `$HIGHER_ORDER_UDF_FUNC` which returns that function named `STATIC_$(UDF)`.
81///
82/// This is used to ensure creating the list of `HigherOrderUDF` only happens once.
83///
84/// # Arguments
85/// * `UDF`: name of the [`HigherOrderUDF`]
86/// * `HIGHER_ORDER_UDF_FUNC`: name of the function to create (just) the `HigherOrderUDF`
87/// * (optional) `$CTOR`: Pass a custom constructor. When omitted it
88///   automatically resolves to `$UDF::new()`.
89///
90/// [`HigherOrderUDF`]: datafusion_expr::HigherOrderUDF
91macro_rules! create_higher_order {
92    ($UDF:ident, $HIGHER_ORDER_UDF_FN:ident) => {
93        create_higher_order!($UDF, $HIGHER_ORDER_UDF_FN, $UDF::new);
94    };
95    ($UDF:ident, $HIGHER_ORDER_UDF_FN:ident, $CTOR:path) => {
96        #[doc = concat!("HigherOrderFunction that returns a [`HigherOrderUDF`](datafusion_expr::HigherOrderUDF) for ")]
97        #[doc = stringify!($UDF)]
98        pub fn $HIGHER_ORDER_UDF_FN() -> std::sync::Arc<datafusion_expr::HigherOrderUDF> {
99            // Singleton instance of [`$UDF`], ensures the UDF is only created once
100            static INSTANCE: std::sync::LazyLock<std::sync::Arc<datafusion_expr::HigherOrderUDF>> =
101                std::sync::LazyLock::new(|| {
102                    std::sync::Arc::new(datafusion_expr::HigherOrderUDF::new_from_impl($CTOR()))
103                });
104            std::sync::Arc::clone(&INSTANCE)
105        }
106    };
107}