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

Commitf9c6d72

Browse files
committed
Cleanup around json_to_record/json_to_recordset
Set function parameter names and defaults. Add jsonb versions (which thecode already provided for so the actual new code is trivial). Add jsonbregression tests and docs.Bump catalog version (which I apparently forgot to do when jsonb wascommitted).
1 parent28475f8 commitf9c6d72

File tree

9 files changed

+99
-19
lines changed

9 files changed

+99
-19
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10443,9 +10443,15 @@ table2-mapping
1044310443
<indexterm>
1044410444
<primary>json_to_record</primary>
1044510445
</indexterm>
10446+
<indexterm>
10447+
<primary>jsonb_to_record</primary>
10448+
</indexterm>
1044610449
<indexterm>
1044710450
<primary>json_to_recordset</primary>
1044810451
</indexterm>
10452+
<indexterm>
10453+
<primary>jsonb_to_recordset</primary>
10454+
</indexterm>
1044910455

1045010456
<table id="functions-json-processing-table">
1045110457
<title>JSON Processing Functions</title>
@@ -10649,9 +10655,9 @@ table2-mapping
1064910655
<entry><literal>number</literal></entry>
1065010656
</row>
1065110657
<row>
10652-
<entry>
10653-
<literal>json_to_record(json, nested_as_text bool)</literal>
10654-
</entry>
10658+
<entry><para><literal>json_to_record(json [, nested_as_text bool=false])</literal>
10659+
</para><para><literal>jsonb_to_record(jsonb [, nested_as_text bool=false])</literal>
10660+
</para></entry>
1065510661
<entry><type>record</type></entry>
1065610662
<entry>
1065710663
Returns an arbitrary record from a JSON object. As with all functions
@@ -10670,9 +10676,9 @@ table2-mapping
1067010676
</entry>
1067110677
</row>
1067210678
<row>
10673-
<entry>
10674-
<literal>json_to_recordset(json, nested_as_text bool)</literal>
10675-
</entry>
10679+
<entry><para><literal>json_to_recordset(json [, nested_as_text bool=false])</literal>
10680+
</para><para><literal>jsonb_to_recordset(jsonb [, nested_as_text bool=false])</literal>
10681+
</para></entry>
1067610682
<entry><type>setof record</type></entry>
1067710683
<entry>
1067810684
Returns an arbitrary set of records from a JSON object. As with

‎src/backend/catalog/system_views.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,22 @@ CREATE OR REPLACE FUNCTION
833833
jsonb_populate_recordset(base anyelement, from_json jsonb, use_json_as_textboolean DEFAULT false)
834834
RETURNS SETOF anyelement LANGUAGE internal STABLE ROWS100AS'jsonb_populate_recordset';
835835

836+
CREATEOR REPLACE FUNCTION
837+
json_to_record(from_json json, nested_as_textboolean DEFAULT false)
838+
RETURNS record LANGUAGE internal STABLEAS'json_to_record';
839+
840+
CREATEOR REPLACE FUNCTION
841+
json_to_recordset(from_json json, nested_as_textboolean DEFAULT false)
842+
RETURNS SETOF record LANGUAGE internal STABLE ROWS100AS'json_to_recordset';
843+
844+
CREATEOR REPLACE FUNCTION
845+
jsonb_to_record(from_json jsonb, nested_as_textboolean DEFAULT false)
846+
RETURNS record LANGUAGE internal STABLEAS'jsonb_to_record';
847+
848+
CREATEOR REPLACE FUNCTION
849+
jsonb_to_recordset(from_json jsonb, nested_as_textboolean DEFAULT false)
850+
RETURNS SETOF record LANGUAGE internal STABLE ROWS100AS'jsonb_to_recordset';
851+
836852
CREATE OR REPLACEFUNCTIONpg_logical_slot_get_changes(
837853
IN slotname name,IN upto_lsn pg_lsn,IN upto_nchangesint, VARIADIC optionstext[] DEFAULT'{}',
838854
OUT location pg_lsn, OUT xid xid, OUT datatext)

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,12 @@ jsonb_populate_record(PG_FUNCTION_ARGS)
20142014
returnpopulate_record_worker(fcinfo, true);
20152015
}
20162016

2017+
Datum
2018+
jsonb_to_record(PG_FUNCTION_ARGS)
2019+
{
2020+
returnpopulate_record_worker(fcinfo, false);
2021+
}
2022+
20172023
Datum
20182024
json_populate_record(PG_FUNCTION_ARGS)
20192025
{
@@ -2449,6 +2455,24 @@ jsonb_populate_recordset(PG_FUNCTION_ARGS)
24492455
returnpopulate_recordset_worker(fcinfo, true);
24502456
}
24512457

2458+
Datum
2459+
jsonb_to_recordset(PG_FUNCTION_ARGS)
2460+
{
2461+
returnpopulate_recordset_worker(fcinfo, false);
2462+
}
2463+
2464+
Datum
2465+
json_populate_recordset(PG_FUNCTION_ARGS)
2466+
{
2467+
returnpopulate_recordset_worker(fcinfo, true);
2468+
}
2469+
2470+
Datum
2471+
json_to_recordset(PG_FUNCTION_ARGS)
2472+
{
2473+
returnpopulate_recordset_worker(fcinfo, false);
2474+
}
2475+
24522476
staticvoid
24532477
make_row_from_rec_and_jsonb(Jsonb*element,PopulateRecordsetState*state)
24542478
{
@@ -2571,18 +2595,6 @@ make_row_from_rec_and_jsonb(Jsonb * element, PopulateRecordsetState *state)
25712595
tuplestore_puttuple(state->tuple_store,rettuple);
25722596
}
25732597

