gustavodemorais commented on code in PR #28688:
URL: https://github.com/apache/flink/pull/28688#discussion_r3630734492
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/JsonFunctionsITCase.java:
##########
@@ -279,7 +281,9 @@ private static TestSetSpec jsonExistsSpec() {
.testTableApiRuntimeError(
$("f0").jsonExists("strict $.invalid",
JsonExistsOnError.ERROR),
TableRuntimeException.class,
- "No results for path: $['invalid']");
+ "No results for path: $['invalid']")
+ .testSqlResult("JSON_EXISTS(f0, '$.items[*]')", false,
BOOLEAN())
Review Comment:
Good, that's the proper place to add this
##########
docs/data/sql_functions.yml:
##########
@@ -1223,7 +1218,51 @@ json:
-- [{"nested_json":{"value":42}}]
JSON_ARRAY(JSON('{"nested_json": {"value": 42}}'))
```
-
+ - sql: JSON_LENGTH(json_doc[, path])
+ table: jsonLength(jsonObject[, path])
+ description: |
+ Returns the number of elements in a JSON document, or the length of the
value at the specified path if one is provided.
+ Returns NULL if the argument is NULL, the json is invalid, or the path
does not locate a value.
Review Comment:
```suggestion
Returns the number of elements in a JSON document, or the length of
the value at the specified path if one is provided.
The input can be a JSON STRING or a VARIANT. Returns NULL if the
argument is NULL, the json is invalid, or the path does not locate a value.
```
##########
flink-python/pyflink/table/expression.py:
##########
@@ -2257,6 +2257,74 @@ def json_unquote(self) -> 'Expression':
"""
return _unary_op("jsonUnquote")(self)
+ def json_length(self, path = None) -> 'Expression':
+ """
+ Returns the number of elements in a JSON document, or the length of
the value at the
+ specified path if one is provided.
+
+ Returns None if the argument is None, the input is not valid JSON, or
the path
+ does not locate a value.
+
+ The length is determined as follows:
+
+ - Scalar values (number, string, boolean) have length 1.
+ - Arrays have a length equal to the number of their elements.
+ - Objects have a length equal to the number of their key-value pairs.
+
+ For the path argument, use the form:
+
+ path ::= '$' ( '.' <field> | '[' <index> ']' )*
+ field ::= a key in a JSON object
+ index ::= a zero-based position in a JSON array
+
+ When provided with a path that uses a wildcard and resolves to 2 or
more paths,
+ 'json_length' resolves to None.
+
+ json_length also supports input of the VARIANT type; you can pass the
output of
+ PARSE_JSON into json_length.
+
+ Because a None result can mean several different things (the input is
not valid
+ JSON, the path does not match anything, or a wildcard path matched 2
or more
+ nodes), it is recommended to pair json_length with a helper function
so invalid
+ input is handled explicitly rather than silently returning None:
+
+ - Without a path, guard the call with is_json to separate malformed
input from a
+ real result.
+ - With a path, use json_exists to tell "the path is absent" apart from
"the path
+ matched but was ambiguous / matched 2 or more nodes".
+
+ ::
+
+ # returns the length only for valid JSON, otherwise None means
"invalid input"
+ >>> lit("[1,2]").is_json().then(lit("[1,2]").json_length(),
null_of(DataTypes.INT()))
+
+ # path is present even when json_length is None due to a
multi-match wildcard
+ >>> lit("{}").json_exists("$.items[*]")
+ >>> lit("{}").json_length("$.items[*]")
+
+ Examples:
+ ::
+
+ >>> lit('{"1": "hello", "2": "bye bye"}').json_length() # 2
+ >>> lit('[1,2,3,4,5]').json_length() # 5
+ >>> lit('"hello"').json_length() # 1
+
+ >>> lit('{"1": "hello", "2": "bye bye"}').json_length('$.1') # 1
+ >>> lit('{"1": [1,2,3], "2": "bye bye"}').json_length('$.1') # 3
+ >>> lit('[1,2,3,4,5]').json_length('$[3]') # 1
+
+ >>> lit('[1,2,3,4,5]').json_length('$.[7]') # None
+ >>> lit('{"1": "bad", "2": "syntax here ->"').json_length('$.1') #
None
+
+ # VARIANT input via PARSE_JSON, reached through call_sql
+ >>> call_sql("PARSE_JSON('[1,2,3,4,5]')").json_length() # 5
+ >>> call_sql("PARSE_JSON('{\"1\":1,\"2\":2}')").json_length('$.1')
# 1
Review Comment:
Just mention that it accepts variant. We don't have to show the user how to
work with variants here
https://github.com/apache/flink/pull/28688/changes#r3630708300
--
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]