huaxingao commented on code in PR #18348:
URL: https://github.com/apache/iceberg/pull/18348#discussion_r4212023701


##########
spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkMicroBatchStream.java:
##########
@@ -244,15 +253,32 @@ public ReadLimit getDefaultReadLimit() {
   public void prepareForTriggerAvailableNow() {
     LOG.info("The streaming query reports to use Trigger.AvailableNow");
 
-    lastOffsetForTriggerAvailableNow =
+    StreamingOffset lastOffset =
         (StreamingOffset) latestOffset(initialOffset, 
ReadLimit.allAvailable());
+    // START_OFFSET means that no snapshot matched stream-from-timestamp. A 
new stream has nothing
+    // to read in this run, but a resumed stream continues from its offset and 
needs an actual cap.
+    this.noSnapshotMatchedForTriggerAvailableNow = 
StreamingOffset.START_OFFSET.equals(lastOffset);
+    this.lastOffsetForTriggerAvailableNow =
+        noSnapshotMatchedForTriggerAvailableNow ? latestAppendOffset() : 
lastOffset;
 
-    LOG.info("lastOffset for Trigger.AvailableNow is {}", 
lastOffsetForTriggerAvailableNow.json());
+    LOG.info("lastOffset for Trigger.AvailableNow is {}", 
lastOffsetForTriggerAvailableNow);
 
     // Reset planner so it gets recreated with the cap on next call
     if (planner != null) {
       planner.stop();
       planner = null;
     }
   }
+
+  // The planners only read append snapshots. A cap at any other snapshot is 
never reached.
+  private StreamingOffset latestAppendOffset() {
+    for (Snapshot snapshot : SnapshotUtil.currentAncestors(table)) {
+      if (DataOperations.APPEND.equals(snapshot.operation())) {
+        return new StreamingOffset(
+            snapshot.snapshotId(), MicroBatchUtils.addedFilesCount(table, 
snapshot), false);
+      }
+    }
+
+    return null;
+  }

Review Comment:
   +1. This method has its own rule for which snapshots count ("only APPEND"), 
but the planners use `shouldProcess`. The two rules can disagree today: with 
`streaming-skip-delete-snapshots=false`, `shouldProcess` throws on a DELETE 
snapshot, but this method skips it. Could this method reuse the `shouldProcess` 
logic? Then the two can't get out of sync.



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