From 630d702f26bdfafa240a3f6827472f41d752e58e Mon Sep 17 00:00:00 2001 From: Abed Alawiyeh Date: Sat, 5 Dec 2026 22:13:12 +0000 Subject: [PATCH v1] Keep the quotes of a jsonb string DEFAULT in JSON_VALUE RETURNING json JSON_VALUE omits the quotes of a scalar string result, except when returning json or jsonb, where ExecEvalJsonExprPath() keeps them. But a jsonb-valued ON EMPTY or ON ERROR DEFAULT expression was coerced with the quotes omitted regardless of the RETURNING type, so JSON_VALUE(jsonb '{}', '$.a' RETURNING json DEFAULT '"123"'::jsonb ON EMPTY) returned 123 instead of "123", and a DEFAULT of '"abc"'::jsonb failed with an invalid json input error. A json-valued DEFAULT and RETURNING jsonb were not affected. Fix by not omitting the quotes when coercing the DEFAULT expression of a JSON_VALUE that returns json or jsonb. Reported-by: Yibo Dong Discussion: https://postgr.es/m/19971-5a844714bd6f89f8@postgresql.org --- src/backend/executor/execExpr.c | 16 ++++++++++-- .../regress/expected/sqljson_queryfuncs.out | 26 +++++++++++++++++++ src/test/regress/sql/sqljson_queryfuncs.sql | 6 +++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c index 91c429b8..58303758 100644 --- a/src/backend/executor/execExpr.c +++ b/src/backend/executor/execExpr.c @@ -4783,9 +4783,21 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state, ErrorSaveContext *escontext; bool returning_domain = get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN; + bool behavior_omit_quotes = jsexpr->omit_quotes; Assert(jsexpr->on_error != NULL); + /* + * JSON_VALUE omits the quotes of a scalar string result, except when + * returning json or jsonb (see ExecEvalJsonExprPath()). Do the same when + * coercing a jsonb ON ERROR or ON EMPTY DEFAULT expression, so that a + * string DEFAULT keeps its quotes in that case. + */ + if (jsexpr->op == JSON_VALUE_OP && + (jsexpr->returning->typid == JSONOID || + jsexpr->returning->typid == JSONBOID)) + behavior_omit_quotes = false; + jsestate->jsexpr = jsexpr; /* @@ -4970,7 +4982,7 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state, /* Step to coerce the ON ERROR expression if needed */ if (jsexpr->on_error->coerce) ExecInitJsonCoercion(state, jsexpr->returning, escontext, - jsexpr->omit_quotes, false, + behavior_omit_quotes, false, resv, resnull); /* @@ -5030,7 +5042,7 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state, /* Step to coerce the ON EMPTY expression if needed */ if (jsexpr->on_empty->coerce) ExecInitJsonCoercion(state, jsexpr->returning, escontext, - jsexpr->omit_quotes, false, + behavior_omit_quotes, false, resv, resnull); /* diff --git a/src/test/regress/expected/sqljson_queryfuncs.out b/src/test/regress/expected/sqljson_queryfuncs.out index 19db825c..add4aeda 100644 --- a/src/test/regress/expected/sqljson_queryfuncs.out +++ b/src/test/regress/expected/sqljson_queryfuncs.out @@ -284,6 +284,32 @@ SELECT JSON_VALUE(jsonb '"\"aaa\""', '$' RETURNING jsonb); "\"aaa\"" (1 row) +-- a jsonb string DEFAULT keeps its quotes when returning json, like a +-- string found by the path +SELECT JSON_VALUE(jsonb '{}', '$.a' RETURNING json DEFAULT '"aaa"'::jsonb ON EMPTY); + json_value +------------ + "aaa" +(1 row) + +SELECT JSON_VALUE(jsonb '{}', '$.a' RETURNING json DEFAULT '"123"'::jsonb ON EMPTY); + json_value +------------ + "123" +(1 row) + +SELECT JSON_VALUE(jsonb '[1]', '$' RETURNING json DEFAULT '"aaa"'::jsonb ON ERROR); + json_value +------------ + "aaa" +(1 row) + +SELECT JSON_VALUE(jsonb '{}', '$.a' RETURNING text DEFAULT '"aaa"'::jsonb ON EMPTY); + json_value +------------ + aaa +(1 row) + SELECT JSON_VALUE(jsonb '"aaa"', '$' RETURNING int); json_value ------------ diff --git a/src/test/regress/sql/sqljson_queryfuncs.sql b/src/test/regress/sql/sqljson_queryfuncs.sql index 5c675d98..7fed5d2a 100644 --- a/src/test/regress/sql/sqljson_queryfuncs.sql +++ b/src/test/regress/sql/sqljson_queryfuncs.sql @@ -62,6 +62,12 @@ SELECT JSON_VALUE(jsonb '"aaa"', '$' RETURNING json ERROR ON ERROR); SELECT JSON_VALUE(jsonb '"aaa"', '$' RETURNING jsonb ERROR ON ERROR); SELECT JSON_VALUE(jsonb '"\"aaa\""', '$' RETURNING json); SELECT JSON_VALUE(jsonb '"\"aaa\""', '$' RETURNING jsonb); +-- a jsonb string DEFAULT keeps its quotes when returning json, like a +-- string found by the path +SELECT JSON_VALUE(jsonb '{}', '$.a' RETURNING json DEFAULT '"aaa"'::jsonb ON EMPTY); +SELECT JSON_VALUE(jsonb '{}', '$.a' RETURNING json DEFAULT '"123"'::jsonb ON EMPTY); +SELECT JSON_VALUE(jsonb '[1]', '$' RETURNING json DEFAULT '"aaa"'::jsonb ON ERROR); +SELECT JSON_VALUE(jsonb '{}', '$.a' RETURNING text DEFAULT '"aaa"'::jsonb ON EMPTY); SELECT JSON_VALUE(jsonb '"aaa"', '$' RETURNING int); SELECT JSON_VALUE(jsonb '"aaa"', '$' RETURNING int ERROR ON ERROR); SELECT JSON_VALUE(jsonb '"aaa"', '$' RETURNING int DEFAULT 111 ON ERROR); -- 2.39.5