-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsonb_gin.c
454 lines (403 loc) · 10.4 KB
/
jsonb_gin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*-------------------------------------------------------------------------
*
* jsonb_gin.c
* GIN support functions for jsonb
*
* Copyright (c) 2014, PostgreSQL Global Development Group
*
*
* IDENTIFICATION
* src/backend/utils/adt/jsonb_gin.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/gin.h"
#include "access/skey.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/jsonb.h"
#define PATH_SEPARATOR ("\0")
typedef struct PathHashStack
{
pg_crc32 hash_state;
struct PathHashStack *next;
} PathHashStack;
static void hash_value(JsonbValue * v, PathHashStack * stack);
static text *makeitem(char *str, int len, char flag);
static text *makeitemFromValue(JsonbValue * v, char flag);
Datum
gin_extract_jsonb(PG_FUNCTION_ARGS)
{
Jsonb *jb = (Jsonb *) PG_GETARG_JSONB(0);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
Datum *entries = NULL;
int total = 2 * JB_ROOT_COUNT(jb);
int i = 0,
r;
JsonbIterator *it;
JsonbValue v;
if (total == 0)
{
*nentries = 0;
PG_RETURN_POINTER(NULL);
}
entries = (Datum *) palloc(sizeof(Datum) * total);
it = JsonbIteratorInit(VARDATA(jb));
while ((r = JsonbIteratorGet(&it, &v, false)) != 0)
{
if (i >= total)
{
total *= 2;
entries = (Datum *) repalloc(entries, sizeof(Datum) * total);
}
switch (r)
{
case WJB_KEY:
/*
* An element of an array is serialized as a key for our
* purposes. This is necessary because array elements are also
* keys.
*/
case WJB_ELEM:
entries[i++] = PointerGetDatum(makeitemFromValue(&v, KEYFLAG));
break;
case WJB_VALUE:
entries[i++] = PointerGetDatum(makeitemFromValue(&v, VALFLAG));
break;
default:
break;
}
}
*nentries = i;
PG_RETURN_POINTER(entries);
}
Datum
gin_extract_jsonb_query(PG_FUNCTION_ARGS)
{
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
StrategyNumber strategy = PG_GETARG_UINT16(2);
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
Datum *entries;
if (strategy == JsonbContainsStrategyNumber)
{
/* Query is a jsonb, so just apply gin_extract_jsonb... */
entries = (Datum *)
DatumGetPointer(DirectFunctionCall2(gin_extract_jsonb,
PG_GETARG_DATUM(0),
PointerGetDatum(nentries)));
/* ... except that "contains {}" requires a full index scan */
if (entries == NULL)
*searchMode = GIN_SEARCH_MODE_ALL;
}
else if (strategy == JsonbExistsStrategyNumber)
{
text *query = PG_GETARG_TEXT_PP(0);
text *item;
*nentries = 1;
entries = (Datum *) palloc(sizeof(Datum));
item = makeitem(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query), KEYFLAG);
entries[0] = PointerGetDatum(item);
}
else if (strategy == JsonbExistsAnyStrategyNumber ||
strategy == JsonbExistsAllStrategyNumber)
{
ArrayType *query = PG_GETARG_ARRAYTYPE_P(0);
Datum *key_datums;
bool *key_nulls;
int key_count;
int i,
j;
text *item;
deconstruct_array(query,
TEXTOID, -1, false, 'i',
&key_datums, &key_nulls, &key_count);
entries = (Datum *) palloc(sizeof(Datum) * key_count);
for (i = 0, j = 0; i < key_count; ++i)
{
/* Nulls in the array are ignored */
if (key_nulls[i])
continue;
item = makeitem(VARDATA(key_datums[i]),
VARSIZE(key_datums[i]) - VARHDRSZ, KEYFLAG);
entries[j++] = PointerGetDatum(item);
}
*nentries = j;
/* ExistsAll with no keys should match everything */
if (j == 0 && strategy == JsonbExistsAllStrategyNumber)
*searchMode = GIN_SEARCH_MODE_ALL;
}
else
{
elog(ERROR, "unrecognized strategy number: %d", strategy);
entries = NULL; /* keep compiler quiet */
}
PG_RETURN_POINTER(entries);
}
Datum
gin_consistent_jsonb(PG_FUNCTION_ARGS)
{
bool *check = (bool *) PG_GETARG_POINTER(0);
StrategyNumber strategy = PG_GETARG_UINT16(1);
/* Jsonb *query = PG_GETARG_JSONB(2); */
int32 nkeys = PG_GETARG_INT32(3);
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
bool *recheck = (bool *) PG_GETARG_POINTER(5);
bool res = true;
int32 i;
if (strategy == JsonbContainsStrategyNumber)
{
/*
* Index doesn't have information about correspondence of keys and
* values, so we must recheck. However, if all of the keys are not
* present, we can fail immediately.
*/
*recheck = true;
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
}
else if (strategy == JsonbExistsStrategyNumber)
{
/* Existence of key guaranteed in default search mode */
*recheck = false;
res = true;
}
else if (strategy == JsonbExistsAnyStrategyNumber)
{
/* Existence of key guaranteed in default search mode */
*recheck = false;
res = true;
}
else if (strategy == JsonbExistsAllStrategyNumber)
{
/* Testing for the presence of all keys gives an exact result */
*recheck = false;
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
}
else
elog(ERROR, "unrecognized strategy number: %d", strategy);
PG_RETURN_BOOL(res);
}
Datum
gin_consistent_jsonb_hash(PG_FUNCTION_ARGS)
{
bool *check = (bool *) PG_GETARG_POINTER(0);
StrategyNumber strategy = PG_GETARG_UINT16(1);
/* Jsonb *query = PG_GETARG_JSONB(2); */
int32 nkeys = PG_GETARG_INT32(3);
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
bool *recheck = (bool *) PG_GETARG_POINTER(5);
bool res = true;
int32 i;
if (strategy == JsonbContainsStrategyNumber)
{
/*
* Index doesn't have information about correspondence of keys and
* values, so we must recheck. However, if not all of the keys are
* present, we can fail immediately.
*/
*recheck = true;
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
}
else
elog(ERROR, "unrecognized strategy number: %d", strategy);
PG_RETURN_BOOL(res);
}
Datum
gin_extract_jsonb_hash(PG_FUNCTION_ARGS)
{
Jsonb *jb = PG_GETARG_JSONB(0);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
Datum *entries = NULL;
int total = 2 * JB_ROOT_COUNT(jb);
int i = 0,
r;
JsonbIterator *it;
JsonbValue v;
PathHashStack tail;
PathHashStack *stack,
*tmp;
pg_crc32 path_crc32;
if (total == 0)
{
*nentries = 0;
PG_RETURN_POINTER(NULL);
}
entries = (Datum *) palloc(sizeof(Datum) * total);
it = JsonbIteratorInit(VARDATA(jb));
tail.next = NULL;
INIT_CRC32(tail.hash_state);
stack = &tail;
/*
* Calculate hashes of all key_1.key_2. ... .key_n.value paths as entries.
* Order of array elements doesn't matter so array keys are empty in path.
* For faster calculation of hashes, use a stack of precalculated hashes of
* prefixes.
*/
while ((r = JsonbIteratorGet(&it, &v, false)) != 0)
{
if (i >= total)
{
total *= 2;
entries = (Datum *) repalloc(entries, sizeof(Datum) * total);
}
switch (r)
{
case WJB_BEGIN_ARRAY:
tmp = stack;
stack = (PathHashStack *) palloc(sizeof(PathHashStack));
stack->next = tmp;
stack->hash_state = tmp->hash_state;
COMP_CRC32(stack->hash_state, PATH_SEPARATOR, 1);
break;
case WJB_BEGIN_OBJECT:
/* Preserve stack item for key */
tmp = stack;
stack = (PathHashStack *) palloc(sizeof(PathHashStack));
stack->next = tmp;
break;
case WJB_KEY:
/* Calc hash of key and separated into preserved stack item */
stack->hash_state = stack->next->hash_state;
hash_value(&v, stack);
COMP_CRC32(stack->hash_state, PATH_SEPARATOR, 1);
break;
case WJB_VALUE:
case WJB_ELEM:
hash_value(&v, stack);
path_crc32 = stack->hash_state;
FIN_CRC32(path_crc32);
entries[i++] = path_crc32;
break;
case WJB_END_ARRAY:
case WJB_END_OBJECT:
/* Pop stack item */
tmp = stack->next;
pfree(stack);
stack = tmp;
break;
default:
break;
}
}
*nentries = i;
PG_RETURN_POINTER(entries);
}
Datum
gin_extract_jsonb_query_hash(PG_FUNCTION_ARGS)
{
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
StrategyNumber strategy = PG_GETARG_UINT16(2);
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
Datum *entries;
if (strategy == JsonbContainsStrategyNumber)
{
/* Query is a jsonb, so just apply gin_extract_jsonb... */
entries = (Datum *)
DatumGetPointer(DirectFunctionCall2(gin_extract_jsonb_hash,
PG_GETARG_DATUM(0),
PointerGetDatum(nentries)));
/* ... although "contains {}" requires a full index scan */
if (entries == NULL)
*searchMode = GIN_SEARCH_MODE_ALL;
}
else
{
elog(ERROR, "unrecognized strategy number: %d", strategy);
entries = NULL; /* keep compiler quiet */
}
PG_RETURN_POINTER(entries);
}
static void
hash_value(JsonbValue * v, PathHashStack * stack)
{
switch (v->type)
{
case jbvNull:
COMP_CRC32(stack->hash_state, "NULL", 5 /* include trailing \0 */ );
break;
case jbvBool:
COMP_CRC32(stack->hash_state, (v->boolean) ? " t" : " f", 2 /* include trailing \0 */ );
break;
case jbvNumeric:
stack->hash_state ^= DatumGetInt32(DirectFunctionCall1(hash_numeric,
NumericGetDatum(v->numeric)));
break;
case jbvString:
COMP_CRC32(stack->hash_state, v->string.val, v->string.len);
break;
default:
elog(ERROR, "invalid jsonb scalar type");
break;
}
}
/* Build an indexable text value */
static text *
makeitem(char *str, int len, char flag)
{
text *item;
item = (text *) palloc(VARHDRSZ + len + 1);
SET_VARSIZE(item, VARHDRSZ + len + 1);
*VARDATA(item) = flag;
if (str && len > 0)
memcpy(VARDATA(item) + 1, str, len);
return item;
}
/*
* Create a textual representation of a jsonbValue for GIN storage.
*
* N.B: This should only be called where the recheck flag will be set.
*/
static text *
makeitemFromValue(JsonbValue * v, char flag)
{
text *item;
char *cstr;
switch (v->type)
{
case jbvNull:
item = makeitem(NULL, 0, NULLFLAG);
break;
case jbvBool:
item = makeitem((v->boolean) ? " t" : " f", 2, flag);
break;
case jbvNumeric:
/*
* A textual, locale and precision independent representation of
* numeric is required. Use the standard hash_numeric to build
* one.
*/
cstr = palloc(8 /* hex numbers */ + 1 /* \0 */ );
snprintf(cstr, 9, "%08x", DatumGetInt32(DirectFunctionCall1(hash_numeric,
NumericGetDatum(v->numeric))));
item = makeitem(cstr, 8, flag);
pfree(cstr);
break;
case jbvString:
item = makeitem(v->string.val, v->string.len, flag);
break;
default:
elog(ERROR, "invalid jsonb scalar type");
}
return item;
}