This is an automated email from the ASF dual-hosted git repository.

dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new 1f7a4c80a21d [SPARK-57536][SS] Use `maxOption` instead of 
`sorted.lastOption` in `HDFSMetadataLog`
1f7a4c80a21d is described below

commit 1f7a4c80a21dcc5470c4feef121f8ca020c93129
Author: Kousuke Saruta <[email protected]>
AuthorDate: Thu Jun 18 13:02:10 2026 -0700

    [SPARK-57536][SS] Use `maxOption` instead of `sorted.lastOption` in 
`HDFSMetadataLog`
    
    ### What changes were proposed in this pull request?
    
    This PR replaces `listBatches.sorted.lastOption` with 
`listBatches.maxOption` in `HDFSMetadataLog.getLatestBatchId()` and 
`HDFSMetadataLog.getLatest()`.
    
    ### Why are the changes needed?
    
    The intent of the code is to find the maximum batch ID. `sorted.lastOption` 
sorts the entire array in O(n log n) to retrieve only the maximum element, 
while `maxOption` achieves the same result in O(n). These methods are called on 
every micro-batch in Structured Streaming, so avoiding unnecessary sorting 
reduces overhead for long-running streaming jobs with many batches.
    
    ### Does this PR introduce _any_ user-facing change?
    No.
    
    ### How was this patch tested?
    GA
    
    ### Was this patch authored or co-authored using generative AI tooling?
    Kiro CLI / Claude
    
    Closes #56596 from sarutak/use-maxOption-in-HDFSMetadataLog.
    
    Authored-by: Kousuke Saruta <[email protected]>
    Signed-off-by: Dongjoon Hyun <[email protected]>
    (cherry picked from commit 982baad5b8ad8415f4d95a4e8bedd7ef016da66f)
    Signed-off-by: Dongjoon Hyun <[email protected]>
---
 .../spark/sql/execution/streaming/checkpointing/HDFSMetadataLog.scala | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/checkpointing/HDFSMetadataLog.scala
 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/checkpointing/HDFSMetadataLog.scala
index fac502f75f3a..b06a90de44c4 100644
--- 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/checkpointing/HDFSMetadataLog.scala
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/checkpointing/HDFSMetadataLog.scala
@@ -266,10 +266,10 @@ class HDFSMetadataLog[T <: AnyRef : ClassTag](
   }
 
   /** Return the latest batch id without reading the file. */
-  def getLatestBatchId(): Option[Long] = listBatches.sorted.lastOption
+  def getLatestBatchId(): Option[Long] = listBatches.maxOption
 
   override def getLatest(): Option[(Long, T)] = {
-    listBatches.sorted.lastOption.map { batchId =>
+    listBatches.maxOption.map { batchId =>
       logInfo(log"Getting latest batch ${MDC(BATCH_ID, batchId)}")
       (batchId, getExistingBatch(batchId))
     }


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

Reply via email to