Skip to content

Commit 14f2b41

Browse files
committed
Update PruneFilter rule.
1 parent ea09164 commit 14f2b41

6 files changed

Lines changed: 22 additions & 28 deletions

File tree

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ abstract class Optimizer(sessionCatalog: SessionCatalog, conf: SQLConf)
102102
SimplifyConditionals,
103103
RemoveDispensableExpressions,
104104
SimplifyBinaryComparison,
105-
PruneFilters(conf),
105+
PruneFilters,
106106
EliminateSorts,
107107
SimplifyCasts,
108108
SimplifyCaseConversionExpressions,
@@ -717,7 +717,7 @@ object EliminateSorts extends Rule[LogicalPlan] {
717717
* 2) by substituting a dummy empty relation when the filter will always evaluate to `false`.
718718
* 3) by eliminating the always-true conditions given the constraints on the child's output.
719719
*/
720-
case class PruneFilters(conf: SQLConf) extends Rule[LogicalPlan] with PredicateHelper {
720+
object PruneFilters extends Rule[LogicalPlan] with PredicateHelper {
721721
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
722722
// If the filter condition always evaluate to true, remove the filter.
723723
case Filter(Literal(true, BooleanType), child) => child

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/BinaryComparisonSimplificationSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class BinaryComparisonSimplificationSuite extends PlanTest with PredicateHelper
3737
ConstantFolding,
3838
BooleanSimplification,
3939
SimplifyBinaryComparison,
40-
PruneFilters(conf)) :: Nil
40+
PruneFilters) :: Nil
4141
}
4242

4343
val nullableRelation = LocalRelation('a.int.withNullability(true))

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/BooleanSimplificationSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class BooleanSimplificationSuite extends PlanTest with PredicateHelper {
3838
NullPropagation(conf),
3939
ConstantFolding,
4040
BooleanSimplification,
41-
PruneFilters(conf)) :: Nil
41+
PruneFilters) :: Nil
4242
}
4343

4444
val testRelation = LocalRelation('a.int, 'b.int, 'c.int, 'd.string)

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PropagateEmptyRelationSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PropagateEmptyRelationSuite extends PlanTest {
3333
ReplaceExceptWithAntiJoin,
3434
ReplaceIntersectWithSemiJoin,
3535
PushDownPredicate,
36-
PruneFilters(conf),
36+
PruneFilters,
3737
PropagateEmptyRelation) :: Nil
3838
}
3939

@@ -45,7 +45,7 @@ class PropagateEmptyRelationSuite extends PlanTest {
4545
ReplaceExceptWithAntiJoin,
4646
ReplaceIntersectWithSemiJoin,
4747
PushDownPredicate,
48-
PruneFilters(conf)) :: Nil
48+
PruneFilters) :: Nil
4949
}
5050

5151
val testRelation1 = LocalRelation.fromExternalRows(Seq('a.int), data = Seq(Row(1)))

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/PruneFiltersSuite.scala

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.apache.spark.sql.catalyst.expressions._
2424
import org.apache.spark.sql.catalyst.plans._
2525
import org.apache.spark.sql.catalyst.plans.logical._
2626
import org.apache.spark.sql.catalyst.rules._
27+
import org.apache.spark.sql.internal.SQLConf
2728
import org.apache.spark.sql.internal.SQLConf.CONSTRAINT_PROPAGATION_ENABLED
2829

2930
class PruneFiltersSuite extends PlanTest {
@@ -34,18 +35,7 @@ class PruneFiltersSuite extends PlanTest {
3435
EliminateSubqueryAliases) ::
3536
Batch("Filter Pushdown and Pruning", Once,
3637
CombineFilters,
37-
PruneFilters(conf),
38-
PushDownPredicate,
39-
PushPredicateThroughJoin) :: Nil
40-
}
41-
42-
object OptimizeWithConstraintPropagationDisabled extends RuleExecutor[LogicalPlan] {
43-
val batches =
44-
Batch("Subqueries", Once,
45-
EliminateSubqueryAliases) ::
46-
Batch("Filter Pushdown and Pruning", Once,
47-
CombineFilters,
48-
PruneFilters(conf.copy(CONSTRAINT_PROPAGATION_ENABLED -> false)),
38+
PruneFilters,
4939
PushDownPredicate,
5040
PushPredicateThroughJoin) :: Nil
5141
}
@@ -159,15 +149,19 @@ class PruneFiltersSuite extends PlanTest {
159149
("tr1.a".attr > 10 || "tr1.c".attr < 10) &&
160150
'd.attr < 100)
161151

162-
val optimized =
163-
OptimizeWithConstraintPropagationDisabled.execute(queryWithUselessFilter.analyze)
164-
// When constraint propagation is disabled, the useless filter won't be pruned.
165-
// It gets pushed down. Because the rule `CombineFilters` runs only once, there are redundant
166-
// and duplicate filters.
167-
val correctAnswer = tr1
168-
.where("tr1.a".attr > 10 || "tr1.c".attr < 10).where("tr1.a".attr > 10 || "tr1.c".attr < 10)
169-
.join(tr2.where('d.attr < 100).where('d.attr < 100),
152+
SQLConf.get.setConf(SQLConf.CONSTRAINT_PROPAGATION_ENABLED, false)
153+
try {
154+
val optimized = Optimize.execute(queryWithUselessFilter.analyze)
155+
// When constraint propagation is disabled, the useless filter won't be pruned.
156+
// It gets pushed down. Because the rule `CombineFilters` runs only once, there are redundant
157+
// and duplicate filters.
158+
val correctAnswer = tr1
159+
.where("tr1.a".attr > 10 || "tr1.c".attr < 10).where("tr1.a".attr > 10 || "tr1.c".attr < 10)
160+
.join(tr2.where('d.attr < 100).where('d.attr < 100),
170161
Inner, Some("tr1.a".attr === "tr2.a".attr)).analyze
171-
comparePlans(optimized, correctAnswer)
162+
comparePlans(optimized, correctAnswer)
163+
} finally {
164+
SQLConf.get.setConf(SQLConf.CONSTRAINT_PROPAGATION_ENABLED, true)
165+
}
172166
}
173167
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/SetOperationSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SetOperationSuite extends PlanTest {
3434
CombineUnions,
3535
PushProjectionThroughUnion,
3636
PushDownPredicate,
37-
PruneFilters(conf)) :: Nil
37+
PruneFilters) :: Nil
3838
}
3939

4040
val testRelation = LocalRelation('a.int, 'b.int, 'c.int)

0 commit comments

Comments
 (0)