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


##########
parquet-variant-compute/src/type_conversion.rs:
##########
@@ -239,44 +247,22 @@ where
             false if r <= half_neg => d.sub_wrapping(I::Native::ONE),
             _ => d,
         };
-        O::Native::from_decimal(adjusted)
-    };
 
-    scaled.filter(|v| is_infallible_cast || O::is_valid_decimal_precision(*v, 
output_precision))
-}
-
-/// Returns true if casting from (input_precision, input_scale) to
-/// (output_precision, output_scale) is infallible based on precision/scale 
math.
-fn is_infallible_decimal_cast(
-    input_precision: u8,
-    input_scale: i8,
-    output_precision: u8,
-    output_scale: i8,
-) -> bool {
-    let delta_scale = output_scale - input_scale;
-    let input_precision = input_precision as i8;
-    let output_precision = output_precision as i8;
-    if delta_scale >= 0 {
-        // 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
-        input_precision + delta_scale <= output_precision
-    } else {
         // 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 for the cast to be 
infallible,
+        // The rounding may add a digit, so for 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
-        input_precision + delta_scale < output_precision
-    }
+        let is_infallible_cast = input_precision as i8 + delta_scale < 
output_precision as i8;
+        (O::Native::from_decimal(adjusted), is_infallible_cast)
+    };
+
+    scaled.filter(|v| is_infallible_cast || O::is_valid_decimal_precision(*v, 
output_precision))

Review Comment:
   To make it simple, how about this
   ```rs
       if is_infallible_cast {
           scaled
       } else {
           scaled.filter(|v| O::is_valid_decimal_precision(*v, 
output_precision))
       }
   ```



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