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