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


##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/BucketIndexSupport.scala:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.hadoop.fs.FileStatus
+import org.apache.hudi.common.config.HoodieMetadataConfig
+import org.apache.hudi.common.fs.FSUtils
+import org.apache.hudi.common.table.HoodieTableConfig
+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.log4j.LogManager
+import org.apache.spark.sql.catalyst.expressions
+import org.apache.spark.sql.catalyst.expressions.{And, Attribute, EmptyRow, 
Expression, Literal}
+import org.apache.spark.sql.types.{DoubleType, FloatType}
+import org.apache.spark.util.collection.BitSet
+
+import scala.collection.{JavaConverters, mutable}
+
+class BucketIndexSupport(metadataConfig: HoodieMetadataConfig) {
+
+  private val log = LogManager.getLogger(getClass);
+
+  /**
+   * Returns the configured bucket field for the table
+   */
+  private def getBucketHashField: Option[String] = {
+    val bucketHashFields = 
metadataConfig.getString(HoodieIndexConfig.BUCKET_INDEX_HASH_FIELD)
+    if (bucketHashFields == null) {
+      val recordKeys = 
metadataConfig.getString(HoodieTableConfig.RECORDKEY_FIELDS)
+      if (recordKeys == null) {
+        Option.apply(null)
+      } else {
+        val recordKeyArray = recordKeys.split(",")
+        if (recordKeyArray.length == 1) {
+          Option.apply(recordKeyArray(0))
+        } else {
+          log.warn("bucket query index only support one bucket field")
+          Option.apply(null)
+        }
+      }
+    } else {
+      val fields = bucketHashFields.split(",")
+      if (fields.length == 1) {
+        Option.apply(fields(0))
+      } else {
+        log.warn("bucket query index only support one bucket field")
+        Option.apply(null)
+      }
+    }
+  }
+
+  def getCandidateFiles(allFiles: Seq[FileStatus], bucketIds: BitSet): 
Set[String] = {
+    val candidateFiles: mutable.Set[String] = mutable.Set.empty
+    for (file <- allFiles) {
+      val fileId = FSUtils.getFileIdFromFilePath(file.getPath)
+      val fileBucketId = BucketIdentifier.bucketIdFromFileId(fileId)
+      if (bucketIds.get(fileBucketId)) {
+        candidateFiles += file.getPath.getName
+      }
+    }
+    candidateFiles.toSet
+  }
+
+  def filterQueriesWithBucketHashField(queryFilters: Seq[Expression]): 
Option[BitSet] = {
+    val bucketNumber = 
metadataConfig.getInt(HoodieIndexConfig.BUCKET_INDEX_NUM_BUCKETS)
+    val bucketHashFieldOpt = getBucketHashField
+    if (bucketHashFieldOpt.isEmpty || queryFilters.isEmpty) {
+      None
+    } else {
+      val matchedBuckets = getExpressionBuckets(queryFilters.reduce(And), 
bucketHashFieldOpt.get, bucketNumber)
+
+      val numBucketsSelected = matchedBuckets.cardinality()
+
+      // None means all the buckets need to be scanned
+      if (numBucketsSelected == bucketNumber) {
+        log.info("bucket query match all file slice, fallback other index")

Review Comment:
   file slice is an internal notion, maybe you should say `The query predicates 
does not specify equality for all the hasing fields, ...`



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