Skip to content

Commit ce3515c

Browse files
committed
Added: public method signature to the init table file.
Moved: the functions for expiring snapshots to their own class.
1 parent 12729fa commit ce3515c

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

pyiceberg/table/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
ManageSnapshots,
120120
UpdateSnapshot,
121121
_FastAppendFiles,
122+
ExpireSnapshots
122123
)
123124
from pyiceberg.table.update.spec import UpdateSpec
124125
from pyiceberg.table.update.statistics import UpdateStatistics
@@ -1078,6 +1079,15 @@ def manage_snapshots(self) -> ManageSnapshots:
10781079
ms.create_tag(snapshot_id1, "Tag_A").create_tag(snapshot_id2, "Tag_B")
10791080
"""
10801081
return ManageSnapshots(transaction=Transaction(self, autocommit=True))
1082+
1083+
def expire_snapshots(self) -> ExpireSnapshots:
1084+
"""
1085+
Shorthand to run expire snapshots by id or by a timestamp.
1086+
1087+
Use table.expire_snapshots().<operation>().commit() to run a specific operation.
1088+
Use table.expire_snapshots().<operation-one>().<operation-two>().commit() to run multiple operations.
1089+
"""
1090+
return ExpireSnapshots(transaction=Transaction(self, autocommit=True))
10811091

10821092
def update_statistics(self) -> UpdateStatistics:
10831093
"""

pyiceberg/table/update/snapshot.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,18 @@ def remove_branch(self, branch_name: str) -> ManageSnapshots:
850850
"""
851851
return self._remove_ref_snapshot(ref_name=branch_name)
852852

853+
class ExpireSnapshots(UpdateTableMetadata["ExpireSnapshots"]):
854+
"""
855+
Expire snapshots by ID or by timestamp.
856+
Use table.expire_snapshots().<operation>().commit() to run a specific operation.
857+
Use table.expire_snapshots().<operation-one>().<operation-two>().commit() to run multiple operations.
858+
Pending changes are applied on commit.
859+
"""
860+
861+
_snapshot_ids_to_expire = set()
862+
_updates: Tuple[TableUpdate, ...] = ()
863+
_requirements: Tuple[TableRequirement, ...] = ()
864+
853865
def _commit(self) -> UpdatesAndRequirements:
854866
"""
855867
Commit the staged updates and requirements.
@@ -863,7 +875,7 @@ def _commit(self) -> UpdatesAndRequirements:
863875
self._updates += (update,)
864876
return self._updates, self._requirements
865877

866-
def expire_snapshot_by_id(self, snapshot_id: int) -> ManageSnapshots:
878+
def expire_snapshot_by_id(self, snapshot_id: int) -> ExpireSnapshots:
867879
"""
868880
Expire a snapshot by its ID.
869881
@@ -878,7 +890,7 @@ def expire_snapshot_by_id(self, snapshot_id: int) -> ManageSnapshots:
878890
self._snapshot_ids_to_expire.add(snapshot_id)
879891
return self
880892

881-
def expire_snapshots_older_than(self, timestamp_ms: int) -> ManageSnapshots:
893+
def expire_snapshots_older_than(self, timestamp_ms: int) -> ExpireSnapshots:
882894
"""
883895
Expire snapshots older than the given timestamp.
884896

tests/table/test_expire_snapshots.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_expire_snapshot(table_v2: Table) -> None:
2828

2929
# Expire the snapshot directly without using a transaction
3030
try:
31-
table_v2.manage_snapshots().expire_snapshot_by_id(EXPIRE_SNAPSHOT).commit()
31+
table_v2.expire_snapshots().expire_snapshot_by_id(EXPIRE_SNAPSHOT).commit()
3232
except Exception as e:
3333
assert False, f"Commit failed with error: {e}"
3434

0 commit comments

Comments
 (0)