You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "ganeshashree (via GitHub)" <gi...@apache.org> on 2026/09/21 08:34:28 UTC
[PR] [WIP][SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in [spark]
ganeshashree opened a new pull request, #58938:
URL: https://github.com/apache/spark/pull/58938
### What changes were proposed in this pull request?
Since SPARK-59144, a clause-free SQL/JSON constructor call (`JSON_VALUE` / `JSON_QUERY` /
`JSON_EXISTS` / `JSON_ARRAY`) routes through function resolution, so a same-named routine on the
SQL PATH can shadow the built-in. However these built-ins' canonical `sql` omits default clauses, so
a call that is a built-in only because of a default clause renders as clause-free SQL; reparsing that
under a shadowing PATH binds the routine instead of the built-in.
This PR renders the default clause (`RETURNING STRING`, or `FALSE ON ERROR` for `JSON_EXISTS`) in
canonical `sql` so the rendering reparses back to the built-in. A shared `RoutedSqlJsonExpression`
trait lets `usePrettyExpression` keep rendering the clean clause-free form for auto-generated column
names (which are never reparsed), so display names stay unaffected.
### Why are the changes needed?
Without this, a canonical-SQL round-trip (e.g. persisting and reloading a view) can silently rebind a
built-in SQL/JSON constructor to a user routine that shadows it on the SQL PATH, changing results.
### Does this PR introduce _any_ user-facing change?
Yes, within unreleased master only. Canonical SQL of a clause-free SQL/JSON constructor now includes
its default clause. Auto-generated top-level column names are unchanged; a nested constructor's
generated name now shows `FORMAT JSON`, e.g. `JSON_ARRAY(JSON_ARRAY(1, 2) FORMAT JSON, 3)`.
### How was this patch tested?
New unit tests in `JsonValueSuite` / `JsonQuerySuite` / `JsonExistsSuite` / `JsonArraySuite`: each
asserts that a default-clause constructor's canonical SQL reparses to the built-in under a shadowing
PATH, and that its auto-generated column name stays clean. Golden files were regenerated.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)
This pull request and its description were written by Isaac.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #58938: [SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in
URL: https://github.com/apache/spark/pull/58938
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in [spark]
Posted by "ganeshashree (via GitHub)" <gi...@apache.org>.
ganeshashree commented on code in PR #58938:
URL: https://github.com/apache/spark/pull/58938#discussion_r4079247272
##########
sql/core/src/test/scala/org/apache/spark/sql/JsonValueSuite.scala:
##########
@@ -61,6 +61,48 @@ class JsonValueSuite extends QueryTest with SharedSparkSession {
}
}
+ test("SPARK-59685: default-clause JSON_VALUE canonical SQL reparses to the built-in under a " +
+ "shadowing PATH") {
+ withSQLConf(
+ SQLConf.PATH_ENABLED.key -> "true",
+ SQLConf.SESSION_FUNCTION_RESOLUTION_ORDER.key -> "second") {
+ try {
+ sql("CREATE TEMPORARY FUNCTION json_value(a STRING, b STRING) RETURNS STRING " +
+ "RETURN 'shadowed'")
+ sql("SET PATH = system.session, system.builtin")
+ // A built-in JSON_VALUE whose only clause is the default RETURNING STRING: the clause makes
+ // it the built-in even under the shadow, but its canonical `sql` would drop the default.
+ val jsonValue = sql(s"SELECT json_value('$doc', '$$.name' RETURNING STRING)")
+ .queryExecution.analyzed.expressions
+ .flatMap(_.collect { case jv: JsonValue => jv }).head
+ // The rendering must reparse back to the built-in, not the same-named routine on the PATH.
+ val reparsed = sql(s"SELECT ${jsonValue.sql}")
+ assert(reparsed.queryExecution.analyzed.expressions
+ .exists(_.exists(_.isInstanceOf[JsonValue])),
+ s"canonical SQL bound the shadow instead of the built-in: ${jsonValue.sql}")
+ checkAnswer(reparsed, Row("Ada"))
+ } finally {
+ sql("SET PATH = DEFAULT_PATH")
+ sql("DROP TEMPORARY FUNCTION IF EXISTS json_value")
+ }
+ }
+ }
+
+ test("SPARK-59685: a default JSON_VALUE keeps a clean auto-generated column name") {
Review Comment:
Done.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in [spark]
Posted by "ganeshashree (via GitHub)" <gi...@apache.org>.
ganeshashree commented on code in PR #58938:
URL: https://github.com/apache/spark/pull/58938#discussion_r4096079064
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -687,6 +687,20 @@ object JsonValueBehavior {
* }}}
*/
// scalastyle:on line.size.limit
+
+/**
Review Comment:
Done.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -844,7 +864,12 @@ case class JsonValue(
// Render the path as a properly escaped string literal so bracket-quoted paths such as
// `$['a']` (and any path containing a quote or backslash) round-trip as valid SQL.
val pathSQL = Literal(UTF8String.fromString(path), StringType).sql
- s"JSON_VALUE(${child.sql}, $pathSQL$returningSQL$emptySQL$errorSQL)"
+ // Append the default `RETURNING STRING` when the render is otherwise clause-free.
Review Comment:
Done.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58938:
URL: https://github.com/apache/spark/pull/58938#discussion_r4078907479
##########
sql/core/src/test/scala/org/apache/spark/sql/JsonValueSuite.scala:
##########
@@ -61,6 +61,48 @@ class JsonValueSuite extends QueryTest with SharedSparkSession {
}
}
+ test("SPARK-59685: default-clause JSON_VALUE canonical SQL reparses to the built-in under a " +
+ "shadowing PATH") {
+ withSQLConf(
+ SQLConf.PATH_ENABLED.key -> "true",
+ SQLConf.SESSION_FUNCTION_RESOLUTION_ORDER.key -> "second") {
+ try {
+ sql("CREATE TEMPORARY FUNCTION json_value(a STRING, b STRING) RETURNS STRING " +
+ "RETURN 'shadowed'")
+ sql("SET PATH = system.session, system.builtin")
+ // A built-in JSON_VALUE whose only clause is the default RETURNING STRING: the clause makes
+ // it the built-in even under the shadow, but its canonical `sql` would drop the default.
+ val jsonValue = sql(s"SELECT json_value('$doc', '$$.name' RETURNING STRING)")
+ .queryExecution.analyzed.expressions
+ .flatMap(_.collect { case jv: JsonValue => jv }).head
+ // The rendering must reparse back to the built-in, not the same-named routine on the PATH.
+ val reparsed = sql(s"SELECT ${jsonValue.sql}")
+ assert(reparsed.queryExecution.analyzed.expressions
+ .exists(_.exists(_.isInstanceOf[JsonValue])),
+ s"canonical SQL bound the shadow instead of the built-in: ${jsonValue.sql}")
+ checkAnswer(reparsed, Row("Ada"))
+ } finally {
+ sql("SET PATH = DEFAULT_PATH")
+ sql("DROP TEMPORARY FUNCTION IF EXISTS json_value")
+ }
+ }
+ }
+
+ test("SPARK-59685: a default JSON_VALUE keeps a clean auto-generated column name") {
Review Comment:
**Non-blocking (P2):** The new display-rendering path also applies `renderChild` to both `DEFAULT` expressions, but these generated-name tests exercise only the main JSON input. Could you add cases with a routed child such as `JSON_ARRAY(1)` in both the `ON EMPTY` and `ON ERROR` default positions? Otherwise reverting either branch to `default.get.sql` would leak the nested `RETURNING STRING` into the schema field name while all current assertions still pass.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in [spark]
Posted by "ganeshashree (via GitHub)" <gi...@apache.org>.
ganeshashree commented on code in PR #58938:
URL: https://github.com/apache/spark/pull/58938#discussion_r4089992852
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -687,6 +687,19 @@ object JsonValueBehavior {
* }}}
*/
// scalastyle:on line.size.limit
+
+/**
+ * A SQL/JSON function whose clause-free call `AstBuilder` routes through function resolution, so
Review Comment:
Done.
##########
sql/core/src/test/scala/org/apache/spark/sql/JsonExistsSuite.scala:
##########
@@ -61,6 +61,46 @@ class JsonExistsSuite extends QueryTest with SharedSparkSession {
}
}
+ test("SPARK-59685: default-clause JSON_EXISTS canonical SQL reparses to the built-in under a " +
+ "shadowing PATH") {
+ withSQLConf(
+ SQLConf.PATH_ENABLED.key -> "true",
+ SQLConf.SESSION_FUNCTION_RESOLUTION_ORDER.key -> "second") {
+ try {
+ sql("CREATE TEMPORARY FUNCTION json_exists(a STRING, b STRING) RETURNS BOOLEAN " +
+ "RETURN false")
+ sql("SET PATH = system.session, system.builtin")
+ // A built-in JSON_EXISTS whose only clause is the default FALSE ON ERROR: the clause makes
+ // it the built-in even under the shadow, but its canonical `sql` would drop the default.
+ val jsonExists = sql(s"SELECT json_exists('$doc', '$$.addr.city' FALSE ON ERROR)")
+ .queryExecution.analyzed.expressions
+ .flatMap(_.collect { case je: JsonExists => je }).head
+ // The rendering must reparse back to the built-in, not the same-named routine on the PATH.
+ val reparsed = sql(s"SELECT ${jsonExists.sql}")
+ assert(reparsed.queryExecution.analyzed.expressions
+ .exists(_.exists(_.isInstanceOf[JsonExists])),
+ s"canonical SQL bound the shadow instead of the built-in: ${jsonExists.sql}")
+ checkAnswer(reparsed, Row(true))
Review Comment:
Done.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58938:
URL: https://github.com/apache/spark/pull/58938#discussion_r4080429583
##########
sql/core/src/test/scala/org/apache/spark/sql/JsonExistsSuite.scala:
##########
@@ -61,6 +61,46 @@ class JsonExistsSuite extends QueryTest with SharedSparkSession {
}
}
+ test("SPARK-59685: default-clause JSON_EXISTS canonical SQL reparses to the built-in under a " +
+ "shadowing PATH") {
+ withSQLConf(
+ SQLConf.PATH_ENABLED.key -> "true",
+ SQLConf.SESSION_FUNCTION_RESOLUTION_ORDER.key -> "second") {
+ try {
+ sql("CREATE TEMPORARY FUNCTION json_exists(a STRING, b STRING) RETURNS BOOLEAN " +
+ "RETURN false")
+ sql("SET PATH = system.session, system.builtin")
+ // A built-in JSON_EXISTS whose only clause is the default FALSE ON ERROR: the clause makes
+ // it the built-in even under the shadow, but its canonical `sql` would drop the default.
+ val jsonExists = sql(s"SELECT json_exists('$doc', '$$.addr.city' FALSE ON ERROR)")
+ .queryExecution.analyzed.expressions
+ .flatMap(_.collect { case je: JsonExists => je }).head
+ // The rendering must reparse back to the built-in, not the same-named routine on the PATH.
+ val reparsed = sql(s"SELECT ${jsonExists.sql}")
+ assert(reparsed.queryExecution.analyzed.expressions
+ .exists(_.exists(_.isInstanceOf[JsonExists])),
+ s"canonical SQL bound the shadow instead of the built-in: ${jsonExists.sql}")
+ checkAnswer(reparsed, Row(true))
Review Comment:
**Non-blocking (P2):** This valid-input Row(true) result is the same for FALSE, TRUE, UNKNOWN, and ERROR ON ERROR, while the expression-type check only proves built-in binding. Could this round-trip test also evaluate malformed JSON (or otherwise assert the emitted mode) so it fails if the canonical renderer changes from the default FALSE ON ERROR?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -687,6 +687,19 @@ object JsonValueBehavior {
* }}}
*/
// scalastyle:on line.size.limit
+
+/**
+ * A SQL/JSON function whose clause-free call `AstBuilder` routes through function resolution, so
Review Comment:
**Nit (P3):** This contract is conditional: AstBuilder directly constructs clause-free JsonQuery and JsonArray expressions when they are top-level JSON_ARRAY elements, so not every implementation occurrence routes through routine resolution. The related JsonArraySuite comment also says every asserted rendering carries RETURNING STRING, but splicedOmit has no RETURNING clause, and the clause is binding-relevant under a shadowing PATH. Could you narrow both comments to the contexts and renderings where the ownership clause actually applies?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58938:
URL: https://github.com/apache/spark/pull/58938#discussion_r4095835163
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -687,6 +687,20 @@ object JsonValueBehavior {
* }}}
*/
// scalastyle:on line.size.limit
+
+/**
Review Comment:
**Nit (P3):** Placing this trait here separates the preceding JSON_VALUE Scaladoc from JsonValue, so that detailed block no longer documents the case class it describes. Please move the trait above the JSON_VALUE Scaladoc, or elsewhere in its logical section, so the Scaladoc directly precedes JsonValue again.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -844,7 +864,12 @@ case class JsonValue(
// Render the path as a properly escaped string literal so bracket-quoted paths such as
// `$['a']` (and any path containing a quote or backslash) round-trip as valid SQL.
val pathSQL = Literal(UTF8String.fromString(path), StringType).sql
- s"JSON_VALUE(${child.sql}, $pathSQL$returningSQL$emptySQL$errorSQL)"
+ // Append the default `RETURNING STRING` when the render is otherwise clause-free.
Review Comment:
**Non-blocking (P2):** The new gate has independent suppression conditions for RETURNING, ON EMPTY, and ON ERROR, and JSON_EXISTS has its own ON ERROR gate, but these tests exercise only the clause-free append path. Please add render-and-reparse cases in JsonValueSuite and JsonExistsSuite with those explicit clauses so a regression that appends a duplicate or conflicting default clause fails.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59685][SQL] Keep clause-free SQL/JSON constructor canonical SQL bound to the built-in [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #58938:
URL: https://github.com/apache/spark/pull/58938#issuecomment-5829875590
merged to master/4.x
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org