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

Commit90d68c7

Browse files
author
Nikita Glukhov
committed
Add generic json support in user functions
1 parent113e5ce commit90d68c7

File tree

5 files changed

+235
-8
lines changed

5 files changed

+235
-8
lines changed

‎src/backend/tsearch/to_tsany.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,11 @@ Datum
314314
json_to_tsvector_byid(PG_FUNCTION_ARGS)
315315
{
316316
OidcfgId=PG_GETARG_OID(0);
317+
#ifndefJSON_GENERIC
317318
text*json=PG_GETARG_TEXT_P(1);
319+
#else
320+
Jsonb*json=DatumGetJsont(PG_GETARG_DATUM(1));
321+
#endif
318322
TSVectorBuildStatestate;
319323
ParsedText*prs= (ParsedText*)palloc(sizeof(ParsedText));
320324

@@ -323,7 +327,11 @@ json_to_tsvector_byid(PG_FUNCTION_ARGS)
323327
state.cfgId=cfgId;
324328
state.prs=prs;
325329

330+
#ifndefJSON_GENERIC
326331
iterate_json_string_values(json,&state, (JsonIterateStringValuesAction)add_to_tsvector);
332+
#else
333+
iterate_jsonb_string_values(json,&state, (JsonIterateStringValuesAction)add_to_tsvector);
334+
#endif
327335

328336
PG_FREE_IF_COPY(json,1);
329337
if (state.result==NULL)

‎src/backend/tsearch/wparser.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ ts_headline_jsonb_byid_opt(PG_FUNCTION_ARGS)
385385
TSQueryquery=PG_GETARG_TSQUERY(2);
386386
text*opt= (PG_NARGS()>3&&PG_GETARG_POINTER(3)) ?PG_GETARG_TEXT_P(3) :NULL;
387387
JsonTransformStringValuesActionaction= (JsonTransformStringValuesAction)headline_json_value;
388-
388+
JsonValuejv;
389389
HeadlineParsedTextprs;
390390
HeadlineJsonState*state=palloc0(sizeof(HeadlineJsonState));
391391

@@ -409,6 +409,9 @@ ts_headline_jsonb_byid_opt(PG_FUNCTION_ARGS)
409409

410410
out=transform_jsonb_string_values(jb,state,action);
411411

412+
/* flatten result to jsonb before jb freeing */
413+
out=DatumGetJsonb(PointerGetDatum(JsonValueToJsonb(JsonToJsonValue(out,&jv))));
414+
412415
PG_FREE_IF_COPY(jb,1);
413416
PG_FREE_IF_COPY(query,2);
414417
if (opt)
@@ -456,7 +459,11 @@ ts_headline_jsonb_opt(PG_FUNCTION_ARGS)
456459
Datum
457460
ts_headline_json_byid_opt(PG_FUNCTION_ARGS)
458461
{
462+
#ifndefJSON_GENERIC
459463
text*json=PG_GETARG_TEXT_P(1);
464+
#else
465+
Jsonb*json=DatumGetJsont(PG_GETARG_DATUM(1));
466+
#endif
460467
TSQueryquery=PG_GETARG_TSQUERY(2);
461468
text*opt= (PG_NARGS()>3&&PG_GETARG_POINTER(3)) ?PG_GETARG_TEXT_P(3) :NULL;
462469
text*out;
@@ -483,7 +490,18 @@ ts_headline_json_byid_opt(PG_FUNCTION_ARGS)
483490
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
484491
errmsg("text search parser does not support headline creation")));
485492

493+
#ifndefJSON_GENERIC
486494
out=transform_json_string_values(json,state,action);
495+
#else
496+
{
497+
Jsonb*jsonb=transform_jsonb_string_values(json,state,action);
498+
char*str=JsonToCString(&jsonb->root);
499+
500+
out=cstring_to_text(str);
501+
502+
pfree(str);
503+
}
504+
#endif
487505

