You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/12/18 01:42:04 UTC

[GitHub] [arrow-rs] liukun4515 commented on a change in pull request #1044: support cast signed numeric to decimal

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



##########
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:
       grate suggestion.
   I will try 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