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


##########
sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4:
##########
@@ -1566,6 +1571,31 @@ jsonArrayValue
     : value=expression (FORMAT JSON)?
     ;
 
+// A key-value pair in JSON_OBJECT: `key VALUE value`, `KEY key VALUE value`, 
or `key : value`.
+// Both sides accept a full `expression` (not just `valueExpression`) so that 
ordinary predicates --
+// e.g. `JSON_OBJECT('present' VALUE x IS NOT NULL)` -- work without 
parentheses, matching normal
+// function-argument syntax. The `VALUE` / `COLON` separator and the trailing 
`ON NULL` / `RETURNING`
+// clauses are keywords that terminate the expression, so this stays 
unambiguous. The optional
+// `FORMAT JSON` clause marks a string value as already-JSON text to splice in 
raw instead of
+// quoting; a lexically-nested JSON constructor carries it implicitly (see 
`jsonArrayValue`).
+jsonObjectMember
+    : KEY keyExpr=expression VALUE valueExpr=expression (FORMAT JSON)?
+    | keyExpr=expression (VALUE | COLON) valueExpr=expression (FORMAT JSON)?

Review Comment:
   Done



##########
sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4:
##########
@@ -1486,6 +1486,11 @@ primaryExpression
       (nullBehavior=jsonConstructorNullBehavior ON NULL)?
       (RETURNING returning=dataType)?
       RIGHT_PAREN                                  #jsonArray
