davidlghellin commented on code in PR #10509:
URL: https://github.com/apache/arrow-rs/pull/10509#discussion_r4191661159
##########
arrow-cast/src/cast/mod.rs:
##########
@@ -93,7 +101,127 @@ where
D: DecimalType,
F: Fn(D::Native) -> f64,
{
- f(x) / 10_f64.powi(scale)
+ let unscaled = f(x);
+ // Both operands are exact in this range, so this rounds once. A negative
scale
+ // multiplies, since `10^-scale` is the exact power of ten there.
+ if (-22..=22).contains(&scale) && unscaled.abs() < F64_EXACT_INT_LIMIT {
+ return if scale >= 0 {
+ unscaled / 10_f64.powi(scale)
+ } else {
+ unscaled * 10_f64.powi(-scale)
+ };
+ }
+ match i8::try_from(scale) {
+ Ok(scale) => decimal_to_f64_rounded_once::<D>(x, scale, unscaled),
+ Err(_) => unscaled / 10_f64.powi(scale),
+ }
+}
+
+/// Rounds once for the values the arithmetic cannot convert exactly, by
parsing
+/// the decimal's own text.
+#[cold]
+#[inline(never)]
+fn decimal_to_f64_rounded_once<D: DecimalType>(x: D::Native, scale: i8,
unscaled: f64) -> f64 {
+ // `format_decimal` always writes a decimal literal, so the parse cannot
fail.
+ // The fallback is there to keep that from being a panic, not because it
runs.
+ D::format_decimal(x, u8::MAX, scale)
+ .parse::<f64>()
+ .unwrap_or_else(|_| unscaled / 10_f64.powi(scale.into()))
+}
+
+/// As [`decimal_to_f64_rounded_once`], but narrowing to `f32` in one step.
+/// Rounding to `f64` first and then to `f32` rounds twice: a decimal just
above an
+/// `f32` midpoint can collapse onto that midpoint in `f64`, and
round-half-even
+/// then sends it the wrong way.
+#[cold]
+#[inline(never)]
+fn decimal_to_f32_rounded_once<D: DecimalType>(x: D::Native, scale: i8,
unscaled: f64) -> f32 {
+ // Unreachable, as in [`decimal_to_f64_rounded_once`].
+ D::format_decimal(x, u8::MAX, scale)
+ .parse::<f32>()
+ .unwrap_or_else(|_| unscaled as f32 / 10_f32.powi(scale.into()))
+}
+
+/// Casts a decimal array to `Float64`, rounding each value once.
+fn cast_decimal_to_f64<D, F>(
+ array: &dyn Array,
+ as_float: &F,
+ scale: i8,
+) -> Result<ArrayRef, ArrowError>
+where
+ D: DecimalType + ArrowPrimitiveType,
+ F: Fn(D::Native) -> f64,
+{
+ let array = array.as_primitive::<D>();
+ // No power of ten outside this range is exactly representable.
+ if !(-22..=22).contains(&scale) {
+ let values = array
+ .unary::<_, Float64Type>(|x| decimal_to_f64_rounded_once::<D>(x,
scale, as_float(x)));
+ return Ok(Arc::new(values));
+ }
+ let pow = 10_f64.powi(scale.unsigned_abs().into());
+ let values = if scale >= 0 {
+ array.unary::<_, Float64Type>(|x| {
+ let unscaled = as_float(x);
+ if unscaled.abs() < F64_EXACT_INT_LIMIT {
Review Comment:
I prefer to merge the fix now and do the performance work in a followup, so
the bug gets fixed first.
Note for the followup: checking only precision/scale is not safe, values are
not validated against it.
--
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]