diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index 97cc3d60340..d71c052cd5b 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -4935,9 +4935,11 @@ jsonb_set_lax(PG_FUNCTION_ARGS) newval = DirectFunctionCall1(jsonb_in, CStringGetDatum("null")); - fcinfo->args[2].value = newval; - fcinfo->args[2].isnull = false; - return jsonb_set(fcinfo); + return DirectFunctionCall4(jsonb_set, + PG_GETARG_DATUM(0), + PG_GETARG_DATUM(1), + newval, + PG_GETARG_DATUM(3)); } else if (strcmp(handle_val, "delete_key") == 0) { diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out index fc27adb913b..5ef6f8e4a2f 100644 --- a/src/test/regress/expected/jsonb.out +++ b/src/test/regress/expected/jsonb.out @@ -4935,6 +4935,34 @@ select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'use_ {"a": 1, "b": null} (1 row) +-- multi-row cases with a constant NULL new_value +select jsonb_set_lax(j, '{a}', null, true, t) +from (values ('{"a":1}'::jsonb, 'use_json_null'), + ('{"a":1}'::jsonb, 'delete_key')) v(j, t); + jsonb_set_lax +--------------- + {"a": null} + {} +(2 rows) + +select jsonb_set_lax(j, '{a}', null, true, t) +from (values ('{"a":1}'::jsonb, 'use_json_null'), + ('{"a":1}'::jsonb, 'return_target')) v(j, t); + jsonb_set_lax +--------------- + {"a": null} + {"a": 1} +(2 rows) + +select jsonb_set_lax(j, '{a}', null) +from (values ('{"a":1}'::jsonb), ('{"a":2}'::jsonb), ('{"a":3}'::jsonb)) v(j); + jsonb_set_lax +--------------- + {"a": null} + {"a": null} + {"a": null} +(3 rows) + \pset null '' -- jsonb_insert select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"'); diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql index a13e904a9e2..a200cbf8642 100644 --- a/src/test/regress/sql/jsonb.sql +++ b/src/test/regress/sql/jsonb.sql @@ -1294,6 +1294,16 @@ select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'retu select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'delete_key') as delete_key; select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'use_json_null') as use_json_null; +-- multi-row cases with a constant NULL new_value +select jsonb_set_lax(j, '{a}', null, true, t) +from (values ('{"a":1}'::jsonb, 'use_json_null'), + ('{"a":1}'::jsonb, 'delete_key')) v(j, t); +select jsonb_set_lax(j, '{a}', null, true, t) +from (values ('{"a":1}'::jsonb, 'use_json_null'), + ('{"a":1}'::jsonb, 'return_target')) v(j, t); +select jsonb_set_lax(j, '{a}', null) +from (values ('{"a":1}'::jsonb), ('{"a":2}'::jsonb), ('{"a":3}'::jsonb)) v(j); + \pset null '' -- jsonb_insert