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

Commit 502a383

Browse files
committed
Correctly handle array pseudotypes in to_json and to_jsonb
Columns with array pseudotypes have not been identified as arrays, so they have been rendered as strings in the json and jsonb conversion routines. This change allows them to be rendered as json arrays, making it possible to deal correctly with the anyarray columns in pg_stats.
1 parent 4c728f3 commit 502a383

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

src/backend/utils/adt/json.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1397,9 +1397,10 @@ json_categorize_type(Oid typoid,
13971397

13981398
default:
13991399
/* Check for arrays and composites */
1400-
if (OidIsValid(get_element_type(typoid)))
1400+
if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
1401+
|| typoid == RECORDARRAYOID)
14011402
*tcategory = JSONTYPE_ARRAY;
1402-
else if (type_is_rowtype(typoid))
1403+
else if (type_is_rowtype(typoid)) /* includes RECORDOID */
14031404
*tcategory = JSONTYPE_COMPOSITE;
14041405
else
14051406
{

src/backend/utils/adt/jsonb.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,10 @@ jsonb_categorize_type(Oid typoid,
644644

645645
default:
646646
/* Check for arrays and composites */
647-
if (OidIsValid(get_element_type(typoid)))
647+
if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
648+
|| typoid == RECORDARRAYOID)
648649
*tcategory = JSONBTYPE_ARRAY;
649-
else if (type_is_rowtype(typoid))
650+
else if (type_is_rowtype(typoid)) /* includes RECORDOID */
650651
*tcategory = JSONBTYPE_COMPOSITE;
651652
else
652653
{

src/test/regress/expected/json.out

+9
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,15 @@ SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),
383383
{"f1":[5,6,7,8,9,10]}
384384
(1 row)
385385

386+
-- anyarray column
387+
select to_json(histogram_bounds) histogram_bounds
388+
from pg_stats
389+
where attname = 'tmplname' and tablename = 'pg_pltemplate';
390+
histogram_bounds
391+
---------------------------------------------------------------------------------------
392+
["plperl","plperlu","plpgsql","plpython2u","plpython3u","plpythonu","pltcl","pltclu"]
393+
(1 row)
394+
386395
-- to_json, timestamps
387396
select to_json(timestamp '2014-05-28 12:22:35.614298');
388397
to_json

src/test/regress/expected/jsonb.out

+9
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,15 @@ SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
279279
[{"a": 1},{"b": [2, 3]}]
280280
(1 row)
281281

282+
-- anyarray column
283+
select to_jsonb(histogram_bounds) histogram_bounds
284+
from pg_stats
285+
where attname = 'tmplname' and tablename = 'pg_pltemplate';
286+
histogram_bounds
287+
----------------------------------------------------------------------------------------------
288+
["plperl", "plperlu", "plpgsql", "plpython2u", "plpython3u", "plpythonu", "pltcl", "pltclu"]
289+
(1 row)
290+
282291
-- to_jsonb, timestamps
283292
select to_jsonb(timestamp '2014-05-28 12:22:35.614298');
284293
to_jsonb

src/test/regress/sql/json.sql

+6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ FROM rows q;
102102

103103
SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
104104

105+
-- anyarray column
106+
107+
select to_json(histogram_bounds) histogram_bounds
108+
from pg_stats
109+
where attname = 'tmplname' and tablename = 'pg_pltemplate';
110+
105111
-- to_json, timestamps
106112

107113
select to_json(timestamp '2014-05-28 12:22:35.614298');

src/test/regress/sql/jsonb.sql

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ SELECT ' '::jsonb; -- ERROR, no value
6262
-- make sure jsonb is passed through json generators without being escaped
6363
SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
6464

65+
-- anyarray column
66+
67+
select to_jsonb(histogram_bounds) histogram_bounds
68+
from pg_stats
69+
where attname = 'tmplname' and tablename = 'pg_pltemplate';
70+
6571
-- to_jsonb, timestamps
6672

6773
select to_jsonb(timestamp '2014-05-28 12:22:35.614298');

0 commit comments

Comments
 (0)