Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-33798][SQL] Add new rule to push down the foldable expressions through CaseWhen/If #30790

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ 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(!EqualTo(_, equalTo).eval(EmptyRow).asInstanceOf[Boolean])
}

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

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

case EqualTo(c @ CaseWhen(branches, elseValue), right: Literal) if c.deterministic &&
(branches.map(_._2) ++ elseValue).forall(_.isInstanceOf[Literal]) &&
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 do the "is literal check" inside isAlwaysFalse, to shorten the code.

isAlwaysFalse(branches.map(_._2) ++ elseValue, right) =>
FalseLiteral
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,97 @@ 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 not change these behaviors.
assertEquivalent(
EqualTo(If(FalseLiteral, Literal(null, IntegerType), 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.

The if condition is a literal, does it really go through the new branch? will it be optimized earlier by some other logics?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer to still use a === Literal(1) as the if conditionn, and make sure the expression is not optimized if there are null values.

TrueLiteral)
assertEquivalent(
EqualTo(If(TrueLiteral, Literal(null, IntegerType), Literal(1)), Literal(1)),
Literal(null, BooleanType))
assertEquivalent(
EqualTo(If(FalseLiteral, Literal(null, IntegerType), Literal(null, IntegerType)), Literal(1)),
Literal(null, BooleanType))

assertEquivalent(
EqualTo(If(FalseLiteral, Literal(1), Literal(2)), Literal(null, IntegerType)),
Literal(null, BooleanType))
assertEquivalent(
EqualTo(If(TrueLiteral, Literal(1), Literal(2)), Literal(null, IntegerType)),
Literal(null, BooleanType))
wangyum marked this conversation as resolved.
Show resolved Hide resolved
}

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.

}
}