peter-toth commented on code in PR #60394:
URL: https://github.com/apache/spark/pull/60394#discussion_r4554549338


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ExprUtils.scala:
##########
@@ -95,6 +97,23 @@ object ExprUtils extends EvalHelper with QueryErrorsBase {
       throw QueryCompilationErrors.nonMapFunctionNotAllowedError()
   }
 
+  /**
+   * Returns the value, possibly null, of a foldable expression, or None if 
the expression is not
+   * foldable or fails to evaluate. `ConstantFolding` keeps a foldable 
expression that fails to
+   * evaluate in a conditional branch, which no row may reach. An expression 
that uses the value
+   * of such a child once, e.g. in generated code, should evaluate the child 
for each row instead,
+   * so that the error is raised only when a row evaluates it, as in `eval`.
+   */
+  def tryEvalFoldable(exp: Expression): Option[Any] = if (exp.foldable) {

Review Comment:
   **Finding 1.** `to_time` has the same problem, but not in code generation. 
`ToTime.replacement` evaluates a foldable format right away 
(`timeExpressions.scala:97-101`), so a format that fails to evaluate fails the 
query even when no row reaches it.
   
   What I ran, with ANSI on, on the view from the description:
   
   ```sql
   SELECT k, IF(k > 0, NULL, to_time('10:00', CAST(10 / 0 AS STRING))) FROM o
   ```
   
   It fails with `DIVIDE_BY_ZERO` on master and here, with whole-stage codegen 
on and off, and with the codegen fallback on and off. With this helper in the 
guard, it returns `[1,null]` and `[2,null]` in all four settings, and 
`TimeExpressionsSuite` passes. The other case already passes the format to the 
parser for each row:
   
   ```scala
       case Some(expr) if ExprUtils.tryEvalFoldable(expr).isDefined =>
   ```
   
   It could also be a follow-up.
   



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

Reply via email to