ganeshashree opened a new pull request, #57888:
URL: https://github.com/apache/spark/pull/57888
### What changes were proposed in this pull request?
Add the ANSI SQL:2016 `JSON_VALUE` scalar function (feature T821):
```
JSON_VALUE(jsonExpr, path
[ RETURNING <type> ]
[ (NULL | ERROR | DEFAULT expr) ON EMPTY ]
[ (NULL | ERROR | DEFAULT expr) ON ERROR ])
```
It extracts the scalar at `path`, cast to the RETURNING type (default
STRING). A missing path fires ON EMPTY; malformed JSON, a non-scalar match, or
a failed cast fires ON ERROR; both default to NULL. An explicit JSON null
returns SQL NULL, and a SQL NULL input propagates to NULL.
Grammar adds non-reserved keywords `JSON_VALUE`, `RETURNING`, `EMPTY` and a
dedicated production, mirroring `JSON_TABLE` (SPARK-58366). Evaluation reuses
JSON_TABLE's token-aware path evaluator via a new `JsonTableEvaluator.lookup`
entry point that preserves the missing / JSON-null / found distinction. The
extracted-scalar cast is always ANSI (throwing) so ON ERROR governs a failed
conversion regardless of `spark.sql.ansi.enabled`.
v1 supports STRING input and scalar (string/numeric/boolean/datetime)
RETURNING types per ANSI 9075-2 6.28; VARIANT RETURNING and PASSING are
deferred.
### Why are the changes needed?
Spark implements none of the ANSI scalar JSON functions. `JSON_VALUE` is
near-universal (Oracle, SQL Server, PostgreSQL, Trino, BigQuery, ...), so its
absence forces migrated queries to rewrite to `CAST(get_json_object(...))`,
which is STRING-only, cannot express RETURNING, and conflates missing vs.
error.
### Does this PR introduce _any_ user-facing change?
Yes. It adds the new `JSON_VALUE` SQL function. The new keywords are
non-reserved in both ANSI and default modes, so existing identifiers named
`json_value` /`returning` / `empty` continue to work.
### How was this patch tested?
New `JsonValueSuite` (both ANSI modes), `json-functions.sql` golden files,
`ExpressionParserSuite`, `SQLKeywordSuite`, and `SparkThrowableSuite` for the
new error conditions.
### Was this patch authored or co-authored using generative AI tooling?
Yes, Claude Opus 4.8.
--
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]