gustavodemorais commented on code in PR #28688: URL: https://github.com/apache/flink/pull/28688#discussion_r3544790226
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/JsonLengthFunction.java: ########## @@ -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) { + 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; + } + + 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) { + if (jsonNode.isArray() || jsonNode.isObject()) { + return jsonNode.size(); + } else if (jsonNode.isTextual() + || jsonNode.isNumber() + || jsonNode.isBoolean() + || jsonNode.isBinary()) { Review Comment: 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 ########## docs/data/sql_functions.yml: ########## @@ -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. + + The length is determined as follows: + a scalar value (number, string, boolean, or null) has length 1; Review Comment: I think in your current logic, null does not return length 1 but rather null. What do you think makes sense here? What do other engines do -- 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]
