@@ -1205,6 +1205,37 @@ def _pyarrow_schema_ensure_small_types(schema: pa.Schema) -> pa.Schema:
12051205 return visit_pyarrow (schema , _ConvertToSmallTypes ())
12061206
12071207
1208+ def _pyarrow_type_ensure_non_dictionary_types (field_type : pa .DataType ) -> pa .DataType :
1209+ if pa .types .is_dictionary (field_type ):
1210+ return _pyarrow_type_ensure_non_dictionary_types (field_type .value_type )
1211+ elif pa .types .is_struct (field_type ):
1212+ return pa .struct ([field .with_type (_pyarrow_type_ensure_non_dictionary_types (field .type )) for field in field_type ])
1213+ elif pa .types .is_list (field_type ):
1214+ return pa .list_ (field_type .value_field .with_type (_pyarrow_type_ensure_non_dictionary_types (field_type .value_type )))
1215+ elif pa .types .is_large_list (field_type ):
1216+ return pa .large_list (field_type .value_field .with_type (_pyarrow_type_ensure_non_dictionary_types (field_type .value_type )))
1217+ elif pa .types .is_fixed_size_list (field_type ):
1218+ return pa .list_ (
1219+ field_type .value_field .with_type (_pyarrow_type_ensure_non_dictionary_types (field_type .value_type )),
1220+ field_type .list_size ,
1221+ )
1222+ elif pa .types .is_map (field_type ):
1223+ return pa .map_ (
1224+ field_type .key_field .with_type (_pyarrow_type_ensure_non_dictionary_types (field_type .key_type )),
1225+ field_type .item_field .with_type (_pyarrow_type_ensure_non_dictionary_types (field_type .item_type )),
1226+ keys_sorted = field_type .keys_sorted ,
1227+ )
1228+ return field_type
1229+
1230+
1231+ def _pyarrow_table_ensure_non_dictionary_types (table : pa .Table ) -> pa .Table :
1232+ schema = pa .schema (
1233+ [field .with_type (_pyarrow_type_ensure_non_dictionary_types (field .type )) for field in table .schema ],
1234+ metadata = table .schema .metadata ,
1235+ )
1236+ return table .cast (schema ) if schema != table .schema else table
1237+
1238+
12081239@singledispatch
12091240def visit_pyarrow (obj : pa .DataType | pa .Schema , visitor : PyArrowSchemaVisitor [T ]) -> T :
12101241 """Apply a pyarrow schema visitor to any point within a schema.
@@ -1795,7 +1826,11 @@ def to_table(self, tasks: Iterable[FileScanTask]) -> pa.Table:
17951826 # Note: cannot use pa.Table.from_batches(itertools.chain([first_batch], batches)))
17961827 # as different batches can use different schema's (due to large_ types)
17971828 result = pa .concat_tables (
1798- (pa .Table .from_batches ([batch ]) for batch in itertools .chain ([first_batch ], batches )), promote_options = "permissive"
1829+ (
1830+ _pyarrow_table_ensure_non_dictionary_types (pa .Table .from_batches ([batch ]))
1831+ for batch in itertools .chain ([first_batch ], batches )
1832+ ),
1833+ promote_options = "permissive" ,
17991834 )
18001835
18011836 return result
0 commit comments