+    | JSON_OBJECT LEFT_PAREN (

Review Comment:
   Done



##########
docs/sql-ref-syntax-qry-select-json-object.md:
##########
@@ -0,0 +1,170 @@
+---
+layout: global
+title: JSON_OBJECT
+displayTitle: JSON_OBJECT
+license: |
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+---
+
+### Description
+
+The `JSON_OBJECT` constructor function builds a JSON object from key/value 
pairs and returns it as
+JSON text. This is the SQL-standard way (SQL:2016) to assemble a JSON object 
inline, with common
+compatibility syntax from other systems. `JSON_OBJECT` is an expression that 
can appear anywhere a
+value is allowed.
+
+Each value is serialized with the same JSON writer as the built-in `to_json` 
function, so numbers,
+decimals, booleans, dates, timestamps, and nested structs/arrays/maps render 
the same way.
+Null-field handling inside a struct value therefore follows
+`spark.sql.jsonGenerator.ignoreNullFields`, exactly as `to_json` does; the `ON 
NULL` clause below
+controls only the top-level object members.
+
+This is an initial subset of the SQL:2016 `JSON_OBJECT` constructor. It 
supports the key/value
+members, the value-level `FORMAT JSON` marker, the `{ NULL | ABSENT } ON NULL` 
clause, and a
+string-type `RETURNING`. The following SQL/JSON clauses are not yet supported:
+
+* The `{ WITH | WITHOUT } UNIQUE KEYS` clause. Duplicate keys are kept in 
source order (the
+  `WITHOUT UNIQUE KEYS` behavior); there is no option to reject them.
+* `RETURNING` to a non-string type, or with a binary/`FORMAT JSON` output 
clause. The result is
+  always JSON text of a string type.
+
+### Syntax
+
+```sql
+JSON_OBJECT ( [ { key VALUE value | KEY key VALUE value | key : value } [ 
FORMAT JSON ] [, ...] ]
+              [ { NULL | ABSENT } ON NULL ]
+              [ RETURNING data_type ] )
+
+JSON_OBJECT ( [ key, value [, key, value] ... ]
+              [ { NULL | ABSENT } ON NULL ]
+              [ RETURNING data_type ] )
+```
+
+### Parameters
+
+* **key**
+
+    An expression producing a member name. It must be a non-null string; a 
`NULL` key raises an
+    error at runtime. The key is validated before the `ON NULL` handling, so a 
`NULL` key raises the
+    error even when the member's value is `NULL` and `ABSENT ON NULL` would 
otherwise omit it.
+
+* **value**
+
+    An expression producing the member value. Values may have different types 
and may be nested
+    JSON constructors. `JSON_OBJECT()` with no members produces the empty 
object `{}`. A value is
+    spliced in as raw JSON, rather than quoted as a string, only when it 
carries `FORMAT JSON` (see
+    below) -- either the explicit clause or the implicit form carried by a 
recognized JSON producer
+    written directly in the value position. This covers a nested `JSON_OBJECT` 
or `JSON_ARRAY` (e.g.
+    `JSON_OBJECT('a' VALUE JSON_OBJECT('b' VALUE 1))` produces 
`{"a":{"b":1}}`), and a nested
+    `JSON_QUERY` under the default `KEEP QUOTES`, which returns JSON text (e.g.
+    `JSON_OBJECT('a' VALUE JSON_QUERY('{"o":{"x":1}}', '$.o'))` produces 
`{"a":{"x":1}}`). A
+    `JSON_QUERY` with `OMIT QUOTES` returns an ordinary string, so it is 
quoted like any other value
+    (e.g. `JSON_OBJECT('n' VALUE JSON_QUERY('{"n":"Ada"}', '$.n' OMIT 
QUOTES))` produces
+    `{"n":"Ada"}`). Without `FORMAT JSON`, an ordinary string value is always 
quoted, even when its
+    contents are themselves valid JSON text (e.g. `JSON_OBJECT('a' VALUE 
'{"b":1}')` produces
+    `{"a":"{\"b\":1}"}`). Implicit raw splicing applies to the direct syntax 
above and to the
+    unqualified comma form (e.g. `json_object('a', json_object('b', 1))`, 
which carries the nested
+    producer through to the built-in and produces `{"a":{"b":1}}`). A 
qualified or otherwise generic
+    routed call (e.g. `builtin.json_object('a', json_object('b', 1))`) 
currently quotes the nested
+    result instead, producing `{"a":"{\"b\":1}"}`.
+
+* **FORMAT JSON**
+
+    Marks a string `value` as already-JSON text, so it is spliced into the 
object verbatim instead
+    of being quoted (e.g. `JSON_OBJECT('a' VALUE '{"b":1}' FORMAT JSON)` 
produces `{"a":{"b":1}}`).
+    A nested JSON constructor written directly in the value position carries 
`FORMAT JSON`
+    implicitly. `FORMAT JSON` requires a string argument (an untyped `NULL` 
literal is also accepted
+    and follows the `ON NULL` behavior). At runtime, a non-null `FORMAT JSON` 
value must contain
+    exactly one well-formed JSON value; malformed text or multiple top-level 
values raise an error.
+
+* **{ NULL | ABSENT } ON NULL**
+
+    How to handle a member whose `value` is `NULL`:
+    * `NULL ON NULL` (the default) keeps the member with a JSON `null` value.
+    * `ABSENT ON NULL` omits the member entirely.
+
+* **RETURNING data_type**
+
+    The type of the result. It must be a string type; the result is JSON text. 
If `RETURNING` is
+    omitted, the result type is `STRING`. `CHAR` / `VARCHAR` are normalized to 
`STRING` (the length
+    is not enforced, because the fragment is serialized directly).
+
+### Examples
+
+```sql
+-- Construct an object from key/value pairs
+SELECT json_object('id' VALUE 7, 'name' VALUE 'Ada');
++-------------------------------------------+
+|json_object('id' VALUE 7, 'name' VALUE Ada)|

Review Comment:
   Done



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -2272,3 +2331,419 @@ case class JsonTypeof(child: Expression)
   override protected def withNewChildInternal(newChild: Expression): 
JsonTypeof =
     copy(child = newChild)
 }
+
+/**
+ * The SQL:2016 `JSON_OBJECT` constructor function (feature T811): constructs 
a JSON object from
+ * key-value pairs, written `key VALUE value`, `KEY key VALUE value`, `key : 
value`, or the
+ * MySQL-style `key, value`.
+ *
+ * Keys must be non-null strings; a null key is an error. The `ON NULL` clause 
controls whether
+ * null-valued pairs are included (NULL ON NULL, the standard default) or 
omitted (ABSENT ON NULL).
+ *
+ * A value is spliced in raw (unquoted) when it carries `FORMAT JSON` (tracked 
per member by
+ * `rawJson`): the explicit clause (e.g. `JSON_OBJECT('a' VALUE '{"b":1}' 
FORMAT JSON)`), or the
+ * implicit form carried by a nested JSON constructor / `JSON_QUERY` under 
KEEP QUOTES (see
+ * `rawJsonValue`). Explicit `FORMAT JSON` on a non-constructor is arbitrary 
user text, so
+ * `needsValidation` marks it for JSON validation at eval; a nested 
constructor is trusted.
+ *
+ * Examples:
+ *   JSON_OBJECT('id' VALUE 7, 'name' VALUE 'Ada')      -> 
'{"id":7,"name":"Ada"}'
+ *   JSON_OBJECT('id': 7, 'v': NULL)                    -> '{"id":7,"v":null}'
+ *   JSON_OBJECT('id': 7, 'v': NULL ABSENT ON NULL)     -> '{"id":7}'
+ *   JSON_OBJECT()                                       -> '{}'
+ *
+ * A clause-free call that is not itself lexically nested in an enclosing 
constructor's *value*
+ * position routes through function resolution and is rebuilt by 
`JsonObjectExpressionBuilder`, so a
+ * same-named routine can shadow the built-in `json_object`: always for the 
comma form (a nested
+ * producer value routes too, carrying its raw-splice eligibility via 
`JsonImplicitFormatCarrier`),
+ * but for the `VALUE` / `:` forms only when no member carries a raw value 
(`rawJson.contains(true)`
+ * -- an explicit `FORMAT JSON` or a nested producer in *value* position -- 
keeps it direct).
+ * Otherwise it is built directly from the grammar (clause-bearing, or 
value-nested); a nested
+ * *key*-position call stays routable (see `AstBuilder.visitJsonObject`).
+ * The user-facing reference lives in 
`docs/sql-ref-syntax-qry-select-json-object.md`.
+ */
+case class JsonObjectExpr(
+    members: Seq[(Expression, Expression)],
+    rawJson: Seq[Boolean],
+    needsValidation: Seq[Boolean],
+    nullBehavior: JsonConstructorNullBehavior = 
JsonConstructorNullBehavior.Null,
+    returning: DataType = StringType,
+    timeZoneId: Option[String] = None)
+  extends Expression
+  with TimeZoneAwareExpression
+  with CodegenFallback
+  with QueryErrorsBase
+  // Default RETURNING is a plain STRING, so 
`DefaultStringProducingExpression` lets
+  // `ApplyDefaultCollation` cast the result to a non-default collation; the 
`dataType` override
+  // below stays authoritative when RETURNING is given explicitly.

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