cloud-fan commented on code in PR #60303:
URL: https://github.com/apache/spark/pull/60303#discussion_r4533806150
##########
sql/core/src/test/scala/org/apache/spark/sql/JsonArraySuite.scala:
##########
@@ -386,6 +387,32 @@ class JsonArraySuite extends QueryTest with
SharedSparkSession {
"JSON_ARRAY(CAST(JSON_ARRAY(1 RETURNING STRING) AS STRING) RETURNING
STRING)")
}
+ test("SPARK-59243: SQL of a spliced aliased child drops the alias") {
+ val inner = JsonArray(
+ Seq(Literal(1)), Seq(false), Seq(false),
JsonConstructorNullBehavior.Absent, StringType)
+ val aliased = JsonArray(
+ Seq(Alias(inner, "x")()), Seq(true), Seq(false),
JsonConstructorNullBehavior.Absent,
+ StringType)
+ assert(aliased.sql == "JSON_ARRAY(JSON_ARRAY(1 RETURNING STRING) RETURNING
STRING)")
+ checkAnswer(sql(s"SELECT ${aliased.sql}"), Row("[[1]]"))
+ val literal = JsonArray(
+ Seq(Alias(Literal("[1]"), "x")()), Seq(true), Seq(true),
JsonConstructorNullBehavior.Absent,
+ StringType)
+ assert(literal.sql == "JSON_ARRAY('[1]' FORMAT JSON)")
+ val collated = JsonArray(
+ Seq(Collate(Alias(inner, "x")(), ResolvedCollation("UTF8_LCASE"))),
Seq(true), Seq(false),
+ JsonConstructorNullBehavior.Absent, StringType)
+ assert(collated.sql ==
+ "JSON_ARRAY(collate(JSON_ARRAY(1 RETURNING STRING), UTF8_LCASE) FORMAT
JSON)")
+ // The column name drops the alias too; a user cast still quotes and keeps
it.
+ val df = spark.range(1).select(call_function("json_array",
expr("json_array(1)").as("x")))
+ assert(df.columns.toSeq == Seq("JSON_ARRAY(JSON_ARRAY(1))"))
+ checkAnswer(
Review Comment:
**Nit (P3):** The comment above says a user cast "still quotes and keeps
it", but this sub-case, like the matching one in `JsonObjectSuite` (lines
726-729), only runs `checkAnswer`. That row comes from the splice flag the
builder freezes at construction, not from `sqlString`, so it never reaches the
`if (isJson)` / `if (raw)` gate in `jsonExpressions.scala`. As I read the code,
if that gate were dropped so that aliases were stripped from every value,
quoted aliased arguments would lose their `AS x` in auto-generated column
names, unlike aliased arguments of other functions, and nothing in either suite
would fail. Query results would not change; only column names and error text
that embeds the expression would.
Could you also assert the column name here? From `usePrettyExpression` and
`Alias.sql`, I'd expect:
```scala
val quoted = spark.range(1).select(
call_function("json_array", expr("CAST(json_array(1) AS STRING)").as("x")))
assert(quoted.columns.toSeq == Seq("JSON_ARRAY(CAST(JSON_ARRAY(1) AS STRING)
AS x)"))
checkAnswer(quoted, Row("""["[1]"]"""))
```
In `JsonObjectSuite`, a check that the user-cast column name contains `" AS
x"` would do the same.
**Verification:**
- **Regression:** Remove the spliced-only condition from JSON_ARRAY's and
JSON_OBJECT's SQL rendering, so that aliases are stripped from every value. The
user-cast case in each constructor's test then fails on its column-name
assertion; with the condition restored, it passes.
- **Behavior:** With the PR's rendering unchanged, the user-cast case in
each constructor's test still returns the quoted JSON string, and its
auto-generated column name keeps the `AS x` alias.
--
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]