datafusion_functions_nested/macros.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 `ScalarUDF` instance
8///
9/// Creates a singleton `ScalarUDF` of the `$UDF` function named `STATIC_$(UDF)` and a
10/// function named `$SCALAR_UDF_FUNC` which returns that function named `STATIC_$(UDF)`.
11///
12/// This is used to ensure creating the list of `ScalarUDF` 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 [`ScalarUDFImpl`]
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/// * `SCALAR_UDF_FUNC`: name of the function to create (just) the `ScalarUDF`
31/// * (optional) `$CTOR`: Pass a custom constructor. When omitted it
32/// automatically resolves to `$UDF::new()`.
33///
34/// [`ScalarUDFImpl`]: datafusion_expr::ScalarUDFImpl
35macro_rules! make_udf_expr_and_func {
36 ($UDF:ident, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $SCALAR_UDF_FN:ident) => {
37 make_udf_expr_and_func!($UDF, $EXPR_FN, $($arg)*, $DOC, $SCALAR_UDF_FN, $UDF::new);
38 };
39 ($UDF:ident, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $SCALAR_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::ScalarFunction(datafusion_expr::expr::ScalarFunction::new_udf(
44 $SCALAR_UDF_FN(),
45 vec![$($arg),*],
46 ))
47 }
48 create_func!($UDF, $SCALAR_UDF_FN, $CTOR);
49 };
50 ($UDF:ident, $EXPR_FN:ident, $DOC:expr, $SCALAR_UDF_FN:ident) => {
51 make_udf_expr_and_func!($UDF, $EXPR_FN, $DOC, $SCALAR_UDF_FN, $UDF::new);
52 };
53 ($UDF:ident, $EXPR_FN:ident, $DOC:expr, $SCALAR_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::ScalarFunction(datafusion_expr::expr::ScalarFunction::new_udf(
58 $SCALAR_UDF_FN(),
59 arg,
60 ))
61 }
62 create_func!($UDF, $SCALAR_UDF_FN, $CTOR);
63 };
64}
65
66/// Creates a singleton `ScalarUDF` of the `$UDF` function named `STATIC_$(UDF)` and a
67/// function named `$SCALAR_UDF_FUNC` which returns that function named `STATIC_$(UDF)`.
68///
69/// This is used to ensure creating the list of `ScalarUDF` only happens once.
70///
71/// # Arguments
72/// * `UDF`: name of the [`ScalarUDFImpl`]
73/// * `SCALAR_UDF_FUNC`: name of the function to create (just) the `ScalarUDF`
74/// * (optional) `$CTOR`: Pass a custom constructor. When omitted it
75/// automatically resolves to `$UDF::new()`.
76///
77/// [`ScalarUDFImpl`]: datafusion_expr::ScalarUDFImpl
78macro_rules! create_func {
79 ($UDF:ident, $SCALAR_UDF_FN:ident) => {
80 create_func!($UDF, $SCALAR_UDF_FN, $UDF::new);
81 };
82 ($UDF:ident, $SCALAR_UDF_FN:ident, $CTOR:path) => {
83 #[doc = concat!("ScalarFunction that returns a [`ScalarUDF`](datafusion_expr::ScalarUDF) for ")]
84 #[doc = stringify!($UDF)]
85 pub fn $SCALAR_UDF_FN() -> std::sync::Arc<datafusion_expr::ScalarUDF> {
86 // Singleton instance of [`$UDF`], ensures the UDF is only created once
87 static INSTANCE: std::sync::LazyLock<std::sync::Arc<datafusion_expr::ScalarUDF>> =
88 std::sync::LazyLock::new(|| {
89 std::sync::Arc::new(datafusion_expr::ScalarUDF::new_from_impl(
90 $CTOR(),
91 ))
92 });
93 std::sync::Arc::clone(&INSTANCE)
94 }
95 };
96}