Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit502a383

Browse files
committed
Correctly handle array pseudotypes in to_json and to_jsonb
Columns with array pseudotypes have not been identified as arrays, sothey have been rendered as strings in the json and jsonb conversionroutines. This change allows them to be rendered as json arrays, makingit possible to deal correctly with the anyarray columns in pg_stats.
1 parent4c728f3 commit502a383

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

‎src/backend/utils/adt/json.c

Lines changed: 3 additions & 2 deletions
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-
elseif (type_is_rowtype(typoid))
1403+
elseif (type_is_rowtype(typoid))/* includes RECORDOID */
14031404
*tcategory=JSONTYPE_COMPOSITE;
14041405
else
14051406
{

‎src/backend/utils/adt/jsonb.c

Lines changed: 3 additions & 2 deletions
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-
elseif (type_is_rowtype(typoid))
650+
elseif (type_is_rowtype(typoid))/* includes RECORDOID */
650651
*tcategory=JSONBTYPE_COMPOSITE;
651652
else
652653
{

‎src/test/regress/expected/json.out

Lines changed: 9 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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 dfrom 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

Lines changed: 6 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp