martin-g commented on code in PR #18999:
URL: https://github.com/apache/datafusion/pull/18999#discussion_r2574884547


##########
datafusion/functions/src/utils.rs:
##########
@@ -219,6 +223,40 @@ pub fn decimal128_to_i128(value: i128, scale: i8) -> 
Result<i128, ArrowError> {
     }
 }
 
+pub fn decimal32_to_f64(value: i32, precision: u8, scale: i8) -> Result<f64, 
ArrowError> {
+    if scale < 0 {
+        Err(ArrowError::ComputeError(
+            "Negative scale is not supported".into(),
+        ))
+    } else if scale as u8 > precision {
+        Err(ArrowError::ComputeError(
+            "scale {scale} is greater than precision {precision}".into(),
+        ))
+    } else {
+        validate_decimal32_precision(value, precision, scale)?;
+
+        let divisor = f64::from(10).pow_checked(scale as u32)?;
+        Ok(value as f64 / divisor)
+    }
+}
+
+pub fn decimal64_to_f64(value: i64, precision: u8, scale: i8) -> Result<f64, 
ArrowError> {
+    if scale < 0 {
+        Err(ArrowError::ComputeError(
+            "Negative scale is not supported".into(),
+        ))
+    } else if scale as u8 > precision {
+        Err(ArrowError::ComputeError(
+            "scale {scale} is greater than precision {precision}".into(),

Review Comment:
   ```suggestion
               format!("scale {scale} is greater than precision {precision}"),
   ```



##########
datafusion/functions/src/utils.rs:
##########
@@ -219,6 +223,40 @@ pub fn decimal128_to_i128(value: i128, scale: i8) -> 
Result<i128, ArrowError> {
     }
 }
 
+pub fn decimal32_to_f64(value: i32, precision: u8, scale: i8) -> Result<f64, 
ArrowError> {
+    if scale < 0 {
+        Err(ArrowError::ComputeError(
+            "Negative scale is not supported".into(),
+        ))
+    } else if scale as u8 > precision {
+        Err(ArrowError::ComputeError(
+            "scale {scale} is greater than precision {precision}".into(),

Review Comment:
   ```suggestion
               format!("scale {scale} is greater than precision {precision}"),
   ```



##########
datafusion/functions/src/utils.rs:
##########
@@ -376,4 +415,74 @@ pub mod test {
             }
         }
     }
+
+    #[test]
+    fn test_decimal32_to_f64() {
+        let cases = [
+            (123, 7, 0, Some(123.0)),
+            (1230, 7, 1, Some(123.0)),
+            (123000, 7, 3, Some(123.0)),
+            (1234567, 7, 2, Some(12345.67)),

Review Comment:
   Add tests for negative values:
   ```suggestion
               (1234567, 7, 2, Some(12345.67)),
               (-1234567, 7, 2, Some(-12345.67)),
   ```



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