Jefffrey commented on code in PR #18999:
URL: https://github.com/apache/datafusion/pull/18999#discussion_r2621296469


##########
datafusion/functions/src/utils.rs:
##########
@@ -219,6 +221,52 @@ pub fn decimal128_to_i128(value: i128, scale: i8) -> 
Result<i128, ArrowError> {
     }
 }
 
+pub fn decimal32_to_i32(value: i32, precision: u8, scale: i8) -> Result<i32, 
ArrowError> {
+    if scale < 0 {
+        Err(ArrowError::ComputeError(
+            "Negative scale is not supported".into(),
+        ))
+    } else if scale as u8 > precision {
+        Err(ArrowError::ComputeError(format!(
+            "scale {scale} is greater than precision {precision}"
+        )))
+    } else if scale == 0 {
+        Ok(value)
+    } else {
+        validate_decimal32_precision(value, precision, scale)?;

Review Comment:
   I'm curious why we differ from the decimal128 version above in having a 
`precision` parameter and doing these extra checks?



##########
datafusion/functions/src/utils.rs:
##########
@@ -376,4 +425,70 @@ pub mod test {
             }
         }
     }
+
+    #[test]
+    fn test_decimal32_to_i32() {
+        let cases = [
+            (123, 7, 0, Some(123)),
+            (1230, 7, 1, Some(123)),
+            (123000, 7, 3, Some(123)),
+            (1234567, 7, 2, Some(12345)),
+            (-1234567, 7, 2, Some(-12345)),
+            (1, 7, 0, Some(1)),
+            (123, 7, -3, None),
+            (123, 7, i8::MAX, None),
+            (999999999, 9, 0, Some(999999999)),
+            (999999999, 9, 3, Some(999999)),
+        ];
+
+        for (value, precision, scale, expected) in cases {
+            match decimal32_to_i32(value, precision, scale) {
+                Ok(actual) => {
+                    assert_eq!(
+                        actual,
+                        expected.expect("Got value but expected none"),
+                        "{value} and {precision} {scale} vs {expected:?}"
+                    );
+                }
+                Err(_) => assert!(expected.is_none()),

Review Comment:
   Would be nice if we can assert the expected error



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to