yihua commented on code in PR #11951:
URL: https://github.com/apache/hudi/pull/11951#discussion_r1769468772


##########
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/functional/TestSecondaryIndexPruning.scala:
##########
@@ -158,6 +158,70 @@ class TestSecondaryIndexPruning extends 
SparkClientFunctionalTestHarness {
     }
   }
 
+  @ParameterizedTest
+  @MethodSource(Array("testSecondaryIndexPruningParameters"))

Review Comment:
   Since dropping index isn't coupled with table type and partitioning, testing 
one combination is good enough.



##########
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/command/index/TestFunctionalIndex.scala:
##########
@@ -248,6 +249,86 @@ class TestFunctionalIndex extends HoodieSparkSqlTestBase {
     }
   }
 
+  test("Test Drop Functional Index") {
+    if (HoodieSparkUtils.gteqSpark3_3) {
+      withTempDir { tmp =>
+        Seq("cow", "mor").foreach { tableType =>

Review Comment:
   Similar here on test combinations



##########
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/command/index/TestFunctionalIndex.scala:
##########
@@ -248,6 +249,86 @@ class TestFunctionalIndex extends HoodieSparkSqlTestBase {
     }
   }
 
+  test("Test Drop Functional Index") {
+    if (HoodieSparkUtils.gteqSpark3_3) {
+      withTempDir { tmp =>
+        Seq("cow", "mor").foreach { tableType =>
+          val databaseName = "default"
+          val tableName = generateTableName
+          val basePath = s"${tmp.getCanonicalPath}/$tableName"
+          spark.sql(
+            s"""
+               |create table $tableName (
+               |  id int,
+               |  name string,
+               |  price double,
+               |  ts long
+               |) using hudi
+               | options (
+               |  primaryKey ='id',
+               |  type = '$tableType',
+               |  preCombineField = 'ts',
+               |  hoodie.metadata.record.index.enable = 'true',
+               |  hoodie.datasource.write.recordkey.field = 'id'
+               | )
+               | partitioned by(ts)
+               | location '$basePath'
+       """.stripMargin)
+          spark.sql(s"insert into $tableName values(1, 'a1', 10, 1000)")
+          spark.sql(s"insert into $tableName values(2, 'a2', 10, 1001)")
+          spark.sql(s"insert into $tableName values(3, 'a3', 10, 1002)")
+
+          var metaClient = createMetaClient(spark, basePath)
+
+          
assert(metaClient.getTableConfig.isMetadataPartitionAvailable(MetadataPartitionType.RECORD_INDEX))
+
+          val sqlParser: ParserInterface = spark.sessionState.sqlParser
+          val analyzer: Analyzer = spark.sessionState.analyzer
+
+          var logicalPlan = sqlParser.parsePlan(s"show indexes from 
default.$tableName")
+          var resolvedLogicalPlan = analyzer.execute(logicalPlan)
+          
assertTableIdentifier(resolvedLogicalPlan.asInstanceOf[ShowIndexesCommand].table,
 databaseName, tableName)
+
+          var createIndexSql = s"create index idx_datestr on $tableName using 
column_stats(ts) options(func='from_unixtime', format='yyyy-MM-dd')"
+          logicalPlan = sqlParser.parsePlan(createIndexSql)
+
+          resolvedLogicalPlan = analyzer.execute(logicalPlan)
+          
assertTableIdentifier(resolvedLogicalPlan.asInstanceOf[CreateIndexCommand].table,
 databaseName, tableName)
+          
assertResult("idx_datestr")(resolvedLogicalPlan.asInstanceOf[CreateIndexCommand].indexName)
+          
assertResult("column_stats")(resolvedLogicalPlan.asInstanceOf[CreateIndexCommand].indexType)
+          
assertResult(false)(resolvedLogicalPlan.asInstanceOf[CreateIndexCommand].ignoreIfExists)

Review Comment:
   Can some of the validation logic be simplified and removed since this test 
only targets "DROP INDEX"?



##########
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/IndexCommands.scala:
##########
@@ -76,7 +76,14 @@ case class DropIndexCommand(table: CatalogTable,
   override def run(sparkSession: SparkSession): Seq[Row] = {
     val tableId = table.identifier
     val metaClient = createHoodieTableMetaClient(tableId, sparkSession)
-    SecondaryIndexManager.getInstance().drop(metaClient, indexName, 
ignoreIfNotExists)
+    try {
+      // need to ensure that the index name is for a valid partition type
+      MetadataPartitionType.fromPartitionPath(indexName)
+      HoodieSparkIndexClient.getInstance(sparkSession).drop(metaClient, 
indexName, ignoreIfNotExists)
+    } catch {
+      case _: IllegalArgumentException =>
+        SecondaryIndexManager.getInstance().drop(metaClient, indexName, 
ignoreIfNotExists)

Review Comment:
   Why drop here again?



##########
hudi-spark-datasource/hudi-spark-common/src/main/java/org/apache/hudi/HoodieSparkIndexClient.java:
##########
@@ -112,6 +112,24 @@ public void create(HoodieTableMetaClient metaClient, 
String indexName, String in
     }
   }
 
+  @Override
+  public void drop(HoodieTableMetaClient metaClient, String indexName, boolean 
ignoreIfNotExists) {
+    if (!indexExists(metaClient, indexName)) {
+      if (ignoreIfNotExists) {
+        return;
+      } else {
+        throw new HoodieFunctionalIndexException("Index does not exist: " + 
indexName);
+      }
+    }
+
+    LOG.info("Dropping index {}", indexName);
+    HoodieIndexDefinition indexDefinition = 
metaClient.getIndexMetadata().get().getIndexDefinitions().get(indexName);
+    try (SparkRDDWriteClient writeClient = 
HoodieCLIUtils.createHoodieWriteClient(
+        sparkSession, metaClient.getBasePath().toString(), 
mapAsScalaImmutableMap(buildWriteConfig(metaClient, indexDefinition)), 
toScalaOption(Option.empty()))) {
+      writeClient.dropIndex(Collections.singletonList(indexName));

Review Comment:
   For future reference, this logic does not have much specifics to engine 
itself, so it can be abstracted to the index client by plugging in the 
engine-specific write client.



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