Skip to content

Commit 91a86fa

Browse files
committed
create rollback_to_snapshot, rollback_to_timestamp, set_current_snapshot apis
1 parent aa361d1 commit 91a86fa

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

pyiceberg/table/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,23 @@ def set_properties(self, properties: Properties = EMPTY_DICT, **kwargs: Any) ->
340340
updates = properties or kwargs
341341
return self._apply((SetPropertiesUpdate(updates=updates),))
342342

343+
def rollback_to_snapshot(self, snapshot_id: int) -> Transaction:
344+
if self.table_metadata.snapshot_by_id(snapshot_id) is None:
345+
raise ValidationError(f"Cannot roll back to unknown snapshot id: {snapshot_id}")
346+
if snapshot_id not in [ancestor.ancestor_id for ancestor in self.current_ancestors()]:
347+
raise ValidationError(f"Cannot roll back to snapshot, not an ancestor of the current state: {snapshot_id}")
348+
return self.set_ref_snapshot(snapshot_id=snapshot_id, ref_name="main", type="branch")
349+
350+
def rollback_to_timestamp(self, timestamp: int) -> Transaction:
351+
if (snapshot := self.latest_snapshot_before_timestamp(timestamp)) is None:
352+
raise ValidationError(f"Cannot roll back, no valid snapshot older than: {timestamp}")
353+
return self.set_ref_snapshot(snapshot_id=snapshot.snapshot_id, ref_name="main", type="branch")
354+
355+
def set_current_snapshot(self, snapshot_id: int) -> None:
356+
if self.table_metadata.snapshot_by_id(snapshot_id) is None:
357+
raise ValidationError(f"Cannot roll back to unknown snapshot id: {snapshot_id}")
358+
return self.set_ref_snapshot(snapshot_id=snapshot_id, ref_name="main", type="branch")
359+
343360
def update_schema(self, allow_incompatible_changes: bool = False, case_sensitive: bool = True) -> UpdateSchema:
344361
"""Create a new UpdateSchema to alter the columns of this table.
345362
@@ -1881,7 +1898,8 @@ def union_by_name(self, new_schema: Union[Schema, "pa.Schema"]) -> UpdateSchema:
18811898
visit_with_partner(
18821899
Catalog._convert_schema_if_needed(new_schema),
18831900
-1,
1884-
UnionByNameVisitor(update_schema=self, existing_schema=self._schema, case_sensitive=self._case_sensitive), # type: ignore
1901+
UnionByNameVisitor(update_schema=self, existing_schema=self._schema, case_sensitive=self._case_sensitive),
1902+
# type: ignore
18851903
PartnerIdByNameAccessor(partner_schema=self._schema, case_sensitive=self._case_sensitive),
18861904
)
18871905
return self

0 commit comments

Comments
 (0)