-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-29938][SQL][FOLLOW-UP] Improve AlterTableAddPartitionCommand #27293
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
03ca0a3
improve AlterTableAddPartitionCommand
Ngone51 5d4ee52
revert config name
Ngone51 3601a74
reuse calculateLocationsSizes
Ngone51 bfe82cc
remove conf
Ngone51 3116493
add whitespace
Ngone51 1898d9b
return sum
Ngone51 c34e1bd
size -> number
Ngone51 d8aa2bd
append ]
Ngone51 1de7baf
rename
Ngone51 06b3cb8
fix style
Ngone51 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 |
|---|---|---|
|
|
@@ -37,6 +37,17 @@ import org.apache.spark.sql.execution.datasources.{DataSourceUtils, InMemoryFile | |
| import org.apache.spark.sql.internal.{SessionState, SQLConf} | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
| * For the purpose of calculating total directory sizes, use this filter to | ||
| * ignore some irrelevant files. | ||
| * @param stagingDir hive staging dir | ||
| */ | ||
| class PathFilterIgnoreNonData(stagingDir: String) extends PathFilter with Serializable { | ||
| override def accept(path: Path): Boolean = { | ||
| val fileName = path.getName | ||
| !fileName.startsWith(stagingDir) && DataSourceUtils.isDataFile(fileName) | ||
| } | ||
| } | ||
|
|
||
| object CommandUtils extends Logging { | ||
|
|
||
|
|
@@ -60,32 +71,21 @@ object CommandUtils extends Logging { | |
| val sessionState = spark.sessionState | ||
| val startTime = System.nanoTime() | ||
| val totalSize = if (catalogTable.partitionColumnNames.isEmpty) { | ||
| calculateLocationSize(sessionState, catalogTable.identifier, catalogTable.storage.locationUri) | ||
| calculateSingleLocationSize(sessionState, catalogTable.identifier, | ||
| catalogTable.storage.locationUri) | ||
| } else { | ||
| // Calculate table size as a sum of the visible partitions. See SPARK-21079 | ||
| val partitions = sessionState.catalog.listPartitions(catalogTable.identifier) | ||
| logInfo(s"Starting to calculate sizes for ${partitions.length} partitions.") | ||
| if (spark.sessionState.conf.parallelFileListingInStatsComputation) { | ||
| val paths = partitions.map(x => new Path(x.storage.locationUri.get)) | ||
| val stagingDir = sessionState.conf.getConfString("hive.exec.stagingdir", ".hive-staging") | ||
| val pathFilter = new PathFilter with Serializable { | ||
| override def accept(path: Path): Boolean = isDataPath(path, stagingDir) | ||
| } | ||
| val fileStatusSeq = InMemoryFileIndex.bulkListLeafFiles( | ||
| paths, sessionState.newHadoopConf(), pathFilter, spark, areRootPaths = true) | ||
| fileStatusSeq.flatMap(_._2.map(_.getLen)).sum | ||
| } else { | ||
| partitions.map { p => | ||
| calculateLocationSize(sessionState, catalogTable.identifier, p.storage.locationUri) | ||
| }.sum | ||
| } | ||
| val paths = partitions.map(_.storage.locationUri) | ||
| calculateTotalLocationSize(spark, catalogTable.identifier, paths) | ||
| } | ||
| logInfo(s"It took ${(System.nanoTime() - startTime) / (1000 * 1000)} ms to calculate" + | ||
| s" the total size for table ${catalogTable.identifier}.") | ||
| totalSize | ||
| } | ||
|
|
||
| def calculateLocationSize( | ||
| def calculateSingleLocationSize( | ||
| sessionState: SessionState, | ||
| identifier: TableIdentifier, | ||
| locationUri: Option[URI]): Long = { | ||
|
|
@@ -137,6 +137,38 @@ object CommandUtils extends Logging { | |
| size | ||
| } | ||
|
|
||
| def calculateTotalLocationSize( | ||
| sparkSession: SparkSession, | ||
| tid: TableIdentifier, | ||
| paths: Seq[Option[URI]]): Long = { | ||
| if (sparkSession.sessionState.conf.parallelFileListingInStatsComputation) { | ||
| calculateLocationsSizesParallel(sparkSession, paths.map(_.map(new Path(_)))) | ||
| } else { | ||
| paths.map(p => calculateSingleLocationSize(sparkSession.sessionState, tid, p)).sum | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Launch a Job to list all leaf files in `paths` and compute the total size | ||
| * for each path. | ||
| * @param sparkSession the [[SparkSession]] | ||
| * @param paths the Seq of [[Option[Path]]]s | ||
| * @return total size of all partitions | ||
| */ | ||
| def calculateLocationsSizesParallel( | ||
|
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. ditto.
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. Renamed to |
||
| sparkSession: SparkSession, | ||
| paths: Seq[Option[Path]]): Long = { | ||
| val stagingDir = sparkSession.sessionState.conf | ||
| .getConfString("hive.exec.stagingdir", ".hive-staging") | ||
| val filter = new PathFilterIgnoreNonData(stagingDir) | ||
| val sizes = InMemoryFileIndex.bulkListLeafFiles(paths.flatten, | ||
| sparkSession.sessionState.newHadoopConf(), filter, sparkSession, areRootPaths = true).map { | ||
| case (_, files) => files.map(_.getLen).sum | ||
| } | ||
| // the size is 0 where paths(i) is not defined and sizes(i) where it is defined | ||
| paths.zipWithIndex.filter(_._1.isDefined).map(i => sizes(i._2)).sum | ||
| } | ||
|
|
||
| def compareAndGetNewStats( | ||
| oldStats: Option[CatalogStatistics], | ||
| newTotalSize: BigInt, | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Since this become a variable from now, we need to add
.checkValue(to prevent accidental user mistakes.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.
Maybe,
> 0? Do you think we have a reasonable upper bound for this, too?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.
Lower bound checking might be enough, done in ac8b771.