vamshikrishnakyatham commented on code in PR #13719: URL: https://github.com/apache/hudi/pull/13719#discussion_r2283374001
########## hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/ShowCleansProcedure.scala: ########## @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.hudi.command.procedures + +import org.apache.hudi.{HoodieCLIUtils, SparkAdapterSupport} +import org.apache.hudi.common.table.timeline.{HoodieInstant, HoodieTimeline, TimelineLayout} + +import org.apache.spark.internal.Logging +import org.apache.spark.sql.Row +import org.apache.spark.sql.types.{DataTypes, Metadata, StructField, StructType} + +import java.util +import java.util.Collections +import java.util.function.Supplier + +import scala.collection.JavaConverters._ + +class ShowCleansProcedure(includePartitionMetadata: Boolean) extends BaseProcedure with ProcedureBuilder with SparkAdapterSupport with Logging { + + private val PARAMETERS = Array[ProcedureParameter]( + ProcedureParameter.required(0, "table", DataTypes.StringType), + ProcedureParameter.optional(1, "limit", DataTypes.IntegerType, 10) + ) + + private val OUTPUT_TYPE = new StructType(Array[StructField]( + StructField("clean_time", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("state_transition_time", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("action", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("start_clean_time", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("time_taken_in_millis", DataTypes.LongType, nullable = true, Metadata.empty), + StructField("total_files_deleted", DataTypes.IntegerType, nullable = true, Metadata.empty), + StructField("earliest_commit_to_retain", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("last_completed_commit_timestamp", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("version", DataTypes.IntegerType, nullable = true, Metadata.empty) + )) + + private val PARTITION_METADATA_OUTPUT_TYPE = new StructType(Array[StructField]( + StructField("clean_time", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("state_transition_time", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("action", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("start_clean_time", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("partition_path", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("policy", DataTypes.StringType, nullable = true, Metadata.empty), + StructField("delete_path_patterns", DataTypes.IntegerType, nullable = true, Metadata.empty), + StructField("success_delete_files", DataTypes.IntegerType, nullable = true, Metadata.empty), + StructField("failed_delete_files", DataTypes.IntegerType, nullable = true, Metadata.empty), + StructField("is_partition_deleted", DataTypes.BooleanType, nullable = true, Metadata.empty), + StructField("time_taken_in_millis", DataTypes.LongType, nullable = true, Metadata.empty), + StructField("total_files_deleted", DataTypes.IntegerType, nullable = true, Metadata.empty) + )) + + def parameters: Array[ProcedureParameter] = PARAMETERS + + def outputType: StructType = if (includePartitionMetadata) PARTITION_METADATA_OUTPUT_TYPE else OUTPUT_TYPE + + override def call(args: ProcedureArgs): Seq[Row] = { + super.checkArgs(PARAMETERS, args) + + val table = getArgValueOrDefault(args, PARAMETERS(0)).get.asInstanceOf[String] + val limit = getArgValueOrDefault(args, PARAMETERS(1)).get.asInstanceOf[Int] + + val hoodieCatalogTable = HoodieCLIUtils.getHoodieCatalogTable(sparkSession, table) + val basePath = hoodieCatalogTable.tableLocation + val metaClient = createMetaClient(jsc, basePath) + + val activeTimeline = metaClient.getActiveTimeline + if (includePartitionMetadata) { + getCleansWithPartitionMetadata(activeTimeline, limit) + } else { + getCleans(activeTimeline, limit) + } + } + + override def build: Procedure = new ShowCleansProcedure(includePartitionMetadata) + + private def getCleansWithPartitionMetadata(timeline: HoodieTimeline, + limit: Int): Seq[Row] = { + import scala.collection.JavaConverters._ + + val (rows: util.ArrayList[Row], cleanInstants: util.ArrayList[HoodieInstant]) = getSortedCleans(timeline) + + for (i <- 0 until cleanInstants.size) { + val cleanInstant = cleanInstants.get(i) + val cleanMetadata = timeline.readCleanMetadata(cleanInstant) + + for (partitionMetadataEntry <- cleanMetadata.getPartitionMetadata.entrySet.asScala) { + val partitionPath = partitionMetadataEntry.getKey + val partitionMetadata = partitionMetadataEntry.getValue + + rows.add(Row( + cleanInstant.requestedTime(), + cleanInstant.getCompletionTime, + cleanInstant.getAction, + cleanMetadata.getStartCleanTime, + partitionPath, + partitionMetadata.getPolicy, + partitionMetadata.getDeletePathPatterns.size(), + partitionMetadata.getSuccessDeleteFiles.size(), + partitionMetadata.getFailedDeleteFiles.size(), + partitionMetadata.getIsPartitionDeleted, + cleanMetadata.getTimeTakenInMillis, + cleanMetadata.getTotalFilesDeleted + )) + } + } + + rows.stream().limit(limit).toArray().map(r => r.asInstanceOf[Row]).toList + } + + private def getSortedCleans(timeline: HoodieTimeline): (util.ArrayList[Row], util.ArrayList[HoodieInstant]) = { + val rows = new util.ArrayList[Row] + val cleanInstants: util.List[HoodieInstant] = timeline.getCleanerTimeline.filterCompletedInstants + .getInstants.toArray().map(instant => instant.asInstanceOf[HoodieInstant]).toList.asJava + val sortedCleanInstants = new util.ArrayList[HoodieInstant](cleanInstants) + val layout = TimelineLayout.fromVersion(timeline.getTimelineLayoutVersion) + Collections.sort(sortedCleanInstants, layout.getInstantComparator.requestedTimeOrderedComparator.reversed) + (rows, sortedCleanInstants) + } + + private def getCleans(timeline: HoodieTimeline, + limit: Int): Seq[Row] = { + val (rows: util.ArrayList[Row], cleanInstants: util.ArrayList[HoodieInstant]) = getSortedCleans(timeline) + + for (i <- 0 until cleanInstants.size) { + val cleanInstant = cleanInstants.get(i) + val cleanMetadata = timeline.readCleanMetadata(cleanInstant) + + rows.add(Row( + cleanInstant.requestedTime(), + cleanInstant.getCompletionTime, + cleanInstant.getAction, + cleanMetadata.getStartCleanTime, + cleanMetadata.getTimeTakenInMillis, + cleanMetadata.getTotalFilesDeleted, + cleanMetadata.getEarliestCommitToRetain, + cleanMetadata.getLastCompletedCommitTimestamp, + cleanMetadata.getVersion + )) + } + + rows.stream().limit(limit).toArray().map(r => r.asInstanceOf[Row]).toList + } +} + +object ShowCleansProcedure { + val NAME = "show_cleans" + + def builder: Supplier[ProcedureBuilder] = () => new ShowCleansProcedure(false) +} + +object ShowCleansPartitionMetadataProcedure { + val NAME = "show_cleans_metadata" Review Comment: its a pattern followed in the codebase -- 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]
