umehrot2 commented on a change in pull request #2651: URL: https://github.com/apache/hudi/pull/2651#discussion_r591998950
########## File path: hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/hudi/HoodieFileIndex.scala ########## @@ -0,0 +1,279 @@ +/* + * 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 scala.collection.JavaConverters._ +import org.apache.hadoop.fs.{FileStatus, FileSystem, Path} +import org.apache.hudi.common.fs.FSUtils +import org.apache.hudi.common.model.HoodieBaseFile +import org.apache.hudi.common.table.{HoodieTableMetaClient, TableSchemaResolver} +import org.apache.hudi.common.table.view.HoodieTableFileSystemView +import org.apache.spark.internal.Logging +import org.apache.spark.sql.catalyst.{InternalRow, expressions} +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.avro.SchemaConverters +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, BoundReference, Expression, InterpretedPredicate} +import org.apache.spark.sql.catalyst.util.{CaseInsensitiveMap, DateTimeUtils} +import org.apache.spark.sql.execution.datasources.{FileIndex, PartitionDirectory, PartitionUtils} +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.StructType + +import scala.collection.mutable +import scala.collection.mutable.ListBuffer + +/** + * 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. + * + * Note: + * Only when the URL_ENCODE_PARTITIONING_OPT_KEY is enable, we can store the partition columns + * to the hoodie.properties in HoodieSqlWriter when write table. So that the query can benefit + * from the partition prune. + */ +case class HoodieFileIndex( + spark: SparkSession, + basePath: String, + schemaSpec: Option[StructType], + options: Map[String, String]) + extends FileIndex with Logging { + + private val hadoopConf = spark.sessionState.newHadoopConf() + private val fs = new Path(basePath).getFileSystem(hadoopConf) + private lazy val metaClient = HoodieTableMetaClient + .builder().setConf(hadoopConf).setBasePath(basePath).build() + + private val queryPath = new Path(options.getOrElse("path", "'path' option required")) + /** + * Get the schema of the table. + */ + lazy val schema: StructType = schemaSpec.getOrElse({ + val schemaUtil = new TableSchemaResolver(metaClient) + SchemaConverters.toSqlType(schemaUtil.getTableAvroSchema) + .dataType.asInstanceOf[StructType] + }) + + /** + * Get the partition schema. + */ + private lazy val _partitionSchema: StructType = { + val tableConfig = metaClient.getTableConfig + val partitionColumns = tableConfig.getPartitionColumns + val nameFieldMap = schema.fields.map(filed => filed.name -> filed).toMap + // If the URL_ENCODE_PARTITIONING_OPT_KEY has enable, the partition schema will stored in + // hoodie.properties, So we can benefit from the partition prune. + if (partitionColumns.isPresent) { + val partitionFields = partitionColumns.get().map(column => + nameFieldMap.getOrElse(column, throw new IllegalArgumentException(s"Cannot find column " + + s"$column in the schema[${schema.fields.mkString(",")}]"))) + new StructType(partitionFields) + } else { // If the URL_ENCODE_PARTITIONING_OPT_KEY is disable, we trait it as a + // none-partitioned table. + new StructType() + } + } + + private val timeZoneId = CaseInsensitiveMap(options) + .get(DateTimeUtils.TIMEZONE_OPTION) + .getOrElse(SQLConf.get.sessionLocalTimeZone) + + @volatile private var fileSystemView: HoodieTableFileSystemView = _ + @volatile private var cachedAllInputFiles: Array[HoodieBaseFile] = _ + @volatile private var cachedFileSize: Long = 0L + @volatile private var cachedAllPartitionPaths: Seq[PartitionPath] = _ + + refresh() + + override def rootPaths: Seq[Path] = queryPath :: Nil + + override def listFiles(partitionFilters: Seq[Expression], + dataFilters: Seq[Expression]): Seq[PartitionDirectory] = { + if (partitionSchema.fields.isEmpty) { // None partition table. + Seq(PartitionDirectory(InternalRow.empty, allFiles)) + } else { + // Prune the partition path by the partition filters + val prunedPartitions = prunePartition(cachedAllPartitionPaths, partitionFilters) + prunedPartitions.map { partition => + val fileStatues = fileSystemView.getLatestBaseFiles(partition.partitionPath).iterator() + .asScala.toSeq + .map(_.getFileStatus) + PartitionDirectory(partition.values, fileStatues) + } + } + } + + override def inputFiles: Array[String] = { + cachedAllInputFiles.map(_.getFileStatus.getPath.toString) + } + + override def refresh(): Unit = { + val startTime = System.currentTimeMillis() + val partitionFiles = loadPartitionPathFiles(queryPath, fs) + val allFiles = if (partitionFiles.isEmpty) Array.empty[FileStatus] + else partitionFiles.values.reduce(_ ++ _).toArray + + metaClient.reloadActiveTimeline() + val activeInstants = metaClient.getActiveTimeline.getCommitsTimeline.filterCompletedInstants + fileSystemView = new HoodieTableFileSystemView(metaClient, activeInstants, allFiles) + cachedAllInputFiles = fileSystemView.getLatestBaseFiles.iterator().asScala.toArray + cachedAllPartitionPaths = partitionFiles.keys.toSeq + cachedFileSize = cachedAllInputFiles.map(_.getFileLen).sum + + val flushSpend = System.currentTimeMillis() - startTime + logInfo(s"Refresh for table ${metaClient.getTableConfig.getTableName}," + + s" spend: $flushSpend ms") + } + + override def sizeInBytes: Long = { + cachedFileSize + } + + override def partitionSchema: StructType = _partitionSchema + + /** + * Get the data schema of the table. + * @return + */ + def dataSchema: StructType = { + val partitionColumns = _partitionSchema.fields.map(_.name).toSet + StructType(schema.fields.filterNot(f => partitionColumns.contains(f.name))) + } + + def allFiles: Seq[FileStatus] = cachedAllInputFiles.map(_.getFileStatus) + + private def prunePartition(partitionPaths: Seq[PartitionPath], + predicates: Seq[Expression]): Seq[PartitionPath] = { + + val partitionColumnNames = partitionSchema.fields.map(_.name).toSet + val partitionPruningPredicates = predicates.filter { + _.references.map(_.name).toSet.subsetOf(partitionColumnNames) + } + if (partitionPruningPredicates.nonEmpty) { + val predicate = partitionPruningPredicates.reduce(expressions.And) + + val boundPredicate = InterpretedPredicate.create(predicate.transform { + case a: AttributeReference => + val index = partitionSchema.indexWhere(a.name == _.name) + BoundReference(index, partitionSchema(index).dataType, nullable = true) + }) + + val selected = partitionPaths.filter { + case PartitionPath(values, _) => boundPredicate.eval(values) + } + logInfo { + val total = partitionPaths.length + val selectedSize = selected.length + val percentPruned = (1 - selectedSize.toDouble / total.toDouble) * 100 + s"Selected $selectedSize partitions out of $total, " + + s"pruned ${if (total == 0) "0" else s"$percentPruned%"} partitions." + } + selected + } else { + partitionPaths + } + } + + /** + * Load all partition paths and it's files under the specify directory. + * @param dir The specify directory to load. + * @param fs FileSystem + * @return A Map of PartitionPath and files in the PartitionPath under the "dir" directory. + */ + private def loadPartitionPathFiles(dir: Path, fs: FileSystem): Map[PartitionPath, Seq[FileStatus]] = { + val subFiles = fs.listStatus(dir).filterNot(_.getPath.getName.startsWith(".")) Review comment: This does not seem right. It is performing listing of the entire dataset at the Spark driver itself. It is loosing all the benefits of distributing the listing across nodes in the cluster. It may work for one partition, but will be terribly slow for large datasets with queries touching large number or all partitions. It needs to be parallelized with spark context. https://github.com/apache/hudi/blob/master/hudi-common/src/main/java/org/apache/hudi/common/fs/FSUtils.java#L254 can list all the partitions in parallel using engine context. We have to use something like that to identify all partitions parallely and on the executors parallely execute `getLatestBaseFiles()` for each partition to get the latest base files. ########## File path: hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/hudi/HoodieFileIndex.scala ########## @@ -0,0 +1,279 @@ +/* + * 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 scala.collection.JavaConverters._ +import org.apache.hadoop.fs.{FileStatus, FileSystem, Path} +import org.apache.hudi.common.fs.FSUtils +import org.apache.hudi.common.model.HoodieBaseFile +import org.apache.hudi.common.table.{HoodieTableMetaClient, TableSchemaResolver} +import org.apache.hudi.common.table.view.HoodieTableFileSystemView +import org.apache.spark.internal.Logging +import org.apache.spark.sql.catalyst.{InternalRow, expressions} +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.avro.SchemaConverters +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, BoundReference, Expression, InterpretedPredicate} +import org.apache.spark.sql.catalyst.util.{CaseInsensitiveMap, DateTimeUtils} +import org.apache.spark.sql.execution.datasources.{FileIndex, PartitionDirectory, PartitionUtils} +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.StructType + +import scala.collection.mutable +import scala.collection.mutable.ListBuffer + +/** + * 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. + * + * Note: + * Only when the URL_ENCODE_PARTITIONING_OPT_KEY is enable, we can store the partition columns + * to the hoodie.properties in HoodieSqlWriter when write table. So that the query can benefit + * from the partition prune. + */ +case class HoodieFileIndex( + spark: SparkSession, + basePath: String, Review comment: We cannot assume one base path that needs to be listed. Hudi also has the `hoodie.datasource.read.paths` option that allows customers to pass several paths. It is not just restricted to Bootstrap code (as is the assumption) in this pull request, and can be used by customers irrespective. For ex: Customer may not pass the actual path, but in `hoodie.datasource.read.paths` pass several partition paths: `s3://basepath/partition1`, `s3://basepath/partition2` Also, if we want to support globbing in that case as well multiple paths need to be supported. ########## File path: hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/hudi/DefaultSource.scala ########## @@ -66,10 +69,15 @@ class DefaultSource extends RelationProvider override def createRelation(sqlContext: SQLContext, optParams: Map[String, String], schema: StructType): BaseRelation = { + // Remove the "*" from the path in order to be compatible with the previous query path with "*" + val path = removeStar(optParams.get("path")) Review comment: I would ideally not like to go with this assumption. This would break and return incorrect results for Hudi customers using globbed paths till now. If possible, we should try to implement it in a way that supports both globbed paths and incorrect paths. Spark's `InMemoryFileIndex` for example can handle both globbed and non-globbed paths, and if we are implementing our own FileIndex then we should see if we can handle that in our implementation too. I need to take a deeper look, but is it not possible to glob the paths and pass all the paths to the `HoodieFileIndex` and then list all of them like `InMemoryFileIndex` does. It accepts multiple `rootPathsSpecified`. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
