dongjoon-hyun opened a new pull request, #60419:
URL: https://github.com/apache/spark/pull/60419

   ### What changes were proposed in this pull request?
   
   This PR aims to fix `to_time` to fail only when a row evaluates a foldable 
format that fails to evaluate, instead of failing the whole query during 
planning.
   
   `ToTime.replacement` evaluates a foldable format eagerly to build the parser 
once. Since `RuntimeReplaceable.dataType` reads `replacement`, this happens 
during analysis. However, since SPARK-39106 (Apache Spark 3.3.0), 
`ConstantFolding` keeps a foldable expression that fails to evaluate in a 
conditional branch, because no row may reach that branch.
   
   With this PR, `ToTime.replacement` uses `ExprUtils.tryEvalFoldable`, which 
SPARK-61552 added. When the foldable format fails to evaluate, the format is 
passed to the parser for each row, which is the existing path for a 
non-foldable format.
   
   ### Why are the changes needed?
   
   `to_time` fails even when no row evaluates the format. This happens with 
whole-stage codegen on and off, in Apache Spark 4.1.0+, which introduced 
`to_time`.
   
   ```sql
   SET spark.sql.ansi.enabled=true;
   SET spark.sql.timeType.enabled=true;
   CREATE OR REPLACE TEMPORARY VIEW o AS SELECT id AS k FROM range(1, 3);
   SELECT k, IF(k > 0, NULL, to_time('10:00', CAST(10 / 0 AS STRING))) FROM o;
   ```
   
   **BEFORE**
   
   ```
   [DIVIDE_BY_ZERO] Division by zero. Use `try_divide` to tolerate divisor 
being 0 and return NULL instead. If necessary set "spark.sql.ansi.enabled" to 
"false" to bypass this error. SQLSTATE: 22012
   ```
   
   **AFTER**
   
   ```
   1    NULL
   2    NULL
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, this fixes a bug. `to_time` and `try_to_time` no longer fail during 
query planning when a foldable format fails to evaluate.
   - `to_time` fails only when a row evaluates the format. A foldable format 
outside a conditional branch still fails in `ConstantFolding`, like any other 
foldable expression that fails to evaluate.
   - `try_to_time` returns `NULL`, as it already does when the input string 
fails to evaluate.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   


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