Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/data/sql_functions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,17 @@ bitmapagg:
`bitmap BITMAP`

Returns a `BIGINT`.
- sql: JSON_LENGTH(json_doc[, path])
Comment thread
VasShabu marked this conversation as resolved.
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.
Comment thread
VasShabu marked this conversation as resolved.
Comment on lines +1689 to +1692

@snuyanzin snuyanzin Jul 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not look true
simple tests show different behavior

SELECT json_length('$');
SELECT json_length('{');

each returns -1 and the doc says nothing about this or did I miss anything?


The length is determined as follows:
a scalar value (number, string, boolean, or null) has length 1;
Comment thread
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.
Comment thread
VasShabu marked this conversation as resolved.

catalog:
- sql: CURRENT_DATABASE()
Expand Down
2 changes: 1 addition & 1 deletion flink-python/docs/reference/pyflink.table/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,13 @@ JSON functions

.. autosummary::
:toctree: api/

Comment thread
VasShabu marked this conversation as resolved.
Expression.is_json
Expression.json_exists
Expression.json_value
Expression.json_query
Expression.json_quote
Expression.json_unquote
Expression.json_length

value modification functions
----------------------------
Expand Down
17 changes: 17 additions & 0 deletions flink-python/pyflink/table/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2257,6 +2257,23 @@ def json_unquote(self) -> 'Expression':
"""
return _unary_op("jsonUnquote")(self)

def json_length(self, path = None) -> 'Expression':
"""
Return the length of a JSON value, or of the value at `path` if given.

