Skip to content

Commit 13e489b

Browse files
committed
[SPARK-22759][SQL] Filters can be combined iff both are deterministic
## What changes were proposed in this pull request? The query execution/optimization does not guarantee the expressions are evaluated in order. We only can combine them if and only if both are deterministic. We need to update the optimizer rule: CombineFilters. ## How was this patch tested? Updated the existing tests. Author: gatorsmile <[email protected]> Closes #19947 from gatorsmile/combineFilters.
1 parent 6b80ce4 commit 13e489b

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,9 @@ object CombineUnions extends Rule[LogicalPlan] {
695695
*/
696696
object CombineFilters extends Rule[LogicalPlan] with PredicateHelper {
697697
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
698-
case Filter(fc, nf @ Filter(nc, grandChild)) =>
698+
// The query execution/optimization does not guarantee the expressions are evaluated in order.
699+
// We only can combine them if and only if both are deterministic.
700+
case Filter(fc, nf @ Filter(nc, grandChild)) if fc.deterministic && nc.deterministic =>
699701
(ExpressionSet(splitConjunctivePredicates(fc)) --
700702
ExpressionSet(splitConjunctivePredicates(nc))).reduceOption(And) match {
701703
case Some(ac) =>

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,15 @@ class FilterPushdownSuite extends PlanTest {
9494
comparePlans(optimized, correctAnswer)
9595
}
9696

97-
test("combine redundant deterministic filters") {
97+
test("do not combine non-deterministic filters even if they are identical") {
9898
val originalQuery =
9999
testRelation
100100
.where(Rand(0) > 0.1 && 'a === 1)
101-
.where(Rand(0) > 0.1 && 'a === 1)
101+
.where(Rand(0) > 0.1 && 'a === 1).analyze
102102

103-
val optimized = Optimize.execute(originalQuery.analyze)
104-
val correctAnswer =
105-
testRelation
106-
.where(Rand(0) > 0.1 && 'a === 1 && Rand(0) > 0.1)
107-
.analyze
103+
val optimized = Optimize.execute(originalQuery)
108104

109-
comparePlans(optimized, correctAnswer)
105+
comparePlans(optimized, originalQuery)
110106
}
111107

112108
test("SPARK-16164: Filter pushdown should keep the ordering in the logical plan") {

0 commit comments

Comments
 (0)