gustavodemorais commented on code in PR #28688:
URL: https://github.com/apache/flink/pull/28688#discussion_r3644796746
##########
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.
+ eg.
+ -- 2
+ JSON_LENGTH('{"1": "hello", "2": "bye bye"}')
+
+ -- 5
+ JSON_LENGTH('[1,2,3,4,5]')
+
+ -- 1
+ JSON_LENGTH('"hello"')
+
+ -- 1
+ JSON_LENGTH('{"1": "hello", "2": "bye bye"}', '$.2')
+
+ -- NULL
+ JSON_LENGTH('{"1": "hello", "2": "BAD SYNTAX ->"', '$.2')
+
+
+ The length is determined as follows:
+
+ - Scalar values (number, string, boolean): has length 1.
+ - Array: has a length equal to the number of its elements.
+ - Object: has a length equal to the number of its key-value pairs.
+
+ The path argument uses the form:
+
+ path ::= '$' ( '.' `field` | '[' `index` ']' )*
+ field ::= a key in a JSON object
+ index ::= a zero-based position in a JSON array
+
+ For example: `$.author.address` or `$.metadata.tags[0]`.
Review Comment:
You resolved this comment even though the docs weren't changed?
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlJsonUtils.java:
##########
@@ -374,6 +375,97 @@ private static Object errorResultForJsonQuery(
}
}
+ /** Accepts a pre-parsed context from {@link #jsonParse}. */
+ public static Integer jsonLength(final JsonValueContext parsedInput) {
+ try{
+ if (parsedInput.hasException()) {
+ return null;
+ }
+ }
+ catch(NullPointerException e){
+ return null;
+ }
Review Comment:
This is not a clean pattern and looks dirty. What about we do
```suggestion
// TODO FLINK-39419: A null context can result from a shared parse that was
short-circuited before parsing.
if (parsedInput == null || parsedInput.hasException()) {
return null;
}
```
--
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]