PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
jsonb_op.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/jsonb.h"
Include dependency graph for jsonb_op.c:

Go to the source code of this file.

Functions

Datum jsonb_exists (PG_FUNCTION_ARGS)
 
Datum jsonb_exists_any (PG_FUNCTION_ARGS)
 
Datum jsonb_exists_all (PG_FUNCTION_ARGS)
 
Datum jsonb_contains (PG_FUNCTION_ARGS)
 
Datum jsonb_contained (PG_FUNCTION_ARGS)
 
Datum jsonb_ne (PG_FUNCTION_ARGS)
 
Datum jsonb_lt (PG_FUNCTION_ARGS)
 
Datum jsonb_gt (PG_FUNCTION_ARGS)
 
Datum jsonb_le (PG_FUNCTION_ARGS)
 
Datum jsonb_ge (PG_FUNCTION_ARGS)
 
Datum jsonb_eq (PG_FUNCTION_ARGS)
 
Datum jsonb_cmp (PG_FUNCTION_ARGS)
 
Datum jsonb_hash (PG_FUNCTION_ARGS)
 

Function Documentation

Datum jsonb_cmp ( PG_FUNCTION_ARGS  )

Definition at line 237 of file jsonb_op.c.

References compareJsonbContainers(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_RETURN_INT32, and Jsonb::root.

238 {
239  Jsonb *jba = PG_GETARG_JSONB(0);
240  Jsonb *jbb = PG_GETARG_JSONB(1);
241  int res;
242 
243  res = compareJsonbContainers(&jba->root, &jbb->root);
244 
245  PG_FREE_IF_COPY(jba, 0);
246  PG_FREE_IF_COPY(jbb, 1);
247  PG_RETURN_INT32(res);
248 }
Definition: jsonb.h:215
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
Definition: jsonb_util.c:178
JsonbContainer root
Definition: jsonb.h:218
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
Datum jsonb_contained ( PG_FUNCTION_ARGS  )

Definition at line 131 of file jsonb_op.c.

References JB_ROOT_IS_OBJECT, JsonbDeepContains(), JsonbIteratorInit(), PG_GETARG_JSONB, PG_RETURN_BOOL, Jsonb::root, and val.

132 {
133  /* Commutator of "contains" */
134  Jsonb *tmpl = PG_GETARG_JSONB(0);
135  Jsonb *val = PG_GETARG_JSONB(1);
136 
137  JsonbIterator *it1,
138  *it2;
139 
140  if (JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl))
141  PG_RETURN_BOOL(false);
142 
143  it1 = JsonbIteratorInit(&val->root);
144  it2 = JsonbIteratorInit(&tmpl->root);
145 
146  PG_RETURN_BOOL(JsonbDeepContains(&it1, &it2));
147 }
#define JB_ROOT_IS_OBJECT(jbp_)
Definition: jsonb.h:224
Definition: jsonb.h:215
bool JsonbDeepContains(JsonbIterator **val, JsonbIterator **mContained)
Definition: jsonb_util.c:963
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:718
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
long val
Definition: informix.c:689
Datum jsonb_contains ( PG_FUNCTION_ARGS  )

Definition at line 113 of file jsonb_op.c.

References JB_ROOT_IS_OBJECT, JsonbDeepContains(), JsonbIteratorInit(), PG_GETARG_JSONB, PG_RETURN_BOOL, Jsonb::root, and val.

114 {
115  Jsonb *val = PG_GETARG_JSONB(0);
116  Jsonb *tmpl = PG_GETARG_JSONB(1);
117 
118  JsonbIterator *it1,
119  *it2;
120 
121  if (JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl))
122  PG_RETURN_BOOL(false);
123 
124  it1 = JsonbIteratorInit(&val->root);
125  it2 = JsonbIteratorInit(&tmpl->root);
126 
127  PG_RETURN_BOOL(JsonbDeepContains(&it1, &it2));
128 }
#define JB_ROOT_IS_OBJECT(jbp_)
Definition: jsonb.h:224
Definition: jsonb.h:215
bool JsonbDeepContains(JsonbIterator **val, JsonbIterator **mContained)
Definition: jsonb_util.c:963
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:718
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
long val
Definition: informix.c:689
Datum jsonb_eq ( PG_FUNCTION_ARGS  )

