ganeshashree commented on code in PR #58005:
URL: https://github.com/apache/spark/pull/58005#discussion_r3841484245


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -4268,6 +4268,60 @@ class AstBuilder extends DataTypeAstBuilder
     JsonQuery(jsonExpr, path, returning, wrapper, quotes, onEmpty, onError)
   }
 
+  /**
+   * Resolve a `jsonConstructorNullBehavior` clause (`NULL` / `ABSENT`) into a
+   * [[JsonConstructorNullBehavior]].
+   */
+  private def buildJsonConstructorNullBehavior(
+      ctx: JsonConstructorNullBehaviorContext): JsonConstructorNullBehavior =
+    ctx match {
+      case _: JsonConstructorNullBehaviorNullContext =>
+        JsonConstructorNullBehavior.Null
+      case _: JsonConstructorNullBehaviorAbsentContext =>
+        JsonConstructorNullBehavior.Absent
+    }
+
+  /**
+   * Create a [[JsonArray]] expression for the SQL:2016 `JSON_ARRAY` 
constructor function.
+   * The `ON NULL` clause defaults to `ABSENT ON NULL` (drops NULL elements), 
and RETURNING
+   * defaults to STRING.
+   */
+  override def visitJsonArray(ctx: JsonArrayContext): Expression = 
withOrigin(ctx) {
+    val arrayValues = ctx.values.asScala.map(v => expression(v.value)).toSeq
+    // Freeze the FORMAT JSON decisions here, from the lexical argument, so a 
later
+    // analyzer/optimizer rewrite that wraps or swaps the child cannot change 
them (see
+    // [[ImplicitlyFormattedAsJson]]). For each element:
+    //  - `formatJson`: whether it is already-JSON text spliced raw. True when 
it carries an
+    //    explicit `FORMAT JSON` clause, or is a (lexically) nested JSON 
constructor -- seen through
+    //    a value-preserving `COLLATE` via `JsonArray.isImplicitlyJson`.
+    //  - `needsValidation`: whether its raw text is arbitrary user input to 
JSON-validate at eval.
+    //    True only for an explicit `FORMAT JSON` on something that is NOT a 
JSON constructor; a
+    //    nested constructor emits well-formed JSON by construction and is 
trusted.
+    val formatArgs = ctx.values.asScala.zip(arrayValues).map { case (v, expr) 
=>
+      val explicit = v.FORMAT() != null
+      val implicitlyJson = JsonArray.isImplicitlyJson(expr)
+      (explicit || implicitlyJson, explicit && !implicitlyJson)
+    }.toSeq
+    val formatJson = formatArgs.map(_._1)
+    val needsValidation = formatArgs.map(_._2)
+    // Default RETURNING type is STRING; the result is JSON text. A 
CHAR/VARCHAR RETURNING is
+    // normalized to STRING unconditionally: JSON_ARRAY serializes the 
fragment itself and never
+    // advertises a CHAR/VARCHAR length it does not enforce. The 
CharVarcharUtils helpers cannot be
+    // used here -- they honor spark.sql.preserveCharVarcharTypeInfo and would 
leave a VARCHAR(n)
+    // length in the output type when that flag is set. A non-string RETURNING 
is left intact for
+    // checkInputDataTypes to fail.
+    val returning = Option(ctx.returning).map(typedVisit[DataType]).map {
+      case c: CharType => c.toStringType
+      case v: VarcharType => v.toStringType
+      case other => other
+    }.getOrElse(StringType)
+    // Default NULL ON NULL behavior is ABSENT (drop NULL elements).

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