|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.catalyst.optimizer |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 21 | +import org.apache.spark.sql.catalyst.dsl.plans._ |
| 22 | +import org.apache.spark.sql.catalyst.plans.PlanTest |
| 23 | +import org.apache.spark.sql.catalyst.plans.logical.LocalRelation |
| 24 | +import org.apache.spark.sql.internal.SQLConf.OPTIMIZER_EXCLUDED_RULES |
| 25 | + |
| 26 | + |
| 27 | +class OptimizerRuleExclusionSuite extends PlanTest { |
| 28 | + |
| 29 | + val testRelation = LocalRelation('a.int, 'b.int, 'c.int) |
| 30 | + |
| 31 | + private def verifyExcludedRules(excludedRuleNames: Seq[String]) { |
| 32 | + val optimizer = new SimpleTestOptimizer() |
| 33 | + // Batches whose rules are all to be excluded should be removed as a whole. |
| 34 | + val excludedBatchNames = optimizer.batches |
| 35 | + .filter(batch => batch.rules.forall(rule => excludedRuleNames.contains(rule.ruleName))) |
| 36 | + .map(_.name) |
| 37 | + |
| 38 | + withSQLConf( |
| 39 | + OPTIMIZER_EXCLUDED_RULES.key -> excludedRuleNames.foldLeft("")((l, r) => l + "," + r)) { |
| 40 | + val batches = optimizer.batches |
| 41 | + assert(batches.forall(batch => !excludedBatchNames.contains(batch.name))) |
| 42 | + assert( |
| 43 | + batches |
| 44 | + .forall(batch => batch.rules.forall(rule => !excludedRuleNames.contains(rule.ruleName)))) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + test("Exclude a single rule from multiple batches") { |
| 49 | + verifyExcludedRules( |
| 50 | + Seq( |
| 51 | + PushPredicateThroughJoin.ruleName)) |
| 52 | + } |
| 53 | + |
| 54 | + test("Exclude multiple rules from single or multiple batches") { |
| 55 | + verifyExcludedRules( |
| 56 | + Seq( |
| 57 | + CombineUnions.ruleName, |
| 58 | + RemoveLiteralFromGroupExpressions.ruleName, |
| 59 | + RemoveRepetitionFromGroupExpressions.ruleName)) |
| 60 | + } |
| 61 | + |
| 62 | + test("Exclude non-existent rule with other valid rules") { |
| 63 | + verifyExcludedRules( |
| 64 | + Seq( |
| 65 | + LimitPushDown.ruleName, |
| 66 | + InferFiltersFromConstraints.ruleName, |
| 67 | + "DummyRuleName")) |
| 68 | + } |
| 69 | + |
| 70 | + test("Try to exclude a non-excludable rule") { |
| 71 | + val excludedRules = Seq( |
| 72 | + ReplaceIntersectWithSemiJoin.ruleName, |
| 73 | + PullupCorrelatedPredicates.ruleName) |
| 74 | + |
| 75 | + val optimizer = new SimpleTestOptimizer() |
| 76 | + |
| 77 | + withSQLConf( |
| 78 | + OPTIMIZER_EXCLUDED_RULES.key -> excludedRules.foldLeft("")((l, r) => l + "," + r)) { |
| 79 | + excludedRules.foreach { excludedRule => |
| 80 | + assert( |
| 81 | + optimizer.batches |
| 82 | + .exists(batch => batch.rules.exists(rule => rule.ruleName == excludedRule))) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + test("Verify optimized plan after excluding CombineUnions rule") { |
| 88 | + val excludedRules = Seq( |
| 89 | + ConvertToLocalRelation.ruleName, |
| 90 | + PropagateEmptyRelation.ruleName, |
| 91 | + CombineUnions.ruleName) |
| 92 | + |
| 93 | + withSQLConf( |
| 94 | + OPTIMIZER_EXCLUDED_RULES.key -> excludedRules.foldLeft("")((l, r) => l + "," + r)) { |
| 95 | + val optimizer = new SimpleTestOptimizer() |
| 96 | + val originalQuery = testRelation.union(testRelation.union(testRelation)).analyze |
| 97 | + val optimized = optimizer.execute(originalQuery) |
| 98 | + comparePlans(originalQuery, optimized) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments