PostgreSQL Source Code  git master
jsonb.h File Reference
#include "lib/stringinfo.h"
#include "utils/array.h"
#include "utils/numeric.h"
Include dependency graph for jsonb.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  JsonbContainer
 
struct  Jsonb
 
struct  JsonbValue
 
struct  JsonbPair
 
struct  JsonbParseState
 
struct  JsonbIterator
 

Macros

#define JsonbContainsStrategyNumber   7
 
#define JsonbExistsStrategyNumber   9
 
#define JsonbExistsAnyStrategyNumber   10
 
#define JsonbExistsAllStrategyNumber   11
 
#define JsonbJsonpathExistsStrategyNumber   15
 
#define JsonbJsonpathPredicateStrategyNumber   16
 
#define JGINFLAG_KEY   0x01 /* key (or string array element) */
 
#define JGINFLAG_NULL   0x02 /* null value */
 
#define JGINFLAG_BOOL   0x03 /* boolean value */
 
#define JGINFLAG_NUM   0x04 /* numeric value */
 
#define JGINFLAG_STR   0x05 /* string value (if not an array element) */
 
#define JGINFLAG_HASHED   0x10 /* OR'd into flag if value was hashed */
 
#define JGIN_MAXLENGTH   125 /* max length of text part before hashing */
 
#define DatumGetJsonbP(d)   ((Jsonb *) PG_DETOAST_DATUM(d))
 
#define DatumGetJsonbPCopy(d)   ((Jsonb *) PG_DETOAST_DATUM_COPY(d))
 
#define JsonbPGetDatum(p)   PointerGetDatum(p)
 
#define PG_GETARG_JSONB_P(x)   DatumGetJsonbP(PG_GETARG_DATUM(x))
 
#define PG_GETARG_JSONB_P_COPY(x)   DatumGetJsonbPCopy(PG_GETARG_DATUM(x))
 
#define PG_RETURN_JSONB_P(x)   PG_RETURN_POINTER(x)
 
#define JENTRY_OFFLENMASK   0x0FFFFFFF
 
#define JENTRY_TYPEMASK   0x70000000
 
#define JENTRY_HAS_OFF   0x80000000
 
#define JENTRY_ISSTRING   0x00000000
 
#define JENTRY_ISNUMERIC   0x10000000
 
#define JENTRY_ISBOOL_FALSE   0x20000000
 
#define JENTRY_ISBOOL_TRUE   0x30000000
 
#define JENTRY_ISNULL   0x40000000
 
#define JENTRY_ISCONTAINER   0x50000000 /* array or object */
 
#define JBE_OFFLENFLD(je_)   ((je_) & JENTRY_OFFLENMASK)
 
#define JBE_HAS_OFF(je_)   (((je_) & JENTRY_HAS_OFF) != 0)
 
#define JBE_ISSTRING(je_)   (((je_) & JENTRY_TYPEMASK) == JENTRY_ISSTRING)
 
#define JBE_ISNUMERIC(je_)   (((je_) & JENTRY_TYPEMASK) == JENTRY_ISNUMERIC)
 
#define JBE_ISCONTAINER(je_)   (((je_) & JENTRY_TYPEMASK) == JENTRY_ISCONTAINER)
 
#define JBE_ISNULL(je_)   (((je_) & JENTRY_TYPEMASK) == JENTRY_ISNULL)
 
#define JBE_ISBOOL_TRUE(je_)   (((je_) & JENTRY_TYPEMASK) == JENTRY_ISBOOL_TRUE)
 
#define JBE_ISBOOL_FALSE(je_)   (((je_) & JENTRY_TYPEMASK) == JENTRY_ISBOOL_FALSE)
 
#define JBE_ISBOOL(je_)   (JBE_ISBOOL_TRUE(je_) || JBE_ISBOOL_FALSE(je_))
 
#define JBE_ADVANCE_OFFSET(offset, je)
 
#define JB_OFFSET_STRIDE   32
 
#define JB_CMASK   0x0FFFFFFF /* mask for count field */
 
#define JB_FSCALAR   0x10000000 /* flag bits */
 
#define JB_FOBJECT   0x20000000
 
#define JB_FARRAY   0x40000000
 
#define JsonContainerSize(jc)   ((jc)->header & JB_CMASK)
 
#define JsonContainerIsScalar(jc)   (((jc)->header & JB_FSCALAR) != 0)
 
#define JsonContainerIsObject(jc)   (((jc)->header & JB_FOBJECT) != 0)
 
#define JsonContainerIsArray(jc)   (((jc)->header & JB_FARRAY) != 0)
 
#define JB_ROOT_COUNT(jbp_)   (*(uint32 *) VARDATA(jbp_) & JB_CMASK)
 
#define JB_ROOT_IS_SCALAR(jbp_)   ((*(uint32 *) VARDATA(jbp_) & JB_FSCALAR) != 0)
 
#define JB_ROOT_IS_OBJECT(jbp_)   ((*(uint32 *) VARDATA(jbp_) & JB_FOBJECT) != 0)
 
#define JB_ROOT_IS_ARRAY(jbp_)   ((*(uint32 *) VARDATA(jbp_) & JB_FARRAY) != 0)
 
#define IsAJsonbScalar(jsonbval)
 

Typedefs

typedef struct JsonbPair JsonbPair
 
typedef struct JsonbValue JsonbValue
 
typedef uint32 JEntry
 
typedef struct JsonbContainer JsonbContainer
 
typedef struct JsonbParseState JsonbParseState
 
typedef struct JsonbIterator JsonbIterator
 

Enumerations

enum  JsonbIteratorToken {
  WJB_DONE, WJB_KEY, WJB_VALUE, WJB_ELEM,
  WJB_BEGIN_ARRAY, WJB_END_ARRAY, WJB_BEGIN_OBJECT, WJB_END_OBJECT
}
 
enum  jbvType {
  jbvNull = 0x0, jbvString, jbvNumeric, jbvBool,
  jbvArray = 0x10, jbvObject, jbvBinary, jbvDatetime = 0x20
}
 
enum  JsonbIterState {
  JBI_ARRAY_START, JBI_ARRAY_ELEM, JBI_OBJECT_START, JBI_OBJECT_KEY,
  JBI_OBJECT_VALUE
}
 

Functions

uint32 getJsonbOffset (const JsonbContainer *jc, int index)
 
uint32 getJsonbLength (const JsonbContainer *jc, int index)
 
int compareJsonbContainers (JsonbContainer *a, JsonbContainer *b)
 
JsonbValuefindJsonbValueFromContainer (JsonbContainer *sheader, uint32 flags, JsonbValue *key)
 
JsonbValuegetKeyJsonValueFromContainer (JsonbContainer *container, const char *keyVal, int keyLen, JsonbValue *res)
 
JsonbValuegetIthJsonbValueFromContainer (JsonbContainer *sheader, uint32 i)
 
JsonbValuepushJsonbValue (JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
 
JsonbIteratorJsonbIteratorInit (JsonbContainer *container)
 
JsonbIteratorToken JsonbIteratorNext (JsonbIterator **it, JsonbValue *val, bool skipNested)
 
JsonbJsonbValueToJsonb (JsonbValue *val)
 
bool JsonbDeepContains (JsonbIterator **val, JsonbIterator **mContained)
 
void JsonbHashScalarValue (const JsonbValue *scalarVal, uint32 *hash)
 
void JsonbHashScalarValueExtended (const JsonbValue *scalarVal, uint64 *hash, uint64 seed)
 
char * JsonbToCString (StringInfo out, JsonbContainer *in, int estimated_len)
 
char * JsonbToCStringIndent (StringInfo out, JsonbContainer *in, int estimated_len)
 
bool JsonbExtractScalar (JsonbContainer *jbc, JsonbValue *res)
 
const char * JsonbTypeName (JsonbValue *jb)
 

Macro Definition Documentation

◆ DatumGetJsonbP

#define DatumGetJsonbP (   d)    ((Jsonb *) PG_DETOAST_DATUM(d))

Definition at line 71 of file jsonb.h.

Referenced by datum_to_jsonb().

◆ DatumGetJsonbPCopy

#define DatumGetJsonbPCopy (   d)    ((Jsonb *) PG_DETOAST_DATUM_COPY(d))

Definition at line 72 of file jsonb.h.

◆ IsAJsonbScalar

#define IsAJsonbScalar (   jsonbval)

◆ JB_CMASK

#define JB_CMASK   0x0FFFFFFF /* mask for count field */

Definition at line 208 of file jsonb.h.

◆ JB_FARRAY

◆ JB_FOBJECT

◆ JB_FSCALAR

#define JB_FSCALAR   0x10000000 /* flag bits */

Definition at line 209 of file jsonb.h.

Referenced by convertJsonbArray().

◆ JB_OFFSET_STRIDE

#define JB_OFFSET_STRIDE   32

Definition at line 186 of file jsonb.h.

Referenced by convertJsonbArray(), and convertJsonbObject().

◆ JB_ROOT_COUNT

◆ JB_ROOT_IS_ARRAY

#define JB_ROOT_IS_ARRAY (   jbp_)    ((*(uint32 *) VARDATA(jbp_) & JB_FARRAY) != 0)

◆ JB_ROOT_IS_OBJECT

#define JB_ROOT_IS_OBJECT (   jbp_)    ((*(uint32 *) VARDATA(jbp_) & JB_FOBJECT) != 0)

◆ JB_ROOT_IS_SCALAR

◆ JBE_ADVANCE_OFFSET

#define JBE_ADVANCE_OFFSET (   offset,
  je 
)
Value:
do { \
JEntry je_ = (je); \
if (JBE_HAS_OFF(je_)) \
(offset) = JBE_OFFLENFLD(je_); \
else \
(offset) += JBE_OFFLENFLD(je_); \
} while(0)
#define JBE_OFFLENFLD(je_)
Definition: jsonb.h:159
#define JBE_HAS_OFF(je_)
Definition: jsonb.h:160