2574-
Datum
2575-
json_populate_recordset(PG_FUNCTION_ARGS)
2576-
{
2577-
returnpopulate_recordset_worker(fcinfo, true);
2578-
}
2579-
2580-
Datum
2581-
json_to_recordset(PG_FUNCTION_ARGS)
2582-
{
2583-
returnpopulate_recordset_worker(fcinfo, false);
2584-
}
2585-
25862598
/*
25872599
* common worker for json_populate_recordset() and json_to_recordset()
25882600
*/

‎src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO201403121
56+
#defineCATALOG_VERSION_NO201403261
5757

5858
#endif

‎src/include/catalog/pg_proc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,6 +4546,10 @@ DATA(insert OID = 3209 ( jsonb_populate_record PGNSP PGUID 12 1 0 0 0 f f f
45464546
DESCR("get record fields from a jsonb object");
45474547
DATA(insertOID=3475 (jsonb_populate_recordsetPGNSPPGUID12110000fffffts302283"2283 3802 16"_null__null__null__null_jsonb_populate_recordset_null__null__null_ ));
45484548
DESCR("get set of records with fields from a jsonb array of objects");
4549+
DATA(insertOID=3490 (jsonb_to_recordPGNSPPGUID121000ffffffs202249"3802 16"_null__null__null__null_jsonb_to_record_null__null__null_ ));
4550+
DESCR("get record fields from a json object");
4551+
DATA(insertOID=3491 (jsonb_to_recordsetPGNSPPGUID12110000fffffts202249"3802 16"_null__null__null__null_jsonb_to_recordset_null__null__null_ ));
4552+
DESCR("get set of records with fields from a json array of objects");
45494553
DATA(insertOID=3210 (jsonb_typeofPGNSPPGUID121000fffftfi1025"3802"_null__null__null__null_jsonb_typeof_null__null__null_ ));
45504554
DESCR("get the type of a jsonb value");
45514555
DATA(insertOID=4038 (jsonb_nePGNSPPGUID121000fffftfi2016"3802 3802"_null__null__null__null_jsonb_ne_null__null__null_ ));

‎src/include/utils/json.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,7 @@ extern Datum jsonb_array_elements_text(PG_FUNCTION_ARGS);
7878
externDatumjsonb_array_elements(PG_FUNCTION_ARGS);
7979
externDatumjsonb_populate_record(PG_FUNCTION_ARGS);
8080
externDatumjsonb_populate_recordset(PG_FUNCTION_ARGS);
81+
externDatumjsonb_to_record(PG_FUNCTION_ARGS);
82+
externDatumjsonb_to_recordset(PG_FUNCTION_ARGS);
8183

8284
#endif/* JSON_H */

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,22 @@ SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' AS not_unescaped;
14301430
null \u0000 escape
14311431
(1 row)
14321432

1433+
-- jsonb_to_record and jsonb_to_recordset
1434+
select * from jsonb_to_record('{"a":1,"b":"foo","c":"bar"}',true)
1435+
as x(a int, b text, d text);
1436+
a | b | d
1437+
---+-----+---
1438+
1 | foo |
1439+
(1 row)
1440+
1441+
select * from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]',false)
1442+
as x(a int, b text, c boolean);
1443+
a | b | c
1444+
---+-----+---
1445+
1 | foo |
1446+
2 | bar | t
1447+
(2 rows)
1448+
14331449
-- indexing
14341450
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
14351451
count

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,22 @@ SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' AS not_unescaped;
14301430
null \u0000 escape
14311431
(1 row)
14321432

1433+
-- jsonb_to_record and jsonb_to_recordset
1434+
select * from jsonb_to_record('{"a":1,"b":"foo","c":"bar"}',true)
1435+
as x(a int, b text, d text);
1436+
a | b | d
1437+
---+-----+---
1438+
1 | foo |
1439+
(1 row)
1440+
1441+
select * from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]',false)
1442+
as x(a int, b text, c boolean);
1443+
a | b | c
1444+
---+-----+---
1445+
1 | foo |
1446+
2 | bar | t
1447+
(2 rows)
1448+
14331449
-- indexing
14341450
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
14351451
count

‎src/test/regress/sql/jsonb.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' AS correct_in_utf8;
319319
SELECT jsonb'{ "a": "dollar\u0024 character" }'->>'a'AS correct_everyWHERE;
320320
SELECT jsonb'{ "a": "null\u0000 escape" }'->>'a'AS not_unescaped;
321321

322+
-- jsonb_to_record and jsonb_to_recordset
323+
324+
select*from jsonb_to_record('{"a":1,"b":"foo","c":"bar"}',true)
325+
as x(aint, btext, dtext);
326+
327+
select*from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]',false)
328+
as x(aint, btext, cboolean);
329+
322330
-- indexing
323331
SELECTcount(*)FROM testjsonbWHERE j @>'{"wait":null}';
324332
SELECTcount(*)FROM testjsonbWHERE j @>'{"wait":"CC"}';

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp