Skip to content

Commit 831bf66

Browse files
committed
Address comments
1 parent 19b0a83 commit 831bf66

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
471471
}
472472

473473
private def isAlwaysFalse(exps: Seq[Expression], equalTo: Literal): Boolean = {
474-
exps.forall(!EqualTo(_, equalTo).eval(EmptyRow).asInstanceOf[Boolean])
474+
exps.forall(_.isInstanceOf[Literal]) &&
475+
exps.forall(!EqualTo(_, equalTo).eval(EmptyRow).asInstanceOf[Boolean])
475476
}
476477

477478
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
@@ -528,13 +529,12 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
528529
e.copy(branches = branches.take(i).map(branch => (branch._1, elseValue)))
529530
}
530531

531-
case EqualTo(i @ If(_, trueValue: Literal, falseValue: Literal), right: Literal)
532+
case EqualTo(i @ If(_, trueValue, falseValue), right: Literal)
532533
if i.deterministic && isAlwaysFalse(trueValue :: falseValue :: Nil, right) =>
533534
FalseLiteral
534535

535-
case EqualTo(c @ CaseWhen(branches, elseValue), right: Literal) if c.deterministic &&
536-
(branches.map(_._2) ++ elseValue).forall(_.isInstanceOf[Literal]) &&
537-
isAlwaysFalse(branches.map(_._2) ++ elseValue, right) =>
536+
case EqualTo(c @ CaseWhen(branches, elseValue), right: Literal)
537+
if c.deterministic && isAlwaysFalse(branches.map(_._2) ++ elseValue, right) =>
538538
FalseLiteral
539539
}
540540
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,23 @@ class SimplifyConditionalSuite extends PlanTest with ExpressionEvalHelper with P
220220
assert(!nonDeterministic.deterministic)
221221
assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1)))
222222

223-
// null check, SPARK-33798 will not change these behaviors.
224-
assertEquivalent(
225-
EqualTo(If(FalseLiteral, Literal(null, IntegerType), Literal(1)), Literal(1)),
226-
TrueLiteral)
223+
// null check, SPARK-33798 will change the following two behaviors.
227224
assertEquivalent(
228-
EqualTo(If(TrueLiteral, Literal(null, IntegerType), Literal(1)), Literal(1)),
229-
Literal(null, BooleanType))
225+
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(2)),
226+
FalseLiteral)
230227
assertEquivalent(
231-
EqualTo(If(FalseLiteral, Literal(null, IntegerType), Literal(null, IntegerType)), Literal(1)),
232-
Literal(null, BooleanType))
228+
EqualTo(If(a =!= Literal(1), Literal(1), Literal(2)), Literal(null, IntegerType)),
229+
FalseLiteral)
233230

234231
assertEquivalent(
235-
EqualTo(If(FalseLiteral, Literal(1), Literal(2)), Literal(null, IntegerType)),
236-
Literal(null, BooleanType))
232+
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)),
233+
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)))
237234
assertEquivalent(
238-
EqualTo(If(TrueLiteral, Literal(1), Literal(2)), Literal(null, IntegerType)),
235+
EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)),
236+
EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)))
237+
assertEquivalent(
238+
EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(null, IntegerType)),
239+
Literal(1)),
239240
Literal(null, BooleanType))
240241
}
241242

0 commit comments

Comments
 (0)