dongjoon-hyun commented on a change in pull request #30865:
URL: https://github.com/apache/spark/pull/30865#discussion_r546477269



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
##########
@@ -570,6 +571,33 @@ object PushFoldableIntoBranches extends Rule[LogicalPlan] 
with PredicateHelper {
 }
 
 
+object SimplifyConditionalInPredicate extends Rule[LogicalPlan] {
+  def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+    case f @ Filter(cond, _) => f.copy(condition = simplifyConditional(cond))
+  }
+
+  private def simplifyConditional(e: Expression): Expression = e match {
+    case cw @ CaseWhen(branches, elseValue) if cw.dataType == BooleanType && 
branches.size == 1 &&
+        elseValue.forall(_.semanticEquals(FalseLiteral)) =>
+      val (whenVal, thenVal) = branches.head
+      And(whenVal, thenVal)
+    case i @ If(pred, trueVal, FalseLiteral) if i.dataType == BooleanType =>
+      And(pred, trueVal)
+    case e if e.dataType == BooleanType =>
+      e
+    case e =>
+      val message = "Expected a Boolean type expression in 
simplifyConditional, " +
+        s"but got the type `${e.dataType.catalogString}` in `${e.sql}`."
+      if (Utils.isTesting) {

Review comment:
       In this case, shall we raise exception always?




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