Definition at line 223 of file jsonb_op.c.

References compareJsonbContainers(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_RETURN_BOOL, and Jsonb::root.

224 {
225  Jsonb *jba = PG_GETARG_JSONB(0);
226  Jsonb *jbb = PG_GETARG_JSONB(1);
227  bool res;
228 
229  res = (compareJsonbContainers(&jba->root, &jbb->root) == 0);
230 
231  PG_FREE_IF_COPY(jba, 0);
232  PG_FREE_IF_COPY(jbb, 1);
233  PG_RETURN_BOOL(res);
234 }
Definition: jsonb.h:215
int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
Definition: jsonb_util.c:178
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
Datum jsonb_exists ( PG_FUNCTION_ARGS  )

Definition at line 22 of file jsonb_op.c.

References findJsonbValueFromContainer(), JB_FARRAY, JB_FOBJECT, jbvString, NULL, PG_GETARG_JSONB, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, Jsonb::root, JsonbValue::type, JsonbValue::val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

23 {
24  Jsonb *jb = PG_GETARG_JSONB(0);
25  text *key = PG_GETARG_TEXT_PP(1);
26  JsonbValue kval;
27  JsonbValue *v = NULL;
28 
29  /*
30  * We only match Object keys (which are naturally always Strings), or
31  * string elements in arrays. In particular, we do not match non-string
32  * scalar elements. Existence of a key/element is only considered at the
33  * top level. No recursion occurs.
34  */
35  kval.type = jbvString;
36  kval.val.string.val = VARDATA_ANY(key);
37  kval.val.string.len = VARSIZE_ANY_EXHDR(key);
38 
41  &kval);
42 
43  PG_RETURN_BOOL(v != NULL);
44 }
#define JB_FARRAY
Definition: jsonb.h:206
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
Definition: jsonb.h:215
char * val
Definition: jsonb.h:259
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define NULL
Definition: c.h:226
#define JB_FOBJECT
Definition: jsonb.h:205
enum jbvType type
Definition: jsonb.h:250
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
JsonbValue * findJsonbValueFromContainer(JsonbContainer *container, uint32 flags, JsonbValue *key)
Definition: jsonb_util.c:327
Definition: c.h:434
Datum jsonb_exists_all ( PG_FUNCTION_ARGS  )

Definition at line 80 of file jsonb_op.c.

References deconstruct_array(), findJsonbValueFromContainer(), i, JB_FARRAY, JB_FOBJECT, jbvString, NULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_JSONB, PG_RETURN_BOOL, Jsonb::root, strVal, TEXTOID, JsonbValue::type, JsonbValue::val, VARDATA, VARHDRSZ, and VARSIZE.

81 {
82  Jsonb *jb = PG_GETARG_JSONB(0);
84  int i;
85  Datum *key_datums;
86  bool *key_nulls;
87  int elem_count;
88 
89  deconstruct_array(keys, TEXTOID, -1, false, 'i', &key_datums, &key_nulls,
90  &elem_count);
91 
92  for (i = 0; i < elem_count; i++)
93  {
95 
96  if (key_nulls[i])
97  continue;
98 
99  strVal.type = jbvString;
100  strVal.val.string.val = VARDATA(key_datums[i]);
101  strVal.val.string.len = VARSIZE(key_datums[i]) - VARHDRSZ;
102 
105  &strVal) == NULL)
106  PG_RETURN_BOOL(false);
107  }
108 
109  PG_RETURN_BOOL(true);
110 }
#define JB_FARRAY
Definition: jsonb.h:206
#define VARDATA(PTR)
Definition: postgres.h:305
Definition: jsonb.h:215
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:440
char * val
Definition: jsonb.h:259
#define strVal(v)
Definition: value.h:54
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
#define JB_FOBJECT
Definition: jsonb.h:205
enum jbvType type
Definition: jsonb.h:250
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
int i
JsonbValue * findJsonbValueFromContainer(JsonbContainer *container, uint32 flags, JsonbValue *key)
Definition: jsonb_util.c:327
Datum jsonb_exists_any ( PG_FUNCTION_ARGS  )

