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



##########
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))

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




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