-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-27384][SQL] File source V2: Prune unnecessary partition columns #24296
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 1 commit
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,15 +16,43 @@ | |
| */ | ||
| package org.apache.spark.sql.execution.datasources.v2 | ||
|
|
||
| import org.apache.spark.sql.sources.v2.reader.{ScanBuilder, SupportsPushDownFilters, SupportsPushDownRequiredColumns} | ||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.execution.datasources.{PartitioningAwareFileIndex, PartitioningUtils} | ||
| import org.apache.spark.sql.sources.v2.reader.{ScanBuilder, SupportsPushDownRequiredColumns} | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| abstract class FileScanBuilder(schema: StructType) | ||
| extends ScanBuilder | ||
| with SupportsPushDownRequiredColumns { | ||
| protected var readSchema = schema | ||
| abstract class FileScanBuilder( | ||
| sparkSession: SparkSession, | ||
| fileIndex: PartitioningAwareFileIndex, | ||
| dataSchema: StructType) extends ScanBuilder with SupportsPushDownRequiredColumns { | ||
| private val partitionSchema = fileIndex.partitionSchema | ||
| private val isCaseSensitive = sparkSession.sessionState.conf.caseSensitiveAnalysis | ||
| protected var requiredSchema = StructType(dataSchema.fields ++ partitionSchema.fields) | ||
|
|
||
| override def pruneColumns(requiredSchema: StructType): Unit = { | ||
| this.readSchema = requiredSchema | ||
| this.requiredSchema = requiredSchema | ||
| } | ||
|
|
||
| protected def readDataSchema: StructType = { | ||
| val fields = dataSchema.fields.filter { field => | ||
| val colName = PartitioningUtils.getColName(field, isCaseSensitive) | ||
| requiredNameSet.contains(colName) && !partitionNameSet.contains(colName) | ||
| } | ||
| StructType(fields) | ||
| } | ||
|
|
||
| protected def readPartitionSchema: StructType = { | ||
| val fields = partitionSchema.fields.filter { field => | ||
| val colName = PartitioningUtils.getColName(field, isCaseSensitive) | ||
| requiredNameSet.contains(colName) | ||
| } | ||
| StructType(fields) | ||
| } | ||
|
|
||
| // Define as method instead of value, since `requiredSchema` is mutable. | ||
|
Contributor
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. but we should not create a set inside loop body. How about |
||
| private def requiredNameSet: Set[String] = | ||
| requiredSchema.fields.map(PartitioningUtils.getColName(_, isCaseSensitive)).toSet | ||
|
|
||
| private val partitionNameSet: Set[String] = | ||
| partitionSchema.fields.map(PartitioningUtils.getColName(_, isCaseSensitive)).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
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
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 |
|---|---|---|
|
|
@@ -52,24 +52,24 @@ import org.apache.spark.util.SerializableConfiguration | |
| case class OrcPartitionReaderFactory( | ||
| sqlConf: SQLConf, | ||
| broadcastedConf: Broadcast[SerializableConfiguration], | ||
| resultSchema: StructType, | ||
|
Contributor
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. let's update the parameter doc in the classdoc. |
||
| dataSchema: StructType, | ||
| partitionSchema: StructType, | ||
| readSchema: StructType) extends FilePartitionReaderFactory { | ||
| readDataSchema: StructType, | ||
| partitionSchema: StructType) extends FilePartitionReaderFactory { | ||
| private val isCaseSensitive = sqlConf.caseSensitiveAnalysis | ||
| private val capacity = sqlConf.orcVectorizedReaderBatchSize | ||
|
|
||
| override def supportColumnarReads(partition: InputPartition): Boolean = { | ||
| sqlConf.orcVectorizedReaderEnabled && sqlConf.wholeStageEnabled && | ||
| readSchema.length <= sqlConf.wholeStageMaxNumFields && | ||
| readSchema.forall(_.dataType.isInstanceOf[AtomicType]) | ||
| resultSchema.length <= sqlConf.wholeStageMaxNumFields && | ||
| resultSchema.forall(_.dataType.isInstanceOf[AtomicType]) | ||
| } | ||
|
|
||
| override def buildReader(file: PartitionedFile): PartitionReader[InternalRow] = { | ||
| val conf = broadcastedConf.value.value | ||
|
|
||
| val readDataSchema = getReadDataSchema(readSchema, partitionSchema, isCaseSensitive) | ||
| val readDataSchemaString = OrcUtils.orcTypeDescriptionString(readDataSchema) | ||
| OrcConf.MAPRED_INPUT_SCHEMA.setString(conf, readDataSchemaString) | ||
| val resultSchemaString = OrcUtils.orcTypeDescriptionString(resultSchema) | ||
| OrcConf.MAPRED_INPUT_SCHEMA.setString(conf, resultSchemaString) | ||
| OrcConf.IS_SCHEMA_EVOLUTION_CASE_SENSITIVE.setBoolean(conf, isCaseSensitive) | ||
|
|
||
| val filePath = new Path(new URI(file.filePath)) | ||
|
|
@@ -113,8 +113,8 @@ case class OrcPartitionReaderFactory( | |
| override def buildColumnarReader(file: PartitionedFile): PartitionReader[ColumnarBatch] = { | ||
| val conf = broadcastedConf.value.value | ||
|
|
||
| val readSchemaString = OrcUtils.orcTypeDescriptionString(readSchema) | ||
| OrcConf.MAPRED_INPUT_SCHEMA.setString(conf, readSchemaString) | ||
| val resultSchemaString = OrcUtils.orcTypeDescriptionString(resultSchema) | ||
| OrcConf.MAPRED_INPUT_SCHEMA.setString(conf, resultSchemaString) | ||
| OrcConf.IS_SCHEMA_EVOLUTION_CASE_SENSITIVE.setBoolean(conf, isCaseSensitive) | ||
|
|
||
| val filePath = new Path(new URI(file.filePath)) | ||
|
|
@@ -124,13 +124,13 @@ case class OrcPartitionReaderFactory( | |
| val reader = OrcFile.createReader(filePath, readerOptions) | ||
|
|
||
| val requestedColIdsOrEmptyFile = OrcUtils.requestedColumnIds( | ||
| isCaseSensitive, dataSchema, readSchema, reader, conf) | ||
| isCaseSensitive, dataSchema, readDataSchema, reader, conf) | ||
|
|
||
| if (requestedColIdsOrEmptyFile.isEmpty) { | ||
| new EmptyPartitionReader | ||
| } else { | ||
| val requestedColIds = requestedColIdsOrEmptyFile.get | ||
| assert(requestedColIds.length == readSchema.length, | ||
| val requestedColIds = requestedColIdsOrEmptyFile.get ++ Array.fill(partitionSchema.length)(-1) | ||
| assert(requestedColIds.length == resultSchema.length, | ||
| "[BUG] requested column IDs do not match required schema") | ||
| val taskConf = new Configuration(conf) | ||
|
|
||
|
|
@@ -140,15 +140,12 @@ case class OrcPartitionReaderFactory( | |
|
|
||
| val batchReader = new OrcColumnarBatchReader(capacity) | ||
| batchReader.initialize(fileSplit, taskAttemptContext) | ||
| val columnNameMap = partitionSchema.fields.map( | ||
| PartitioningUtils.getColName(_, isCaseSensitive)).zipWithIndex.toMap | ||
| val requestedPartitionColIds = readSchema.fields.map { field => | ||
| columnNameMap.getOrElse(PartitioningUtils.getColName(field, isCaseSensitive), -1) | ||
| } | ||
| val requestedPartitionColIds = | ||
| Array.fill(readDataSchema.length)(-1) ++ Range(0, partitionSchema.length) | ||
|
|
||
| batchReader.initBatch( | ||
| TypeDescription.fromString(readSchemaString), | ||
| readSchema.fields, | ||
| TypeDescription.fromString(resultSchemaString), | ||
| resultSchema.fields, | ||
| requestedColIds, | ||
| requestedPartitionColIds, | ||
| file.partitionValues) | ||
|
|
||
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.
we should not create an attribute and just use its name. This can be