Definition at line 170 of file jsonb.h.

Referenced by findJsonbValueFromContainer(), and JsonbIteratorNext().

◆ JBE_HAS_OFF

#define JBE_HAS_OFF (   je_)    (((je_) & JENTRY_HAS_OFF) != 0)

Definition at line 160 of file jsonb.h.

Referenced by getJsonbLength(), and getJsonbOffset().

◆ JBE_ISBOOL

#define JBE_ISBOOL (   je_)    (JBE_ISBOOL_TRUE(je_) || JBE_ISBOOL_FALSE(je_))

Definition at line 167 of file jsonb.h.

◆ JBE_ISBOOL_FALSE

#define JBE_ISBOOL_FALSE (   je_)    (((je_) & JENTRY_TYPEMASK) == JENTRY_ISBOOL_FALSE)

Definition at line 166 of file jsonb.h.

Referenced by fillJsonbValue().

◆ JBE_ISBOOL_TRUE

#define JBE_ISBOOL_TRUE (   je_)    (((je_) & JENTRY_TYPEMASK) == JENTRY_ISBOOL_TRUE)

Definition at line 165 of file jsonb.h.

Referenced by fillJsonbValue().

◆ JBE_ISCONTAINER

#define JBE_ISCONTAINER (   je_)    (((je_) & JENTRY_TYPEMASK) == JENTRY_ISCONTAINER)

Definition at line 163 of file jsonb.h.

Referenced by fillJsonbValue().

◆ JBE_ISNULL

#define JBE_ISNULL (   je_)    (((je_) & JENTRY_TYPEMASK) == JENTRY_ISNULL)

Definition at line 164 of file jsonb.h.

Referenced by fillJsonbValue().

◆ JBE_ISNUMERIC

#define JBE_ISNUMERIC (   je_)    (((je_) & JENTRY_TYPEMASK) == JENTRY_ISNUMERIC)

Definition at line 162 of file jsonb.h.

Referenced by fillJsonbValue().

◆ JBE_ISSTRING

#define JBE_ISSTRING (   je_)    (((je_) & JENTRY_TYPEMASK) == JENTRY_ISSTRING)

Definition at line 161 of file jsonb.h.

Referenced by fillJsonbValue().

◆ JBE_OFFLENFLD

#define JBE_OFFLENFLD (   je_)    ((je_) & JENTRY_OFFLENMASK)

Definition at line 159 of file jsonb.h.

Referenced by convertJsonbArray(), convertJsonbObject(), getJsonbLength(), and getJsonbOffset().

◆ JENTRY_HAS_OFF

#define JENTRY_HAS_OFF   0x80000000

Definition at line 148 of file jsonb.h.

Referenced by convertJsonbArray(), and convertJsonbObject().

◆ JENTRY_ISBOOL_FALSE

#define JENTRY_ISBOOL_FALSE   0x20000000

Definition at line 153 of file jsonb.h.

Referenced by convertJsonbScalar().

◆ JENTRY_ISBOOL_TRUE

#define JENTRY_ISBOOL_TRUE   0x30000000

Definition at line 154 of file jsonb.h.

Referenced by convertJsonbScalar().

◆ JENTRY_ISCONTAINER

#define JENTRY_ISCONTAINER   0x50000000 /* array or object */

Definition at line 156 of file jsonb.h.

Referenced by convertJsonbArray(), and convertJsonbObject().

◆ JENTRY_ISNULL

#define JENTRY_ISNULL   0x40000000

Definition at line 155 of file jsonb.h.

Referenced by convertJsonbScalar().

◆ JENTRY_ISNUMERIC

#define JENTRY_ISNUMERIC   0x10000000

Definition at line 152 of file jsonb.h.

Referenced by convertJsonbScalar().

◆ JENTRY_ISSTRING

#define JENTRY_ISSTRING   0x00000000

Definition at line 151 of file jsonb.h.

◆ JENTRY_OFFLENMASK

#define JENTRY_OFFLENMASK   0x0FFFFFFF

Definition at line 146 of file jsonb.h.

Referenced by checkStringLen(), convertJsonbArray(), and convertJsonbObject().

◆ JENTRY_TYPEMASK

#define JENTRY_TYPEMASK   0x70000000

Definition at line 147 of file jsonb.h.

Referenced by convertJsonbArray(), and convertJsonbObject().

◆ JGIN_MAXLENGTH

#define JGIN_MAXLENGTH   125 /* max length of text part before hashing */

Definition at line 68 of file jsonb.h.

Referenced by make_text_key().

◆ JGINFLAG_BOOL

#define JGINFLAG_BOOL   0x03 /* boolean value */

Definition at line 64 of file jsonb.h.

Referenced by make_scalar_key().

◆ JGINFLAG_HASHED

#define JGINFLAG_HASHED   0x10 /* OR'd into flag if value was hashed */

Definition at line 67 of file jsonb.h.

Referenced by make_text_key().

◆ JGINFLAG_KEY

#define JGINFLAG_KEY   0x01 /* key (or string array element) */

Definition at line 62 of file jsonb.h.

Referenced by gin_extract_jsonb_query(), jsonb_ops__add_path_item(), and make_scalar_key().

◆ JGINFLAG_NULL

#define JGINFLAG_NULL   0x02 /* null value */

Definition at line 63 of file jsonb.h.

Referenced by make_scalar_key().

◆ JGINFLAG_NUM

#define JGINFLAG_NUM   0x04 /* numeric value */

Definition at line 65 of file jsonb.h.

Referenced by make_scalar_key().

◆ JGINFLAG_STR

#define JGINFLAG_STR   0x05 /* string value (if not an array element) */

Definition at line 66 of file jsonb.h.

Referenced by make_scalar_key().

◆ JsonbContainsStrategyNumber

◆ JsonbExistsAllStrategyNumber

#define JsonbExistsAllStrategyNumber   11

Definition at line 36 of file jsonb.h.

Referenced by gin_consistent_jsonb(), gin_extract_jsonb_query(), and gin_triconsistent_jsonb().

◆ JsonbExistsAnyStrategyNumber

#define JsonbExistsAnyStrategyNumber   10

Definition at line 35 of file jsonb.h.

Referenced by gin_consistent_jsonb(), gin_extract_jsonb_query(), and gin_triconsistent_jsonb().

◆ JsonbExistsStrategyNumber

#define JsonbExistsStrategyNumber   9

Definition at line 34 of file jsonb.h.

Referenced by gin_consistent_jsonb(), gin_extract_jsonb_query(), and gin_triconsistent_jsonb().

◆ JsonbJsonpathExistsStrategyNumber

◆ JsonbJsonpathPredicateStrategyNumber

#define JsonbJsonpathPredicateStrategyNumber   16

◆ JsonbPGetDatum

#define JsonbPGetDatum (   p)    PointerGetDatum(p)

Definition at line 73 of file jsonb.h.

Referenced by jsonb_path_query_internal(), and populate_scalar().

◆ JsonContainerIsArray

◆ JsonContainerIsObject

◆ JsonContainerIsScalar

#define JsonContainerIsScalar (   jc)    (((jc)->header & JB_FSCALAR) != 0)

◆ JsonContainerSize

◆ PG_GETARG_JSONB_P

◆ PG_GETARG_JSONB_P_COPY

#define PG_GETARG_JSONB_P_COPY (   x)    DatumGetJsonbPCopy(PG_GETARG_DATUM(x))

