You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2020/12/17 18:25:46 UTC

[GitHub] [spark] cloud-fan commented on a change in pull request #30790: [SPARK-33798][SQL] Add new rule to push down the foldable expressions through CaseWhen/If

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



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
##########
@@ -528,6 +528,28 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
 }
 
 
+/**
+ * Push the foldable expression into (if / case) branches.
+ */
+object PushFoldableIntoBranches extends Rule[LogicalPlan] with PredicateHelper {
+  def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+    case q: LogicalPlan => q transformExpressionsUp {
+      case b @ BinaryComparison(i @ If(_, trueValue, falseValue), right) if i.deterministic &&
+          right.foldable && trueValue.foldable && falseValue.foldable =>
+        i.copy(
+          trueValue = b.makeCopy(Array(trueValue, right)),
+          falseValue = b.makeCopy(Array(falseValue, right)))
+
+      case b @ BinaryComparison(c @ CaseWhen(branches, elseValue), right) if c.deterministic &&
+          right.foldable && (branches.map(_._2) ++ elseValue).forall(_.foldable) =>

Review comment:
       some more ideas:
   1. I think it's helpful to pushdown foldables into branches as long as there is at lease one foldable branche. To be conservative, we can do the pushdown if the IF/CASE WHEN has at most one non-foldable branch, so that we never increase the expression tree size, so is the generated code size.
   2.  I think it's also useful to pushdown expressions like Add, e.g. `IF(cond, 1, 2) +1` -> `IF(cond, 2, 3)`. We can use `BinnaryExpression` instead of `BinaryComparison`.




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org