yongster commented on code in PR #10707:
URL: https://github.com/apache/arrow-rs/pull/10707#discussion_r3794814682
##########
arrow-cast/src/cast/mod.rs:
##########
@@ -348,6 +348,19 @@ pub fn cast(array: &dyn Array, to_type: &DataType) ->
Result<ArrayRef, ArrowErro
cast_with_options(array, to_type, &CastOptions::default())
}
+/// Convert an integer to a decimal native value without wrapping.
+///
+/// `AsPrimitive` / `as` silently truncates when the source is wider than `M`
+/// (for example `5_000_000_000i64 as i32`). All integer sources fit in `i128`,
+/// so go through that and then use [`DecimalCast`] which is range-checked.
+fn integer_to_decimal_native<I, M>(value: I) -> Option<M>
+where
+ I: NumCast,
+ M: DecimalCast,
+{
+ num_cast::<I, i128>(value).and_then(M::from_decimal)
+}
Review Comment:
Yes, the `i128` intermediate is intentional.
This helper is shared by casts to Decimal32, Decimal64, Decimal128, and
Decimal256, so the target native type `M` can be `i32`, `i64`, `i128`, or
`i256`. A direct generic `num_cast::<I, M>` is not available for all of these
targets, and `DecimalCast::from_decimal` provides the checked final
conversion.
All integer input types dispatched to this helper fit losslessly in `i128`.
I agree that the current `num_cast` expression obscures that invariant, so I
will replace it with an explicit lossless `Into<i128>` conversion.
--
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]