-
Notifications
You must be signed in to change notification settings - Fork 49
/
jsonb_gin_ops.c
1355 lines (1210 loc) · 30.7 KB
/
jsonb_gin_ops.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* jsonb_gin_ops.c
* Support GIN over jsonb with jsquery operation
*
* Copyright (c) 2014, PostgreSQL Global Development Group
* Portions Copyright (c) 2017-2021, Postgres Professional
* Author: Alexander Korotkov <[email protected]>
*
* IDENTIFICATION
* contrib/jsquery/jsonb_gin_ops.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/hash.h"
#include "access/skey.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/jsonb.h"
#include "jsquery.h"
typedef struct PathHashStack
{
uint32 hash;
struct PathHashStack *parent;
} PathHashStack;
typedef struct
{
int32 vl_len_;
uint32 hash;
uint8 type;
char data[1];
} GINKey;
#define GINKEYLEN offsetof(GINKey, data)
#define GINKeyTrue 0x80
#define GINKeyMinusInf 0x80
#define GINKeyEmptyArray 0x80
#define GINKeyLenString (INTALIGN(offsetof(GINKey, data)) + sizeof(uint32))
#define GINKeyLenNumeric(len) (INTALIGN(offsetof(GINKey, data)) + len)
#define GINKeyDataString(key) (*(uint32 *)((Pointer)key + INTALIGN(offsetof(GINKey, data))))
#define GINKeyDataNumeric(key) ((Pointer)key + INTALIGN(offsetof(GINKey, data)))
#define GINKeyType(key) ((key)->type & 0x7F)
#define GINKeyIsTrue(key) ((key)->type & GINKeyTrue)
#define GINKeyIsMinusInf(key) ((key)->type & GINKeyMinusInf)
#define GINKeyIsEmptyArray(key) ((key)->type & GINKeyEmptyArray)
#define BLOOM_BITS 2
#define JsonbNestedContainsStrategyNumber 13
#define JsQueryMatchStrategyNumber 14
typedef struct
{
Datum *entries;
Pointer *extra_data;
bool *partial_match;
int *map;
int count, total;
} Entries;
typedef struct
{
ExtractedNode *root;
ExtractedNode *node;
uint32 hash;
bool lossyHash;
GINKey *rightBound;
} KeyExtra;
static uint32 get_bloom_value(uint32 hash);
static uint32 get_path_bloom(PathHashStack *stack);
static GINKey *make_gin_key(JsonbValue *v, uint32 hash);
static GINKey *make_gin_key_string(uint32 hash);
static GINKey *make_gin_query_value_key(JsQueryItem *value, uint32 hash);
static GINKey *make_gin_query_key(ExtractedNode *node, bool *partialMatch, uint32 hash, KeyExtra *keyExtra);
static GINKey *make_gin_query_key_minus_inf(uint32 hash);
static int32 compare_gin_key_value(GINKey *arg1, GINKey *arg2);
static int add_entry(Entries *e, Datum key, Pointer extra, bool pmatch);
PG_FUNCTION_INFO_V1(gin_compare_jsonb_value_path);
PG_FUNCTION_INFO_V1(gin_compare_partial_jsonb_value_path);
PG_FUNCTION_INFO_V1(gin_extract_jsonb_value_path);
PG_FUNCTION_INFO_V1(gin_extract_jsonb_query_value_path);
PG_FUNCTION_INFO_V1(gin_consistent_jsonb_value_path);
PG_FUNCTION_INFO_V1(gin_triconsistent_jsonb_value_path);
PG_FUNCTION_INFO_V1(gin_debug_query_value_path);
Datum gin_compare_jsonb_value_path(PG_FUNCTION_ARGS);
Datum gin_compare_partial_jsonb_value_path(PG_FUNCTION_ARGS);
Datum gin_extract_jsonb_value_path(PG_FUNCTION_ARGS);
Datum gin_extract_jsonb_query_value_path(PG_FUNCTION_ARGS);
Datum gin_consistent_jsonb_value_path(PG_FUNCTION_ARGS);
Datum gin_triconsistent_jsonb_value_path(PG_FUNCTION_ARGS);
Datum gin_debug_query_value_path(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(gin_compare_jsonb_path_value);
PG_FUNCTION_INFO_V1(gin_compare_partial_jsonb_path_value);
PG_FUNCTION_INFO_V1(gin_extract_jsonb_path_value);
PG_FUNCTION_INFO_V1(gin_extract_jsonb_query_path_value);
PG_FUNCTION_INFO_V1(gin_consistent_jsonb_path_value);
PG_FUNCTION_INFO_V1(gin_triconsistent_jsonb_path_value);
PG_FUNCTION_INFO_V1(gin_debug_query_path_value);
Datum gin_compare_jsonb_path_value(PG_FUNCTION_ARGS);
Datum gin_compare_partial_jsonb_path_value(PG_FUNCTION_ARGS);
Datum gin_extract_jsonb_path_value(PG_FUNCTION_ARGS);
Datum gin_extract_jsonb_query_path_value(PG_FUNCTION_ARGS);
Datum gin_consistent_jsonb_path_value(PG_FUNCTION_ARGS);
Datum gin_triconsistent_jsonb_path_value(PG_FUNCTION_ARGS);
Datum gin_debug_query_path_value(PG_FUNCTION_ARGS);
static int
add_entry(Entries *e, Datum key, Pointer extra, bool pmatch)
{
int entryNum;
if (!e->entries)
{
e->total = 16;
e->entries = (Datum *)palloc(e->total * sizeof(Datum));
e->extra_data = (Pointer *)palloc(e->total * sizeof(Pointer));
e->partial_match = (bool *)palloc(e->total * sizeof(bool));
}
if (e->count + 1 > e->total)
{
e->total *= 2;
e->entries = (Datum *)repalloc(e->entries, e->total * sizeof(Datum));
e->extra_data = (Pointer *)repalloc(e->extra_data, e->total * sizeof(Pointer));
e->partial_match = (bool *)repalloc(e->partial_match, e->total * sizeof(bool));
}
entryNum = e->count;
e->count++;
e->entries[entryNum] = key;
e->extra_data[entryNum] = extra;
e->partial_match[entryNum] = pmatch;
return entryNum;
}
static uint32
get_bloom_value(uint32 hash)
{
int i, j, vals[BLOOM_BITS], val, tmp;
uint32 res = 0;
for (i = 0; i < BLOOM_BITS; i++)
{
val = hash % (32 - i) + i;
hash /= (32 - i);
vals[i] = val;
j = i;
while (j > 0 && vals[j] <= vals[j - 1])
{
tmp = vals[j] - 1;
vals[j] = vals[j - 1];
vals[j - 1] = tmp;
j--;
}
}
for (i = 0; i < BLOOM_BITS; i++)
{
res |= (1 << vals[i]);
}
return res;
}
static uint32
get_path_bloom(PathHashStack *stack)
{
uint32 res = 0, val;
while (stack)
{
uint32 hash = stack->hash;
val = get_bloom_value(hash);
res |= val;
stack = stack->parent;
}
return res;
}
static uint32
get_query_path_bloom(PathItem *pathItem, bool *lossy)
{
uint32 res = 0, val;
*lossy = false;
while (pathItem)
{
uint32 hash;
if (pathItem->type == iKey)
{
hash = hash_any((unsigned char *)pathItem->s, pathItem->len);
val = get_bloom_value(hash);
res |= val;
}
else if (pathItem->type == iAny || pathItem->type == iAnyKey)
{
*lossy = true;
}
pathItem = pathItem->parent;
}
return res;
}
#ifdef NOT_USED
static void
log_gin_key(GINKey *key)
{
if (GINKeyType(key) == jbvNull)
{
elog(NOTICE, "hash = %X, NULL", key->hash);
}
else if (GINKeyType(key) == jbvBool)
{
if (GINKeyIsTrue(key))
elog(NOTICE, "hash = %X, true", key->hash);
else
elog(NOTICE, "hash = %X, false", key->hash);
}
else if (GINKeyType(key) == jbvNumeric)
{
if (GINKeyIsMinusInf(key))
{
elog(NOTICE, "hash = %X, -inf", key->hash);
}
else
{
char *s;
s = DatumGetCString(DirectFunctionCall1(numeric_out, PointerGetDatum(GINKeyDataNumeric(key))));
elog(NOTICE, "hash = %X, \"%s\"", key->hash, s);
}
}
else if (GINKeyType(key) == jbvString)
{
elog(NOTICE, "hash = %X, %X", key->hash, GINKeyDataString(key));
}
else
{
elog(ERROR, "GINKey must be scalar");
}
}
#endif
static GINKey *
make_gin_key(JsonbValue *v, uint32 hash)
{
GINKey *key;
switch (v->type)
{
case jbvNull:
case jbvObject:
{
key = (GINKey *)palloc(GINKEYLEN);
key->type = v->type;
SET_VARSIZE(key, GINKEYLEN);
break;
}
case jbvBool:
{
key = (GINKey *)palloc(GINKEYLEN);
key->type = v->type | (v->val.boolean ? GINKeyTrue : 0);
SET_VARSIZE(key, GINKEYLEN);
break;
}
case jbvArray:
{
key = (GINKey *)palloc(GINKEYLEN);
key->type = v->type;
if (v->val.array.nElems == 0)
key->type |= GINKeyEmptyArray;
SET_VARSIZE(key, GINKEYLEN);
break;
}
case jbvNumeric:
{
key = (GINKey *) palloc0(GINKeyLenNumeric(VARSIZE_ANY(v->val.numeric)));
key->type = v->type;
memcpy(GINKeyDataNumeric(key), v->val.numeric, VARSIZE_ANY(v->val.numeric));
SET_VARSIZE(key, GINKeyLenNumeric(VARSIZE_ANY(v->val.numeric)));
break;
}
case jbvString:
{
key = (GINKey *) palloc0(GINKeyLenString);
key->type = v->type;
GINKeyDataString(key) = hash_any((unsigned char *)v->val.string.val,
v->val.string.len);
SET_VARSIZE(key, GINKeyLenString);
break;
}
default:
elog(ERROR, "GINKey must be scalar");
}
key->hash = hash;
return key;
}
static GINKey *
make_gin_key_string(uint32 hash)
{
GINKey *key;
key = (GINKey *)palloc(GINKeyLenString);
key->type = jbvString;
GINKeyDataString(key) = 0;
SET_VARSIZE(key, GINKeyLenString);
key->hash = hash;
return key;
}
static GINKey *
make_gin_query_value_key(JsQueryItem *value, uint32 hash)
{
GINKey *key;
int32 len;
char *s;
Numeric numeric;
switch(value->type)
{
case jqiNull:
key = (GINKey *)palloc(GINKEYLEN);
key->type = jbvNull;
SET_VARSIZE(key, GINKEYLEN);
break;
case jqiString:
key = (GINKey *)palloc(GINKeyLenString);
key->type = jbvString;
s = jsqGetString(value, &len);
GINKeyDataString(key) = hash_any((unsigned char *)s, len);
SET_VARSIZE(key, GINKeyLenString);
break;
case jqiBool:
key = (GINKey *)palloc(GINKEYLEN);
key->type = jbvBool | (jsqGetBool(value) ? GINKeyTrue : 0);
SET_VARSIZE(key, GINKEYLEN);
break;
case jqiNumeric:
numeric = jsqGetNumeric(value);
key = (GINKey *)palloc(GINKeyLenNumeric(VARSIZE_ANY(numeric)));
key->type = jbvNumeric;
memcpy(GINKeyDataNumeric(key), numeric, VARSIZE_ANY(numeric));
SET_VARSIZE(key, GINKeyLenNumeric(VARSIZE_ANY(numeric)));
break;
default:
elog(ERROR,"Wrong state");
}
key->hash = hash;
return key;
}
static GINKey *
make_gin_query_key(ExtractedNode *node, bool *partialMatch, uint32 hash, KeyExtra *keyExtra)
{
JsonbValue v;
GINKey *key;
switch (node->type)
{
case eExactValue:
key = make_gin_query_value_key(node->exactValue, hash);
break;
case eEmptyArray:
v.type = jbvArray;
v.val.array.nElems = 0;
key = make_gin_key(&v, hash);
break;
case eInequality:
*partialMatch = true;
if (node->bounds.leftBound)
key = make_gin_query_value_key(node->bounds.leftBound, hash);
else
key = make_gin_query_key_minus_inf(hash);
if (node->bounds.rightBound)
keyExtra->rightBound = make_gin_query_value_key(node->bounds.rightBound, hash);
else
keyExtra->rightBound = NULL;
break;
case eIs:
switch (node->isType)
{
case jbvArray:
*partialMatch = true;
v.type = jbvArray;
v.val.array.nElems = 1;
key = make_gin_key(&v, hash);
break;
case jbvObject:
*partialMatch = true;
v.type = jbvObject;
key = make_gin_key(&v, hash);
break;
case jbvString:
*partialMatch = true;
key = make_gin_key_string(hash);
break;
case jbvNumeric:
*partialMatch = true;
key = make_gin_query_key_minus_inf(hash);
break;
case jbvBool:
*partialMatch = true;
v.type = jbvBool;
v.val.boolean = false;
key = make_gin_key(&v, hash);
break;
case jbvNull:
v.type = jbvNull;
key = make_gin_key(&v, hash);
break;
default:
elog(ERROR,"Wrong type");
return NULL;
}
break;
case eAny:
v.type = jbvNull;
key = make_gin_key(&v, hash);
*partialMatch = true;
break;
default:
elog(ERROR, "Wrong type");
break;
}
return key;
}
static GINKey *
make_gin_query_key_minus_inf(uint32 hash)
{
GINKey *key;
key = (GINKey *)palloc(GINKEYLEN);
key->type = jbvNumeric | GINKeyMinusInf;
key->hash = hash;
SET_VARSIZE(key, GINKEYLEN);
return key;
}
static bool
check_value_path_entry_handler(ExtractedNode *node, Pointer extra)
{
return true;
}
static int
make_value_path_entry_handler(ExtractedNode *node, Pointer extra)
{
Entries *e = (Entries *)extra;
uint32 hash;
bool lossy, partialMatch = false;
GINKey *key;
KeyExtra *keyExtra;
int result;
Assert(!isLogicalNodeType(node->type));
hash = get_query_path_bloom(node->path, &lossy);
keyExtra = (KeyExtra *)palloc(sizeof(KeyExtra));
keyExtra->hash = hash;
keyExtra->node = node;
keyExtra->lossyHash = lossy;
key = make_gin_query_key(node, &partialMatch, hash, keyExtra);
result = add_entry(e, PointerGetDatum(key), (Pointer)keyExtra,
lossy | partialMatch);
return result;
}
static int32
compare_gin_key_value(GINKey *arg1, GINKey *arg2)
{
if (GINKeyType(arg1) != GINKeyType(arg2))
{
return (GINKeyType(arg1) > GINKeyType(arg2)) ? 1 : -1;
}
else
{
switch(GINKeyType(arg1))
{
case jbvNull:
case jbvArray:
if (GINKeyIsEmptyArray(arg1) == GINKeyIsEmptyArray(arg2))
return 0;
else if (GINKeyIsEmptyArray(arg1) > GINKeyIsEmptyArray(arg2))
return 1;
else
return -1;
case jbvObject:
return 0;
case jbvBool:
if (GINKeyIsTrue(arg1) == GINKeyIsTrue(arg2))
return 0;
else if (GINKeyIsTrue(arg1) > GINKeyIsTrue(arg2))
return 1;
else
return -1;
case jbvNumeric:
if (GINKeyIsMinusInf(arg1))
{
if (GINKeyIsMinusInf(arg2))
return 0;
else
return -1;
}
else
{
if (GINKeyIsMinusInf(arg2))
return 1;
}
return DatumGetInt32(DirectFunctionCall2(numeric_cmp,
PointerGetDatum(GINKeyDataNumeric(arg1)),
PointerGetDatum(GINKeyDataNumeric(arg2))));
case jbvString:
if (GINKeyDataString(arg1) < GINKeyDataString(arg2))
return -1;
else if (GINKeyDataString(arg1) == GINKeyDataString(arg2))
return 0;
else
return 1;
default:
elog(ERROR, "GINKey must be scalar");
return 0;
}
}
}
Datum
gin_compare_jsonb_value_path(PG_FUNCTION_ARGS)
{
GINKey *arg1 = (GINKey *)PG_GETARG_VARLENA_P(0);
GINKey *arg2 = (GINKey *)PG_GETARG_VARLENA_P(1);
int32 result = 0;
result = compare_gin_key_value(arg1, arg2);
if (result == 0 && arg1->hash != arg2->hash)
{
result = (arg1->hash > arg2->hash) ? 1 : -1;
}
PG_FREE_IF_COPY(arg1, 0);
PG_FREE_IF_COPY(arg2, 1);
PG_RETURN_INT32(result);
}
Datum
gin_compare_partial_jsonb_value_path(PG_FUNCTION_ARGS)
{
GINKey *partial_key = (GINKey *)PG_GETARG_VARLENA_P(0);
GINKey *key = (GINKey *)PG_GETARG_VARLENA_P(1);
StrategyNumber strategy = PG_GETARG_UINT16(2);
int32 result;
if (strategy == JsQueryMatchStrategyNumber)
{
KeyExtra *extra = (KeyExtra *)PG_GETARG_POINTER(3);
ExtractedNode *node = extra->node;
switch (node->type)
{
case eExactValue:
case eEmptyArray:
result = compare_gin_key_value(key, partial_key);
break;
case eInequality:
result = 0;
if (!node->bounds.leftInclusive &&
compare_gin_key_value(key, partial_key) <= 0)
{
result = -1;
}
if (result == 0 && extra->rightBound)
{
result = compare_gin_key_value(key, extra->rightBound);
if ((node->bounds.rightInclusive && result <= 0)
|| result < 0)
result = 0;
else
result = 1;
}
break;
case eIs:
if (node->isType == GINKeyType(key))
result = 0;
else
result = (GINKeyType(key) > node->isType) ? 1 : -1;
break;
case eAny:
result = 0;
break;
default:
elog(ERROR, "Wrong type");
break;
}
if (result == 0)
{
if (extra->lossyHash)
{
if ((key->hash & extra->hash) != extra->hash)
result = -1;
}
else
{
if (key->hash != extra->hash)
result = -1;
}
}
}
else
{
uint32 *extra_data = (uint32 *)PG_GETARG_POINTER(3);
uint32 bloom = *extra_data;
result = compare_gin_key_value(key, partial_key);
if (result == 0)
{
if ((key->hash & bloom) != bloom)
result = -1;
}
}
PG_FREE_IF_COPY(partial_key, 0);
PG_FREE_IF_COPY(key, 1);
PG_RETURN_INT32(result);
}
static Datum *
gin_extract_jsonb_value_path_internal(Jsonb *jb, int32 *nentries, uint32 **bloom)
{
int total = 2 * JB_ROOT_COUNT(jb);
JsonbIterator *it;
JsonbValue v;
PathHashStack *stack;
int i = 0,
r;
Datum *entries = NULL;
uint32 hash;
if (total == 0)
{
*nentries = 0;
return NULL;
}
entries = (Datum *) palloc(sizeof(Datum) * total);
if (bloom)
(*bloom) = (uint32 *) palloc(sizeof(uint32) * total);
it = JsonbIteratorInit(&jb->root);
stack = NULL;
while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
{
PathHashStack *tmp;
if (i >= total)
{
total *= 2;
entries = (Datum *) repalloc(entries, sizeof(Datum) * total);
if (bloom)
(*bloom) = (uint32 *) repalloc(*bloom, sizeof(uint32) * total);
}
switch (r)
{
case WJB_BEGIN_ARRAY:
if (!v.val.array.rawScalar)
entries[i++] = PointerGetDatum(make_gin_key(&v, get_path_bloom(stack)));
break;
case WJB_BEGIN_OBJECT:
entries[i++] = PointerGetDatum(make_gin_key(&v, get_path_bloom(stack)));
tmp = stack;
stack = (PathHashStack *) palloc(sizeof(PathHashStack));
stack->parent = tmp;
break;
case WJB_KEY:
if (!stack) /* should never happen */
elog(ERROR, "error jsonb iteration");
stack->hash = 0;
JsonbHashScalarValue(&v, &stack->hash);
break;
case WJB_ELEM:
case WJB_VALUE:
if (bloom)
{
(*bloom)[i] = get_path_bloom(stack);
hash = 0;
}
else
{
hash = get_path_bloom(stack);
}
entries[i++] = PointerGetDatum(make_gin_key(&v, hash));
break;
case WJB_END_OBJECT:
/* Pop the stack */
if (!stack) /* should never happen */
elog(ERROR, "error jsonb iteration");
tmp = stack->parent;
pfree(stack);
stack = tmp;
break;
case WJB_END_ARRAY:
break;
default:
elog(ERROR, "invalid JsonbIteratorNext rc: %d", r);
}
}
*nentries = i;
return entries;
}
Datum
gin_extract_jsonb_value_path(PG_FUNCTION_ARGS)
{
Jsonb *jb = PG_GETARG_JSONB_P(0);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(gin_extract_jsonb_value_path_internal(jb, nentries, NULL));
}
Datum
gin_debug_query_value_path(PG_FUNCTION_ARGS)
{
JsQuery *jq;
Entries e = {0};
char *s;
jq = PG_GETARG_JSQUERY(0);
s = debugJsQuery(jq, make_value_path_entry_handler,
check_value_path_entry_handler, (Pointer)&e);
PG_RETURN_TEXT_P(cstring_to_text(s));
}
Datum
gin_extract_jsonb_query_value_path(PG_FUNCTION_ARGS)
{
Jsonb *jb;
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
StrategyNumber strategy = PG_GETARG_UINT16(2);
bool **pmatch = (bool **) PG_GETARG_POINTER(3);
Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
Datum *entries = NULL;
int i, n;
uint32 *bloom;
Entries e = {0};
JsQuery *jq;
ExtractedNode *root;
switch(strategy)
{
case JsonbContainsStrategyNumber:
jb = PG_GETARG_JSONB_P(0);
entries = gin_extract_jsonb_value_path_internal(jb, nentries, NULL);
break;
case JsonbNestedContainsStrategyNumber:
jb = PG_GETARG_JSONB_P(0);
entries = gin_extract_jsonb_value_path_internal(jb, nentries, &bloom);
n = *nentries;
*pmatch = (bool *) palloc(sizeof(bool) * n);
for (i = 0; i < n; i++)
(*pmatch)[i] = true;
*extra_data = (Pointer *) palloc(sizeof(Pointer) * n);
for (i = 0; i < n; i++)
(*extra_data)[i] = (Pointer)&bloom[i];
break;
case JsQueryMatchStrategyNumber:
jq = PG_GETARG_JSQUERY(0);
root = extractJsQuery(jq, make_value_path_entry_handler,
check_value_path_entry_handler, (Pointer)&e);
if (root)
{
*nentries = e.count;
entries = e.entries;
*pmatch = e.partial_match;
*extra_data = e.extra_data;
for (i = 0; i < e.count; i++)
((KeyExtra *)e.extra_data[i])->root = root;
}
else
{
entries = NULL;
*nentries = 0;
}
break;
default:
elog(ERROR, "unrecognized strategy number: %d", strategy);
break;
}
/* ...although "contains {}" requires a full index scan */
if (entries == NULL)
*searchMode = GIN_SEARCH_MODE_ALL;
PG_RETURN_POINTER(entries);
}
Datum
gin_consistent_jsonb_value_path(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;
*recheck = true;
switch (strategy)
{
case JsonbContainsStrategyNumber:
case JsonbNestedContainsStrategyNumber:
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
break;
case JsQueryMatchStrategyNumber:
if (nkeys == 0)
res = true;
else
res = execRecursive(((KeyExtra *)extra_data[0])->root, check);
break;
default:
elog(ERROR, "unrecognized strategy number: %d", strategy);
break;
}
PG_RETURN_BOOL(res);
}
Datum
gin_triconsistent_jsonb_value_path(PG_FUNCTION_ARGS)
{
GinTernaryValue *check = (GinTernaryValue *) PG_GETARG_POINTER(0);
StrategyNumber strategy = PG_GETARG_UINT16(1);
/* Jsonb *query = PG_GETARG_JSONB_P(2); */
int32 nkeys = PG_GETARG_INT32(3);
Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
GinTernaryValue res = GIN_TRUE;
int32 i;
bool has_maybe = false;
switch (strategy)
{
case JsonbContainsStrategyNumber:
case JsonbNestedContainsStrategyNumber:
/*
* All extracted keys must be present. A combination of GIN_MAYBE and
* GIN_TRUE induces a GIN_MAYBE result, because then all keys may be
* present.
*/
for (i = 0; i < nkeys; i++)
{
if (check[i] == GIN_FALSE)
{
res = GIN_FALSE;
break;
}
if (check[i] == GIN_MAYBE)
{
res = GIN_MAYBE;
has_maybe = true;
}
}
/*
* jsonb_hash_ops index doesn't have information about correspondence of
* Jsonb keys and values (as distinct from GIN keys, which for this opclass
* are a hash of a pair, or a hash of just an element), so invariably we
* recheck. This is also reflected in how GIN_MAYBE is given in response
* to there being no GIN_MAYBE input.
*/
if (!has_maybe && res == GIN_TRUE)
res = GIN_MAYBE;
break;
case JsQueryMatchStrategyNumber:
if (nkeys == 0)
res = GIN_MAYBE;
else
res = execRecursiveTristate(((KeyExtra *)extra_data[0])->root, check);
if (res == GIN_TRUE)
res = GIN_MAYBE;
break;
default:
elog(ERROR, "unrecognized strategy number: %d", strategy);
break;
}
PG_RETURN_GIN_TERNARY_VALUE(res);
}
static bool
get_query_path_hash(PathItem *pathItem, uint32 *hash)
{
check_stack_depth();
if (!pathItem)
return true;
if (!get_query_path_hash(pathItem->parent, hash))
{
return false;
}
else
{
if (pathItem->type == iAny || pathItem->type == iAnyKey)
{
return false;
}
else
{
if (pathItem->type == iKey)
{
*hash = (*hash << 1) | (*hash >> 31);
*hash ^= hash_any((unsigned char *)pathItem->s, pathItem->len);
}
else if (pathItem->type == iAnyArray || pathItem->type == iIndexArray)
{
*hash = (*hash << 1) | (*hash >> 31);
*hash ^= JB_FARRAY;
}
return true;
}
}
}
static bool
check_path_value_entry_handler(ExtractedNode *node, Pointer extra)
{
uint32 hash;
hash = 0;
if (!get_query_path_hash(node->path, &hash))
return false;
return true;
}
static int
make_path_value_entry_handler(ExtractedNode *node, Pointer extra)
{
Entries *e = (Entries *)extra;
uint32 hash;
GINKey *key;
KeyExtra *keyExtra;
int result;
bool partialMatch = false;
Assert(!isLogicalNodeType(node->type));
hash = 0;
if (!get_query_path_hash(node->path, &hash))
return -1;
keyExtra = (KeyExtra *)palloc(sizeof(KeyExtra));
keyExtra->hash = hash;
keyExtra->node = node;
key = make_gin_query_key(node, &partialMatch, hash, keyExtra);
result = add_entry(e, PointerGetDatum(key), (Pointer)keyExtra, partialMatch);
return result;
}