diff --git a/python/src/functions.rs b/python/src/functions.rs index fa4a4868bb472..087fa041591b1 100644 --- a/python/src/functions.rs +++ b/python/src/functions.rs @@ -17,9 +17,15 @@ use std::sync::Arc; +use pyo3::{prelude::*, wrap_pyfunction}; + use datafusion::arrow::datatypes::DataType; use datafusion::logical_plan; -use pyo3::{prelude::*, wrap_pyfunction}; +use datafusion::logical_plan::Expr; +use datafusion::physical_plan::{ + aggregates::AggregateFunction, functions::BuiltinScalarFunction, udaf::AggregateUDF, + udf::ScalarUDF, +}; use crate::{ expression::{PyAggregateUDF, PyExpr, PyScalarUDF}, @@ -94,98 +100,131 @@ fn concat_ws(sep: String, args: Vec) -> PyResult { Ok(logical_plan::concat_ws(sep, &args).into()) } -macro_rules! define_unary_function { - ($NAME: ident) => { - #[doc = "This function is not documented yet"] - #[pyfunction] - fn $NAME(value: PyExpr) -> PyExpr { - logical_plan::$NAME(value.expr).into() - } +macro_rules! scalar_function { + ($NAME: ident, $FUNC: ident) => { + scalar_function!($NAME, $FUNC, stringify!($NAME)); }; - ($NAME: ident, $DOC: expr) => { + ($NAME: ident, $FUNC: ident, $DOC: expr) => { #[doc = $DOC] - #[pyfunction] - fn $NAME(value: PyExpr) -> PyExpr { - logical_plan::$NAME(value.expr).into() + #[pyfunction(args = "*")] + fn $NAME(args: Vec) -> PyExpr { + let expr = Expr::ScalarFunction { + fun: BuiltinScalarFunction::$FUNC, + args: args.into_iter().map(|e| e.into()).collect(), + }; + expr.into() } }; } -define_unary_function!(sqrt, "sqrt"); -define_unary_function!(sin, "sin"); -define_unary_function!(cos, "cos"); -define_unary_function!(tan, "tan"); -define_unary_function!(asin, "asin"); -define_unary_function!(acos, "acos"); -define_unary_function!(atan, "atan"); -define_unary_function!(floor, "floor"); -define_unary_function!(ceil, "ceil"); -define_unary_function!(round, "round"); -define_unary_function!(trunc, "trunc"); -define_unary_function!(abs, "abs"); -define_unary_function!(signum, "signum"); -define_unary_function!(exp, "exp"); -define_unary_function!(ln, "ln"); -define_unary_function!(log2, "log2"); -define_unary_function!(log10, "log10"); +macro_rules! aggregate_function { + ($NAME: ident, $FUNC: ident) => { + aggregate_function!($NAME, $FUNC, stringify!($NAME)); + }; + ($NAME: ident, $FUNC: ident, $DOC: expr) => { + #[doc = $DOC] + #[pyfunction(args = "*", distinct = "false")] + fn $NAME(args: Vec, distinct: bool) -> PyExpr { + let expr = Expr::AggregateFunction { + fun: AggregateFunction::$FUNC, + args: args.into_iter().map(|e| e.into()).collect(), + distinct, + }; + expr.into() + } + }; +} -define_unary_function!(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."); -define_unary_function!(sum); -define_unary_function!( +scalar_function!(abs, Abs); +scalar_function!(acos, Acos); +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); +scalar_function!(atan, Atan); +scalar_function!( bit_length, + BitLength, "Returns number of bits in the string (8 times the octet_length)." ); -define_unary_function!(btrim, "Removes the longest string containing only characters in characters (a space by default) from the start and end of string."); -define_unary_function!( +scalar_function!(btrim, Btrim, "Removes the longest string containing only characters in characters (a space by default) from the start and end of string."); +scalar_function!(ceil, Ceil); +scalar_function!( character_length, + CharacterLength, "Returns number of characters in the string." ); -define_unary_function!(chr, "Returns the character with the given code."); -define_unary_function!(initcap, "Converts the first letter of each word to upper case and the rest to lower case. Words are sequences of alphanumeric characters separated by non-alphanumeric characters."); -define_unary_function!(left, "Returns first n characters in the string, or when n is negative, returns all but last |n| characters."); -define_unary_function!(lower, "Converts the string to all lower case"); -define_unary_function!(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)."); -define_unary_function!(ltrim, "Removes the longest string containing only characters in characters (a space by default) from the start of string."); -define_unary_function!( +scalar_function!(chr, Chr, "Returns the character with the given code."); +scalar_function!(cos, Cos); +scalar_function!(exp, Exp); +scalar_function!(floor, Floor); +scalar_function!(initcap, InitCap, "Converts the first letter of each word to upper case and the rest to lower case. Words are sequences of alphanumeric characters separated by non-alphanumeric characters."); +scalar_function!(left, Left, "Returns first n characters in the string, or when n is negative, returns all but last |n| characters."); +scalar_function!(ln, Ln); +scalar_function!(log10, Log10); +scalar_function!(log2, Log2); +scalar_function!(lower, 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!( md5, + MD5, "Computes the MD5 hash of the argument, with the result written in hexadecimal." ); -define_unary_function!(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."); -define_unary_function!( - replace, - "Replaces all occurrences in string of substring from with substring to." -); -define_unary_function!(repeat, "Repeats string the specified number of times."); -define_unary_function!( +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."); +scalar_function!( regexp_replace, + RegexpReplace, "Replaces substring(s) matching a POSIX regular expression" ); -define_unary_function!( +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." +); +scalar_function!( reverse, + Reverse, "Reverses the order of the characters in the string." ); -define_unary_function!(right, "Returns last n characters in the string, or when n is negative, returns all but first |n| characters."); -define_unary_function!(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."); -define_unary_function!(rtrim, "Removes the longest string containing only characters in characters (a space by default) from the end of string."); -define_unary_function!(sha224); -define_unary_function!(sha256); -define_unary_function!(sha384); -define_unary_function!(sha512); -define_unary_function!(split_part, "Splits string at occurrences of delimiter and returns the n'th field (counting from one)."); -define_unary_function!(starts_with, "Returns true if string starts with prefix."); -define_unary_function!(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.)"); -define_unary_function!(substr); -define_unary_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); +scalar_function!(signum, Signum); +scalar_function!(sin, Sin); +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." +); +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!( to_hex, + ToHex, "Converts the number to its equivalent hexadecimal representation." ); -define_unary_function!(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."); -define_unary_function!(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."); -define_unary_function!(upper, "Converts the string to all upper case."); -define_unary_function!(avg); -define_unary_function!(min); -define_unary_function!(max); -define_unary_function!(count); +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."); +scalar_function!(trunc, Trunc); +scalar_function!(upper, Upper, "Converts the string to all upper case."); + +aggregate_function!(avg, Avg); +aggregate_function!(count, Count); +aggregate_function!(max, Max); +aggregate_function!(min, Min); +aggregate_function!(sum, Sum); pub(crate) fn create_udf( fun: PyObject, @@ -240,70 +279,69 @@ fn udaf( }) } -pub fn init(module: &PyModule) -> PyResult<()> { - module.add_function(wrap_pyfunction!(abs, module)?)?; - module.add_function(wrap_pyfunction!(acos, module)?)?; - module.add_function(wrap_pyfunction!(array, module)?)?; - module.add_function(wrap_pyfunction!(ascii, module)?)?; - module.add_function(wrap_pyfunction!(asin, module)?)?; - module.add_function(wrap_pyfunction!(atan, module)?)?; - module.add_function(wrap_pyfunction!(avg, module)?)?; - module.add_function(wrap_pyfunction!(bit_length, module)?)?; - module.add_function(wrap_pyfunction!(btrim, module)?)?; - module.add_function(wrap_pyfunction!(ceil, module)?)?; - module.add_function(wrap_pyfunction!(character_length, module)?)?; - module.add_function(wrap_pyfunction!(chr, module)?)?; - module.add_function(wrap_pyfunction!(col, module)?)?; - module.add_function(wrap_pyfunction!(concat_ws, module)?)?; - module.add_function(wrap_pyfunction!(concat, module)?)?; - module.add_function(wrap_pyfunction!(cos, module)?)?; - module.add_function(wrap_pyfunction!(count, module)?)?; - module.add_function(wrap_pyfunction!(exp, module)?)?; - module.add_function(wrap_pyfunction!(floor, module)?)?; - module.add_function(wrap_pyfunction!(in_list, module)?)?; - module.add_function(wrap_pyfunction!(initcap, module)?)?; - module.add_function(wrap_pyfunction!(left, module)?)?; - module.add_function(wrap_pyfunction!(lit, module)?)?; - module.add_function(wrap_pyfunction!(ln, module)?)?; - module.add_function(wrap_pyfunction!(log10, module)?)?; - module.add_function(wrap_pyfunction!(log2, module)?)?; - module.add_function(wrap_pyfunction!(lower, module)?)?; - module.add_function(wrap_pyfunction!(lpad, module)?)?; - module.add_function(wrap_pyfunction!(ltrim, module)?)?; - module.add_function(wrap_pyfunction!(max, module)?)?; - module.add_function(wrap_pyfunction!(md5, module)?)?; - module.add_function(wrap_pyfunction!(min, module)?)?; - module.add_function(wrap_pyfunction!(now, module)?)?; - module.add_function(wrap_pyfunction!(octet_length, module)?)?; - module.add_function(wrap_pyfunction!(random, module)?)?; - module.add_function(wrap_pyfunction!(regexp_replace, module)?)?; - module.add_function(wrap_pyfunction!(repeat, module)?)?; - module.add_function(wrap_pyfunction!(replace, module)?)?; - module.add_function(wrap_pyfunction!(reverse, module)?)?; - module.add_function(wrap_pyfunction!(right, module)?)?; - module.add_function(wrap_pyfunction!(round, module)?)?; - module.add_function(wrap_pyfunction!(rpad, module)?)?; - module.add_function(wrap_pyfunction!(rtrim, module)?)?; - module.add_function(wrap_pyfunction!(sha224, module)?)?; - module.add_function(wrap_pyfunction!(sha256, module)?)?; - module.add_function(wrap_pyfunction!(sha384, module)?)?; - module.add_function(wrap_pyfunction!(sha512, module)?)?; - module.add_function(wrap_pyfunction!(signum, module)?)?; - module.add_function(wrap_pyfunction!(sin, module)?)?; - module.add_function(wrap_pyfunction!(split_part, module)?)?; - module.add_function(wrap_pyfunction!(sqrt, module)?)?; - module.add_function(wrap_pyfunction!(starts_with, module)?)?; - module.add_function(wrap_pyfunction!(strpos, module)?)?; - module.add_function(wrap_pyfunction!(substr, module)?)?; - module.add_function(wrap_pyfunction!(sum, module)?)?; - module.add_function(wrap_pyfunction!(tan, module)?)?; - module.add_function(wrap_pyfunction!(to_hex, module)?)?; - module.add_function(wrap_pyfunction!(translate, module)?)?; - module.add_function(wrap_pyfunction!(trim, module)?)?; - module.add_function(wrap_pyfunction!(trunc, module)?)?; - module.add_function(wrap_pyfunction!(udaf, module)?)?; - module.add_function(wrap_pyfunction!(udf, module)?)?; - module.add_function(wrap_pyfunction!(upper, module)?)?; - +pub fn init(m: &PyModule) -> PyResult<()> { + m.add_wrapped(wrap_pyfunction!(abs))?; + m.add_wrapped(wrap_pyfunction!(acos))?; + m.add_wrapped(wrap_pyfunction!(array))?; + m.add_wrapped(wrap_pyfunction!(ascii))?; + m.add_wrapped(wrap_pyfunction!(asin))?; + m.add_wrapped(wrap_pyfunction!(atan))?; + m.add_wrapped(wrap_pyfunction!(avg))?; + m.add_wrapped(wrap_pyfunction!(bit_length))?; + m.add_wrapped(wrap_pyfunction!(btrim))?; + m.add_wrapped(wrap_pyfunction!(ceil))?; + m.add_wrapped(wrap_pyfunction!(character_length))?; + m.add_wrapped(wrap_pyfunction!(chr))?; + m.add_wrapped(wrap_pyfunction!(col))?; + m.add_wrapped(wrap_pyfunction!(concat_ws))?; + m.add_wrapped(wrap_pyfunction!(concat))?; + m.add_wrapped(wrap_pyfunction!(cos))?; + m.add_wrapped(wrap_pyfunction!(count))?; + m.add_wrapped(wrap_pyfunction!(exp))?; + m.add_wrapped(wrap_pyfunction!(floor))?; + m.add_wrapped(wrap_pyfunction!(in_list))?; + m.add_wrapped(wrap_pyfunction!(initcap))?; + m.add_wrapped(wrap_pyfunction!(left))?; + m.add_wrapped(wrap_pyfunction!(lit))?; + m.add_wrapped(wrap_pyfunction!(ln))?; + m.add_wrapped(wrap_pyfunction!(log10))?; + m.add_wrapped(wrap_pyfunction!(log2))?; + m.add_wrapped(wrap_pyfunction!(lower))?; + m.add_wrapped(wrap_pyfunction!(lpad))?; + m.add_wrapped(wrap_pyfunction!(ltrim))?; + m.add_wrapped(wrap_pyfunction!(max))?; + m.add_wrapped(wrap_pyfunction!(md5))?; + m.add_wrapped(wrap_pyfunction!(min))?; + m.add_wrapped(wrap_pyfunction!(now))?; + m.add_wrapped(wrap_pyfunction!(octet_length))?; + m.add_wrapped(wrap_pyfunction!(random))?; + m.add_wrapped(wrap_pyfunction!(regexp_replace))?; + m.add_wrapped(wrap_pyfunction!(repeat))?; + m.add_wrapped(wrap_pyfunction!(replace))?; + m.add_wrapped(wrap_pyfunction!(reverse))?; + m.add_wrapped(wrap_pyfunction!(right))?; + m.add_wrapped(wrap_pyfunction!(round))?; + m.add_wrapped(wrap_pyfunction!(rpad))?; + m.add_wrapped(wrap_pyfunction!(rtrim))?; + m.add_wrapped(wrap_pyfunction!(sha224))?; + m.add_wrapped(wrap_pyfunction!(sha256))?; + m.add_wrapped(wrap_pyfunction!(sha384))?; + m.add_wrapped(wrap_pyfunction!(sha512))?; + m.add_wrapped(wrap_pyfunction!(signum))?; + m.add_wrapped(wrap_pyfunction!(sin))?; + m.add_wrapped(wrap_pyfunction!(split_part))?; + m.add_wrapped(wrap_pyfunction!(sqrt))?; + m.add_wrapped(wrap_pyfunction!(starts_with))?; + m.add_wrapped(wrap_pyfunction!(strpos))?; + m.add_wrapped(wrap_pyfunction!(substr))?; + m.add_wrapped(wrap_pyfunction!(sum))?; + m.add_wrapped(wrap_pyfunction!(tan))?; + m.add_wrapped(wrap_pyfunction!(to_hex))?; + m.add_wrapped(wrap_pyfunction!(translate))?; + m.add_wrapped(wrap_pyfunction!(trim))?; + m.add_wrapped(wrap_pyfunction!(trunc))?; + m.add_wrapped(wrap_pyfunction!(udaf))?; + m.add_wrapped(wrap_pyfunction!(udf))?; + m.add_wrapped(wrap_pyfunction!(upper))?; Ok(()) }