Fokko commented on code in PR #4359:
URL: https://github.com/apache/iceberg-python/pull/4359#discussion_r4700853778


##########
pyiceberg/io/pyarrow.py:
##########
@@ -2760,6 +2760,25 @@ def _nan_value_counts_from_arrow(table: pa.Table, 
stats_columns: dict[int, Stati
     return nan_value_counts
 
 
+def _file_parquet_path_to_id_mapping(parquet_metadata: pq.FileMetaData, 
file_schema: Schema) -> dict[str, int]:
+    """
+    Compute the mapping of the parquet column paths of a file to the Iceberg 
IDs of its columns.
+
+    Unlike `parquet_path_to_id_mapping`, the paths are the ones stored in the 
file, so a list element keeps
+    the name that the file gives it. The IDs come from the schema of the file, 
so a column keeps its ID
+    when the table has renamed it.
+
+    Args:
+        parquet_metadata (pyarrow.parquet.FileMetaData): The metadata of the 
parquet file.
+        file_schema (pyiceberg.schema.Schema): The Iceberg schema of the file, 
as returned by
+            `_check_pyarrow_schema_compatible`.
+    """
+    # The leaf fields of the file schema are in the same depth-first order as 
the parquet columns.
+    field_ids = [pair.field_id for pair in pre_order_visit(file_schema, 
ID2ParquetPathVisitor())]
+    paths = [parquet_metadata.schema.column(pos).path for pos in 
range(parquet_metadata.num_columns)]
+    return dict(zip(paths, field_ids, strict=True))

Review Comment:
   Nit: `column(pos).path` uses legacy names (list element `item`), while 
`data_file_statistics_from_parquet_metadata` looks stats up with 
`column.path_in_schema` (compliant `list.element`). The two spellings differ 
for lists, so the mapping below is keyed by the legacy spelling on purpose, but 
nothing says so. Worth a comment so a future cleanup to `path_in_schema` here 
does not silently change the keys.
   
   Also a question: for a file without field IDs, 
`_check_pyarrow_schema_compatible` resolves IDs through the table's 
`name_mapping`, which is built from current names. A pre-rename file without 
IDs would then miss its renamed column (or, after a drop plus name reuse, map 
to the wrong ID). Is that case intentionally out of scope, since it needs the 
name mapping rather than the file?
   
   ```suggestion
       # The leaf fields of the file schema are in the same depth-first order 
as the parquet columns.
       # Note: `column(pos).path` spells the list element `item`, while 
`path_in_schema` (used for
       # the stats lookup) spells it `element`; the mapping is keyed by the 
file's own paths on purpose.
       field_ids = [pair.field_id for pair in pre_order_visit(file_schema, 
ID2ParquetPathVisitor())]
       paths = [parquet_metadata.schema.column(pos).path for pos in 
range(parquet_metadata.num_columns)]
       return dict(zip(paths, field_ids, strict=True))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to