liamzwbao commented on code in PR #8552:
URL: https://github.com/apache/arrow-rs/pull/8552#discussion_r2418191364


##########
arrow-cast/src/cast/decimal.rs:
##########
@@ -174,55 +307,20 @@ where
     I::Native: DecimalCast + ArrowNativeTypeOp,
     O::Native: DecimalCast + ArrowNativeTypeOp,
 {
-    let error = cast_decimal_to_decimal_error::<I, O>(output_precision, 
output_scale);
-    let delta_scale = input_scale - output_scale;
-    // if the reduction of the input number through scaling (dividing) is 
greater
-    // than a possible precision loss (plus potential increase via rounding)
-    // every input number will fit into the output type
-    // Example: If we are starting with any number of precision 5 [xxxxx],
-    // then and decrease the scale by 3 will have the following effect on the 
representation:
-    // [xxxxx] -> [xx] (+ 1 possibly, due to rounding).
-    // The rounding may add an additional digit, so the cast to be infallible,
-    // the output type needs to have at least 3 digits of precision.
-    // e.g. Decimal(5, 3) 99.999 to Decimal(3, 0) will result in 100:
-    // [99999] -> [99] + 1 = [100], a cast to Decimal(2, 0) would not be 
possible
-    let is_infallible_cast = (input_precision as i8) - delta_scale < 
(output_precision as i8);
-
-    let div = I::Native::from_decimal(10_i128)
-        .unwrap()
-        .pow_checked(delta_scale as u32)?;
-
-    let half = div.div_wrapping(I::Native::from_usize(2).unwrap());
-    let half_neg = half.neg_wrapping();
-
-    let f = |x: I::Native| {
-        // div is >= 10 and so this cannot overflow
-        let d = x.div_wrapping(div);
-        let r = x.mod_wrapping(div);
+    // make sure we don't perform calculations that don't make sense w/o 
validation
+    validate_decimal_precision_and_scale::<O>(output_precision, output_scale)?;

Review Comment:
   I think this could be an improvement since it validates the output precision 
and scale before performing operations on the array. However, these checks 
don’t affect correctness, because the same validation is performed again when 
creating the output decimal array 
[here](https://github.com/apache/arrow-rs/blob/main/arrow-cast/src/cast/decimal.rs#L353-L356).
 
   
   The main benefit is that it can fail early on invalid operations and avoid 
unnecessary operation on array, but it does add some overhead for valid 
operations since the conditions are checked twice.
   
   So to be consistent, I think we should either add or remove this check 
across all branches.



-- 
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]

Reply via email to