Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion datafusion/functions/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ macro_rules! export_functions {
}

/// Creates a singleton `ScalarUDF` of the `$UDF` function and a function
/// named `$NAME` which returns that singleton.
/// named `$NAME` which returns that singleton. Optionally use a custom constructor
/// `$CTOR` which defaults to `$UDF::new()` if not specified.
///
/// This is used to ensure creating the list of `ScalarUDF` only happens once.
#[macro_export]
Expand All @@ -97,6 +98,21 @@ macro_rules! make_udf_function {
std::sync::Arc::clone(&INSTANCE)
}
};
($UDF:ty, $NAME:ident, $CTOR:path) => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to accommodate being able to use a single struct (e.g. SparkBitShift) for multiple different functions; similar to how allow for window functions:

macro_rules! get_or_init_udwf {
($UDWF:ident, $OUT_FN_NAME:ident, $DOC:expr) => {
get_or_init_udwf!($UDWF, $OUT_FN_NAME, $DOC, $UDWF::default);
};
($UDWF:ident, $OUT_FN_NAME:ident, $DOC:expr, $CTOR:path) => {
paste::paste! {
#[doc = concat!(" Returns a [`WindowUDF`](datafusion_expr::WindowUDF) for [`", stringify!($OUT_FN_NAME), "`].")]
#[doc = ""]
#[doc = concat!(" ", $DOC)]
pub fn [<$OUT_FN_NAME _udwf>]() -> std::sync::Arc<datafusion_expr::WindowUDF> {
// Singleton instance of UDWF, ensures it is only created once.
static INSTANCE: std::sync::LazyLock<std::sync::Arc<datafusion_expr::WindowUDF>> =
std::sync::LazyLock::new(|| {
std::sync::Arc::new(datafusion_expr::WindowUDF::from($CTOR()))
});
std::sync::Arc::clone(&INSTANCE)
}
}
};
}

I had a bit of difficulty trying to reduce it to something simpler like:

macro_rules! make_udf_function {
    ($UDF:ty, $NAME:ident) => {
        make_udf_function!($UDF, $NAME, $UDF::new); // Error on :: token
    };
    ($UDF:ty, $NAME:ident, $CTOR:path) => {
        #[allow(rustdoc::redundant_explicit_links)]
        #[doc = concat!("Return a [`ScalarUDF`](datafusion_expr::ScalarUDF) implementation of ", stringify!($NAME))]
        pub fn $NAME() -> std::sync::Arc<datafusion_expr::ScalarUDF> {
            // Singleton instance of the function
            static INSTANCE: std::sync::LazyLock<
                std::sync::Arc<datafusion_expr::ScalarUDF>,
            > = std::sync::LazyLock::new(|| {
                std::sync::Arc::new(datafusion_expr::ScalarUDF::new_from_impl(
                    $CTOR(),
                ))
            });
            std::sync::Arc::clone(&INSTANCE)
        }
    };
}

To reduce duplication. For now just kept this minor duplication of the arms.

#[allow(rustdoc::redundant_explicit_links)]
#[doc = concat!("Return a [`ScalarUDF`](datafusion_expr::ScalarUDF) implementation of ", stringify!($NAME))]
pub fn $NAME() -> std::sync::Arc<datafusion_expr::ScalarUDF> {
// Singleton instance of the function
static INSTANCE: std::sync::LazyLock<
std::sync::Arc<datafusion_expr::ScalarUDF>,
> = std::sync::LazyLock::new(|| {
std::sync::Arc::new(datafusion_expr::ScalarUDF::new_from_impl(
$CTOR(),
))
});
std::sync::Arc::clone(&INSTANCE)
}
};
}

/// Creates a singleton `ScalarUDF` of the `$UDF` function and a function
Expand Down
Loading