andygrove commented on code in PR #23471:
URL: https://github.com/apache/datafusion/pull/23471#discussion_r3574014085
##########
datafusion/functions/src/math/round.rs:
##########
@@ -859,15 +845,84 @@ fn round_integer_array(
}
}
-fn round_float<T>(value: T, decimal_places: i32) -> Result<T, ArrowError>
+/// Rounds a float array to `decimal_places`, taking the hoisted-factor fast
+/// path when it applies and falling back to the shared binary-math kernel
+/// otherwise.
+fn round_float_column<PT>(
+ value_array: &ArrayRef,
+ decimal_places: &ColumnarValue,
+) -> Result<ArrayRef>
where
- T: num_traits::Float,
+ PT: ArrowPrimitiveType,
+ PT::Native: num_traits::Float,
{
- let factor = T::from(10_f64.powi(decimal_places)).ok_or_else(|| {
+ if let Some(arr) = round_float_fast::<PT>(value_array, decimal_places)? {
+ return Ok(arr);
+ }
+ let result = calculate_binary_math::<PT, Int32Type, PT, _>(
+ value_array.as_ref(),
+ decimal_places,
+ round_float::<PT::Native>,
+ )?;
+ Ok(result as _)
+}
+
+/// Fast path for rounding a float array to a scalar number of
`decimal_places`.
+///
+/// The shared `calculate_binary_math` kernel routes through `try_unary` and
+/// re-evaluates `round_float` (including `10f64.powi(decimal_places)` and a
+/// `Result` check) for every element. When `decimal_places` is a non-null
+/// scalar and the value column has no nulls, we can hoist the scaling factor
+/// out of the loop and use the infallible `unary` kernel instead, which the
+/// compiler can autovectorize. Requiring no null slots keeps the output
+/// bit-identical to the fallible kernel: `unary` writes a computed value into
+/// every slot while `try_unary` leaves null slots zeroed, so the two only
agree
+/// when there are no null slots. Returns `Ok(None)` when the fast path does
not
Review Comment:
Good point. Thanks, I fixed that.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]