Skip to content

[FLINK-37695][table] Fix parsing for built-in function JSON in JSON_OBJECT for all positions #26474

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

Merged
merged 4 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import org.apache.flink.table.planner.calcite.{FlinkTypeFactory, RexDistinctKeyV
import org.apache.flink.table.planner.codegen.CodeGenUtils._
import org.apache.flink.table.planner.codegen.GeneratedExpression.{NEVER_NULL, NO_CODE}
import org.apache.flink.table.planner.codegen.GenerateUtils._
import org.apache.flink.table.planner.codegen.JsonGenerateUtils.{isJsonArrayOperand, isJsonFunctionOperand, isJsonObjectOperand}
import org.apache.flink.table.planner.codegen.JsonGenerateUtils.{inValuesParam, isJsonArrayOperand, isJsonFunctionOperand, isJsonObjectOperand}
import org.apache.flink.table.planner.codegen.calls._
import org.apache.flink.table.planner.codegen.calls.ScalarOperatorGens._
import org.apache.flink.table.planner.codegen.calls.SearchOperatorGen.generateSearch
Expand Down Expand Up @@ -489,7 +489,7 @@ class ExprCodeGenerator(ctx: CodeGeneratorContext, nullableInput: Boolean)
// We only support JSON function operands as the value param of a JSON_OBJECT or JSON_ARRAY function
case (operand: RexNode, i)
if isJsonFunctionOperand(operand) &&
(isJsonArrayOperand(call) || i == 2 && isJsonObjectOperand(call)) =>
(isJsonArrayOperand(call) || isJsonObjectOperand(call) && inValuesParam(i)) =>
generateJsonCall(operand)

case (o @ _, _) => o.accept(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ object JsonGenerateUtils {
}
}

/**
* Determines if the parameter index is % 2, which means checking that we only allow JSON calls
* for the VALUE parameter of a JSON_OBJECT call.
*/
def inValuesParam(i: Int): Boolean = {
(i % 2) == 0
}

/** Generates a method to convert arrays into [[ArrayNode]]. */
private def generateArrayConverter(
ctx: CodeGeneratorContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,47 @@ private static List<TestSetSpec> jsonSpec() {
.testTableApiRuntimeError(
jsonObject(JsonOnNull.NULL, "K", json("{")),
TableRuntimeException.class,
"Invalid JSON string in JSON(value) function"),
"Invalid JSON string in JSON(value) function")

// Tests for JSON_OBJECT with multiple parameters
.testResult(
jsonObject(JsonOnNull.NULL, "key1", "val", "key2", json($("f1"))),
"JSON_OBJECT(KEY 'key1' VALUE 'val', KEY 'key2' VALUE JSON(f1))",
"{\"key1\":\"val\",\"key2\":{\"key\":{\"value\":42}}}",
STRING().notNull())
.testResult(
jsonObject(
JsonOnNull.NULL,
"key1",
json($("f0")),
"key2",
json($("f1"))),
"JSON_OBJECT(KEY 'key1' VALUE JSON(f0), KEY 'key2' VALUE JSON(f1))",
"{\"key1\":{\"key\":\"value\"},\"key2\":{\"key\":{\"value\":42}}}",
STRING().notNull())
.testResult(
jsonObject(
JsonOnNull.NULL,
"outerKey",
"outerValue",
"nestedObject",
jsonObject(JsonOnNull.NULL, "innerKey", json($("f0")))),
"JSON_OBJECT(KEY 'outerKey' VALUE 'outerValue', KEY 'nestedObject' VALUE JSON_OBJECT(KEY 'innerKey' VALUE JSON(f0)))",
"{\"nestedObject\":{\"innerKey\":{\"key\":\"value\"}},\"outerKey\":\"outerValue\"}",
STRING().notNull())
.testResult(
jsonObject(
JsonOnNull.NULL,
"p1",
json($("f0")),
"p2",
json($("f1")),
"p3",
json("[1, 2, 3]")),
"JSON_OBJECT(KEY 'p1' VALUE JSON(f0), KEY 'p2' VALUE JSON(f1), KEY 'p3' VALUE JSON('[1, 2, 3]'))",
"{\"p1\":{\"key\":\"value\"},\"p2\":{\"key\":{\"value\":42}},\"p3\":[1,2,3]}",
STRING().notNull()),

// Tests for JSON calls inside of JSON_ARRAY
TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_ARRAY)
.onFieldsWithData("{\"key\":\"value\"}", "{\"key\": {\"value\": 42}}")
Expand Down