You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/04/14 18:22:20 UTC

[GitHub] [arrow-rs] alamb commented on a diff in pull request #4088: optimize cast for same decimal type and same scale

alamb commented on code in PR #4088:
URL: https://github.com/apache/arrow-rs/pull/4088#discussion_r1167159839


##########
arrow-cast/src/cast.rs:
##########
@@ -2081,29 +2081,102 @@ impl DecimalCast for i256 {
     }
 }
 
-fn cast_decimal_to_decimal<I, O>(
-    array: &PrimitiveArray<I>,
-    input_scale: i8,
+fn cast_decimal_to_decimal_error<O>(
     output_precision: u8,
     output_scale: i8,
-    cast_options: &CastOptions,
-) -> Result<ArrayRef, ArrowError>
+) -> impl Fn(<O as ArrowPrimitiveType>::Native) -> ArrowError
 where
-    I: DecimalType,
     O: DecimalType,
-    I::Native: DecimalCast + ArrowNativeTypeOp,
     O::Native: DecimalCast + ArrowNativeTypeOp,
 {
-    let error = |x| {
+    move |x: O::Native| {
         ArrowError::CastError(format!(
             "Cannot cast to {}({}, {}). Overflowing on {:?}",
             O::PREFIX,
             output_precision,
             output_scale,
             x
         ))
+    }
+}
+
+fn cast_decimal_to_decimal_same_type<T>(

Review Comment:
   Could you please add some comments here about what the difference between `cast_decimal_to_decimal_same_type` and `cast_decimal_to_decimal` are, especially given their arguments appear to be the the same. 
   



##########
arrow-cast/src/cast.rs:
##########
@@ -2081,29 +2081,102 @@ impl DecimalCast for i256 {
     }
 }
 
-fn cast_decimal_to_decimal<I, O>(
-    array: &PrimitiveArray<I>,
-    input_scale: i8,
+fn cast_decimal_to_decimal_error<O>(
     output_precision: u8,
     output_scale: i8,
-    cast_options: &CastOptions,
-) -> Result<ArrayRef, ArrowError>
+) -> impl Fn(<O as ArrowPrimitiveType>::Native) -> ArrowError
 where
-    I: DecimalType,
     O: DecimalType,
-    I::Native: DecimalCast + ArrowNativeTypeOp,
     O::Native: DecimalCast + ArrowNativeTypeOp,
 {
-    let error = |x| {
+    move |x: O::Native| {
         ArrowError::CastError(format!(
             "Cannot cast to {}({}, {}). Overflowing on {:?}",
             O::PREFIX,
             output_precision,
             output_scale,
             x
         ))
+    }
+}
+
+fn cast_decimal_to_decimal_same_type<T>(
+    array: &PrimitiveArray<T>,
+    input_scale: i8,
+    output_precision: u8,
+    output_scale: i8,
+    cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError>
+where
+    T: DecimalType,
+    T::Native: DecimalCast + ArrowNativeTypeOp,
+{
+    let error = cast_decimal_to_decimal_error::<T>(output_precision, output_scale);
+
+    let array: PrimitiveArray<T> = if input_scale == output_scale {
+        // the scale doesn't change, the native value don't need to be changed
+        array.clone()
+    } else if input_scale > output_scale {

Review Comment:
   this code and below looks very similar to what is in `cast_decimal_to_decimal`
   
   Is there any way they can be combined to reduce the maintenance burden longer term?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org