cloud-fan commented on code in PR #36468:
URL: https://github.com/apache/spark/pull/36468#discussion_r868771361
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -52,22 +56,42 @@ object ConstantFolding extends Rule[LogicalPlan] {
case _ => false
}
+ private def constantFolding(
+ e: Expression,
+ isConditionalBranch: Boolean = false): Expression = e match {
+ case c: ConditionalExpression if !c.foldable =>
+ c.mapChildren(constantFolding(_, isConditionalBranch = true))
+
+ // Skip redundant folding of literals. This rule is technically not
necessary. Placing this
+ // here avoids running the next rule for Literal values, which would
create a new Literal
+ // object and running eval unnecessarily.
+ case l: Literal => l
+
+ case Size(c: CreateArray, _) if c.children.forall(hasNoSideEffect) =>
+ Literal(c.children.length)
+ case Size(c: CreateMap, _) if c.children.forall(hasNoSideEffect) =>
+ Literal(c.children.length / 2)
+
+ case e if e.getTagValue(FAILED_TO_EVALUATE).getOrElse(false) => e
+
+ // Fold expressions that are foldable.
+ case e if e.foldable =>
+ try {
+ Literal.create(e.eval(EmptyRow), e.dataType)
+ } catch {
+ case NonFatal(_) if isConditionalBranch =>
+ // Fold the children expression inside a conditional expression
which is not foldable.
+ // Some branches may not be evaluated at runtime, so here we should
in case the exception
+ // and leave it to runtime.
Review Comment:
```
When doing constant folding inside conditional expressions, we should not
fail
during expression evaluation, as the branch we are evaluating may not be
reached at
runtime, and we shouldn't fail the query, to match the original behavior.
```
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]