alamb commented on a change in pull request #1044:
URL: https://github.com/apache/arrow-rs/pull/1044#discussion_r771587196



##########
File path: arrow/src/compute/kernels/cast.rs
##########
@@ -1837,6 +1906,125 @@ where
 mod tests {
     use super::*;
     use crate::{buffer::Buffer, util::display::array_value_to_string};
+    use num::traits::Pow;
+
+    #[test]
+    fn test_cast_numeric_to_decimal() {
+        let data_types = vec![
+            DataType::Int8,
+            DataType::Int16,
+            DataType::Int32,
+            DataType::Int64,
+            DataType::Float32,
+            DataType::Float64,
+        ];
+        let decimal_type = DataType::Decimal(38, 6);
+        for data_type in data_types {
+            assert!(can_cast_types(&data_type, &decimal_type))
+        }
+        assert!(!can_cast_types(&DataType::UInt64, &decimal_type));
+
+        // test i8 to decimal type
+        let array = Int8Array::from(vec![1, 2, 3, 4, 5]);

Review comment:
       I also think we should test Nulls here too 

##########
File path: arrow/src/compute/kernels/cast.rs
##########
@@ -1837,6 +1906,125 @@ where
 mod tests {
     use super::*;
     use crate::{buffer::Buffer, util::display::array_value_to_string};
+    use num::traits::Pow;
+
+    #[test]
+    fn test_cast_numeric_to_decimal() {
+        let data_types = vec![
+            DataType::Int8,
+            DataType::Int16,
+            DataType::Int32,
+            DataType::Int64,
+            DataType::Float32,
+            DataType::Float64,
+        ];
+        let decimal_type = DataType::Decimal(38, 6);
+        for data_type in data_types {
+            assert!(can_cast_types(&data_type, &decimal_type))
+        }
+        assert!(!can_cast_types(&DataType::UInt64, &decimal_type));
+
+        // test i8 to decimal type
+        let array = Int8Array::from(vec![1, 2, 3, 4, 5]);
+        let array = Arc::new(array) as ArrayRef;
+        let casted_array = cast(&array, &decimal_type).unwrap();
+        let decimal_array = casted_array
+            .as_any()
+            .downcast_ref::<DecimalArray>()
+            .unwrap();
+        assert_eq!(&decimal_type, decimal_array.data_type());
+        for i in 0..array.len() {
+            assert_eq!(
+                10_i128.pow(6) * (i as i128 + 1),
+                decimal_array.value(i as usize)
+            );
+        }
+        // test i8 to decimal type with overflow the result type
+        // the 100 will be converted to 1000_i128, but it is out of range for 
max value in the precision 3.
+        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
+        let array = Arc::new(array) as ArrayRef;
+        let casted_array = cast(&array, &DataType::Decimal(3, 1));
+        assert!(casted_array.is_err());
+        assert_eq!("Invalid argument error: The value of 1000 i128 is not 
compatible with Decimal(3,1)", casted_array.unwrap_err().to_string());
+
+        // test i16 to decimal type
+        let array = Int16Array::from(vec![1, 2, 3, 4, 5]);
+        let array = Arc::new(array) as ArrayRef;
+        let casted_array = cast(&array, &decimal_type).unwrap();
+        let decimal_array = casted_array
+            .as_any()
+            .downcast_ref::<DecimalArray>()
+            .unwrap();
+        assert_eq!(&decimal_type, decimal_array.data_type());
+        for i in 0..array.len() {
+            assert_eq!(
+                10_i128.pow(6) * (i as i128 + 1),
+                decimal_array.value(i as usize)
+            );
+        }
+
+        // test i32 to decimal type
+        let array = Int32Array::from(vec![1, 2, 3, 4, 5]);

Review comment:
       Rather than just copy / pasting the integer tests when the differ only 
in the array type, what do you think about making a look like
   
   ```
   let input = vec![
     Arc::new(Int8Array::from(vec![1, 2, 3, 4, 5])),
     Arc::new(Int16Array::from(vec![1, 2, 3, 4, 5])),
   ...
   ]
   ```
   
   And then testing that in a loop? It would also allow coverage of 
`Int16Array` more easily
   
   




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


Reply via email to