rahil-c commented on code in PR #13719: URL: https://github.com/apache/hudi/pull/13719#discussion_r2286394921
########## hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/ShowCleansPlanProcedure.scala: ########## @@ -0,0 +1,213 @@ +/* + * 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.HoodieTableMetaClient +import org.apache.hudi.common.table.timeline.{HoodieInstant, TimelineLayout} +import org.apache.hudi.exception.HoodieException + +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.function.Supplier + +import scala.collection.JavaConverters._ +import scala.util.{Failure, Success, Try} + +class ShowCleansPlanProcedure extends BaseProcedure with ProcedureBuilder with SparkAdapterSupport with Logging { + + import ShowCleansPlanProcedure._ + + override def parameters: Array[ProcedureParameter] = PARAMETERS + + override def outputType: StructType = OUTPUT_TYPE + + override def call(args: ProcedureArgs): Seq[Row] = { + super.checkArgs(PARAMETERS, args) + + val tableName = getArgValueOrDefault(args, PARAMETERS(0)).get.asInstanceOf[String] + val limit = getArgValueOrDefault(args, PARAMETERS(1)).get.asInstanceOf[Int] + val showArchived = getArgValueOrDefault(args, PARAMETERS(2)).get.asInstanceOf[Boolean] + val filter = getArgValueOrDefault(args, PARAMETERS(3)).get.asInstanceOf[String] + + validateInputs(tableName, limit) + + if (filter != null && filter.trim.nonEmpty) { + HoodieProcedureFilterUtils.validateFilterExpression(filter, outputType, sparkSession) match { + case Left(errorMessage) => + throw new IllegalArgumentException(s"Invalid filter expression: $errorMessage") + case Right(_) => // Validation passed, continue + } + } + + val rows = Try { + val hoodieCatalogTable = HoodieCLIUtils.getHoodieCatalogTable(sparkSession, tableName) + val metaClient = createMetaClient(jsc, hoodieCatalogTable.tableLocation) + getCleanerPlans(metaClient, limit, showArchived) + } match { + case Success(result) => result + case Failure(exception) => + logError(s"Failed to retrieve clean plan information for table '$tableName'", exception) + throw new HoodieException(s"Error retrieving clean plans for table '$tableName': ${exception.getMessage}", exception) + } + + if (filter != null && filter.trim.nonEmpty) { + HoodieProcedureFilterUtils.evaluateFilter(rows, filter, outputType, sparkSession) + } else { + rows + } + } + + override def build: Procedure = new ShowCleansPlanProcedure() + + private def validateInputs(tableName: String, limit: Int): Unit = { + require(tableName.nonEmpty, "Table name cannot be empty") + require(limit > 0, s"Limit must be positive, got: $limit") + } + + private def getCleanerPlans(metaClient: HoodieTableMetaClient, limit: Int, showArchived: Boolean): Seq[Row] = { + val timeline = if (showArchived) { + metaClient.getArchivedTimeline.mergeTimeline(metaClient.getActiveTimeline) Review Comment: I guess could you apply your limit to both the active timeline and the archived timeline and then do the merge. And then from that list take the limit for the final result? so if limit is 10, take 10 from active, 10 from archived and then sort and limit back to 10? -- 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]
