PavithranRick commented on code in PR #17460:
URL: https://github.com/apache/hudi/pull/17460#discussion_r2761803113


##########
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/ShowSavepointsProcedure.scala:
##########
@@ -45,17 +120,126 @@ class ShowSavepointsProcedure extends BaseProcedure with 
ProcedureBuilder {
 
     val tableName = getArgValueOrDefault(args, PARAMETERS(0))
     val tablePath = getArgValueOrDefault(args, PARAMETERS(1))
+    val limit = getArgValueOrDefault(args, PARAMETERS(2)).get.asInstanceOf[Int]
+    val showArchived = getArgValueOrDefault(args, 
PARAMETERS(3)).get.asInstanceOf[Boolean]
+    val filter = getArgValueOrDefault(args, 
PARAMETERS(4)).get.asInstanceOf[String]
+    val startTime = getArgValueOrDefault(args, 
PARAMETERS(5)).get.asInstanceOf[String]
+    val endTime = getArgValueOrDefault(args, 
PARAMETERS(6)).get.asInstanceOf[String]
+
+    validateFilter(filter, outputType)
 
     val basePath: String = getBasePath(tableName, tablePath)
     val metaClient = createMetaClient(jsc, basePath)
 
-    val activeTimeline: HoodieActiveTimeline = metaClient.getActiveTimeline
-    val timeline: HoodieTimeline = 
activeTimeline.getSavePointTimeline.filterCompletedInstants
-    val commits: util.List[HoodieInstant] = 
timeline.getReverseOrderedInstants.collect(Collectors.toList[HoodieInstant])
+    val activeResults = 
getCombinedSavepointsWithPartitionMetadata(metaClient.getActiveTimeline, limit, 
startTime, endTime, "ACTIVE")
+    val finalResults = if (showArchived) {
+      val archivedResults = 
getCombinedSavepointsWithPartitionMetadata(metaClient.getArchivedTimeline, 
limit, startTime, endTime, "ARCHIVED")
+      val combinedResults = (activeResults ++ archivedResults)
+        .sortWith((a, b) => a.getString(0) > b.getString(0))
+      if (startTime.trim.nonEmpty && endTime.trim.nonEmpty) {
+        combinedResults
+      } else {
+        combinedResults.take(limit)
+      }
+    } else {
+      if (startTime.trim.nonEmpty && endTime.trim.nonEmpty) {
+        activeResults
+      } else {
+        activeResults.take(limit)
+      }
+    }
+    applyFilter(finalResults, filter, outputType)
+  }
 
-    if (commits.isEmpty) Seq.empty[Row] else {
-      commits.toArray.map(instant => 
instant.asInstanceOf[HoodieInstant].requestedTime).map(p => Row(p)).toSeq
+  private def getCombinedSavepointsWithPartitionMetadata(timeline: 
HoodieTimeline,
+                                                         limit: Int,
+                                                         startTime: String,
+                                                         endTime: String,
+                                                         timelineType: 
String): Seq[Row] = {
+    import scala.collection.mutable.ListBuffer
+    import scala.util.{Failure, Success, Try}
+
+    val allRows = ListBuffer[Row]()
+
+    val filteredSavepointInstants = 
timeline.getSavePointTimeline.getInstants.iterator().asScala
+      .filter(_.getAction == HoodieTimeline.SAVEPOINT_ACTION)
+      .filter { instant =>
+        val instantTime = instant.requestedTime()
+        val withinStartTime = if (startTime.nonEmpty) instantTime >= startTime 
else true
+        val withinEndTime = if (endTime.nonEmpty) instantTime <= endTime else 
true
+        withinStartTime && withinEndTime
+      }
+      .toSeq
+
+    val allSavepointInstants = if (startTime.nonEmpty && endTime.nonEmpty) {
+      filteredSavepointInstants.sortBy(_.requestedTime).reverse
+    } else {
+      filteredSavepointInstants.sortBy(_.requestedTime).reverse.take(limit)
+    }
+    // there is no pending states in the savepoint action, so we are only 
processing completed ones
+    allSavepointInstants.foreach { savepointInstant =>
+      if (savepointInstant.getState == HoodieInstant.State.COMPLETED) {
+        Try {
+          val savepointMetadata = 
timeline.readSavepointMetadata(savepointInstant)
+          if (savepointMetadata.getPartitionMetadata != null && 
!savepointMetadata.getPartitionMetadata.isEmpty) {
+            savepointMetadata.getPartitionMetadata.entrySet.asScala.foreach { 
partitionEntry =>
+              val partitionPath = partitionEntry.getKey
+              val partitionMetadata = partitionEntry.getValue
+              partitionMetadata.getSavepointDataFile.asScala.foreach { 
dataFile =>
+                val row = Row(
+                  savepointInstant.requestedTime,
+                  savepointInstant.getCompletionTime,
+                  savepointInstant.getState.name,
+                  savepointInstant.getAction,
+                  timelineType,
+                  savepointMetadata.getSavepointedBy,
+                  savepointMetadata.getSavepointedAt,
+                  savepointMetadata.getComments,
+                  partitionPath,
+                  dataFile,
+                  savepointMetadata.getVersion
+                )
+                allRows += row
+              }
+            }
+          } else {
+            val row = Row(
+              savepointInstant.requestedTime,
+              savepointInstant.getCompletionTime,
+              savepointInstant.getState.name,
+              savepointInstant.getAction,
+              timelineType,
+              savepointMetadata.getSavepointedBy,
+              savepointMetadata.getSavepointedAt,
+              savepointMetadata.getComments,
+              null,
+              null,
+              savepointMetadata.getVersion
+            )
+            allRows += row
+          }
+        } match {
+          case Success(_) => // Success, rows already added
+          case Failure(e) =>
+            log.warn(s"Failed to read savepoint metadata for instant 
${savepointInstant.requestedTime()}", e)

Review Comment:
   yes



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

Reply via email to