himadripal commented on code in PR #7021:
URL: https://github.com/apache/arrow-rs/pull/7021#discussion_r1949606637


##########
arrow-cast/src/cast/decimal.rs:
##########
@@ -144,14 +164,26 @@ where
     I::Native: DecimalCast + ArrowNativeTypeOp,
     O::Native: DecimalCast + ArrowNativeTypeOp,
 {
+    validate_decimal_precision_and_scale::<O>(output_precision, output_scale)?;
     let error = cast_decimal_to_decimal_error::<I, O>(output_precision, 
output_scale);
+    let delta_scale = output_scale - input_scale;
     let mul = O::Native::from_decimal(10_i128)
         .unwrap()
-        .pow_checked((output_scale - input_scale) as u32)?;
-
+        .pow_checked(delta_scale as u32)?;
+
+    // if the gain in precision (digits) is greater than the multiplication 
due to scaling
+    // every number will fit into the output type
+    // Example: If we are starting with any number of precision 5 [xxxxx],
+    // then an increase of scale by 3 will have the following effect on the 
representation:
+    // [xxxxx] -> [xxxxx000], so for the cast to be infallible, the output type
+    // needs to provide at least 8 digits precision
+    let is_infallible_cast = (input_precision as i8) + delta_scale <= 
(output_precision as i8);
     let f = |x| O::Native::from_decimal(x).and_then(|x| 
x.mul_checked(mul).ok());
 
-    Ok(if cast_options.safe {
+    Ok(if is_infallible_cast {

Review Comment:
   may be add a comment like above, `unwrapping is safe as the result will fit`



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