-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-18510][SQL] Follow up to address comments in #15951 #15997
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
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -118,8 +118,10 @@ case class DataSource( | |
| private def getOrInferFileFormatSchema( | ||
| format: FileFormat, | ||
| justPartitioning: Boolean = false): (StructType, StructType) = { | ||
| // the operations below are expensive therefore try not to do them if we don't need to | ||
| lazy val tempFileCatalog = { | ||
| // the operations below are expensive therefore try not to do them if we don't need to, e.g., | ||
| // in streaming mode, we have already inferred and registered partition columns, we will | ||
| // never have to materialize the lazy val below | ||
| lazy val tempFileIndex = { | ||
| val allPaths = caseInsensitiveOptions.get("path") ++ paths | ||
| val hadoopConf = sparkSession.sessionState.newHadoopConf() | ||
| val globbedPaths = allPaths.toSeq.flatMap { path => | ||
|
|
@@ -133,25 +135,25 @@ case class DataSource( | |
| val partitionSchema = if (partitionColumns.isEmpty && catalogTable.isEmpty) { | ||
| // Try to infer partitioning, because no DataSource in the read path provides the partitioning | ||
| // columns properly unless it is a Hive DataSource | ||
| val resolved = tempFileCatalog.partitionSchema.map { partitionField => | ||
| val resolved = tempFileIndex.partitionSchema.map { partitionField => | ||
| val equality = sparkSession.sessionState.conf.resolver | ||
| // SPARK-18510: try to get schema from userSpecifiedSchema, otherwise fallback to inferred | ||
| userSpecifiedSchema.flatMap(_.find(f => equality(f.name, partitionField.name))).getOrElse( | ||
| partitionField) | ||
| } | ||
| StructType(resolved) | ||
| } else { | ||
| // in streaming mode, we have already inferred and registered partition columns, we will | ||
| // never have to materialize the lazy val below | ||
| lazy val inferredPartitions = tempFileCatalog.partitionSchema | ||
| // maintain old behavior before SPARK-18510. If userSpecifiedSchema is empty used inferred | ||
| // partitioning | ||
| if (userSpecifiedSchema.isEmpty) { | ||
| val inferredPartitions = tempFileIndex.partitionSchema | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still keep the |
||
| inferredPartitions | ||
| } else { | ||
| val partitionFields = partitionColumns.map { partitionColumn => | ||
| userSpecifiedSchema.flatMap(_.find(_.name == partitionColumn)).orElse { | ||
| val inferredOpt = inferredPartitions.find(_.name == partitionColumn) | ||
| val equality = sparkSession.sessionState.conf.resolver | ||
| userSpecifiedSchema.flatMap(_.find(c => equality(c.name, partitionColumn))).orElse { | ||
| val inferredPartitions = tempFileIndex.partitionSchema | ||
| val inferredOpt = inferredPartitions.find(p => equality(p.name, partitionColumn)) | ||
| if (inferredOpt.isDefined) { | ||
| logDebug( | ||
| s"""Type of partition column: $partitionColumn not found in specified schema | ||
|
|
@@ -163,7 +165,7 @@ case class DataSource( | |
| |Falling back to inferred dataType if it exists. | ||
| """.stripMargin) | ||
| } | ||
| inferredPartitions.find(_.name == partitionColumn) | ||
| inferredOpt | ||
| }.getOrElse { | ||
| throw new AnalysisException(s"Failed to resolve the schema for $format for " + | ||
| s"the partition column: $partitionColumn. It must be specified manually.") | ||
|
|
@@ -182,7 +184,7 @@ case class DataSource( | |
| format.inferSchema( | ||
| sparkSession, | ||
| caseInsensitiveOptions, | ||
| tempFileCatalog.allFiles()) | ||
| tempFileIndex.allFiles()) | ||
| }.getOrElse { | ||
| throw new AnalysisException( | ||
| s"Unable to infer schema for $format. It must be specified manually.") | ||
|
|
@@ -224,8 +226,11 @@ case class DataSource( | |
| "you may be able to create a static DataFrame on that directory with " + | ||
| "'spark.read.load(directory)' and infer schema from it.") | ||
| } | ||
| val (schema, partCols) = getOrInferFileFormatSchema(format) | ||
| SourceInfo(s"FileSource[$path]", StructType(schema ++ partCols), partCols.fieldNames) | ||
| val (dataSchema, partitionSchema) = getOrInferFileFormatSchema(format) | ||
| SourceInfo( | ||
| s"FileSource[$path]", | ||
| StructType(dataSchema ++ partitionSchema), | ||
| partitionSchema.fieldNames) | ||
|
|
||
| case _ => | ||
| throw new UnsupportedOperationException( | ||
|
|
@@ -379,7 +384,7 @@ case class DataSource( | |
| globPath | ||
| }.toArray | ||
|
|
||
| val (dataSchema, inferredPartitionSchema) = getOrInferFileFormatSchema(format) | ||
| val (dataSchema, partitionSchema) = getOrInferFileFormatSchema(format) | ||
|
|
||
| val fileCatalog = if (sparkSession.sqlContext.conf.manageFilesourcePartitions && | ||
| catalogTable.isDefined && catalogTable.get.tracksPartitionsInCatalog) { | ||
|
|
@@ -388,12 +393,12 @@ case class DataSource( | |
| catalogTable.get, | ||
| catalogTable.get.stats.map(_.sizeInBytes.toLong).getOrElse(0L)) | ||
| } else { | ||
| new InMemoryFileIndex(sparkSession, globbedPaths, options, Some(inferredPartitionSchema)) | ||
| new InMemoryFileIndex(sparkSession, globbedPaths, options, Some(partitionSchema)) | ||
| } | ||
|
|
||
| HadoopFsRelation( | ||
| fileCatalog, | ||
| partitionSchema = inferredPartitionSchema, | ||
| partitionSchema = partitionSchema, | ||
| dataSchema = dataSchema.asNullable, | ||
| bucketSpec = bucketSpec, | ||
| format, | ||
|
|
||
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.
I removed this lazy val so that it's easy to track when
tempFileCatalogis materialized.