Skip to content

Commit

Permalink
Apply PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
CurtHagenlocher committed Feb 6, 2025
1 parent 27cb5d2 commit b9fb40f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
16 changes: 8 additions & 8 deletions arrow-cast/src/cast/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) trait DecimalCast: Sized {

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

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

impl DecimalCast for i128 {
Expand All @@ -42,7 +42,7 @@ impl DecimalCast for i128 {
n.to_i128()
}

fn from_f64<T: DecimalCast>(n: f64) -> Option<Self> {
fn from_f64(n: f64) -> Option<Self> {
n.to_i128()
}
}
Expand All @@ -60,7 +60,7 @@ impl DecimalCast for i256 {
n.to_i256()
}

fn from_f64<T: DecimalCast>(n: f64) -> Option<Self> {
fn from_f64(n: f64) -> Option<Self> {
i256::from_f64(n)
}
}
Expand Down Expand Up @@ -474,31 +474,31 @@ where
Ok(Arc::new(result))
}

pub(crate) fn cast_floating_point_to_decimal<T: ArrowPrimitiveType, D, M>(
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<Native = M>,
M: ArrowNativeTypeOp + DecimalCast,
D: DecimalType + ArrowPrimitiveType,
<D as ArrowPrimitiveType>::Native: DecimalCast,
{
let mul = 10_f64.powi(scale as i32);

if cast_options.safe {
array
.unary_opt::<_, D>(|v| {
M::from_f64::<M>((mul * v.as_()).round())
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::<_, D, _>(|v| {
M::from_f64::<M>((mul * v.as_()).round())
D::Native::from_f64((mul * v.as_()).round())
.ok_or_else(|| {
ArrowError::CastError(format!(
"Cannot cast to {}({}, {}). Overflowing on {:?}",
Expand Down
11 changes: 5 additions & 6 deletions arrow-cast/src/cast/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ 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) => pack_decimal_to_dictionary::<K, Decimal128Type, _>(
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, _>(
Decimal256(p, s) => pack_decimal_to_dictionary::<K, Decimal256Type>(
array,
dict_value_type,
p,
Expand Down Expand Up @@ -329,7 +329,7 @@ where
Ok(Arc::new(b.finish()))
}

pub(crate) fn pack_decimal_to_dictionary<K, D, M>(
pub(crate) fn pack_decimal_to_dictionary<K, D>(
array: &dyn Array,
dict_value_type: &DataType,
precision: u8,
Expand All @@ -338,15 +338,14 @@ pub(crate) fn pack_decimal_to_dictionary<K, D, M>(
) -> Result<ArrayRef, ArrowError>
where
K: ArrowDictionaryKeyType,
D: DecimalType + ArrowPrimitiveType<Native = M>,
M: ArrowNativeTypeOp + DecimalCast,
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 {}", D::PREFIX))
ArrowError::ComputeError(format!("Internal Error: Cannot cast dict to {}Array", D::PREFIX))
})?;
let value = dict.values().clone();
// Set correct precision/scale
Expand Down
4 changes: 2 additions & 2 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2051,13 +2051,13 @@ where
base,
cast_options,
),
Float32 => cast_floating_point_to_decimal::<_, D, _>(
Float32 => cast_floating_point_to_decimal::<_, D>(
array.as_primitive::<Float32Type>(),
*precision,
*scale,
cast_options,
),
Float64 => cast_floating_point_to_decimal::<_, D, _>(
Float64 => cast_floating_point_to_decimal::<_, D>(
array.as_primitive::<Float64Type>(),
*precision,
*scale,
Expand Down
7 changes: 3 additions & 4 deletions arrow-schema/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,9 @@ impl TryFrom<&FFI_ArrowSchema> for DataType {
"The decimal type requires an integer scale".to_string(),
)
})?;
let parsed_bits = bits.parse::<u16>().unwrap_or(0);
match parsed_bits {
128 => DataType::Decimal128(parsed_precision, parsed_scale),
256 => DataType::Decimal256(parsed_precision, parsed_scale),
match *bits {
"128" => DataType::Decimal128(parsed_precision, parsed_scale),
"256" => DataType::Decimal256(parsed_precision, parsed_scale),
_ => return Err(ArrowError::CDataInterface("Only 128- and 256- bit wide decimals are supported in the Rust implementation".to_string())),
}
}
Expand Down

0 comments on commit b9fb40f

Please sign in to comment.