Skip to content

Commit 859893d

Browse files
committed
Null value should be handled by NullPropagation.
1 parent 831bf66 commit 859893d

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
472472

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

@@ -529,12 +530,15 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
529530
e.copy(branches = branches.take(i).map(branch => (branch._1, elseValue)))
530531
}
531532

533+
// Null value should be handled by NullPropagation.
532534
case EqualTo(i @ If(_, trueValue, falseValue), right: Literal)
533-
if i.deterministic && isAlwaysFalse(trueValue :: falseValue :: Nil, right) =>
535+
if i.deterministic && right.value != null &&
536+
isAlwaysFalse(trueValue :: falseValue :: Nil, right) =>
534537
FalseLiteral
535538

536539
case EqualTo(c @ CaseWhen(branches, elseValue), right: Literal)
537-
if c.deterministic && isAlwaysFalse(branches.map(_._2) ++ elseValue, right) =>
540+
if c.deterministic && right.value != null &&
541+
isAlwaysFalse(branches.map(_._2) ++ elseValue, right) =>
538542
FalseLiteral
539543
}
540544
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,13 @@ 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 change the following two behaviors.
223+
// Null value should be handled by NullPropagation.
224224
assertEquivalent(
225225
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(2)),
226-
FalseLiteral)
226+
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(2)))
227227
assertEquivalent(
228228
EqualTo(If(a =!= Literal(1), Literal(1), Literal(2)), Literal(null, IntegerType)),
229-
FalseLiteral)
230-
229+
EqualTo(If(a =!= Literal(1), Literal(1), Literal(2)), Literal(null, IntegerType)))
231230
assertEquivalent(
232231
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)),
233232
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)))
@@ -273,14 +272,13 @@ class SimplifyConditionalSuite extends PlanTest with ExpressionEvalHelper with P
273272
assert(!nonDeterministic.deterministic)
274273
assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1)))
275274

276-
// null check, SPARK-33798 will change the following two behaviors.
275+
// Null value should be handled by NullPropagation.
277276
assertEquivalent(
278277
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(2)),
279-
FalseLiteral)
278+
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(2)))
280279
assertEquivalent(
281280
EqualTo(CaseWhen(Seq((a, Literal(1))), Some(Literal(2))), Literal(null, IntegerType)),
282-
FalseLiteral)
283-
281+
EqualTo(CaseWhen(Seq((a, Literal(1))), Some(Literal(2))), Literal(null, IntegerType)))
284282
assertEquivalent(
285283
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(1)),
286284
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(1)))

0 commit comments

Comments
 (0)