snuyanzin commented on code in PR #28688:
URL: https://github.com/apache/flink/pull/28688#discussion_r3602931925
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlJsonUtils.java:
##########
@@ -374,6 +375,74 @@ private static Object errorResultForJsonQuery(
}
}
+ /** Accepts a pre-parsed context from {@link #jsonParse}. */
+ public static Integer jsonLength(final JsonValueContext parsedInput) {
+ if (parsedInput.hasException()) {
+ return null;
+ }
+ // Whole document: a top-level JSON null literal counts as a scalar
(length 1).
+ return jsonLengthValue(parsedInput.obj);
+ }
+
+ /** Accepts a pre-parsed context from {@link #jsonParse}. */
+ public static Integer jsonLength(final JsonValueContext parsedInput, final
String pathSpec) {
+ if (parsedInput.hasException()) {
+ // invalid input JSON
+ return null;
+ }
+ final JsonPathContext context = jsonApiCommonSyntax(parsedInput,
pathSpec);
+
+ final Object value = context.hasException() ? null : context.obj;
+ if (context.hasException() || value == null) {
+ return pathExists(parsedInput.obj, pathSpec) ? 1 : null;
+ }
+
+ final Matcher matcher = JSON_PATH_BASE.matcher(pathSpec);
+ final String pathStr = matcher.matches() ? matcher.group("spec") :
pathSpec;
+ if (!JsonPath.isPathDefinite(pathStr)) {
+ final List<?> matched = (List<?>) value;
+ return matched.size() == 1 ? jsonLengthValue(matched.get(0)) :
null;
+ }
+
+ return jsonLengthValue(value);
+ }
+
+ private static int jsonLengthValue(final Object value) {
+ if (value instanceof Map) {
+ return ((Map<?, ?>) value).size();
+ }
+ if (value instanceof Collection) {
+ return ((Collection<?>) value).size();
+ }
+ // Scalars, including a JSON null literal, have length 1.
+ return 1;
Review Comment:
are we sure it can not be array here?
--
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]