yihua commented on a change in pull request #4520:
URL: https://github.com/apache/hudi/pull/4520#discussion_r784477170



##########
File path: 
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/hudi/AbstractHoodieTableFileIndex.scala
##########
@@ -0,0 +1,309 @@
+/*
+ * 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, Path}
+import org.apache.hudi.DataSourceReadOptions.{QUERY_TYPE, 
QUERY_TYPE_SNAPSHOT_OPT_VAL}
+import org.apache.hudi.common.config.{HoodieMetadataConfig, TypedProperties}
+import org.apache.hudi.common.engine.HoodieEngineContext
+import org.apache.hudi.common.fs.FSUtils
+import org.apache.hudi.common.model.FileSlice
+import org.apache.hudi.common.model.HoodieTableType.MERGE_ON_READ
+import org.apache.hudi.common.table.HoodieTableMetaClient
+import org.apache.hudi.common.table.view.{FileSystemViewStorageConfig, 
HoodieTableFileSystemView}
+
+import scala.collection.JavaConversions._
+import scala.collection.JavaConverters._
+import scala.collection.mutable
+
+/**
+ * A file index which support partition prune for hoodie snapshot and 
read-optimized query.
+ *
+ * Main steps to get the file list for query:
+ * 1、Load all files and partition values from the table path.
+ * 2、Do the partition prune by the partition filter condition.
+ *
+ * There are 3 cases for this:
+ * 1、If the partition columns size is equal to the actually partition path 
level, we
+ * read it as partitioned table.(e.g partition column is "dt", the partition 
path is "2021-03-10")
+ *
+ * 2、If the partition columns size is not equal to the partition path level, 
but the partition
+ * column size is "1" (e.g. partition column is "dt", but the partition path 
is "2021/03/10"
+ * who's directory level is 3).We can still read it as a partitioned table. We 
will mapping the
+ * partition path (e.g. 2021/03/10) to the only partition column (e.g. "dt").
+ *
+ * 3、Else the the partition columns size is not equal to the partition 
directory level and the
+ * size is great than "1" (e.g. partition column is "dt,hh", the partition 
path is "2021/03/10/12"),
+ * we read it as a Non-Partitioned table because we cannot know how to mapping 
the partition
+ * path with the partition columns in this case.
+ *
+ */
+abstract class AbstractHoodieTableFileIndex(engineContext: HoodieEngineContext,
+                                            metaClient: HoodieTableMetaClient,
+                                            configProperties: TypedProperties,
+                                            specifiedQueryInstant: 
Option[String] = None,
+                                            @transient fileStatusCache: 
FileStatusCacheTrait) {
+  /**
+   * Get all completeCommits.
+   */
+  lazy val completedCommits = metaClient.getCommitsTimeline
+    
.filterCompletedInstants().getInstants.iterator().toList.map(_.getTimestamp)
+  /**
+   * Get the partition schema from the hoodie.properties.
+   */
+  private lazy val _partitionColumns: Array[String] =
+    metaClient.getTableConfig.getPartitionFields.orElse(Array[String]())
+
+  private lazy val fileSystemStorageConfig = 
FileSystemViewStorageConfig.newBuilder()
+    .fromProperties(configProperties)
+    .build()
+  private lazy val metadataConfig = HoodieMetadataConfig.newBuilder
+    .fromProperties(configProperties)
+    .build()
+  protected val basePath: String = metaClient.getBasePath
+
+  private val queryType = configProperties(QUERY_TYPE.key())
+  private val tableType = metaClient.getTableType
+
+  @transient private val queryPath = new 
Path(configProperties.getOrElse("path", "'path' option required"))
+  @transient
+  @volatile protected var cachedFileSize: Long = 0L
+  @transient
+  @volatile protected var cachedAllInputFileSlices: Map[PartitionPath, 
Seq[FileSlice]] = _
+  @volatile protected var queryAsNonePartitionedTable: Boolean = _
+  @transient
+  @volatile private var fileSystemView: HoodieTableFileSystemView = _
+
+  refresh0()
+
+  /**
+   * Fetch list of latest base files and log files per partition.
+   *
+   * @return mapping from string partition paths to its base/log files
+   */
+  def listFileSlices(): Map[String, Seq[FileSlice]] = {
+    if (queryAsNonePartitionedTable) {
+      // Read as Non-Partitioned table.
+      cachedAllInputFileSlices.map(entry => (entry._1.path, entry._2))
+    } else {
+      cachedAllInputFileSlices.keys.toSeq.map(partition => {
+        (partition.path, cachedAllInputFileSlices(partition))
+      }).toMap
+    }
+  }
+
+  /**
+   * Returns the FileStatus for all the base files (excluding log files). This 
should be used only for
+   * cases where Spark directly fetches the list of files via HoodieFileIndex 
or for read optimized query logic
+   * implemented internally within Hudi like HoodieBootstrapRelation. This 
helps avoid the use of path filter
+   * to filter out log files within Spark.
+   *
+   * @return List of FileStatus for base files
+   */
+  def allFiles: Seq[FileStatus] = {
+    cachedAllInputFileSlices.values.flatten
+      .filter(_.getBaseFile.isPresent)
+      .map(_.getBaseFile.get().getFileStatus)
+      .toSeq
+  }
+
+  private def refresh0(): Unit = {
+    val startTime = System.currentTimeMillis()
+    val partitionFiles = loadPartitionPathFiles()
+    val allFiles = partitionFiles.values.reduceOption(_ ++ _)
+      .getOrElse(Array.empty[FileStatus])
+
+    metaClient.reloadActiveTimeline()
+    val activeInstants = 
metaClient.getActiveTimeline.getCommitsTimeline.filterCompletedInstants
+    val latestInstant = activeInstants.lastInstant()
+    // TODO we can optimize the flow by:
+    //  - First fetch list of files from instants of interest
+    //  - Load FileStatus's
+    fileSystemView = new HoodieTableFileSystemView(metaClient, activeInstants, 
allFiles)
+    val queryInstant = if (specifiedQueryInstant.isDefined) {
+      specifiedQueryInstant
+    } else if (latestInstant.isPresent) {
+      Some(latestInstant.get.getTimestamp)
+    } else {
+      None
+    }
+
+    (tableType, queryType) match {
+      case (MERGE_ON_READ, QUERY_TYPE_SNAPSHOT_OPT_VAL) =>

Review comment:
       Should incremental be supported here as well?




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