Definition at line 75 of file jsonb.h.

Referenced by jsonb_path_query_internal().

◆ PG_RETURN_JSONB_P

Typedef Documentation

◆ JEntry

typedef uint32 JEntry

Definition at line 144 of file jsonb.h.

◆ JsonbContainer

◆ JsonbIterator

typedef struct JsonbIterator JsonbIterator

◆ JsonbPair

typedef struct JsonbPair JsonbPair

Definition at line 78 of file jsonb.h.

◆ JsonbParseState

◆ JsonbValue

typedef struct JsonbValue JsonbValue

Definition at line 79 of file jsonb.h.

Enumeration Type Documentation

◆ jbvType

enum jbvType
Enumerator
jbvNull 
jbvString 
jbvNumeric 
jbvBool 
jbvArray 
jbvObject 
jbvBinary 
jbvDatetime 

Definition at line 233 of file jsonb.h.

234 {
235  /* Scalar types */
236  jbvNull = 0x0,
237  jbvString,
238  jbvNumeric,
239  jbvBool,
240  /* Composite types */
241  jbvArray = 0x10,
242  jbvObject,
243  /* Binary (i.e. struct Jsonb) jbvArray/jbvObject */
244  jbvBinary,
245 
246  /*
247  * Virtual types.
248  *
249  * These types are used only for in-memory JSON processing and serialized
250  * into JSON strings when outputted to json/jsonb.
251  */
252  jbvDatetime = 0x20,
253 };
Definition: jsonb.h:239
Definition: jsonb.h:236

◆ JsonbIteratorToken

Enumerator
WJB_DONE 
WJB_KEY 
WJB_VALUE 
WJB_ELEM 
WJB_BEGIN_ARRAY 
WJB_END_ARRAY 
WJB_BEGIN_OBJECT 
WJB_END_OBJECT 

Definition at line 20 of file jsonb.h.

21 {
22  WJB_DONE,
23  WJB_KEY,
24  WJB_VALUE,
25  WJB_ELEM,
Definition: jsonb.h:22
Definition: jsonb.h:23
JsonbIteratorToken
Definition: jsonb.h:20
Definition: jsonb.h:25

◆ JsonbIterState

Enumerator
JBI_ARRAY_START 
JBI_ARRAY_ELEM 
JBI_OBJECT_START 
JBI_OBJECT_KEY 
JBI_OBJECT_VALUE 

Definition at line 338 of file jsonb.h.

Function Documentation

◆ compareJsonbContainers()

int compareJsonbContainers ( JsonbContainer a,
JsonbContainer b 
)

Definition at line 183 of file jsonb_util.c.

References Assert, compareJsonbScalarValue(), elog, ERROR, i, jbvArray, jbvBinary, jbvBool, jbvDatetime, jbvNull, jbvNumeric, jbvObject, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), JsonbIterator::parent, pfree(), JsonbValue::type, JsonbValue::val, WJB_DONE, WJB_END_ARRAY, and WJB_END_OBJECT.

Referenced by jsonb_cmp(), jsonb_eq(), jsonb_ge(), jsonb_gt(), jsonb_le(), jsonb_lt(), and jsonb_ne().

184 {
185  JsonbIterator *ita,
186  *itb;
187  int res = 0;
188 
189  ita = JsonbIteratorInit(a);
190  itb = JsonbIteratorInit(b);
191 
192  do
193  {
194  JsonbValue va,
195  vb;
197  rb;
198 
199  ra = JsonbIteratorNext(&ita, &va, false);
200  rb = JsonbIteratorNext(&itb, &vb, false);
201 
202  if (ra == rb)
203  {
204  if (ra == WJB_DONE)
205  {
206  /* Decisively equal */
207  break;
208  }
209 
210  if (ra == WJB_END_ARRAY || ra == WJB_END_OBJECT)
211  {
212  /*
213  * There is no array or object to compare at this stage of
214  * processing. jbvArray/jbvObject values are compared
215  * initially, at the WJB_BEGIN_ARRAY and WJB_BEGIN_OBJECT
216  * tokens.
217  */
218  continue;
219  }
220 
221  if (va.type == vb.type)
222  {
223  switch (va.type)
224  {
225  case jbvString:
226  case jbvNull:
227  case jbvNumeric:
228  case jbvBool:
229  res = compareJsonbScalarValue(&va, &vb);
230  break;
231  case jbvArray:
232 
233  /*
234  * This could be a "raw scalar" pseudo array. That's
235  * a special case here though, since we still want the
236  * general type-based comparisons to apply, and as far
237  * as we're concerned a pseudo array is just a scalar.
238  */
239  if (va.val.array.rawScalar != vb.val.array.rawScalar)
240  res = (va.val.array.rawScalar) ? -1 : 1;
241  if (va.val.array.nElems != vb.val.array.nElems)
242  res = (va.val.array.nElems > vb.val.array.nElems) ? 1 : -1;
243  break;
244  case jbvObject:
245  if (va.val.object.nPairs != vb.val.object.nPairs)
246  res = (va.val.object.nPairs > vb.val.object.nPairs) ? 1 : -1;
247  break;
248  case jbvBinary:
249  elog(ERROR, "unexpected jbvBinary value");
250  break;
251  case jbvDatetime:
252  elog(ERROR, "unexpected jbvDatetime value");
253  break;
254  }
255  }
256  else
257  {
258  /* Type-defined order */
259  res = (va.type > vb.type) ? 1 : -1;
260  }
261  }
262  else
263  {
264  /*
265  * It's safe to assume that the types differed, and that the va
266  * and vb values passed were set.
267  *
268  * If the two values were of the same container type, then there'd
269  * have been a chance to observe the variation in the number of
270  * elements/pairs (when processing WJB_BEGIN_OBJECT, say). They're
271  * either two heterogeneously-typed containers, or a container and
272  * some scalar type.
273  *
274  * We don't have to consider the WJB_END_ARRAY and WJB_END_OBJECT
275  * cases here, because we would have seen the corresponding
276  * WJB_BEGIN_ARRAY and WJB_BEGIN_OBJECT tokens first, and
277  * concluded that they don't match.
278  */
279  Assert(ra != WJB_END_ARRAY && ra != WJB_END_OBJECT);
280  Assert(rb != WJB_END_ARRAY && rb != WJB_END_OBJECT);
281 
282  Assert(va.type != vb.type);
283  Assert(va.type != jbvBinary);
284  Assert(vb.type != jbvBinary);
285  /* Type-defined order */
286  res = (va.type > vb.type) ? 1 : -1;
287  }
288  }
289  while (res == 0);
290 
291  while (ita != NULL)
292  {
293  JsonbIterator *i = ita->parent;
294 
295  pfree(ita);
296  ita = i;
297  }
298  while (itb != NULL)
299  {
300  JsonbIterator *i = itb->parent;
301 
302  pfree(itb);
303  itb = i;
304  }
305 
306  return res;
307 }
char * val
Definition: jsonb.h:272
Definition: jsonb.h:239
Definition: jsonb.h:22
Definition: jsonb.h:236
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
JsonbIteratorToken
Definition: jsonb.h:20
static int compareJsonbScalarValue(JsonbValue *a, JsonbValue *b)
Definition: jsonb_util.c:1375
#define Assert(condition)
Definition: c.h:733
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:759
struct JsonbIterator * parent
Definition: jsonb.h:374
enum jbvType type
Definition: jsonb.h:263
#define elog(elevel,...)
Definition: elog.h:228
int i
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:795

◆ findJsonbValueFromContainer()

JsonbValue* findJsonbValueFromContainer ( JsonbContainer sheader,
uint32  flags,
JsonbValue key 
)

Definition at line 336 of file jsonb_util.c.

References Assert, JsonbContainer::children, equalsJsonbScalarValue(), fillJsonbValue(), getKeyJsonValueFromContainer(), i, JB_FARRAY, JB_FOBJECT, JBE_ADVANCE_OFFSET, jbvString, JsonContainerIsArray, JsonContainerIsObject, JsonContainerSize, palloc(), pfree(), JsonbValue::type, and JsonbValue::val.

Referenced by executeItemOptUnwrapTarget(), getJsonPathVariable(), jsonb_exists(), jsonb_exists_all(), jsonb_exists_any(), and JsonbDeepContains().

