Skip to content

Commit

Permalink
[FLINK-35687] JSON_QUERY should return a well formatted nested object…
Browse files Browse the repository at this point in the history
…s/arrays for ARRAY<STRING> (#24976)
  • Loading branch information
dawidwys committed Feb 6, 2025
1 parent 14e853a commit 1b5fc06
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,33 @@ private static List<TestSetSpec> jsonQuerySpec() {
"JSON_QUERY(f0, '$.a' RETURNING ARRAY<STRING> WITHOUT WRAPPER EMPTY ARRAY ON ERROR)",
new String[] {},
DataTypes.ARRAY(DataTypes.STRING())),

// stringifying RETURNING<ARRAY>
TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_QUERY)
.onFieldsWithData(
"{\"items\": [{\"itemId\":1234, \"count\":10}, null, {\"itemId\":4567, \"count\":11}]}",
"{\"items\": [[1234, 2345], null, [\"itemId\", \"count\"]]}",
"{\"arr\": [\"abc\", null, \"def\"]}")
.andDataTypes(STRING(), STRING(), STRING())
.testResult(
$("f0").jsonQuery("$.items", ARRAY(STRING())),
"JSON_QUERY(f0, '$.items' RETURNING ARRAY<STRING>)",
new String[] {
"{\"itemId\":1234,\"count\":10}",
null,
"{\"itemId\":4567,\"count\":11}"
},
ARRAY(STRING()))
.testResult(
$("f1").jsonQuery("$.items", ARRAY(STRING())),
"JSON_QUERY(f1, '$.items' RETURNING ARRAY<STRING>)",
new String[] {"[1234,2345]", null, "[\"itemId\",\"count\"]"},
ARRAY(STRING()))
.testResult(
$("f2").jsonQuery("$.arr", ARRAY(STRING())),
"JSON_QUERY(f2, '$.arr' RETURNING ARRAY<STRING>)",
new String[] {"abc", null, "def"},
ARRAY(STRING())),
TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_QUERY)
.onFieldsWithData(jsonValue)
.andDataTypes(STRING())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,13 @@ private static Object jsonQuery(
for (int i = 0; i < list.size(); i++) {
final Object el = list.get(i);
if (el != null) {
arr[i] = StringData.fromString(el.toString());
final String stringifiedEl;
if (isScalarObject(el)) {
stringifiedEl = String.valueOf(el);
} else {
stringifiedEl = jsonize(el);
}
arr[i] = StringData.fromString(stringifiedEl);
}
}

Expand Down

0 comments on commit 1b5fc06

Please sign in to comment.