perf: Optimize trunc scalar performance#19788
Conversation
| match &args.args[1] { | ||
| ColumnarValue::Scalar(Int64(Some(p))) => *p, | ||
| ColumnarValue::Scalar(Int64(None)) => { | ||
| return Ok(ColumnarValue::Scalar(ScalarValue::Float64(None))); |
There was a problem hiding this comment.
I think this should check the scalar type to decide whether to return Float64 or Float32
|
|
||
| fn compute_truncate32(x: f32, y: i64) -> f32 { | ||
| let factor = 10.0_f32.powi(y as i32); | ||
| (x * factor).round() / factor |
There was a problem hiding this comment.
Not introduced in this PR but why f32::round() is used here instead of f32::trunc() ?
Same for f64 below.
fn main() {
let factor = 10_f64;
let r = (3.76_f64 * factor).round() / factor;
let t = (3.76_f64 * factor).trunc() / factor;
println!("round: {r}\ntrunc: {t}");
}prints:
round: 3.8
trunc: 3.7
There was a problem hiding this comment.
Yeah it does seem like a bug. I will file an issue
| }; | ||
|
|
||
| match scalar { | ||
| ScalarValue::Float64(v) => { |
There was a problem hiding this comment.
Fast path for ScalarValue::Null too ?!
| ScalarValue::Float64(v) => { | ||
| let result = v.map(|x| { | ||
| if precision == 0 { | ||
| if x == 0.0 { 0.0 } else { x.trunc() } |
There was a problem hiding this comment.
| if x == 0.0 { 0.0 } else { x.trunc() } | |
| x.trunc() |
| ScalarValue::Float32(v) => { | ||
| let result = v.map(|x| { | ||
| if precision == 0 { | ||
| if x == 0.0 { 0.0 } else { x.trunc() } |
There was a problem hiding this comment.
| if x == 0.0 { 0.0 } else { x.trunc() } | |
| x.trunc() |
| )]; | ||
| let scalar_arg_fields = vec![Field::new("a", DataType::Float64, false).into()]; | ||
| let scalar_return_field = Field::new("f", DataType::Float64, false).into(); | ||
| let config_options = Arc::new(ConfigOptions::default()); |
There was a problem hiding this comment.
nit: This variable shadows the same one from line 40
|
Thanks for the feedback @martin-g, incorporated the changes. |
| return make_scalar_function(trunc, vec![])(&args.args); | ||
| } | ||
| None => Some(0), // default precision | ||
| _ => Some(0), |
There was a problem hiding this comment.
This catch all arm should return an internal error, unless theres a case I'm missing?
There was a problem hiding this comment.
Yes it should. Made changes
Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
|
Thanks @kumarUjjawal & @martin-g |
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Part of apache/datafusion-comet#2986. ## Rationale for this change The current `trunc` implementation always converts scalar inputs to arrays via `make_scalar_function`, which introduces unnecessary overhead when processing single values. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - Add scalar fast path for `trunc` function to process Float32/Float64 scalar inputs directly - Handle optional precision argument for scalar inputs - Add scalar benchmarks to measure performance <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Yes all sqllogictest pass ## Benchmark Results | Type | Before | After | Speedup | |------|--------|-------|---------| | f64 scalar | 256 ns | 55 ns | **4.6x** | | f32 scalar | 247 ns | 56 ns | **4.4x** | <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? No <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
Which issue does this PR close?
Rationale for this change
The current
truncimplementation always converts scalar inputs to arrays viamake_scalar_function, which introduces unnecessary overhead when processing single values.What changes are included in this PR?
truncfunction to process Float32/Float64 scalar inputs directlyAre these changes tested?
Yes all sqllogictest pass
Benchmark Results
Are there any user-facing changes?
No