forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-24802][SQL] Add a new config for Optimization Rule Exclusion
## What changes were proposed in this pull request? Since Spark has provided fairly clear interfaces for adding user-defined optimization rules, it would be nice to have an easy-to-use interface for excluding an optimization rule from the Spark query optimizer as well. This would make customizing Spark optimizer easier and sometimes could debugging issues too. - Add a new config spark.sql.optimizer.excludedRules, with the value being a list of rule names separated by comma. - Modify the current batches method to remove the excluded rules from the default batches. Log the rules that have been excluded. - Split the existing default batches into "post-analysis batches" and "optimization batches" so that only rules in the "optimization batches" can be excluded. ## How was this patch tested? Add a new test suite: OptimizerRuleExclusionSuite Author: maryannxue <[email protected]> Closes apache#21764 from maryannxue/rule-exclusion.
- Loading branch information
1 parent
63b644b
commit 19041a4
Showing
3 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
101 changes: 101 additions & 0 deletions
101
.../src/test/scala/org/apache/spark/sql/catalyst/optimizer/OptimizerRuleExclusionSuite.scala
This file contains 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,101 @@ | ||
/* | ||
* 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.catalyst.optimizer | ||
|
||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.dsl.plans._ | ||
import org.apache.spark.sql.catalyst.plans.PlanTest | ||
import org.apache.spark.sql.catalyst.plans.logical.LocalRelation | ||
import org.apache.spark.sql.internal.SQLConf.OPTIMIZER_EXCLUDED_RULES | ||
|
||
|
||
class OptimizerRuleExclusionSuite extends PlanTest { | ||
|
||
val testRelation = LocalRelation('a.int, 'b.int, 'c.int) | ||
|
||
private def verifyExcludedRules(excludedRuleNames: Seq[String]) { | ||
val optimizer = new SimpleTestOptimizer() | ||
// Batches whose rules are all to be excluded should be removed as a whole. | ||
val excludedBatchNames = optimizer.batches | ||
.filter(batch => batch.rules.forall(rule => excludedRuleNames.contains(rule.ruleName))) | ||
.map(_.name) | ||
|
||
withSQLConf( | ||
OPTIMIZER_EXCLUDED_RULES.key -> excludedRuleNames.foldLeft("")((l, r) => l + "," + r)) { | ||
val batches = optimizer.batches | ||
assert(batches.forall(batch => !excludedBatchNames.contains(batch.name))) | ||
assert( | ||
batches | ||
.forall(batch => batch.rules.forall(rule => !excludedRuleNames.contains(rule.ruleName)))) | ||
} | ||
} | ||
|
||
test("Exclude a single rule from multiple batches") { | ||
verifyExcludedRules( | ||
Seq( | ||
PushPredicateThroughJoin.ruleName)) | ||
} | ||
|
||
test("Exclude multiple rules from single or multiple batches") { | ||
verifyExcludedRules( | ||
Seq( | ||
CombineUnions.ruleName, | ||
RemoveLiteralFromGroupExpressions.ruleName, | ||
RemoveRepetitionFromGroupExpressions.ruleName)) | ||
} | ||
|
||
test("Exclude non-existent rule with other valid rules") { | ||
verifyExcludedRules( | ||
Seq( | ||
LimitPushDown.ruleName, | ||
InferFiltersFromConstraints.ruleName, | ||
"DummyRuleName")) | ||
} | ||
|
||
test("Try to exclude a non-excludable rule") { | ||
val excludedRules = Seq( | ||
ReplaceIntersectWithSemiJoin.ruleName, | ||
PullupCorrelatedPredicates.ruleName) | ||
|
||
val optimizer = new SimpleTestOptimizer() | ||
|
||
withSQLConf( | ||
OPTIMIZER_EXCLUDED_RULES.key -> excludedRules.foldLeft("")((l, r) => l + "," + r)) { | ||
excludedRules.foreach { excludedRule => | ||
assert( | ||
optimizer.batches | ||
.exists(batch => batch.rules.exists(rule => rule.ruleName == excludedRule))) | ||
} | ||
} | ||
} | ||
|
||
test("Verify optimized plan after excluding CombineUnions rule") { | ||
val excludedRules = Seq( | ||
ConvertToLocalRelation.ruleName, | ||
PropagateEmptyRelation.ruleName, | ||
CombineUnions.ruleName) | ||
|
||
withSQLConf( | ||
OPTIMIZER_EXCLUDED_RULES.key -> excludedRules.foldLeft("")((l, r) => l + "," + r)) { | ||
val optimizer = new SimpleTestOptimizer() | ||
val originalQuery = testRelation.union(testRelation.union(testRelation)).analyze | ||
val optimized = optimizer.execute(originalQuery) | ||
comparePlans(originalQuery, optimized) | ||
} | ||
} | ||
} |