cloud-fan commented on code in PR #57920: URL: https://github.com/apache/spark/pull/57920#discussion_r3764143686
########## docs/sql-ref-syntax-qry-select-json-exists.md: ########## @@ -0,0 +1,127 @@ +--- +layout: global +title: JSON_EXISTS +displayTitle: JSON_EXISTS +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_EXISTS` predicate tests whether a SQL/JSON path matches at least one item in a JSON +document, returning a `BOOLEAN`. This is the SQL-standard (SQL:2016) way to test for the presence +of a JSON value, and is commonly used to migrate queries from other systems such as Oracle, DB2, +and PostgreSQL. + +Unlike `get_json_object(json_expr, path) IS NOT NULL`, `JSON_EXISTS` distinguishes a path that is +_present but whose value is JSON `null`_ (which is `true`) from a path that is _absent_ (which is +`false`). + +### Syntax + +```sql +JSON_EXISTS ( json_expr, path [ { TRUE | FALSE | UNKNOWN | ERROR } ON ERROR ] ) +``` + +### Parameters + +* **json_expr** + + An expression that evaluates to a `STRING` containing the JSON document. A `NULL` input yields + `NULL` (SQL Unknown), regardless of the `ON ERROR` clause. + +* **path** + + A constant SQL/JSON path literal (for example `'$.a.b'`, `'$.tags[0]'`, or `'$.a[*].b'`). Paths + are evaluated in **lax** mode, matching Oracle and PostgreSQL: array wildcards (`[*]`) and member + wildcards (`.*` / `['*']`) are supported, and arrays are auto-wrapped/unwrapped (a member, index, + or wildcard step applied to an array is applied to each element, and a non-array value is treated + as a single-element array). A structural mismatch is a non-match, not an error. A syntactically + invalid path is rejected during analysis. + +* **{ TRUE | FALSE | UNKNOWN | ERROR } ON ERROR** + + Controls the result when `json_expr` is not a single well-formed JSON value (malformed input, + or a valid value followed by trailing content). `TRUE`, `FALSE`, and `UNKNOWN` produce that + value (`UNKNOWN` is a `BOOLEAN` `NULL`); `ERROR` raises an error. The default is + `FALSE ON ERROR`. + +### Result + +* The path matches at least one item (including a match whose value is JSON `null`) → `true`. +* The path matches nothing → `false`. +* `json_expr` is SQL `NULL` → `NULL`. +* `json_expr` is not a single well-formed JSON value → the `ON ERROR` behavior. + +A structural mismatch is treated as "no match" (`false`), not an error -- for example reading a key Review Comment: **Nit:** This says a key can belong to a scalar, which obscures the operation that fails. Please phrase this as attempting to read an absent key or attempting to read a key from a scalar. -- 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]
