This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-4363-bdebaf35ae5215b6679c418b926ca174bda8953b
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git

commit 46749af65be8cb091310316b8a23025b3a0f512d
Author: Ankit Kumar <[email protected]>
AuthorDate: Wed Nov 25 22:17:52 2026 +0000

    fix(inspect): map partition record by field id to support renamed partition 
fields (#4363)
    
    * fix(inspect): map partition record by field id to support renamed 
partition fields
    
    When a partition field is renamed (via update_spec().rename_field() or via 
v1 auto-renaming voided fields on re-add), specs_struct() adopts the latest 
field names per field_id. Previously, inspect.py constructed partition dicts 
using the historical spec's field names directly, causing PyArrow conversion to 
silently drop pre-rename partition values as None (or misattribute them to 
newly added fields sharing the old name).
    
    This updates InspectTable.entries(), partitions(), and 
_get_files_from_manifest() to map partition field values by field_id using 
specs_struct() names, and keys partitions_map by (spec_id, 
partition_record_dict) to track partition stats per specification layout.
    
    * style(inspect): use direct lookup for partition field ID mapping
---
 pyiceberg/table/inspect.py  | 19 ++++++++++-----
 tests/table/test_inspect.py | 58 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 70 insertions(+), 7 deletions(-)

diff --git a/pyiceberg/table/inspect.py b/pyiceberg/table/inspect.py
index 4feba3d53..7951d311b 100644
--- a/pyiceberg/table/inspect.py
+++ b/pyiceberg/table/inspect.py
@@ -195,6 +195,7 @@ class InspectTable:
 
         partition_record = self.tbl.metadata.specs_struct()
         pa_record_struct = schema_to_pyarrow(partition_record)
+        id_to_name = {field.field_id: field.name for field in 
partition_record.fields}
 
         entries_schema = pa.schema(
             [
@@ -254,8 +255,9 @@ class InspectTable:
                 }
 
                 partition = entry.data_file.partition
+                # specs_struct guarantees all partition field IDs across specs 
are mapped to their latest names
                 partition_record_dict = {
-                    field.name: partition[pos]
+                    id_to_name[field.field_id]: partition[pos]
                     for pos, field in 
enumerate(self.tbl.metadata.specs()[manifest.partition_spec_id].fields)
                 }
 
@@ -386,6 +388,7 @@ class InspectTable:
         spec_ids = {manifest.partition_spec_id for manifest in 
snapshot.manifests(self.tbl.io, self.tbl.encryption_manager)}
         partition_record = self.tbl.metadata.specs_struct(spec_ids=spec_ids)
         has_partitions = len(partition_record.fields) > 0
+        id_to_name = {field.field_id: field.name for field in 
partition_record.fields}
 
         if has_partitions:
             pa_record_struct = schema_to_pyarrow(partition_record)
@@ -407,13 +410,15 @@ class InspectTable:
             encryption_manager=self.tbl.encryption_manager,
         )
 
-        partitions_map: dict[tuple[str, Any], Any] = {}
+        partitions_map: dict[tuple[int, Any], Any] = {}
         snapshots_by_id = self._get_snapshots_by_id()
 
         for entry in 
itertools.chain.from_iterable(scan._plan_manifest_entries()):
             partition = entry.data_file.partition
+            # specs_struct guarantees all partition field IDs across specs are 
mapped to their latest names
             partition_record_dict = {
-                field.name: partition[pos] for pos, field in 
enumerate(self.tbl.metadata.specs()[entry.data_file.spec_id].fields)
+                id_to_name[field.field_id]: partition[pos]
+                for pos, field in 
enumerate(self.tbl.metadata.specs()[entry.data_file.spec_id].fields)
             }
             entry_snapshot = snapshots_by_id.get(entry.snapshot_id) if 
entry.snapshot_id is not None else None
             self._update_partitions_map_from_manifest_entry(
@@ -427,7 +432,7 @@ class InspectTable:
 
     def _update_partitions_map_from_manifest_entry(
         self,
-        partitions_map: dict[tuple[str, Any], Any],
+        partitions_map: dict[tuple[int, Any], Any],
         file: DataFile,
         partition_record_dict: dict[str, Any],
         snapshot: Snapshot | None,
@@ -444,7 +449,7 @@ class InspectTable:
             partition_record_dict: Maps partition field names to their values.
             snapshot: The owning snapshot, used to track last-updated 
timestamps.
         """
-        partition_record_key = _convert_to_hashable_type(partition_record_dict)
+        partition_record_key = (file.spec_id, 
_convert_to_hashable_type(partition_record_dict))
         if partition_record_key not in partitions_map:
             partitions_map[partition_record_key] = {
                 "partition": partition_record_dict,
@@ -737,6 +742,7 @@ class InspectTable:
 
         files: list[dict[str, Any]] = []
         schema = self.tbl.metadata.schema()
+        id_to_name = {field.field_id: field.name for field in 
self.tbl.metadata.specs_struct().fields}
         io = self.tbl.io
 
         for manifest_entry in manifest_list.fetch_manifest_entry(io):
@@ -761,8 +767,9 @@ class InspectTable:
                 for field in self.tbl.metadata.schema().fields
             }
             partition = data_file.partition
+            # specs_struct guarantees all partition field IDs across specs are 
mapped to their latest names
             partition_record_dict = {
-                field.name: partition[pos]
+                id_to_name[field.field_id]: partition[pos]
                 for pos, field in 
enumerate(self.tbl.metadata.specs()[manifest_list.partition_spec_id].fields)
             }
             files.append(
diff --git a/tests/table/test_inspect.py b/tests/table/test_inspect.py
index e507b62f7..4dc59e8cb 100644
--- a/tests/table/test_inspect.py
+++ b/tests/table/test_inspect.py
@@ -116,7 +116,7 @@ def 
test_partitions_last_updated_uses_latest_snapshot_regardless_of_order(newest
     data_file.spec_id = 0
 
     inspect = InspectTable.__new__(InspectTable)
-    partitions_map: dict[tuple[str, Any], Any] = {}
+    partitions_map: dict[tuple[int, Any], Any] = {}
     for snapshot in [newer, older] if newest_first else [older, newer]:
         inspect._update_partitions_map_from_manifest_entry(partitions_map, 
data_file, {"part": "a"}, snapshot)
 
@@ -166,3 +166,59 @@ def test_inspect_bounds_after_type_promotion(catalog: 
InMemoryCatalog) -> None:
         partition_summaries = 
tbl.inspect.manifests().to_pydict()["partition_summaries"][0]
         assert partition_summaries[0]["lower_bound"] == str(values[0]), name
         assert partition_summaries[0]["upper_bound"] == str(values[-1]), name
+
+
+def test_inspect_partitions_and_files_after_rename_field(catalog: 
InMemoryCatalog) -> None:
+    schema = Schema(
+        NestedField(1, "id", LongType(), required=False),
+        NestedField(2, "p", StringType(), required=False),
+    )
+    spec = PartitionSpec(PartitionField(2, 1000, IdentityTransform(), "p"))
+    tbl = catalog.create_table("default.renamed_partition_col", schema, 
partition_spec=spec)
+    tbl.append(pa.table({"id": [1, 2], "p": ["a", "b"]}))
+
+    with tbl.update_spec() as spec_update:
+        spec_update.rename_field("p", "p_renamed")
+    tbl.append(pa.table({"id": [3], "p": ["c"]}))
+
+    partitions = tbl.inspect.partitions().to_pydict()
+    assert sorted(part["p_renamed"] for part in partitions["partition"]) == 
["a", "b", "c"]
+    assert set(partitions["spec_id"]) == {0, 1}
+
+    for files_table in [
+        tbl.inspect.files(),
+        tbl.inspect.data_files(),
+        tbl.inspect.all_files(),
+        tbl.inspect.all_data_files(),
+    ]:
+        assert sorted(part["p_renamed"] for part in 
files_table.to_pydict()["partition"]) == ["a", "b", "c"]
+
+    entries = tbl.inspect.entries().to_pydict()
+    assert sorted(df["partition"]["p_renamed"] for df in entries["data_file"]) 
== ["a", "b", "c"]
+
+
+def test_inspect_files_after_v1_partition_field_readd(catalog: 
InMemoryCatalog) -> None:
+    schema = Schema(
+        NestedField(1, "id", LongType(), required=False),
+        NestedField(2, "p", StringType(), required=False),
+    )
+    spec = PartitionSpec(PartitionField(2, 1000, IdentityTransform(), "p"))
+    tbl = catalog.create_table("default.v1_partition_readd", schema, 
partition_spec=spec, properties={"format-version": "1"})
+    tbl.append(pa.table({"id": [1, 2], "p": ["a", "b"]}))
+
+    with tbl.update_spec() as spec_update:
+        spec_update.remove_field("p")
+    with tbl.update_spec() as spec_update:
+        spec_update.add_identity("p")
+
+    # In v1, the voided field is renamed to p_1000 and the new identity field 
is assigned id 1001 with name p.
+    # Pre-existing data files from spec 0 belong to field 1000 (p_1000), not 
the newly added field 1001 (p).
+    for files_table in [tbl.inspect.files(), tbl.inspect.all_files()]:
+        partitions = files_table.to_pydict()["partition"]
+        assert sorted(part["p_1000"] for part in partitions) == ["a", "b"]
+        assert all(part["p"] is None for part in partitions)
+
+    entries = tbl.inspect.entries().to_pydict()
+    entry_partitions = [df["partition"] for df in entries["data_file"]]
+    assert sorted(part["p_1000"] for part in entry_partitions) == ["a", "b"]
+    assert all(part["p"] is None for part in entry_partitions)

Reply via email to