Definition at line 47 of file jsonb_op.c.

References deconstruct_array(), findJsonbValueFromContainer(), i, JB_FARRAY, JB_FOBJECT, jbvString, NULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_JSONB, PG_RETURN_BOOL, Jsonb::root, strVal, TEXTOID, JsonbValue::type, JsonbValue::val, VARDATA, VARHDRSZ, and VARSIZE.

48 {
49  Jsonb *jb = PG_GETARG_JSONB(0);
51  int i;
52  Datum *key_datums;
53  bool *key_nulls;
54  int elem_count;
55 
56  deconstruct_array(keys, TEXTOID, -1, false, 'i', &key_datums, &key_nulls,
57  &elem_count);
58 
59  for (i = 0; i < elem_count; i++)
60  {
62 
63  if (key_nulls[i])
64  continue;
65 
66  strVal.type = jbvString;
67  strVal.val.string.val = VARDATA(key_datums[i]);
68  strVal.val.string.len = VARSIZE(key_datums[i]) - VARHDRSZ;
69 
72  &strVal) != NULL)
73  PG_RETURN_BOOL(true);
74  }
75 
76  PG_RETURN_BOOL(false);
77 }
#define JB_FARRAY
Definition: jsonb.h:206
#define VARDATA(PTR)
Definition: postgres.h:305
Definition: jsonb.h:215
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:440
char * val
Definition: jsonb.h:259
#define strVal(v)
Definition: value.h:54
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
#define JB_FOBJECT
Definition: jsonb.h:205
enum jbvType type
Definition: jsonb.h:250
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
int i
JsonbValue * findJsonbValueFromContainer(JsonbContainer *container, uint32 flags, JsonbValue *key)
Definition: jsonb_util.c:327
Datum jsonb_ge ( PG_FUNCTION_ARGS  )

Definition at line 209 of file jsonb_op.c.

