Skip to content

Commit da55f93

Browse files
authored
Clean the expr_fn - use scalar_expr to create unary scalar expr functions, remove macro unary_scalar_functions (#4357)
* update scalar_expr Signed-off-by: remzi <13716567376yh@gmail.com> * delete unary macro Signed-off-by: remzi <13716567376yh@gmail.com> * update nary macro Signed-off-by: remzi <13716567376yh@gmail.com> * clean Signed-off-by: remzi <13716567376yh@gmail.com> * fmt Signed-off-by: remzi <13716567376yh@gmail.com> Signed-off-by: remzi <13716567376yh@gmail.com>
1 parent be1d376 commit da55f93

1 file changed

Lines changed: 164 additions & 133 deletions

File tree

datafusion/expr/src/expr_fn.rs

Lines changed: 164 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -305,22 +305,9 @@ pub fn is_not_unknown(expr: Expr) -> Expr {
305305
Expr::IsNotUnknown(Box::new(expr))
306306
}
307307

308-
/// Create an convenience function representing a unary scalar function
309-
macro_rules! unary_scalar_expr {
310-
($ENUM:ident, $FUNC:ident, $DOC:expr) => {
311-
#[doc = $DOC ]
312-
pub fn $FUNC(e: Expr) -> Expr {
313-
Expr::ScalarFunction {
314-
fun: built_in_function::BuiltinScalarFunction::$ENUM,
315-
args: vec![e],
316-
}
317-
}
318-
};
319-
}
320-
321308
macro_rules! scalar_expr {
322-
($ENUM:ident, $FUNC:ident, $($arg:ident),*) => {
323-
#[doc = concat!("Scalar function definition for ", stringify!($FUNC) ) ]
309+
($ENUM:ident, $FUNC:ident, $($arg:ident)*, $DOC:expr) => {
310+
#[doc = $DOC ]
324311
pub fn $FUNC($($arg: Expr),*) -> Expr {
325312
Expr::ScalarFunction {
326313
fun: built_in_function::BuiltinScalarFunction::$ENUM,
@@ -331,8 +318,8 @@ macro_rules! scalar_expr {
331318
}
332319

333320
macro_rules! nary_scalar_expr {
334-
($ENUM:ident, $FUNC:ident) => {
335-
#[doc = concat!("Scalar function definition for ", stringify!($FUNC) ) ]
321+
($ENUM:ident, $FUNC:ident, $DOC:expr) => {
322+
#[doc = $DOC ]
336323
pub fn $FUNC(args: Vec<Expr>) -> Expr {
337324
Expr::ScalarFunction {
338325
fun: built_in_function::BuiltinScalarFunction::$ENUM,
@@ -345,136 +332,181 @@ macro_rules! nary_scalar_expr {
345332
// generate methods for creating the supported unary/binary expressions
346333

347334
// math functions
348-
unary_scalar_expr!(Sqrt, sqrt, "square root of a number");
349-
unary_scalar_expr!(Sin, sin, "sine");
350-
unary_scalar_expr!(Cos, cos, "cosine");
351-
unary_scalar_expr!(Tan, tan, "tangent");
352-
unary_scalar_expr!(Asin, asin, "inverse sine");
353-
unary_scalar_expr!(Acos, acos, "inverse cosine");
354-
unary_scalar_expr!(Atan, atan, "inverse tangent");
355-
unary_scalar_expr!(
335+
scalar_expr!(Sqrt, sqrt, num, "square root of a number");
336+
scalar_expr!(Sin, sin, num, "sine");
337+
scalar_expr!(Cos, cos, num, "cosine");
338+
scalar_expr!(Tan, tan, num, "tangent");
339+
scalar_expr!(Asin, asin, num, "inverse sine");
340+
scalar_expr!(Acos, acos, num, "inverse cosine");
341+
scalar_expr!(Atan, atan, num, "inverse tangent");
342+
scalar_expr!(
356343
Floor,
357344
floor,
345+
num,
358346
"nearest integer less than or equal to argument"
359347
);
360-
unary_scalar_expr!(
348+
scalar_expr!(
361349
Ceil,
362350
ceil,
351+
num,
363352
"nearest integer greater than or equal to argument"
364353
);
365-
unary_scalar_expr!(Round, round, "round to nearest integer");
366-
unary_scalar_expr!(Trunc, trunc, "truncate toward zero");
367-
unary_scalar_expr!(Abs, abs, "absolute value");
368-
unary_scalar_expr!(Signum, signum, "sign of the argument (-1, 0, +1) ");
369-
unary_scalar_expr!(Exp, exp, "exponential");
370-
unary_scalar_expr!(Log2, log2, "base 2 logarithm");
371-
unary_scalar_expr!(Log10, log10, "base 10 logarithm");
372-
unary_scalar_expr!(Ln, ln, "natural logarithm");
373-
scalar_expr!(NullIf, nullif, arg_1, arg_2);
374-
scalar_expr!(Power, power, base, exponent);
375-
scalar_expr!(Atan2, atan2, y, x);
354+
scalar_expr!(Round, round, num, "round to nearest integer");
355+
scalar_expr!(Trunc, trunc, num, "truncate toward zero");
356+
scalar_expr!(Abs, abs, num, "absolute value");
357+
scalar_expr!(Signum, signum, num, "sign of the argument (-1, 0, +1) ");
358+
scalar_expr!(Exp, exp, num, "exponential");
359+
scalar_expr!(Log2, log2, num, "base 2 logarithm");
360+
scalar_expr!(Log10, log10, num, "base 10 logarithm");
361+
scalar_expr!(Ln, ln, num, "natural logarithm");
362+
scalar_expr!(NullIf, nullif, arg_1 arg_2, "returns NULL if value1 equals value2; otherwise it returns value1. This can be used to perform the inverse operation of the COALESCE expression.");
363+
scalar_expr!(Power, power, base exponent, "`base` raised to the power of `exponent`");
364+
scalar_expr!(Atan2, atan2, y x, "inverse tangent of a division given in the argument");
365+
scalar_expr!(
366+
ToHex,
367+
to_hex,
368+
num,
369+
"returns the hexdecimal representation of an integer"
370+
);
371+
scalar_expr!(Uuid, uuid, , "Returns uuid v4 as a string value");
376372

377373
// string functions
378-
scalar_expr!(Ascii, ascii, string);
379-
scalar_expr!(BitLength, bit_length, string);
380-
scalar_expr!(CharacterLength, character_length, string);
381-
scalar_expr!(CharacterLength, length, string);
382-
scalar_expr!(Chr, chr, string);
383-
scalar_expr!(Digest, digest, input, algorithm);
384-
scalar_expr!(InitCap, initcap, string);
385-
scalar_expr!(Left, left, string, count);
386-
scalar_expr!(Lower, lower, string);
387-
scalar_expr!(Ltrim, ltrim, string);
388-
scalar_expr!(MD5, md5, string);
389-
scalar_expr!(OctetLength, octet_length, string);
390-
scalar_expr!(Replace, replace, string, from, to);
391-
scalar_expr!(Repeat, repeat, string, count);
392-
scalar_expr!(Reverse, reverse, string);
393-
scalar_expr!(Right, right, string, count);
394-
scalar_expr!(Rtrim, rtrim, string);
395-
scalar_expr!(SHA224, sha224, string);
396-
scalar_expr!(SHA256, sha256, string);
397-
scalar_expr!(SHA384, sha384, string);
398-
scalar_expr!(SHA512, sha512, string);
399-
scalar_expr!(SplitPart, split_part, expr, delimiter, index);
400-
scalar_expr!(StartsWith, starts_with, string, characters);
401-
scalar_expr!(Strpos, strpos, string, substring);
402-
scalar_expr!(Substr, substr, string, position);
403-
scalar_expr!(Substr, substring, string, position, count);
404-
scalar_expr!(ToHex, to_hex, string);
405-
scalar_expr!(Translate, translate, string, from, to);
406-
scalar_expr!(Trim, trim, string);
407-
scalar_expr!(Upper, upper, string);
374+
scalar_expr!(Ascii, ascii, chr, "ASCII code value of the character");
375+
scalar_expr!(
376+
BitLength,
377+
bit_length,
378+
string,
379+
"the number of bits in the `string`"
380+
);
381+
scalar_expr!(
382+
CharacterLength,
383+
character_length,
384+
string,
385+
"the number of characters in the `string`"
386+
);
387+
scalar_expr!(
388+
Chr,
389+
chr,
390+
code_point,
391+
"converts the Unicode code point to a UTF8 character"
392+
);
393+
scalar_expr!(Digest, digest, input algorithm, "compute the binary hash of `input`, using the `algorithm`");
394+
scalar_expr!(InitCap, initcap, string, "converts the first letter of each word in `string` in uppercase and the remaining characters in lowercase");
395+
scalar_expr!(Left, left, string n, "returns the first `n` characters in the `string`");
396+
scalar_expr!(Lower, lower, string, "convert the string to lower case");
397+
scalar_expr!(
398+
Ltrim,
399+
ltrim,
400+
string,
401+
"removes all characters, spaces by default, from the beginning of a string"
402+
);
403+
scalar_expr!(MD5, md5, string, "returns the MD5 hash of a string");
404+
scalar_expr!(
405+
OctetLength,
406+
octet_length,
407+
string,
408+
"returns the number of bytes of a string"
409+
);
410+
scalar_expr!(Replace, replace, string from to, "replaces all occurrences of `from` with `to` in the `string`");
411+
scalar_expr!(Repeat, repeat, string n, "repeats the `string` to `n` times");
412+
scalar_expr!(Reverse, reverse, string, "reverses the `string`");
413+
scalar_expr!(Right, right, string n, "returns the last `n` characters in the `string`");
414+
scalar_expr!(
415+
Rtrim,
416+
rtrim,
417+
string,
418+
"removes all characters, spaces by default, from the end of a string"
419+
);
420+
scalar_expr!(SHA224, sha224, string, "SHA-224 hash");
421+
scalar_expr!(SHA256, sha256, string, "SHA-256 hash");
422+
scalar_expr!(SHA384, sha384, string, "SHA-384 hash");
423+
scalar_expr!(SHA512, sha512, string, "SHA-512 hash");
424+
scalar_expr!(SplitPart, split_part, string delimiter index, "splits a string based on a delimiter and picks out the desired field based on the index. ");
425+
scalar_expr!(StartsWith, starts_with, string prefix, "whether the `string` starts with the `prefix`");
426+
scalar_expr!(Strpos, strpos, string substring, "finds the position from where the `substring` matchs the `string`");
427+
scalar_expr!(Substr, substr, string position, "substring from the `position` to the end");
428+
scalar_expr!(Substr, substring, string position length, "substring from the `position` with `length` characters");
429+
scalar_expr!(Translate, translate, string from to, "replaces the characters in `from` with the counterpart in `to`");
430+
scalar_expr!(
431+
Trim,
432+
trim,
433+
string,
434+
"removes all characters, space by default from the string"
435+
);
436+
scalar_expr!(Upper, upper, string, "converts the string to upper case");
408437
//use vec as parameter
409-
nary_scalar_expr!(Lpad, lpad);
410-
nary_scalar_expr!(Rpad, rpad);
411-
nary_scalar_expr!(RegexpReplace, regexp_replace);
412-
nary_scalar_expr!(RegexpMatch, regexp_match);
413-
nary_scalar_expr!(Btrim, btrim);
438+
nary_scalar_expr!(
439+
Lpad,
440+
lpad,
441+
"fill up a string to the length by prepending the characters"
442+
);
443+
nary_scalar_expr!(
444+
Rpad,
445+
rpad,
446+
"fill up a string to the length by appending the characters"
447+
);
448+
nary_scalar_expr!(
449+
RegexpReplace,
450+
regexp_replace,
451+
"replace strings that match a regular expression"
452+
);
453+
nary_scalar_expr!(
454+
RegexpMatch,
455+
regexp_match,
456+
"matches a regular expression against a string and returns matched substrings."
457+
);
458+
nary_scalar_expr!(
459+
Btrim,
460+
btrim,
461+
"removes all characters, spaces by default, from both sides of a string"
462+
);
463+
nary_scalar_expr!(
464+
MakeArray,
465+
array,
466+
"returns an array of fixed size with each argument on it."
467+
);
468+
nary_scalar_expr!(Coalesce, coalesce, "returns `coalesce(args...)`, which evaluates to the value of the first [Expr] which is not NULL");
414469
//there is a func concat_ws before, so use concat_ws_expr as name.c
415-
nary_scalar_expr!(ConcatWithSeparator, concat_ws_expr);
416-
nary_scalar_expr!(Concat, concat_expr);
470+
nary_scalar_expr!(
471+
ConcatWithSeparator,
472+
concat_ws_expr,
473+
"concatenates several strings, placing a seperator between each one"
474+
);
475+
nary_scalar_expr!(Concat, concat_expr, "concatenates several strings");
417476

418477
// date functions
419-
scalar_expr!(DatePart, date_part, part, date);
420-
scalar_expr!(DateTrunc, date_trunc, part, date);
421-
scalar_expr!(DateBin, date_bin, stride, source, origin);
422-
scalar_expr!(ToTimestampMillis, to_timestamp_millis, date);
423-
scalar_expr!(ToTimestampMicros, to_timestamp_micros, date);
424-
scalar_expr!(ToTimestampSeconds, to_timestamp_seconds, date);
425-
scalar_expr!(FromUnixtime, from_unixtime, unixtime);
426-
427-
unary_scalar_expr!(ArrowTypeof, arrow_typeof, "data type");
428-
429-
/// Returns an array of fixed size with each argument on it.
430-
pub fn array(args: Vec<Expr>) -> Expr {
431-
Expr::ScalarFunction {
432-
fun: built_in_function::BuiltinScalarFunction::MakeArray,
433-
args,
434-
}
435-
}
436-
437-
/// Returns `coalesce(args...)`, which evaluates to the value of the first [Expr]
438-
/// which is not NULL
439-
pub fn coalesce(args: Vec<Expr>) -> Expr {
440-
Expr::ScalarFunction {
441-
fun: BuiltinScalarFunction::Coalesce,
442-
args,
443-
}
444-
}
445-
446-
/// Returns current timestamp in nanoseconds, using the same value for all instances of now() in
447-
/// same statement.
448-
pub fn now() -> Expr {
449-
Expr::ScalarFunction {
450-
fun: BuiltinScalarFunction::Now,
451-
args: vec![],
452-
}
453-
}
454-
455-
/// Returns current UTC date as a [`DataType::Date32`] value
456-
pub fn current_date() -> Expr {
457-
Expr::ScalarFunction {
458-
fun: BuiltinScalarFunction::CurrentDate,
459-
args: vec![],
460-
}
461-
}
462-
463-
/// Returns uuid v4 as a string value
464-
pub fn uuid() -> Expr {
465-
Expr::ScalarFunction {
466-
fun: BuiltinScalarFunction::Uuid,
467-
args: vec![],
468-
}
469-
}
478+
scalar_expr!(DatePart, date_part, part date, "extracts a subfield from the date");
479+
scalar_expr!(DateTrunc, date_trunc, part date, "truncates the date to a specified level of precision");
480+
scalar_expr!(DateBin, date_bin, stride source origin, "coerces an arbitrary timestamp to the start of the nearest specified interval");
481+
scalar_expr!(
482+
ToTimestampMillis,
483+
to_timestamp_millis,
484+
date,
485+
"converts a string to a `Timestamp(Milliseconds, None)`"
486+
);
487+
scalar_expr!(
488+
ToTimestampMicros,
489+
to_timestamp_micros,
490+
date,
491+
"converts a string to a `Timestamp(Microseconds, None)`"
492+
);
493+
scalar_expr!(
494+
ToTimestampSeconds,
495+
to_timestamp_seconds,
496+
date,
497+
"converts a string to a `Timestamp(Seconds, None)`"
498+
);
499+
scalar_expr!(
500+
FromUnixtime,
501+
from_unixtime,
502+
unixtime,
503+
"returns the unix time in format"
504+
);
505+
scalar_expr!(CurrentDate, current_date, ,"returns current UTC date as a [`DataType::Date32`] value");
506+
scalar_expr!(Now, now, ,"returns current timestamp in nanoseconds, using the same value for all instances of now() in same statement");
507+
scalar_expr!(CurrentTime, current_time, , "returns current UTC time as a [`DataType::Time64`] value");
470508

471-
/// Returns current UTC time as a [`DataType::Time64`] value
472-
pub fn current_time() -> Expr {
473-
Expr::ScalarFunction {
474-
fun: BuiltinScalarFunction::CurrentTime,
475-
args: vec![],
476-
}
477-
}
509+
scalar_expr!(ArrowTypeof, arrow_typeof, val, "data type");
478510

479511
/// Create a CASE WHEN statement with literal WHEN expressions for comparison to the base expression.
480512
pub fn case(expr: Expr) -> CaseBuilder {
@@ -635,7 +667,6 @@ mod test {
635667
test_nary_scalar_expr!(Btrim, btrim, string);
636668
test_nary_scalar_expr!(Btrim, btrim, string, characters);
637669
test_scalar_expr!(CharacterLength, character_length, string);
638-
test_scalar_expr!(CharacterLength, length, string);
639670
test_scalar_expr!(Chr, chr, string);
640671
test_scalar_expr!(Digest, digest, string, algorithm);
641672
test_scalar_expr!(InitCap, initcap, string);

0 commit comments

Comments
 (0)