338 {
339  JEntry *children = container->children;
340  int count = JsonContainerSize(container);
341 
342  Assert((flags & ~(JB_FARRAY | JB_FOBJECT)) == 0);
343 
344  /* Quick out without a palloc cycle if object/array is empty */
345  if (count <= 0)
346  return NULL;
347 
348  if ((flags & JB_FARRAY) && JsonContainerIsArray(container))
349  {
350  JsonbValue *result = palloc(sizeof(JsonbValue));
351  char *base_addr = (char *) (children + count);
352  uint32 offset = 0;
353  int i;
354 
355  for (i = 0; i < count; i++)
356  {
357  fillJsonbValue(container, i, base_addr, offset, result);
358 
359  if (key->type == result->type)
360  {
361  if (equalsJsonbScalarValue(key, result))
362  return result;
363  }
364 
365  JBE_ADVANCE_OFFSET(offset, children[i]);
366  }
367 
368  pfree(result);
369  }
370  else if ((flags & JB_FOBJECT) && JsonContainerIsObject(container))
371  {
372  /* Object key passed by caller must be a string */
373  Assert(key->type == jbvString);
374 
375  return getKeyJsonValueFromContainer(container, key->val.string.val,
376  key->val.string.len, NULL);
377  }
378 
379  /* Not found */
380  return NULL;
381 }
#define JB_FARRAY
Definition: jsonb.h:211
char * val
Definition: jsonb.h:272
JsonbValue * getKeyJsonValueFromContainer(JsonbContainer *container, const char *keyVal, int keyLen, JsonbValue *res)
Definition: jsonb_util.c:390
#define JBE_ADVANCE_OFFSET(offset, je)
Definition: jsonb.h:170
void pfree(void *pointer)
Definition: mcxt.c:1056
#define JsonContainerSize(jc)
Definition: jsonb.h:214
unsigned int uint32
Definition: c.h:359
static void fillJsonbValue(JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result)
Definition: jsonb_util.c:497
#define JsonContainerIsArray(jc)
Definition: jsonb.h:217
static bool equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b)
Definition: jsonb_util.c:1343
#define JsonContainerIsObject(jc)
Definition: jsonb.h:216
#define Assert(condition)
Definition: c.h:733
#define JB_FOBJECT
Definition: jsonb.h:210
uint32 JEntry
Definition: jsonb.h:144
enum jbvType type
Definition: jsonb.h:263
void * palloc(Size size)
Definition: mcxt.c:949
int i

◆ getIthJsonbValueFromContainer()

JsonbValue* getIthJsonbValueFromContainer ( JsonbContainer sheader,
uint32  i 
)

Definition at line 460 of file jsonb_util.c.

References JsonbContainer::children, elog, ERROR, fillJsonbValue(), getJsonbOffset(), JsonContainerIsArray, JsonContainerSize, and palloc().

Referenced by executeItemOptUnwrapTarget(), get_jsonb_path_all(), jsonb_array_element(), and jsonb_array_element_text().

461 {
462  JsonbValue *result;
463  char *base_addr;
464  uint32 nelements;
465 
466  if (!JsonContainerIsArray(container))
467  elog(ERROR, "not a jsonb array");
468 
469  nelements = JsonContainerSize(container);
470  base_addr = (char *) &container->children[nelements];
471 
472  if (i >= nelements)
473  return NULL;
474 
475  result = palloc(sizeof(JsonbValue));
476 
477  fillJsonbValue(container, i, base_addr,
478  getJsonbOffset(container, i),
479  result);
480 
481  return result;
482 }
uint32 getJsonbOffset(const JsonbContainer *jc, int index)
Definition: jsonb_util.c:126
#define ERROR
Definition: elog.h:43
#define JsonContainerSize(jc)
Definition: jsonb.h:214
unsigned int uint32
Definition: c.h:359
static void fillJsonbValue(JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result)
Definition: jsonb_util.c:497
#define JsonContainerIsArray(jc)
Definition: jsonb.h:217
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
int i

◆ getJsonbLength()

uint32 getJsonbLength ( const JsonbContainer jc,
int  index 
)

Definition at line 151 of file jsonb_util.c.

References JsonbContainer::children, getJsonbOffset(), JBE_HAS_OFF, and JBE_OFFLENFLD.

Referenced by fillJsonbValue(), and getKeyJsonValueFromContainer().

152 {
153  uint32 off;
154  uint32 len;
155 
156  /*
157  * If the length is stored directly in the JEntry, just return it.
158  * Otherwise, get the begin offset of the entry, and subtract that from
159  * the stored end+1 offset.
160  */
161  if (JBE_HAS_OFF(jc->children[index]))
162  {
163  off = getJsonbOffset(jc, index);
164  len = JBE_OFFLENFLD(jc->children[index]) - off;
165  }
166  else
167  len = JBE_OFFLENFLD(jc->children[index]);
168 
169  return len;
170 }
JEntry children[FLEXIBLE_ARRAY_MEMBER]
Definition: jsonb.h:202
uint32 getJsonbOffset(const JsonbContainer *jc, int index)
Definition: jsonb_util.c:126
#define JBE_OFFLENFLD(je_)
Definition: jsonb.h:159
Definition: type.h:89
unsigned int uint32
Definition: c.h:359
#define JBE_HAS_OFF(je_)
Definition: jsonb.h:160

◆ getJsonbOffset()

uint32 getJsonbOffset ( const JsonbContainer jc,
int  index 
)

Definition at line 126 of file jsonb_util.c.

References JsonbContainer::children, i, JBE_HAS_OFF, and JBE_OFFLENFLD.

Referenced by getIthJsonbValueFromContainer(), getJsonbLength(), getKeyJsonValueFromContainer(), and JsonbIteratorNext().

127 {
128  uint32 offset = 0;
129  int i;
130 
131  /*
132  * Start offset of this entry is equal to the end offset of the previous
133  * entry. Walk backwards to the most recent entry stored as an end
134  * offset, returning that offset plus any lengths in between.
135  */
136  for (i = index - 1; i >= 0; i--)
137  {
138  offset += JBE_OFFLENFLD(jc->children[i]);
139  if (JBE_HAS_OFF(jc->children[i]))
140  break;
141  }
142 
143  return offset;
144 }
JEntry children[FLEXIBLE_ARRAY_MEMBER]
Definition: jsonb.h:202
#define JBE_OFFLENFLD(je_)
Definition: jsonb.h:159
Definition: type.h:89
unsigned int uint32
Definition: c.h:359
#define JBE_HAS_OFF(je_)
Definition: jsonb.h:160
int i

◆ getKeyJsonValueFromContainer()

JsonbValue* getKeyJsonValueFromContainer ( JsonbContainer container,
const char *  keyVal,
int  keyLen,
JsonbValue res 
)

Definition at line 390 of file jsonb_util.c.

References Assert, JsonbContainer::children, difference(), fillJsonbValue(), getJsonbLength(), getJsonbOffset(), JsonContainerIsObject, JsonContainerSize, lengthCompareJsonbString(), and palloc().

Referenced by findJsonbValueFromContainer(), get_jsonb_path_all(), JsObjectGetField(), jsonb_object_field(), jsonb_object_field_text(), and JsonbDeepContains().

392 {
393  JEntry *children = container->children;
394  int count = JsonContainerSize(container);
395  char *baseAddr;
396  uint32 stopLow,
397  stopHigh;
398 
399  Assert(JsonContainerIsObject(container));
400 
401  /* Quick out without a palloc cycle if object is empty */
402  if (count <= 0)
403  return NULL;
404 
405  /*
406  * Binary search the container. Since we know this is an object, account
407  * for *Pairs* of Jentrys
408  */
409  baseAddr = (char *) (children + count * 2);
410  stopLow = 0;
411  stopHigh = count;
412  while (stopLow < stopHigh)
413  {
414  uint32 stopMiddle;
415  int difference;
416  const char *candidateVal;
417  int candidateLen;
418 
419  stopMiddle = stopLow + (stopHigh - stopLow) / 2;
420 
421  candidateVal = baseAddr + getJsonbOffset(container, stopMiddle);
422  candidateLen = getJsonbLength(container, stopMiddle);
423 
424  difference = lengthCompareJsonbString(candidateVal, candidateLen,
425  keyVal, keyLen);
426 
427  if (difference == 0)
428  {
429  /* Found our key, return corresponding value */
430  int index = stopMiddle + count;
431 
432  if (!res)
433  res = palloc(sizeof(JsonbValue));
434 
435  fillJsonbValue(container, index, baseAddr,
436  getJsonbOffset(container, index),
437  res);
438 
439  return res;
440  }
441  else
442  {
443  if (difference < 0)
444  stopLow = stopMiddle + 1;
445  else
446  stopHigh = stopMiddle;
447  }
448  }
449 
450  /* Not found */
451  return NULL;
452 }
JEntry children[FLEXIBLE_ARRAY_MEMBER]
Definition: jsonb.h:202
uint32 getJsonbOffset(const JsonbContainer *jc, int index)
Definition: jsonb_util.c:126
uint32 getJsonbLength(const JsonbContainer *jc, int index)
Definition: jsonb_util.c:151
static int lengthCompareJsonbString(const char *val1, int len1, const char *val2, int len2)
Definition: jsonb_util.c:1849
Definition: type.h:89
#define JsonContainerSize(jc)
Definition: jsonb.h:214
unsigned int uint32
Definition: c.h:359
static void fillJsonbValue(JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result)
Definition: jsonb_util.c:497
#define JsonContainerIsObject(jc)
Definition: jsonb.h:216
Datum difference(PG_FUNCTION_ARGS)
#define Assert(condition)
Definition: c.h:733
uint32 JEntry
Definition: jsonb.h:144
void * palloc(Size size)
Definition: mcxt.c:949

