-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-14237][SQL] De-duplicate partition value appending logic in various buildReader() implementations #12866
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ import org.apache.spark.internal.Logging | |
| import org.apache.spark.sql._ | ||
| import org.apache.spark.sql.catalyst.{expressions, CatalystTypeConverters, InternalRow} | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.GenerateUnsafeProjection | ||
| import org.apache.spark.sql.execution.FileRelation | ||
| import org.apache.spark.sql.sources.{BaseRelation, Filter} | ||
| import org.apache.spark.sql.types.{StringType, StructType} | ||
|
|
@@ -238,6 +239,45 @@ trait FileFormat { | |
| throw new UnsupportedOperationException(s"buildReader is not supported for $this") | ||
| } | ||
|
|
||
| /** | ||
| * Exactly the same as [[buildReader]] except that the reader function returned by this method | ||
| * appends partition values to [[InternalRow]]s produced by the reader function [[buildReader]] | ||
| * returns. | ||
| */ | ||
| private[sql] def buildReaderWithPartitionValues( | ||
| sparkSession: SparkSession, | ||
| dataSchema: StructType, | ||
| partitionSchema: StructType, | ||
| requiredSchema: StructType, | ||
| filters: Seq[Filter], | ||
| options: Map[String, String], | ||
| hadoopConf: Configuration): PartitionedFile => Iterator[InternalRow] = { | ||
| val dataReader = buildReader( | ||
| sparkSession, dataSchema, partitionSchema, requiredSchema, filters, options, hadoopConf) | ||
|
|
||
| new (PartitionedFile => Iterator[InternalRow]) with Serializable { | ||
| private val fullSchema = requiredSchema.toAttributes ++ partitionSchema.toAttributes | ||
|
|
||
| private val joinedRow = new JoinedRow() | ||
|
|
||
| // Using lazy val to avoid serialization | ||
|
||
| private lazy val appendPartitionColumns = | ||
| GenerateUnsafeProjection.generate(fullSchema, fullSchema) | ||
|
|
||
| override def apply(file: PartitionedFile): Iterator[InternalRow] = { | ||
| // Using local val to avoid per-row lazy val check (pre-mature optimization?...) | ||
| val converter = appendPartitionColumns | ||
|
|
||
| // Note that we have to apply the converter even though `file.partitionValues` is empty. | ||
| // This is because the converter is also responsible for converting safe `InternalRow`s into | ||
| // `UnsafeRow`s. | ||
| dataReader(file).map { dataRow => | ||
| converter(joinedRow(dataRow, file.partitionValues)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns a [[OutputWriterFactory]] for generating output writers that can write data. | ||
| * This method is current used only by FileStreamSinkWriter to generate output writers that | ||
|
|
||
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.
@cloud-fan Instead of adding a new
ReaderFunctiontrait with aninitialize()method as you suggested, I used an anonymousFunction1class here. Not quite sure how useful theinitialize()method can be in more general cases...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.
e.g. the text datasource, which need to initialize a
UnsafeRowWriterfor one reader function(not every file).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.
That's a reasonable use case. But we can also use an anonymous
Function1class there.