This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 1019f5b27d fix: issue introduced in #6833 -  less than equal check for 
scale in decimal conversion (#7070)
1019f5b27d is described below

commit 1019f5b27d3596077bcdd7e10b67e2c6d4cfbf02
Author: Himadri Pal <[email protected]>
AuthorDate: Wed Feb 5 06:43:42 2025 -0800

    fix: issue introduced in #6833 -  less than equal check for scale in 
decimal conversion (#7070)
    
    * fix <= check for scale in decimal conversion
    
    * Update arrow-cast/src/cast/mod.rs
    
    name change
    
    Co-authored-by: Arttu <[email protected]>
    
    * remove incorrect comment
    
    ---------
    
    Co-authored-by: Arttu <[email protected]>
---
 arrow-cast/src/cast/decimal.rs |  3 +--
 arrow-cast/src/cast/mod.rs     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/arrow-cast/src/cast/decimal.rs b/arrow-cast/src/cast/decimal.rs
index ba82ca9040..fee9c007c0 100644
--- a/arrow-cast/src/cast/decimal.rs
+++ b/arrow-cast/src/cast/decimal.rs
@@ -167,8 +167,7 @@ where
     let array: PrimitiveArray<T> =
         if input_scale == output_scale && input_precision <= output_precision {
             array.clone()
-        } else if input_scale < output_scale {
-            // the scale doesn't change, but precision may change and cause 
overflow
+        } else if input_scale <= output_scale {
             convert_to_bigger_or_equal_scale_decimal::<T, T>(
                 array,
                 input_scale,
diff --git a/arrow-cast/src/cast/mod.rs b/arrow-cast/src/cast/mod.rs
index 440d0a8bec..00d6b14964 100644
--- a/arrow-cast/src/cast/mod.rs
+++ b/arrow-cast/src/cast/mod.rs
@@ -9980,7 +9980,26 @@ mod tests {
         };
         let result = cast_with_options(&array, &output_type, &options);
         assert_eq!(result.unwrap_err().to_string(),
-                   "Invalid argument error: 123456790 is too large to store in 
a Decimal128 of precision 6. Max is 999999");
+                   "Invalid argument error: 123456789 is too large to store in 
a Decimal128 of precision 6. Max is 999999");
+    }
+
+    #[test]
+    fn test_decimal_to_decimal_same_scale() {
+        let array = vec![Some(520)];
+        let array = create_decimal_array(array, 4, 2).unwrap();
+        let input_type = DataType::Decimal128(4, 2);
+        let output_type = DataType::Decimal128(3, 2);
+        assert!(can_cast_types(&input_type, &output_type));
+
+        let options = CastOptions {
+            safe: false,
+            ..Default::default()
+        };
+        let result = cast_with_options(&array, &output_type, &options);
+        assert_eq!(
+            result.unwrap().as_primitive::<Decimal128Type>().value(0),
+            520
+        );
     }
 
     #[test]

Reply via email to