You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Chiicake (via GitHub)" <gi...@apache.org> on 2026/01/17 05:22:38 UTC

[PR] Support casting negative scale decimals to numeric [arrow-rs]

Chiicake opened a new pull request, #9207:
URL: https://github.com/apache/arrow-rs/pull/9207

   # Which issue does this PR close?
   
   - Closes #9201 .
   
   # Rationale for this change
   
   Casting decimals with negative scale to integer types currently errors because the scale factor is always applied as a division. Negative scales represent powers of ten that should scale the integer value up, so the cast should multiply instead.
   
   # What changes are included in this PR?
   
   - Apply the scale factor using multiplication when scale < 0 in cast_decimal_to_integer.
   
   # Are these changes tested?
   
   Yes, the test given by issue is passed.
   ```
   // arrow-cast/src/cast/mod.rs
       #[test]
       fn test_cast_decimal_to_numeric_negative_scale() {
           let value_array: Vec<Option<i256>> = vec![
               Some(i256::from_i128(125)),
               Some(i256::from_i128(225)),
               Some(i256::from_i128(325)),
               None,
               Some(i256::from_i128(525)),
           ];
           let array = create_decimal256_array(value_array, 38, -1).unwrap();
   
           generate_cast_test_case!(
               &array,
               Int64Array,
               &DataType::Int64,
               vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
           );
       }
   ```
   
   # Are there any user-facing changes?
   
   No.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Support casting negative scale decimals to numeric [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #9207:
URL: https://github.com/apache/arrow-rs/pull/9207#discussion_r2702544673


##########
arrow-cast/src/cast/decimal.rs:
##########
@@ -826,17 +831,21 @@ where
 
     let mut value_builder = PrimitiveBuilder::<T>::with_capacity(array.len());
 
+    let scale_op: ScaleOp<D> =

Review Comment:
   Maybe we're better off using an approach similar to the integer to decimal function:
   
   https://github.com/apache/arrow-rs/blob/9c6065c451a1131393191ea828151639f031bf87/arrow-cast/src/cast/mod.rs#L384-L412
   
   Even though it duplicates more code, it avoids the need for this type alias; and I'm not 100% sure but maybe it inlines better 🤔 



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Support casting negative scale decimals to numeric [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #9207:
URL: https://github.com/apache/arrow-rs/pull/9207


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Support casting negative scale decimals to numeric [arrow-rs]

Posted by "Chiicake (via GitHub)" <gi...@apache.org>.
Chiicake commented on code in PR #9207:
URL: https://github.com/apache/arrow-rs/pull/9207#discussion_r2700688584


##########
arrow-cast/src/cast/mod.rs:
##########
@@ -3886,6 +3886,25 @@ mod tests {
         );
     }
 
+    #[test]
+    fn test_cast_decimal_to_numeric_negative_scale() {

Review Comment:
   get it



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Support casting negative scale decimals to numeric [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #9207:
URL: https://github.com/apache/arrow-rs/pull/9207#discussion_r2700685049


##########
arrow-cast/src/cast/decimal.rs:
##########
@@ -831,11 +831,12 @@ where
             if array.is_null(i) {
                 value_builder.append_null();
             } else {
-                let v = array
-                    .value(i)
-                    .div_checked(div)
-                    .ok()
-                    .and_then(<T::Native as NumCast>::from::<D::Native>);
+                let v = if scale < 0 {

Review Comment:
   Could we pull this check outside the hot loop?



##########
arrow-cast/src/cast/mod.rs:
##########
@@ -3886,6 +3886,25 @@ mod tests {
         );
     }
 
+    #[test]
+    fn test_cast_decimal_to_numeric_negative_scale() {

Review Comment:
   Can we add some more tests with different scales (e.g. that cause overflow with and without safe casting) and other decimal types



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Support casting negative scale decimals to numeric [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #9207:
URL: https://github.com/apache/arrow-rs/pull/9207#issuecomment-3801931097

   Looks good to me -- thank you @Chiicake and @Jefffrey 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Support casting negative scale decimals to numeric [arrow-rs]

Posted by "Chiicake (via GitHub)" <gi...@apache.org>.
Chiicake commented on PR #9207:
URL: https://github.com/apache/arrow-rs/pull/9207#issuecomment-3802670482

   @Jefffrey Thank you for your detailed review!


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org