@@ -6,12 +6,13 @@ use std::sync::Arc;
66use arrow_array:: { ArrayRef , StructArray } ;
77use arrow_schema:: { Field , Fields } ;
88use vortex_error:: VortexResult ;
9- use vortex_vector:: StructVector ;
9+ use vortex_vector:: { StructVector , VectorOps } ;
1010
1111use crate :: arrow:: IntoArrow ;
1212
1313impl 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 ( )
@@ -22,14 +23,26 @@ impl IntoArrow<ArrayRef> for StructVector {
2223 // indices.
2324 let fields = Fields :: from (
2425 ( 0 ..arrow_fields. len ( ) )
25- . 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+ } )
2633 . collect :: < Vec < Field > > ( ) ,
2734 ) ;
2835
29- Ok ( Arc :: new ( StructArray :: try_new (
30- fields,
31- arrow_fields,
32- validity. into_arrow ( ) ?,
33- ) ?) )
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+ } ) )
3447 }
3548}
0 commit comments