This is an automated email from the ASF dual-hosted git repository.

dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new a6f7be7a1184 [SPARK-61575][SQL] Fix `to_time` failing on a foldable 
format that no row evaluates
a6f7be7a1184 is described below

commit a6f7be7a1184eef575413ad7ec73b7a5f7401857
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Thu Nov 12 19:46:38 2026 -0800

    [SPARK-61575][SQL] Fix `to_time` failing on a foldable format that no row 
evaluates
    
    ### 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
    
    Closes #60419 from dongjoon-hyun/SPARK-61575.
    
    Authored-by: Dongjoon Hyun <[email protected]>
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    (cherry picked from commit ab8a264ed93afe403c7445c5806c63753c2cb22b)
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    
    skip-checks: true
---
 .../spark/sql/catalyst/expressions/timeExpressions.scala  | 11 ++++++-----
 .../sql/catalyst/expressions/TimeExpressionsSuite.scala   | 15 +++++++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/timeExpressions.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/timeExpressions.scala
index dc91dd672a36..47ebb97d694f 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/timeExpressions.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/timeExpressions.scala
@@ -93,13 +93,14 @@ case class ToTime(str: Expression, format: 
Option[Expression])
       methodInputTypes = arguments.map(_.dataType))
   }
 
-  override lazy val replacement: Expression = format match {
-    case None => invokeParser()
-    case Some(expr) if expr.foldable =>
-      Option(expr.eval())
+  // A foldable format that fails to evaluate is passed to the parser for each 
row instead, so
+  // that only a row that evaluates the format fails.
+  override lazy val replacement: Expression = 
format.flatMap(ExprUtils.tryEvalFoldable) match {
+    case Some(fmt) =>
+      Option(fmt)
         .map(f => invokeParser(Some(f.toString), Seq(str)))
         .getOrElse(Literal(null, TimeType()))
-    case _ => invokeParser()
+    case None => invokeParser()
   }
 
   override def inputTypes: Seq[AbstractDataType] = {
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TimeExpressionsSuite.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TimeExpressionsSuite.scala
index f8f34ae9d2c7..449d28864710 100644
--- 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TimeExpressionsSuite.scala
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TimeExpressionsSuite.scala
@@ -25,6 +25,7 @@ import 
org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{DataTypeMismatch,
 import org.apache.spark.sql.catalyst.expressions.Cast.{toSQLId, toSQLValue}
 import org.apache.spark.sql.catalyst.util.DateTimeTestUtils._
 import org.apache.spark.sql.catalyst.util.SparkDateTimeUtils.localTimeToNanos
+import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.sql.types.{DayTimeIntervalType, Decimal, DecimalType, 
IntegerType, LongType, StringType, TimeType}
 import org.apache.spark.sql.types.DayTimeIntervalType.{DAY, HOUR, SECOND}
 
@@ -73,6 +74,20 @@ class TimeExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper {
     assert(new ToTime(Literal("00:00:00"), Literal("HH:mm:ss")).dataType === 
TimeType())
   }
 
+  test("SPARK-61575: to_time with a foldable format that fails to evaluate") {
+    // `ConstantFolding` leaves such a format in a conditional branch. Only a 
row that
+    // evaluates the format should fail.
+    withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+      val str = BoundReference(0, StringType, nullable = true)
+      val expr = If(
+        IsNull(str),
+        Literal.create(null, TimeType()),
+        ToTime(str, Some(Cast(Divide(Literal(1.0), Literal(0.0)), 
StringType))))
+      checkEvaluation(expr, null, create_row(null))
+      checkExceptionInExpression[ArithmeticException](expr, 
create_row("10:00"), "DIVIDE_BY_ZERO")
+    }
+  }
+
   test("HourExpressionBuilder") {
     // Empty expressions list
     checkError(


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to