◆ JsonbDeepContains()

bool JsonbDeepContains ( JsonbIterator **  val,
JsonbIterator **  mContained 
)

Definition at line 1004 of file jsonb_util.c.

References Assert, check_stack_depth(), elog, equalsJsonbScalarValue(), ERROR, findJsonbValueFromContainer(), getKeyJsonValueFromContainer(), i, IsAJsonbScalar, JB_FARRAY, jbvArray, jbvBinary, jbvObject, jbvString, JsonbDeepContains(), JsonbIteratorInit(), JsonbIteratorNext(), palloc(), pfree(), JsonbValue::type, JsonbValue::val, WJB_BEGIN_ARRAY, WJB_BEGIN_OBJECT, WJB_ELEM, WJB_END_ARRAY, WJB_END_OBJECT, WJB_KEY, and WJB_VALUE.

Referenced by jsonb_contained(), jsonb_contains(), and JsonbDeepContains().

1005 {
1006  JsonbValue vval,
1007  vcontained;
1008  JsonbIteratorToken rval,
1009  rcont;
1010 
1011  /*
1012  * Guard against stack overflow due to overly complex Jsonb.
1013  *
1014  * Functions called here independently take this precaution, but that
1015  * might not be sufficient since this is also a recursive function.
1016  */
1018 
1019  rval = JsonbIteratorNext(val, &vval, false);
1020  rcont = JsonbIteratorNext(mContained, &vcontained, false);
1021 
1022  if (rval != rcont)
1023  {
1024  /*
1025  * The differing return values can immediately be taken as indicating
1026  * two differing container types at this nesting level, which is
1027  * sufficient reason to give up entirely (but it should be the case
1028  * that they're both some container type).
1029  */
1030  Assert(rval == WJB_BEGIN_OBJECT || rval == WJB_BEGIN_ARRAY);
1031  Assert(rcont == WJB_BEGIN_OBJECT || rcont == WJB_BEGIN_ARRAY);
1032  return false;
1033  }
1034  else if (rcont == WJB_BEGIN_OBJECT)
1035  {
1036  Assert(vval.type == jbvObject);
1037  Assert(vcontained.type == jbvObject);
1038 
1039  /*
1040  * If the lhs has fewer pairs than the rhs, it can't possibly contain
1041  * the rhs. (This conclusion is safe only because we de-duplicate
1042  * keys in all Jsonb objects; thus there can be no corresponding
1043  * optimization in the array case.) The case probably won't arise
1044  * often, but since it's such a cheap check we may as well make it.
1045  */
1046  if (vval.val.object.nPairs < vcontained.val.object.nPairs)
1047  return false;
1048 
1049  /* Work through rhs "is it contained within?" object */
1050  for (;;)
1051  {
1052  JsonbValue *lhsVal; /* lhsVal is from pair in lhs object */
1053  JsonbValue lhsValBuf;
1054 
1055  rcont = JsonbIteratorNext(mContained, &vcontained, false);
1056 
1057  /*
1058  * When we get through caller's rhs "is it contained within?"
1059  * object without failing to find one of its values, it's
1060  * contained.
1061  */
1062  if (rcont == WJB_END_OBJECT)
1063  return true;
1064 
1065  Assert(rcont == WJB_KEY);
1066  Assert(vcontained.type == jbvString);
1067 
1068  /* First, find value by key... */
1069  lhsVal =
1070  getKeyJsonValueFromContainer((*val)->container,
1071  vcontained.val.string.val,
1072  vcontained.val.string.len,
1073  &lhsValBuf);
1074  if (!lhsVal)
1075  return false;
1076 
1077  /*
1078  * ...at this stage it is apparent that there is at least a key
1079  * match for this rhs pair.
1080  */
1081  rcont = JsonbIteratorNext(mContained, &vcontained, true);
1082 
1083  Assert(rcont == WJB_VALUE);
1084 
1085  /*
1086  * Compare rhs pair's value with lhs pair's value just found using
1087  * key
1088  */
1089  if (lhsVal->type != vcontained.type)
1090  {
1091  return false;
1092  }
1093  else if (IsAJsonbScalar(lhsVal))
1094  {
1095  if (!equalsJsonbScalarValue(lhsVal, &vcontained))
1096  return false;
1097  }
1098  else
1099  {
1100  /* Nested container value (object or array) */
1101  JsonbIterator *nestval,
1102  *nestContained;
1103 
1104  Assert(lhsVal->type == jbvBinary);
1105  Assert(vcontained.type == jbvBinary);
1106 
1107  nestval = JsonbIteratorInit(lhsVal->val.binary.data);
1108  nestContained = JsonbIteratorInit(vcontained.val.binary.data);
1109 
1110  /*
1111  * Match "value" side of rhs datum object's pair recursively.
1112  * It's a nested structure.
1113  *
1114  * Note that nesting still has to "match up" at the right
1115  * nesting sub-levels. However, there need only be zero or
1116  * more matching pairs (or elements) at each nesting level
1117  * (provided the *rhs* pairs/elements *all* match on each
1118  * level), which enables searching nested structures for a
1119  * single String or other primitive type sub-datum quite
1120  * effectively (provided the user constructed the rhs nested
1121  * structure such that we "know where to look").
1122  *
1123  * In other words, the mapping of container nodes in the rhs
1124  * "vcontained" Jsonb to internal nodes on the lhs is
1125  * injective, and parent-child edges on the rhs must be mapped
1126  * to parent-child edges on the lhs to satisfy the condition
1127  * of containment (plus of course the mapped nodes must be
1128  * equal).
1129  */
1130  if (!JsonbDeepContains(&nestval, &nestContained))
1131  return false;
1132  }
1133  }
1134  }
1135  else if (rcont == WJB_BEGIN_ARRAY)
1136  {
1137  JsonbValue *lhsConts = NULL;
1138  uint32 nLhsElems = vval.val.array.nElems;
1139 
1140  Assert(vval.type == jbvArray);
1141  Assert(vcontained.type == jbvArray);
1142 
1143  /*
1144  * Handle distinction between "raw scalar" pseudo arrays, and real
1145  * arrays.
1146  *
1147  * A raw scalar may contain another raw scalar, and an array may
1148  * contain a raw scalar, but a raw scalar may not contain an array. We
1149  * don't do something like this for the object case, since objects can
1150  * only contain pairs, never raw scalars (a pair is represented by an
1151  * rhs object argument with a single contained pair).
1152  */
1153  if (vval.val.array.rawScalar && !vcontained.val.array.rawScalar)
1154  return false;
1155 
1156  /* Work through rhs "is it contained within?" array */
1157  for (;;)
1158  {
1159  rcont = JsonbIteratorNext(mContained, &vcontained, true);
1160 
1161  /*
1162  * When we get through caller's rhs "is it contained within?"
1163  * array without failing to find one of its values, it's
1164  * contained.
1165  */
1166  if (rcont == WJB_END_ARRAY)
1167  return true;
1168 
1169  Assert(rcont == WJB_ELEM);
1170 
1171  if (IsAJsonbScalar(&vcontained))
1172  {
1173  if (!findJsonbValueFromContainer((*val)->container,
1174  JB_FARRAY,
1175  &vcontained))
1176  return false;
1177  }
1178  else
1179  {
1180  uint32 i;
1181 
1182  /*
1183  * If this is first container found in rhs array (at this
1184  * depth), initialize temp lhs array of containers
1185  */
1186  if (lhsConts == NULL)
1187  {
1188  uint32 j = 0;
1189 
1190  /* Make room for all possible values */
1191  lhsConts = palloc(sizeof(JsonbValue) * nLhsElems);
1192 
1193  for (i = 0; i < nLhsElems; i++)
1194  {
1195  /* Store all lhs elements in temp array */
1196  rcont = JsonbIteratorNext(val, &vval, true);
1197  Assert(rcont == WJB_ELEM);
1198 
1199  if (vval.type == jbvBinary)
1200  lhsConts[j++] = vval;
1201  }
1202 
1203  /* No container elements in temp array, so give up now */
1204  if (j == 0)
1205  return false;
1206 
1207  /* We may have only partially filled array */
1208  nLhsElems = j;
1209  }
1210 
1211  /* XXX: Nested array containment is O(N^2) */
1212  for (i = 0; i < nLhsElems; i++)
1213  {
1214  /* Nested container value (object or array) */
1215  JsonbIterator *nestval,
1216  *nestContained;
1217  bool contains;
1218 
1219  nestval = JsonbIteratorInit(lhsConts[i].val.binary.data);
1220  nestContained = JsonbIteratorInit(vcontained.val.binary.data);
1221 
1222  contains = JsonbDeepContains(&nestval, &nestContained);
1223 
1224  if (nestval)
1225  pfree(nestval);
1226  if (nestContained)
1227  pfree(nestContained);
1228  if (contains)
1229  break;
1230  }
1231 
1232  /*
1233  * Report rhs container value is not contained if couldn't
1234  * match rhs container to *some* lhs cont
1235  */
1236  if (i == nLhsElems)
1237  return false;
1238  }
1239  }
1240  }
1241  else
1242  {
1243  elog(ERROR, "invalid jsonb container type");
1244  }
1245 
1246  elog(ERROR, "unexpectedly fell off end of jsonb container");
1247  return false;
1248 }
#define JB_FARRAY
Definition: jsonb.h:211
bool JsonbDeepContains(JsonbIterator **val, JsonbIterator **mContained)
Definition: jsonb_util.c:1004
char * val
Definition: jsonb.h:272
JsonbValue * getKeyJsonValueFromContainer(JsonbContainer *container, const char *keyVal, int keyLen, JsonbValue *res)
Definition: jsonb_util.c:390
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
void check_stack_depth(void)
Definition: postgres.c:3284
unsigned int uint32
Definition: c.h:359
Definition: jsonb.h:23
static bool equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b)
Definition: jsonb_util.c:1343
JsonbIteratorToken
Definition: jsonb.h:20
#define Assert(condition)
Definition: c.h:733
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:759
#define IsAJsonbScalar(jsonbval)
Definition: jsonb.h:305
enum jbvType type
Definition: jsonb.h:263
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
int i
JsonbValue * findJsonbValueFromContainer(JsonbContainer *container, uint32 flags, JsonbValue *key)
Definition: jsonb_util.c:336
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:795
Definition: jsonb.h:25

