From 83249fe9a4e809bb6065671bcaee543065d2d356 Mon Sep 17 00:00:00 2001 From: Michael-J-Ward Date: Wed, 1 May 2024 13:33:10 -0500 Subject: [PATCH] wip: migrate functions.rs, compiles but not all tests pass --- src/functions.rs | 374 ++++++++++++++++++++++++++++++----------------- 1 file changed, 243 insertions(+), 131 deletions(-) diff --git a/src/functions.rs b/src/functions.rs index 069b88da..881f52b6 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -78,6 +78,11 @@ pub fn list_join(expr: PyExpr, delim: PyExpr) -> PyExpr { array_to_string(expr, delim) } +#[pyfunction] +pub fn range(start: PyExpr, stop: PyExpr, step: PyExpr) -> PyExpr { + datafusion_functions_array::expr_fn::range(start.into(), stop.into(), step.into()).into() +} + #[pyfunction] fn in_list(expr: PyExpr, value: Vec, negated: bool) -> PyExpr { datafusion_expr::in_list( @@ -88,6 +93,54 @@ fn in_list(expr: PyExpr, value: Vec, negated: bool) -> PyExpr { .into() } +#[pyfunction] +#[pyo3(signature = (*exprs))] +fn make_array(exprs: Vec) -> PyExpr { + datafusion_functions_array::expr_fn::make_array(exprs.into_iter().map(|x| x.into()).collect()) + .into() +} + +#[pyfunction] +#[pyo3(signature = (*exprs))] +fn array(exprs: Vec) -> PyExpr { + make_array(exprs) +} + +#[pyfunction] +fn array_concat(expr1: PyExpr, expr2: PyExpr) -> PyExpr { + let exprs = vec![expr1.into(), expr2.into()]; + datafusion_functions_array::expr_fn::array_concat(exprs).into() +} + +#[pyfunction] +fn array_cat(expr1: PyExpr, expr2: PyExpr) -> PyExpr { + // alias for array_concat + array_concat(expr1, expr2) +} + +/// Replaces substring(s) matching a POSIX regular expression +#[pyfunction] +fn regexp_replace(arg1: PyExpr, arg2: PyExpr, arg3: PyExpr, arg4: PyExpr) -> PyExpr { + functions::expr_fn::regexp_replace(arg1.into(), arg2.into(), arg3.into(), arg4.into()).into() +} + +/// Replaces all occurrences in string of substring from with substring to. +#[pyfunction] +fn replace(string: PyExpr, from: PyExpr, to: PyExpr) -> PyExpr { + functions::expr_fn::replace(string.into(), from.into(), to.into()).into() +} + +/// Splits string at occurrences of delimiter and returns the n'th field (counting from one). +#[pyfunction] +fn split_part(string: PyExpr, delimiter: PyExpr, index: PyExpr) -> PyExpr { + functions::expr_fn::split_part(string.into(), delimiter.into(), index.into()).into() +} + +#[pyfunction] +fn date_bin(stride: PyExpr, source: PyExpr, origin: PyExpr) -> PyExpr { + functions::expr_fn::date_bin(stride.into(), source.into(), origin.into()).into() +} + /// Computes a binary hash of the given data. type is the algorithm to use. /// Standard algorithms are md5, sha224, sha256, sha384, sha512, blake2s, blake2b, and blake3. // #[pyfunction(value, method)] @@ -95,7 +148,7 @@ fn in_list(expr: PyExpr, value: Vec, negated: bool) -> PyExpr { #[pyo3(signature = (value, method))] fn digest(value: PyExpr, method: PyExpr) -> PyExpr { PyExpr { - expr: datafusion_expr::digest(value.expr, method.expr), + expr: functions::expr_fn::digest(value.expr, method.expr), } } @@ -265,31 +318,119 @@ macro_rules! aggregate_function { }; } -scalar_function!(abs, Abs); -scalar_function!(acos, Acos); +macro_rules! expr_fn { + ($NAME: ident) => { + expr_fn!($NAME, $NAME, stringify!($NAME)); + }; + ($NAME: ident, $DOC: expr) => { + expr_fn!($NAME, $NAME, $DOC); + }; + ($NAME: ident, $FUNC: ident, $DOC: expr) => { + #[doc = $DOC] + #[pyfunction] + fn $NAME(expr: PyExpr) -> PyExpr { + functions::expr_fn::$FUNC(expr.into()).into() + } + }; +} + +macro_rules! expr_fn_zero { + ($NAME: ident) => { + expr_fn_zero!($NAME, $NAME, stringify!($NAME)); + }; + ($NAME: ident, $DOC: expr) => { + expr_fn_zero!($NAME, $NAME, $DOC); + }; + ($NAME: ident, $FUNC: ident, $DOC: expr) => { + #[doc = $DOC] + #[pyfunction] + fn $NAME() -> PyExpr { + functions::expr_fn::$FUNC().into() + } + }; +} + +macro_rules! expr_fn_dos { + ($NAME: ident) => { + expr_fn_dos!($NAME, $NAME, stringify!($NAME)); + }; + ($NAME: ident, $FUNC: ident) => { + expr_fn_dos!($NAME, $FUNC, stringify!($NAME)); + }; + ($NAME: ident, $DOC: expr) => { + expr_fn_dos!($NAME, $NAME, $DOC); + }; + ($NAME: ident, $FUNC: ident, $DOC: expr) => { + #[doc = $DOC] + #[pyfunction] + fn $NAME(expr1: PyExpr, expr2: PyExpr) -> PyExpr { + functions::expr_fn::$FUNC(expr1.into(), expr2.into()).into() + } + }; +} + +macro_rules! expr_fn_vec { + ($NAME: ident) => { + expr_fn_vec!($NAME, $NAME, stringify!($NAME)); + }; + ($NAME: ident, $DOC: expr) => { + expr_fn_vec!($NAME, $NAME, $DOC); + }; + ($NAME: ident, $FUNC: ident, $DOC: expr) => { + #[doc = $DOC] + #[pyfunction] + fn $NAME(args: Vec) -> PyExpr { + let args = args.into_iter().map(|e| e.into()).collect::>(); + functions::expr_fn::$FUNC(args).into() + } + }; +} + +macro_rules! array_fn { + ($NAME: ident) => { + array_fn!($NAME, $NAME, , stringify!($NAME)); + }; + ($NAME:ident, $($arg:ident)*) => { + array_fn!($NAME, $NAME, $($arg)*, stringify!($FUNC)); + }; + ($NAME: ident, $FUNC:ident, $($arg:ident)*) => { + array_fn!($NAME, $FUNC, $($arg)*, stringify!($FUNC)); + }; + ($NAME: ident, $DOC: expr) => { + array_fn!($NAME, $NAME, , $DOC); + }; + ($NAME: ident, $FUNC:ident, $($arg:ident)*, $DOC:expr) => { + #[doc = $DOC] + #[pyfunction] + fn $NAME($($arg: PyExpr),*) -> PyExpr { + datafusion_functions_array::expr_fn::$FUNC($($arg.into()),*).into() + } + }; +} + +expr_fn!(abs); +expr_fn!(acos); scalar_function!(acosh, Acosh); -scalar_function!(ascii, Ascii, "Returns the numeric code of the first character of the argument. In UTF8 encoding, returns the Unicode code point of the character. In other multibyte encodings, the argument must be an ASCII character."); -scalar_function!(asin, Asin); +expr_fn!(ascii, "Returns the numeric code of the first character of the argument. In UTF8 encoding, returns the Unicode code point of the character. In other multibyte encodings, the argument must be an ASCII character."); +expr_fn!(asin); scalar_function!(asinh, Asinh); scalar_function!(atan, Atan); scalar_function!(atanh, Atanh); scalar_function!(atan2, Atan2); -scalar_function!( +expr_fn!( bit_length, - BitLength, "Returns number of bits in the string (8 times the octet_length)." ); -scalar_function!(btrim, Btrim, "Removes the longest string containing only characters in characters (a space by default) from the start and end of string."); +expr_fn_vec!(btrim, "Removes the longest string containing only characters in characters (a space by default) from the start and end of string."); scalar_function!(cbrt, Cbrt); scalar_function!(ceil, Ceil); -scalar_function!( +expr_fn!( character_length, - CharacterLength, "Returns number of characters in the string." ); -scalar_function!(length, CharacterLength); -scalar_function!(char_length, CharacterLength); -scalar_function!(chr, Chr, "Returns the character with the given code."); +expr_fn!(length); +expr_fn!(char_length); +expr_fn!(chr, "Returns the character with the given code."); scalar_function!(coalesce, Coalesce); scalar_function!(cos, Cos); scalar_function!(cosh, Cosh); @@ -306,12 +447,11 @@ scalar_function!(ln, Ln); scalar_function!(log, Log); scalar_function!(log10, Log10); scalar_function!(log2, Log2); -scalar_function!(lower, Lower, "Converts the string to all lower case"); +expr_fn!(lower, "Converts the string to all lower case"); scalar_function!(lpad, Lpad, "Extends the string to length length by prepending the characters fill (a space by default). If the string is already longer than length then it is truncated (on the right)."); -scalar_function!(ltrim, Ltrim, "Removes the longest string containing only characters in characters (a space by default) from the start of string."); -scalar_function!( +expr_fn_vec!(ltrim, "Removes the longest string containing only characters in characters (a space by default) from the start of string."); +expr_fn!( md5, - MD5, "Computes the MD5 hash of the argument, with the result written in hexadecimal." ); scalar_function!( @@ -319,27 +459,13 @@ scalar_function!( Nanvl, "Returns x if x is not NaN otherwise returns y." ); -scalar_function!(octet_length, OctetLength, "Returns number of bytes in the string. Since this version of the function accepts type character directly, it will not strip trailing spaces."); +expr_fn_vec!(octet_length, "Returns number of bytes in the string. Since this version of the function accepts type character directly, it will not strip trailing spaces."); scalar_function!(pi, Pi); scalar_function!(power, Power); scalar_function!(pow, Power); scalar_function!(radians, Radians); -scalar_function!(regexp_match, RegexpMatch); -scalar_function!( - regexp_replace, - RegexpReplace, - "Replaces substring(s) matching a POSIX regular expression" -); -scalar_function!( - repeat, - Repeat, - "Repeats string the specified number of times." -); -scalar_function!( - replace, - Replace, - "Replaces all occurrences in string of substring from with substring to." -); +expr_fn_dos!(regexp_match); +expr_fn_dos!(repeat, "Repeats string the specified number of times."); scalar_function!( reverse, Reverse, @@ -348,117 +474,103 @@ scalar_function!( scalar_function!(right, Right, "Returns last n characters in the string, or when n is negative, returns all but first |n| characters."); scalar_function!(round, Round); scalar_function!(rpad, Rpad, "Extends the string to length length by appending the characters fill (a space by default). If the string is already longer than length then it is truncated."); -scalar_function!(rtrim, Rtrim, "Removes the longest string containing only characters in characters (a space by default) from the end of string."); -scalar_function!(sha224, SHA224); -scalar_function!(sha256, SHA256); -scalar_function!(sha384, SHA384); -scalar_function!(sha512, SHA512); +expr_fn_vec!(rtrim, "Removes the longest string containing only characters in characters (a space by default) from the end of string."); +expr_fn!(sha224); +expr_fn!(sha256); +expr_fn!(sha384); +expr_fn!(sha512); scalar_function!(signum, Signum); scalar_function!(sin, Sin); scalar_function!(sinh, Sinh); -scalar_function!( - split_part, - SplitPart, - "Splits string at occurrences of delimiter and returns the n'th field (counting from one)." -); + scalar_function!(sqrt, Sqrt); -scalar_function!( - starts_with, - StartsWith, - "Returns true if string starts with prefix." -); +expr_fn_dos!(starts_with, "Returns true if string starts with prefix."); scalar_function!(strpos, Strpos, "Returns starting index of specified substring within string, or zero if it's not present. (Same as position(substring in string), but note the reversed argument order.)"); scalar_function!(substr, Substr); -scalar_function!(tan, Tan); -scalar_function!(tanh, Tanh); -scalar_function!( +expr_fn!(tan); +expr_fn!(tanh); +expr_fn!( to_hex, - ToHex, "Converts the number to its equivalent hexadecimal representation." ); -scalar_function!(now, Now); -scalar_function!(to_timestamp, ToTimestamp); -scalar_function!(to_timestamp_millis, ToTimestampMillis); -scalar_function!(to_timestamp_micros, ToTimestampMicros); -scalar_function!(to_timestamp_seconds, ToTimestampSeconds); -scalar_function!(current_date, CurrentDate); -scalar_function!(current_time, CurrentTime); -scalar_function!(datepart, DatePart); -scalar_function!(date_part, DatePart); -scalar_function!(date_trunc, DateTrunc); -scalar_function!(datetrunc, DateTrunc); -scalar_function!(date_bin, DateBin); +expr_fn_zero!(now); +expr_fn_vec!(to_timestamp); +expr_fn_vec!(to_timestamp_millis); +expr_fn_vec!(to_timestamp_micros); +expr_fn_vec!(to_timestamp_seconds); +expr_fn_zero!(current_date); +expr_fn_zero!(current_time); +expr_fn_dos!(datepart, date_part); +expr_fn_dos!(date_part); +expr_fn_dos!(date_trunc); +expr_fn_dos!(datetrunc, date_trunc); + scalar_function!(translate, Translate, "Replaces each character in string that matches a character in the from set with the corresponding character in the to set. If from is longer than to, occurrences of the extra characters in from are deleted."); -scalar_function!(trim, Trim, "Removes the longest string containing only characters in characters (a space by default) from the start, end, or both ends (BOTH is the default) of string."); +expr_fn_vec!(trim, "Removes the longest string containing only characters in characters (a space by default) from the start, end, or both ends (BOTH is the default) of string."); scalar_function!(trunc, Trunc); -scalar_function!(upper, Upper, "Converts the string to all upper case."); -scalar_function!(make_array, MakeArray); -scalar_function!(array, MakeArray); -scalar_function!(range, Range); -scalar_function!(uuid, Uuid); -scalar_function!(r#struct, Struct); // Use raw identifier since struct is a keyword -scalar_function!(from_unixtime, FromUnixtime); -scalar_function!(arrow_typeof, ArrowTypeof); +expr_fn!(upper, "Converts the string to all upper case."); +expr_fn_zero!(uuid); +expr_fn!(r#struct); // Use raw identifier since struct is a keyword +expr_fn!(from_unixtime); +expr_fn!(arrow_typeof); scalar_function!(random, Random); // Array Functions -scalar_function!(array_append, ArrayAppend); -scalar_function!(array_push_back, ArrayAppend); -scalar_function!(list_append, ArrayAppend); -scalar_function!(list_push_back, ArrayAppend); -scalar_function!(array_concat, ArrayConcat); -scalar_function!(array_cat, ArrayConcat); -scalar_function!(array_dims, ArrayDims); -scalar_function!(array_distinct, ArrayDistinct); -scalar_function!(list_distinct, ArrayDistinct); -scalar_function!(list_dims, ArrayDims); -scalar_function!(array_element, ArrayElement); -scalar_function!(array_extract, ArrayElement); -scalar_function!(list_element, ArrayElement); -scalar_function!(list_extract, ArrayElement); -scalar_function!(array_length, ArrayLength); -scalar_function!(list_length, ArrayLength); -scalar_function!(array_has, ArrayHas); -scalar_function!(array_has_all, ArrayHasAll); -scalar_function!(array_has_any, ArrayHasAny); -scalar_function!(array_position, ArrayPosition); -scalar_function!(array_indexof, ArrayPosition); -scalar_function!(list_position, ArrayPosition); -scalar_function!(list_indexof, ArrayPosition); -scalar_function!(array_positions, ArrayPositions); -scalar_function!(list_positions, ArrayPositions); -scalar_function!(array_ndims, ArrayNdims); -scalar_function!(list_ndims, ArrayNdims); -scalar_function!(array_prepend, ArrayPrepend); -scalar_function!(array_push_front, ArrayPrepend); -scalar_function!(list_prepend, ArrayPrepend); -scalar_function!(list_push_front, ArrayPrepend); -scalar_function!(array_pop_back, ArrayPopBack); -scalar_function!(array_pop_front, ArrayPopFront); -scalar_function!(array_remove, ArrayRemove); -scalar_function!(list_remove, ArrayRemove); -scalar_function!(array_remove_n, ArrayRemoveN); -scalar_function!(list_remove_n, ArrayRemoveN); -scalar_function!(array_remove_all, ArrayRemoveAll); -scalar_function!(list_remove_all, ArrayRemoveAll); -scalar_function!(array_repeat, ArrayRepeat); -scalar_function!(array_replace, ArrayReplace); -scalar_function!(list_replace, ArrayReplace); -scalar_function!(array_replace_n, ArrayReplaceN); -scalar_function!(list_replace_n, ArrayReplaceN); -scalar_function!(array_replace_all, ArrayReplaceAll); -scalar_function!(list_replace_all, ArrayReplaceAll); -scalar_function!(array_slice, ArraySlice); -scalar_function!(list_slice, ArraySlice); -scalar_function!(array_intersect, ArrayIntersect); -scalar_function!(list_intersect, ArrayIntersect); -scalar_function!(array_union, ArrayUnion); -scalar_function!(list_union, ArrayUnion); -scalar_function!(array_except, ArrayExcept); -scalar_function!(list_except, ArrayExcept); -scalar_function!(array_resize, ArrayResize); -scalar_function!(list_resize, ArrayResize); -scalar_function!(flatten, Flatten); +array_fn!(array_append, array elem); +array_fn!(array_push_back, array_prepend, array elem); +array_fn!(list_append, array_append, array elem); +array_fn!(list_push_back, array_append, array elem); +array_fn!(array_dims, array); +array_fn!(array_distinct, array); +array_fn!(list_distinct, array_distinct, array); +array_fn!(list_dims, array_dims, array); +array_fn!(array_element, array element); +array_fn!(array_extract, array_element, array element); +array_fn!(list_element, array_element, array element); +array_fn!(list_extract, array_element, array element); +array_fn!(array_length, array); +array_fn!(list_length, array_length, array); +array_fn!(array_has, first_array second_array); +array_fn!(array_has_all, first_array second_array); +array_fn!(array_has_any, first_array second_array); +array_fn!(array_position, array element index); +array_fn!(array_indexof, array_position, array element index); +array_fn!(list_position, array_position, array element index); +array_fn!(list_indexof, array_position, array element index); +array_fn!(array_positions, array_position, array element indexs); +array_fn!(list_positions, array_position, array element indexs); +array_fn!(array_ndims, array); +array_fn!(list_ndims, array_ndims, array); +array_fn!(array_prepend, array element); +array_fn!(array_push_front, array_prepend, array element); +array_fn!(list_prepend, array_prepend, array element); +array_fn!(list_push_front, array_prepend, array element); +array_fn!(array_pop_back, array); +array_fn!(array_pop_front, array); +array_fn!(array_remove, array element); +array_fn!(list_remove, array_remove, array element); +array_fn!(array_remove_n, array element max); +array_fn!(list_remove_n, array_remove_n, array element max); +array_fn!(array_remove_all, array element); +array_fn!(list_remove_all, array_remove_all, array element); +array_fn!(array_repeat, element count); +array_fn!(array_replace, array from to); +array_fn!(list_replace, array_replace, array from to); +array_fn!(array_replace_n, array from to max); +array_fn!(list_replace_n, array_replace_n, array from to max); +array_fn!(array_replace_all, array from to); +array_fn!(list_replace_all, array_replace_all, array from to); +array_fn!(array_slice, array begin end stride); +array_fn!(list_slice, array_slice, array begin end stride); +array_fn!(array_intersect, first_array second_array); +array_fn!(list_intersect, array_intersect, first_array second_array); +array_fn!(array_union, array1 array2); +array_fn!(list_union, array_union, array1 array2); +array_fn!(array_except, first_array second_array); +array_fn!(list_except, array_except, first_array second_array); +array_fn!(array_resize, array size value); +array_fn!(list_resize, array_resize, array size value); +array_fn!(flatten, array); aggregate_function!(approx_distinct, ApproxDistinct); aggregate_function!(approx_median, ApproxMedian);