From 87c9db437a530e246528e72d4af902b78ce2623e Mon Sep 17 00:00:00 2001 From: Liang-Chi Hsieh Date: Fri, 28 Oct 2022 21:06:47 -0700 Subject: [PATCH] Use unary (#2973) --- arrow/src/compute/kernels/cast.rs | 61 +++++++------------------------ 1 file changed, 13 insertions(+), 48 deletions(-) diff --git a/arrow/src/compute/kernels/cast.rs b/arrow/src/compute/kernels/cast.rs index 73868dd98c06..3e55791502c2 100644 --- a/arrow/src/compute/kernels/cast.rs +++ b/arrow/src/compute/kernels/cast.rs @@ -41,8 +41,8 @@ use std::sync::Arc; use crate::buffer::MutableBuffer; use crate::compute::kernels::cast_utils::string_to_timestamp_nanos; -use crate::compute::try_unary; use crate::compute::{divide_scalar, multiply_scalar}; +use crate::compute::{try_unary, unary}; use crate::datatypes::*; use crate::error::{ArrowError, Result}; use crate::temporal_conversions::{ @@ -306,44 +306,6 @@ pub fn cast(array: &ArrayRef, to_type: &DataType) -> Result { cast_with_options(array, to_type, &DEFAULT_CAST_OPTIONS) } -/// Cast the primitive array to defined decimal128 data type array -fn cast_primitive_to_decimal128( - array: T, - op: F, - precision: u8, - scale: u8, -) -> Result -where - F: Fn(T::Item) -> i128, -{ - #[allow(clippy::redundant_closure)] - let decimal_array = ArrayIter::new(array) - .map(|v| v.map(|v| op(v))) - .collect::() - .with_precision_and_scale(precision, scale)?; - - Ok(Arc::new(decimal_array)) -} - -/// Cast the primitive array to defined decimal256 data type array -fn cast_primitive_to_decimal256( - array: T, - op: F, - precision: u8, - scale: u8, -) -> Result -where - F: Fn(T::Item) -> i256, -{ - #[allow(clippy::redundant_closure)] - let decimal_array = ArrayIter::new(array) - .map(|v| v.map(|v| op(v))) - .collect::() - .with_precision_and_scale(precision, scale)?; - - Ok(Arc::new(decimal_array)) -} - fn cast_integer_to_decimal128( array: &PrimitiveArray, precision: u8, @@ -354,7 +316,9 @@ where { let mul: i128 = 10_i128.pow(scale as u32); - cast_primitive_to_decimal128(array, |v| v.as_() * mul, precision, scale) + unary::(array, |v| v.as_() * mul) + .with_precision_and_scale(precision, scale) + .map(|a| Arc::new(a) as ArrayRef) } fn cast_integer_to_decimal256( @@ -374,7 +338,9 @@ where )) })?; - cast_primitive_to_decimal256(array, |v| v.as_().wrapping_mul(mul), precision, scale) + unary::(array, |v| v.as_().wrapping_mul(mul)) + .with_precision_and_scale(precision, scale) + .map(|a| Arc::new(a) as ArrayRef) } fn cast_floating_point_to_decimal128( @@ -387,7 +353,9 @@ where { let mul = 10_f64.powi(scale as i32); - cast_primitive_to_decimal128(array, |v| (v.as_() * mul) as i128, precision, scale) + unary::(array, |v| (v.as_() * mul) as i128) + .with_precision_and_scale(precision, scale) + .map(|a| Arc::new(a) as ArrayRef) } fn cast_floating_point_to_decimal256( @@ -400,12 +368,9 @@ where { let mul = 10_f64.powi(scale as i32); - cast_primitive_to_decimal256( - array, - |v| i256::from_i128((v.as_() * mul) as i128), - precision, - scale, - ) + unary::(array, |v| i256::from_i128((v.as_() * mul) as i128)) + .with_precision_and_scale(precision, scale) + .map(|a| Arc::new(a) as ArrayRef) } /// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]