sdf-jkl commented on code in PR #11302:
URL: https://github.com/apache/arrow-rs/pull/11302#discussion_r4276288970
##########
arrow-cast/src/cast/decimal.rs:
##########
@@ -675,37 +675,116 @@ where
D: DecimalType + ArrowPrimitiveType,
<D as ArrowPrimitiveType>::Native: DecimalCast,
{
+ validate_decimal_precision_and_scale::<D>(precision, scale)?;
let mul = 10_f64.powi(scale as i32);
if cast_options.safe {
array
- .unary_opt::<_, D>(|v| {
- single_float_to_decimal::<D>(v.as_(), mul)
- .filter(|v| D::is_valid_decimal_precision(*v, precision))
- })
+ .unary_opt::<_, D>(|v| float_to_decimal_checked::<D>(v.as_(), mul,
precision).ok())
.with_precision_and_scale(precision, scale)
.map(|a| Arc::new(a) as ArrayRef)
} else {
array
.try_unary::<_, D, _>(|v| {
- let v = single_float_to_decimal::<D>(v.as_(),
mul).ok_or_else(|| {
- ArrowError::CastError(format!(
- "Cannot cast to {}({}, {}). Overflowing on {:?}",
- D::PREFIX,
- precision,
- scale,
- v
- ))
- })?;
- D::validate_decimal_precision(v, precision, scale).map(|()| v)
+ float_to_decimal_checked::<D>(v.as_(), mul, precision)
+ .map_err(|e| e.into_arrow_error(v, precision, scale))
})?
.with_precision_and_scale(precision, scale)
.map(|a| Arc::new(a) as ArrayRef)
}
}
+/// Converts a floating point value to the unscaled integer representation of
a decimal.
+///
+/// Scales the input by `10^scale` and rounds half away from zero. Returns an
error
+/// if the precision or scale is invalid, or the result does not fit the native
+/// type or the requested precision. Non-finite inputs are rejected.
+/// Use [`float_to_decimal_opt`] when the reason for failure is not needed.
+///
+/// ```
+/// use arrow_array::types::Decimal32Type;
+/// use arrow_cast::cast::float_to_decimal;
+///
+/// assert_eq!(float_to_decimal::<Decimal32Type>(12.345, 5, 2).unwrap(), 1235);
+/// assert!(float_to_decimal::<Decimal32Type>(12345.678, 5, 2).is_err());
+/// ```
+#[inline]
Review Comment:
This one shows actuall regressions on strict paths if removed -- ~10% locally
--
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]