cloud-fan commented on code in PR #58034: URL: https://github.com/apache/spark/pull/58034#discussion_r4300600426
########## docs/sql-ref-syntax-qry-select-json-object.md: ########## @@ -0,0 +1,172 @@ +--- +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. + In the `key : value` form, the first top-level `:` after the key ends it, so a key that is + itself a `:` extraction (e.g. `v:x`) must be parenthesized: `JSON_OBJECT((v:x) : 1)`. Review Comment: **Nit (P3):** The parentheses fix only the parse. A parenthesized `:` extraction is always VARIANT-typed: `ExtractSemiStructuredFields` rewrites `v:x` to a `VariantGet` returning VARIANT, and a non-VARIANT column fails earlier with `COLUMN_IS_NOT_VARIANT_TYPE`. `JsonObjectExpr.checkInputDataTypes` accepts only STRING or NULL keys and adds no implicit cast, so this example, `JSON_OBJECT((v:x) : 1)`, fails with `DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE`. A user who follows the page's only advice for extraction keys gets an analysis error instead of an object. Showing the cast that `JsonObjectSuite` already uses, `JSON_OBJECT((v:x::string) : 1)` (or the VALUE form, `JSON_OBJECT(v:x::string VALUE 1)`), and noting that an extraction yields a VARIANT, would make the example work as written. **Verification:** - **Inspection:** The documented extraction-key spelling casts the parenthesized `:` extraction to a string type, so its key type satisfies the JSON_OBJECT string-key check. - **Behavior:** Running the documented example against a VARIANT column returns an object keyed by the extracted value instead of a DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE error. -- 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]
