cloud-fan commented on code in PR #60336:
URL: https://github.com/apache/spark/pull/60336#discussion_r4564753489


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -1715,22 +1715,17 @@ case class JsonArray(
     // `RETURNING STRING`, which `DataTypeAstBuilder` maps to the companion. 
(A non-default
     // collation is not `==` to the companion anyway, since StringType.equals 
compares constraint.)
     val returningSQL = if (returning.eq(StringType)) "" else s" RETURNING 
${returning.sql}"
+    // The non-default `NULL ON NULL` always renders. Canonical `sql` 
(`forceBuiltinOwnership`) also
+    // appends the default `ABSENT ON NULL` so the reparse stays on the direct 
grammar path instead
+    // of routing to a shadowing routine; the pretty form omits it to keep 
column names clean.
+    // Mirrors `JsonObjectExpr`.
     val nullSQL = nullBehavior match {
       case JsonConstructorNullBehavior.Null => " NULL ON NULL"
-      case JsonConstructorNullBehavior.Absent => ""
+      case JsonConstructorNullBehavior.Absent =>
+        if (forceBuiltinOwnership) " ABSENT ON NULL" else ""
     }
-    // Add `RETURNING STRING` so a clause-free render still means the built-in 
when reparsed (a
-    // clause-free call, even a nested one, could bind a shadow). A rendered 
FORMAT JSON, null, or
-    // RETURNING clause already does that.
-    val rendersFormatJson = rendered.exists {
-      case (v, isJson) => isJson && !directlyImplicit(v)
-    }
-    val ownershipClause =
-      if (forceBuiltinOwnership && !rendersFormatJson && nullSQL.isEmpty &&
-          returningSQL.isEmpty) {
-        " RETURNING STRING"
-      } else ""
-    s"JSON_ARRAY($valuesSQL$nullSQL$returningSQL$ownershipClause)"
+    // `trim` drops the leading space when only a clause renders 
(`JSON_ARRAY(ABSENT ON NULL)`).
+    s"JSON_ARRAY(${s"$valuesSQL$nullSQL$returningSQL".trim})"

Review Comment:
   **Non-blocking (P2):** `String.trim` runs on the whole text inside the 
parentheses, not just the separator, so in the display form 
(`forceBuiltinOwnership = false`) it also strips whitespace that belongs to the 
values. `usePrettyExpression` uses that form for auto-generated column names 
and for `toSQLExpr` error text. There a string literal renders unquoted 
(`PrettyAttribute(s.toString)`, 
`sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/package.scala:102`),
 an attribute renders its raw name, and no default clause follows the last 
value.
   
   Reading the code, `SELECT json_array(' a')` is now named `JSON_ARRAY(a)` 
instead of the base branch's `JSON_ARRAY( a)`. `json_array('a ')` becomes 
`JSON_ARRAY(a)`, the same name `json_array(a)` gets, and `json_array(' ')` 
becomes `JSON_ARRAY()`, which collides with the empty call. Tabs and newlines 
at the edges are dropped the same way. This contradicts the PR's statement that 
column names stay the same, and the comment above, which limits the trim to the 
clause-only case. Canonical `sql` is not affected, because its edges are always 
quoted child SQL and an ON NULL clause.
   
   Dropping the leading space only when there are no values keeps value text 
verbatim. Testing `values` rather than the rendered text also keeps 
`json_array('' NULL ON NULL)` at its base-branch name `JSON_ARRAY( NULL ON 
NULL)`:
   
   ```suggestion
       // A zero-element array drops the leading space of its clauses 
(`JSON_ARRAY(ABSENT ON NULL)`).
       val clausesSQL = s"$nullSQL$returningSQL"
       val inner = if (values.isEmpty) clausesSQL.stripPrefix(" ") else 
s"$valuesSQL$clausesSQL"
       s"JSON_ARRAY($inner)"
   ```
   
   The test comment at 
`sql/core/src/test/scala/org/apache/spark/sql/JsonArraySuite.scala:537`, which 
credits `trim` for the zero-element form, needs the same update.
   
   **Verification:**
   - **Regression:** Auto-generated column names keep whitespace at the edges 
of string-literal elements: `json_array(' a')` is named `JSON_ARRAY( a)`, 
`json_array('a ')` is named `JSON_ARRAY(a )`, `json_array(' a' NULL ON NULL)` 
is named `JSON_ARRAY( a NULL ON NULL)`, and selecting `json_array(' ')` 
together with `json_array()` produces two distinct column names.
   - **Behavior:** Zero-element arrays still render without a leading space: 
canonical SQL of the default and NULL ON NULL empty arrays is 
`JSON_ARRAY(ABSENT ON NULL)` and `JSON_ARRAY(NULL ON NULL)`, an empty array 
with a collated RETURNING renders `JSON_ARRAY(ABSENT ON NULL RETURNING STRING 
COLLATE UTF8_LCASE)` and reparses to `[]`, and the column name of 
`json_array(NULL ON NULL)` is `JSON_ARRAY(NULL ON NULL)`.
   - **Inspection:** No comment in jsonExpressions.scala's JsonArray rendering 
or in JsonArraySuite.scala still says that `trim` produces the zero-element 
form.



-- 
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