tustvold commented on code in PR #3021:
URL: https://github.com/apache/arrow-rs/pull/3021#discussion_r1014554917
##########
arrow-cast/src/cast.rs:
##########
@@ -344,16 +348,58 @@ fn cast_floating_point_to_decimal128<T:
ArrowPrimitiveType>(
array: &PrimitiveArray<T>,
precision: u8,
scale: u8,
+ cast_options: &CastOptions,
) -> Result<ArrayRef, ArrowError>
where
<T as ArrowPrimitiveType>::Native: AsPrimitive<f64>,
{
let mul = 10_f64.powi(scale as i32);
- array
- .unary::<_, Decimal128Type>(|v| (v.as_() * mul).round() as i128)
- .with_precision_and_scale(precision, scale)
- .map(|a| Arc::new(a) as ArrayRef)
+ if cast_options.safe {
+ let iter = array.iter().map(|v| {
+ v.and_then(|v| {
+ let mul_v = (mul * v.as_()).round() as i128;
+ if precision <= DECIMAL128_MAX_PRECISION
+ && (mul_v > MAX_DECIMAL_FOR_EACH_PRECISION[precision as
usize - 1]
+ || mul_v < MIN_DECIMAL_FOR_EACH_PRECISION[precision as
usize - 1])
Review Comment:
I don't think we need to validate precision, as we don't in other places.
This is explicitly an opt-in, we only need to error if the underlying value is
truncated/overflows - i.e. data loss has occurred.
--
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]