PostgreSQL Source Code git master
Loading...
Searching...
No Matches
jsonb_op.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * jsonb_op.c
4 * Special operators for jsonb only, used by various index access methods
5 *
6 * Copyright (c) 2014-2026, PostgreSQL Global Development Group
7 *
8 *
9 * IDENTIFICATION
10 * src/backend/utils/adt/jsonb_op.c
11 *
12 *-------------------------------------------------------------------------
13 */
14#include "postgres.h"
15
16#include "catalog/pg_type.h"
17#include "utils/fmgrprotos.h"
18#include "utils/jsonb.h"
19
22{
24 text *key = PG_GETARG_TEXT_PP(1);
26 JsonbValue *v = NULL;
27
28 /*
29 * We only match Object keys (which are naturally always Strings), or
30 * string elements in arrays. In particular, we do not match non-string
31 * scalar elements. Existence of a key/element is only considered at the
32 * top level. No recursion occurs.
33 */
35 kval.val.string.val = VARDATA_ANY(key);
36 kval.val.string.len = VARSIZE_ANY_EXHDR(key);
37
40 &kval);
41
42 PG_RETURN_BOOL(v != NULL);
43}
44
47{
50 int i;
52 bool *key_nulls;
53 int elem_count;
54
56
57 for (i = 0; i < elem_count; i++)
58 {
60
61 if (key_nulls[i])
62 continue;
63
65 /* We rely on the array elements not being toasted */
66 strVal.val.string.val = VARDATA_ANY(DatumGetPointer(key_datums[i]));
68
71 &strVal) != NULL)
72 PG_RETURN_BOOL(true);
73 }
74
75 PG_RETURN_BOOL(false);
76}
77
80{
83 int i;
85 bool *key_nulls;
86 int elem_count;
87
89
90 for (i = 0; i < elem_count; i++)
91 {
93
94 if (key_nulls[i])
95 continue;
96
98 /* We rely on the array elements not being toasted */
99 strVal.val.string.val = VARDATA_ANY(DatumGetPointer(key_datums[i]));
101
104 &strVal) == NULL)
105 PG_RETURN_BOOL(false);
106 }
107
108 PG_RETURN_BOOL(true);
109}
110
111Datum
128
129Datum
131{
132 /* Commutator of "contains" */
135
137 *it2;
138
140 PG_RETURN_BOOL(false);
141
142 it1 = JsonbIteratorInit(&val->root);
143 it2 = JsonbIteratorInit(&tmpl->root);
144
146}
147
148Datum
150{
153 bool res;
154
155 res = (compareJsonbContainers(&jba->root, &jbb->root) != 0);
156
159 PG_RETURN_BOOL(res);
160}
161
162/*
163 * B-Tree operator class operators, support function
164 */
165Datum
167{
170 bool res;
171
172 res = (compareJsonbContainers(&jba->root, &jbb->root) < 0);
173
176 PG_RETURN_BOOL(res);
177}
178
179Datum
181{
184 bool res;
185
186 res = (compareJsonbContainers(&jba->root, &jbb->root) > 0);
187
190 PG_RETURN_BOOL(res);
191}
192
193Datum
195{
198 bool res;
199
200 res = (compareJsonbContainers(&jba->root, &jbb->root) <= 0);
201
204 PG_RETURN_BOOL(res);
205}
206
207Datum
209{
212 bool res;
213
214 res = (compareJsonbContainers(&jba->root, &jbb->root) >= 0);
215
218 PG_RETURN_BOOL(res);
219}
220
221Datum
223{
226 bool res;
227
228 res = (compareJsonbContainers(&jba->root, &jbb->root) == 0);
229
232 PG_RETURN_BOOL(res);
233}
234
235Datum
237{
240 int res;
241
242 res = compareJsonbContainers(&jba->root, &jbb->root);
243
246 PG_RETURN_INT32(res);
247}
248
249/*
250 * Hash operator class jsonb hashing function
251 */
252Datum
254{
257 JsonbValue v;
259 uint32 hash = 0;
260
261 if (JB_ROOT_COUNT(jb) == 0)
263
264 it = JsonbIteratorInit(&jb->root);
265
266 while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
267 {
268 switch (r)
269 {
270 /* Rotation is left to JsonbHashScalarValue() */
271 case WJB_BEGIN_ARRAY:
272 hash ^= JB_FARRAY;
273 break;
274 case WJB_BEGIN_OBJECT:
275 hash ^= JB_FOBJECT;
276 break;
277 case WJB_KEY:
278 case WJB_VALUE:
279 case WJB_ELEM:
281 break;
282 case WJB_END_ARRAY:
283 case WJB_END_OBJECT:
284 break;
285 default:
286 elog(ERROR, "invalid JsonbIteratorNext rc: %d", (int) r);
287 }
288 }
289
292}
293
294Datum
296{
298 uint64 seed = PG_GETARG_INT64(1);
300 JsonbValue v;
302 uint64 hash = 0;
303
304 if (JB_ROOT_COUNT(jb) == 0)
305 PG_RETURN_UINT64(seed);
306
307 it = JsonbIteratorInit(&jb->root);
308
309 while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
310 {
311 switch (r)
312 {
313 /* Rotation is left to JsonbHashScalarValueExtended() */
314 case WJB_BEGIN_ARRAY:
315 hash ^= ((uint64) JB_FARRAY) << 32 | JB_FARRAY;
316 break;
317 case WJB_BEGIN_OBJECT:
318 hash ^= ((uint64) JB_FOBJECT) << 32 | JB_FOBJECT;
319 break;
320 case WJB_KEY:
321 case WJB_VALUE:
322 case WJB_ELEM:
324 break;
325 case WJB_END_ARRAY:
326 case WJB_END_OBJECT:
327 break;
328 default:
329 elog(ERROR, "invalid JsonbIteratorNext rc: %d", (int) r);
330 }
331 }
332
335}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
uint64_t uint64
Definition c.h:547
uint32_t uint32
Definition c.h:546
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define PG_FREE_IF_COPY(ptr, n)
Definition fmgr.h:260
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
#define PG_RETURN_UINT64(x)
Definition fmgr.h:371
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
long val
Definition informix.c:689
int i
Definition isn.c:77
@ jbvString
Definition jsonb.h:231
#define JB_ROOT_IS_OBJECT(jbp_)
Definition jsonb.h:223
#define PG_GETARG_JSONB_P(x)
Definition jsonb.h:418
#define JB_FARRAY
Definition jsonb.h:205
JsonbIteratorToken
Definition jsonb.h:21
@ WJB_KEY
Definition jsonb.h:23
@ WJB_DONE
Definition jsonb.h:22
@ WJB_END_ARRAY
Definition jsonb.h:27
@ WJB_VALUE
Definition jsonb.h:24
@ WJB_END_OBJECT
Definition jsonb.h:29
@ WJB_ELEM
Definition jsonb.h:25
@ WJB_BEGIN_OBJECT
Definition jsonb.h:28
@ WJB_BEGIN_ARRAY
Definition jsonb.h:26
#define JB_ROOT_COUNT(jbp_)
Definition jsonb.h:221
#define JB_FOBJECT
Definition jsonb.h:204
Datum jsonb_le(PG_FUNCTION_ARGS)
Definition jsonb_op.c:194
Datum jsonb_exists_all(PG_FUNCTION_ARGS)
Definition jsonb_op.c:79
Datum jsonb_gt(PG_FUNCTION_ARGS)
Definition jsonb_op.c:180
Datum jsonb_hash_extended(PG_FUNCTION_ARGS)
Definition jsonb_op.c:295
Datum jsonb_exists_any(PG_FUNCTION_ARGS)
Definition jsonb_op.c:46
Datum jsonb_hash(PG_FUNCTION_ARGS)
Definition jsonb_op.c:253
Datum jsonb_contains(PG_FUNCTION_ARGS)
Definition jsonb_op.c:112
Datum jsonb_lt(PG_FUNCTION_ARGS)
Definition jsonb_op.c:166
Datum jsonb_contained(PG_FUNCTION_ARGS)
Definition jsonb_op.c:130
Datum jsonb_cmp(PG_FUNCTION_ARGS)
Definition jsonb_op.c:236
Datum jsonb_ge(PG_FUNCTION_ARGS)
Definition jsonb_op.c:208
Datum jsonb_ne(PG_FUNCTION_ARGS)
Definition jsonb_op.c:149
Datum jsonb_eq(PG_FUNCTION_ARGS)
Definition jsonb_op.c:222
Datum jsonb_exists(PG_FUNCTION_ARGS)
Definition jsonb_op.c:21
int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
Definition jsonb_util.c:194
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition jsonb_util.c:935
void JsonbHashScalarValue(const JsonbValue *scalarVal, uint32 *hash)
JsonbValue * findJsonbValueFromContainer(JsonbContainer *container, uint32 flags, JsonbValue *key)
Definition jsonb_util.c:348
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition jsonb_util.c:973
void JsonbHashScalarValueExtended(const JsonbValue *scalarVal, uint64 *hash, uint64 seed)
bool JsonbDeepContains(JsonbIterator **val, JsonbIterator **mContained)
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
static int fb(int x)
static unsigned hash(unsigned *uv, int n)
Definition rege_dfa.c:715
enum jbvType type
Definition jsonb.h:257
Definition jsonb.h:215
Definition c.h:706
#define strVal(v)
Definition value.h:82
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486