- Scalars have length 1.
- Arrays have length equal to their number of elements.
- Objects have length equal to their number of keys.
- Nested arrays/objects are not counted recursively.
- Returns None if the value is None.
"""


return _unary_op("jsonLength")(self) if path is None else _binary_op("jsonLength")(self, path)




Comment thread
VasShabu marked this conversation as resolved.
# ---------------------------- value modification functions -----------------------------

def object_update(self, *kv) -> "Expression":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
import static org.apache.flink.table.functions.BuiltInFunctionDefinitions.IS_TRUE;
import static org.apache.flink.table.functions.BuiltInFunctionDefinitions.IS_VALID_UTF8;
import static org.apache.flink.table.functions.BuiltInFunctionDefinitions.JSON_EXISTS;
import static org.apache.flink.table.functions.BuiltInFunctionDefinitions.JSON_LENGTH;
import static org.apache.flink.table.functions.BuiltInFunctionDefinitions.JSON_QUERY;
import static org.apache.flink.table.functions.BuiltInFunctionDefinitions.JSON_QUOTE;
import static org.apache.flink.table.functions.BuiltInFunctionDefinitions.JSON_UNQUOTE;
Expand Down Expand Up @@ -2523,6 +2524,27 @@ public OutType jsonQuery(String path, JsonQueryWrapper wrappingBehavior) {
return jsonQuery(
path, wrappingBehavior, JsonQueryOnEmptyOrError.NULL, JsonQueryOnEmptyOrError.NULL);
}
/**
* Returns the number of elements contained in a JSON value, optionally at a given path.
*
* <p>Counting works differently depending on the JSON type: objects report how many
* key-value pairs they contain, arrays report how many entries they hold, and any scalar
* value (such as a number, string, or boolean) is treated as a single element and reports 1.
* Only the top level is measured — elements that are themselves arrays or objects contribute
* 1 to the count regardless of what they contain.
Comment thread
VasShabu marked this conversation as resolved.
*
* <p>When a path is provided, the count applies to the value found at that path rather than
* the document as a whole. Returns NULL if any argument is NULL{@code} or the path does not
Comment thread
VasShabu marked this conversation as resolved.
* match a value.
*/

public OutType jsonLength() {
return toApiSpecificExpression(unresolvedCall(JSON_LENGTH, toExpr()));
}

public OutType jsonLength(String path) {
return toApiSpecificExpression(unresolvedCall(JSON_LENGTH, toExpr(), valueLiteral(path)));
}

/**
* Extracts JSON values from a JSON string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3081,6 +3081,23 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
.runtimeProvided()
.build();

public static final BuiltInFunctionDefinition JSON_LENGTH =
BuiltInFunctionDefinition.newBuilder()
.name("JSON_LENGTH")
.kind(SCALAR)
.inputTypeStrategy(
or(
sequence(logical(LogicalTypeFamily.CHARACTER_STRING)),
sequence(
logical(LogicalTypeFamily.CHARACTER_STRING),
and(
logical(LogicalTypeFamily.CHARACTER_STRING),
LITERAL))))
.outputTypeStrategy(nullableIfArgs(explicit(DataTypes.INT())))
.runtimeClass(
"org.apache.flink.table.runtime.functions.scalar.JsonLengthFunction")
.build();
// I need to complete this
Comment thread
VasShabu marked this conversation as resolved.
// --------------------------------------------------------------------------------------------
// Variant functions
// --------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Stream<TestSetSpec> getTestSetSpecs() {
final List<TestSetSpec> testCases = new ArrayList<>();
testCases.add(jsonExistsSpec());
testCases.add(jsonValueSpec());
testCases.add(jsonLengthSpec());
testCases.addAll(isJsonSpec());
testCases.addAll(jsonQuerySpec());
testCases.addAll(jsonStringSpec());
Expand All @@ -97,6 +98,58 @@ Stream<TestSetSpec> getTestSetSpecs() {
return testCases.stream();
}

private static TestSetSpec jsonLengthSpec() {
Comment thread
VasShabu marked this conversation as resolved.
final String jsonValue = getJsonFromResource("/json/json-exists.json");

return TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_LENGTH)
.onFieldsWithData(
jsonValue, // f0: existing resource JSON
"{\"a\":1,\"b\":2}", // f1: object
"[1,2,3]", // f2: array
"\"abc\"", // f3: scalar string
"null", // f4: JSON null
"{", // f5: invalid JSON
(String) null) // f6: SQL NULL
.andDataTypes(
STRING(),
STRING(),
STRING(),
STRING(),
STRING(),
STRING(),
STRING())

// SQL NULL input
.testSqlResult("JSON_LENGTH(f6)", null, INT().nullable())

// whole-document length from the existing resource:
// top-level keys are type, author, metadata
.testSqlResult("JSON_LENGTH(f0)", 3, INT().nullable())

// basic shapes
.testSqlResult("JSON_LENGTH(f1)", 2, INT().nullable())
.testSqlResult("JSON_LENGTH(f2)", 3, INT().nullable())
.testSqlResult("JSON_LENGTH(f3)", 1, INT().nullable())

// keep this line aligned with your intended semantics
// current implementation returns NULL for JSON literal null
.testSqlResult("JSON_LENGTH(f4)", null, INT().nullable())
// if you later treat JSON null as a scalar, change expected result to 1
Comment thread
VasShabu marked this conversation as resolved.

// invalid JSON -> NULL
.testSqlResult("JSON_LENGTH(f5)", null, INT().nullable())
.testSqlResult("JSON_LENGTH(f0, '$')", 3, INT().nullable())
.testSqlResult("JSON_LENGTH(f0, '$.type')", 1, INT().nullable())
.testSqlResult("JSON_LENGTH(f0, '$.author')", 2, INT().nullable())
.testSqlResult("JSON_LENGTH(f0, '$.author.address')", 2, INT().nullable())
.testSqlResult("JSON_LENGTH(f0, '$.metadata.tags')", 3, INT().nullable())
.testSqlResult("JSON_LENGTH(f0, '$.metadata.references')", 1, INT().nullable())
.testSqlResult("JSON_LENGTH(f0, '$.metadata.references[0]')", 2, INT().nullable())
.testSqlResult("JSON_LENGTH(f0, '$.metadata.references[0].url')", 1, INT().nullable())
.testSqlResult("JSON_LENGTH(f0, '$.missing')", null, INT().nullable());
Comment thread
VasShabu marked this conversation as resolved.
}


private static TestSetSpec jsonExistsSpec() {
final String jsonValue = getJsonFromResource("/json/json-exists.json");
return TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_EXISTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,27 @@ private static Object errorResultForJsonQuery(
}
}

public static Integer jsonLength(String input, String pathSpec) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 JsonLengthFunction?


public static Object json(String input) {
try {
String trimmed = input.trim();
Expand Down
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: here and everywhere else make parameters/variables that are immutable final (not enforced on Flink but nicer to read).

Suggested change
public @Nullable Integer eval(@Nullable StringData jsonInput) {
public @Nullable Integer eval(final @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;
Comment on lines +50 to +51

@raminqaf raminqaf Jul 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you you change the return type to Integer you can directly return null or the length.

Suggested change
final int length = computeLength(jsonNode);
return length == -1 ? null : length;
return computeLength(jsonNode);

}

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
private static int computeLength(JsonNode jsonNode) {
private static @Nullable Integer computeLength(JsonNode jsonNode) {

if (jsonNode.isArray() || jsonNode.isObject()) {
return jsonNode.size();
} else if (jsonNode.isTextual()
|| jsonNode.isNumber()
|| jsonNode.isBoolean()
|| jsonNode.isBinary()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}
}
}