Skip to content

Commit db519c3

Browse files
committed
Add CombineFilters
1 parent 6596327 commit db519c3

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ abstract class Optimizer(sessionCatalog: SessionCatalog)
177177
InferFiltersFromConstraints,
178178
PushDownPredicate,
179179
CollapseProject,
180+
CombineFilters,
180181
RemoveRedundantProject) :+
181182
Batch("UpdateAttributeReferences", Once,
182183
UpdateNullabilityInAttributeReferences)

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ class RewriteSubquerySuite extends PlanTest {
3737
InferFiltersFromConstraints,
3838
PushDownPredicate,
3939
CollapseProject,
40+
CombineFilters,
4041
RemoveRedundantProject) :: Nil
4142
}
4243

44+
val relation = LocalRelation('a.int, 'b.int)
45+
val relInSubquery = LocalRelation('x.int, 'y.int, 'z.int)
46+
4347
test("Column pruning after rewriting predicate subquery") {
4448
withSQLConf(SQLConf.CONSTRAINT_PROPAGATION_ENABLED.key -> "false") {
45-
val relation = LocalRelation('a.int, 'b.int)
46-
val relInSubquery = LocalRelation('x.int, 'y.int, 'z.int)
47-
4849
val query = relation.where('a.in(ListQuery(relInSubquery.select('x)))).select('a)
49-
5050
val optimized = Optimize.execute(query.analyze)
51+
5152
val correctAnswer = relation
5253
.select('a)
5354
.join(relInSubquery.select('x), LeftSemi, Some('a === 'x))
@@ -59,12 +60,9 @@ class RewriteSubquerySuite extends PlanTest {
5960

6061
test("Infer filters and push down predicate after rewriting predicate subquery") {
6162
withSQLConf(SQLConf.CONSTRAINT_PROPAGATION_ENABLED.key -> "true") {
62-
val relation = LocalRelation('a.int, 'b.int)
63-
val relInSubquery = LocalRelation('x.int, 'y.int, 'z.int)
64-
6563
val query = relation.where('a.in(ListQuery(relInSubquery.select('x)))).select('a)
66-
6764
val optimized = Optimize.execute(query.analyze)
65+
6866
val correctAnswer = relation
6967
.where(IsNotNull('a)).select('a)
7068
.join(relInSubquery.where(IsNotNull('x)).select('x), LeftSemi, Some('a === 'x))
@@ -74,4 +72,17 @@ class RewriteSubquerySuite extends PlanTest {
7472
}
7573
}
7674

75+
test("combine filters after rewriting predicate subquery") {
76+
val query = relation.where('a.in(ListQuery(relInSubquery.select('x).where('y > 1)))).select('a)
77+
val optimized = Optimize.execute(query.analyze)
78+
79+
val correctAnswer = relation
80+
.where(IsNotNull('a)).select('a)
81+
.join(relInSubquery.where(IsNotNull('x) && IsNotNull('y) && 'y > 1).select('x),
82+
LeftSemi, Some('a === 'x))
83+
.analyze
84+
85+
comparePlans(optimized, correctAnswer)
86+
}
87+
7788
}

0 commit comments

Comments
 (0)