amogh-jahagirdar commented on code in PR #4578:
URL: https://github.com/apache/iceberg/pull/4578#discussion_r873228264


##########
core/src/main/java/org/apache/iceberg/RemoveSnapshots.java:
##########
@@ -155,27 +168,93 @@ public List<Snapshot> apply() {
     TableMetadata updated = internalApply();
     List<Snapshot> removed = Lists.newArrayList(base.snapshots());
     removed.removeAll(updated.snapshots());
-
     return removed;
   }
 
   private TableMetadata internalApply() {
     this.base = ops.refresh();
+    if (base.refs().isEmpty()) {
+      return base;
+    }
 
     Set<Long> idsToRetain = Sets.newHashSet();
-    List<Long> ancestorIds = SnapshotUtil.ancestorIds(base.currentSnapshot(), 
base::snapshot);
-    if (minNumSnapshots >= ancestorIds.size()) {
-      idsToRetain.addAll(ancestorIds);
-    } else {
-      idsToRetain.addAll(ancestorIds.subList(0, minNumSnapshots));
+    // Identify refs that should be removed
+    Map<String, SnapshotRef> retainedRefs = computeRetainedRefs(base.refs());
+    Set<Long> retainedRefIds = retainedRefs.entrySet().stream()
+        .map(refEntry -> refEntry.getValue().snapshotId())
+        .collect(Collectors.toSet());
+
+    for (long idToRemove : idsToRemove) {
+      Preconditions.checkArgument(!retainedRefIds.contains(idToRemove),
+          "Cannot expire %s. There is a reference to it", idToRemove);

Review Comment:
   I've updated to maintain the refs associated with a given snapshot ID so 
that the exception message could be clearer. @rdblue let me know what you 
think! 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to