cloud-fan commented on a change in pull request #30790:
URL: https://github.com/apache/spark/pull/30790#discussion_r543878249



##########
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 ifExp = If(a === Literal(1), 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(ifExp, NonFoldableLiteral(true)),
+      EqualTo(ifExp, NonFoldableLiteral(true)))
+    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)))
+
+    // null check, SPARK-33798 will not change these behaviors.
+    assertEquivalent(
+      EqualTo(If(FalseLiteral, Literal(null, IntegerType), Literal(1)), 
Literal(1)),

Review comment:
       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.




----------------------------------------------------------------
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]

Reply via email to