488506
PG_FREE_IF_COPY(json,1);
489507
PG_FREE_IF_COPY(query,2);

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,65 @@
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
14+
15+
#defineJSON_C
16+
17+
#definejsonb_in_json_in
18+
#definejsonb_recv_json_recv
19+
20+
#definejsonb_outjson_out
21+
#definejsonb_sendjson_send
22+
#definearray_to_jsonbarray_to_json
23+
#definerow_to_jsonbrow_to_json
24+
#definerow_to_jsonb_prettyrow_to_json_pretty
25+
#defineto_jsonbto_json
26+
#definejsonb_agg_transfnjson_agg_transfn
27+
#definejsonb_agg_finalfnjson_agg_finalfn
28+
#definejsonb_object_agg_transfnjson_object_agg_transfn
29+
#definejsonb_object_agg_finalfnjson_object_agg_finalfn
30+
#definejsonb_build_objectjson_build_object
31+
#definejsonb_build_object_noargsjson_build_object_noargs
32+
#definejsonb_build_arrayjson_build_array
33+
#definejsonb_build_array_noargsjson_build_array_noargs
34+
#definejsonb_objectjson_object
35+
#definejsonb_object_two_argjson_object_two_arg
36+
#definejsonb_typeofjson_typeof
37+
38+
#definejsonb_array_elementjson_array_element
39+
#definejsonb_array_element_textjson_array_element_text
40+
#definejsonb_array_elementsjson_array_elements
41+
#definejsonb_array_elements_textjson_array_elements_text
42+
#definejsonb_array_lengthjson_array_length
43+
#definejsonb_canonicaljson_canonical
44+
#definejsonb_concatjson_concat
45+
#definejsonb_deletejson_delete
46+
#definejsonb_delete_idxjson_delete_idx
47+
#definejsonb_delete_pathjson_delete_path
48+
#definejsonb_delete_arrayjson_delete_array
49+
#definejsonb_eachjson_each
50+
#definejsonb_each_textjson_each_text
51+
#definejsonb_insertjson_insert
52+
#definejsonb_extract_pathjson_extract_path
53+
#definejsonb_extract_path_textjson_extract_path_text
54+
#definejsonb_object_fieldjson_object_field
55+
#definejsonb_object_field_textjson_object_field_text
56+
#definejsonb_object_keysjson_object_keys
57+
#definejsonb_populate_recordjson_populate_record
58+
#definejsonb_populate_recordsetjson_populate_recordset
59+
#definejsonb_prettyjson_pretty
60+
#definejsonb_setjson_set
61+
#definejsonb_strip_nullsjson_strip_nulls
62+
#definejsonb_to_recordjson_to_record
63+
#definejsonb_to_recordsetjson_to_recordset
64+
65+
#defineJsonxContainerOps(&jsontContainerOps)
66+
#ifdefJSON_FLATTEN_INTO_TARGET
67+
# defineJsonxGetDatum(json)\
68+
PointerGetDatum(cstring_to_text(JsonToCString(JsonRoot(json))))
69+
#else
70+
# defineJsonxGetDatum(json)JsontGetDatum(json)
71+
#endif
72+
1473
#include"postgres.h"
1574

1675
#include"access/htup_details.h"
@@ -33,6 +92,15 @@
3392
#include"utils/typcache.h"
3493
#include"utils/syscache.h"
3594

95+
#ifndefJSON_FLATTEN_INTO_TARGET
96+
staticinlineDatum
97+
JsontGetDatum(Json*json)
98+
{
99+
json->is_json= true;
100+
returnJsonGetEOHDatum(json);
101+
}
102+
#endif
103+
36104
/*
37105
* The context of the parser is maintained by the recursive descent
38106
* mechanism, but is passed explicitly to the error reporting routine
@@ -102,9 +170,11 @@ static void json_categorize_type(Oid typoid,
102170
staticvoiddatum_to_json(Datumval,boolis_null,StringInforesult,
103171
JsonTypeCategorytcategory,Oidoutfuncoid,
104172
boolkey_scalar);
173+
#ifndefJSON_GENERIC
105174
staticvoidadd_json(Datumval,boolis_null,StringInforesult,
106175
Oidval_type,boolkey_scalar);
107176
statictext*catenate_stringinfo_string(StringInfobuffer,constchar*addon);
177+
#endif
108178

109179
/* the null action object used for pure validation */
110180
staticJsonSemActionnullSemAction=
@@ -241,6 +311,7 @@ json_in(PG_FUNCTION_ARGS)
241311
PG_RETURN_TEXT_P(result);
242312
}
243313

314+
#ifndefJSON_GENERIC
244315
/*
245316
* Output.
246317
*/
@@ -266,6 +337,7 @@ json_send(PG_FUNCTION_ARGS)
266337
pq_sendtext(&buf,VARDATA_ANY(t),VARSIZE_ANY_EXHDR(t));
267338
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
268339
}
340+
#endif
269341

270342
/*
271343
* Binary receive.
@@ -1747,6 +1819,8 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
17471819
ReleaseTupleDesc(tupdesc);
17481820
}
17491821

1822+
#ifndefJSON_GENERIC
1823+
17501824
/*
17511825
* Append JSON text for "val" to "result".
17521826
*
@@ -1778,6 +1852,8 @@ add_json(Datum val, bool is_null, StringInfo result,
17781852
datum_to_json(val,is_null,result,tcategory,outfuncoid,key_scalar);
17791853
}
17801854

1855+
#endif
1856+
17811857
/*
17821858
* SQL function array_to_json(row)
17831859
*/
@@ -1844,6 +1920,8 @@ row_to_json_pretty(PG_FUNCTION_ARGS)
18441920
PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data,result->len));
18451921
}
18461922

1923+
#ifndefJSON_GENERIC
1924+
18471925
/*
18481926
* SQL function to_json(anyvalue)
18491927
*/
@@ -2427,7 +2505,7 @@ json_object_two_arg(PG_FUNCTION_ARGS)
24272505

24282506
PG_RETURN_TEXT_P(rval);
24292507
}
2430-
2508+
#endif
24312509

