-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-23817][SQL] Create file source V2 framework and migrate ORC read path #23383
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
5d5f59e
9ac801e
5dd34c4
5f35e75
cdb1f99
c1e1f4b
ba66011
91b146b
91475ca
38a27f2
9bbd87b
38b8af5
c91b84a
2c43797
af9de22
2f7b3c6
91aeafb
5ebeda0
3b50f2d
ea8f178
aff3788
575643b
ac8acdd
13d615b
a1e66f3
c4b94c8
471ff1b
7da03ea
0ce4a30
ff8608e
6e87532
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 |
|---|---|---|
|
|
@@ -373,8 +373,7 @@ case class FileSourceScanExec( | |
| val filesGroupedToBuckets = | ||
| selectedPartitions.flatMap { p => | ||
| p.files.filter(_.getLen > 0).map { f => | ||
| val hosts = getBlockHosts(getBlockLocations(f), 0, f.getLen) | ||
| PartitionedFile(p.values, f.getPath.toUri.toString, 0, f.getLen, hosts) | ||
| PartitionedFileUtil.getPartitionedFile(f, f.getPath, p.values) | ||
| } | ||
| }.groupBy { f => | ||
| BucketingUtils | ||
|
|
@@ -410,107 +409,35 @@ case class FileSourceScanExec( | |
| readFile: (PartitionedFile) => Iterator[InternalRow], | ||
| selectedPartitions: Seq[PartitionDirectory], | ||
| fsRelation: HadoopFsRelation): RDD[InternalRow] = { | ||
| val defaultMaxSplitBytes = | ||
| fsRelation.sparkSession.sessionState.conf.filesMaxPartitionBytes | ||
| val openCostInBytes = fsRelation.sparkSession.sessionState.conf.filesOpenCostInBytes | ||
| val defaultParallelism = fsRelation.sparkSession.sparkContext.defaultParallelism | ||
| val totalBytes = selectedPartitions.flatMap(_.files.map(_.getLen + openCostInBytes)).sum | ||
| val bytesPerCore = totalBytes / defaultParallelism | ||
|
|
||
| val maxSplitBytes = Math.min(defaultMaxSplitBytes, Math.max(openCostInBytes, bytesPerCore)) | ||
| val maxSplitBytes = | ||
| FilePartition.maxSplitBytes(fsRelation.sparkSession, selectedPartitions) | ||
| logInfo(s"Planning scan with bin packing, max size: $maxSplitBytes bytes, " + | ||
| s"open cost is considered as scanning $openCostInBytes bytes.") | ||
|
|
||
| val splitFiles = selectedPartitions.flatMap { partition => | ||
| partition.files.filter(_.getLen > 0).flatMap { file => | ||
| val blockLocations = getBlockLocations(file) | ||
| if (fsRelation.fileFormat.isSplitable( | ||
| fsRelation.sparkSession, fsRelation.options, file.getPath)) { | ||
| (0L until file.getLen by maxSplitBytes).map { offset => | ||
| val remaining = file.getLen - offset | ||
| val size = if (remaining > maxSplitBytes) maxSplitBytes else remaining | ||
| val hosts = getBlockHosts(blockLocations, offset, size) | ||
| PartitionedFile( | ||
| partition.values, file.getPath.toUri.toString, offset, size, hosts) | ||
| } | ||
| } else { | ||
| val hosts = getBlockHosts(blockLocations, 0, file.getLen) | ||
| Seq(PartitionedFile( | ||
| partition.values, file.getPath.toUri.toString, 0, file.getLen, hosts)) | ||
| } | ||
| // getPath() is very expensive so we only want to call it once in this block: | ||
| val filePath = file.getPath | ||
|
Member
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. @gengliangwang I'd like to understand why "getPath() is very expensive", it seems just a simple field access, should be relatively cheap.
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. It can be expensive on object storage services like aws s3
Member
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. @gengliangwang Could you please elaborate more? I didn't find anything special for I noticed this comment when working on SPARK-52011 (#50765). The change brings many benefits for HDFS, but I don't have an object storage environment to test. |
||
| val isSplitable = relation.fileFormat.isSplitable( | ||
| relation.sparkSession, relation.options, filePath) | ||
| PartitionedFileUtil.splitFiles( | ||
| sparkSession = relation.sparkSession, | ||
| file = file, | ||
| filePath = filePath, | ||
| isSplitable = isSplitable, | ||
| maxSplitBytes = maxSplitBytes, | ||
| partitionValues = partition.values | ||
| ) | ||
| } | ||
| }.toArray.sortBy(_.length)(implicitly[Ordering[Long]].reverse) | ||
|
|
||
| val partitions = new ArrayBuffer[FilePartition] | ||
| val currentFiles = new ArrayBuffer[PartitionedFile] | ||
| var currentSize = 0L | ||
|
|
||
| /** Close the current partition and move to the next. */ | ||
| def closePartition(): Unit = { | ||
| if (currentFiles.nonEmpty) { | ||
| val newPartition = | ||
| FilePartition( | ||
| partitions.size, | ||
| currentFiles.toArray.toSeq) // Copy to a new Array. | ||
| partitions += newPartition | ||
| } | ||
| currentFiles.clear() | ||
| currentSize = 0 | ||
| } | ||
|
|
||
| // Assign files to partitions using "Next Fit Decreasing" | ||
| splitFiles.foreach { file => | ||
| if (currentSize + file.length > maxSplitBytes) { | ||
| closePartition() | ||
| } | ||
| // Add the given file to the current partition. | ||
| currentSize += file.length + openCostInBytes | ||
| currentFiles += file | ||
| } | ||
| closePartition() | ||
| val partitions = | ||
| FilePartition.getFilePartitions(relation.sparkSession, splitFiles, maxSplitBytes) | ||
|
|
||
| new FileScanRDD(fsRelation.sparkSession, readFile, partitions) | ||
| } | ||
|
|
||
| private def getBlockLocations(file: FileStatus): Array[BlockLocation] = file match { | ||
| case f: LocatedFileStatus => f.getBlockLocations | ||
| case f => Array.empty[BlockLocation] | ||
| } | ||
|
|
||
| // Given locations of all blocks of a single file, `blockLocations`, and an `(offset, length)` | ||
| // pair that represents a segment of the same file, find out the block that contains the largest | ||
| // fraction the segment, and returns location hosts of that block. If no such block can be found, | ||
| // returns an empty array. | ||
| private def getBlockHosts( | ||
| blockLocations: Array[BlockLocation], offset: Long, length: Long): Array[String] = { | ||
| val candidates = blockLocations.map { | ||
| // The fragment starts from a position within this block | ||
| case b if b.getOffset <= offset && offset < b.getOffset + b.getLength => | ||
| b.getHosts -> (b.getOffset + b.getLength - offset).min(length) | ||
|
|
||
| // The fragment ends at a position within this block | ||
| case b if offset <= b.getOffset && offset + length < b.getLength => | ||
| b.getHosts -> (offset + length - b.getOffset).min(length) | ||
|
|
||
| // The fragment fully contains this block | ||
| case b if offset <= b.getOffset && b.getOffset + b.getLength <= offset + length => | ||
| b.getHosts -> b.getLength | ||
|
|
||
| // The fragment doesn't intersect with this block | ||
| case b => | ||
| b.getHosts -> 0L | ||
| }.filter { case (hosts, size) => | ||
| size > 0L | ||
| } | ||
|
|
||
| if (candidates.isEmpty) { | ||
| Array.empty[String] | ||
| } else { | ||
| val (hosts, _) = candidates.maxBy { case (_, size) => size } | ||
| hosts | ||
| } | ||
| } | ||
|
|
||
| override def doCanonicalize(): FileSourceScanExec = { | ||
| FileSourceScanExec( | ||
| relation, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * 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.spark.sql.execution | ||
|
|
||
| import org.apache.hadoop.fs.{BlockLocation, FileStatus, LocatedFileStatus, Path} | ||
|
|
||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.execution.datasources._ | ||
|
|
||
| object PartitionedFileUtil { | ||
|
||
| def splitFiles( | ||
| sparkSession: SparkSession, | ||
| file: FileStatus, | ||
| filePath: Path, | ||
| isSplitable: Boolean, | ||
| maxSplitBytes: Long, | ||
| partitionValues: InternalRow): Seq[PartitionedFile] = { | ||
| if (isSplitable) { | ||
| (0L until file.getLen by maxSplitBytes).map { offset => | ||
| val remaining = file.getLen - offset | ||
| val size = if (remaining > maxSplitBytes) maxSplitBytes else remaining | ||
| val hosts = getBlockHosts(getBlockLocations(file), offset, size) | ||
| PartitionedFile(partitionValues, filePath.toUri.toString, offset, size, hosts) | ||
| } | ||
| } else { | ||
| Seq(getPartitionedFile(file, filePath, partitionValues)) | ||
| } | ||
| } | ||
|
|
||
| def getPartitionedFile( | ||
| file: FileStatus, | ||
| filePath: Path, | ||
| partitionValues: InternalRow): PartitionedFile = { | ||
| val hosts = getBlockHosts(getBlockLocations(file), 0, file.getLen) | ||
| PartitionedFile(partitionValues, filePath.toUri.toString, 0, file.getLen, hosts) | ||
| } | ||
|
|
||
| private def getBlockLocations(file: FileStatus): Array[BlockLocation] = file match { | ||
| case f: LocatedFileStatus => f.getBlockLocations | ||
| case f => Array.empty[BlockLocation] | ||
| } | ||
| // Given locations of all blocks of a single file, `blockLocations`, and an `(offset, length)` | ||
| // pair that represents a segment of the same file, find out the block that contains the largest | ||
| // fraction the segment, and returns location hosts of that block. If no such block can be found, | ||
| // returns an empty array. | ||
| private def getBlockHosts( | ||
gengliangwang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| blockLocations: Array[BlockLocation], | ||
| offset: Long, | ||
| length: Long): Array[String] = { | ||
| val candidates = blockLocations.map { | ||
| // The fragment starts from a position within this block | ||
| case b if b.getOffset <= offset && offset < b.getOffset + b.getLength => | ||
| b.getHosts -> (b.getOffset + b.getLength - offset).min(length) | ||
|
|
||
| // The fragment ends at a position within this block | ||
| case b if offset <= b.getOffset && offset + length < b.getLength => | ||
| b.getHosts -> (offset + length - b.getOffset).min(length) | ||
|
|
||
| // The fragment fully contains this block | ||
| case b if offset <= b.getOffset && b.getOffset + b.getLength <= offset + length => | ||
| b.getHosts -> b.getLength | ||
|
|
||
| // The fragment doesn't intersect with this block | ||
| case b => | ||
| b.getHosts -> 0L | ||
| }.filter { case (hosts, size) => | ||
| size > 0L | ||
| } | ||
|
|
||
| if (candidates.isEmpty) { | ||
| Array.empty[String] | ||
| } else { | ||
| val (hosts, _) = candidates.maxBy { case (_, size) => size } | ||
| hosts | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
Given the overall DSv2 risk, shall we make this
publicand add migration doc officially, @gatorsmile ?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.
the streaming flag is also internal, if we want to change it, let's change them together in a followup PR.