nsivabalan commented on code in PR #18265:
URL: https://github.com/apache/hudi/pull/18265#discussion_r2880318654


##########
hudi-spark-datasource/hudi-spark-common/src/test/scala/org/apache/hudi/TestDataSourceOptions.scala:
##########
@@ -96,3 +96,4 @@ class TestDataSourceOptions {
     DFSPropertiesConfiguration.clearGlobalProps()
   }
 }
+

Review Comment:
   why extra line?



##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/metadata/CatalogBackedTableMetadata.scala:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.hudi.metadata
+
+import org.apache.hudi.client.common.HoodieSparkEngineContext
+import org.apache.hudi.common.engine.HoodieEngineContext
+import org.apache.hudi.common.fs.FSUtils
+import org.apache.hudi.common.table.HoodieTableConfig
+import org.apache.hudi.internal.schema.Types
+import org.apache.hudi.storage.{HoodieStorage, StoragePath}
+import org.apache.hudi.util.PartitionPathFilterUtil
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.catalog.CatalogTablePartition
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.internal.SQLConf
+
+import java.util
+
+import scala.collection.JavaConverters._
+
+class CatalogBackedTableMetadata(engineContext: HoodieEngineContext,
+                                 tableConfig: HoodieTableConfig,
+                                 storage: HoodieStorage,
+                                 datasetBasePath: String) extends
+  FileSystemBackedTableMetadata(engineContext, tableConfig, storage, 
datasetBasePath) with Logging {
+
+  lazy val sparkSession = 
engineContext.asInstanceOf[HoodieSparkEngineContext].getSqlContext.sparkSession
+
+  override def getAllPartitionPaths():
+  util.List[String] = {
+    val catalogTablePartitionSeq =
+      sparkSession.sessionState.catalog.externalCatalog
+        .listPartitions(getDatabaseName, getTableName)
+    catalogTablePartitionSeq
+      .map(catalogTablePartition => {
+        val partitionPathURI = new StoragePath(catalogTablePartition.location)
+        FSUtils.getRelativePartitionPath(dataBasePath, partitionPathURI)
+      }).asJava
+  }
+
+  override def getPartitionPathWithPathPrefixes(relativePathPrefixes: 
util.List[String]):
+  util.List[String] = {
+    val catalogTablePartitionSeq =
+      sparkSession.sessionState.catalog.externalCatalog
+        .listPartitions(getDatabaseName, getTableName)
+    filterPartitionsBasedOnRelativePathPrefixs(relativePathPrefixes, 
catalogTablePartitionSeq)
+  }
+
+  override def 
getPartitionPathWithPathPrefixUsingFilterExpression(relativePathPrefix: 
util.List[String],
+                                                                   
partitionFields: Types.RecordType,
+                                                                   pushedExpr: 
org.apache.hudi.expression.Expression,

Review Comment:
   `pushedExpr` is never used only in the impl (L71 to 77). 
   If its not supported, should we throw Unsupported Exception



##########
hudi-common/src/main/java/org/apache/hudi/BaseHoodieTableFileIndex.java:
##########
@@ -362,7 +361,8 @@ private Map<PartitionPath, List<FileSlice>> 
filterFiles(List<PartitionPath> part
 
   protected List<PartitionPath> listPartitionPaths(List<String> 
relativePartitionPaths,
                                                    Types.RecordType 
partitionFields,
-                                                   Expression 
partitionColumnPredicates) {
+                                                   Expression 
partitionColumnPredicates,
+                                                   List<Object> 
partitionPredicateExpressions) {

Review Comment:
   is the last arg required? partitionPredicateExpressions



##########
hudi-common/src/main/java/org/apache/hudi/util/PartitionPathFilterUtil.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.hudi.util;
+
+import org.apache.hudi.common.util.StringUtils;
+
+import java.util.List;
+import java.util.function.Predicate;
+
+public class PartitionPathFilterUtil {
+
+  public static Predicate<String> relativePathPrefixPredicate(List<String> 
relativePathPrefixes) {

Review Comment:
   Do we have UTs for this?



##########
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/common/TestCatalogBackedTableMetadata.scala:
##########
@@ -0,0 +1,510 @@
+/*
+ * 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.common
+
+import org.apache.hudi.DataSourceReadOptions
+import org.apache.hudi.client.common.HoodieSparkEngineContext
+import org.apache.hudi.common.config.HoodieMetadataConfig
+import org.apache.hudi.common.fs.FSUtils
+import org.apache.hudi.common.table.HoodieTableMetaClient
+import org.apache.hudi.hadoop.fs.HadoopFSUtils
+import org.apache.hudi.metadata.{CatalogBackedTableMetadata, 
HoodieBackedTableMetadata}
+import org.apache.hudi.storage.StoragePath
+
+import org.junit.jupiter.api.Assertions.{assertEquals, assertFalse, assertTrue}
+
+/**
+ * Tests for CatalogBackedTableMetadata to verify partition listing via catalog
+ * when metadata table is disabled or corrupted.
+ */
+class TestCatalogBackedTableMetadata extends HoodieSparkSqlTestBase {
+
+  test("Test catalog-backed partition listing with metadata table disabled") {
+    withTempDir { tmp =>
+      val targetTable = generateTableName
+      val tablePath = s"${tmp.getCanonicalPath}/$targetTable"
+
+      // Enable catalog-backed partition listing
+      
spark.conf.set(DataSourceReadOptions.FILE_INDEX_PARTITION_LISTING_VIA_CATALOG.key,
 "true")
+
+      spark.sql(
+        s"""
+           |create table $targetTable (
+           |  `id` string,
+           |  `name` string,
+           |  `ts` bigint,
+           |  `year` string,
+           |  `month` string,
+           |  `day` string
+           |) using hudi
+           | tblproperties (
+           |  'primaryKey' = 'id',
+           |  'type' = 'COW',
+           |  'preCombineField'='ts'
+           | )
+           | partitioned by (`year`, `month`, `day`)
+           | location '$tablePath'
+        """.stripMargin)
+
+      // Insert data across multiple partitions using SQL to ensure catalog is 
updated
+      spark.sql(
+        s"""
+           |INSERT INTO $targetTable VALUES
+           |  ('1', 'a1', 1000, '2024', '01', '01'),
+           |  ('2', 'a2', 2000, '2024', '01', '02'),
+           |  ('3', 'a3', 3000, '2024', '01', '03'),
+           |  ('4', 'a4', 4000, '2024', '02', '01'),
+           |  ('5', 'a5', 5000, '2024', '02', '02'),
+           |  ('6', 'a6', 6000, '2024', '03', '01')
+        """.stripMargin)
+
+      // Verify metadata table is disabled
+      val metaClient = HoodieTableMetaClient.builder()
+        
.setConf(HadoopFSUtils.getStorageConfWithCopy(spark.sparkContext.hadoopConfiguration))
+        .setBasePath(tablePath)
+        .build()
+
+      // Verify catalog-backed metadata can list partitions
+      val engine = new HoodieSparkEngineContext(spark.sparkContext)
+      val storage = metaClient.getStorage
+      val catalogBackedMetadata = new CatalogBackedTableMetadata(
+        engine, metaClient.getTableConfig, storage, tablePath)
+
+      val allPartitions = catalogBackedMetadata.getAllPartitionPaths
+      assertEquals(6, allPartitions.size(), "Should have 6 partitions")
+      assertTrue(allPartitions.contains("2024/01/01"))
+      assertTrue(allPartitions.contains("2024/01/02"))
+      assertTrue(allPartitions.contains("2024/01/03"))
+      assertTrue(allPartitions.contains("2024/02/01"))
+      assertTrue(allPartitions.contains("2024/02/02"))
+      assertTrue(allPartitions.contains("2024/03/01"))
+
+      // Test partition pruning via catalog
+      checkAnswer(s"select id, name from $targetTable where year = '2024' and 
month = '01' order by id")(
+        Seq("1", "a1"),
+        Seq("2", "a2"),
+        Seq("3", "a3")
+      )
+
+      checkAnswer(s"select id, name from $targetTable where year = '2024' and 
month = '02' order by id")(
+        Seq("4", "a4"),
+        Seq("5", "a5")
+      )
+
+      checkAnswer(s"select id, name from $targetTable where day = '01' order 
by id")(

Review Comment:
   we should corrupt data in non matching partitions to ensure the partition 
pruning is effective.



##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/metadata/CatalogBackedTableMetadata.scala:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.hudi.metadata
+
+import org.apache.hudi.client.common.HoodieSparkEngineContext
+import org.apache.hudi.common.engine.HoodieEngineContext
+import org.apache.hudi.common.fs.FSUtils
+import org.apache.hudi.common.table.HoodieTableConfig
+import org.apache.hudi.internal.schema.Types
+import org.apache.hudi.storage.{HoodieStorage, StoragePath}
+import org.apache.hudi.util.PartitionPathFilterUtil
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.catalog.CatalogTablePartition
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.internal.SQLConf
+
+import java.util
+
+import scala.collection.JavaConverters._
+
+class CatalogBackedTableMetadata(engineContext: HoodieEngineContext,
+                                 tableConfig: HoodieTableConfig,
+                                 storage: HoodieStorage,
+                                 datasetBasePath: String) extends
+  FileSystemBackedTableMetadata(engineContext, tableConfig, storage, 
datasetBasePath) with Logging {
+
+  lazy val sparkSession = 
engineContext.asInstanceOf[HoodieSparkEngineContext].getSqlContext.sparkSession
+
+  override def getAllPartitionPaths():
+  util.List[String] = {
+    val catalogTablePartitionSeq =
+      sparkSession.sessionState.catalog.externalCatalog
+        .listPartitions(getDatabaseName, getTableName)
+    catalogTablePartitionSeq
+      .map(catalogTablePartition => {
+        val partitionPathURI = new StoragePath(catalogTablePartition.location)
+        FSUtils.getRelativePartitionPath(dataBasePath, partitionPathURI)
+      }).asJava
+  }
+
+  override def getPartitionPathWithPathPrefixes(relativePathPrefixes: 
util.List[String]):
+  util.List[String] = {
+    val catalogTablePartitionSeq =
+      sparkSession.sessionState.catalog.externalCatalog
+        .listPartitions(getDatabaseName, getTableName)
+    filterPartitionsBasedOnRelativePathPrefixs(relativePathPrefixes, 
catalogTablePartitionSeq)
+  }
+
+  override def 
getPartitionPathWithPathPrefixUsingFilterExpression(relativePathPrefix: 
util.List[String],
+                                                                   
partitionFields: Types.RecordType,
+                                                                   pushedExpr: 
org.apache.hudi.expression.Expression,
+                                                                   
partitionPredicateExpressions: util.List[Object]):
+  util.List[String] = {
+    val partitionPredicateExpressionSeq = 
partitionPredicateExpressions.asScala.map(_.asInstanceOf[Expression])
+    val catalogTablePartitionSeq =
+      sparkSession.sessionState.catalog.externalCatalog
+        .listPartitionsByFilter(getDatabaseName, getTableName, 
partitionPredicateExpressionSeq,
+          SQLConf.get.sessionLocalTimeZone)
+    filterPartitionsBasedOnRelativePathPrefixs(relativePathPrefix, 
catalogTablePartitionSeq)
+  }
+
+  private def filterPartitionsBasedOnRelativePathPrefixs(relativePathPrefix: 
util.List[String],
+                                                         
catalogTablePartitionSeq: Seq[CatalogTablePartition]) = {
+    // Convert CatalogTablePartition object to String object containing 
relativePartitionPath.
+    // and use relativePathPrefixesPredicate to filter the partition paths 
further
+    val relativePathPrefixPredicate = 
PartitionPathFilterUtil.relativePathPrefixPredicate(relativePathPrefix)
+    val relativePartitionPathsList = catalogTablePartitionSeq

Review Comment:
   we could return directly from here right?



##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/SparkHoodieTableFileIndex.scala:
##########
@@ -122,6 +123,12 @@ class SparkHoodieTableFileIndex(spark: SparkSession,
 
   protected lazy val shouldFastBootstrap = 
configProperties.getBoolean(DATA_QUERIES_ONLY.key, false)
 
+  lazy val isPartitionListingViaCatalogEnabled: Boolean = {
+    configProperties.getBoolean(FILE_INDEX_PARTITION_LISTING_VIA_CATALOG.key,
+      FILE_INDEX_PARTITION_LISTING_VIA_CATALOG.defaultValue()) &&
+      !metaClient.getTableConfig.isMetadataTableAvailable

Review Comment:
   do we need the 2nd condition?
   for the motivation you are adding this feature, if the mdt is corrupted, we 
might want to go w/ catalog based listing. 
   but then, here, we have to first disable metadata table before we can 
attempt listing via catalog backed table metadata.  



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