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 2020/08/29 15:08:18 UTC

[GitHub] [arrow] andygrove commented on a change in pull request #8077: ARROW-9886: [Rust] [DataFusion] Parameterized testing of physical cast.

andygrove commented on a change in pull request #8077:
URL: https://github.com/apache/arrow/pull/8077#discussion_r479658360



##########
File path: rust/datafusion/src/execution/physical_plan/expressions.rs
##########
@@ -1740,64 +1740,83 @@ mod tests {
         Ok(())
     }
 
-    #[test]
-    fn cast_i32_to_u32() -> Result<()> {
-        let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
-        let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
-        let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
+    // runs an end-to-end test of physical type cast
+    // 1. construct a record batch with a column "a" of type A
+    // 2. construct a physical expression of CAST(a AS B)
+    // 3. evaluate the expression
+    // 4. verify that the resulting expression is of type B
+    // 5. verify that the resulting values are downcastable and correct
+    macro_rules! generic_test_cast {
+        ($A_ARRAY:ident, $A_TYPE:expr, $A_VEC:expr, $TYPEARRAY:ident, $TYPE:expr, $VEC:expr) => {{
+            let schema = Schema::new(vec![Field::new("a", $A_TYPE, false)]);
+            let a = $A_ARRAY::from($A_VEC);
+            let batch =
+                RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
 
-        let cast = cast(col("a"), &schema, DataType::UInt32)?;
-        assert_eq!("CAST(a AS UInt32)", format!("{}", cast));
-        let result = cast.evaluate(&batch)?;
-        assert_eq!(result.len(), 5);
+            // verify that we can construct the expression
+            let expression = cast(col("a"), &schema, $TYPE)?;
 
-        let result = result
-            .as_any()
-            .downcast_ref::<UInt32Array>()
-            .expect("failed to downcast to UInt32Array");
-        assert_eq!(result.value(0), 1_u32);
+            // verify that its display is correct
+            assert_eq!(format!("CAST(a AS {:?})", $TYPE), format!("{}", expression));
 
-        Ok(())
-    }
+            // verify that the expression's type is correct
+            assert_eq!(expression.data_type(&schema)?, $TYPE);
 
-    #[test]
-    fn cast_i32_to_utf8() -> Result<()> {
-        let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
-        let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
-        let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
+            // compute
+            let result = expression.evaluate(&batch)?;
 
-        let cast = cast(col("a"), &schema, DataType::Utf8)?;
-        let result = cast.evaluate(&batch)?;
-        assert_eq!(result.len(), 5);
+            // verify that the array's data_type is correct
+            assert_eq!(*result.data_type(), $TYPE);
 
-        let result = result
-            .as_any()
-            .downcast_ref::<StringArray>()
-            .expect("failed to downcast to StringArray");
-        assert_eq!(result.value(0), "1");
+            // verify that the len is correct
+            assert_eq!(result.len(), $A_VEC.len());
 
-        Ok(())
+            // verify that the data itself is downcastable
+            let result = result
+                .as_any()
+                .downcast_ref::<$TYPEARRAY>()
+                .expect("failed to downcast");
+
+            // verify that the result itself is correct
+            for (i, x) in $VEC.iter().enumerate() {
+                assert_eq!(result.value(i), *x);
+            }
+        }};
     }
 
     #[test]

Review comment:
       I like the cleanup with the macro! I think this test contains multiple tests though that should be separated out into their own test functions? e.g. `test_cast_i32_i32` and `test_cast_i32_string` and so on, so that if one of these fails it is easier to track down?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org