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

cloud-fan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 3c648daf5efc [SPARK-59244][SQL] Emit a clean analysis error for a 
throwing foldable JSON path
3c648daf5efc is described below

commit 3c648daf5efc196002554cbd7b996b33f339eeaa
Author: Ganesha S <[email protected]>
AuthorDate: Fri Nov 13 14:47:01 2026 +0000

    [SPARK-59244][SQL] Emit a clean analysis error for a throwing foldable JSON 
path
    
    ### What changes were proposed in this pull request?
    
    `json_value`, `json_query` and `json_exists` evaluate their `path` argument 
during analysis. A path can be a constant expression that still fails when 
evaluated, for example:
    
    ```sql
    SELECT json_value('{}', element_at(array('$.a'), 0));
    ```
    
    Before this change, the runtime error from evaluating the path (here 
`INVALID_INDEX_OF_ZERO`) leaked out of analysis as-is. Now it is reported as an 
invalid `path` argument:
    
    ```
    [INVALID_PARAMETER_VALUE.EVALUATION_ERROR] The value of parameter(s) `path` 
in `json_value` is invalid:
    the expression "element_at(array($.a), 0)" could not be evaluated.
    ```
    
    The original error is kept as the cause.
    
    ### Why are the changes needed?
    
    The error should name the argument that is wrong and come from analysis, 
like the other path-argument errors these functions already raise (non-foldable 
path, NULL path, wrong type).
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes, the error class for this case changes as described above. These 
functions are not released yet, so this does not affect any released Spark 
version.
    
    ### How was this patch tested?
    
    Added a test in `JsonValueSuite` covering all three functions, called both 
unqualified and qualified.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Claude Opus 5.5)
    
    Closes #60335 from ganeshashree/SPARK-59244.
    
    Lead-authored-by: Ganesha S <[email protected]>
    Co-authored-by: Ganesha S <[email protected]>
    Signed-off-by: Wenchen Fan <[email protected]>
    
    
    skip-checks: true
---
 .../src/main/resources/error/error-conditions.json |  5 +++++
 .../sql/catalyst/expressions/jsonExpressions.scala | 14 ++++++++++---
 .../spark/sql/errors/QueryCompilationErrors.scala  | 14 +++++++++++++
 .../org/apache/spark/sql/JsonValueSuite.scala      | 24 +++++++++++++++++++++-
 4 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/common/utils/src/main/resources/error/error-conditions.json 
b/common/utils/src/main/resources/error/error-conditions.json
index cc585d421e51..da1ce39af833 100644
--- a/common/utils/src/main/resources/error/error-conditions.json
+++ b/common/utils/src/main/resources/error/error-conditions.json
@@ -5275,6 +5275,11 @@
           "Unsupported dtype: <invalidValue>. Valid values: float64, float32."
         ]
       },
+      "EVALUATION_ERROR" : {
+        "message" : [
+          "the expression <invalidValue> could not be evaluated."
+        ]
+      },
       "EXTENSION" : {
         "message" : [
           "Invalid extension: <invalidValue>. Extension must be non-empty and 
contain only letters."
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
index 6d04e45baec3..1c3297630511 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
@@ -17,6 +17,8 @@
 
 package org.apache.spark.sql.catalyst.expressions
 
+import scala.util.control.NonFatal
+
 import com.fasterxml.jackson.core.{JsonFactory, JsonProcessingException}
 
 import org.apache.spark.SparkException
@@ -2023,9 +2025,15 @@ abstract class JsonPathExpressionBuilder extends 
ExpressionBuilder {
     val pathExpr = expressions(1)
     pathExpr.dataType match {
       case _: StringType if pathExpr.foldable =>
-        // TODO(SPARK-59244): wrap eval() so a foldable-but-throwing path 
re-throws as a clean
-        // invalid-argument analysis error naming `path`.
-        val pathValue = pathExpr.eval()
+        // A foldable path can still fail to evaluate (e.g. 
`element_at(array('$.a'), 0)`); report
+        // it as an invalid `path` argument, keeping the runtime error as the 
cause.
+        val pathValue =
+          try pathExpr.eval()
+          catch {
+            case NonFatal(e) =>
+              throw QueryCompilationErrors.cannotEvaluateParameterError(
+                funcName, "path", pathExpr, e)
+          }
         if (pathValue == null) {
           throw QueryCompilationErrors.unexpectedNullError("path", pathExpr)
         }
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
index f4d5f4556285..f375dc580e8d 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
@@ -259,6 +259,20 @@ private[sql] object QueryCompilationErrors extends 
QueryErrorsBase with Compilat
     invalidParameter("PARSE_SQL_MODE", "parse_sql", "mode", mode)
   }
 
+  def cannotEvaluateParameterError(
+      functionName: String,
+      parameter: String,
+      invalidValue: Expression,
+      cause: Throwable): Throwable = {
+    new AnalysisException(
+      errorClass = "INVALID_PARAMETER_VALUE.EVALUATION_ERROR",
+      messageParameters = Map(
+        "functionName" -> toSQLId(functionName),
+        "parameter" -> toSQLId(parameter),
+        "invalidValue" -> toSQLExpr(invalidValue)),
+      cause = Some(cause))
+  }
+
   def unorderablePivotColError(pivotCol: Expression): Throwable = {
     new AnalysisException(
       errorClass = "INCOMPARABLE_PIVOT_COLUMN",
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/JsonValueSuite.scala 
b/sql/core/src/test/scala/org/apache/spark/sql/JsonValueSuite.scala
index 60512f6a3166..ad1bcb0105e0 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/JsonValueSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/JsonValueSuite.scala
@@ -17,7 +17,7 @@
 
 package org.apache.spark.sql
 
-import org.apache.spark.SparkRuntimeException
+import org.apache.spark.{SparkRuntimeException, SparkThrowable}
 import org.apache.spark.sql.catalyst.expressions.JsonValue
 import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.sql.test.SharedSparkSession
@@ -380,6 +380,28 @@ class JsonValueSuite extends QueryTest with 
SharedSparkSession {
     }
   }
 
+  test("invalid: a foldable path that throws when evaluated is a clean 
invalid-argument error") {
+    // A foldable path whose evaluation always fails: index 0 is invalid for 
element_at.
+    for {
+      func <- Seq("json_value", "json_query", "json_exists")
+      prefix <- Seq("", "builtin.", "system.builtin.")
+    } {
+      val call = s"$prefix$func('{}', element_at(array('$$.a'), 0))"
+      val e = intercept[AnalysisException] {
+        sql(s"SELECT $call").collect()
+      }
+      checkError(
+        exception = e,
+        condition = "INVALID_PARAMETER_VALUE.EVALUATION_ERROR",
+        parameters = Map(
+          "functionName" -> s"`$func`",
+          "parameter" -> "`path`",
+          "invalidValue" -> "\"element_at(array($.a), 0)\""),
+        context = ExpectedContext(fragment = call, start = 7, stop = 6 + 
call.length))
+      assert(e.getCause.asInstanceOf[SparkThrowable].getCondition == 
"INVALID_INDEX_OF_ZERO")
+    }
+  }
+
   test("invalid: plain SQL/JSON path functions require exactly two arguments") 
{
     // A call that does not match the SQL/JSON grammar (wrong arity) is parsed 
as a plain function
     // call and reaches the registered built-in, which requires exactly the 
(jsonStr, path) pair.


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

Reply via email to