Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor some decimal-related code and tests #7062

Merged
merged 6 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 20 additions & 55 deletions arrow-cast/src/cast/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub(crate) trait DecimalCast: Sized {
fn to_i256(self) -> Option<i256>;

fn from_decimal<T: DecimalCast>(n: T) -> Option<Self>;

fn from_f64(n: f64) -> Option<Self>;
}

impl DecimalCast for i128 {
Expand All @@ -39,6 +41,10 @@ impl DecimalCast for i128 {
fn from_decimal<T: DecimalCast>(n: T) -> Option<Self> {
n.to_i128()
}

fn from_f64(n: f64) -> Option<Self> {
n.to_i128()
}
}

impl DecimalCast for i256 {
Expand All @@ -53,6 +59,10 @@ impl DecimalCast for i256 {
fn from_decimal<T: DecimalCast>(n: T) -> Option<Self> {
n.to_i256()
}

fn from_f64(n: f64) -> Option<Self> {
i256::from_f64(n)
}
}

pub(crate) fn cast_decimal_to_decimal_error<I, O>(
Expand Down Expand Up @@ -463,86 +473,41 @@ where
Ok(Arc::new(result))
}

pub(crate) fn cast_floating_point_to_decimal128<T: ArrowPrimitiveType>(
array: &PrimitiveArray<T>,
precision: u8,
scale: i8,
cast_options: &CastOptions,
) -> Result<ArrayRef, ArrowError>
where
<T as ArrowPrimitiveType>::Native: AsPrimitive<f64>,
{
let mul = 10_f64.powi(scale as i32);

if cast_options.safe {
array
.unary_opt::<_, Decimal128Type>(|v| {
(mul * v.as_())
.round()
.to_i128()
.filter(|v| Decimal128Type::is_valid_decimal_precision(*v, precision))
})
.with_precision_and_scale(precision, scale)
.map(|a| Arc::new(a) as ArrayRef)
} else {
array
.try_unary::<_, Decimal128Type, _>(|v| {
(mul * v.as_())
.round()
.to_i128()
.ok_or_else(|| {
ArrowError::CastError(format!(
"Cannot cast to {}({}, {}). Overflowing on {:?}",
Decimal128Type::PREFIX,
precision,
scale,
v
))
})
.and_then(|v| {
Decimal128Type::validate_decimal_precision(v, precision).map(|_| v)
})
})?
.with_precision_and_scale(precision, scale)
.map(|a| Arc::new(a) as ArrayRef)
}
}

pub(crate) fn cast_floating_point_to_decimal256<T: ArrowPrimitiveType>(
pub(crate) fn cast_floating_point_to_decimal<T: ArrowPrimitiveType, D>(
array: &PrimitiveArray<T>,
precision: u8,
scale: i8,
cast_options: &CastOptions,
) -> Result<ArrayRef, ArrowError>
where
<T as ArrowPrimitiveType>::Native: AsPrimitive<f64>,
D: DecimalType + ArrowPrimitiveType,
<D as ArrowPrimitiveType>::Native: DecimalCast,
{
let mul = 10_f64.powi(scale as i32);

if cast_options.safe {
array
.unary_opt::<_, Decimal256Type>(|v| {
i256::from_f64((v.as_() * mul).round())
.filter(|v| Decimal256Type::is_valid_decimal_precision(*v, precision))
.unary_opt::<_, D>(|v| {
D::Native::from_f64((mul * v.as_()).round())
.filter(|v| D::is_valid_decimal_precision(*v, precision))
})
.with_precision_and_scale(precision, scale)
.map(|a| Arc::new(a) as ArrayRef)
} else {
array
.try_unary::<_, Decimal256Type, _>(|v| {
i256::from_f64((v.as_() * mul).round())
.try_unary::<_, D, _>(|v| {
D::Native::from_f64((mul * v.as_()).round())
.ok_or_else(|| {
ArrowError::CastError(format!(
"Cannot cast to {}({}, {}). Overflowing on {:?}",
Decimal256Type::PREFIX,
D::PREFIX,
precision,
scale,
v
))
})
.and_then(|v| {
Decimal256Type::validate_decimal_precision(v, precision).map(|_| v)
})
.and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
})?
.with_precision_and_scale(precision, scale)
.map(|a| Arc::new(a) as ArrayRef)
Expand Down
88 changes: 44 additions & 44 deletions arrow-cast/src/cast/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,50 +214,20 @@ pub(crate) fn cast_to_dictionary<K: ArrowDictionaryKeyType>(
UInt16 => pack_numeric_to_dictionary::<K, UInt16Type>(array, dict_value_type, cast_options),
UInt32 => pack_numeric_to_dictionary::<K, UInt32Type>(array, dict_value_type, cast_options),
UInt64 => pack_numeric_to_dictionary::<K, UInt64Type>(array, dict_value_type, cast_options),
Decimal128(p, s) => {
let dict = pack_numeric_to_dictionary::<K, Decimal128Type>(
array,
dict_value_type,
cast_options,
)?;
let dict = dict
.as_dictionary::<K>()
.downcast_dict::<Decimal128Array>()
.ok_or_else(|| {
ArrowError::ComputeError(
"Internal Error: Cannot cast dict to Decimal128Array".to_string(),
)
})?;
let value = dict.values().clone();
// Set correct precision/scale
let value = value.with_precision_and_scale(p, s)?;
Ok(Arc::new(DictionaryArray::<K>::try_new(
dict.keys().clone(),
Arc::new(value),
)?))
}
Decimal256(p, s) => {
let dict = pack_numeric_to_dictionary::<K, Decimal256Type>(
array,
dict_value_type,
cast_options,
)?;
let dict = dict
.as_dictionary::<K>()
.downcast_dict::<Decimal256Array>()
.ok_or_else(|| {
ArrowError::ComputeError(
"Internal Error: Cannot cast dict to Decimal256Array".to_string(),
)
})?;
let value = dict.values().clone();
// Set correct precision/scale
let value = value.with_precision_and_scale(p, s)?;
Ok(Arc::new(DictionaryArray::<K>::try_new(
dict.keys().clone(),
Arc::new(value),
)?))
}
Decimal128(p, s) => pack_decimal_to_dictionary::<K, Decimal128Type>(
array,
dict_value_type,
p,
s,
cast_options,
),
Decimal256(p, s) => pack_decimal_to_dictionary::<K, Decimal256Type>(
array,
dict_value_type,
p,
s,
cast_options,
),
Float16 => {
pack_numeric_to_dictionary::<K, Float16Type>(array, dict_value_type, cast_options)
}
Expand Down Expand Up @@ -359,6 +329,36 @@ where
Ok(Arc::new(b.finish()))
}

pub(crate) fn pack_decimal_to_dictionary<K, D>(
array: &dyn Array,
dict_value_type: &DataType,
precision: u8,
scale: i8,
cast_options: &CastOptions,
) -> Result<ArrayRef, ArrowError>
where
K: ArrowDictionaryKeyType,
D: DecimalType + ArrowPrimitiveType,
{
let dict = pack_numeric_to_dictionary::<K, D>(array, dict_value_type, cast_options)?;
let dict = dict
.as_dictionary::<K>()
.downcast_dict::<PrimitiveArray<D>>()
.ok_or_else(|| {
ArrowError::ComputeError(format!(
"Internal Error: Cannot cast dict to {}Array",
D::PREFIX
))
})?;
let value = dict.values().clone();
// Set correct precision/scale
let value = value.with_precision_and_scale(precision, scale)?;
Ok(Arc::new(DictionaryArray::<K>::try_new(
dict.keys().clone(),
Arc::new(value),
)?))
}

pub(crate) fn string_view_to_dictionary<K, O: OffsetSizeTrait>(
array: &dyn Array,
) -> Result<ArrayRef, ArrowError>
Expand Down
Loading
Loading