◆ JsonbExtractScalar()

bool JsonbExtractScalar ( JsonbContainer jbc,
JsonbValue res 
)

Definition at line 1895 of file jsonb.c.

References Assert, IsAJsonbScalar, jbvArray, jbvObject, JsonbIteratorInit(), JsonbIteratorNext(), JsonContainerIsArray, JsonContainerIsScalar, PG_USED_FOR_ASSERTS_ONLY, JsonbValue::type, JsonbValue::val, WJB_BEGIN_ARRAY, WJB_DONE, WJB_ELEM, and WJB_END_ARRAY.

Referenced by executeJsonPath(), jsonb_bool(), jsonb_float4(), jsonb_float8(), jsonb_int2(), jsonb_int4(), jsonb_int8(), jsonb_numeric(), and JsonbContainerTypeName().

1896 {
1897  JsonbIterator *it;
1899  JsonbValue tmp;
1900 
1901  if (!JsonContainerIsArray(jbc) || !JsonContainerIsScalar(jbc))
1902  {
1903  /* inform caller about actual type of container */
1904  res->type = (JsonContainerIsArray(jbc)) ? jbvArray : jbvObject;
1905  return false;
1906  }
1907 
1908  /*
1909  * A root scalar is stored as an array of one element, so we get the array
1910  * and then its first (and only) member.
1911  */
1912  it = JsonbIteratorInit(jbc);
1913 
1914  tok = JsonbIteratorNext(&it, &tmp, true);
1915  Assert(tok == WJB_BEGIN_ARRAY);
1916  Assert(tmp.val.array.nElems == 1 && tmp.val.array.rawScalar);
1917 
1918  tok = JsonbIteratorNext(&it, res, true);
1919  Assert(tok == WJB_ELEM);
1920  Assert(IsAJsonbScalar(res));
1921 
1922  tok = JsonbIteratorNext(&it, &tmp, true);
1923  Assert(tok == WJB_END_ARRAY);
1924 
1925  tok = JsonbIteratorNext(&it, &tmp, true);
1926  Assert(tok == WJB_DONE);
1927 
1928  return true;
1929 }
char * val
Definition: jsonb.h:272
#define JsonContainerIsScalar(jc)
Definition: jsonb.h:215
Definition: jsonb.h:22
#define JsonContainerIsArray(jc)
Definition: jsonb.h:217
JsonbIteratorToken
Definition: jsonb.h:20
#define Assert(condition)
Definition: c.h:733
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:759
#define IsAJsonbScalar(jsonbval)
Definition: jsonb.h:305
enum jbvType type
Definition: jsonb.h:263
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:123
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:795
Definition: jsonb.h:25

◆ JsonbHashScalarValue()

void JsonbHashScalarValue ( const JsonbValue scalarVal,
uint32 hash 
)

Definition at line 1258 of file jsonb_util.c.

References DatumGetUInt32, DirectFunctionCall1, elog, ERROR, hash_any(), hash_numeric(), jbvBool, jbvNull, jbvNumeric, jbvString, NumericGetDatum, JsonbValue::type, and JsonbValue::val.

Referenced by gin_extract_jsonb_path(), jsonb_hash(), jsonb_path_ops__add_path_item(), and jsonb_path_ops__extract_nodes().

1259 {
1260  uint32 tmp;
1261 
1262  /* Compute hash value for scalarVal */
1263  switch (scalarVal->type)
1264  {
1265  case jbvNull:
1266  tmp = 0x01;
1267  break;
1268  case jbvString:
1269  tmp = DatumGetUInt32(hash_any((const unsigned char *) scalarVal->val.string.val,
1270  scalarVal->val.string.len));
1271  break;
1272  case jbvNumeric:
1273  /* Must hash equal numerics to equal hash codes */
1275  NumericGetDatum(scalarVal->val.numeric)));
1276  break;
1277  case jbvBool:
1278  tmp = scalarVal->val.boolean ? 0x02 : 0x04;
1279 
1280  break;
1281  default:
1282  elog(ERROR, "invalid jsonb scalar type");
1283  tmp = 0; /* keep compiler quiet */
1284  break;
1285  }
1286 
1287  /*
1288  * Combine hash values of successive keys, values and elements by rotating
1289  * the previous value left 1 bit, then XOR'ing in the new
1290  * key/value/element's hash value.
1291  */
1292  *hash = (*hash << 1) | (*hash >> 31);
1293  *hash ^= tmp;
1294 }
#define DatumGetUInt32(X)
Definition: postgres.h:486
Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.c:148
Datum hash_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2259
char * val
Definition: jsonb.h:272
#define NumericGetDatum(X)
Definition: numeric.h:51
Definition: jsonb.h:239
Definition: jsonb.h:236
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
#define ERROR
Definition: elog.h:43
unsigned int uint32
Definition: c.h:359
enum jbvType type
Definition: jsonb.h:263
#define elog(elevel,...)
Definition: elog.h:228
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541

◆ JsonbHashScalarValueExtended()

void JsonbHashScalarValueExtended ( const JsonbValue scalarVal,
uint64 *  hash,
uint64  seed 
)

Definition at line 1301 of file jsonb_util.c.

References BoolGetDatum, DatumGetUInt64, DirectFunctionCall2, elog, ERROR, hash_any_extended(), hash_numeric_extended(), hashcharextended(), jbvBool, jbvNull, jbvNumeric, jbvString, NumericGetDatum, ROTATE_HIGH_AND_LOW_32BITS, JsonbValue::type, UInt64GetDatum, and JsonbValue::val.

Referenced by jsonb_hash_extended().