References compareJsonbContainers(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_RETURN_BOOL, and Jsonb::root.

210 {
211  Jsonb *jba = PG_GETARG_JSONB(0);
212  Jsonb *jbb = PG_GETARG_JSONB(1);
213  bool res;
214 
215  res = (compareJsonbContainers(&jba->root, &jbb->root) >= 0);
216 
217  PG_FREE_IF_COPY(jba, 0);
218  PG_FREE_IF_COPY(jbb, 1);
219  PG_RETURN_BOOL(res);
220 }
Definition: jsonb.h:215
int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
Definition: jsonb_util.c:178
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
Datum jsonb_gt ( PG_FUNCTION_ARGS  )

Definition at line 181 of file jsonb_op.c.

References compareJsonbContainers(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_RETURN_BOOL, and Jsonb::root.

182 {
183  Jsonb *jba = PG_GETARG_JSONB(0);
184  Jsonb *jbb = PG_GETARG_JSONB(1);
185  bool res;
186 
187  res = (compareJsonbContainers(&jba->root, &jbb->root) > 0);
188 
189  PG_FREE_IF_COPY(jba, 0);
190  PG_FREE_IF_COPY(jbb, 1);
191  PG_RETURN_BOOL(res);
192 }
Definition: jsonb.h:215
int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
Definition: jsonb_util.c:178
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
Datum jsonb_hash ( PG_FUNCTION_ARGS  )

Definition at line 254 of file jsonb_op.c.

References elog, ERROR, hash(), JB_FARRAY, JB_FOBJECT, JB_ROOT_COUNT, JsonbHashScalarValue(), JsonbIteratorInit(), JsonbIteratorNext(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_RETURN_INT32, Jsonb::root, WJB_BEGIN_ARRAY, WJB_BEGIN_OBJECT, WJB_DONE, WJB_ELEM, WJB_END_ARRAY, WJB_END_OBJECT, WJB_KEY, and WJB_VALUE.

255 {
256  Jsonb *jb = PG_GETARG_JSONB(0);
257  JsonbIterator *it;
258  JsonbValue v;
260  uint32 hash = 0;
261 
262  if (JB_ROOT_COUNT(jb) == 0)
263  PG_RETURN_INT32(0);
264 
265  it = JsonbIteratorInit(&jb->root);
266 
267  while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
268  {
269  switch (r)
270  {
271  /* Rotation is left to JsonbHashScalarValue() */
272  case WJB_BEGIN_ARRAY:
273  hash ^= JB_FARRAY;
274  break;
275  case WJB_BEGIN_OBJECT:
276  hash ^= JB_FOBJECT;
277  break;
278  case WJB_KEY:
279  case WJB_VALUE:
280  case WJB_ELEM:
281  JsonbHashScalarValue(&v, &hash);
282  break;
283  case WJB_END_ARRAY:
284  case WJB_END_OBJECT:
285  break;
286  default:
287  elog(ERROR, "invalid JsonbIteratorNext rc: %d", (int) r);
288  }
289  }
290 
291  PG_FREE_IF_COPY(jb, 0);
292  PG_RETURN_INT32(hash);
293 }
#define JB_FARRAY
Definition: jsonb.h:206
Definition: jsonb.h:215
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
Definition: jsonb.h:22
#define JB_ROOT_COUNT(jbp_)
Definition: jsonb.h:222
void JsonbHashScalarValue(const JsonbValue *scalarVal, uint32 *hash)
Definition: jsonb_util.c:1214
#define ERROR
Definition: elog.h:43
unsigned int uint32
Definition: c.h:265
Definition: jsonb.h:23
JsonbIteratorToken
Definition: jsonb.h:20
JsonbContainer root
Definition: jsonb.h:218
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:718
#define JB_FOBJECT
Definition: jsonb.h:205
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
#define elog
Definition: elog.h:219
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:754
Definition: jsonb.h:25
Datum jsonb_le ( PG_FUNCTION_ARGS  )

Definition at line 195 of file jsonb_op.c.

References compareJsonbContainers(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_RETURN_BOOL, and Jsonb::root.

196 {
197  Jsonb *jba = PG_GETARG_JSONB(0);
198  Jsonb *jbb = PG_GETARG_JSONB(1);
199  bool res;
200 
201  res = (compareJsonbContainers(&jba->root, &jbb->root) <= 0);
202 
203  PG_FREE_IF_COPY(jba, 0);
204  PG_FREE_IF_COPY(jbb, 1);
205  PG_RETURN_BOOL(res);
206 }
Definition: jsonb.h:215
int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
Definition: jsonb_util.c:178
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
Datum jsonb_lt ( PG_FUNCTION_ARGS  )

Definition at line 167 of file jsonb_op.c.

References compareJsonbContainers(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_RETURN_BOOL, and Jsonb::root.

168 {
169  Jsonb *jba = PG_GETARG_JSONB(0);
170  Jsonb *jbb = PG_GETARG_JSONB(1);
171  bool res;
172 
173  res = (compareJsonbContainers(&jba->root, &jbb->root) < 0);
174 
175  PG_FREE_IF_COPY(jba, 0);
176  PG_FREE_IF_COPY(jbb, 1);
177  PG_RETURN_BOOL(res);
178 }
Definition: jsonb.h:215
int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
Definition: jsonb_util.c:178
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70
Datum jsonb_ne ( PG_FUNCTION_ARGS  )

Definition at line 150 of file jsonb_op.c.

References compareJsonbContainers(), PG_FREE_IF_COPY, PG_GETARG_JSONB, PG_RETURN_BOOL, and Jsonb::root.

151 {
152  Jsonb *jba = PG_GETARG_JSONB(0);
153  Jsonb *jbb = PG_GETARG_JSONB(1);
154  bool res;
155 
156  res = (compareJsonbContainers(&jba->root, &jbb->root) != 0);
157 
158  PG_FREE_IF_COPY(jba, 0);
159  PG_FREE_IF_COPY(jbb, 1);
160  PG_RETURN_BOOL(res);
161 }
Definition: jsonb.h:215
int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
Definition: jsonb_util.c:178
JsonbContainer root
Definition: jsonb.h:218
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
#define PG_GETARG_JSONB(x)
Definition: jsonb.h:70