snuyanzin commented on code in PR #28688:
URL: https://github.com/apache/flink/pull/28688#discussion_r3658393520
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/BaseExpressions.java:
##########
@@ -2525,6 +2525,70 @@ public OutType jsonQuery(String path, JsonQueryWrapper
wrappingBehavior) {
path, wrappingBehavior, JsonQueryOnEmptyOrError.NULL,
JsonQueryOnEmptyOrError.NULL);
}
+ public OutType jsonLength() {
+ return toApiSpecificExpression(unresolvedCall(JSON_LENGTH, toExpr()));
+ }
+
+ /**
+ * Returns the number of elements in a JSON document, or the length of the
value at the
+ * specified path if one is provided.
+ *
+ * <p>The input can be a JSON STRING or a VARIANT. Returns {@code NULL} if
the argument is
+ * {@code NULL}, the json is invalid, or the path does not locate a value.
+ *
+ * <p>The length is determined as follows:
+ *
+ * <ul>
+ * <li>Scalar values (number, string, boolean) have length 1.
+ * <li>Arrays have a length equal to the number of their elements.
+ * <li>Objects have a length equal to the number of their key-value
pairs.
+ * </ul>
+ *
+ * <p>When provided with a path that uses a wildcard and resolves to 2 or
more paths, {@code
+ * JSON_LENGTH} resolves to {@code NULL}.
+ *
+ * <p>JSON_LENGTH also supports input of the VARIANT type; you can pass
the output of PARSE_JSON
+ * into JSON_LENGTH.
+ *
+ * <p>Because a {@code NULL} 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 {@code JSON_LENGTH} with a helper function so
invalid input is handled
+ * explicitly rather than silently returning {@code NULL}:
+ *
+ * <ul>
+ * <li>Without a path, guard the call with {@code IS JSON} to separate
malformed input from a
+ * real result.
+ * <li>With a path, use {@code JSON_EXISTS} to tell "the path is absent"
apart from "the path
+ * matched but was ambiguous / matched 2 or more nodes".
+ * </ul>
+ *
+ * <pre>{@code
+ * // returns the length only for valid JSON, otherwise NULL means
"invalid input"
+ * lit("[1,2,3]").isJson().then(lit("[1,2,3]").jsonLength(),
nullOf(DataTypes.INT()))
+ *
+ * // pathPresent is true even when jsonLength is NULL because of a
multi-match wildcard
+ * lit("{}").jsonExists("$.items[*]")
+ * lit("{}").jsonLength("$.items[*]")
+ * }</pre>
+ *
+ * <p>Examples:
+ *
+ * <pre>{@code
+ * JSON_LENGTH('{"1": "hello", "2": "bye bye"}') // 2
+ * JSON_LENGTH('[1,2,3,4,5]') // 5
+ * JSON_LENGTH('"hello"') // 1
+ *
+ * JSON_LENGTH('{"1": "hello", "2": "bye bye"}', '$.1') // 1
+ * JSON_LENGTH('{"1": [1,2,3], "2": "bye bye"}', '$.1') // 3
+ * JSON_LENGTH('[1,2,3,4,5]', '$[3]') // 1
+ *
+ * JSON_LENGTH('[1,2,3,4,5]', '$[7]') // Null
Review Comment:
```suggestion
* JSON_LENGTH('[1,2,3,4,5]', '$[7]') // NULL
```
we should be consistent
--
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]