joyhaldar commented on code in PR #15154:
URL: https://github.com/apache/iceberg/pull/15154#discussion_r2734712123


##########
spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java:
##########
@@ -174,14 +182,71 @@ public Dataset<FileInfo> expireFiles() {
 
       // fetch valid files after expiration
       TableMetadata updatedMetadata = ops.refresh();
-      Dataset<FileInfo> validFileDS = fileDS(updatedMetadata);
 
-      // fetch files referenced by expired snapshots
+      // find IDs of expired snapshots
       Set<Long> deletedSnapshotIds = findExpiredSnapshotIds(originalMetadata, 
updatedMetadata);
-      Dataset<FileInfo> deleteCandidateFileDS = fileDS(originalMetadata, 
deletedSnapshotIds);
+      if (deletedSnapshotIds.isEmpty()) {
+        this.expiredFileDS = emptyFileInfoDS();
+        return expiredFileDS;
+      }
+
+      Table originalTable = newStaticTable(originalMetadata, table.io());
+      Table updatedTable = newStaticTable(updatedMetadata, table.io());
+
+      Dataset<Row> expiredManifestDF =
+          loadMetadataTable(originalTable, MetadataTableType.ALL_MANIFESTS)
+              .filter(
+                  
col(AllManifestsTable.REF_SNAPSHOT_ID.name()).isInCollection(deletedSnapshotIds));
+
+      Dataset<Row> liveManifestDF =
+          loadMetadataTable(updatedTable, MetadataTableType.ALL_MANIFESTS);
+
+      Dataset<String> expiredManifestPaths =
+          
expiredManifestDF.select(col("path")).distinct().as(Encoders.STRING());
+
+      Dataset<String> liveManifestPaths =
+          liveManifestDF.select(col("path")).distinct().as(Encoders.STRING());
+
+      Dataset<String> orphanedManifestPaths = 
expiredManifestPaths.except(liveManifestPaths);
+
+      Dataset<FileInfo> expiredManifestLists = manifestListDS(originalTable, 
deletedSnapshotIds);
+      Dataset<FileInfo> liveManifestLists = manifestListDS(updatedTable, null);
+      Dataset<FileInfo> orphanedManifestLists = 
expiredManifestLists.except(liveManifestLists);
+
+      Dataset<FileInfo> expiredStats = statisticsFileDS(originalTable, 
deletedSnapshotIds);
+      Dataset<FileInfo> liveStats = statisticsFileDS(updatedTable, null);
+      Dataset<FileInfo> orphanedStats = expiredStats.except(liveStats);
+
+      if (orphanedManifestPaths.isEmpty()) {

Review Comment:
   Thanks for the review. `Dataset.isEmpty()` uses `limit(1)` and 
`executeTake(1)`, it only fetches a single row to check emptiness, not the full 
dataset.
   
   Source: 
https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/classic/Dataset.scala#L557-L560



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