alamb commented on code in PR #5311:
URL: https://github.com/apache/arrow-datafusion/pull/5311#discussion_r1111055533
##########
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:
the `assert_batches_eq!` macro is often used for such comparisons too
##########
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));
Review Comment:
👍
##########
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!(
+ result == expected_result.clone(),
+ "result: {:?} != expected result: {:?}",
+ result,
+ expected_result
+ );
+ assert_eq!(result.data_type(), &DataType::Float64);
+ }
+
+ fn float64_array(value: Option<f64>) -> ArrayRef {
+ match value {
+ Some(v) => {
+ let mut b = PrimitiveBuilder::<Float64Type>::new();
+ b.append_value(v);
+ Arc::new(b.finish())
+ }
+ None => {
+ let mut b = PrimitiveBuilder::<Float64Type>::new();
+ b.append_null();
+ Arc::new(b.finish())
+ }
+ }
Review Comment:
I think you can make a Float64Array directly from the value if you wanted:
```rust
Arc::new(Float64Array::from_value(value, 1))
```
https://docs.rs/arrow/33.0.0/arrow/array/struct.PrimitiveArray.html#method.from_value
--
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]