yongster commented on code in PR #10707:
URL: https://github.com/apache/arrow-rs/pull/10707#discussion_r3795684984
##########
arrow-cast/src/cast/mod.rs:
##########
@@ -348,6 +348,36 @@ 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`
+/// losslessly, which [`DecimalCast`] then converts to the decimal native type
+/// with a range check.
+fn integer_to_decimal_native<I, M>(value: I) -> Option<M>
+where
+ I: Into<i128>,
+ M: DecimalCast,
+{
+ M::from_decimal(value.into())
+}
+
+/// Scale an integer down in its native type before narrowing it to the decimal
+/// native type.
+///
+/// If the scale factor cannot be represented by the input type, it is larger
+/// than every possible input value and integer division therefore produces
+/// zero.
+fn scale_integer_down<I>(value: I, scale: u32) -> Option<I>
+where
+ I: ArrowNativeTypeOp,
+{
+ match I::usize_as(10).pow_checked(scale) {
Review Comment:
Good point. I now compute the scale factor once, outside the array kernel, in
the source integer type. The hot loop only performs the division and checked
conversion. If the factor exceeds the source type's range, every input value
scales to zero, so that case directly produces zero while preserving nulls.
--
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]