@@ -1112,6 +1112,18 @@ fn build_field_id_map(parquet_schema: &SchemaDescriptor) -> Result<Option<HashMa
11121112
11131113/// Build a fallback field ID map for Parquet files without embedded field IDs.
11141114///
1115+ /// Returns the number of primitive (leaf) columns in a Parquet type, recursing into groups.
1116+ fn leaf_count ( ty : & parquet:: schema:: types:: Type ) -> usize {
1117+ if ty. is_primitive ( ) {
1118+ 1
1119+ } else {
1120+ ty. get_fields ( ) . iter ( ) . map ( |f| leaf_count ( f) ) . sum ( )
1121+ }
1122+ }
1123+
1124+ /// Builds a mapping from fallback field IDs to leaf column indices for Parquet files
1125+ /// without embedded field IDs. Returns entries only for primitive top-level fields.
1126+ ///
11151127/// Must use top-level field positions (not leaf column positions) to stay consistent
11161128/// with `add_fallback_field_ids_to_arrow_schema`, which assigns ordinal IDs to
11171129/// top-level Arrow fields. Using leaf positions instead would produce wrong indices
@@ -1125,19 +1137,10 @@ fn build_fallback_field_id_map(parquet_schema: &SchemaDescriptor) -> HashMap<i32
11251137
11261138 for ( top_pos, field) in parquet_schema. root_schema ( ) . get_fields ( ) . iter ( ) . enumerate ( ) {
11271139 let field_id = ( top_pos + 1 ) as i32 ;
1128-
11291140 if field. is_primitive ( ) {
11301141 column_map. insert ( field_id, leaf_idx) ;
1131- leaf_idx += 1 ;
1132- } else {
1133- // Advance past all leaves in this group. Count them by checking
1134- // how many subsequent leaves share this root index.
1135- while leaf_idx < parquet_schema. num_columns ( )
1136- && parquet_schema. get_column_root_idx ( leaf_idx) == top_pos
1137- {
1138- leaf_idx += 1 ;
1139- }
11401142 }
1143+ leaf_idx += leaf_count ( field) ;
11411144 }
11421145
11431146 column_map
0 commit comments