55
55
from pyiceberg .partitioning import (
56
56
PartitionSpec ,
57
57
)
58
+ from pyiceberg .table .refs import SnapshotRefType
58
59
from pyiceberg .table .snapshots import (
59
60
Operation ,
60
61
Snapshot ,
@@ -857,7 +858,7 @@ class ExpireSnapshots(UpdateTableMetadata["ExpireSnapshots"]):
857
858
Use table.expire_snapshots().<operation-one>().<operation-two>().commit() to run multiple operations.
858
859
Pending changes are applied on commit.
859
860
"""
860
-
861
+
861
862
_snapshot_ids_to_expire = set ()
862
863
_updates : Tuple [TableUpdate , ...] = ()
863
864
_requirements : Tuple [TableRequirement , ...] = ()
@@ -875,6 +876,21 @@ def _commit(self) -> UpdatesAndRequirements:
875
876
self ._updates += (update ,)
876
877
return self ._updates , self ._requirements
877
878
879
+ def _get_protected_snapshot_ids (self ):
880
+ """
881
+ Get the IDs of protected snapshots. These are the HEAD snapshots of all branches
882
+ and all tagged snapshots. These ids are to be excluded from expiration.
883
+ Returns:
884
+ Set of protected snapshot IDs to exclude from expiration.
885
+ """
886
+ protected_ids = set ()
887
+
888
+ for ref in self ._transaction .table_metadata .refs .values ():
889
+ if ref .snapshot_ref_type in [SnapshotRefType .TAG , SnapshotRefType .BRANCH ]:
890
+ protected_ids .add (ref .snapshot_id )
891
+
892
+ return protected_ids
893
+
878
894
def expire_snapshot_by_id (self , snapshot_id : int ) -> ExpireSnapshots :
879
895
"""
880
896
Expire a snapshot by its ID.
@@ -885,7 +901,13 @@ def expire_snapshot_by_id(self, snapshot_id: int) -> ExpireSnapshots:
885
901
Returns:
886
902
This for method chaining.
887
903
"""
904
+
888
905
if self ._transaction .table_metadata .snapshot_by_id (snapshot_id ) is None :
889
906
raise ValueError (f"Snapshot with ID { snapshot_id } does not exist." )
907
+
908
+ if snapshot_id in self ._get_protected_snapshot_ids ():
909
+ raise ValueError (f"Snapshot with ID { snapshot_id } is protected and cannot be expired." )
910
+
890
911
self ._snapshot_ids_to_expire .add (snapshot_id )
891
- return self
912
+
913
+ return self
0 commit comments