-
Notifications
You must be signed in to change notification settings - Fork 14k
[FLINK-40089][table] Implement JSON_LENGTH function #28688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1685,6 +1685,17 @@ bitmapagg: | |
| `bitmap BITMAP` | ||
|
|
||
| Returns a `BIGINT`. | ||
| - 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 or the path does not locate a value. | ||
|
VasShabu marked this conversation as resolved.
Comment on lines
+1689
to
+1692
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this does not look true SELECT json_length('$');
SELECT json_length('{');each returns |
||
|
|
||
| The length is determined as follows: | ||
| a scalar value (number, string, boolean, or null) has length 1; | ||
|
VasShabu marked this conversation as resolved.
|
||
| an array has a length equal to the number of its elements; | ||
| and an object has a length equal to the number of its key-value pairs. | ||
| Nested arrays and objects each count as a single element and their contents are not included in the count. | ||
|
VasShabu marked this conversation as resolved.
|
||
|
|
||
| catalog: | ||
| - sql: CURRENT_DATABASE() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -374,6 +374,27 @@ private static Object errorResultForJsonQuery( | |
| } | ||
| } | ||
|
|
||
| public static Integer jsonLength(String input, String pathSpec) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have these utilities here and some other in JsonLengthFuntion.java itself. Check if we don't already have utilities for performing the length check. If not, check if it makes sense to move the new private utilities you added to this file |
||
| return jsonLength(jsonApiCommonSyntax(input, pathSpec)); | ||
| } | ||
|
|
||
| private static Integer jsonLength(JsonPathContext context) { | ||
| if (context.hasException()) { | ||
| return null; | ||
| } | ||
| final Object value = context.obj; | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
| if (value instanceof Map) { | ||
| return ((Map<?, ?>) value).size(); | ||
| } | ||
| if (value instanceof Collection) { | ||
| return ((Collection<?>) value).size(); | ||
| } | ||
| return 1; | ||
| } | ||
|
Comment on lines
+377
to
+396
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why to we need this? Don't we have already the runtime class |
||
|
|
||
| public static Object json(String input) { | ||
| try { | ||
| String trimmed = input.trim(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | ||||||||
| /* | ||||||||
| * 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. | ||||||||
| */ | ||||||||
|
|
||||||||
| package org.apache.flink.table.runtime.functions.scalar; | ||||||||
|
|
||||||||
| import org.apache.flink.annotation.Internal; | ||||||||
| import org.apache.flink.table.data.StringData; | ||||||||
| import org.apache.flink.table.functions.BuiltInFunctionDefinitions; | ||||||||
| import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext; | ||||||||
| import org.apache.flink.table.runtime.functions.SqlJsonUtils; | ||||||||
|
|
||||||||
| import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode; | ||||||||
| import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper; | ||||||||
|
|
||||||||
| import javax.annotation.Nullable; | ||||||||
|
|
||||||||
| /** Implementation of {@link BuiltInFunctionDefinitions#JSON_LENGTH}. */ | ||||||||
| @Internal | ||||||||
| public class JsonLengthFunction extends BuiltInScalarFunction { | ||||||||
|
|
||||||||
| private static final ObjectMapper MAPPER = new ObjectMapper(); | ||||||||
|
|
||||||||
| public JsonLengthFunction(SpecializedContext context) { | ||||||||
| super(BuiltInFunctionDefinitions.JSON_LENGTH, context); | ||||||||
| } | ||||||||
|
|
||||||||
| public @Nullable Integer eval(@Nullable StringData jsonInput) { | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: here and everywhere else make parameters/variables that are immutable
Suggested change
|
||||||||
| if (jsonInput == null) { | ||||||||
| return null; | ||||||||
| } | ||||||||
| final JsonNode jsonNode = parse(jsonInput.toString()); | ||||||||
| if (jsonNode == null) { | ||||||||
| return null; | ||||||||
| } | ||||||||
| final int length = computeLength(jsonNode); | ||||||||
| return length == -1 ? null : length; | ||||||||
|
Comment on lines
+50
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you you change the return type to
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| public @Nullable Integer eval(@Nullable StringData jsonInput, @Nullable StringData path) { | ||||||||
| if (jsonInput == null || path == null) { | ||||||||
| return null; | ||||||||
| } | ||||||||
| try { | ||||||||
| return SqlJsonUtils.jsonLength(jsonInput.toString(), path.toString()); | ||||||||
| } catch (Exception e) { | ||||||||
| return null; | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| private static int computeLength(JsonNode jsonNode) { | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change to Integer and directly return null instead of -1 and rechecking again
Suggested change
|
||||||||
| if (jsonNode.isArray() || jsonNode.isObject()) { | ||||||||
| return jsonNode.size(); | ||||||||
| } else if (jsonNode.isTextual() | ||||||||
| || jsonNode.isNumber() | ||||||||
| || jsonNode.isBoolean() | ||||||||
| || jsonNode.isBinary()) { | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this check dead code? Can a jsonNode be binary? You can try to write a test to check if it's dead code or it's possible |
||||||||
| return 1; | ||||||||
| } | ||||||||
| return -1; | ||||||||
| } | ||||||||
|
|
||||||||
| private static @Nullable JsonNode parse(String jsonStr) { | ||||||||
| try { | ||||||||
| return MAPPER.readTree(jsonStr); | ||||||||
| } catch (Exception e) { | ||||||||
| return null; | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
Uh oh!
There was an error while loading. Please reload this page.