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

Commitc2cd19a

Browse files
author
Nikita Glukhov
committed
Add uniquification for binary jsons in JsonbGetDatum()
1 parentd1ddafc commitc2cd19a

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#definejsonb_to_recordsetjson_to_recordset
6767

6868
#defineJsonxContainerOps(&jsontContainerOps)
69+
#defineJsonxGetUniquified(json)(json)
6970
#defineJsonxPGetDatum(json)JsontPGetDatum(json)
7071

7172
#include"postgres.h"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#definegin_triconsistent_jsonb_pathgin_triconsistent_json_path
2020

2121
#defineJsonxContainerOps(&jsontContainerOps)
22-
#defineJsonxGetDatum(json)JsontGetDatum(json)
22+
#defineJsonxGetUniquified(json)(json)
23+
#defineJsonxPGetDatum(json)JsontGetDatum(json)
2324

2425
#include"utils/json_generic.h"
2526

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#definejsonb_hash_extendedjson_hash_extended
2525

2626
#defineJsonxContainerOps(&jsontContainerOps)
27-
#defineJsonxGetDatum(json)JsontGetDatum(json)
27+
#defineJsonxGetUniquified(json)(json)
28+
#defineJsonxPGetDatum(json)JsontGetDatum(json)
2829

2930
#include"utils/json_generic.h"
3031

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,9 @@ JsonValueUniquify(JsonValue *res, const JsonValue *val)
21332133
{
21342134
check_stack_depth();
21352135

2136+
if (!res)
2137+
res= (JsonValue*)palloc(sizeof(JsonValue));
2138+
21362139
if (val->type==jbvObject&&
21372140
(!val->val.object.valuesUniquified|| !val->val.object.uniquified))
21382141
{
@@ -2186,6 +2189,31 @@ JsonValueUniquify(JsonValue *res, const JsonValue *val)
21862189
returnres;
21872190
}
21882191

2192+
Json*
2193+
JsonUniquify(Json*json)
2194+
{
2195+
if (JsonRoot(json)->ops==&jsontContainerOps)
2196+
{
2197+
JsonValueval;
2198+
Json*res=JsonValueToJson(JsonValueUnpackBinary(JsonToJsonValue(json,&val)));
2199+
res->is_json=json->is_json;
2200+
returnres;
2201+
}
2202+
elseif (JsonRoot(json)->ops==&jsonvContainerOps)
2203+
{
2204+
constJsonValue*val= (constJsonValue*)JsonRoot(json)->data;
2205+
2206+
if (!JsonValueIsUniquified(val))
2207+
{
2208+
Json*res=JsonValueToJson(JsonValueUniquify(NULL,val));
2209+
res->is_json=json->is_json;
2210+
returnres;
2211+
}
2212+
}
2213+
2214+
returnjson;
2215+
}
2216+
21892217
staticvoid
21902218
jsonbInitContainer(JsonContainerData*jc,JsonbContainer*jbc,intlen)
21912219
{

‎src/include/utils/json_generic.h

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,10 @@ typedef struct Json
115115
*/
116116

117117
#undef JsonbPGetDatum
118-
#defineJsonbPGetDatum(json)JsonFlattenToJsonbDatum(json)
119-
120-
#ifndefJsonxPGetDatum
121-
# ifdefJSON_FLATTEN_INTO_TARGET
122-
# defineJsonxPGetDatum(json)JsonbPGetDatum(json)
123-
# else
124-
# defineJsonxPGetDatum(json)JsonGetEOHDatum(json)
125-
# endif
118+
#ifdefJSON_FLATTEN_INTO_TARGET
119+
# defineJsonbPGetDatum(json)JsonFlattenToJsonbDatum(JsonGetUniquified(json))
120+
#else
121+
# defineJsonbPGetDatum(json)JsonGetEOHDatum(JsonGetUniquified(json))
126122
#endif
127123

128124
#ifdefJSON_FLATTEN_INTO_TARGET
@@ -137,7 +133,13 @@ JsontPGetDatum(Json *json)
137133
}
138134
#endif
139135

140-
#defineJsonGetDatum(json)JsonxPGetDatum(json)
136+
#ifdefJsonxPGetDatum
137+
# defineJsonGetDatum(json)JsonxPGetDatum(json)
138+
#elif defined(JsonxGetUniquified)
139+
# defineJsonGetDatum(json)JsonGetEOHDatum(JsonxGetUniquified(json))
140+
#else
141+
# defineJsonGetDatum(json)JsonbPGetDatum(json)
142+
#endif
141143

142144
#undef DatumGetJsonbP
143145
#defineDatumGetJsonbP(datum)DatumGetJson(datum, &jsonbContainerOps, NULL)
@@ -191,14 +193,15 @@ JsontPGetDatum(Json *json)
191193
((jc)->ops != &jsonvContainerOps || \
192194
JsonValueIsUniquified((JsonValue *) jc->data)))
193195

196+
#defineJsonIsUniquified(json)JsonContainerIsUniquified(JsonRoot(json))
197+
194198
#defineJsonValueIsScalar(jsval)IsAJsonbScalar(jsval)
195199

196200
#defineJsonContainerGetType(jc) ((jc)->ops->type)
197201
#defineJsonContainerGetOpsByType(type) \
198202
((type) == JsonContainerJsont ? &jsontContainerOps : \
199203
(type) == JsonContainerJsonb ? &jsonbContainerOps : NULL)
200204

201-
202205
#ifdefJSONB_UTIL_C
203206
#defineJsonbValueToJsonb JsonValueToJsonb
204207
#else
@@ -270,10 +273,15 @@ JsonIteratorNext(JsonIterator **it, JsonValue *val, bool skipNested)
270273
#definecompareJsonbContainersJsonCompareContainers
271274
#defineequalsJsonbScalarValueJsonValueScalarEquals
272275

276+
externJsonContainerOpsjsonbContainerOps;
277+
externJsonContainerOpsjsontContainerOps;
278+
externJsonContainerOpsjsonvContainerOps;
279+
273280
externJson*DatumGetJson(Datumval,JsonContainerOps*ops,Json*tmp);
274281

275282
externvoidJsonFree(Json*json);
276283
externJson*JsonCopyTemporary(Json*tmp);
284+
externJson*JsonUniquify(Json*json);
277285

278286
#defineJsonContainerAlloc() \
279287
((JsonContainerData *) palloc(sizeof(JsonContainerData)))
@@ -315,6 +323,12 @@ JsonGetNonTemporary(Json *json)
315323
returnJsonIsTemporary(json) ?JsonCopyTemporary(json) :json;
316324
}
317325

326+
staticinlineJson*
327+
JsonGetUniquified(Json*json)
328+
{
329+
returnJsonIsUniquified(json) ?json :JsonUniquify(json);
330+
}
331+
318332
staticinlineJsonValue*
319333
JsonValueInitObject(JsonValue*val,intnPairs,intnPairsAllocated,
320334
booluniquified)
@@ -425,8 +439,4 @@ extern int lengthCompareJsonbStringValue(const void *a, const void *b);
425439
externintlengthCompareJsonbString(constchar*val1,intlen1,
426440
constchar*val2,intlen2);
427441

428-
externJsonContainerOpsjsonbContainerOps;
429-
externJsonContainerOpsjsontContainerOps;
430-
externJsonContainerOpsjsonvContainerOps;
431-
432442
#endif/* UTILS_JSON_GENERIC_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp