Skip to content
Closed
Prev Previous commit
Next Next commit
Null value should be handled by NullPropagation.
  • Loading branch information
wangyum committed Dec 16, 2020
commit 859893d0a8629d689ee72db9e9897f772e5ef4a5
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {

private def isAlwaysFalse(exps: Seq[Expression], equalTo: Literal): Boolean = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isAlwaysFalse seems to be mismatched a little with the behavior of this function. Technically, this function resembles contains function where expr is a list and equalTo literal is the item to be check the containment.

scala> Seq(1, 2).contains(3)
res0: Boolean = false

scala> Seq(1, 2).contains(2)
res1: Boolean = true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, do you have a recommended function name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, something like notEqualForAll instead of isAlwaysFalse?

exps.forall(_.isInstanceOf[Literal]) &&
exps.forall(_.asInstanceOf[Literal].value != null) &&
exps.forall(!EqualTo(_, equalTo).eval(EmptyRow).asInstanceOf[Boolean])
Copy link
Contributor

@cloud-fan cloud-fan Dec 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EqualTo may return null, we need to take care of it, because null is not false in SQL.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Null value should be handled by NullPropagation.

}

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

// Null value should be handled by NullPropagation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to make optimizer rules orthogonal and don't rely on each other. Let's make isAlwaysFalse stricter:

exps.forall  { e =>
  val res = EqualTo(_, equalTo).eval(EmptyRow)
  res != null && !res.asInstanceOf[Boolean]
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

exps.forall {
  case l: Literal =>
    val res = EqualTo(l, equalTo).eval(EmptyRow)
    res != null && !res.asInstanceOf[Boolean]
  case _ => false
}

otherwise this test will fail:

val nonFoldable = CaseWhen(Seq(normalBranch, (a, b)), None)
assertEquivalent(EqualTo(nonFoldable, Literal(1)), EqualTo(nonFoldable, Literal(1)))

case EqualTo(i @ If(_, trueValue, falseValue), right: Literal)
if i.deterministic && isAlwaysFalse(trueValue :: falseValue :: Nil, right) =>
if i.deterministic && right.value != null &&
isAlwaysFalse(trueValue :: falseValue :: Nil, right) =>
FalseLiteral

case EqualTo(c @ CaseWhen(branches, elseValue), right: Literal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more general idea is to push down EqualTo(or other binary expressions) through If/CaseWhen.

For the example case, (CASE WHEN id = 1 THEN 'b' WHEN id = 3 THEN 'c' END) = 'a', we go through the following steps

  1. CASE WHEN id = 1 THEN 'b' = 'a' WHEN id = 3 THEN 'c' = 'a' END
  2. CASE WHEN id = 1 THEN false WHEN id = 3 THEN false END
  3. false

Step 3 may need more work in SimplifyConditionals to make it possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this more general idea.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can create a new rule to do it, e.g. PushFoldableIntoBranches

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

if c.deterministic && isAlwaysFalse(branches.map(_._2) ++ elseValue, right) =>
if c.deterministic && right.value != null &&
isAlwaysFalse(branches.map(_._2) ++ elseValue, right) =>
FalseLiteral
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,13 @@ class SimplifyConditionalSuite extends PlanTest with ExpressionEvalHelper with P
assert(!nonDeterministic.deterministic)
assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1)))

// null check, SPARK-33798 will change the following two behaviors.
// Null value should be handled by NullPropagation.
assertEquivalent(
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(2)),
FalseLiteral)
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(2)))
assertEquivalent(
EqualTo(If(a =!= Literal(1), Literal(1), Literal(2)), Literal(null, IntegerType)),
FalseLiteral)

EqualTo(If(a =!= Literal(1), Literal(1), Literal(2)), Literal(null, IntegerType)))
assertEquivalent(
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)),
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)))
Expand Down Expand Up @@ -273,14 +272,13 @@ class SimplifyConditionalSuite extends PlanTest with ExpressionEvalHelper with P
assert(!nonDeterministic.deterministic)
assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1)))

// null check, SPARK-33798 will change the following two behaviors.
// Null value should be handled by NullPropagation.
assertEquivalent(
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(2)),
FalseLiteral)
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(2)))
assertEquivalent(
EqualTo(CaseWhen(Seq((a, Literal(1))), Some(Literal(2))), Literal(null, IntegerType)),
FalseLiteral)

EqualTo(CaseWhen(Seq((a, Literal(1))), Some(Literal(2))), Literal(null, IntegerType)))
assertEquivalent(
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(1)),
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(1)))
Expand Down