1303 {
1304  uint64 tmp;
1305 
1306  switch (scalarVal->type)
1307  {
1308  case jbvNull:
1309  tmp = seed + 0x01;
1310  break;
1311  case jbvString:
1312  tmp = DatumGetUInt64(hash_any_extended((const unsigned char *) scalarVal->val.string.val,
1313  scalarVal->val.string.len,
1314  seed));
1315  break;
1316  case jbvNumeric:
1318  NumericGetDatum(scalarVal->val.numeric),
1319  UInt64GetDatum(seed)));
1320  break;
1321  case jbvBool:
1322  if (seed)
1324  BoolGetDatum(scalarVal->val.boolean),
1325  UInt64GetDatum(seed)));
1326  else
1327  tmp = scalarVal->val.boolean ? 0x02 : 0x04;
1328 
1329  break;
1330  default:
1331  elog(ERROR, "invalid jsonb scalar type");
1332  break;
1333  }
1334 
1336  *hash ^= tmp;
1337 }
Datum hash_numeric_extended(PG_FUNCTION_ARGS)
Definition: numeric.c:2339
#define UInt64GetDatum(X)
Definition: postgres.h:648
char * val
Definition: jsonb.h:272
#define NumericGetDatum(X)
Definition: numeric.h:51
Definition: jsonb.h:239
Definition: jsonb.h:236
Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.c:374
#define ERROR
Definition: elog.h:43
#define ROTATE_HIGH_AND_LOW_32BITS(v)
Definition: hashutils.h:18
#define BoolGetDatum(X)
Definition: postgres.h:402
#define DatumGetUInt64(X)
Definition: postgres.h:634
enum jbvType type
Definition: jsonb.h:263
Datum hashcharextended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:53
#define elog(elevel,...)
Definition: elog.h:228
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617

◆ JsonbIteratorInit()

◆ JsonbIteratorNext()

JsonbIteratorToken JsonbIteratorNext ( JsonbIterator **  it,
JsonbValue val,
bool  skipNested 
)

Definition at line 795 of file jsonb_util.c.

References elog, ERROR, fillJsonbValue(), freeAndGetParent(), getJsonbOffset(), IsAJsonbScalar, iteratorFromContainer(), JBE_ADVANCE_OFFSET, JBI_ARRAY_ELEM, JBI_ARRAY_START, JBI_OBJECT_KEY, JBI_OBJECT_START, JBI_OBJECT_VALUE, jbvArray, jbvObject, jbvString, JsonbValue::type, JsonbValue::val, WJB_BEGIN_ARRAY, WJB_BEGIN_OBJECT, WJB_DONE, WJB_ELEM, WJB_END_ARRAY, WJB_END_OBJECT, WJB_KEY, and WJB_VALUE.

Referenced by addJsonbToParseState(), compareJsonbContainers(), datum_to_jsonb(), each_worker_jsonb(), elements_worker_jsonb(), executeAnyItem(), executeKeyValueMethod(), gin_extract_jsonb(), gin_extract_jsonb_path(), iterate_jsonb_values(), IteratorConcat(), jsonb_agg_transfn(), jsonb_delete(), jsonb_delete_array(), jsonb_delete_idx(), jsonb_hash(), jsonb_hash_extended(), jsonb_object_agg_transfn(), jsonb_object_keys(), jsonb_strip_nulls(), Jsonb_to_SV(), JsonbDeepContains(), JsonbExtractScalar(), JsonbToCStringWorker(), parse_jsonb_index_flags(), PLyObject_FromJsonbContainer(), populate_array_dim_jsonb(), populate_recordset_worker(), pushJsonbValue(), setPath(), setPathArray(), setPathObject(), and transform_jsonb_string_values().

796 {
797  if (*it == NULL)
798  return WJB_DONE;
799 
800  /*
801  * When stepping into a nested container, we jump back here to start
802  * processing the child. We will not recurse further in one call, because
803  * processing the child will always begin in JBI_ARRAY_START or
804  * JBI_OBJECT_START state.
805  */
806 recurse:
807  switch ((*it)->state)
808  {
809  case JBI_ARRAY_START:
810  /* Set v to array on first array call */
811  val->type = jbvArray;
812  val->val.array.nElems = (*it)->nElems;
813 
814  /*
815  * v->val.array.elems is not actually set, because we aren't doing
816  * a full conversion
817  */
818  val->val.array.rawScalar = (*it)->isScalar;
819  (*it)->curIndex = 0;
820  (*it)->curDataOffset = 0;
821  (*it)->curValueOffset = 0; /* not actually used */
822  /* Set state for next call */
823  (*it)->state = JBI_ARRAY_ELEM;
824  return WJB_BEGIN_ARRAY;
825 
826  case JBI_ARRAY_ELEM:
827  if ((*it)->curIndex >= (*it)->nElems)
828  {
829  /*
830  * All elements within array already processed. Report this
831  * to caller, and give it back original parent iterator (which
832  * independently tracks iteration progress at its level of
833  * nesting).
834  */
835  *it = freeAndGetParent(*it);
836  return WJB_END_ARRAY;
837  }
838 
839  fillJsonbValue((*it)->container, (*it)->curIndex,
840  (*it)->dataProper, (*it)->curDataOffset,
841  val);
842 
843  JBE_ADVANCE_OFFSET((*it)->curDataOffset,
844  (*it)->children[(*it)->curIndex]);
845  (*it)->curIndex++;
846 
847  if (!IsAJsonbScalar(val) && !skipNested)
848  {
849  /* Recurse into container. */
850  *it = iteratorFromContainer(val->val.binary.data, *it);
851  goto recurse;
852  }
853  else
854  {
855  /*
856  * Scalar item in array, or a container and caller didn't want
857  * us to recurse into it.
858  */
859  return WJB_ELEM;
860  }
861 
862  case JBI_OBJECT_START:
863  /* Set v to object on first object call */
864  val->type = jbvObject;
865  val->val.object.nPairs = (*it)->nElems;
866 
867  /*
868  * v->val.object.pairs is not actually set, because we aren't
869  * doing a full conversion
870  */
871  (*it)->curIndex = 0;
872  (*it)->curDataOffset = 0;
873  (*it)->curValueOffset = getJsonbOffset((*it)->container,
874  (*it)->nElems);
875  /* Set state for next call */
876  (*it)->state = JBI_OBJECT_KEY;
877  return WJB_BEGIN_OBJECT;
878 
879  case JBI_OBJECT_KEY:
880  if ((*it)->curIndex >= (*it)->nElems)
881  {
882  /*
883  * All pairs within object already processed. Report this to
884  * caller, and give it back original containing iterator
885  * (which independently tracks iteration progress at its level
886  * of nesting).
887  */
888  *it = freeAndGetParent(*it);
889  return WJB_END_OBJECT;
890  }
891  else
892  {
893  /* Return key of a key/value pair. */
894  fillJsonbValue((*it)->container, (*it)->curIndex,
895  (*it)->dataProper, (*it)->curDataOffset,
896  val);
897  if (val->type != jbvString)
898  elog(ERROR, "unexpected jsonb type as object key");
899 
900  /* Set state for next call */
901  (*it)->state = JBI_OBJECT_VALUE;
902  return WJB_KEY;
903  }
904 
905  case JBI_OBJECT_VALUE:
906  /* Set state for next call */
907  (*it)->state = JBI_OBJECT_KEY;
908 
909  fillJsonbValue((*it)->container, (*it)->curIndex + (*it)->nElems,
910  (*it)->dataProper, (*it)->curValueOffset,
911  val);
912 
913  JBE_ADVANCE_OFFSET((*it)->curDataOffset,
914  (*it)->children[(*it)->curIndex]);
915  JBE_ADVANCE_OFFSET((*it)->curValueOffset,
916  (*it)->children[(*it)->curIndex + (*it)->nElems]);
917  (*it)->curIndex++;
918 
919  /*
920  * Value may be a container, in which case we recurse with new,
921  * child iterator (unless the caller asked not to, by passing
922  * skipNested).
923  */
924  if (!IsAJsonbScalar(val) && !skipNested)
925  {
926  *it = iteratorFromContainer(val->val.binary.data, *it);
927  goto recurse;
928  }
929  else
930  return WJB_VALUE;
931  }
932 
933  elog(ERROR, "invalid iterator state");
934  return -1;
935 }
uint32 getJsonbOffset(const JsonbContainer *jc, int index)
Definition: jsonb_util.c:126
char * val
Definition: jsonb.h:272
Definition: jsonb.h:22
static JsonbIterator * freeAndGetParent(JsonbIterator *it)
Definition: jsonb_util.c:983
#define JBE_ADVANCE_OFFSET(offset, je)
Definition: jsonb.h:170
#define ERROR
Definition: elog.h:43
Definition: jsonb.h:23
static void fillJsonbValue(JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result)
Definition: jsonb_util.c:497
static JsonbIterator * iteratorFromContainer(JsonbContainer *container, JsonbIterator *parent)
Definition: jsonb_util.c:941
#define IsAJsonbScalar(jsonbval)
Definition: jsonb.h:305
enum jbvType type
Definition: jsonb.h:263
#define elog(elevel,...)
Definition: elog.h:228
Definition: jsonb.h:25

◆ JsonbToCString()

char* JsonbToCString ( StringInfo  out,
JsonbContainer in,
int  estimated_len 
)

