Skip to content

Commit 4fc6a55

Browse files
committed
SQL/JSON: Respect OMIT QUOTES when RETURNING domains over jsonb
populate_domain() didn't take into account the omit_quotes flag passed down to json_populate_type() by ExecEvalJsonCoercion() and that led to incorrect behavior when the RETURNING type is a domain over jsonb. Fix that by passing the flag by adding a new function parameter to populate_domain(). Reported-by: Jian He <jian.universality@gmail.com> Discussion: https://postgr.es/m/CACJufxEo4sUjKCYtda0_qt9tazqqKPmF1cqhW9KBOUeJFqQd2g@mail.gmail.com Backpatch-through: 17
1 parent 231b7d6 commit 4fc6a55

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/backend/utils/adt/jsonfuncs.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ static void prepare_column_cache(ColumnIOData *column, Oid typid, int32 typmod,
453453
static Datum populate_record_field(ColumnIOData *col, Oid typid, int32 typmod,
454454
const char *colname, MemoryContext mcxt, Datum defaultval,
455455
JsValue *jsv, bool *isnull, Node *escontext,
456-
bool omit_quotes);
456+
bool omit_scalar_quotes);
457457
static RecordIOData *allocate_record_info(MemoryContext mcxt, int ncolumns);
458458
static bool JsObjectGetField(JsObject *obj, char *field, JsValue *jsv);
459459
static void populate_recordset_record(PopulateRecordsetState *state, JsObject *obj);
@@ -470,7 +470,7 @@ static Datum populate_array(ArrayIOData *aio, const char *colname,
470470
Node *escontext);
471471
static Datum populate_domain(DomainIOData *io, Oid typid, const char *colname,
472472
MemoryContext mcxt, JsValue *jsv, bool *isnull,
473-
Node *escontext);
473+
Node *escontext, bool omit_quotes);
474474

475475
/* functions supporting jsonb_delete, jsonb_set and jsonb_concat */
476476
static JsonbValue *IteratorConcat(JsonbIterator **it1, JsonbIterator **it2,
@@ -3218,7 +3218,8 @@ populate_domain(DomainIOData *io,
32183218
MemoryContext mcxt,
32193219
JsValue *jsv,
32203220
bool *isnull,
3221-
Node *escontext)
3221+
Node *escontext,
3222+
bool omit_quotes)
32223223
{
32233224
Datum res;
32243225

@@ -3229,7 +3230,7 @@ populate_domain(DomainIOData *io,
32293230
res = populate_record_field(io->base_io,
32303231
io->base_typid, io->base_typmod,
32313232
colname, mcxt, PointerGetDatum(NULL),
3232-
jsv, isnull, escontext, false);
3233+
jsv, isnull, escontext, omit_quotes);
32333234
Assert(!*isnull || SOFT_ERROR_OCCURRED(escontext));
32343235
}
32353236

@@ -3461,7 +3462,7 @@ populate_record_field(ColumnIOData *col,
34613462

34623463
case TYPECAT_DOMAIN:
34633464
return populate_domain(&col->io.domain, typid, colname, mcxt,
3464-
jsv, isnull, escontext);
3465+
jsv, isnull, escontext, omit_scalar_quotes);
34653466

34663467
default:
34673468
elog(ERROR, "unrecognized type category '%c'", typcat);

src/test/regress/expected/sqljson_queryfuncs.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,21 @@ SELECT JSON_QUERY(jsonb'{"rec": "[1,2]"}', '$.rec' returning int4range keep quot
734734
SELECT JSON_QUERY(jsonb'{"rec": "[1,2]"}', '$.rec' returning int4range keep quotes error on error);
735735
ERROR: malformed range literal: ""[1,2]""
736736
DETAIL: Missing left parenthesis or bracket.
737+
CREATE DOMAIN qf_char_domain AS char(1);
738+
CREATE DOMAIN qf_jsonb_domain AS jsonb;
739+
SELECT JSON_QUERY(jsonb '"1"', '$' RETURNING qf_char_domain OMIT QUOTES ERROR ON ERROR);
740+
json_query
741+
------------
742+
1
743+
(1 row)
744+
745+
SELECT JSON_QUERY(jsonb '"1"', '$' RETURNING qf_jsonb_domain OMIT QUOTES ERROR ON ERROR);
746+
json_query
747+
------------
748+
1
749+
(1 row)
750+
751+
DROP DOMAIN qf_char_domain, qf_jsonb_domain;
737752
SELECT JSON_QUERY(jsonb '[]', '$[*]');
738753
json_query
739754
------------

src/test/regress/sql/sqljson_queryfuncs.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ SELECT JSON_QUERY(jsonb'{"rec": "{1,2,3}"}', '$.rec' returning int[] keep quotes
214214
SELECT JSON_QUERY(jsonb'{"rec": "[1,2]"}', '$.rec' returning int4range omit quotes);
215215
SELECT JSON_QUERY(jsonb'{"rec": "[1,2]"}', '$.rec' returning int4range keep quotes);
216216
SELECT JSON_QUERY(jsonb'{"rec": "[1,2]"}', '$.rec' returning int4range keep quotes error on error);
217+
CREATE DOMAIN qf_char_domain AS char(1);
218+
CREATE DOMAIN qf_jsonb_domain AS jsonb;
219+
SELECT JSON_QUERY(jsonb '"1"', '$' RETURNING qf_char_domain OMIT QUOTES ERROR ON ERROR);
220+
SELECT JSON_QUERY(jsonb '"1"', '$' RETURNING qf_jsonb_domain OMIT QUOTES ERROR ON ERROR);
221+
DROP DOMAIN qf_char_domain, qf_jsonb_domain;
217222

218223
SELECT JSON_QUERY(jsonb '[]', '$[*]');
219224
SELECT JSON_QUERY(jsonb '[]', '$[*]' NULL ON EMPTY);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy