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 184 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().

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

◆ findJsonbValueFromContainer()

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

Definition at line 337 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().

339 {
340  JEntry *children = container->children;
341  int count = JsonContainerSize(container);
342 
343  Assert((flags & ~(JB_FARRAY | JB_FOBJECT)) == 0);
344 
345  /* Quick out without a palloc cycle if object/array is empty */
346  if (count <= 0)
347  return NULL;
348 
349  if ((flags & JB_FARRAY) && JsonContainerIsArray(container))
350  {
351  JsonbValue *result = palloc(sizeof(JsonbValue));
352  char *base_addr = (char *) (children + count);
353  uint32 offset = 0;
354  int i;
355 
356  for (i = 0; i < count; i++)
357  {
358  fillJsonbValue(container, i, base_addr, offset, result);
359 
360  if (key->type == result->type)
361  {
362  if (equalsJsonbScalarValue(key, result))
363  return result;
364  }
365 
366  JBE_ADVANCE_OFFSET(offset, children[i]);
367  }
368 
369  pfree(result);
370  }
371  else if ((flags & JB_FOBJECT) && JsonContainerIsObject(container))
372  {
373  /* Object key passed by caller must be a string */
374  Assert(key->type == jbvString);
375 
376  return getKeyJsonValueFromContainer(container, key->val.string.val,
377  key->val.string.len, NULL);
378  }
379 
380  /* Not found */
381  return NULL;
382 }
#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:391
#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:367
static void fillJsonbValue(JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result)
Definition: jsonb_util.c:498
#define JsonContainerIsArray(jc)
Definition: jsonb.h:217
static bool equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b)
Definition: jsonb_util.c:1344
#define JsonContainerIsObject(jc)
Definition: jsonb.h:216
#define Assert(condition)
Definition: c.h:738
#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 461 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().

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

◆ getJsonbLength()

uint32 getJsonbLength ( const JsonbContainer jc,
int  index 
)

Definition at line 152 of file jsonb_util.c.

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

Referenced by fillJsonbValue(), and getKeyJsonValueFromContainer().

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

◆ getJsonbOffset()

uint32 getJsonbOffset ( const JsonbContainer jc,
int  index 
)

Definition at line 127 of file jsonb_util.c.

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

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

128 {
129  uint32 offset = 0;
130  int i;
131 
132  /*
133  * Start offset of this entry is equal to the end offset of the previous
134  * entry. Walk backwards to the most recent entry stored as an end
135  * offset, returning that offset plus any lengths in between.
136  */
137  for (i = index - 1; i >= 0; i--)
138  {
139  offset += JBE_OFFLENFLD(jc->children[i]);
140  if (JBE_HAS_OFF(jc->children[i]))
141  break;
142  }
143 
144  return offset;
145 }
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:367
#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 391 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().

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

◆ JsonbDeepContains()

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

Definition at line 1005 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().

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

◆ JsonbHashScalarValue()

void JsonbHashScalarValue ( const JsonbValue scalarVal,
uint32 hash 
)

Definition at line 1259 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().

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

1304 {
1305  uint64 tmp;
1306 
1307  switch (scalarVal->type)
1308  {
1309  case jbvNull:
1310  tmp = seed + 0x01;
1311  break;
1312  case jbvString:
1313  tmp = DatumGetUInt64(hash_any_extended((const unsigned char *) scalarVal->val.string.val,
1314  scalarVal->val.string.len,
1315  seed));
1316  break;
1317  case jbvNumeric:
1319  NumericGetDatum(scalarVal->val.numeric),
1320  UInt64GetDatum(seed)));
1321  break;
1322  case jbvBool:
1323  if (seed)
1325  BoolGetDatum(scalarVal->val.boolean),
1326  UInt64GetDatum(seed)));
1327  else
1328  tmp = scalarVal->val.boolean ? 0x02 : 0x04;
1329 
1330  break;
1331  default:
1332  elog(ERROR, "invalid jsonb scalar type");
1333  break;
1334  }
1335 
1337  *hash ^= tmp;
1338 }
Datum hash_numeric_extended(PG_FUNCTION_ARGS)
Definition: numeric.c:2333
#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
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
#define ERROR
Definition: elog.h:43
#define ROTATE_HIGH_AND_LOW_32BITS(v)
Definition: hashfn.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:214
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ JsonbIteratorInit()

◆ JsonbIteratorNext()

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

Definition at line 796 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().

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

◆ JsonbValueToJsonb()

Jsonb* JsonbValueToJsonb ( JsonbValue val)

Definition at line 85 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().

86 {
87  Jsonb *out;
88 
89  if (IsAJsonbScalar(val))
90  {
91  /* Scalar value */
92  JsonbParseState *pstate = NULL;
93  JsonbValue *res;
94  JsonbValue scalarArray;
95 
96  scalarArray.type = jbvArray;
97  scalarArray.val.array.rawScalar = true;
98  scalarArray.val.array.nElems = 1;
99 
100  pushJsonbValue(&pstate, WJB_BEGIN_ARRAY, &scalarArray);
101  pushJsonbValue(&pstate, WJB_ELEM, val);
102  res = pushJsonbValue(&pstate, WJB_END_ARRAY, NULL);
103 
104  out = convertToJsonb(res);
105  }
106  else if (val->type == jbvObject || val->type == jbvArray)
107  {
108  out = convertToJsonb(val);
109  }
110  else
111  {
112  Assert(val->type == jbvBinary);
113  out = palloc(VARHDRSZ + val->val.binary.len);
114  SET_VARSIZE(out, VARHDRSZ + val->val.binary.len);
115  memcpy(VARDATA(out), val->val.binary.data, val->val.binary.len);
116  }
117 
118  return out;
119 }
#define VARDATA(PTR)
Definition: postgres.h:302
Definition: jsonb.h:220
#define VARHDRSZ
Definition: c.h:561
char * val
Definition: jsonb.h:272
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:559
#define Assert(condition)
Definition: c.h:738
static Jsonb * convertToJsonb(JsonbValue *val)
Definition: jsonb_util.c:1491
#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 559 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().

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