cloud-fan commented on a change in pull request #30790:
URL: https://github.com/apache/spark/pull/30790#discussion_r544346802
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/SimplifyConditionalSuite.scala
##########
@@ -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 b = UnresolvedAttribute("b")
+ val ifExp = If(a, Literal(2), Literal(3))
+
+ assertEquivalent(EqualTo(ifExp, Literal(4)), FalseLiteral)
+ assertEquivalent(EqualTo(ifExp, Literal(3)), EqualTo(ifExp, Literal(3)))
+ assertEquivalent(EqualTo(ifExp, Literal("4")), FalseLiteral)
+ assertEquivalent(EqualTo(ifExp, Literal("3")), EqualTo(ifExp, Literal(3)))
+
+ // Do not simplify if it contains non foldable expressions.
+ assertEquivalent(
+ EqualTo(If(a, b, Literal(2)), Literal(3)),
+ EqualTo(If(a, b, Literal(2)), Literal(3)))
+ val nonFoldable = If(NonFoldableLiteral(true), Literal(1), Literal(2))
+ 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))
+ assert(!nonDeterministic.deterministic)
+ assertEquivalent(EqualTo(nonDeterministic, Literal(-1)),
EqualTo(nonDeterministic, Literal(-1)))
+
+ // Should not handle Null values.
+ assertEquivalent(
+ EqualTo(If(a, Literal(null, IntegerType), Literal(1)), Literal(2)),
+ EqualTo(If(a, Literal(null, IntegerType), Literal(1)), Literal(2)))
+ assertEquivalent(
+ EqualTo(If(!a, Literal(1), Literal(2)), Literal(null, IntegerType)),
Review comment:
as a unit test, we know that we only care if the condition is foldable
or not. I think we don't need to test `!a` as it doesn't improve test coverage.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]