Skip to content

Commit 1361988

Browse files
authored
Chore: use try_new for struct array into_arrow (#5159)
plus some comments --------- Signed-off-by: Connor Tsui <[email protected]>
1 parent be23ca2 commit 1361988

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

vortex-compute/src/arrow/struct_.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,43 @@ use std::sync::Arc;
66
use arrow_array::{ArrayRef, StructArray};
77
use arrow_schema::{Field, Fields};
88
use vortex_error::VortexResult;
9-
use vortex_vector::StructVector;
9+
use vortex_vector::{StructVector, VectorOps};
1010

1111
use crate::arrow::IntoArrow;
1212

1313
impl IntoArrow<ArrayRef> for StructVector {
1414
fn into_arrow(self) -> VortexResult<ArrayRef> {
15+
let len = self.len();
1516
let (fields, validity) = self.into_parts();
1617
let arrow_fields = fields
1718
.iter()
1819
.map(|field| field.clone().into_arrow())
1920
.collect::<VortexResult<Vec<ArrayRef>>>()?;
2021

21-
// We need to make up the field names since vectors are unnamed.
22+
// We need to make up the field names since vectors are unnamed, so we just use the field
23+
// indices.
2224
let fields = Fields::from(
2325
(0..arrow_fields.len())
24-
.map(|i| Field::new(i.to_string(), arrow_fields[i].data_type().clone(), true))
26+
.map(|i| {
27+
Field::new(
28+
i.to_string(),
29+
arrow_fields[i].data_type().clone(),
30+
true, // Vectors are always nullable.
31+
)
32+
})
2533
.collect::<Vec<Field>>(),
2634
);
2735

28-
Ok(Arc::new(StructArray::new(
29-
fields,
30-
arrow_fields,
31-
validity.into_arrow()?,
32-
)))
36+
// SAFETY: Since all of these components came from a valid `StructVector`, we know that all
37+
// of the lengths of the vectors are correct. Additionally, all extra metadata is directly
38+
// derived from the existing components so all invariants are upheld.
39+
Ok(Arc::new(unsafe {
40+
StructArray::new_unchecked_with_length(
41+
fields,
42+
arrow_fields,
43+
validity.into_arrow()?,
44+
len,
45+
)
46+
}))
3347
}
3448
}

0 commit comments

Comments
 (0)