Skip to content

Commit 0f5634e

Browse files
authored
refactor: simplify the make_udf_function macro (#13712)
1 parent 62fc4db commit 0f5634e

File tree

9 files changed

+101
-153
lines changed

9 files changed

+101
-153
lines changed

datafusion/functions/src/core/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ pub mod r#struct;
3535
pub mod version;
3636

3737
// create UDFs
38-
make_udf_function!(arrow_cast::ArrowCastFunc, ARROW_CAST, arrow_cast);
39-
make_udf_function!(nullif::NullIfFunc, NULLIF, nullif);
40-
make_udf_function!(nvl::NVLFunc, NVL, nvl);
41-
make_udf_function!(nvl2::NVL2Func, NVL2, nvl2);
42-
make_udf_function!(arrowtypeof::ArrowTypeOfFunc, ARROWTYPEOF, arrow_typeof);
43-
make_udf_function!(r#struct::StructFunc, STRUCT, r#struct);
44-
make_udf_function!(named_struct::NamedStructFunc, NAMED_STRUCT, named_struct);
45-
make_udf_function!(getfield::GetFieldFunc, GET_FIELD, get_field);
46-
make_udf_function!(coalesce::CoalesceFunc, COALESCE, coalesce);
47-
make_udf_function!(greatest::GreatestFunc, GREATEST, greatest);
48-
make_udf_function!(version::VersionFunc, VERSION, version);
38+
make_udf_function!(arrow_cast::ArrowCastFunc, arrow_cast);
39+
make_udf_function!(nullif::NullIfFunc, nullif);
40+
make_udf_function!(nvl::NVLFunc, nvl);
41+
make_udf_function!(nvl2::NVL2Func, nvl2);
42+
make_udf_function!(arrowtypeof::ArrowTypeOfFunc, arrow_typeof);
43+
make_udf_function!(r#struct::StructFunc, r#struct);
44+
make_udf_function!(named_struct::NamedStructFunc, named_struct);
45+
make_udf_function!(getfield::GetFieldFunc, get_field);
46+
make_udf_function!(coalesce::CoalesceFunc, coalesce);
47+
make_udf_function!(greatest::GreatestFunc, greatest);
48+
make_udf_function!(version::VersionFunc, version);
4949

5050
pub mod expr_fn {
5151
use datafusion_expr::{Expr, Literal};

datafusion/functions/src/crypto/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ pub mod sha224;
2727
pub mod sha256;
2828
pub mod sha384;
2929
pub mod sha512;
30-
make_udf_function!(digest::DigestFunc, DIGEST, digest);
31-
make_udf_function!(md5::Md5Func, MD5, md5);
32-
make_udf_function!(sha224::SHA224Func, SHA224, sha224);
33-
make_udf_function!(sha256::SHA256Func, SHA256, sha256);
34-
make_udf_function!(sha384::SHA384Func, SHA384, sha384);
35-
make_udf_function!(sha512::SHA512Func, SHA512, sha512);
30+
make_udf_function!(digest::DigestFunc, digest);
31+
make_udf_function!(md5::Md5Func, md5);
32+
make_udf_function!(sha224::SHA224Func, sha224);
33+
make_udf_function!(sha256::SHA256Func, sha256);
34+
make_udf_function!(sha384::SHA384Func, sha384);
35+
make_udf_function!(sha512::SHA512Func, sha512);
3636

3737
pub mod expr_fn {
3838
export_functions!((

datafusion/functions/src/datetime/mod.rs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,23 @@ pub mod to_timestamp;
3737
pub mod to_unixtime;
3838

3939
// create UDFs
40-
make_udf_function!(current_date::CurrentDateFunc, CURRENT_DATE, current_date);
41-
make_udf_function!(current_time::CurrentTimeFunc, CURRENT_TIME, current_time);
42-
make_udf_function!(date_bin::DateBinFunc, DATE_BIN, date_bin);
43-
make_udf_function!(date_part::DatePartFunc, DATE_PART, date_part);
44-
make_udf_function!(date_trunc::DateTruncFunc, DATE_TRUNC, date_trunc);
45-
make_udf_function!(make_date::MakeDateFunc, MAKE_DATE, make_date);
46-
make_udf_function!(
47-
from_unixtime::FromUnixtimeFunc,
48-
FROM_UNIXTIME,
49-
from_unixtime
50-
);
51-
make_udf_function!(now::NowFunc, NOW, now);
52-
make_udf_function!(to_char::ToCharFunc, TO_CHAR, to_char);
53-
make_udf_function!(to_date::ToDateFunc, TO_DATE, to_date);
54-
make_udf_function!(to_local_time::ToLocalTimeFunc, TO_LOCAL_TIME, to_local_time);
55-
make_udf_function!(to_unixtime::ToUnixtimeFunc, TO_UNIXTIME, to_unixtime);
56-
make_udf_function!(to_timestamp::ToTimestampFunc, TO_TIMESTAMP, to_timestamp);
57-
make_udf_function!(
58-
to_timestamp::ToTimestampSecondsFunc,
59-
TO_TIMESTAMP_SECONDS,
60-
to_timestamp_seconds
61-
);
62-
make_udf_function!(
63-
to_timestamp::ToTimestampMillisFunc,
64-
TO_TIMESTAMP_MILLIS,
65-
to_timestamp_millis
66-
);
67-
make_udf_function!(
68-
to_timestamp::ToTimestampMicrosFunc,
69-
TO_TIMESTAMP_MICROS,
70-
to_timestamp_micros
71-
);
72-
make_udf_function!(
73-
to_timestamp::ToTimestampNanosFunc,
74-
TO_TIMESTAMP_NANOS,
75-
to_timestamp_nanos
76-
);
40+
make_udf_function!(current_date::CurrentDateFunc, current_date);
41+
make_udf_function!(current_time::CurrentTimeFunc, current_time);
42+
make_udf_function!(date_bin::DateBinFunc, date_bin);
43+
make_udf_function!(date_part::DatePartFunc, date_part);
44+
make_udf_function!(date_trunc::DateTruncFunc, date_trunc);
45+
make_udf_function!(make_date::MakeDateFunc, make_date);
46+
make_udf_function!(from_unixtime::FromUnixtimeFunc, from_unixtime);
47+
make_udf_function!(now::NowFunc, now);
48+
make_udf_function!(to_char::ToCharFunc, to_char);
49+
make_udf_function!(to_date::ToDateFunc, to_date);
50+
make_udf_function!(to_local_time::ToLocalTimeFunc, to_local_time);
51+
make_udf_function!(to_unixtime::ToUnixtimeFunc, to_unixtime);
52+
make_udf_function!(to_timestamp::ToTimestampFunc, to_timestamp);
53+
make_udf_function!(to_timestamp::ToTimestampSecondsFunc, to_timestamp_seconds);
54+
make_udf_function!(to_timestamp::ToTimestampMillisFunc, to_timestamp_millis);
55+
make_udf_function!(to_timestamp::ToTimestampMicrosFunc, to_timestamp_micros);
56+
make_udf_function!(to_timestamp::ToTimestampNanosFunc, to_timestamp_nanos);
7757

7858
// we cannot currently use the export_functions macro since it doesn't handle
7959
// functions with varargs currently

datafusion/functions/src/encoding/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use std::sync::Arc;
2121
pub mod inner;
2222

2323
// create `encode` and `decode` UDFs
24-
make_udf_function!(inner::EncodeFunc, ENCODE, encode);
25-
make_udf_function!(inner::DecodeFunc, DECODE, decode);
24+
make_udf_function!(inner::EncodeFunc, encode);
25+
make_udf_function!(inner::DecodeFunc, decode);
2626

2727
// Export the functions out of this package, both as expr_fn as well as a list of functions
2828
pub mod expr_fn {

datafusion/functions/src/macros.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,23 @@ macro_rules! export_functions {
6565
};
6666
}
6767

68-
/// Creates a singleton `ScalarUDF` of the `$UDF` function named `$GNAME` and a
69-
/// function named `$NAME` which returns that singleton.
68+
/// Creates a singleton `ScalarUDF` of the `$UDF` function and a function
69+
/// named `$NAME` which returns that singleton.
7070
///
7171
/// This is used to ensure creating the list of `ScalarUDF` only happens once.
7272
macro_rules! make_udf_function {
73-
($UDF:ty, $GNAME:ident, $NAME:ident) => {
74-
#[doc = "Return a [`ScalarUDF`](datafusion_expr::ScalarUDF) implementation "]
75-
#[doc = stringify!($UDF)]
73+
($UDF:ty, $NAME:ident) => {
74+
#[doc = concat!("Return a [`ScalarUDF`](datafusion_expr::ScalarUDF) implementation of ", stringify!($NAME))]
7675
pub fn $NAME() -> std::sync::Arc<datafusion_expr::ScalarUDF> {
7776
// Singleton instance of the function
78-
static $GNAME: std::sync::LazyLock<
77+
static INSTANCE: std::sync::LazyLock<
7978
std::sync::Arc<datafusion_expr::ScalarUDF>,
8079
> = std::sync::LazyLock::new(|| {
8180
std::sync::Arc::new(datafusion_expr::ScalarUDF::new_from_impl(
8281
<$UDF>::new(),
8382
))
8483
});
85-
std::sync::Arc::clone(&$GNAME)
84+
std::sync::Arc::clone(&INSTANCE)
8685
}
8786
};
8887
}
@@ -134,13 +133,13 @@ macro_rules! downcast_arg {
134133
/// applies a unary floating function to the argument, and returns a value of the same type.
135134
///
136135
/// $UDF: the name of the UDF struct that implements `ScalarUDFImpl`
137-
/// $GNAME: a singleton instance of the UDF
138136
/// $NAME: the name of the function
139137
/// $UNARY_FUNC: the unary function to apply to the argument
140138
/// $OUTPUT_ORDERING: the output ordering calculation method of the function
139+
/// $GET_DOC: the function to get the documentation of the UDF
141140
macro_rules! make_math_unary_udf {
142-
($UDF:ident, $GNAME:ident, $NAME:ident, $UNARY_FUNC:ident, $OUTPUT_ORDERING:expr, $EVALUATE_BOUNDS:expr, $GET_DOC:expr) => {
143-
make_udf_function!($NAME::$UDF, $GNAME, $NAME);
141+
($UDF:ident, $NAME:ident, $UNARY_FUNC:ident, $OUTPUT_ORDERING:expr, $EVALUATE_BOUNDS:expr, $GET_DOC:expr) => {
142+
make_udf_function!($NAME::$UDF, $NAME);
144143

145144
mod $NAME {
146145
use std::any::Any;
@@ -248,13 +247,13 @@ macro_rules! make_math_unary_udf {
248247
/// applies a binary floating function to the argument, and returns a value of the same type.
249248
///
250249
/// $UDF: the name of the UDF struct that implements `ScalarUDFImpl`
251-
/// $GNAME: a singleton instance of the UDF
252250
/// $NAME: the name of the function
253251
/// $BINARY_FUNC: the binary function to apply to the argument
254252
/// $OUTPUT_ORDERING: the output ordering calculation method of the function
253+
/// $GET_DOC: the function to get the documentation of the UDF
255254
macro_rules! make_math_binary_udf {
256-
($UDF:ident, $GNAME:ident, $NAME:ident, $BINARY_FUNC:ident, $OUTPUT_ORDERING:expr, $GET_DOC:expr) => {
257-
make_udf_function!($NAME::$UDF, $GNAME, $NAME);
255+
($UDF:ident, $NAME:ident, $BINARY_FUNC:ident, $OUTPUT_ORDERING:expr, $GET_DOC:expr) => {
256+
make_udf_function!($NAME::$UDF, $NAME);
258257

259258
mod $NAME {
260259
use std::any::Any;

0 commit comments

Comments
 (0)