-
Notifications
You must be signed in to change notification settings - Fork 425
feat: Add support for rolling back to timestamp #2879
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 all commits
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 |
|---|---|---|
|
|
@@ -472,3 +472,24 @@ def ancestors_between(from_snapshot: Snapshot | None, to_snapshot: Snapshot, tab | |
| break | ||
| else: | ||
| yield from ancestors_of(to_snapshot, table_metadata) | ||
|
|
||
|
|
||
| def latest_ancestor_before_timestamp(table_metadata: TableMetadata, timestamp_ms: int) -> Snapshot | None: | ||
| """Find the latest ancestor snapshot whose timestamp is before the provided timestamp. | ||
|
Comment on lines
+477
to
+478
Contributor
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. It might be nice from a users perspective to allow this to be a datetime as well? Does differ from the java impl though... |
||
|
|
||
| Args: | ||
| table_metadata: The table metadata for a table | ||
| timestamp_ms: lookup snapshots strictly before this timestamp | ||
|
|
||
| Returns: | ||
| The latest ancestor snapshot older than the timestamp, or None if not found. | ||
| """ | ||
| result: Snapshot | None = None | ||
| result_timestamp: int = 0 | ||
|
|
||
| for ancestor in ancestors_of(table_metadata.current_snapshot(), table_metadata): | ||
| if timestamp_ms > ancestor.timestamp_ms > result_timestamp: | ||
| result = ancestor | ||
| result_timestamp = ancestor.timestamp_ms | ||
|
Comment on lines
+490
to
+493
Contributor
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. what about using a max(filter(...)) statement here? I think a little easier to follow than the double greater than expression?
Contributor
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. maybe not, this does match the java |
||
|
|
||
| return result | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,7 @@ | |
| SnapshotSummaryCollector, | ||
| Summary, | ||
| ancestors_of, | ||
| latest_ancestor_before_timestamp, | ||
| update_snapshot_summaries, | ||
| ) | ||
| from pyiceberg.table.update import ( | ||
|
|
@@ -1008,6 +1009,26 @@ def rollback_to_snapshot(self, snapshot_id: int) -> ManageSnapshots: | |
|
|
||
| return self.set_current_snapshot(snapshot_id=snapshot_id) | ||
|
|
||
| def rollback_to_timestamp(self, timestamp_ms: int) -> ManageSnapshots: | ||
|
Contributor
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. could add support for datetime too. |
||
| """Rollback the table to the latest snapshot before the given timestamp. | ||
|
|
||
| Finds the latest ancestor snapshot whose timestamp is before the given timestamp and rolls back to it. | ||
|
|
||
| Args: | ||
| timestamp_ms: Rollback to the latest snapshot before this timestamp in milliseconds. | ||
|
|
||
| Returns: | ||
| This for method chaining | ||
|
|
||
| Raises: | ||
| ValueError: If no valid snapshot exists older than the given timestamp. | ||
| """ | ||
| snapshot = latest_ancestor_before_timestamp(self._transaction.table_metadata, timestamp_ms) | ||
| if snapshot is None: | ||
| raise ValueError(f"Cannot roll back, no valid snapshot older than: {timestamp_ms}") | ||
|
|
||
| return self.set_current_snapshot(snapshot_id=snapshot.snapshot_id) | ||
|
|
||
| def _is_current_ancestor(self, snapshot_id: int) -> bool: | ||
| return snapshot_id in self._current_ancestors() | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.