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


##########
tests/integration/test_manifest_pruning_spec_evolution.py:
##########
@@ -0,0 +1,283 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+Regression / investigation test for manifest pruning correctness under 
partition spec evolution.
+
+Context
+-------
+PR #3011 (merged Feb 20 2026) added manifest pruning to _OverwriteFiles and 
_DeleteFiles
+in pyiceberg/table/update/snapshot.py. The pruning builds a partition 
predicate from the
+*current* partition spec and evaluates it against every manifest in the 
snapshot via a
+KeyDefaultDict of per-spec evaluators.
+
+The question this test file investigates:
+  When a table has been through partition spec evolution, its snapshot may 
contain manifests
+  written under *different* partition_spec_ids. Does the manifest evaluator 
correctly resolve
+  each manifest's own spec before deciding whether to include or skip it?
+
+If the answer is "no", the overwrite will silently skip manifests from the old 
spec, leaving
+stale data files that should have been deleted -- a silent correctness bug.
+
+How to run
+----------
+  pytest tests/integration/test_manifest_pruning_spec_evolution.py -v
+"""
+
+import tempfile
+from typing import Any
+
+import pyarrow as pa
+
+from pyiceberg.catalog import Catalog, load_catalog
+from pyiceberg.partitioning import PartitionField, PartitionSpec
+from pyiceberg.schema import Schema
+from pyiceberg.transforms import IdentityTransform
+from pyiceberg.types import LongType, NestedField, StringType
+
+# ---------------------------------------------------------------------------
+# Helpers
+# ---------------------------------------------------------------------------
+
+SCHEMA = Schema(
+    NestedField(field_id=1, name="category", field_type=StringType(), 
required=False),
+    NestedField(field_id=2, name="region", field_type=StringType(), 
required=False),
+    NestedField(field_id=3, name="value", field_type=LongType(), 
required=False),
+)
+
+# Spec 0: partitioned only by category
+SPEC_V0 = PartitionSpec(PartitionField(source_id=1, field_id=1000, 
transform=IdentityTransform(), name="category"))
+
+
+def make_catalog(warehouse: str) -> Catalog:
+    """Spin up a local SQLite-backed catalog -- no services needed."""
+    return load_catalog(
+        "test",
+        type="sql",
+        uri=f"sqlite:///{warehouse}/catalog.db",
+        warehouse=f"file://{warehouse}",
+    )
+
+
+def arrow_table(rows: list[dict[str, Any]]) -> pa.Table:
+    return pa.Table.from_pylist(
+        rows,
+        schema=pa.schema(
+            [
+                pa.field("category", pa.string()),
+                pa.field("region", pa.string()),
+                pa.field("value", pa.int64()),
+            ]
+        ),
+    )
+
+
+# ---------------------------------------------------------------------------
+# Test 1: Mixed spec snapshot -- overwrite partition present in both specs
+# ---------------------------------------------------------------------------
+
+
+def test_overwrite_after_partition_spec_evolution_correctness() -> None:
+    """
+    Verifies that dynamic_partition_overwrite correctly replaces ALL data files
+    for the target partition, including those written under a previous 
partition spec.
+
+    Setup:
+      - Spec 0: partition by identity(category)
+      - Write A(1,2,3) and B(10,11) under spec 0
+      - Evolve to spec 1: add identity(region)
+      - Write A(100,101) and B(200) under spec 1
+      - Overwrite category=A with new rows (999, 888)
+
+    Expected after overwrite:
+      - Only new A rows: values [888, 999]
+      - All B rows untouched: values [10, 11, 200]
+      - Total: 5 rows
+
+    Bug (pre-fix): spec-0 A manifests are skipped by the evaluator,
+    leaving stale A rows (1, 2, 3) in the table -> 8 rows total.

Review Comment:
   Hmm. I'm not sure if we can make this change. Existing users might be used 
to the existing behavior. When we change this, it will drop all many more rows 
than before.



-- 
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