Skip to main content

datafusion_functions_nested/
macros_lambda.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5/// Creates external API functions for an array UDF. Specifically, creates
6///
7/// 1. Single `HigherOrderUDF` instance
8///
9/// Creates a singleton `HigherOrderUDF` of the `$UDF` function named `STATIC_$(UDF)` and a
10/// function named `$HIGHER_ORDER_UDF_FUNC` which returns that function named `STATIC_$(UDF)`.
11///
12/// This is used to ensure creating the list of `HigherOrderUDF` only happens once.
13///
14/// # 2. `expr_fn` style function
15///
16/// These are functions that create an `Expr` that invokes the UDF, used
17/// primarily to programmatically create expressions.
18///
19/// For example:
20/// ```text
21/// pub fn array_to_string(delimiter: Expr) -> Expr {
22/// ...
23/// }
24/// ```
25/// # Arguments
26/// * `UDF`: name of the [`HigherOrderUDF`]
27/// * `EXPR_FN`: name of the expr_fn function to be created
28/// * `arg`: 0 or more named arguments for the function
29/// * `DOC`: documentation string for the function
30/// * `HIGHER_ORDER_UDF_FUNC`: name of the function to create (just) the `HigherOrderUDF`
31/// * (optional) `$CTOR`: Pass a custom constructor. When omitted it
32///   automatically resolves to `$UDF::new()`.
33///
34/// [`HigherOrderUDF`]: datafusion_expr::HigherOrderUDF
35macro_rules! make_higher_order_function_expr_and_func {
36    ($UDF:ident, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $HIGHER_ORDER_UDF_FN:ident) => {
37        make_higher_order_function_expr_and_func!($UDF, $EXPR_FN, $($arg)*, $DOC, $HIGHER_ORDER_UDF_FN, $UDF::new);
38    };
39    ($UDF:ident, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $HIGHER_ORDER_UDF_FN:ident, $CTOR:path) => {
40        // "fluent expr_fn" style function
41        #[doc = $DOC]
42        pub fn $EXPR_FN($($arg: datafusion_expr::Expr),*) -> datafusion_expr::Expr {
43            datafusion_expr::Expr::HigherOrderFunction(datafusion_expr::expr::HigherOrderFunction::new(
44                $HIGHER_ORDER_UDF_FN(),
45                vec![$($arg),*],
46            ))
47        }
48        create_higher_order!($UDF, $HIGHER_ORDER_UDF_FN, $CTOR);
49    };
50    ($UDF:ident, $EXPR_FN:ident, $DOC:expr, $HIGHER_ORDER_UDF_FN:ident) => {
51        make_higher_order_function_expr_and_func!($UDF, $EXPR_FN, $DOC, $HIGHER_ORDER_UDF_FN, $UDF::new);
52    };
53    ($UDF:ident, $EXPR_FN:ident, $DOC:expr, $HIGHER_ORDER_UDF_FN:ident, $CTOR:path) => {
54        // "fluent expr_fn" style function
55        #[doc = $DOC]
56        pub fn $EXPR_FN(arg: Vec<datafusion_expr::Expr>) -> datafusion_expr::Expr {
57            datafusion_expr::Expr::HigherOrderFunction(datafusion_expr::expr::HigherOrderFunction::new(
58                $HIGHER_ORDER_UDF_FN(),
59                arg,
60            ))
61        }
62        create_higher_order!($UDF, $HIGHER_ORDER_UDF_FN, $CTOR);
63    };
64}
65
66/// Creates a singleton `HigherOrderUDF` of the `$UDF` function named `STATIC_$(UDF)` and a
67/// function named `$HIGHER_ORDER_UDF_FUNC` which returns that function named `STATIC_$(UDF)`.
68///
69/// This is used to ensure creating the list of `HigherOrderUDF` only happens once.
70///
71/// # Arguments
72/// * `UDF`: name of the [`HigherOrderUDF`]
73/// * `HIGHER_ORDER_UDF_FUNC`: name of the function to create (just) the `HigherOrderUDF`
74/// * (optional) `$CTOR`: Pass a custom constructor. When omitted it
75///   automatically resolves to `$UDF::new()`.
76///
77/// [`HigherOrderUDF`]: datafusion_expr::HigherOrderUDF
78macro_rules! create_higher_order {
79    ($UDF:ident, $HIGHER_ORDER_UDF_FN:ident) => {
80        create_higher_order!($UDF, $HIGHER_ORDER_UDF_FN, $UDF::new);
81    };
82    ($UDF:ident, $HIGHER_ORDER_UDF_FN:ident, $CTOR:path) => {
83        #[doc = concat!("HigherOrderFunction that returns a [`HigherOrderUDF`](datafusion_expr::HigherOrderUDF) for ")]
84        #[doc = stringify!($UDF)]
85        pub fn $HIGHER_ORDER_UDF_FN() -> std::sync::Arc<datafusion_expr::HigherOrderUDF> {
86            // Singleton instance of [`$UDF`], ensures the UDF is only created once
87            static INSTANCE: std::sync::LazyLock<std::sync::Arc<datafusion_expr::HigherOrderUDF>> =
88                std::sync::LazyLock::new(|| {
89                    std::sync::Arc::new(datafusion_expr::HigherOrderUDF::new_from_impl($CTOR()))
90                });
91            std::sync::Arc::clone(&INSTANCE)
92        }
93    };
94}