Definition at line 460 of file jsonb.c.

References JsonbToCStringWorker().

Referenced by get_jsonb_path_all(), jsonb_out(), jsonb_send(), JsonbValueAsText(), and populate_scalar().

461 {
462  return JsonbToCStringWorker(out, in, estimated_len, false);
463 }
static char * JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent)
Definition: jsonb.c:478

◆ JsonbToCStringIndent()

char* JsonbToCStringIndent ( StringInfo  out,
JsonbContainer in,
int  estimated_len 
)

Definition at line 469 of file jsonb.c.

References JsonbToCStringWorker().

Referenced by jsonb_pretty().

470 {
471  return JsonbToCStringWorker(out, in, estimated_len, true);
472 }
static char * JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent)
Definition: jsonb.c:478

◆ JsonbTypeName()

const char* JsonbTypeName ( JsonbValue jb)

Definition at line 191 of file jsonb.c.

References elog, ERROR, jbvArray, jbvBinary, jbvBool, jbvDatetime, jbvNull, jbvNumeric, jbvObject, jbvString, JsonbContainerTypeName(), JsonbValue::type, and JsonbValue::val.

Referenced by executeItemOptUnwrapTarget(), and JsonbContainerTypeName().

192 {
193  switch (jbv->type)
194  {
195  case jbvBinary:
196  return JsonbContainerTypeName(jbv->val.binary.data);
197  case jbvObject:
198  return "object";
199  case jbvArray:
200  return "array";
201  case jbvNumeric:
202  return "number";
203  case jbvString:
204  return "string";
205  case jbvBool:
206  return "boolean";
207  case jbvNull:
208  return "null";
209  case jbvDatetime:
210  switch (jbv->val.datetime.typid)
211  {
212  case DATEOID:
213  return "date";
214  case TIMEOID:
215  return "time without time zone";
216  case TIMETZOID:
217  return "time with time zone";
218  case TIMESTAMPOID:
219  return "timestamp without time zone";
220  case TIMESTAMPTZOID:
221  return "timestamp with time zone";
222  default:
223  elog(ERROR, "unrecognized jsonb value datetime type: %d",
224  jbv->val.datetime.typid);
225  }
226  return "unknown";
227  default:
228  elog(ERROR, "unrecognized jsonb value type: %d", jbv->type);
229  return "unknown";
230  }
231 }
Definition: jsonb.h:239
Definition: jsonb.h:236
#define ERROR
Definition: elog.h:43
static const char * JsonbContainerTypeName(JsonbContainer *jbc)
Definition: jsonb.c:170
#define elog(elevel,...)
Definition: elog.h:228

◆ JsonbValueToJsonb()

Jsonb* JsonbValueToJsonb ( JsonbValue val)

Definition at line 84 of file jsonb_util.c.

References Assert, convertToJsonb(), IsAJsonbScalar, jbvArray, jbvBinary, jbvObject, palloc(), pushJsonbValue(), SET_VARSIZE, JsonbValue::type, JsonbValue::val, VARDATA, VARHDRSZ, WJB_BEGIN_ARRAY, WJB_ELEM, and WJB_END_ARRAY.

Referenced by each_worker_jsonb(), elements_worker_jsonb(), executeKeyValueMethod(), get_jsonb_path_all(), hstore_to_jsonb(), hstore_to_jsonb_loose(), jsonb_agg_finalfn(), jsonb_agg_transfn(), jsonb_array_element(), jsonb_build_array(), jsonb_build_array_noargs(), jsonb_build_object(), jsonb_build_object_noargs(), jsonb_concat(), jsonb_delete(), jsonb_delete_array(), jsonb_delete_idx(), jsonb_delete_path(), jsonb_from_cstring(), jsonb_insert(), jsonb_object(), jsonb_object_agg_finalfn(), jsonb_object_agg_transfn(), jsonb_object_field(), jsonb_object_two_arg(), jsonb_path_query_array_internal(), jsonb_path_query_first_internal(), jsonb_path_query_internal(), jsonb_set(), jsonb_strip_nulls(), plperl_to_jsonb(), plpython_to_jsonb(), populate_scalar(), to_jsonb(), and transform_jsonb_string_values().

85 {
86  Jsonb *out;
87 
88  if (IsAJsonbScalar(val))
89  {
90  /* Scalar value */
91  JsonbParseState *pstate = NULL;
92  JsonbValue *res;
93  JsonbValue scalarArray;
94 
95  scalarArray.type = jbvArray;
96  scalarArray.val.array.rawScalar = true;
97  scalarArray.val.array.nElems = 1;
98 
99  pushJsonbValue(&pstate, WJB_BEGIN_ARRAY, &scalarArray);
100  pushJsonbValue(&pstate, WJB_ELEM, val);
101  res = pushJsonbValue(&pstate, WJB_END_ARRAY, NULL);
102 
103  out = convertToJsonb(res);
104  }
105  else if (val->type == jbvObject || val->type == jbvArray)
106  {
107  out = convertToJsonb(val);
108  }
109  else
110  {
111  Assert(val->type == jbvBinary);
112  out = palloc(VARHDRSZ + val->val.binary.len);
113  SET_VARSIZE(out, VARHDRSZ + val->val.binary.len);
114  memcpy(VARDATA(out), val->val.binary.data, val->val.binary.len);
115  }
116 
117  return out;
118 }
#define VARDATA(PTR)
Definition: postgres.h:302
Definition: jsonb.h:220
#define VARHDRSZ
Definition: c.h:556
char * val
Definition: jsonb.h:272
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:558
#define Assert(condition)
Definition: c.h:733
static Jsonb * convertToJsonb(JsonbValue *val)
Definition: jsonb_util.c:1490
#define IsAJsonbScalar(jsonbval)
Definition: jsonb.h:305
enum jbvType type
Definition: jsonb.h:263
void * palloc(Size size)
Definition: mcxt.c:949
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
Definition: jsonb.h:25

◆ pushJsonbValue()

JsonbValue* pushJsonbValue ( JsonbParseState **  pstate,
JsonbIteratorToken  seq,
JsonbValue jbval 
)

Definition at line 558 of file jsonb_util.c.

References jbvBinary, JsonbIteratorInit(), JsonbIteratorNext(), pushJsonbValueScalar(), JsonbValue::type, JsonbValue::val, WJB_BEGIN_ARRAY, WJB_DONE, WJB_ELEM, and WJB_VALUE.

Referenced by addJsonbToParseState(), array_dim_to_jsonb(), array_to_jsonb_internal(), AV_to_JsonbValue(), composite_to_jsonb(), datum_to_jsonb(), executeKeyValueMethod(), hstore_to_jsonb(), hstore_to_jsonb_loose(), HV_to_JsonbValue(), IteratorConcat(), jsonb_agg_finalfn(), jsonb_agg_transfn(), jsonb_build_array(), jsonb_build_array_noargs(), jsonb_build_object(), jsonb_build_object_noargs(), jsonb_delete(), jsonb_delete_array(), jsonb_delete_idx(), jsonb_in_array_end(), jsonb_in_array_start(), jsonb_in_object_end(), jsonb_in_object_field_start(), jsonb_in_object_start(), jsonb_in_scalar(), jsonb_object(), jsonb_object_agg_finalfn(), jsonb_object_agg_transfn(), jsonb_object_two_arg(), jsonb_strip_nulls(), JsonbValueToJsonb(), PLyMapping_ToJsonbValue(), PLyObject_ToJsonbValue(), PLySequence_ToJsonbValue(), setPath(), setPathArray(), setPathObject(), SV_to_JsonbValue(), transform_jsonb_string_values(), and wrapItemsInArray().

560 {
561  JsonbIterator *it;
562  JsonbValue *res = NULL;
563  JsonbValue v;
564  JsonbIteratorToken tok;
565 
566  if (!jbval || (seq != WJB_ELEM && seq != WJB_VALUE) ||
567  jbval->type != jbvBinary)
568  {
569  /* drop through */
570  return pushJsonbValueScalar(pstate, seq, jbval);
571  }
572 
573  /* unpack the binary and add each piece to the pstate */
574  it = JsonbIteratorInit(jbval->val.binary.data);
575  while ((tok = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
576  res = pushJsonbValueScalar(pstate, tok,
577  tok < WJB_BEGIN_ARRAY ? &v : NULL);
578 
579  return res;
580 }
char * val
Definition: jsonb.h:272
Definition: jsonb.h:22
static JsonbValue * pushJsonbValueScalar(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *scalarVal)
Definition: jsonb_util.c:587
JsonbIteratorToken
Definition: jsonb.h:20
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:759
enum jbvType type
Definition: jsonb.h:263
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:795
Definition: jsonb.h:25