24322510
/*
24332511
* Produce a JSON string literal, properly escaping characters in the text.
@@ -2474,6 +2552,7 @@ escape_json(StringInfo buf, const char *str)
24742552
appendStringInfoCharMacro(buf,'"');
24752553
}
24762554

2555+
#ifndefJSON_GENERIC
24772556
/*
24782557
* SQL function json_typeof(json) -> text
24792558
*
@@ -2528,6 +2607,12 @@ json_typeof(PG_FUNCTION_ARGS)
25282607

25292608
PG_RETURN_TEXT_P(cstring_to_text(type));
25302609
}
2610+
2611+
#endif
2612+
2613+
#include"jsonb.c"
2614+
#include"jsonfuncs.c"
2615+
25312616
externJsonContainerOpsjsontContainerOps;
25322617

25332618
staticvoid

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ static void jsonb_in_object_end(void *pstate);
7474
staticvoidjsonb_in_array_start(void*pstate);
7575
staticvoidjsonb_in_array_end(void*pstate);
7676
staticvoidjsonb_in_object_field_start(void*pstate,char*fname,boolisnull);
77+
#ifndefJSON_C
7778
staticvoidjsonb_put_escaped_value(StringInfoout,JsonbValue*scalarVal);
79+
#endif
7880
staticvoidjsonb_in_scalar(void*pstate,char*token,JsonTokenTypetokentype);
7981
staticvoidjsonb_categorize_type(Oidtypoid,
8082
JsonbTypeCategory*tcategory,
@@ -92,9 +94,11 @@ static void datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
9294
boolkey_scalar);
9395
staticvoidadd_jsonb(Datumval,boolis_null,JsonbInState*result,
9496
Oidval_type,boolkey_scalar);
97+
#ifndefJSON_C
9598
staticchar*JsonbToCStringWorker(StringInfoout,JsonbContainer*in,
9699
intestimated_len,JsonFormatformat);
97100
staticvoidadd_indent(StringInfoout,boolindent,intlevel);
101+
#endif
98102

99103
/*
100104
* jsonb type input function
@@ -222,6 +226,7 @@ jsonb_typeof(PG_FUNCTION_ARGS)
222226
PG_RETURN_TEXT_P(cstring_to_text(result));
223227
}
224228

229+
#ifndefJSON_C
225230
JsonbValue*
226231
JsonValueFromCString(char*json,intlen)
227232
{
@@ -247,6 +252,7 @@ JsonValueFromCString(char *json, int len)
247252
/* after parsing, the item member has the composed jsonb structure */
248253
returnstate.res;
249254
}
255+
#endif
250256

251257
/*
252258
* jsonb_from_cstring
@@ -307,6 +313,7 @@ jsonb_in_object_field_start(void *pstate, char *fname, bool isnull)
307313
_state->res=pushJsonbValue(&_state->parseState,WJB_KEY,&v);
308314
}
309315

316+
#ifndefJSON_C
310317
staticvoid
311318
jsonb_put_escaped_value(StringInfoout,JsonbValue*scalarVal)
312319
{
@@ -333,7 +340,7 @@ jsonb_put_escaped_value(StringInfo out, JsonbValue *scalarVal)
333340
elog(ERROR,"unknown jsonb scalar type");
334341
}
335342
}
336-
343+
#endif
337344

338345
/*
339346
* For jsonb we always want the de-escaped value - that's what's in token
@@ -386,6 +393,7 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
386393
_state->res=pushScalarJsonbValue(&_state->parseState,&v, false);
387394
}
388395

396+
#ifndefJSON_C
389397
/*
390398
* JsonbToCString
391399
* Converts jsonb value to a C-string.
@@ -559,7 +567,7 @@ add_indent(StringInfo out, bool indent, int level)
559567
appendBinaryStringInfo(out," ",4);
560568
}
561569
}
562-
570+
#endif
563571

564572
/*
565573
* Determine how we want to render values of a given type in datum_to_jsonb.
@@ -833,7 +841,10 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
833841
}
834842
break;
835843
caseJSONBTYPE_JSONCAST:
844+
#ifndefJSON_GENERIC
836845
caseJSONBTYPE_JSON:
846+
847+
#endif
837848
{
838849
/* parse the json right into the existing result object */
839850
JsonLexContext*lex;
@@ -857,9 +868,18 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
857868

858869
}
859870
break;
871+
#ifdefJSON_GENERIC
872+
caseJSONBTYPE_JSON:
873+
#endif
860874
caseJSONBTYPE_JSONB:
861875
{
876+
#ifndefJSON_GENERIC
862877
Jsonb*jsonb=DatumGetJsonb(val);
878+
#else
879+
Jsonb*jsonb=tcategory==JSONBTYPE_JSON
880+
?DatumGetJsont(val)
881+
:DatumGetJsonb(val);
882+
#endif
863883
JsonbIterator*it;
864884

865885
it=JsonbIteratorInit(&jsonb->root);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp