Skip to content

Commit 68cff23

Browse files
committed
Make edge-case behavior of jsonb_populate_record match json_populate_record
json_populate_record throws an error if asked to convert a JSON scalar or array into a composite type. jsonb_populate_record was returning a record full of NULL fields instead. It seems better to make it throw an error for this case as well. Nikita Glukhov Discussion: https://postgr.es/m/fbd1d566-bba0-a3de-d6d0-d3b1d7c24ff2@postgrespro.ru
1 parent e45c5be commit 68cff23

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/backend/utils/adt/jsonfuncs.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2682,9 +2682,24 @@ JsValueToJsObject(JsValue *jsv, JsObject *jso)
26822682

26832683
if (jbv->type == jbvBinary &&
26842684
JsonContainerIsObject(jbv->val.binary.data))
2685+
{
26852686
jso->val.jsonb_cont = jbv->val.binary.data;
2687+
}
26862688
else
2687-
jso->val.jsonb_cont = NULL;
2689+
{
2690+
bool is_scalar;
2691+
2692+
is_scalar = IsAJsonbScalar(jbv) ||
2693+
(jbv->type == jbvBinary &&
2694+
JsonContainerIsScalar(jbv->val.binary.data));
2695+
ereport(ERROR,
2696+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2697+
is_scalar
2698+
? errmsg("cannot call %s on a scalar",
2699+
"populate_composite")
2700+
: errmsg("cannot call %s on an array",
2701+
"populate_composite")));
2702+
}
26882703
}
26892704
}
26902705

src/test/regress/expected/jsonb.out

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,17 +2276,9 @@ SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": ["aaa", null, [1, 2
22762276
(1 row)
22772277

22782278
SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": 123}') q;
2279-
rec
2280-
------
2281-
(,,)
2282-
(1 row)
2283-
2279+
ERROR: cannot call populate_composite on a scalar
22842280
SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": [1, 2]}') q;
2285-
rec
2286-
------
2287-
(,,)
2288-
(1 row)
2289-
2281+
ERROR: cannot call populate_composite on an array
22902282
SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
22912283
rec
22922284
-----------------------------------
@@ -2303,11 +2295,7 @@ SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": 123}') q;
23032295
ERROR: expected json array
23042296
HINT: see the value of key "reca"
23052297
SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [1, 2]}') q;
2306-
reca
2307-
-----------------
2308-
{"(,,)","(,,)"}
2309-
(1 row)
2310-
2298+
ERROR: cannot call populate_composite on a scalar
23112299
SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
23122300
reca
23132301
--------------------------------------------------------

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