ahmedriza commented on code in PR #5311:
URL: https://github.com/apache/arrow-datafusion/pull/5311#discussion_r1111109682


##########
datafusion/physical-expr/src/expressions/get_indexed_field.rs:
##########
@@ -390,4 +404,78 @@ mod tests {
         )?;
         Ok(())
     }
+
+    #[test]
+    fn get_indexed_field_list_out_of_bounds() {
+        let fields = vec![
+            Field::new("id", DataType::Int64, true),
+            Field::new(
+                "a",
+                DataType::List(Box::new(Field::new("item", DataType::Float64, 
true))),
+                true,
+            ),
+        ];
+
+        let schema = Schema::new(fields);
+        let mut int_builder = PrimitiveBuilder::<Int64Type>::new();
+        int_builder.append_value(1);
+
+        let mut lb = ListBuilder::new(PrimitiveBuilder::<Float64Type>::new());
+        lb.values().append_value(1.0);
+        lb.values().append_null();
+        lb.values().append_value(3.0);
+        lb.append(true);
+
+        let batch = RecordBatch::try_new(
+            Arc::new(schema.clone()),
+            vec![Arc::new(int_builder.finish()), Arc::new(lb.finish())],
+        )
+        .unwrap();
+
+        let col_a = col("a", &schema).unwrap();
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 0, float64_array(None));
+
+        verify_index_evaluation(&batch, col_a.clone(), 1, 
float64_array(Some(1.0)));
+        verify_index_evaluation(&batch, col_a.clone(), 2, float64_array(None));
+        verify_index_evaluation(&batch, col_a.clone(), 3, 
float64_array(Some(3.0)));
+
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 100, 
float64_array(None));
+    }
+
+    fn verify_index_evaluation(
+        batch: &RecordBatch,
+        arg: Arc<dyn PhysicalExpr>,
+        index: i64,
+        expected_result: ArrayRef,
+    ) {
+        let expr = Arc::new(GetIndexedFieldExpr::new(
+            arg,
+            ScalarValue::Int64(Some(index)),
+        ));
+        let result = 
expr.evaluate(batch).unwrap().into_array(batch.num_rows());
+        assert!(

Review Comment:
   Thanks @alamb.  Didn't know about this macro. Can see it in 
`datafusion::core::test_util.rs`.  However, can't see `core` being a dependency 
of `datafusion-physical-expr` where this test is (hence, in order to use the 
macro, will need to add an additional dependency here).  Perhaps I've 
misunderstood what was meant.



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