-
Notifications
You must be signed in to change notification settings - Fork 473
feat: Add metadata-only replace API to Table for REPLACE snapshot operations #3131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 14 commits
19e8dee
6800bb4
e5e11b9
070c885
f12fa5d
94bd87e
a2f2b18
356d704
87f6848
41eb549
a91dfb4
ef9b84f
596df80
33aaef0
b0a770c
59f555e
c8162a8
d939b67
c3570d8
d7e89db
9681ec3
8f1f9b9
c60d5ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -667,6 +667,96 @@ def _get_entries(manifest: ManifestFile) -> list[ManifestEntry]: | |||
| return [] | ||||
|
|
||||
|
|
||||
| class _RewriteFiles(_SnapshotProducer["_RewriteFiles"]): | ||||
| """A snapshot producer that rewrites data files.""" | ||||
|
|
||||
| def __init__(self, operation: Operation, transaction: Transaction, io: FileIO, snapshot_properties: dict[str, str]): | ||||
| super().__init__(operation, transaction, io, snapshot_properties=snapshot_properties) | ||||
|
|
||||
| def _commit(self) -> UpdatesAndRequirements: | ||||
| # Only produce a commit when there is something to rewrite | ||||
| if self._deleted_data_files or self._added_data_files: | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can replicate the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
| # 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 ValueError("Cannot delete files that are not present 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: | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where are you seeing this invariant? I mean this seems correct since the spec says rewrite must be "logically equivalent". This check could reasonable as a safety guard, but what happens when delete file rewriting is added? Then these numbers could be incorrect.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @geruh, thanks for flagging this, you're right that this is a safety guard, but it doesn't yet factor in future changes when adding delete file rewriting. Should we add something like this? # Note: This physical record count invariant is a sanity guard for data file
# compaction to ensure no data is accidentally duplicated or invented.
# TODO: This will need to be evolved into a logical record count validation
# once PyIceberg supports rewriting delete files (Merge-on-Read).
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 ValueError(f"Invalid replace: records added ({added_records}) exceeds records removed ({deleted_records})")This logical record count validation would involve something like having the
Otherwise, I can also remove it from
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay let's keep the check I took a deeper look into the snapshot producer on the java side so let's align closer to that: |
||||
| raise ValueError(f"Invalid replace: records added ({added_records}) exceeds records removed ({deleted_records})") | ||||
|
|
||||
| return super()._commit() | ||||
| else: | ||||
| return (), () | ||||
|
|
||||
| def _deleted_entries(self) -> list[ManifestEntry]: | ||||
| """Check if we need to mark the files as deleted.""" | ||||
| if self._parent_snapshot_id is not None: | ||||
| previous_snapshot = self._transaction.table_metadata.snapshot_by_id(self._parent_snapshot_id) | ||||
| if previous_snapshot is None: | ||||
| raise ValueError(f"Could not find the previous snapshot: {self._parent_snapshot_id}") | ||||
|
|
||||
| executor = ExecutorFactory.get_or_create() | ||||
|
|
||||
| def _get_entries(manifest: ManifestFile) -> list[ManifestEntry]: | ||||
| return [ | ||||
| ManifestEntry.from_args( | ||||
| status=ManifestEntryStatus.DELETED, | ||||
| snapshot_id=self.snapshot_id, | ||||
| sequence_number=entry.sequence_number, | ||||
| file_sequence_number=entry.file_sequence_number, | ||||
| data_file=entry.data_file, | ||||
| ) | ||||
| for entry in manifest.fetch_manifest_entry(self._io, discard_deleted=True) | ||||
| if entry.data_file.content == DataFileContent.DATA and entry.data_file in self._deleted_data_files | ||||
| ] | ||||
|
|
||||
| list_of_entries = executor.map(_get_entries, previous_snapshot.manifests(self._io)) | ||||
| return list(itertools.chain(*list_of_entries)) | ||||
| else: | ||||
| return [] | ||||
|
|
||||
| def _existing_manifests(self) -> list[ManifestFile]: | ||||
| """To determine if there are any existing manifests.""" | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic looks nearly identical to the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
| existing_files = [] | ||||
| if snapshot := self._transaction.table_metadata.snapshot_by_name(name=self._target_branch): | ||||
| for manifest_file in snapshot.manifests(io=self._io): | ||||
| entries_to_write: set[ManifestEntry] = set() | ||||
| found_deleted_entries: set[ManifestEntry] = set() | ||||
|
|
||||
| for entry in manifest_file.fetch_manifest_entry(io=self._io, discard_deleted=True): | ||||
| if entry.data_file in self._deleted_data_files: | ||||
| found_deleted_entries.add(entry) | ||||
| else: | ||||
| entries_to_write.add(entry) | ||||
|
|
||||
| if len(found_deleted_entries) == 0: | ||||
| existing_files.append(manifest_file) | ||||
| continue | ||||
|
|
||||
| if len(entries_to_write) == 0: | ||||
| continue | ||||
|
|
||||
| with self.new_manifest_writer(self.spec(manifest_file.partition_spec_id)) as writer: | ||||
| for entry in entries_to_write: | ||||
| writer.add_entry( | ||||
| ManifestEntry.from_args( | ||||
| status=ManifestEntryStatus.EXISTING, | ||||
| snapshot_id=entry.snapshot_id, | ||||
| sequence_number=entry.sequence_number, | ||||
| file_sequence_number=entry.file_sequence_number, | ||||
| data_file=entry.data_file, | ||||
| ) | ||||
| ) | ||||
| existing_files.append(writer.to_manifest_file()) | ||||
| return existing_files | ||||
|
|
||||
|
|
||||
| class UpdateSnapshot: | ||||
| _transaction: Transaction | ||||
| _io: FileIO | ||||
|
|
@@ -724,6 +814,14 @@ def delete(self) -> _DeleteFiles: | |||
| snapshot_properties=self._snapshot_properties, | ||||
| ) | ||||
|
|
||||
| def replace(self) -> _RewriteFiles: | ||||
| return _RewriteFiles( | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sort of confused by the naming since we are introducing a user facing API
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @geruh, you bring up a good point, and it's something I noticed seemed off along the way. The reason why we have this discrepancy is because we're mirroring what's found in the Java code itself.
I named the Python API That said, if you feel strongly about matching the Java API's user-facing method (
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah there is a bit of a distinction here, since rewrite is basically the rewrite of data files and replace is the logical change to your snapshot metadata. My thinking is that the users in java today are used to interacting with this api through: table.newRewrite()
.deleteFile(old)
.addFile(new)
.commit();So someone coming from Java Iceberg will look for rewrite, not replace. But ultimately maybe there is more of a history as to why the it follows this naming convention im missing on. WDYT @kevinjqliu? |
||||
| operation=Operation.REPLACE, | ||||
| transaction=self._transaction, | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that branch is missing here is there a reason for that?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
| io=self._io, | ||||
| snapshot_properties=self._snapshot_properties, | ||||
| ) | ||||
|
|
||||
|
|
||||
| class _ManifestMergeManager(Generic[U]): | ||||
| _target_size_bytes: int | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we can remove the constructor here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @geruh, the changes have been made in c3570d8 as part of adding
branchas an arg for_RewriteFiles.