diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index d6715f6cbc8..035aca2d810 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -2904,7 +2904,6 @@ eval_const_expressions_mutator(Node *node, } break; } - case T_JsonValueExpr: { JsonValueExpr *jve = (JsonValueExpr *) node; @@ -2928,7 +2927,41 @@ eval_const_expressions_mutator(Node *node, (Expr *) formatted_expr, copyObject(jve->format)); } + case T_JsonConstructorExpr: + { + JsonConstructorExpr *jce = (JsonConstructorExpr *) node; + JsonConstructorExpr *newjce; + Node *save_case_val; + /* + * Copy the node and const-simplify its arguments. We can't + * use ece_generic_processing() here because we need to mess + * with case_val only while processing the coercion. + */ + newjce = makeNode(JsonConstructorExpr); + memcpy(newjce, jce, sizeof(JsonConstructorExpr)); + newjce->args = (List *) + eval_const_expressions_mutator((Node *) jce->args, + context); + newjce->func = (Expr *) + eval_const_expressions_mutator((Node *) jce->func, + context); + + /* + * Set up for the CaseTestExpr node contained in the coercion. + * We must prevent it from absorbing any outer CASE value. + */ + save_case_val = context->case_val; + context->case_val = NULL; + + newjce->coercion = (Expr *) + eval_const_expressions_mutator((Node *) jce->coercion, + context); + + context->case_val = save_case_val; + + return (Node *) newjce; + } case T_SubPlan: case T_AlternativeSubPlan: diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 2f4edf22433..59f6dbd629b 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -1271,11 +1271,11 @@ typedef struct CaseWhen * * Placeholder for intermediate results in some SQL/JSON expression nodes, * such as JsonConstructorExpr. * - * The uses in CaseExpr and ArrayCoerceExpr are safe only to the extent that - * there is not any other CaseExpr or ArrayCoerceExpr between the value source - * node and its child CaseTestExpr(s). This is true in the parse analysis - * output, but the planner's function-inlining logic has to be careful not to - * break it. + * The uses in CaseExpr, ArrayCoerceExpr, and JsonConstructorExpr are safe + * only to the extent that there is not any other such node between the + * value source node and its child CaseTestExpr(s). This is true in the + * parse analysis output, but the planner's constant-folding of simple CASE + * and its function-inlining logic have to be careful not to break it. * * The nested-assignment-expression case is safe because the only node types * that can be above such CaseTestExprs are FieldStore and SubscriptingRef. diff --git a/src/test/regress/expected/sqljson.out b/src/test/regress/expected/sqljson.out index 09bd600d2a9..b42732d51fc 100644 --- a/src/test/regress/expected/sqljson.out +++ b/src/test/regress/expected/sqljson.out @@ -281,6 +281,13 @@ SELECT JSON_OBJECT(1: 1, '2': NULL, '3': 1, 4: NULL, '5': 'a' ABSENT ON NULL WIT {"1": 1, "3": 1, "5": "a"} (1 row) +-- the RETURNING coercion must not pick up the test value of an enclosing CASE +SELECT CASE 'x' WHEN JSON_OBJECT('a': 'b' RETURNING text) THEN 1 ELSE 0 END; + case +------ + 0 +(1 row) + -- JSON_ARRAY() SELECT JSON_ARRAY(); json_array diff --git a/src/test/regress/sql/sqljson.sql b/src/test/regress/sql/sqljson.sql index 05a7f1ffe0d..c781653928d 100644 --- a/src/test/regress/sql/sqljson.sql +++ b/src/test/regress/sql/sqljson.sql @@ -88,6 +88,8 @@ SELECT JSON_OBJECT(1: 1, '2': NULL, '1': 1 ABSENT ON NULL WITH UNIQUE RETURNING SELECT JSON_OBJECT(1: 1, '2': NULL, '1': 1 ABSENT ON NULL WITHOUT UNIQUE RETURNING jsonb); SELECT JSON_OBJECT(1: 1, '2': NULL, '3': 1, 4: NULL, '5': 'a' ABSENT ON NULL WITH UNIQUE RETURNING jsonb); +-- the RETURNING coercion must not pick up the test value of an enclosing CASE +SELECT CASE 'x' WHEN JSON_OBJECT('a': 'b' RETURNING text) THEN 1 ELSE 0 END; -- JSON_ARRAY() SELECT JSON_ARRAY();