Skip to content

Commit 6737ebe

Browse files
committed
Use ArrayData::slice instead of re-inventing the wheel
1 parent fbc2955 commit 6737ebe

1 file changed

Lines changed: 6 additions & 23 deletions

File tree

arrow-array/src/array/struct_array.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -301,26 +301,11 @@ impl From<ArrayData> for StructArray {
301301
.child_data()
302302
.iter()
303303
.map(|cd| {
304-
let child_offset = cd.offset();
305-
let child_len = cd.len();
306-
assert!(
307-
child_len >= parent_len + parent_offset,
308-
"struct array has offset {} and len {} but child array only has {} items",
309-
parent_offset,
310-
parent_len,
311-
child_len
312-
);
313-
// SAFETY: We have already checked that the child array has enough items and the
314-
// only thing we are changing is the offset and length. As long as the child data
315-
// was previously valid, then the new child data is also valid.
316-
let cd = unsafe {
317-
cd.clone()
318-
.into_builder()
319-
.offset(child_offset + parent_offset)
320-
.len(parent_len)
321-
.build_unchecked()
322-
};
323-
make_array(cd.clone())
304+
if parent_offset != 0 || cd.len() != parent_len {
305+
make_array(cd.slice(parent_offset, parent_len))
306+
} else {
307+
make_array(cd.clone())
308+
}
324309
})
325310
.collect();
326311

@@ -592,9 +577,7 @@ mod tests {
592577
}
593578

594579
#[test]
595-
#[should_panic(
596-
expected = "struct array has offset 3 and len 3 but child array only has 5 items"
597-
)]
580+
#[should_panic(expected = "assertion failed: (offset + length) <= self.len()")]
598581
fn test_struct_array_from_data_with_offset_and_length_error() {
599582
let int_arr = Int32Array::from(vec![1, 2, 3, 4, 5]);
600583
let int_field = Field::new("x", DataType::Int32, false);

0 commit comments

Comments
 (0)