-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-15616][SQL] Add optimizer rule PruneHiveTablePartitions #26805
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
15 commits
Select commit
Hold shift + click to select a range
8981759
Add optimizer rule PruneHiveTablePartitions pruning hive table partit…
fuwhu cd4af95
Refine code.
fuwhu e4698c5
Remove conf item FALL_BACK_TO_HDFS_FOR_STATS_MAX_PART_NUM, leaving it…
fuwhu 8987233
Refine code.
fuwhu 1eafe1e
Add PruneHiveTablePartitionsSute.
fuwhu 5dd01fd
Refine PruneHiveTablePartitions : prune partitions through metasotre …
fuwhu 79e5cf9
Fix indentation.
fuwhu 7fa3718
Drop sizeInBytes of partition when it can't be got from metadata.
fuwhu 0b21e77
Don't need to prune again in PruneHiveTablePartitions.prunePartitions…
fuwhu a9ce634
Make PruneHiveTablePartitions.getPartitionKeyFilters follows PruneFil…
fuwhu fea6fdc
empty commit
fuwhu 14ae878
leave statistic unchanged if the sizeInBytes of some partition is not…
fuwhu 6a4a4b2
refine code.
fuwhu ce20439
Refine code.
fuwhu b1798d5
Fix scala code style.
fuwhu 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
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
110 changes: 110 additions & 0 deletions
110
sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/PruneHiveTablePartitions.scala
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 |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * 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.hive.execution | ||
|
|
||
| import org.apache.hadoop.hive.common.StatsSetupConst | ||
|
|
||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.catalyst.analysis.CastSupport | ||
| import org.apache.spark.sql.catalyst.catalog.{CatalogStatistics, CatalogTable, CatalogTablePartition, ExternalCatalogUtils, HiveTableRelation} | ||
| import org.apache.spark.sql.catalyst.expressions.{And, AttributeSet, Expression, ExpressionSet, SubqueryExpression} | ||
| import org.apache.spark.sql.catalyst.planning.PhysicalOperation | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.execution.datasources.DataSourceStrategy | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| /** | ||
| * TODO: merge this with PruneFileSourcePartitions after we completely make hive as a data source. | ||
| */ | ||
| private[sql] class PruneHiveTablePartitions(session: SparkSession) | ||
| extends Rule[LogicalPlan] with CastSupport { | ||
|
|
||
| override val conf: SQLConf = session.sessionState.conf | ||
|
|
||
| /** | ||
| * Extract the partition filters from the filters on the table. | ||
| */ | ||
| private def getPartitionKeyFilters( | ||
| filters: Seq[Expression], | ||
| relation: HiveTableRelation): ExpressionSet = { | ||
| val normalizedFilters = DataSourceStrategy.normalizeExprs( | ||
| filters.filter(f => f.deterministic && !SubqueryExpression.hasSubquery(f)), relation.output) | ||
| val partitionColumnSet = AttributeSet(relation.partitionCols) | ||
| ExpressionSet(normalizedFilters.filter { f => | ||
| !f.references.isEmpty && f.references.subsetOf(partitionColumnSet) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Prune the hive table using filters on the partitions of the table. | ||
| */ | ||
| private def prunePartitions( | ||
| relation: HiveTableRelation, | ||
| partitionFilters: ExpressionSet): Seq[CatalogTablePartition] = { | ||
| if (conf.metastorePartitionPruning) { | ||
cloud-fan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| session.sessionState.catalog.listPartitionsByFilter( | ||
| relation.tableMeta.identifier, partitionFilters.toSeq) | ||
| } else { | ||
| ExternalCatalogUtils.prunePartitionsByFilter(relation.tableMeta, | ||
cloud-fan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| session.sessionState.catalog.listPartitions(relation.tableMeta.identifier), | ||
| partitionFilters.toSeq, conf.sessionLocalTimeZone) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update the statistics of the table. | ||
| */ | ||
| private def updateTableMeta( | ||
| tableMeta: CatalogTable, | ||
| prunedPartitions: Seq[CatalogTablePartition]): CatalogTable = { | ||
| val sizeOfPartitions = prunedPartitions.map { partition => | ||
| val rawDataSize = partition.parameters.get(StatsSetupConst.RAW_DATA_SIZE).map(_.toLong) | ||
| val totalSize = partition.parameters.get(StatsSetupConst.TOTAL_SIZE).map(_.toLong) | ||
| if (rawDataSize.isDefined && rawDataSize.get > 0) { | ||
| rawDataSize.get | ||
| } else if (totalSize.isDefined && totalSize.get > 0L) { | ||
| totalSize.get | ||
| } else { | ||
| 0L | ||
| } | ||
| } | ||
| if (sizeOfPartitions.forall(_ > 0)) { | ||
| val sizeInBytes = sizeOfPartitions.sum | ||
| tableMeta.copy(stats = Some(CatalogStatistics(sizeInBytes = BigInt(sizeInBytes)))) | ||
| } else { | ||
| tableMeta | ||
| } | ||
| } | ||
|
|
||
| override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { | ||
| case op @ PhysicalOperation(projections, filters, relation: HiveTableRelation) | ||
| if filters.nonEmpty && relation.isPartitioned && relation.prunedPartitions.isEmpty => | ||
| val partitionKeyFilters = getPartitionKeyFilters(filters, relation) | ||
| if (partitionKeyFilters.nonEmpty) { | ||
| val newPartitions = prunePartitions(relation, partitionKeyFilters) | ||
| val newTableMeta = updateTableMeta(relation.tableMeta, newPartitions) | ||
| val newRelation = relation.copy( | ||
| tableMeta = newTableMeta, prunedPartitions = Some(newPartitions)) | ||
| // Keep partition filters so that they are visible in physical planning | ||
| Project(projections, Filter(filters.reduceLeft(And), newRelation)) | ||
cloud-fan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
cloud-fan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| op | ||
| } | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
...ve/src/test/scala/org/apache/spark/sql/hive/execution/PruneHiveTablePartitionsSuite.scala
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * 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.hive.execution | ||
|
|
||
| import org.apache.spark.sql.QueryTest | ||
| import org.apache.spark.sql.catalyst.analysis.EliminateSubqueryAliases | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
| import org.apache.spark.sql.hive.test.TestHiveSingleton | ||
| import org.apache.spark.sql.test.SQLTestUtils | ||
|
|
||
| class PruneHiveTablePartitionsSuite extends QueryTest with SQLTestUtils with TestHiveSingleton { | ||
|
|
||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = | ||
| Batch("PruneHiveTablePartitions", Once, | ||
| EliminateSubqueryAliases, new PruneHiveTablePartitions(spark)) :: Nil | ||
| } | ||
|
|
||
| test("SPARK-15616 statistics pruned after going throuhg PruneHiveTablePartitions") { | ||
| withTable("test", "temp") { | ||
| sql( | ||
| s""" | ||
| |CREATE TABLE test(i int) | ||
| |PARTITIONED BY (p int) | ||
| |STORED AS textfile""".stripMargin) | ||
| spark.range(0, 1000, 1).selectExpr("id as col") | ||
| .createOrReplaceTempView("temp") | ||
|
|
||
| for (part <- Seq(1, 2, 3, 4)) { | ||
| sql( | ||
| s""" | ||
| |INSERT OVERWRITE TABLE test PARTITION (p='$part') | ||
| |select col from temp""".stripMargin) | ||
| } | ||
| val analyzed1 = sql("select i from test where p > 0").queryExecution.analyzed | ||
| val analyzed2 = sql("select i from test where p = 1").queryExecution.analyzed | ||
| assert(Optimize.execute(analyzed1).stats.sizeInBytes / 4 === | ||
| Optimize.execute(analyzed2).stats.sizeInBytes) | ||
| } | ||
| } | ||
| } |
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.
@fuwhu We need a description about the rule. Could you submit a follow-up PR to add the descriptions to both PruneHiveTablePartitions and PruneFileSourcePartitions?
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.
sure, so you mean just add class description in PruneHiveTablePartitions.scala and PruneFileSourcePartitions.scala file ? Or need to add comment in some doc ?
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.
classdoc is good enough
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.
@gatorsmile @cloud-fan classdoc added in #27535 , please help review, thanks.