qzyu999 commented on code in PR #3131:
URL: https://github.com/apache/iceberg-python/pull/3131#discussion_r3983953126


##########
pyiceberg/table/update/snapshot.py:
##########
@@ -852,6 +850,120 @@ def _get_entries(manifest: ManifestFile) -> 
list[ManifestEntry]:
             return []
 
 
+class _RewriteFiles(_SnapshotProducer["_RewriteFiles"]):
+    """A snapshot producer that rewrites data files.
+
+    Produces a REPLACE snapshot that swaps existing data files for new ones 
without
+    changing the logical contents of the table. This is the metadata-only 
operation
+    used by compaction (bin-packing, sort, format migration).
+
+    Current scope:
+        - Data file rewriting only (delete + add DataFiles)
+        - Validates: files-to-delete exist, added_records <= deleted_records,
+          no new delete files conflict with replaced data files
+
+    Future work (additive — no structural changes needed):
+        - Delete-file rewriting (add _deleted_delete_files set + separate 
manifest handling)
+        - dataSequenceNumber override (pin new files' seq to match replaced, 
for eq-delete safety)
+        - validateFromSnapshot (expose _starting_snapshot_id setter for 
long-running planners)
+        - ignoreEqualityDeletes in validation (coupled with dataSequenceNumber)
+    """
+
+    def _commit(self) -> UpdatesAndRequirements:
+        # Only produce a commit when there is something to rewrite
+        if self._deleted_data_files or self._added_data_files:
+            # Grab the entries that we actually found in the table's manifests
+            deleted_entries = self._deleted_entries()
+            found_deleted_files = {entry.data_file for entry in 
deleted_entries}
+
+            # If the user asked to delete files that aren't in the table, 
abort.
+            if len(found_deleted_files) != len(self._deleted_data_files):
+                raise ValidationException("Cannot commit, missing data files 
to be rewritten that are not in the table")
+
+            added_records = sum(f.record_count for f in self._added_data_files)
+            deleted_records = sum(entry.data_file.record_count for entry in 
deleted_entries)
+
+            if added_records > deleted_records:
+                raise ValidationException(
+                    f"Invalid replace: records added ({added_records}) exceeds 
records removed ({deleted_records})"
+                )
+
+            return super()._commit()
+        else:
+            return (), ()
+
+    @cached_property
+    def _cached_deleted_entries(self) -> list[ManifestEntry]:

Review Comment:
   Done, `_cached_deleted_entries` and the executor/manifest-scanning 
boilerplate now live in `_SnapshotProducer`. Each subclass just overrides 
`_get_deleted_manifest_entries(manifest)` to control which entries get marked 
as `DELETED`. `_OverwriteFiles` adds partition-level evaluator pruning in its 
override; `_RewriteFiles` matches on `_deleted_data_files` directly.



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