Skip to content

Commit 6cb3d7e

Browse files
refactor: reduce duplication in make_udf_function macro (#18733)
## Which issue does this PR close? Closes #18669 ## Rationale for this change The `make_udf_function!` macro had significant code duplication between its two arms. Both arms contained nearly identical 16-line implementations that only differed in the constructor call. Additionally, the `$UDF` parameter was unused in the second arm, which was inefficient. This refactoring eliminates the duplication by applying a delegation pattern, where the simpler 2-parameter arm now delegates to the more complete 3-parameter arm with a default constructor. ## What changes are included in this PR? - **Reordered macro arms**: The 3-parameter arm (with `$CTOR`) is now first and contains the full implementation - **Delegation pattern**: The 2-parameter arm now delegates to the 3-parameter arm by calling `make_udf_function!($UDF, $NAME, <$UDF>::new)` - **Parameter type change**: Changed `$CTOR` from `:path` to `:expr` and wrap the call as `($CTOR)()` for flexibility - **Fixed unused parameter**: The `$UDF` type parameter is now utilized in the 2-parameter arm to construct the default constructor path - **Code reduction**: Reduced the macro from 33 lines to 25 lines by eliminating duplication ## Are these changes tested? Yes ## Are there any user-facing changes? No
1 parent ac9bb17 commit 6cb3d7e

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

datafusion/functions/src/macros.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ macro_rules! export_functions {
8383
/// This is used to ensure creating the list of `ScalarUDF` only happens once.
8484
#[macro_export]
8585
macro_rules! make_udf_function {
86-
($UDF:ty, $NAME:ident) => {
86+
($UDF:ty, $NAME:ident, $CTOR:expr) => {
8787
#[allow(rustdoc::redundant_explicit_links)]
8888
#[doc = concat!("Return a [`ScalarUDF`](datafusion_expr::ScalarUDF) implementation of ", stringify!($NAME))]
8989
pub fn $NAME() -> std::sync::Arc<datafusion_expr::ScalarUDF> {
@@ -92,26 +92,14 @@ macro_rules! make_udf_function {
9292
std::sync::Arc<datafusion_expr::ScalarUDF>,
9393
> = std::sync::LazyLock::new(|| {
9494
std::sync::Arc::new(datafusion_expr::ScalarUDF::new_from_impl(
95-
<$UDF>::new(),
95+
($CTOR)(),
9696
))
9797
});
9898
std::sync::Arc::clone(&INSTANCE)
9999
}
100100
};
101-
($UDF:ty, $NAME:ident, $CTOR:path) => {
102-
#[allow(rustdoc::redundant_explicit_links)]
103-
#[doc = concat!("Return a [`ScalarUDF`](datafusion_expr::ScalarUDF) implementation of ", stringify!($NAME))]
104-
pub fn $NAME() -> std::sync::Arc<datafusion_expr::ScalarUDF> {
105-
// Singleton instance of the function
106-
static INSTANCE: std::sync::LazyLock<
107-
std::sync::Arc<datafusion_expr::ScalarUDF>,
108-
> = std::sync::LazyLock::new(|| {
109-
std::sync::Arc::new(datafusion_expr::ScalarUDF::new_from_impl(
110-
$CTOR(),
111-
))
112-
});
113-
std::sync::Arc::clone(&INSTANCE)
114-
}
101+
($UDF:ty, $NAME:ident) => {
102+
make_udf_function!($UDF, $NAME, <$UDF>::new);
115103
};
116104
}
117105

0 commit comments

Comments
 (0)