@@ -20,6 +20,15 @@ def struct_field_names(struct_type: pa.StructType) -> list[str]:
2020 return [f .name for f in struct_type ]
2121
2222
23+ def struct_fields (struct_type : pa .StructType ) -> list [pa .Field ]:
24+ """Return fields of a pyarrow.StructType in a pyarrow<18-compatible way.
25+
26+ Note: Once we bump our pyarrow requirement to ">=18", this helper can be
27+ replaced with direct usage of ``struct_type.fields`` throughout the codebase.
28+ """
29+ return [struct_type .field (i ) for i in range (struct_type .num_fields )]
30+
31+
2332def is_pa_type_a_list (pa_type : pa .DataType ) -> bool :
2433 """Check if the given pyarrow type is a list type.
2534
@@ -108,7 +117,7 @@ def align_struct_list_offsets(array: pa.StructArray) -> pa.StructArray:
108117 value_lengths = list_array .value_lengths ()
109118 elif not value_lengths .equals (list_array .value_lengths ()):
110119 raise ValueError (
111- f"List lengths do not match for struct fields { array .type .fields [ 0 ] .name } and { field .name } " ,
120+ f"List lengths do not match for struct fields { array .type .field ( 0 ) .name } and { field .name } " ,
112121 )
113122
114123 list_arrays .append (
@@ -119,7 +128,7 @@ def align_struct_list_offsets(array: pa.StructArray) -> pa.StructArray:
119128 )
120129 new_array = pa .StructArray .from_arrays (
121130 arrays = list_arrays ,
122- fields = array .type . fields ,
131+ fields = struct_fields ( array .type ) ,
123132 )
124133 return new_array
125134
@@ -286,7 +295,7 @@ def validate_struct_list_type(t: pa.StructType) -> None:
286295 if not pa .types .is_struct (t ):
287296 raise ValueError (f"Expected a StructType, got { t } " )
288297
289- for field in t . fields :
298+ for field in struct_fields ( t ) :
290299 if not is_pa_type_a_list (field .type ):
291300 raise ValueError (f"Expected a ListType for field { field .name } , got { field .type } " )
292301
0 commit comments