liukun4515 commented on code in PR #4088:
URL: https://github.com/apache/arrow-rs/pull/4088#discussion_r1169498040
##########
arrow-cast/src/cast.rs:
##########
@@ -2081,66 +2081,166 @@ impl DecimalCast for i256 {
}
}
-fn cast_decimal_to_decimal<I, O>(
- array: &PrimitiveArray<I>,
- input_scale: i8,
+fn cast_decimal_to_decimal_error<I, O>(
output_precision: u8,
output_scale: i8,
- cast_options: &CastOptions,
-) -> Result<ArrayRef, ArrowError>
+) -> impl Fn(<I as ArrowPrimitiveType>::Native) -> ArrowError
where
I: DecimalType,
O: DecimalType,
I::Native: DecimalCast + ArrowNativeTypeOp,
O::Native: DecimalCast + ArrowNativeTypeOp,
{
- let error = |x| {
+ move |x: I::Native| {
ArrowError::CastError(format!(
"Cannot cast to {}({}, {}). Overflowing on {:?}",
O::PREFIX,
output_precision,
output_scale,
x
))
- };
+ }
+}
- let array: PrimitiveArray<O> = if input_scale > output_scale {
- let div = I::Native::from_decimal(10_i128)
- .unwrap()
- .pow_checked((input_scale - output_scale) as u32)?;
+fn convert_to_smaller_equal_scale_decimal<I, O>(
+ array: &PrimitiveArray<I>,
+ input_scale: i8,
+ output_precision: u8,
+ output_scale: i8,
+ cast_options: &CastOptions,
+) -> Result<PrimitiveArray<O>, ArrowError>
+where
+ I: DecimalType,
+ O: DecimalType,
+ I::Native: DecimalCast + ArrowNativeTypeOp,
+ O::Native: DecimalCast + ArrowNativeTypeOp,
+{
+ let error = cast_decimal_to_decimal_error::<I, O>(output_precision,
output_scale);
+ let div = I::Native::from_decimal(10_i128)
+ .unwrap()
+ .pow_checked((input_scale - output_scale) as u32)?;
- let half = div.div_wrapping(I::Native::from_usize(2).unwrap());
- let half_neg = half.neg_wrapping();
+ 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);
+ 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);
- // Round result
- let adjusted = match x >= I::Native::ZERO {
- true if r >= half => d.add_wrapping(I::Native::ONE),
- false if r <= half_neg => d.sub_wrapping(I::Native::ONE),
- _ => d,
- };
- O::Native::from_decimal(adjusted)
+ // Round result
+ let adjusted = match x >= I::Native::ZERO {
+ true if r >= half => d.add_wrapping(I::Native::ONE),
+ false if r <= half_neg => d.sub_wrapping(I::Native::ONE),
+ _ => d,
};
+ O::Native::from_decimal(adjusted)
+ };
- match cast_options.safe {
- true => array.unary_opt(f),
- false => array.try_unary(|x| f(x).ok_or_else(|| error(x)))?,
- }
- } else {
- let mul = O::Native::from_decimal(10_i128)
- .unwrap()
- .pow_checked((output_scale - input_scale) as u32)?;
+ Ok(match cast_options.safe {
+ true => array.unary_opt(f),
+ false => array.try_unary(|x| f(x).ok_or_else(|| error(x)))?,
+ })
+}
- let f = |x| O::Native::from_decimal(x).and_then(|x|
x.mul_checked(mul).ok());
+fn convert_to_bigger_scale_decimal<I, O>(
+ array: &PrimitiveArray<I>,
+ input_scale: i8,
+ output_precision: u8,
+ output_scale: i8,
+ cast_options: &CastOptions,
+) -> Result<PrimitiveArray<O>, ArrowError>
+where
+ I: DecimalType,
+ O: DecimalType,
+ I::Native: DecimalCast + ArrowNativeTypeOp,
+ O::Native: DecimalCast + ArrowNativeTypeOp,
+{
+ let error = cast_decimal_to_decimal_error::<I, O>(output_precision,
output_scale);
+ let mul = O::Native::from_decimal(10_i128)
+ .unwrap()
+ .pow_checked((output_scale - input_scale) as u32)?;
- match cast_options.safe {
- true => array.unary_opt(f),
- false => array.try_unary(|x| f(x).ok_or_else(|| error(x)))?,
+ let f = |x| O::Native::from_decimal(x).and_then(|x|
x.mul_checked(mul).ok());
+
+ Ok(match cast_options.safe {
+ true => array.unary_opt(f),
+ false => array.try_unary(|x| f(x).ok_or_else(|| error(x)))?,
+ })
+}
+
+// Only support one type of decimal cast operations
+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 array: PrimitiveArray<T> = match input_scale.cmp(&output_scale) {
+ Ordering::Equal => {
+ // the scale doesn't change, the native value don't need to be
changed
+ array.clone()
}
+ Ordering::Greater => convert_to_smaller_equal_scale_decimal::<T, T>(
Review Comment:
`convert_to_smaller_equal_scale_decimal` can do the casting for different
input and output decimal type with smaller or equal scale.
But it is not efficient for same input and output type with the same scale.
--
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]