Skip to content

Commit 8bd9ef9

Browse files
committed
further optimize
1 parent cccbaf1 commit 8bd9ef9

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ import org.apache.spark.sql.types.BooleanType
3939
* - CASE WHEN cond THEN trueVal ELSE null END => AND(cond, trueVal)
4040
* - CASE WHEN cond THEN trueVal ELSE true END => OR(NOT(cond), trueVal)
4141
* - CASE WHEN cond THEN false ELSE elseVal END => AND(NOT(cond), elseVal)
42-
* - CASE WHEN cond THEN false END => AND(NOT(cond), false)
42+
* - CASE WHEN cond THEN false END => false
4343
* - CASE WHEN cond THEN true ELSE elseVal END => OR(cond, elseVal)
44-
* - CASE WHEN cond THEN true END => OR(cond, false)
44+
* - CASE WHEN cond THEN true END => cond
4545
*/
4646
object SimplifyConditionalsInPredicate extends Rule[LogicalPlan] {
4747

@@ -64,14 +64,18 @@ object SimplifyConditionalsInPredicate extends Rule[LogicalPlan] {
6464
And(cond, trueValue)
6565
case CaseWhen(Seq((cond, trueValue)), Some(TrueLiteral)) =>
6666
Or(Not(cond), trueValue)
67-
case CaseWhen(Seq((cond, FalseLiteral)), elseValue) =>
68-
And(Not(cond), elseValue.getOrElse(FalseLiteral))
69-
case CaseWhen(Seq((cond, TrueLiteral)), elseValue) =>
70-
Or(cond, elseValue.getOrElse(FalseLiteral))
67+
case CaseWhen(Seq((_, FalseLiteral)), Some(FalseLiteral) | None) =>
68+
FalseLiteral
69+
case CaseWhen(Seq((cond, FalseLiteral)), Some(elseValue)) =>
70+
And(Not(cond), elseValue)
71+
case CaseWhen(Seq((cond, TrueLiteral)), Some(FalseLiteral) | None) =>
72+
cond
73+
case CaseWhen(Seq((cond, TrueLiteral)), Some(elseValue)) =>
74+
Or(cond, elseValue)
7175
case e if e.dataType == BooleanType => e
7276
case e =>
7377
assert(e.dataType != BooleanType,
74-
"Expected a Boolean type expression in simplifyConditional, " +
78+
"Expected a Boolean type expression in SimplifyConditionalsInPredicate, " +
7579
s"but got the type `${e.dataType.catalogString}` in `${e.sql}`.")
7680
e
7781
}

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
4444
LocalRelation('i.int, 'b.boolean, 'a.array(IntegerType), 'm.map(IntegerType, IntegerType))
4545
private val anotherTestRelation = LocalRelation('d.int)
4646

47-
test("if(cond, trueVal, false) => And(cond, trueVal)") {
47+
test("IF(cond, trueVal, false) => AND(cond, trueVal)") {
4848
val originalCond = If(
4949
UnresolvedAttribute("i") > Literal(10),
5050
UnresolvedAttribute("b"),
@@ -59,7 +59,7 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
5959
testProjection(originalCond, expectedExpr = originalCond)
6060
}
6161

62-
test("if(cond, trueVal, true) => or(not(cond), trueVal)") {
62+
test("IF(cond, trueVal, true) => OR(NOT(cond), trueVal)") {
6363
val originalCond = If(
6464
UnresolvedAttribute("i") > Literal(10),
6565
UnresolvedAttribute("b"),
@@ -74,7 +74,7 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
7474
testProjection(originalCond, expectedExpr = originalCond)
7575
}
7676

77-
test("if(cond, false, falseVal) => and(not(cond), falseVal)") {
77+
test("IF(cond, false, falseVal) => AND(NOT(cond), elseVal)") {
7878
val originalCond = If(
7979
UnresolvedAttribute("i") > Literal(10),
8080
FalseLiteral,
@@ -89,7 +89,7 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
8989
testProjection(originalCond, expectedExpr = originalCond)
9090
}
9191

92-
test("if(cond, true, falseVal) => or(cond, falseVal)") {
92+
test("IF(cond, true, falseVal) => OR(cond, elseVal)") {
9393
val originalCond = If(
9494
UnresolvedAttribute("i") > Literal(10),
9595
TrueLiteral,
@@ -104,8 +104,7 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
104104
testProjection(originalCond, expectedExpr = originalCond)
105105
}
106106

107-
108-
test("case when cond then trueVal else false end => And(cond, trueVal)") {
107+
test("CASE WHEN cond THEN trueVal ELSE false END => AND(cond, trueVal)") {
109108
Seq(Some(FalseLiteral), None, Some(Literal(null, BooleanType))).foreach { elseExp =>
110109
val originalCond = CaseWhen(
111110
Seq((UnresolvedAttribute("i") > Literal(10), UnresolvedAttribute("b"))),
@@ -121,7 +120,7 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
121120
}
122121
}
123122

124-
test("case when cond then trueVal else true end => or(not(cond), trueVal)") {
123+
test("CASE WHEN cond THEN trueVal ELSE true END => OR(NOT(cond), trueVal)") {
125124
val originalCond = CaseWhen(
126125
Seq((UnresolvedAttribute("i") > Literal(10), UnresolvedAttribute("b"))),
127126
TrueLiteral)
@@ -135,8 +134,7 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
135134
testProjection(originalCond, expectedExpr = originalCond)
136135
}
137136

138-
test("case when cond then false else elseValue end => and(not(cond), elseValue)") {
139-
Seq()
137+
test("CASE WHEN cond THEN false ELSE elseVal END => AND(NOT(cond), elseVal)") {
140138
val originalCond = CaseWhen(
141139
Seq((UnresolvedAttribute("i") > Literal(10), FalseLiteral)),
142140
UnresolvedAttribute("b"))
@@ -150,7 +148,17 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
150148
testProjection(originalCond, expectedExpr = originalCond)
151149
}
152150

153-
test("case when cond then true else elseValue end => or(cond, elseValue)") {
151+
test("CASE WHEN cond THEN false END => false") {
152+
val originalCond = CaseWhen(
153+
Seq((UnresolvedAttribute("i") > Literal(10), FalseLiteral)))
154+
testFilter(originalCond, expectedCond = FalseLiteral)
155+
testJoin(originalCond, expectedCond = FalseLiteral)
156+
testDelete(originalCond, expectedCond = FalseLiteral)
157+
testUpdate(originalCond, expectedCond = FalseLiteral)
158+
testProjection(originalCond, expectedExpr = originalCond)
159+
}
160+
161+
test("CASE WHEN cond THEN true ELSE elseVal END => OR(cond, elseVal)") {
154162
val originalCond = CaseWhen(
155163
Seq((UnresolvedAttribute("i") > Literal(10), TrueLiteral)),
156164
UnresolvedAttribute("b"))
@@ -164,7 +172,7 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
164172
testProjection(originalCond, expectedExpr = originalCond)
165173
}
166174

167-
test("case when cond then true end => or(cond, null)") {
175+
test("CASE WHEN cond THEN true END => cond") {
168176
val originalCond = CaseWhen(
169177
Seq((UnresolvedAttribute("i") > Literal(10), TrueLiteral)))
170178
val expectedCond = UnresolvedAttribute("i") > Literal(10)
@@ -189,7 +197,7 @@ class SimplifyConditionalsInPredicateSuite extends PlanTest {
189197
testProjection(originalCond, expectedExpr = originalCond)
190198
}
191199

192-
test("Not expected type - simplifyConditional") {
200+
test("Not expected type - SimplifyConditionalsInPredicate") {
193201
val e = intercept[AnalysisException] {
194202
testFilter(originalCond = Literal(null, IntegerType), expectedCond = FalseLiteral)
195203
}.getMessage

0 commit comments

Comments
 (0)