Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
case _ => false
}

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(!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.

}

def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case q: LogicalPlan => q transformExpressionsUp {
case If(TrueLiteral, trueValue, _) => trueValue
Expand Down Expand Up @@ -523,6 +528,14 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
} else {
e.copy(branches = branches.take(i).map(branch => (branch._1, elseValue)))
}

case EqualTo(i @ If(_, trueValue, falseValue), right: Literal)
if i.deterministic && 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) =>
FalseLiteral
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,98 @@ class SimplifyConditionalSuite extends PlanTest with ExpressionEvalHelper with P
If(Factorial(5) > 100L, b, nullLiteral).eval(EmptyRow))
}
}

test("SPARK-33798: simplify EqualTo(If, Literal) always false") {
val a = EqualTo(UnresolvedAttribute("a"), Literal(100))
val ifExp = If(a === Literal(1), Literal(2), Literal(3))
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's make sure the test expression is valid. a is an EqualTo, comparing boolean and Literal(1) is invalid.


assertEquivalent(EqualTo(ifExp, Literal(4)), FalseLiteral)
assertEquivalent(EqualTo(ifExp, Literal(3)), EqualTo(ifExp, Literal(3)))
assertEquivalent(EqualTo(ifExp, Literal("4")), FalseLiteral)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto, let's don't test invalid expressions, ifExp.dataType is int, not string.

assertEquivalent(EqualTo(ifExp, Literal("3")), EqualTo(ifExp, Literal(3)))

// Do not simplify if it contains non foldable expressions.
assertEquivalent(EqualTo(ifExp, NonFoldableLiteral(true)),
EqualTo(ifExp, NonFoldableLiteral(true)))
val nonFoldable = If(NonFoldableLiteral(true), Literal(1), Literal(2))
Copy link
Contributor

Choose a reason for hiding this comment

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

NonFoldableLiteral is nothing special comparing to 'a == 100, as they are both non-foldable. I think we don't need to test with NonFoldableLiteral

assertEquivalent(EqualTo(nonFoldable, Literal(1)), EqualTo(nonFoldable, Literal(1)))

// Do not simplify if it contains non-deterministic expressions.
val nonDeterministic = If(LessThan(Rand(1), Literal(0.5)), Literal(1), Literal(1))
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a case that we should optimize. But we need to distinguish between non-deterministic and has-side-effect. While we can skip running non-deterministic expressions, we can't skip running has-side-effect expressions.

This is not related to this PR, but is something worth considering. cc @viirya @dbtsai @maropu @rednaxelafx

assert(!nonDeterministic.deterministic)
assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1)))

// null check, SPARK-33798 will change the following two behaviors.
assertEquivalent(
EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(2)),
FalseLiteral)
assertEquivalent(
EqualTo(If(a =!= Literal(1), Literal(1), Literal(2)), Literal(null, IntegerType)),
FalseLiteral)

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)))
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)))
assertEquivalent(
EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(null, IntegerType)),
Literal(1)),
Literal(null, BooleanType))
}

test("SPARK-33798: simplify EqualTo(CaseWhen, Literal) always false") {
val a = EqualTo(UnresolvedAttribute("a"), Literal(100))
val b = UnresolvedAttribute("b")
val c = EqualTo(UnresolvedAttribute("c"), Literal(true))
val caseWhen = CaseWhen(Seq((a, Literal(1)), (c, Literal(2))), Some(Literal(3)))

assertEquivalent(EqualTo(caseWhen, Literal(4)), FalseLiteral)
assertEquivalent(EqualTo(caseWhen, Literal(3)), EqualTo(caseWhen, Literal(3)))
assertEquivalent(EqualTo(caseWhen, Literal("4")), FalseLiteral)
assertEquivalent(EqualTo(caseWhen, Literal("3")), EqualTo(caseWhen, Literal(3)))
assertEquivalent(
EqualTo(CaseWhen(Seq((a, Literal("1")), (c, Literal("2"))), None), Literal("4")),
FalseLiteral)

assertEquivalent(
And(EqualTo(caseWhen, Literal(5)), EqualTo(caseWhen, Literal(6))),
FalseLiteral)

assertEquivalent(
EqualTo(CaseWhen(Seq(normalBranch, (a, Literal(1)), (c, Literal(1))), None), Literal(-1)),
FalseLiteral)

// Do not simplify if it contains non foldable expressions.
assertEquivalent(EqualTo(caseWhen, NonFoldableLiteral(true)),
EqualTo(caseWhen, NonFoldableLiteral(true)))
val nonFoldable = CaseWhen(Seq(normalBranch, (a, b)), None)
assertEquivalent(EqualTo(nonFoldable, Literal(1)), EqualTo(nonFoldable, Literal(1)))

// Do not simplify if it contains non-deterministic expressions.
val nonDeterministic = CaseWhen(Seq((LessThan(Rand(1), Literal(0.5)), Literal(1))), Some(b))
assert(!nonDeterministic.deterministic)
assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1)))

// null check, SPARK-33798 will change the following two behaviors.
assertEquivalent(
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(2)),
FalseLiteral)
assertEquivalent(
EqualTo(CaseWhen(Seq((a, Literal(1))), Some(Literal(2))), Literal(null, IntegerType)),
FalseLiteral)
Copy link
Member Author

Choose a reason for hiding this comment

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

SPARK-33798 will change these behaviors as expected.


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)))
assertEquivalent(
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(null, IntegerType))),
Literal(1)),
Literal(null, BooleanType))
assertEquivalent(
EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(null, IntegerType))),
Literal(null, IntegerType)),
Literal(null, BooleanType))
Copy link
Member Author

Choose a reason for hiding this comment

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

SPARK-33798 will not change these behaviors.

}
}