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/// * (optional) `$CTOR`: Pass a custom constructor. When omitted it
45///   automatically resolves to `$UDF::new()`.
46///
47/// [`ScalarUDFImpl`]: datafusion_expr::ScalarUDFImpl
48macro_rules! make_udf_expr_and_func {
49    ($UDF:ident, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $SCALAR_UDF_FN:ident) => {
50        make_udf_expr_and_func!($UDF, $EXPR_FN, $($arg)*, $DOC, $SCALAR_UDF_FN, $UDF::new);
51    };
52    ($UDF:ident, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $SCALAR_UDF_FN:ident, $CTOR:path) => {
53        paste::paste! {
54            // "fluent expr_fn" style function
55            #[doc = $DOC]
56            pub fn $EXPR_FN($($arg: datafusion_expr::Expr),*) -> datafusion_expr::Expr {
57                datafusion_expr::Expr::ScalarFunction(datafusion_expr::expr::ScalarFunction::new_udf(
58                    $SCALAR_UDF_FN(),
59                    vec![$($arg),*],
60                ))
61            }
62            create_func!($UDF, $SCALAR_UDF_FN, $CTOR);
63        }
64    };
65    ($UDF:ident, $EXPR_FN:ident, $DOC:expr, $SCALAR_UDF_FN:ident) => {
66        make_udf_expr_and_func!($UDF, $EXPR_FN, $DOC, $SCALAR_UDF_FN, $UDF::new);
67    };
68    ($UDF:ident, $EXPR_FN:ident, $DOC:expr, $SCALAR_UDF_FN:ident, $CTOR:path) => {
69        paste::paste! {
70            // "fluent expr_fn" style function
71            #[doc = $DOC]
72            pub fn $EXPR_FN(arg: Vec<datafusion_expr::Expr>) -> datafusion_expr::Expr {
73                datafusion_expr::Expr::ScalarFunction(datafusion_expr::expr::ScalarFunction::new_udf(
74                    $SCALAR_UDF_FN(),
75                    arg,
76                ))
77            }
78            create_func!($UDF, $SCALAR_UDF_FN, $CTOR);
79        }
80    };
81}
82
83/// Creates a singleton `ScalarUDF` of the `$UDF` function named `STATIC_$(UDF)` and a
84/// function named `$SCALAR_UDF_FUNC` which returns that function named `STATIC_$(UDF)`.
85///
86/// This is used to ensure creating the list of `ScalarUDF` only happens once.
87///
88/// # Arguments
89/// * `UDF`: name of the [`ScalarUDFImpl`]
90/// * `SCALAR_UDF_FUNC`: name of the function to create (just) the `ScalarUDF`
91/// * (optional) `$CTOR`: Pass a custom constructor. When omitted it
92///   automatically resolves to `$UDF::new()`.
93///
94/// [`ScalarUDFImpl`]: datafusion_expr::ScalarUDFImpl
95macro_rules! create_func {
96    ($UDF:ident, $SCALAR_UDF_FN:ident) => {
97        create_func!($UDF, $SCALAR_UDF_FN, $UDF::new);
98    };
99    ($UDF:ident, $SCALAR_UDF_FN:ident, $CTOR:path) => {
100        paste::paste! {
101            #[doc = concat!("ScalarFunction that returns a [`ScalarUDF`](datafusion_expr::ScalarUDF) for ")]
102            #[doc = stringify!($UDF)]
103            pub fn $SCALAR_UDF_FN() -> std::sync::Arc<datafusion_expr::ScalarUDF> {
104                // Singleton instance of [`$UDF`], ensures the UDF is only created once
105                static INSTANCE: std::sync::LazyLock<std::sync::Arc<datafusion_expr::ScalarUDF>> =
106                    std::sync::LazyLock::new(|| {
107                        std::sync::Arc::new(datafusion_expr::ScalarUDF::new_from_impl(
108                            $CTOR(),
109                        ))
110                    });
111                std::sync::Arc::clone(&INSTANCE)
112            }
113        }
114    };
115}