Apache DataFusion SQL Query Engine
refactor: simplify the `make_udf_function` macro (#13712)
Jonah Gao0f5634ec3de8
diff --git a/datafusion/functions/src/core/mod.rs b/datafusion/functions/src/core/mod.rsindex 24d26c5395..bd8305cd56 100644--- a/datafusion/functions/src/core/mod.rs+++ b/datafusion/functions/src/core/mod.rs@@ -35,17 +35,17 @@ pub mod r#struct;pub mod version;// create UDFs-make_udf_function!(arrow_cast::ArrowCastFunc, ARROW_CAST, arrow_cast);-make_udf_function!(nullif::NullIfFunc, NULLIF, nullif);-make_udf_function!(nvl::NVLFunc, NVL, nvl);-make_udf_function!(nvl2::NVL2Func, NVL2, nvl2);-make_udf_function!(arrowtypeof::ArrowTypeOfFunc, ARROWTYPEOF, arrow_typeof);-make_udf_function!(r#struct::StructFunc, STRUCT, r#struct);-make_udf_function!(named_struct::NamedStructFunc, NAMED_STRUCT, named_struct);-make_udf_function!(getfield::GetFieldFunc, GET_FIELD, get_field);-make_udf_function!(coalesce::CoalesceFunc, COALESCE, coalesce);-make_udf_function!(greatest::GreatestFunc, GREATEST, greatest);-make_udf_function!(version::VersionFunc, VERSION, version);+make_udf_function!(arrow_cast::ArrowCastFunc, arrow_cast);+make_udf_function!(nullif::NullIfFunc, nullif);+make_udf_function!(nvl::NVLFunc, nvl);+make_udf_function!(nvl2::NVL2Func, nvl2);+make_udf_function!(arrowtypeof::ArrowTypeOfFunc, arrow_typeof);+make_udf_function!(r#struct::StructFunc, r#struct);+make_udf_function!(named_struct::NamedStructFunc, named_struct);+make_udf_function!(getfield::GetFieldFunc, get_field);+make_udf_function!(coalesce::CoalesceFunc, coalesce);+make_udf_function!(greatest::GreatestFunc, greatest);+make_udf_function!(version::VersionFunc, version);pub mod expr_fn {use datafusion_expr::{Expr, Literal};diff --git a/datafusion/functions/src/crypto/mod.rs b/datafusion/functions/src/crypto/mod.rsindex 46177fc22b..62ea3c2e27 100644--- a/datafusion/functions/src/crypto/mod.rs+++ b/datafusion/functions/src/crypto/mod.rs@@ -27,12 +27,12 @@ pub mod sha224;pub mod sha256;pub mod sha384;pub mod sha512;-make_udf_function!(digest::DigestFunc, DIGEST, digest);-make_udf_function!(md5::Md5Func, MD5, md5);-make_udf_function!(sha224::SHA224Func, SHA224, sha224);-make_udf_function!(sha256::SHA256Func, SHA256, sha256);-make_udf_function!(sha384::SHA384Func, SHA384, sha384);-make_udf_function!(sha512::SHA512Func, SHA512, sha512);+make_udf_function!(digest::DigestFunc, digest);+make_udf_function!(md5::Md5Func, md5);+make_udf_function!(sha224::SHA224Func, sha224);+make_udf_function!(sha256::SHA256Func, sha256);+make_udf_function!(sha384::SHA384Func, sha384);+make_udf_function!(sha512::SHA512Func, sha512);pub mod expr_fn {export_functions!((diff --git a/datafusion/functions/src/datetime/mod.rs b/datafusion/functions/src/datetime/mod.rsindex db4e365267..96ca63010e 100644--- a/datafusion/functions/src/datetime/mod.rs+++ b/datafusion/functions/src/datetime/mod.rs@@ -37,43 +37,23 @@ pub mod to_timestamp;pub mod to_unixtime;// create UDFs-make_udf_function!(current_date::CurrentDateFunc, CURRENT_DATE, current_date);-make_udf_function!(current_time::CurrentTimeFunc, CURRENT_TIME, current_time);-make_udf_function!(date_bin::DateBinFunc, DATE_BIN, date_bin);-make_udf_function!(date_part::DatePartFunc, DATE_PART, date_part);-make_udf_function!(date_trunc::DateTruncFunc, DATE_TRUNC, date_trunc);-make_udf_function!(make_date::MakeDateFunc, MAKE_DATE, make_date);-make_udf_function!(- from_unixtime::FromUnixtimeFunc,- FROM_UNIXTIME,- from_unixtime-);-make_udf_function!(now::NowFunc, NOW, now);-make_udf_function!(to_char::ToCharFunc, TO_CHAR, to_char);-make_udf_function!(to_date::ToDateFunc, TO_DATE, to_date);-make_udf_function!(to_local_time::ToLocalTimeFunc, TO_LOCAL_TIME, to_local_time);-make_udf_function!(to_unixtime::ToUnixtimeFunc, TO_UNIXTIME, to_unixtime);-make_udf_function!(to_timestamp::ToTimestampFunc, TO_TIMESTAMP, to_timestamp);-make_udf_function!(- to_timestamp::ToTimestampSecondsFunc,- TO_TIMESTAMP_SECONDS,- to_timestamp_seconds-);-make_udf_function!(- to_timestamp::ToTimestampMillisFunc,- TO_TIMESTAMP_MILLIS,- to_timestamp_millis-);-make_udf_function!(- to_timestamp::ToTimestampMicrosFunc,- TO_TIMESTAMP_MICROS,- to_timestamp_micros-);-make_udf_function!(- to_timestamp::ToTimestampNanosFunc,- TO_TIMESTAMP_NANOS,- to_timestamp_nanos-);+make_udf_function!(current_date::CurrentDateFunc, current_date);+make_udf_function!(current_time::CurrentTimeFunc, current_time);+make_udf_function!(date_bin::DateBinFunc, date_bin);+make_udf_function!(date_part::DatePartFunc, date_part);+make_udf_function!(date_trunc::DateTruncFunc, date_trunc);+make_udf_function!(make_date::MakeDateFunc, make_date);+make_udf_function!(from_unixtime::FromUnixtimeFunc, from_unixtime);+make_udf_function!(now::NowFunc, now);+make_udf_function!(to_char::ToCharFunc, to_char);+make_udf_function!(to_date::ToDateFunc, to_date);+make_udf_function!(to_local_time::ToLocalTimeFunc, to_local_time);+make_udf_function!(to_unixtime::ToUnixtimeFunc, to_unixtime);+make_udf_function!(to_timestamp::ToTimestampFunc, to_timestamp);+make_udf_function!(to_timestamp::ToTimestampSecondsFunc, to_timestamp_seconds);+make_udf_function!(to_timestamp::ToTimestampMillisFunc, to_timestamp_millis);+make_udf_function!(to_timestamp::ToTimestampMicrosFunc, to_timestamp_micros);+make_udf_function!(to_timestamp::ToTimestampNanosFunc, to_timestamp_nanos);// we cannot currently use the export_functions macro since it doesn't handle// functions with varargs currentlydiff --git a/datafusion/functions/src/encoding/mod.rs b/datafusion/functions/src/encoding/mod.rsindex 48171370ad..b0ddbd368a 100644--- a/datafusion/functions/src/encoding/mod.rs+++ b/datafusion/functions/src/encoding/mod.rs@@ -21,8 +21,8 @@ use std::sync::Arc;pub mod inner;// create `encode` and `decode` UDFs-make_udf_function!(inner::EncodeFunc, ENCODE, encode);-make_udf_function!(inner::DecodeFunc, DECODE, decode);+make_udf_function!(inner::EncodeFunc, encode);+make_udf_function!(inner::DecodeFunc, decode);// Export the functions out of this package, both as expr_fn as well as a list of functionspub mod expr_fn {diff --git a/datafusion/functions/src/macros.rs b/datafusion/functions/src/macros.rsindex bedec9bb2e..8230860149 100644--- a/datafusion/functions/src/macros.rs+++ b/datafusion/functions/src/macros.rs@@ -65,24 +65,23 @@ macro_rules! export_functions {};}-/// Creates a singleton `ScalarUDF` of the `$UDF` function named `$GNAME` and a-/// function named `$NAME` which returns that singleton.+/// Creates a singleton `ScalarUDF` of the `$UDF` function and a function+/// named `$NAME` which returns that singleton.////// This is used to ensure creating the list of `ScalarUDF` only happens once.macro_rules! make_udf_function {- ($UDF:ty, $GNAME:ident, $NAME:ident) => {- #[doc = "Return a [`ScalarUDF`](datafusion_expr::ScalarUDF) implementation "]- #[doc = stringify!($UDF)]+ ($UDF:ty, $NAME:ident) => {+ #[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 $GNAME: std::sync::LazyLock<+ 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(<$UDF>::new(),))});- std::sync::Arc::clone(&$GNAME)+ std::sync::Arc::clone(&INSTANCE)}};}@@ -134,13 +133,13 @@ macro_rules! downcast_arg {/// applies a unary floating function to the argument, and returns a value of the same type.////// $UDF: the name of the UDF struct that implements `ScalarUDFImpl`-/// $GNAME: a singleton instance of the UDF/// $NAME: the name of the function/// $UNARY_FUNC: the unary function to apply to the argument/// $OUTPUT_ORDERING: the output ordering calculation method of the function+/// $GET_DOC: the function to get the documentation of the UDFmacro_rules! make_math_unary_udf {- ($UDF:ident, $GNAME:ident, $NAME:ident, $UNARY_FUNC:ident, $OUTPUT_ORDERING:expr, $EVALUATE_BOUNDS:expr, $GET_DOC:expr) => {- make_udf_function!($NAME::$UDF, $GNAME, $NAME);+ ($UDF:ident, $NAME:ident, $UNARY_FUNC:ident, $OUTPUT_ORDERING:expr, $EVALUATE_BOUNDS:expr, $GET_DOC:expr) => {+ make_udf_function!($NAME::$UDF, $NAME);mod $NAME {use std::any::Any;@@ -248,13 +247,13 @@ macro_rules! make_math_unary_udf {/// applies a binary floating function to the argument, and returns a value of the same type.////// $UDF: the name of the UDF struct that implements `ScalarUDFImpl`-/// $GNAME: a singleton instance of the UDF/// $NAME: the name of the function/// $BINARY_FUNC: the binary function to apply to the argument/// $OUTPUT_ORDERING: the output ordering calculation method of the function+/// $GET_DOC: the function to get the documentation of the UDFmacro_rules! make_math_binary_udf {- ($UDF:ident, $GNAME:ident, $NAME:ident, $BINARY_FUNC:ident, $OUTPUT_ORDERING:expr, $GET_DOC:expr) => {- make_udf_function!($NAME::$UDF, $GNAME, $NAME);+ ($UDF:ident, $NAME:ident, $BINARY_FUNC:ident, $OUTPUT_ORDERING:expr, $GET_DOC:expr) => {+ make_udf_function!($NAME::$UDF, $NAME);mod $NAME {use std::any::Any;diff --git a/datafusion/functions/src/math/mod.rs b/datafusion/functions/src/math/mod.rsindex 1452bfdee5..4eb337a301 100644--- a/datafusion/functions/src/math/mod.rs+++ b/datafusion/functions/src/math/mod.rs@@ -40,10 +40,9 @@ pub mod signum;pub mod trunc;// Create UDFs-make_udf_function!(abs::AbsFunc, ABS, abs);+make_udf_function!(abs::AbsFunc, abs);make_math_unary_udf!(AcosFunc,- ACOS,acos,acos,super::acos_order,@@ -52,7 +51,6 @@ make_math_unary_udf!();make_math_unary_udf!(AcoshFunc,- ACOSH,acosh,acosh,super::acosh_order,@@ -61,7 +59,6 @@ make_math_unary_udf!();make_math_unary_udf!(AsinFunc,- ASIN,asin,asin,super::asin_order,@@ -70,7 +67,6 @@ make_math_unary_udf!();make_math_unary_udf!(AsinhFunc,- ASINH,asinh,asinh,super::asinh_order,@@ -79,7 +75,6 @@ make_math_unary_udf!();make_math_unary_udf!(AtanFunc,- ATAN,atan,atan,super::atan_order,@@ -88,7 +83,6 @@ make_math_unary_udf!();make_math_unary_udf!(AtanhFunc,- ATANH,atanh,atanh,super::atanh_order,@@ -97,7 +91,6 @@ make_math_unary_udf!();make_math_binary_udf!(Atan2,- ATAN2,atan2,atan2,super::atan2_order,@@ -105,7 +98,6 @@ make_math_binary_udf!();make_math_unary_udf!(CbrtFunc,- CBRT,cbrt,cbrt,super::cbrt_order,@@ -114,7 +106,6 @@ make_math_unary_udf!();make_math_unary_udf!(CeilFunc,- CEIL,ceil,ceil,super::ceil_order,@@ -123,7 +114,6 @@ make_math_unary_udf!();make_math_unary_udf!(CosFunc,- COS,cos,cos,super::cos_order,@@ -132,17 +122,15 @@ make_math_unary_udf!();make_math_unary_udf!(CoshFunc,- COSH,cosh,cosh,super::cosh_order,super::bounds::cosh_bounds,super::get_cosh_doc);-make_udf_function!(cot::CotFunc, COT, cot);+make_udf_function!(cot::CotFunc, cot);make_math_unary_udf!(DegreesFunc,- DEGREES,degrees,to_degrees,super::degrees_order,@@ -151,31 +139,28 @@ make_math_unary_udf!();make_math_unary_udf!(ExpFunc,- EXP,exp,exp,super::exp_order,super::bounds::exp_bounds,super::get_exp_doc);-make_udf_function!(factorial::FactorialFunc, FACTORIAL, factorial);+make_udf_function!(factorial::FactorialFunc, factorial);make_math_unary_udf!(FloorFunc,- FLOOR,floor,floor,super::floor_order,super::bounds::unbounded_bounds,super::get_floor_doc);-make_udf_function!(log::LogFunc, LOG, log);-make_udf_function!(gcd::GcdFunc, GCD, gcd);-make_udf_function!(nans::IsNanFunc, ISNAN, isnan);-make_udf_function!(iszero::IsZeroFunc, ISZERO, iszero);-make_udf_function!(lcm::LcmFunc, LCM, lcm);+make_udf_function!(log::LogFunc, log);+make_udf_function!(gcd::GcdFunc, gcd);+make_udf_function!(nans::IsNanFunc, isnan);+make_udf_function!(iszero::IsZeroFunc, iszero);+make_udf_function!(lcm::LcmFunc, lcm);make_math_unary_udf!(LnFunc,- LN,ln,ln,super::ln_order,@@ -184,7 +169,6 @@ make_math_unary_udf!();make_math_unary_udf!(Log2Func,- LOG2,log2,log2,super::log2_order,@@ -193,31 +177,28 @@ make_math_unary_udf!();make_math_unary_udf!(Log10Func,- LOG10,log10,log10,super::log10_order,super::bounds::unbounded_bounds,super::get_log10_doc);-make_udf_function!(nanvl::NanvlFunc, NANVL, nanvl);-make_udf_function!(pi::PiFunc, PI, pi);-make_udf_function!(power::PowerFunc, POWER, power);+make_udf_function!(nanvl::NanvlFunc, nanvl);+make_udf_function!(pi::PiFunc, pi);+make_udf_function!(power::PowerFunc, power);make_math_unary_udf!(RadiansFunc,- RADIANS,radians,to_radians,super::radians_order,super::bounds::radians_bounds,super::get_radians_doc);-make_udf_function!(random::RandomFunc, RANDOM, random);-make_udf_function!(round::RoundFunc, ROUND, round);-make_udf_function!(signum::SignumFunc, SIGNUM, signum);+make_udf_function!(random::RandomFunc, random);+make_udf_function!(round::RoundFunc, round);+make_udf_function!(signum::SignumFunc, signum);make_math_unary_udf!(SinFunc,- SIN,sin,sin,super::sin_order,@@ -226,7 +207,6 @@ make_math_unary_udf!();make_math_unary_udf!(SinhFunc,- SINH,sinh,sinh,super::sinh_order,@@ -235,7 +215,6 @@ make_math_unary_udf!();make_math_unary_udf!(SqrtFunc,- SQRT,sqrt,sqrt,super::sqrt_order,@@ -244,7 +223,6 @@ make_math_unary_udf!();make_math_unary_udf!(TanFunc,- TAN,tan,tan,super::tan_order,@@ -253,14 +231,13 @@ make_math_unary_udf!();make_math_unary_udf!(TanhFunc,- TANH,tanh,tanh,super::tanh_order,super::bounds::tanh_bounds,super::get_tanh_doc);-make_udf_function!(trunc::TruncFunc, TRUNC, trunc);+make_udf_function!(trunc::TruncFunc, trunc);pub mod expr_fn {export_functions!(diff --git a/datafusion/functions/src/regex/mod.rs b/datafusion/functions/src/regex/mod.rsindex 803f51e915..13fbc049af 100644--- a/datafusion/functions/src/regex/mod.rs+++ b/datafusion/functions/src/regex/mod.rs@@ -25,14 +25,10 @@ pub mod regexpmatch;pub mod regexpreplace;// create UDFs-make_udf_function!(regexpcount::RegexpCountFunc, REGEXP_COUNT, regexp_count);-make_udf_function!(regexpmatch::RegexpMatchFunc, REGEXP_MATCH, regexp_match);-make_udf_function!(regexplike::RegexpLikeFunc, REGEXP_LIKE, regexp_like);-make_udf_function!(- regexpreplace::RegexpReplaceFunc,- REGEXP_REPLACE,- regexp_replace-);+make_udf_function!(regexpcount::RegexpCountFunc, regexp_count);+make_udf_function!(regexpmatch::RegexpMatchFunc, regexp_match);+make_udf_function!(regexplike::RegexpLikeFunc, regexp_like);+make_udf_function!(regexpreplace::RegexpReplaceFunc, regexp_replace);pub mod expr_fn {use datafusion_expr::Expr;diff --git a/datafusion/functions/src/string/mod.rs b/datafusion/functions/src/string/mod.rsindex 622802f014..f156f070d9 100644--- a/datafusion/functions/src/string/mod.rs+++ b/datafusion/functions/src/string/mod.rs@@ -45,28 +45,28 @@ pub mod to_hex;pub mod upper;pub mod uuid;// create UDFs-make_udf_function!(ascii::AsciiFunc, ASCII, ascii);-make_udf_function!(bit_length::BitLengthFunc, BIT_LENGTH, bit_length);-make_udf_function!(btrim::BTrimFunc, BTRIM, btrim);-make_udf_function!(chr::ChrFunc, CHR, chr);-make_udf_function!(concat::ConcatFunc, CONCAT, concat);-make_udf_function!(concat_ws::ConcatWsFunc, CONCAT_WS, concat_ws);-make_udf_function!(ends_with::EndsWithFunc, ENDS_WITH, ends_with);-make_udf_function!(initcap::InitcapFunc, INITCAP, initcap);-make_udf_function!(levenshtein::LevenshteinFunc, LEVENSHTEIN, levenshtein);-make_udf_function!(ltrim::LtrimFunc, LTRIM, ltrim);-make_udf_function!(lower::LowerFunc, LOWER, lower);-make_udf_function!(octet_length::OctetLengthFunc, OCTET_LENGTH, octet_length);-make_udf_function!(overlay::OverlayFunc, OVERLAY, overlay);-make_udf_function!(repeat::RepeatFunc, REPEAT, repeat);-make_udf_function!(replace::ReplaceFunc, REPLACE, replace);-make_udf_function!(rtrim::RtrimFunc, RTRIM, rtrim);-make_udf_function!(starts_with::StartsWithFunc, STARTS_WITH, starts_with);-make_udf_function!(split_part::SplitPartFunc, SPLIT_PART, split_part);-make_udf_function!(to_hex::ToHexFunc, TO_HEX, to_hex);-make_udf_function!(upper::UpperFunc, UPPER, upper);-make_udf_function!(uuid::UuidFunc, UUID, uuid);-make_udf_function!(contains::ContainsFunc, CONTAINS, contains);+make_udf_function!(ascii::AsciiFunc, ascii);+make_udf_function!(bit_length::BitLengthFunc, bit_length);+make_udf_function!(btrim::BTrimFunc, btrim);+make_udf_function!(chr::ChrFunc, chr);+make_udf_function!(concat::ConcatFunc, concat);+make_udf_function!(concat_ws::ConcatWsFunc, concat_ws);+make_udf_function!(ends_with::EndsWithFunc, ends_with);+make_udf_function!(initcap::InitcapFunc, initcap);+make_udf_function!(levenshtein::LevenshteinFunc, levenshtein);+make_udf_function!(ltrim::LtrimFunc, ltrim);+make_udf_function!(lower::LowerFunc, lower);+make_udf_function!(octet_length::OctetLengthFunc, octet_length);+make_udf_function!(overlay::OverlayFunc, overlay);+make_udf_function!(repeat::RepeatFunc, repeat);+make_udf_function!(replace::ReplaceFunc, replace);+make_udf_function!(rtrim::RtrimFunc, rtrim);+make_udf_function!(starts_with::StartsWithFunc, starts_with);+make_udf_function!(split_part::SplitPartFunc, split_part);+make_udf_function!(to_hex::ToHexFunc, to_hex);+make_udf_function!(upper::UpperFunc, upper);+make_udf_function!(uuid::UuidFunc, uuid);+make_udf_function!(contains::ContainsFunc, contains);pub mod expr_fn {use datafusion_expr::Expr;diff --git a/datafusion/functions/src/unicode/mod.rs b/datafusion/functions/src/unicode/mod.rsindex 40915bc9ef..f31ece9196 100644--- a/datafusion/functions/src/unicode/mod.rs+++ b/datafusion/functions/src/unicode/mod.rs@@ -34,22 +34,18 @@ pub mod substrindex;pub mod translate;// create UDFs-make_udf_function!(- character_length::CharacterLengthFunc,- CHARACTER_LENGTH,- character_length-);-make_udf_function!(find_in_set::FindInSetFunc, FIND_IN_SET, find_in_set);-make_udf_function!(left::LeftFunc, LEFT, left);-make_udf_function!(lpad::LPadFunc, LPAD, lpad);-make_udf_function!(right::RightFunc, RIGHT, right);-make_udf_function!(reverse::ReverseFunc, REVERSE, reverse);-make_udf_function!(rpad::RPadFunc, RPAD, rpad);-make_udf_function!(strpos::StrposFunc, STRPOS, strpos);-make_udf_function!(substr::SubstrFunc, SUBSTR, substr);-make_udf_function!(substr::SubstrFunc, SUBSTRING, substring);-make_udf_function!(substrindex::SubstrIndexFunc, SUBSTR_INDEX, substr_index);-make_udf_function!(translate::TranslateFunc, TRANSLATE, translate);+make_udf_function!(character_length::CharacterLengthFunc, character_length);+make_udf_function!(find_in_set::FindInSetFunc, find_in_set);+make_udf_function!(left::LeftFunc, left);+make_udf_function!(lpad::LPadFunc, lpad);+make_udf_function!(right::RightFunc, right);+make_udf_function!(reverse::ReverseFunc, reverse);+make_udf_function!(rpad::RPadFunc, rpad);+make_udf_function!(strpos::StrposFunc, strpos);+make_udf_function!(substr::SubstrFunc, substr);+make_udf_function!(substr::SubstrFunc, substring);+make_udf_function!(substrindex::SubstrIndexFunc, substr_index);+make_udf_function!(translate::TranslateFunc, translate);pub mod expr_fn {use datafusion_expr::Expr;