datafusion_functions_nested/
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
18/// Creates external API functions for an array UDF. Specifically, creates
19///
20/// 1. Single `ScalarUDF` instance
21///
22/// Creates a singleton `ScalarUDF` of the `$UDF` function named `STATIC_$(UDF)` and a
23/// function named `$SCALAR_UDF_FUNC` which returns that function named `STATIC_$(UDF)`.
24///
25/// This is used to ensure creating the list of `ScalarUDF` 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 [`ScalarUDFImpl`]
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/// * `SCALAR_UDF_FUNC`: name of the function to create (just) the `ScalarUDF`
44///
45/// [`ScalarUDFImpl`]: datafusion_expr::ScalarUDFImpl
46macro_rules! make_udf_expr_and_func {
47    ($UDF:ty, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr , $SCALAR_UDF_FN:ident) => {
48        paste::paste! {
49            // "fluent expr_fn" style function
50            #[doc = $DOC]
51            pub fn $EXPR_FN($($arg: datafusion_expr::Expr),*) -> datafusion_expr::Expr {
52                datafusion_expr::Expr::ScalarFunction(datafusion_expr::expr::ScalarFunction::new_udf(
53                    $SCALAR_UDF_FN(),
54                    vec![$($arg),*],
55                ))
56            }
57            create_func!($UDF, $SCALAR_UDF_FN);
58        }
59    };
60    ($UDF:ty, $EXPR_FN:ident, $DOC:expr , $SCALAR_UDF_FN:ident) => {
61        paste::paste! {
62            // "fluent expr_fn" style function
63            #[doc = $DOC]
64            pub fn $EXPR_FN(arg: Vec<datafusion_expr::Expr>) -> datafusion_expr::Expr {
65                datafusion_expr::Expr::ScalarFunction(datafusion_expr::expr::ScalarFunction::new_udf(
66                    $SCALAR_UDF_FN(),
67                    arg,
68                ))
69            }
70            create_func!($UDF, $SCALAR_UDF_FN);
71        }
72    };
73}
74
75/// Creates a singleton `ScalarUDF` of the `$UDF` function named `STATIC_$(UDF)` and a
76/// function named `$SCALAR_UDF_FUNC` which returns that function named `STATIC_$(UDF)`.
77///
78/// This is used to ensure creating the list of `ScalarUDF` only happens once.
79///
80/// # Arguments
81/// * `UDF`: name of the [`ScalarUDFImpl`]
82/// * `SCALAR_UDF_FUNC`: name of the function to create (just) the `ScalarUDF`
83///
84/// [`ScalarUDFImpl`]: datafusion_expr::ScalarUDFImpl
85macro_rules! create_func {
86    ($UDF:ty, $SCALAR_UDF_FN:ident) => {
87        paste::paste! {
88            #[doc = concat!("ScalarFunction that returns a [`ScalarUDF`](datafusion_expr::ScalarUDF) for ")]
89            #[doc = stringify!($UDF)]
90            pub fn $SCALAR_UDF_FN() -> std::sync::Arc<datafusion_expr::ScalarUDF> {
91                // Singleton instance of [`$UDF`], ensures the UDF is only created once
92                static INSTANCE: std::sync::LazyLock<std::sync::Arc<datafusion_expr::ScalarUDF>> =
93                    std::sync::LazyLock::new(|| {
94                        std::sync::Arc::new(datafusion_expr::ScalarUDF::new_from_impl(
95                            <$UDF>::new(),
96                        ))
97                    });
98                std::sync::Arc::clone(&INSTANCE)
99            }
100        }
101    };
102}