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


##########
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:
   There can be multiple references to the given idToRemove; so to get this 
information the grouping from retained snapshot id to the list of refs has to 
be computed. I do think the extra computation is worth it, it is odd to to 
throw an exception "There is a reference to it" without mentioning what the 
references are. 
   
   Will update to include the refs in the message. I think the message can 
contain list of refs for the given id directly, I think in practice there would 
not be so many refs for a given snapshot id that it would pollute the logs to 
log it.



-- 
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