ganeshashree opened a new pull request, #60303:
URL: https://github.com/apache/spark/pull/60303

   ### What changes were proposed in this pull request?
   
   A follow-up to #59828. That PR lets a JSON_ARRAY / JSON_OBJECT argument 
splice when it is an `Alias` over a JSON producer 
(`ImplicitlyFormattedAsJson.splicesThroughCollationCast` sees through `Alias`), 
but the SQL rendering of `JsonArray` and `JsonObjectExpr` still rendered the 
alias and then appended `FORMAT JSON`, e.g. `JSON_ARRAY(JSON_ARRAY(1) AS x 
FORMAT JSON)`.
   
   This PR adds `ImplicitlyFormattedAsJson.stripAliases`, which drops those 
aliases (directly or under a `Collate`), and uses it in `JsonArray.sqlString` 
and `JsonObjectExpr.sqlString` for spliced values only. Quoted values still 
render through `Alias.sql` as before. `JsonArray` also uses the stripped values 
when deciding whether to add the default `RETURNING STRING` that keeps a 
reparsed call bound to the built-in; otherwise an aliased child made it look as 
if a `FORMAT JSON` had been rendered and the clause was dropped.
   
   ### Why are the changes needed?
   
   The rendered text is not valid SQL, and the same rendering builds 
auto-generated column names. For example, before this PR
   
   ```scala
   spark.range(1).select(call_function("json_array", 
expr("json_array(1)").as("x")))
   ```
   
   got a column name containing `AS x FORMAT JSON`. Nested aliases are removed 
at the end of analysis, so analyzed plans (and the SQL text stored for views) 
are not affected, and query results were always correct.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, but only relative to the unreleased master / branch-4.x after #59828: 
the auto-generated column name above is now `JSON_ARRAY(JSON_ARRAY(1))`. Query 
results do not change. There is no change compared to released Spark versions.
   
   ### How was this patch tested?
   
   New tests in `JsonArraySuite` and `JsonObjectSuite`:
   - canonical SQL of an aliased nested constructor and of an aliased `FORMAT 
JSON` literal, reparsed and evaluated, and of an aliased nested constructor 
under `COLLATE`;
   - the auto-generated column name of a DataFrame call that passes an aliased 
nested constructor;
   - a user `CAST(... AS STRING)` under an alias still quotes.
   
   Without the change, the new tests fail with `AS x FORMAT JSON` in the 
rendered SQL; without only the `Collate` case, the `COLLATE` assertions fail. 
`JsonArraySuite`, `JsonObjectSuite`, `JsonQuerySuite`, 
`SparkSessionExtensionSuite`, `JsonExpressionsSuite` and scalastyle pass.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Opus 5.5)
   
   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: [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