Jefffrey commented on code in PR #10934:
URL: https://github.com/apache/arrow-rs/pull/10934#discussion_r3911558208
##########
arrow-data/src/data.rs:
##########
@@ -2511,6 +2514,78 @@ mod tests {
assert_eq!(child_arr_data, arr_data.child_data()[0]);
}
+ #[test]
+ fn test_struct_validation_accounts_for_parent_offset() {
+ let data_type =
+ DataType::Struct(Fields::from(vec![Field::new("x",
DataType::Int32, false)]));
+ let child = ArrayData::builder(DataType::Int32)
+ .len(5)
+ .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4]))
+ .build()
+ .unwrap();
+
+ // The parent needs child elements 1..6, but the child only has five.
+ let result = ArrayData::builder(data_type)
+ .len(5)
+ .offset(1)
+ .add_child_data(child)
+ .build();
+
+ assert!(result.is_err());
Review Comment:
lets assert the error message too; i left it out in the original issue since
the code at that point didnt throw an error so didnt know what error message
would be
##########
arrow-array/src/array/struct_array.rs:
##########
@@ -864,22 +864,23 @@ mod tests {
}
#[test]
- #[should_panic(expected = "assertion failed: end <= self.len()")]
fn test_struct_array_from_data_with_offset_and_length_error() {
let int_arr = Int32Array::from(vec![1, 2, 3, 4, 5]);
let int_field = Field::new("x", DataType::Int32, false);
let struct_nulls = NullBuffer::new(BooleanBuffer::from(vec![true,
true, false]));
let int_data = int_arr.to_data();
// If parent offset is 3 and len is 3 then child must have 6 items
- let struct_data =
-
ArrayData::builder(DataType::Struct(Fields::from(vec![int_field.clone()])))
- .len(3)
- .offset(3)
- .nulls(Some(struct_nulls))
- .add_child_data(int_data)
- .build()
- .unwrap();
- let _ = StructArray::from(struct_data);
+ let err =
ArrayData::builder(DataType::Struct(Fields::from(vec![int_field.clone()])))
+ .len(3)
+ .offset(3)
+ .nulls(Some(struct_nulls))
+ .add_child_data(int_data)
+ .build()
+ .unwrap_err()
+ .to_string();
+
+ assert!(err.contains("child array #0 for field x has length smaller
than expected"));
+ assert!(err.contains("(5 < 6)"));
Review Comment:
```suggestion
assert!(err.contains(
"child array #0 for field x has length smaller than expected for
struct array (5 < 6)"
));
```
--
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]