danny0405 commented on code in PR #10191:
URL: https://github.com/apache/hudi/pull/10191#discussion_r1606136024


##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/BucketIndexSupport.scala:
##########
@@ -0,0 +1,219 @@
+/*
+ * 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
+
+import org.apache.avro.generic.GenericData
+import org.apache.hudi.common.config.HoodieMetadataConfig
+import org.apache.hudi.common.fs.FSUtils
+import org.apache.hudi.common.model.FileSlice
+import org.apache.hudi.common.table.{HoodieTableConfig, HoodieTableMetaClient, 
TableSchemaResolver}
+import org.apache.hudi.config.HoodieIndexConfig
+import org.apache.hudi.index.HoodieIndex
+import org.apache.hudi.index.HoodieIndex.IndexType
+import org.apache.hudi.index.bucket.BucketIdentifier
+import org.apache.hudi.keygen.KeyGenerator
+import org.apache.hudi.keygen.factory.HoodieSparkKeyGeneratorFactory
+import org.apache.spark.sql.catalyst.expressions
+import org.apache.spark.sql.catalyst.expressions.{And, Attribute, EmptyRow, 
Expression, Literal}
+import org.apache.hudi.common.util.collection.Pair
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.types.{DoubleType, FloatType, StructType}
+import org.apache.spark.util.collection.BitSet
+import org.slf4j.LoggerFactory
+
+import scala.collection.{JavaConverters, mutable}
+
+class BucketIndexSupport(spark: SparkSession,
+                         metadataConfig: HoodieMetadataConfig,
+                         metaClient: HoodieTableMetaClient)
+  extends SparkBaseIndexSupport (spark, metadataConfig, metaClient){
+
+  private val log = LoggerFactory.getLogger(getClass)
+
+  private val keyGenerator =
+    HoodieSparkKeyGeneratorFactory.createKeyGenerator(metadataConfig.getProps)
+
+  private lazy val avroSchema = new 
TableSchemaResolver(metaClient).getTableAvroSchema(false)
+
+  override def getIndexName: String = "BUCKET"
+
+  /**
+   * Return true if table can use bucket index
+   * - has bucket hash field
+   * - table is bucket index writer
+   * - only support simple bucket engine
+   */
+  def isIndexAvailable: Boolean = {
+    indexBucketHashFieldsOpt.isDefined &&
+      metadataConfig.getStringOrDefault(HoodieIndexConfig.INDEX_TYPE, 
"").equalsIgnoreCase(IndexType.BUCKET.name()) &&
+      
metadataConfig.getStringOrDefault(HoodieIndexConfig.BUCKET_INDEX_ENGINE_TYPE).equalsIgnoreCase(HoodieIndex.BucketIndexEngineType.SIMPLE.name())
 &&
+      metadataConfig.getBooleanOrDefault(HoodieIndexConfig.BUCKET_QUERY_INDEX)
+  }
+
+  override def invalidateCaches(): Unit = {
+    // no caches for this index type, do nothing
+  }
+
+  override def computeCandidateFileNames(fileIndex: HoodieFileIndex,
+                                         queryFilters: Seq[Expression],
+                                         queryReferencedColumns: Seq[String],
+                                         prunedPartitionsAndFileSlices: 
Seq[(Option[BaseHoodieTableFileIndex.PartitionPath], Seq[FileSlice])],
+                                         shouldPushDownFilesFilter: Boolean): 
Option[Set[String]] = {
+
+    val bucketIdsBitMapByFilter = 
filterQueriesWithBucketHashField(queryFilters)
+
+    if (bucketIdsBitMapByFilter.isDefined && 
bucketIdsBitMapByFilter.get.cardinality() > 0) {
+      val allFilesName = getPrunedFileNames(prunedPartitionsAndFileSlices)
+      Option.apply(getCandidateFiles(allFilesName, 
bucketIdsBitMapByFilter.get))
+    } else {
+      Option.empty
+    }
+  }
+
+  def getCandidateFiles(allFilesName: Set[String], bucketIds: BitSet): 
Set[String] = {
+    val candidateFiles: mutable.Set[String] = mutable.Set.empty
+    for (fileName <- allFilesName) {
+      val fileId = FSUtils.getFileIdFromFileName(fileName)
+      val fileBucketId = BucketIdentifier.bucketIdFromFileId(fileId)
+      if (bucketIds.get(fileBucketId)) {
+        candidateFiles += fileName
+      }
+    }
+    candidateFiles.toSet
+  }
+
+  def filterQueriesWithBucketHashField(queryFilters: Seq[Expression]): 
Option[BitSet] = {
+    val bucketNumber = 
metadataConfig.getIntOrDefault(HoodieIndexConfig.BUCKET_INDEX_NUM_BUCKETS)
+    if (indexBucketHashFieldsOpt.isEmpty || queryFilters.isEmpty) {
+      None
+    } else {
+      var matchedBuckets: BitSet = null
+      if (indexBucketHashFieldsOpt.get.size == 1) {
+        matchedBuckets = 
getBucketsBySingleHashFields(queryFilters.reduce(And), 
indexBucketHashFieldsOpt.get.get(0), bucketNumber)
+      } else {
+        matchedBuckets = getBucketsByMultipleHashFields(queryFilters,
+          
JavaConverters.asScalaBufferConverter(indexBucketHashFieldsOpt.get).asScala.toSet,
 bucketNumber)
+      }
+
+      val numBucketsSelected = matchedBuckets.cardinality()
+
+      // None means all the buckets need to be scanned
+      if (numBucketsSelected == bucketNumber) {
+        log.info("the query predicates does not specify equality for all the 
hashing fields, fallback other index")
+        None

Review Comment:
   `The query predicates do not include equality expressions for all the 
hashing fields, fall back to the other indices`



-- 
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: commits-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to