@@ -26,14 +26,14 @@ use uuid::Uuid;
2626
2727use super :: get_field_id;
2828use crate :: spec:: {
29- visit_struct_with_partner, Literal , Map , PartnerAccessor , PrimitiveType ,
30- SchemaWithPartnerVisitor , Struct , StructType ,
29+ visit_struct_with_partner, ListType , Literal , Map , MapType , NestedField , PartnerAccessor ,
30+ PrimitiveType , SchemaWithPartnerVisitor , Struct , StructType ,
3131} ;
3232use crate :: { Error , ErrorKind , Result } ;
3333
34- struct ArrowArrayConverter ;
34+ struct ArrowArrayToIcebergStructConverter ;
3535
36- impl SchemaWithPartnerVisitor < ArrayRef > for ArrowArrayConverter {
36+ impl SchemaWithPartnerVisitor < ArrayRef > for ArrowArrayToIcebergStructConverter {
3737 type T = Vec < Option < Literal > > ;
3838
3939 fn schema (
@@ -56,7 +56,9 @@ impl SchemaWithPartnerVisitor<ArrayRef> for ArrowArrayConverter {
5656 return Err ( Error :: new (
5757 ErrorKind :: DataInvalid ,
5858 "The field is required but has null value" ,
59- ) ) ;
59+ )
60+ . with_context ( "field_id" , field. id . to_string ( ) )
61+ . with_context ( "field_name" , & field. name ) ) ;
6062 }
6163 Ok ( value)
6264 }
@@ -68,11 +70,13 @@ impl SchemaWithPartnerVisitor<ArrayRef> for ArrowArrayConverter {
6870 results : Vec < Vec < Option < Literal > > > ,
6971 ) -> Result < Vec < Option < Literal > > > {
7072 let row_len = results. first ( ) . map ( |column| column. len ( ) ) . unwrap_or ( 0 ) ;
71- if results. iter ( ) . any ( |column| column . len ( ) != row_len) {
73+ if let Some ( col ) = results. iter ( ) . find ( |col| col . len ( ) != row_len) {
7274 return Err ( Error :: new (
7375 ErrorKind :: DataInvalid ,
7476 "The struct columns have different row length" ,
75- ) ) ;
77+ )
78+ . with_context ( "first col length" , row_len. to_string ( ) )
79+ . with_context ( "actual col length" , col. len ( ) . to_string ( ) ) ) ;
7680 }
7781
7882 let mut struct_literals = Vec :: with_capacity ( row_len) ;
@@ -98,7 +102,7 @@ impl SchemaWithPartnerVisitor<ArrayRef> for ArrowArrayConverter {
98102
99103 fn list (
100104 & mut self ,
101- list : & crate :: spec :: ListType ,
105+ list : & ListType ,
102106 array : & ArrayRef ,
103107 elements : Vec < Option < Literal > > ,
104108 ) -> Result < Vec < Option < Literal > > > {
@@ -164,7 +168,7 @@ impl SchemaWithPartnerVisitor<ArrayRef> for ArrowArrayConverter {
164168
165169 fn map (
166170 & mut self ,
167- _map : & crate :: spec :: MapType ,
171+ _map : & MapType ,
168172 partner : & ArrayRef ,
169173 key_values : Vec < Option < Literal > > ,
170174 values : Vec < Option < Literal > > ,
@@ -437,8 +441,7 @@ impl PartnerAccessor<ArrayRef> for ArrowArrayAccessor {
437441 fn field_partner < ' a > (
438442 & self ,
439443 struct_partner : & ' a ArrayRef ,
440- field_id : i32 ,
441- _field_name : & str ,
444+ field : & NestedField ,
442445 ) -> Result < & ' a ArrayRef > {
443446 let struct_array = struct_partner
444447 . as_any ( )
@@ -452,15 +455,15 @@ impl PartnerAccessor<ArrayRef> for ArrowArrayAccessor {
452455 let field_pos = struct_array
453456 . fields ( )
454457 . iter ( )
455- . position ( |field | {
456- get_field_id ( field )
457- . map ( |id| id == field_id )
458+ . position ( |arrow_field | {
459+ get_field_id ( arrow_field )
460+ . map ( |id| id == field . id )
458461 . unwrap_or ( false )
459462 } )
460463 . ok_or_else ( || {
461464 Error :: new (
462465 ErrorKind :: DataInvalid ,
463- format ! ( "Field id {} not found in struct array" , field_id ) ,
466+ format ! ( "Field id {} not found in struct array" , field . id ) ,
464467 )
465468 } ) ?;
466469 Ok ( struct_array. column ( field_pos) )
@@ -541,7 +544,7 @@ pub fn arrow_struct_to_literal(
541544 visit_struct_with_partner (
542545 ty,
543546 struct_array,
544- & mut ArrowArrayConverter ,
547+ & mut ArrowArrayToIcebergStructConverter ,
545548 & ArrowArrayAccessor ,
546549 )
547550}
0 commit comments