Skip to content

Commit

Permalink
Move radians, signum, sin, sinh and sqrt functions to `data…
Browse files Browse the repository at this point in the history
…fusion-functions` crate (apache#9882)

* Issue-9860 - Move Radians, Signum, Sin, Sinh, Sqrt functions to datafusion-functions

* Issue-9860 - Fix UT failure

---------

Co-authored-by: Andrew Lamb <[email protected]>
  • Loading branch information
erenavsarogullari and alamb authored Apr 2, 2024
1 parent e9c5dc9 commit f5805b7
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 130 deletions.
34 changes: 0 additions & 34 deletions datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,8 @@ pub enum BuiltinScalarFunction {
Pi,
/// power
Power,
/// radians
Radians,
/// round
Round,
/// signum
Signum,
/// sin
Sin,
/// sinh
Sinh,
/// sqrt
Sqrt,
/// trunc
Trunc,
/// cot
Expand Down Expand Up @@ -165,18 +155,13 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::Pi => Volatility::Immutable,
BuiltinScalarFunction::Power => Volatility::Immutable,
BuiltinScalarFunction::Round => Volatility::Immutable,
BuiltinScalarFunction::Signum => Volatility::Immutable,
BuiltinScalarFunction::Sin => Volatility::Immutable,
BuiltinScalarFunction::Sinh => Volatility::Immutable,
BuiltinScalarFunction::Sqrt => Volatility::Immutable,
BuiltinScalarFunction::Cbrt => Volatility::Immutable,
BuiltinScalarFunction::Cot => Volatility::Immutable,
BuiltinScalarFunction::Trunc => Volatility::Immutable,
BuiltinScalarFunction::Concat => Volatility::Immutable,
BuiltinScalarFunction::ConcatWithSeparator => Volatility::Immutable,
BuiltinScalarFunction::EndsWith => Volatility::Immutable,
BuiltinScalarFunction::InitCap => Volatility::Immutable,
BuiltinScalarFunction::Radians => Volatility::Immutable,

// Volatile builtin functions
BuiltinScalarFunction::Random => Volatility::Volatile,
Expand Down Expand Up @@ -241,12 +226,7 @@ impl BuiltinScalarFunction {
| BuiltinScalarFunction::Degrees
| BuiltinScalarFunction::Exp
| BuiltinScalarFunction::Floor
| BuiltinScalarFunction::Radians
| BuiltinScalarFunction::Round
| BuiltinScalarFunction::Signum
| BuiltinScalarFunction::Sin
| BuiltinScalarFunction::Sinh
| BuiltinScalarFunction::Sqrt
| BuiltinScalarFunction::Cbrt
| BuiltinScalarFunction::Trunc
| BuiltinScalarFunction::Cot => match input_expr_types[0] {
Expand Down Expand Up @@ -335,11 +315,6 @@ impl BuiltinScalarFunction {
| BuiltinScalarFunction::Degrees
| BuiltinScalarFunction::Exp
| BuiltinScalarFunction::Floor
| BuiltinScalarFunction::Radians
| BuiltinScalarFunction::Signum
| BuiltinScalarFunction::Sin
| BuiltinScalarFunction::Sinh
| BuiltinScalarFunction::Sqrt
| BuiltinScalarFunction::Cot => {
// math expressions expect 1 argument of type f64 or f32
// priority is given to f64 because e.g. `sqrt(1i32)` is in IR (real numbers) and thus we
Expand All @@ -366,11 +341,7 @@ impl BuiltinScalarFunction {
| BuiltinScalarFunction::Exp
| BuiltinScalarFunction::Factorial
| BuiltinScalarFunction::Floor
| BuiltinScalarFunction::Radians
| BuiltinScalarFunction::Round
| BuiltinScalarFunction::Signum
| BuiltinScalarFunction::Sinh
| BuiltinScalarFunction::Sqrt
| BuiltinScalarFunction::Cbrt
| BuiltinScalarFunction::Trunc
| BuiltinScalarFunction::Pi
Expand Down Expand Up @@ -402,13 +373,8 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::Nanvl => &["nanvl"],
BuiltinScalarFunction::Pi => &["pi"],
BuiltinScalarFunction::Power => &["power", "pow"],
BuiltinScalarFunction::Radians => &["radians"],
BuiltinScalarFunction::Random => &["random"],
BuiltinScalarFunction::Round => &["round"],
BuiltinScalarFunction::Signum => &["signum"],
BuiltinScalarFunction::Sin => &["sin"],
BuiltinScalarFunction::Sinh => &["sinh"],
BuiltinScalarFunction::Sqrt => &["sqrt"],
BuiltinScalarFunction::Trunc => &["trunc"],

// conditional functions
Expand Down
14 changes: 2 additions & 12 deletions datafusion/expr/src/expr_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,9 @@ macro_rules! nary_scalar_expr {
// generate methods for creating the supported unary/binary expressions

// math functions
scalar_expr!(Sqrt, sqrt, num, "square root of a number");
scalar_expr!(Cbrt, cbrt, num, "cube root of a number");
scalar_expr!(Sin, sin, num, "sine");
scalar_expr!(Cos, cos, num, "cosine");
scalar_expr!(Cot, cot, num, "cotangent");
scalar_expr!(Sinh, sinh, num, "hyperbolic sine");
scalar_expr!(Cosh, cosh, num, "hyperbolic cosine");
scalar_expr!(Factorial, factorial, num, "factorial");
scalar_expr!(
Expand All @@ -555,14 +552,12 @@ scalar_expr!(
"nearest integer greater than or equal to argument"
);
scalar_expr!(Degrees, degrees, num, "converts radians to degrees");
scalar_expr!(Radians, radians, num, "converts degrees to radians");
nary_scalar_expr!(Round, round, "round to nearest integer");
nary_scalar_expr!(
Trunc,
trunc,
"truncate toward zero, with optional precision"
);
scalar_expr!(Signum, signum, num, "sign of the argument (-1, 0, +1) ");
scalar_expr!(Exp, exp, num, "exponential");
scalar_expr!(Gcd, gcd, arg_1 arg_2, "greatest common divisor");
scalar_expr!(Lcm, lcm, arg_1 arg_2, "least common multiple");
Expand Down Expand Up @@ -885,8 +880,8 @@ impl WindowUDFImpl for SimpleWindowUDF {
/// ```
/// use datafusion_expr::{col, lit, call_fn};
///
/// // create the expression sin(x) < 0.2
/// let expr = call_fn("sin", vec![col("x")]).unwrap().lt(lit(0.2));
/// // create the expression trunc(x) < 0.2
/// let expr = call_fn("trunc", vec![col("x")]).unwrap().lt(lit(0.2));
/// ```
pub fn call_fn(name: impl AsRef<str>, args: Vec<Expr>) -> Result<Expr> {
match name.as_ref().parse::<BuiltinScalarFunction>() {
Expand Down Expand Up @@ -967,23 +962,18 @@ mod test {

#[test]
fn scalar_function_definitions() {
test_unary_scalar_expr!(Sqrt, sqrt);
test_unary_scalar_expr!(Cbrt, cbrt);
test_unary_scalar_expr!(Sin, sin);
test_unary_scalar_expr!(Cos, cos);
test_unary_scalar_expr!(Cot, cot);
test_unary_scalar_expr!(Sinh, sinh);
test_unary_scalar_expr!(Cosh, cosh);
test_unary_scalar_expr!(Factorial, factorial);
test_unary_scalar_expr!(Floor, floor);
test_unary_scalar_expr!(Ceil, ceil);
test_unary_scalar_expr!(Degrees, degrees);
test_unary_scalar_expr!(Radians, radians);
test_nary_scalar_expr!(Round, round, input);
test_nary_scalar_expr!(Round, round, input, decimal_places);
test_nary_scalar_expr!(Trunc, trunc, num);
test_nary_scalar_expr!(Trunc, trunc, num, precision);
test_unary_scalar_expr!(Signum, signum);
test_unary_scalar_expr!(Exp, exp);
test_scalar_expr!(Nanvl, nanvl, x, y);
test_scalar_expr!(Iszero, iszero, input);
Expand Down
13 changes: 12 additions & 1 deletion datafusion/functions/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ make_math_unary_udf!(AcoshFunc, ACOSH, acosh, acosh, Some(vec![Some(true)]));
make_math_unary_udf!(AtanFunc, ATAN, atan, atan, Some(vec![Some(true)]));
make_math_binary_udf!(Atan2, ATAN2, atan2, atan2, Some(vec![Some(true)]));

make_math_unary_udf!(RadiansFunc, RADIANS, radians, to_radians, None);
make_math_unary_udf!(SignumFunc, SIGNUM, signum, signum, None);
make_math_unary_udf!(SinFunc, SIN, sin, sin, None);
make_math_unary_udf!(SinhFunc, SINH, sinh, sinh, None);
make_math_unary_udf!(SqrtFunc, SQRT, sqrt, sqrt, None);

// Export the functions out of this package, both as expr_fn as well as a list of functions
export_functions!(
(
Expand Down Expand Up @@ -66,5 +72,10 @@ export_functions!(
(asinh, num, "returns inverse hyperbolic sine"),
(acosh, num, "returns inverse hyperbolic cosine"),
(atan, num, "returns inverse tangent"),
(atan2, y x, "returns inverse tangent of a division given in the argument")
(atan2, y x, "returns inverse tangent of a division given in the argument"),
(radians, num, "converts degrees to radians"),
(signum, num, "sign of the argument (-1, 0, +1)"),
(sin, num, "sine"),
(sinh, num, "hyperbolic sine"),
(sqrt, num, "square root of a number")
);
5 changes: 0 additions & 5 deletions datafusion/physical-expr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,10 @@ pub fn create_physical_fun(
BuiltinScalarFunction::Nanvl => {
Arc::new(|args| make_scalar_function_inner(math_expressions::nanvl)(args))
}
BuiltinScalarFunction::Radians => Arc::new(math_expressions::to_radians),
BuiltinScalarFunction::Random => Arc::new(math_expressions::random),
BuiltinScalarFunction::Round => {
Arc::new(|args| make_scalar_function_inner(math_expressions::round)(args))
}
BuiltinScalarFunction::Signum => Arc::new(math_expressions::signum),
BuiltinScalarFunction::Sin => Arc::new(math_expressions::sin),
BuiltinScalarFunction::Sinh => Arc::new(math_expressions::sinh),
BuiltinScalarFunction::Sqrt => Arc::new(math_expressions::sqrt),
BuiltinScalarFunction::Cbrt => Arc::new(math_expressions::cbrt),
BuiltinScalarFunction::Trunc => {
Arc::new(|args| make_scalar_function_inner(math_expressions::trunc)(args))
Expand Down
5 changes: 0 additions & 5 deletions datafusion/physical-expr/src/math_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,8 @@ macro_rules! make_function_scalar_inputs_return_type {
}};
}

math_unary_function!("sqrt", sqrt);
math_unary_function!("cbrt", cbrt);
math_unary_function!("sin", sin);
math_unary_function!("cos", cos);
math_unary_function!("sinh", sinh);
math_unary_function!("cosh", cosh);
math_unary_function!("asin", asin);
math_unary_function!("acos", acos);
Expand All @@ -169,13 +166,11 @@ math_unary_function!("acosh", acosh);
math_unary_function!("atanh", atanh);
math_unary_function!("floor", floor);
math_unary_function!("ceil", ceil);
math_unary_function!("signum", signum);
math_unary_function!("exp", exp);
math_unary_function!("ln", ln);
math_unary_function!("log2", log2);
math_unary_function!("log10", log10);
math_unary_function!("degrees", to_degrees);
math_unary_function!("radians", to_radians);

/// Factorial SQL function
pub fn factorial(args: &[ArrayRef]) -> Result<ArrayRef> {
Expand Down
10 changes: 5 additions & 5 deletions datafusion/proto/proto/datafusion.proto
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@ enum ScalarFunction {
// 12 was Log10
// 13 was Log2
Round = 14;
Signum = 15;
Sin = 16;
Sqrt = 17;
// 15 was Signum
// 16 was Sin
// 17 was Sqrt
// Tan = 18;
Trunc = 19;
// 20 was Array
Expand Down Expand Up @@ -618,12 +618,12 @@ enum ScalarFunction {
// 74 Acosh
// 75 was Asinh
// 76 was Atanh
Sinh = 77;
// 77 was Sinh
Cosh = 78;
// Tanh = 79;
Pi = 80;
Degrees = 81;
Radians = 82;
// 82 was Radians
Factorial = 83;
Lcm = 84;
Gcd = 85;
Expand Down
15 changes: 0 additions & 15 deletions datafusion/proto/src/generated/pbjson.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 5 additions & 15 deletions datafusion/proto/src/generated/prost.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f5805b7

Please sign in to comment.