Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 94c745e

Browse files
committed
Fix two-argument jsonb_object when called with empty arrays
Some over-eager copy-and-pasting on my part resulted in a nonsense result being returned in this case. I have adopted the same pattern for handling this case as is used in the one argument form of the function, i.e. we just skip over the code that adds values to the object. Diagnosis and patch from Michael Paquier, although not quite his solution. Fixes bug #13936. Backpatch to 9.5 where jsonb_object was introduced.
1 parent 88aca56 commit 94c745e

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

src/backend/utils/adt/jsonb.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ jsonb_object_two_arg(PG_FUNCTION_ARGS)
14551455
errmsg("wrong number of array subscripts")));
14561456

14571457
if (nkdims == 0)
1458-
PG_RETURN_DATUM(CStringGetTextDatum("{}"));
1458+
goto close_object;
14591459

14601460
deconstruct_array(key_array,
14611461
TEXTOID, -1, false, 'i',
@@ -1509,13 +1509,14 @@ jsonb_object_two_arg(PG_FUNCTION_ARGS)
15091509
(void) pushJsonbValue(&result.parseState, WJB_VALUE, &v);
15101510
}
15111511

1512-
result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
1513-
15141512
pfree(key_datums);
15151513
pfree(key_nulls);
15161514
pfree(val_datums);
15171515
pfree(val_nulls);
15181516

1517+
close_object:
1518+
result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
1519+
15191520
PG_RETURN_POINTER(JsonbValueToJsonb(result.res));
15201521
}
15211522

src/test/regress/expected/json.out

+14
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,20 @@ INSERT INTO foo VALUES (999999, NULL, 'bar');
15101510
SELECT json_object_agg(name, type) FROM foo;
15111511
ERROR: field name must not be null
15121512
-- json_object
1513+
-- empty object, one dimension
1514+
SELECT json_object('{}');
1515+
json_object
1516+
-------------
1517+
{}
1518+
(1 row)
1519+
1520+
-- empty object, two dimensions
1521+
SELECT json_object('{}', '{}');
1522+
json_object
1523+
-------------
1524+
{}
1525+
(1 row)
1526+
15131527
-- one dimension
15141528
SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
15151529
json_object

src/test/regress/expected/jsonb.out

+14
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,20 @@ INSERT INTO foo VALUES (999999, NULL, 'bar');
14101410
SELECT jsonb_object_agg(name, type) FROM foo;
14111411
ERROR: field name must not be null
14121412
-- jsonb_object
1413+
-- empty object, one dimension
1414+
SELECT jsonb_object('{}');
1415+
jsonb_object
1416+
--------------
1417+
{}
1418+
(1 row)
1419+
1420+
-- empty object, two dimensions
1421+
SELECT jsonb_object('{}', '{}');
1422+
jsonb_object
1423+
--------------
1424+
{}
1425+
(1 row)
1426+
14131427
-- one dimension
14141428
SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
14151429
jsonb_object

src/test/regress/sql/json.sql

+6
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,12 @@ SELECT json_object_agg(name, type) FROM foo;
462462

463463
-- json_object
464464

465+
-- empty object, one dimension
466+
SELECT json_object('{}');
467+
468+
-- empty object, two dimensions
469+
SELECT json_object('{}', '{}');
470+
465471
-- one dimension
466472
SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
467473

src/test/regress/sql/jsonb.sql

+6
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ SELECT jsonb_object_agg(name, type) FROM foo;
352352

353353
-- jsonb_object
354354

355+
-- empty object, one dimension
356+
SELECT jsonb_object('{}');
357+
358+
-- empty object, two dimensions
359+
SELECT jsonb_object('{}', '{}');
360+
355361
-- one dimension
356362
SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
357363

0 commit comments

Comments
 (0)