-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-15103][SQL] Refactored FileCatalog class to allow StreamFileCatalog to infer partitioning #12879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[SPARK-15103][SQL] Refactored FileCatalog class to allow StreamFileCatalog to infer partitioning #12879
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -287,36 +287,24 @@ trait FileCatalog { | |
| } | ||
|
|
||
| /** | ||
| * A file catalog that caches metadata gathered by scanning all the files present in `paths` | ||
| * recursively. | ||
| * An abstract class that represents [[FileCatalog]]s that are aware of partitioned tables. | ||
| * It provides the necessary methods to parse partition data based on a set of files. | ||
| * | ||
| * @param parameters as set of options to control discovery | ||
| * @param paths a list of paths to scan | ||
| * @param parameters as set of options to control partition discovery | ||
| * @param partitionSchema an optional partition schema that will be use to provide types for the | ||
| * discovered partitions | ||
| */ | ||
| class HDFSFileCatalog( | ||
| */ | ||
| abstract class PartitioningAwareFileCatalog( | ||
| sparkSession: SparkSession, | ||
| parameters: Map[String, String], | ||
| override val paths: Seq[Path], | ||
| partitionSchema: Option[StructType]) | ||
| extends FileCatalog with Logging { | ||
|
|
||
| private val hadoopConf = sparkSession.sessionState.newHadoopConfWithOptions(parameters) | ||
| protected val hadoopConf = sparkSession.sessionState.newHadoopConfWithOptions(parameters) | ||
|
|
||
| var leafFiles = mutable.LinkedHashMap.empty[Path, FileStatus] | ||
| var leafDirToChildrenFiles = mutable.Map.empty[Path, Array[FileStatus]] | ||
| var cachedPartitionSpec: PartitionSpec = _ | ||
| protected def leafFiles: mutable.LinkedHashMap[Path, FileStatus] | ||
|
|
||
| def partitionSpec(): PartitionSpec = { | ||
| if (cachedPartitionSpec == null) { | ||
| cachedPartitionSpec = inferPartitioning(partitionSchema) | ||
| } | ||
|
|
||
| cachedPartitionSpec | ||
| } | ||
|
|
||
| refresh() | ||
| protected def leafDirToChildrenFiles: Map[Path, Array[FileStatus]] | ||
|
|
||
| override def listFiles(filters: Seq[Expression]): Seq[Partition] = { | ||
| if (partitionSpec().partitionColumns.isEmpty) { | ||
|
|
@@ -331,7 +319,44 @@ class HDFSFileCatalog( | |
| } | ||
| } | ||
|
|
||
| protected def prunePartitions( | ||
| override def allFiles(): Seq[FileStatus] = leafFiles.values.toSeq | ||
|
|
||
| override def getStatus(path: Path): Array[FileStatus] = leafDirToChildrenFiles(path) | ||
|
|
||
| protected def inferPartitioning(): PartitionSpec = { | ||
| // We use leaf dirs containing data files to discover the schema. | ||
| val leafDirs = leafDirToChildrenFiles.keys.toSeq | ||
| partitionSchema match { | ||
| case Some(userProvidedSchema) if userProvidedSchema.nonEmpty => | ||
| val spec = PartitioningUtils.parsePartitions( | ||
| leafDirs, | ||
| PartitioningUtils.DEFAULT_PARTITION_NAME, | ||
| typeInference = false, | ||
| basePaths = basePaths) | ||
|
|
||
| // Without auto inference, all of value in the `row` should be null or in StringType, | ||
| // we need to cast into the data type that user specified. | ||
| def castPartitionValuesToUserSchema(row: InternalRow) = { | ||
| InternalRow((0 until row.numFields).map { i => | ||
| Cast( | ||
| Literal.create(row.getUTF8String(i), StringType), | ||
| userProvidedSchema.fields(i).dataType).eval() | ||
| }: _*) | ||
| } | ||
|
|
||
| PartitionSpec(userProvidedSchema, spec.partitions.map { part => | ||
| part.copy(values = castPartitionValuesToUserSchema(part.values)) | ||
| }) | ||
| case _ => | ||
| PartitioningUtils.parsePartitions( | ||
| leafDirs, | ||
| PartitioningUtils.DEFAULT_PARTITION_NAME, | ||
| typeInference = sparkSession.sessionState.conf.partitionColumnTypeInferenceEnabled(), | ||
| basePaths = basePaths) | ||
| } | ||
| } | ||
|
|
||
| private def prunePartitions( | ||
| predicates: Seq[Expression], | ||
| partitionSpec: PartitionSpec): Seq[PartitionDirectory] = { | ||
| val PartitionSpec(partitionColumns, partitions) = partitionSpec | ||
|
|
@@ -365,11 +390,78 @@ class HDFSFileCatalog( | |
| } | ||
| } | ||
|
|
||
| def allFiles(): Seq[FileStatus] = leafFiles.values.toSeq | ||
| /** | ||
| * Contains a set of paths that are considered as the base dirs of the input datasets. | ||
| * The partitioning discovery logic will make sure it will stop when it reaches any | ||
| * base path. By default, the paths of the dataset provided by users will be base paths. | ||
| * For example, if a user uses `sqlContext.read.parquet("/path/something=true/")`, the base path | ||
| * will be `/path/something=true/`, and the returned DataFrame will not contain a column of | ||
| * `something`. If users want to override the basePath. They can set `basePath` in the options | ||
| * to pass the new base path to the data source. | ||
| * For the above example, if the user-provided base path is `/path/`, the returned | ||
| * DataFrame will have the column of `something`. | ||
| */ | ||
| private def basePaths: Set[Path] = { | ||
| val userDefinedBasePath = parameters.get("basePath").map(basePath => Set(new Path(basePath))) | ||
| userDefinedBasePath.getOrElse { | ||
| // If the user does not provide basePath, we will just use paths. | ||
| paths.toSet | ||
| }.map { hdfsPath => | ||
| // Make the path qualified (consistent with listLeafFiles and listLeafFilesInParallel). | ||
| val fs = hdfsPath.getFileSystem(hadoopConf) | ||
| hdfsPath.makeQualified(fs.getUri, fs.getWorkingDirectory) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * A file catalog that caches metadata gathered by scanning all the files present in `paths` | ||
| * recursively. | ||
| * | ||
| * @param parameters as set of options to control discovery | ||
| * @param paths a list of paths to scan | ||
| * @param partitionSchema an optional partition schema that will be use to provide types for the | ||
| * discovered partitions | ||
| */ | ||
| class HDFSFileCatalog( | ||
|
||
| sparkSession: SparkSession, | ||
| parameters: Map[String, String], | ||
| override val paths: Seq[Path], | ||
| partitionSchema: Option[StructType]) | ||
| extends PartitioningAwareFileCatalog(sparkSession, parameters, partitionSchema) { | ||
|
|
||
| @volatile private var cachedLeafFiles: mutable.LinkedHashMap[Path, FileStatus] = _ | ||
| @volatile private var cachedLeafDirToChildrenFiles: Map[Path, Array[FileStatus]] = _ | ||
| @volatile private var cachedPartitionSpec: PartitionSpec = _ | ||
|
|
||
| refresh() | ||
|
|
||
| override def partitionSpec(): PartitionSpec = { | ||
| if (cachedPartitionSpec == null) { | ||
| cachedPartitionSpec = inferPartitioning() | ||
| } | ||
| cachedPartitionSpec | ||
| } | ||
|
|
||
| override protected def leafFiles: mutable.LinkedHashMap[Path, FileStatus] = { | ||
| cachedLeafFiles | ||
| } | ||
|
|
||
| override protected def leafDirToChildrenFiles: Map[Path, Array[FileStatus]] = { | ||
| cachedLeafDirToChildrenFiles | ||
| } | ||
|
|
||
| override def refresh(): Unit = { | ||
| val files = listLeafFiles(paths) | ||
| cachedLeafFiles = | ||
| new mutable.LinkedHashMap[Path, FileStatus]() ++= files.map(f => f.getPath -> f) | ||
| cachedLeafDirToChildrenFiles = files.toArray.groupBy(_.getPath.getParent) | ||
| cachedPartitionSpec = null | ||
| } | ||
|
|
||
| def getStatus(path: Path): Array[FileStatus] = leafDirToChildrenFiles(path) | ||
|
|
||
| private def listLeafFiles(paths: Seq[Path]): mutable.LinkedHashSet[FileStatus] = { | ||
| protected def listLeafFiles(paths: Seq[Path]): mutable.LinkedHashSet[FileStatus] = { | ||
| if (paths.length >= sparkSession.sessionState.conf.parallelPartitionDiscoveryThreshold) { | ||
| HadoopFsRelation.listLeafFilesInParallel(paths, hadoopConf, sparkSession.sparkContext) | ||
| } else { | ||
|
|
@@ -415,73 +507,6 @@ class HDFSFileCatalog( | |
| } | ||
| } | ||
|
|
||
| def inferPartitioning(schema: Option[StructType]): PartitionSpec = { | ||
| // We use leaf dirs containing data files to discover the schema. | ||
| val leafDirs = leafDirToChildrenFiles.keys.toSeq | ||
| schema match { | ||
| case Some(userProvidedSchema) if userProvidedSchema.nonEmpty => | ||
| val spec = PartitioningUtils.parsePartitions( | ||
| leafDirs, | ||
| PartitioningUtils.DEFAULT_PARTITION_NAME, | ||
| typeInference = false, | ||
| basePaths = basePaths) | ||
|
|
||
| // Without auto inference, all of value in the `row` should be null or in StringType, | ||
| // we need to cast into the data type that user specified. | ||
| def castPartitionValuesToUserSchema(row: InternalRow) = { | ||
| InternalRow((0 until row.numFields).map { i => | ||
| Cast( | ||
| Literal.create(row.getUTF8String(i), StringType), | ||
| userProvidedSchema.fields(i).dataType).eval() | ||
| }: _*) | ||
| } | ||
|
|
||
| PartitionSpec(userProvidedSchema, spec.partitions.map { part => | ||
| part.copy(values = castPartitionValuesToUserSchema(part.values)) | ||
| }) | ||
| case _ => | ||
| PartitioningUtils.parsePartitions( | ||
| leafDirs, | ||
| PartitioningUtils.DEFAULT_PARTITION_NAME, | ||
| typeInference = sparkSession.sessionState.conf.partitionColumnTypeInferenceEnabled(), | ||
| basePaths = basePaths) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Contains a set of paths that are considered as the base dirs of the input datasets. | ||
| * The partitioning discovery logic will make sure it will stop when it reaches any | ||
| * base path. By default, the paths of the dataset provided by users will be base paths. | ||
| * For example, if a user uses `sqlContext.read.parquet("/path/something=true/")`, the base path | ||
| * will be `/path/something=true/`, and the returned DataFrame will not contain a column of | ||
| * `something`. If users want to override the basePath. They can set `basePath` in the options | ||
| * to pass the new base path to the data source. | ||
| * For the above example, if the user-provided base path is `/path/`, the returned | ||
| * DataFrame will have the column of `something`. | ||
| */ | ||
| private def basePaths: Set[Path] = { | ||
| val userDefinedBasePath = parameters.get("basePath").map(basePath => Set(new Path(basePath))) | ||
| userDefinedBasePath.getOrElse { | ||
| // If the user does not provide basePath, we will just use paths. | ||
| paths.toSet | ||
| }.map { hdfsPath => | ||
| // Make the path qualified (consistent with listLeafFiles and listLeafFilesInParallel). | ||
| val fs = hdfsPath.getFileSystem(hadoopConf) | ||
| hdfsPath.makeQualified(fs.getUri, fs.getWorkingDirectory) | ||
| } | ||
| } | ||
|
|
||
| def refresh(): Unit = { | ||
| val files = listLeafFiles(paths) | ||
|
|
||
| leafFiles.clear() | ||
| leafDirToChildrenFiles.clear() | ||
|
|
||
| leafFiles ++= files.map(f => f.getPath -> f) | ||
| leafDirToChildrenFiles ++= files.toArray.groupBy(_.getPath.getParent) | ||
|
|
||
| cachedPartitionSpec = null | ||
| } | ||
|
|
||
| override def equals(other: Any): Boolean = other match { | ||
| case hdfs: HDFSFileCatalog => paths.toSet == hdfs.paths.toSet | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marmbrus These methods are basically unchanged, just moved around from HDFSFileCatalog to PartitioningAwareFileCatalog.