-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
functions: support trunc() function with one or two args #6942
Conversation
can describe the issue? |
see the relationale for this change |
THanks @Syleechan -- I plan to review this over the next day or two! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the contribution @Syleechan
I left some style suggestions, but overall I think this PR is close
The only thing I think is needed to merge is some SQL level tests -- perhaps you could add some sqllogic tests (readme is here)
https://github.com/apache/arrow-datafusion/blob/9338880d8f64f8143e348a60beee8af2789fa8ae/datafusion/core/tests/sqllogictests/test_files/scalar.slt#L892-L914 along side the 1 argument version
Also, I think it would be good to include a test of calling trunc(1, 2, 3)
with more than two arguments and ensure we get a Planning or Execution error rather than an InternalError. Internal errors signal a bug in DataFusion
datafusion/expr/src/expr_fn.rs
Outdated
@@ -501,7 +501,7 @@ scalar_expr!( | |||
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"); | |||
scalar_expr!(Trunc, trunc, num, "truncate toward zero"); | |||
nary_scalar_expr!(Trunc, trunc, "truncate toward to precision"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this function truncates toward precision
-- instead it truncates towards zero with an optional precision
nary_scalar_expr!(Trunc, trunc, "truncate toward to precision"); | |
nary_scalar_expr!(Trunc, trunc, "truncate toward zero, with optional precision"); |
let mut _precision = ColumnarValue::Scalar(Int32(Some(0))); | ||
|
||
let num = &args[0]; | ||
if args.len() == 2 { | ||
_precision = ColumnarValue::Array(args[1].clone()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of _
as a prefix of a variable is used to tell the compiler it can be unused. In this case I think it is used and thus the variable should be named precision
A more "idiomatic" rust way (and avoiding the mut
) of writing this might be
let mut _precision = ColumnarValue::Scalar(Int32(Some(0))); | |
let num = &args[0]; | |
if args.len() == 2 { | |
_precision = ColumnarValue::Array(args[1].clone()); | |
} | |
let precision = if args.len() == 1 { | |
ColumnarValue::Scalar(Int32(Some(0))) | |
} else { | |
ColumnarValue::Array(args[1].clone()) | |
}; |
} | ||
|
||
fn compute_truncate32(x: f32, y: usize) -> f32 { | ||
let s = format!("{:.precision$}", x, precision = y); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can probably make this quite a bit more efficient by avoiding strings, for example something like (untested):
let factor = 10.0f32.pow(precision);
(x * factor).round() / factor;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
# Conflicts: # datafusion/physical-expr/src/math_expressions.rs
thanks for your suggestion.Now I try with sql level test.But it shows with the message:
I am not familiar with the sql test function, so can you tell me how to deal with the error. @alamb |
I can help, but probably not until tomorrow. I don't think the error you are seeing is related to the testing function, it is related somehow to how the function is registered with datafusion |
thanks for your help. |
I have found the issue and fixed it, please help to review again, thanks @alamb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me @Syleechan -- thank you very much.
@@ -1072,6 +1072,15 @@ impl BuiltinScalarFunction { | |||
], | |||
self.volatility(), | |||
), | |||
BuiltinScalarFunction::Trunc => Signature::one_of( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
here is a follow on PR #7042 with test and doc updates |
Which issue does this PR close?
Closes #.
Rationale for this change
before change the trunc function only with one arg and truncate the number after the decimal point
after change the trunc function with two args, and can truncate the decimal customarily, see the detail from the link below
https://www.postgresql.org/docs/current/functions-math.html#:~:text=trunc%20(%20numeric,2)%20%E2%86%92%2042.43
What changes are included in this PR?
Are these changes tested?
Are there any user-facing changes?