PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 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)
 
#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)
 

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

static JsonbDatumGetJsonbP (Datum d)
 
static JsonbDatumGetJsonbPCopy (Datum d)
 
static Datum JsonbPGetDatum (const Jsonb *p)
 
uint32 getJsonbOffset (const JsonbContainer *jc, int index)
 
uint32 getJsonbLength (const JsonbContainer *jc, int index)
 
int compareJsonbContainers (JsonbContainer *a, JsonbContainer *b)
 
JsonbValuefindJsonbValueFromContainer (JsonbContainer *container, uint32 flags, JsonbValue *key)
 
JsonbValuegetKeyJsonValueFromContainer (JsonbContainer *container, const char *keyVal, int keyLen, JsonbValue *res)
 
JsonbValuegetIthJsonbValueFromContainer (JsonbContainer *container, uint32 i)
 
JsonbValuepushJsonbValue (JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
 
JsonbIteratorJsonbIteratorInit (JsonbContainer *container)
 
JsonbIteratorToken JsonbIteratorNext (JsonbIterator **it, JsonbValue *val, bool skipNested)
 
void JsonbToJsonbValue (Jsonb *jsonb, JsonbValue *val)
 
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)
 
char * JsonbUnquote (Jsonb *jb)
 
bool JsonbExtractScalar (JsonbContainer *jbc, JsonbValue *res)
 
const char * JsonbTypeName (JsonbValue *val)
 
Datum jsonb_set_element (Jsonb *jb, Datum *path, int path_len, JsonbValue *newval)
 
Datum jsonb_get_element (Jsonb *jb, Datum *path, int npath, bool *isnull, bool as_text)
 
bool to_jsonb_is_immutable (Oid typoid)
 
Datum jsonb_build_object_worker (int nargs, const Datum *args, const bool *nulls, const Oid *types, bool absent_on_null, bool unique_keys)
 
Datum jsonb_build_array_worker (int nargs, const Datum *args, const bool *nulls, const Oid *types, bool absent_on_null)
 

Macro Definition Documentation

◆ IsAJsonbScalar

#define IsAJsonbScalar (   jsonbval)
Value:
(((jsonbval)->type >= jbvNull && \
(jsonbval)->type <= jbvBool) || \
(jsonbval)->type == jbvDatetime)
@ jbvBool
Definition: jsonb.h:231
@ jbvNull
Definition: jsonb.h:228
@ jbvDatetime
Definition: jsonb.h:244
const char * type

Definition at line 297 of file jsonb.h.

◆ JB_CMASK

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

Definition at line 200 of file jsonb.h.

◆ JB_FARRAY

#define JB_FARRAY   0x40000000

Definition at line 203 of file jsonb.h.

◆ JB_FOBJECT

#define JB_FOBJECT   0x20000000

Definition at line 202 of file jsonb.h.

◆ JB_FSCALAR

#define JB_FSCALAR   0x10000000 /* flag bits */

Definition at line 201 of file jsonb.h.

◆ JB_OFFSET_STRIDE

#define JB_OFFSET_STRIDE   32

Definition at line 178 of file jsonb.h.

◆ JB_ROOT_COUNT

#define JB_ROOT_COUNT (   jbp_)    (*(uint32 *) VARDATA(jbp_) & JB_CMASK)

Definition at line 219 of file jsonb.h.

◆ JB_ROOT_IS_ARRAY

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

Definition at line 222 of file jsonb.h.

◆ JB_ROOT_IS_OBJECT

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

Definition at line 221 of file jsonb.h.

◆ JB_ROOT_IS_SCALAR

#define JB_ROOT_IS_SCALAR (   jbp_)    ((*(uint32 *) VARDATA(jbp_) & JB_FSCALAR) != 0)

Definition at line 220 of file jsonb.h.

◆ 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_HAS_OFF(je_)
Definition: jsonb.h:152
#define JBE_OFFLENFLD(je_)
Definition: jsonb.h:151

Definition at line 162 of file jsonb.h.

◆ JBE_HAS_OFF

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

Definition at line 152 of file jsonb.h.

◆ JBE_ISBOOL

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

Definition at line 159 of file jsonb.h.

◆ JBE_ISBOOL_FALSE

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

Definition at line 158 of file jsonb.h.

◆ JBE_ISBOOL_TRUE

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

Definition at line 157 of file jsonb.h.

◆ JBE_ISCONTAINER

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

Definition at line 155 of file jsonb.h.

◆ JBE_ISNULL

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

Definition at line 156 of file jsonb.h.

◆ JBE_ISNUMERIC

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

Definition at line 154 of file jsonb.h.

◆ JBE_ISSTRING

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

Definition at line 153 of file jsonb.h.

◆ JBE_OFFLENFLD

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

Definition at line 151 of file jsonb.h.

◆ JENTRY_HAS_OFF

#define JENTRY_HAS_OFF   0x80000000

Definition at line 140 of file jsonb.h.

◆ JENTRY_ISBOOL_FALSE

#define JENTRY_ISBOOL_FALSE   0x20000000

Definition at line 145 of file jsonb.h.

◆ JENTRY_ISBOOL_TRUE

#define JENTRY_ISBOOL_TRUE   0x30000000

Definition at line 146 of file jsonb.h.

◆ JENTRY_ISCONTAINER

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

Definition at line 148 of file jsonb.h.

◆ JENTRY_ISNULL

#define JENTRY_ISNULL   0x40000000

Definition at line 147 of file jsonb.h.

◆ JENTRY_ISNUMERIC

#define JENTRY_ISNUMERIC   0x10000000

Definition at line 144 of file jsonb.h.

◆ JENTRY_ISSTRING

#define JENTRY_ISSTRING   0x00000000

Definition at line 143 of file jsonb.h.

◆ JENTRY_OFFLENMASK

#define JENTRY_OFFLENMASK   0x0FFFFFFF

Definition at line 138 of file jsonb.h.

◆ JENTRY_TYPEMASK

#define JENTRY_TYPEMASK   0x70000000

Definition at line 139 of file jsonb.h.

◆ JGIN_MAXLENGTH

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

Definition at line 68 of file jsonb.h.

◆ JGINFLAG_BOOL

#define JGINFLAG_BOOL   0x03 /* boolean value */

Definition at line 64 of file jsonb.h.

◆ JGINFLAG_HASHED

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

Definition at line 67 of file jsonb.h.

◆ JGINFLAG_KEY

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

Definition at line 62 of file jsonb.h.

◆ JGINFLAG_NULL

#define JGINFLAG_NULL   0x02 /* null value */

Definition at line 63 of file jsonb.h.

◆ JGINFLAG_NUM

#define JGINFLAG_NUM   0x04 /* numeric value */

Definition at line 65 of file jsonb.h.

◆ JGINFLAG_STR

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

Definition at line 66 of file jsonb.h.

◆ JsonbContainsStrategyNumber

#define JsonbContainsStrategyNumber   7

Definition at line 33 of file jsonb.h.

◆ JsonbExistsAllStrategyNumber

#define JsonbExistsAllStrategyNumber   11

Definition at line 36 of file jsonb.h.

◆ JsonbExistsAnyStrategyNumber

#define JsonbExistsAnyStrategyNumber   10

Definition at line 35 of file jsonb.h.

◆ JsonbExistsStrategyNumber

#define JsonbExistsStrategyNumber   9

Definition at line 34 of file jsonb.h.

◆ JsonbJsonpathExistsStrategyNumber

#define JsonbJsonpathExistsStrategyNumber   15

Definition at line 37 of file jsonb.h.

◆ JsonbJsonpathPredicateStrategyNumber

#define JsonbJsonpathPredicateStrategyNumber   16

Definition at line 38 of file jsonb.h.

◆ JsonContainerIsArray

#define JsonContainerIsArray (   jc)    (((jc)->header & JB_FARRAY) != 0)

Definition at line 209 of file jsonb.h.

◆ JsonContainerIsObject

#define JsonContainerIsObject (   jc)    (((jc)->header & JB_FOBJECT) != 0)

Definition at line 208 of file jsonb.h.

◆ JsonContainerIsScalar

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

Definition at line 207 of file jsonb.h.

◆ JsonContainerSize

#define JsonContainerSize (   jc)    ((jc)->header & JB_CMASK)

Definition at line 206 of file jsonb.h.

◆ PG_GETARG_JSONB_P

#define PG_GETARG_JSONB_P (   x)    DatumGetJsonbP(PG_GETARG_DATUM(x))

Definition at line 391 of file jsonb.h.

◆ PG_GETARG_JSONB_P_COPY

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

Definition at line 392 of file jsonb.h.

◆ PG_RETURN_JSONB_P

#define PG_RETURN_JSONB_P (   x)    PG_RETURN_POINTER(x)

Definition at line 393 of file jsonb.h.

Typedef Documentation

◆ JEntry

typedef uint32 JEntry

Definition at line 136 of file jsonb.h.

◆ JsonbContainer

◆ JsonbIterator

typedef struct JsonbIterator JsonbIterator

◆ JsonbPair

typedef struct JsonbPair JsonbPair

Definition at line 1 of file jsonb.h.

◆ JsonbParseState

◆ JsonbValue

typedef struct JsonbValue JsonbValue

Definition at line 1 of file jsonb.h.

Enumeration Type Documentation

◆ jbvType

enum jbvType
Enumerator
jbvNull 
jbvString 
jbvNumeric 
jbvBool 
jbvArray 
jbvObject 
jbvBinary 
jbvDatetime 

Definition at line 225 of file jsonb.h.

226 {
227  /* Scalar types */
228  jbvNull = 0x0,
229  jbvString,
230  jbvNumeric,
231  jbvBool,
232  /* Composite types */
233  jbvArray = 0x10,
234  jbvObject,
235  /* Binary (i.e. struct Jsonb) jbvArray/jbvObject */
236  jbvBinary,
237 
238  /*
239  * Virtual types.
240  *
241  * These types are used only for in-memory JSON processing and serialized
242  * into JSON strings when outputted to json/jsonb.
243  */
244  jbvDatetime = 0x20,
245 };
@ jbvObject
Definition: jsonb.h:234
@ jbvNumeric
Definition: jsonb.h:230
@ jbvArray
Definition: jsonb.h:233
@ jbvBinary
Definition: jsonb.h:236
@ jbvString
Definition: jsonb.h:229

◆ 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,
JsonbIteratorToken
Definition: jsonb.h:21
@ WJB_KEY
Definition: jsonb.h:23
@ WJB_DONE
Definition: jsonb.h:22
@ WJB_END_ARRAY
Definition: jsonb.h:27
@ WJB_VALUE
Definition: jsonb.h:24
@ WJB_END_OBJECT
Definition: jsonb.h:29
@ WJB_ELEM
Definition: jsonb.h:25
@ WJB_BEGIN_OBJECT
Definition: jsonb.h:28
@ WJB_BEGIN_ARRAY
Definition: jsonb.h:26

◆ JsonbIterState

Enumerator
JBI_ARRAY_START 
JBI_ARRAY_ELEM 
JBI_OBJECT_START 
JBI_OBJECT_KEY 
JBI_OBJECT_VALUE 

Definition at line 332 of file jsonb.h.

333 {
JsonbIterState
Definition: jsonb.h:333
@ JBI_OBJECT_VALUE
Definition: jsonb.h:338
@ JBI_ARRAY_START
Definition: jsonb.h:334
@ JBI_ARRAY_ELEM
Definition: jsonb.h:335
@ JBI_OBJECT_START
Definition: jsonb.h:336
@ JBI_OBJECT_KEY
Definition: jsonb.h:337

Function Documentation

◆ compareJsonbContainers()

int compareJsonbContainers ( JsonbContainer a,
JsonbContainer b 
)

Definition at line 191 of file jsonb_util.c.

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

References a, Assert, b, compareJsonbScalarValue(), elog, ERROR, i, jbvArray, jbvBinary, jbvBool, jbvDatetime, jbvNull, jbvNumeric, jbvObject, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), JsonbIterator::parent, pfree(), res, 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().

◆ DatumGetJsonbP()

static Jsonb* DatumGetJsonbP ( Datum  d)
inlinestatic

◆ DatumGetJsonbPCopy()

static Jsonb* DatumGetJsonbPCopy ( Datum  d)
inlinestatic

Definition at line 380 of file jsonb.h.

381 {
382  return (Jsonb *) PG_DETOAST_DATUM_COPY(d);
383 }
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:242

References PG_DETOAST_DATUM_COPY.

◆ findJsonbValueFromContainer()

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

Definition at line 344 of file jsonb_util.c.

346 {
347  JEntry *children = container->children;
348  int count = JsonContainerSize(container);
349 
350  Assert((flags & ~(JB_FARRAY | JB_FOBJECT)) == 0);
351 
352  /* Quick out without a palloc cycle if object/array is empty */
353  if (count <= 0)
354  return NULL;
355 
356  if ((flags & JB_FARRAY) && JsonContainerIsArray(container))
357  {
358  JsonbValue *result = palloc(sizeof(JsonbValue));
359  char *base_addr = (char *) (children + count);
360  uint32 offset = 0;
361  int i;
362 
363  for (i = 0; i < count; i++)
364  {
365  fillJsonbValue(container, i, base_addr, offset, result);
366 
367  if (key->type == result->type)
368  {
369  if (equalsJsonbScalarValue(key, result))
370  return result;
371  }
372 
373  JBE_ADVANCE_OFFSET(offset, children[i]);
374  }
375 
376  pfree(result);
377  }
378  else if ((flags & JB_FOBJECT) && JsonContainerIsObject(container))
379  {
380  /* Object key passed by caller must be a string */
381  Assert(key->type == jbvString);
382 
383  return getKeyJsonValueFromContainer(container, key->val.string.val,
384  key->val.string.len, NULL);
385  }
386 
387  /* Not found */
388  return NULL;
389 }
unsigned int uint32
Definition: c.h:518
#define JsonContainerIsArray(jc)
Definition: jsonb.h:209
#define JsonContainerSize(jc)
Definition: jsonb.h:206
#define JsonContainerIsObject(jc)
Definition: jsonb.h:208
#define JB_FARRAY
Definition: jsonb.h:203
uint32 JEntry
Definition: jsonb.h:136
#define JB_FOBJECT
Definition: jsonb.h:202
#define JBE_ADVANCE_OFFSET(offset, je)
Definition: jsonb.h:162
static void fillJsonbValue(JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result)
Definition: jsonb_util.c:505
JsonbValue * getKeyJsonValueFromContainer(JsonbContainer *container, const char *keyVal, int keyLen, JsonbValue *res)
Definition: jsonb_util.c:398
static bool equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b)
Definition: jsonb_util.c:1401
void * palloc(Size size)
Definition: mcxt.c:1317
JEntry children[FLEXIBLE_ARRAY_MEMBER]
Definition: jsonb.h:194

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

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

◆ getIthJsonbValueFromContainer()

JsonbValue* getIthJsonbValueFromContainer ( JsonbContainer container,
uint32  i 
)

Definition at line 468 of file jsonb_util.c.

469 {
470  JsonbValue *result;
471  char *base_addr;
472  uint32 nelements;
473 
474  if (!JsonContainerIsArray(container))
475  elog(ERROR, "not a jsonb array");
476 
477  nelements = JsonContainerSize(container);
478  base_addr = (char *) &container->children[nelements];
479 
480  if (i >= nelements)
481  return NULL;
482 
483  result = palloc(sizeof(JsonbValue));
484 
485  fillJsonbValue(container, i, base_addr,
486  getJsonbOffset(container, i),
487  result);
488 
489  return result;
490 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
uint32 getJsonbOffset(const JsonbContainer *jc, int index)
Definition: jsonb_util.c:134

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

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

◆ getJsonbLength()

uint32 getJsonbLength ( const JsonbContainer jc,
int  index 
)

Definition at line 159 of file jsonb_util.c.

160 {
161  uint32 off;
162  uint32 len;
163 
164  /*
165  * If the length is stored directly in the JEntry, just return it.
166  * Otherwise, get the begin offset of the entry, and subtract that from
167  * the stored end+1 offset.
168  */
169  if (JBE_HAS_OFF(jc->children[index]))
170  {
171  off = getJsonbOffset(jc, index);
172  len = JBE_OFFLENFLD(jc->children[index]) - off;
173  }
174  else
176 
177  return len;
178 }
const void size_t len
Definition: type.h:95

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

Referenced by fillJsonbValue(), and getKeyJsonValueFromContainer().

◆ getJsonbOffset()

uint32 getJsonbOffset ( const JsonbContainer jc,
int  index 
)

Definition at line 134 of file jsonb_util.c.

135 {
136  uint32 offset = 0;
137  int i;
138 
139  /*
140  * Start offset of this entry is equal to the end offset of the previous
141  * entry. Walk backwards to the most recent entry stored as an end
142  * offset, returning that offset plus any lengths in between.
143  */
144  for (i = index - 1; i >= 0; i--)
145  {
146  offset += JBE_OFFLENFLD(jc->children[i]);
147  if (JBE_HAS_OFF(jc->children[i]))
148  break;
149  }
150 
151  return offset;
152 }

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

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

◆ getKeyJsonValueFromContainer()

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

Definition at line 398 of file jsonb_util.c.

400 {
401  JEntry *children = container->children;
402  int count = JsonContainerSize(container);
403  char *baseAddr;
404  uint32 stopLow,
405  stopHigh;
406 
407  Assert(JsonContainerIsObject(container));
408 
409  /* Quick out without a palloc cycle if object is empty */
410  if (count <= 0)
411  return NULL;
412 
413  /*
414  * Binary search the container. Since we know this is an object, account
415  * for *Pairs* of Jentrys
416  */
417  baseAddr = (char *) (children + count * 2);
418  stopLow = 0;
419  stopHigh = count;
420  while (stopLow < stopHigh)
421  {
422  uint32 stopMiddle;
423  int difference;
424  const char *candidateVal;
425  int candidateLen;
426 
427  stopMiddle = stopLow + (stopHigh - stopLow) / 2;
428 
429  candidateVal = baseAddr + getJsonbOffset(container, stopMiddle);
430  candidateLen = getJsonbLength(container, stopMiddle);
431 
432  difference = lengthCompareJsonbString(candidateVal, candidateLen,
433  keyVal, keyLen);
434 
435  if (difference == 0)
436  {
437  /* Found our key, return corresponding value */
438  int index = stopMiddle + count;
439 
440  if (!res)
441  res = palloc(sizeof(JsonbValue));
442 
443  fillJsonbValue(container, index, baseAddr,
444  getJsonbOffset(container, index),
445  res);
446 
447  return res;
448  }
449  else
450  {
451  if (difference < 0)
452  stopLow = stopMiddle + 1;
453  else
454  stopHigh = stopMiddle;
455  }
456  }
457 
458  /* Not found */
459  return NULL;
460 }
Datum difference(PG_FUNCTION_ARGS)
static int lengthCompareJsonbString(const char *val1, int len1, const char *val2, int len2)
Definition: jsonb_util.c:1899
uint32 getJsonbLength(const JsonbContainer *jc, int index)
Definition: jsonb_util.c:159

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

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

◆ jsonb_build_array_worker()

Datum jsonb_build_array_worker ( int  nargs,
const Datum args,
const bool nulls,
const Oid types,
bool  absent_on_null 
)

Definition at line 1210 of file jsonb.c.

1212 {
1213  int i;
1214  JsonbInState result;
1215 
1216  memset(&result, 0, sizeof(JsonbInState));
1217 
1218  result.res = pushJsonbValue(&result.parseState, WJB_BEGIN_ARRAY, NULL);
1219 
1220  for (i = 0; i < nargs; i++)
1221  {
1222  if (absent_on_null && nulls[i])
1223  continue;
1224 
1225  add_jsonb(args[i], nulls[i], &result, types[i], false);
1226  }
1227 
1228  result.res = pushJsonbValue(&result.parseState, WJB_END_ARRAY, NULL);
1229 
1230  return JsonbPGetDatum(JsonbValueToJsonb(result.res));
1231 }
struct typedefs * types
Definition: ecpg.c:29
static void add_jsonb(Datum val, bool is_null, JsonbInState *result, Oid val_type, bool key_scalar)
Definition: jsonb.c:1016
static Datum JsonbPGetDatum(const Jsonb *p)
Definition: jsonb.h:386
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:92
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:566
JsonbParseState * parseState
Definition: jsonb.c:30
JsonbValue * res
Definition: jsonb.c:31

References add_jsonb(), generate_unaccent_rules::args, i, JsonbPGetDatum(), JsonbValueToJsonb(), JsonbInState::parseState, pushJsonbValue(), JsonbInState::res, types, WJB_BEGIN_ARRAY, and WJB_END_ARRAY.

Referenced by ExecEvalJsonConstructor(), and jsonb_build_array().

◆ jsonb_build_object_worker()

Datum jsonb_build_object_worker ( int  nargs,
const Datum args,
const bool nulls,
const Oid types,
bool  absent_on_null,
bool  unique_keys 
)

Definition at line 1125 of file jsonb.c.

1127 {
1128  int i;
1129  JsonbInState result;
1130 
1131  if (nargs % 2 != 0)
1132  ereport(ERROR,
1133  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1134  errmsg("argument list must have even number of elements"),
1135  /* translator: %s is a SQL function name */
1136  errhint("The arguments of %s must consist of alternating keys and values.",
1137  "jsonb_build_object()")));
1138 
1139  memset(&result, 0, sizeof(JsonbInState));
1140 
1141  result.res = pushJsonbValue(&result.parseState, WJB_BEGIN_OBJECT, NULL);
1142  result.parseState->unique_keys = unique_keys;
1143  result.parseState->skip_nulls = absent_on_null;
1144 
1145  for (i = 0; i < nargs; i += 2)
1146  {
1147  /* process key */
1148  bool skip;
1149 
1150  if (nulls[i])
1151  ereport(ERROR,
1152  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1153  errmsg("argument %d: key must not be null", i + 1)));
1154 
1155  /* skip null values if absent_on_null */
1156  skip = absent_on_null && nulls[i + 1];
1157 
1158  /* we need to save skipped keys for the key uniqueness check */
1159  if (skip && !unique_keys)
1160  continue;
1161 
1162  add_jsonb(args[i], false, &result, types[i], true);
1163 
1164  /* process value */
1165  add_jsonb(args[i + 1], nulls[i + 1], &result, types[i + 1], false);
1166  }
1167 
1168  result.res = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
1169 
1170  return JsonbPGetDatum(JsonbValueToJsonb(result.res));
1171 }
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
static const struct exclude_list_item skip[]
Definition: pg_checksums.c:107
bool unique_keys
Definition: jsonb.h:324
bool skip_nulls
Definition: jsonb.h:325

References add_jsonb(), generate_unaccent_rules::args, ereport, errcode(), errhint(), errmsg(), ERROR, i, JsonbPGetDatum(), JsonbValueToJsonb(), JsonbInState::parseState, pushJsonbValue(), JsonbInState::res, skip, JsonbParseState::skip_nulls, types, JsonbParseState::unique_keys, WJB_BEGIN_OBJECT, and WJB_END_OBJECT.

Referenced by ExecEvalJsonConstructor(), and jsonb_build_object().

◆ jsonb_get_element()

Datum jsonb_get_element ( Jsonb jb,
Datum path,
int  npath,
bool isnull,
bool  as_text 
)

Definition at line 1530 of file jsonfuncs.c.

1531 {
1532  JsonbContainer *container = &jb->root;
1533  JsonbValue *jbvp = NULL;
1534  int i;
1535  bool have_object = false,
1536  have_array = false;
1537 
1538  *isnull = false;
1539 
1540  /* Identify whether we have object, array, or scalar at top-level */
1541  if (JB_ROOT_IS_OBJECT(jb))
1542  have_object = true;
1543  else if (JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb))
1544  have_array = true;
1545  else
1546  {
1548  /* Extract the scalar value, if it is what we'll return */
1549  if (npath <= 0)
1550  jbvp = getIthJsonbValueFromContainer(container, 0);
1551  }
1552 
1553  /*
1554  * If the array is empty, return the entire LHS object, on the grounds
1555  * that we should do zero field or element extractions. For the
1556  * non-scalar case we can just hand back the object without much work. For
1557  * the scalar case, fall through and deal with the value below the loop.
1558  * (This inconsistency arises because there's no easy way to generate a
1559  * JsonbValue directly for root-level containers.)
1560  */
1561  if (npath <= 0 && jbvp == NULL)
1562  {
1563  if (as_text)
1564  {
1566  container,
1567  VARSIZE(jb))));
1568  }
1569  else
1570  {
1571  /* not text mode - just hand back the jsonb */
1572  PG_RETURN_JSONB_P(jb);
1573  }
1574  }
1575 
1576  for (i = 0; i < npath; i++)
1577  {
1578  if (have_object)
1579  {
1580  text *subscr = DatumGetTextPP(path[i]);
1581 
1582  jbvp = getKeyJsonValueFromContainer(container,
1583  VARDATA_ANY(subscr),
1584  VARSIZE_ANY_EXHDR(subscr),
1585  NULL);
1586  }
1587  else if (have_array)
1588  {
1589  int lindex;
1590  uint32 index;
1591  char *indextext = TextDatumGetCString(path[i]);
1592  char *endptr;
1593 
1594  errno = 0;
1595  lindex = strtoint(indextext, &endptr, 10);
1596  if (endptr == indextext || *endptr != '\0' || errno != 0)
1597  {
1598  *isnull = true;
1599  return PointerGetDatum(NULL);
1600  }
1601 
1602  if (lindex >= 0)
1603  {
1604  index = (uint32) lindex;
1605  }
1606  else
1607  {
1608  /* Handle negative subscript */
1609  uint32 nelements;
1610 
1611  /* Container must be array, but make sure */
1612  if (!JsonContainerIsArray(container))
1613  elog(ERROR, "not a jsonb array");
1614 
1615  nelements = JsonContainerSize(container);
1616 
1617  if (lindex == INT_MIN || -lindex > nelements)
1618  {
1619  *isnull = true;
1620  return PointerGetDatum(NULL);
1621  }
1622  else
1623  index = nelements + lindex;
1624  }
1625 
1626  jbvp = getIthJsonbValueFromContainer(container, index);
1627  }
1628  else
1629  {
1630  /* scalar, extraction yields a null */
1631  *isnull = true;
1632  return PointerGetDatum(NULL);
1633  }
1634 
1635  if (jbvp == NULL)
1636  {
1637  *isnull = true;
1638  return PointerGetDatum(NULL);
1639  }
1640  else if (i == npath - 1)
1641  break;
1642 
1643  if (jbvp->type == jbvBinary)
1644  {
1645  container = jbvp->val.binary.data;
1646  have_object = JsonContainerIsObject(container);
1647  have_array = JsonContainerIsArray(container);
1648  Assert(!JsonContainerIsScalar(container));
1649  }
1650  else
1651  {
1652  Assert(IsAJsonbScalar(jbvp));
1653  have_object = false;
1654  have_array = false;
1655  }
1656  }
1657 
1658  if (as_text)
1659  {
1660  if (jbvp->type == jbvNull)
1661  {
1662  *isnull = true;
1663  return PointerGetDatum(NULL);
1664  }
1665 
1666  return PointerGetDatum(JsonbValueAsText(jbvp));
1667  }
1668  else
1669  {
1670  Jsonb *res = JsonbValueToJsonb(jbvp);
1671 
1672  /* not text mode - just hand back the jsonb */
1674  }
1675 }
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define DatumGetTextPP(X)
Definition: fmgr.h:292
char * JsonbToCString(StringInfo out, JsonbContainer *in, int estimated_len)
Definition: jsonb.c:473
#define JsonContainerIsScalar(jc)
Definition: jsonb.h:207
#define JB_ROOT_IS_OBJECT(jbp_)
Definition: jsonb.h:221
#define IsAJsonbScalar(jsonbval)
Definition: jsonb.h:297
#define PG_RETURN_JSONB_P(x)
Definition: jsonb.h:393
#define JB_ROOT_IS_ARRAY(jbp_)
Definition: jsonb.h:222
#define JB_ROOT_IS_SCALAR(jbp_)
Definition: jsonb.h:220
JsonbValue * getIthJsonbValueFromContainer(JsonbContainer *container, uint32 i)
Definition: jsonb_util.c:468
static text * JsonbValueAsText(JsonbValue *v)
Definition: jsonfuncs.c:1804
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
JsonbContainer root
Definition: jsonb.h:215
Definition: c.h:690
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE(PTR)
Definition: varatt.h:279
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
text * cstring_to_text(const char *s)
Definition: varlena.c:184

References Assert, cstring_to_text(), DatumGetTextPP, elog, ERROR, getIthJsonbValueFromContainer(), getKeyJsonValueFromContainer(), i, IsAJsonbScalar, JB_ROOT_IS_ARRAY, JB_ROOT_IS_OBJECT, JB_ROOT_IS_SCALAR, jbvBinary, jbvNull, JsonbToCString(), JsonbValueAsText(), JsonbValueToJsonb(), JsonContainerIsArray, JsonContainerIsObject, JsonContainerIsScalar, JsonContainerSize, PG_RETURN_JSONB_P, PointerGetDatum(), res, Jsonb::root, strtoint(), TextDatumGetCString, JsonbValue::type, JsonbValue::val, VARDATA_ANY, VARSIZE, and VARSIZE_ANY_EXHDR.

Referenced by get_jsonb_path_all(), jsonb_subscript_fetch(), and jsonb_subscript_fetch_old().

◆ jsonb_set_element()

Datum jsonb_set_element ( Jsonb jb,
Datum path,
int  path_len,
JsonbValue newval 
)

Definition at line 1678 of file jsonfuncs.c.

1680 {
1681  JsonbValue *res;
1682  JsonbParseState *state = NULL;
1683  JsonbIterator *it;
1684  bool *path_nulls = palloc0(path_len * sizeof(bool));
1685 
1686  if (newval->type == jbvArray && newval->val.array.rawScalar)
1687  *newval = newval->val.array.elems[0];
1688 
1689  it = JsonbIteratorInit(&jb->root);
1690 
1691  res = setPath(&it, path, path_nulls, path_len, &state, 0, newval,
1694 
1695  pfree(path_nulls);
1696 
1698 }
#define newval
static JsonbValue * setPath(JsonbIterator **it, Datum *path_elems, bool *path_nulls, int path_len, JsonbParseState **st, int level, JsonbValue *newval, int op_type)
Definition: jsonfuncs.c:5181
#define JB_PATH_CREATE
Definition: jsonfuncs.c:44
#define JB_PATH_CONSISTENT_POSITION
Definition: jsonfuncs.c:52
#define JB_PATH_FILL_GAPS
Definition: jsonfuncs.c:51
void * palloc0(Size size)
Definition: mcxt.c:1347
Definition: regguts.h:323

References JB_PATH_CONSISTENT_POSITION, JB_PATH_CREATE, JB_PATH_FILL_GAPS, jbvArray, JsonbIteratorInit(), JsonbValueToJsonb(), newval, palloc0(), pfree(), PG_RETURN_JSONB_P, res, Jsonb::root, and setPath().

Referenced by jsonb_subscript_assign().

◆ JsonbDeepContains()

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

Definition at line 1062 of file jsonb_util.c.

1063 {
1064  JsonbValue vval,
1065  vcontained;
1066  JsonbIteratorToken rval,
1067  rcont;
1068 
1069  /*
1070  * Guard against stack overflow due to overly complex Jsonb.
1071  *
1072  * Functions called here independently take this precaution, but that
1073  * might not be sufficient since this is also a recursive function.
1074  */
1076 
1077  rval = JsonbIteratorNext(val, &vval, false);
1078  rcont = JsonbIteratorNext(mContained, &vcontained, false);
1079 
1080  if (rval != rcont)
1081  {
1082  /*
1083  * The differing return values can immediately be taken as indicating
1084  * two differing container types at this nesting level, which is
1085  * sufficient reason to give up entirely (but it should be the case
1086  * that they're both some container type).
1087  */
1088  Assert(rval == WJB_BEGIN_OBJECT || rval == WJB_BEGIN_ARRAY);
1089  Assert(rcont == WJB_BEGIN_OBJECT || rcont == WJB_BEGIN_ARRAY);
1090  return false;
1091  }
1092  else if (rcont == WJB_BEGIN_OBJECT)
1093  {
1094  Assert(vval.type == jbvObject);
1095  Assert(vcontained.type == jbvObject);
1096 
1097  /*
1098  * If the lhs has fewer pairs than the rhs, it can't possibly contain
1099  * the rhs. (This conclusion is safe only because we de-duplicate
1100  * keys in all Jsonb objects; thus there can be no corresponding
1101  * optimization in the array case.) The case probably won't arise
1102  * often, but since it's such a cheap check we may as well make it.
1103  */
1104  if (vval.val.object.nPairs < vcontained.val.object.nPairs)
1105  return false;
1106 
1107  /* Work through rhs "is it contained within?" object */
1108  for (;;)
1109  {
1110  JsonbValue *lhsVal; /* lhsVal is from pair in lhs object */
1111  JsonbValue lhsValBuf;
1112 
1113  rcont = JsonbIteratorNext(mContained, &vcontained, false);
1114 
1115  /*
1116  * When we get through caller's rhs "is it contained within?"
1117  * object without failing to find one of its values, it's
1118  * contained.
1119  */
1120  if (rcont == WJB_END_OBJECT)
1121  return true;
1122 
1123  Assert(rcont == WJB_KEY);
1124  Assert(vcontained.type == jbvString);
1125 
1126  /* First, find value by key... */
1127  lhsVal =
1128  getKeyJsonValueFromContainer((*val)->container,
1129  vcontained.val.string.val,
1130  vcontained.val.string.len,
1131  &lhsValBuf);
1132  if (!lhsVal)
1133  return false;
1134 
1135  /*
1136  * ...at this stage it is apparent that there is at least a key
1137  * match for this rhs pair.
1138  */
1139  rcont = JsonbIteratorNext(mContained, &vcontained, true);
1140 
1141  Assert(rcont == WJB_VALUE);
1142 
1143  /*
1144  * Compare rhs pair's value with lhs pair's value just found using
1145  * key
1146  */
1147  if (lhsVal->type != vcontained.type)
1148  {
1149  return false;
1150  }
1151  else if (IsAJsonbScalar(lhsVal))
1152  {
1153  if (!equalsJsonbScalarValue(lhsVal, &vcontained))
1154  return false;
1155  }
1156  else
1157  {
1158  /* Nested container value (object or array) */
1159  JsonbIterator *nestval,
1160  *nestContained;
1161 
1162  Assert(lhsVal->type == jbvBinary);
1163  Assert(vcontained.type == jbvBinary);
1164 
1165  nestval = JsonbIteratorInit(lhsVal->val.binary.data);
1166  nestContained = JsonbIteratorInit(vcontained.val.binary.data);
1167 
1168  /*
1169  * Match "value" side of rhs datum object's pair recursively.
1170  * It's a nested structure.
1171  *
1172  * Note that nesting still has to "match up" at the right
1173  * nesting sub-levels. However, there need only be zero or
1174  * more matching pairs (or elements) at each nesting level
1175  * (provided the *rhs* pairs/elements *all* match on each
1176  * level), which enables searching nested structures for a
1177  * single String or other primitive type sub-datum quite
1178  * effectively (provided the user constructed the rhs nested
1179  * structure such that we "know where to look").
1180  *
1181  * In other words, the mapping of container nodes in the rhs
1182  * "vcontained" Jsonb to internal nodes on the lhs is
1183  * injective, and parent-child edges on the rhs must be mapped
1184  * to parent-child edges on the lhs to satisfy the condition
1185  * of containment (plus of course the mapped nodes must be
1186  * equal).
1187  */
1188  if (!JsonbDeepContains(&nestval, &nestContained))
1189  return false;
1190  }
1191  }
1192  }
1193  else if (rcont == WJB_BEGIN_ARRAY)
1194  {
1195  JsonbValue *lhsConts = NULL;
1196  uint32 nLhsElems = vval.val.array.nElems;
1197 
1198  Assert(vval.type == jbvArray);
1199  Assert(vcontained.type == jbvArray);
1200 
1201  /*
1202  * Handle distinction between "raw scalar" pseudo arrays, and real
1203  * arrays.
1204  *
1205  * A raw scalar may contain another raw scalar, and an array may
1206  * contain a raw scalar, but a raw scalar may not contain an array. We
1207  * don't do something like this for the object case, since objects can
1208  * only contain pairs, never raw scalars (a pair is represented by an
1209  * rhs object argument with a single contained pair).
1210  */
1211  if (vval.val.array.rawScalar && !vcontained.val.array.rawScalar)
1212  return false;
1213 
1214  /* Work through rhs "is it contained within?" array */
1215  for (;;)
1216  {
1217  rcont = JsonbIteratorNext(mContained, &vcontained, true);
1218 
1219  /*
1220  * When we get through caller's rhs "is it contained within?"
1221  * array without failing to find one of its values, it's
1222  * contained.
1223  */
1224  if (rcont == WJB_END_ARRAY)
1225  return true;
1226 
1227  Assert(rcont == WJB_ELEM);
1228 
1229  if (IsAJsonbScalar(&vcontained))
1230  {
1231  if (!findJsonbValueFromContainer((*val)->container,
1232  JB_FARRAY,
1233  &vcontained))
1234  return false;
1235  }
1236  else
1237  {
1238  uint32 i;
1239 
1240  /*
1241  * If this is first container found in rhs array (at this
1242  * depth), initialize temp lhs array of containers
1243  */
1244  if (lhsConts == NULL)
1245  {
1246  uint32 j = 0;
1247 
1248  /* Make room for all possible values */
1249  lhsConts = palloc(sizeof(JsonbValue) * nLhsElems);
1250 
1251  for (i = 0; i < nLhsElems; i++)
1252  {
1253  /* Store all lhs elements in temp array */
1254  rcont = JsonbIteratorNext(val, &vval, true);
1255  Assert(rcont == WJB_ELEM);
1256 
1257  if (vval.type == jbvBinary)
1258  lhsConts[j++] = vval;
1259  }
1260 
1261  /* No container elements in temp array, so give up now */
1262  if (j == 0)
1263  return false;
1264 
1265  /* We may have only partially filled array */
1266  nLhsElems = j;
1267  }
1268 
1269  /* XXX: Nested array containment is O(N^2) */
1270  for (i = 0; i < nLhsElems; i++)
1271  {
1272  /* Nested container value (object or array) */
1273  JsonbIterator *nestval,
1274  *nestContained;
1275  bool contains;
1276 
1277  nestval = JsonbIteratorInit(lhsConts[i].val.binary.data);
1278  nestContained = JsonbIteratorInit(vcontained.val.binary.data);
1279 
1280  contains = JsonbDeepContains(&nestval, &nestContained);
1281 
1282  if (nestval)
1283  pfree(nestval);
1284  if (nestContained)
1285  pfree(nestContained);
1286  if (contains)
1287  break;
1288  }
1289 
1290  /*
1291  * Report rhs container value is not contained if couldn't
1292  * match rhs container to *some* lhs cont
1293  */
1294  if (i == nLhsElems)
1295  return false;
1296  }
1297  }
1298  }
1299  else
1300  {
1301  elog(ERROR, "invalid jsonb container type");
1302  }
1303 
1304  elog(ERROR, "unexpectedly fell off end of jsonb container");
1305  return false;
1306 }
long val
Definition: informix.c:689
int j
Definition: isn.c:73
JsonbValue * findJsonbValueFromContainer(JsonbContainer *container, uint32 flags, JsonbValue *key)
Definition: jsonb_util.c:344
bool JsonbDeepContains(JsonbIterator **val, JsonbIterator **mContained)
Definition: jsonb_util.c:1062
void check_stack_depth(void)
Definition: postgres.c:3563

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

Referenced by jsonb_contained(), and jsonb_contains().

◆ JsonbExtractScalar()

bool JsonbExtractScalar ( JsonbContainer jbc,
JsonbValue res 
)

Definition at line 1968 of file jsonb.c.

1969 {
1970  JsonbIterator *it;
1972  JsonbValue tmp;
1973 
1974  if (!JsonContainerIsArray(jbc) || !JsonContainerIsScalar(jbc))
1975  {
1976  /* inform caller about actual type of container */
1977  res->type = (JsonContainerIsArray(jbc)) ? jbvArray : jbvObject;
1978  return false;
1979  }
1980 
1981  /*
1982  * A root scalar is stored as an array of one element, so we get the array
1983  * and then its first (and only) member.
1984  */
1985  it = JsonbIteratorInit(jbc);
1986 
1987  tok = JsonbIteratorNext(&it, &tmp, true);
1988  Assert(tok == WJB_BEGIN_ARRAY);
1989  Assert(tmp.val.array.nElems == 1 && tmp.val.array.rawScalar);
1990 
1991  tok = JsonbIteratorNext(&it, res, true);
1992  Assert(tok == WJB_ELEM);
1994 
1995  tok = JsonbIteratorNext(&it, &tmp, true);
1996  Assert(tok == WJB_END_ARRAY);
1997 
1998  tok = JsonbIteratorNext(&it, &tmp, true);
1999  Assert(tok == WJB_DONE);
2000 
2001  return true;
2002 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:197

References Assert, IsAJsonbScalar, jbvArray, jbvObject, JsonbIteratorInit(), JsonbIteratorNext(), JsonContainerIsArray, JsonContainerIsScalar, PG_USED_FOR_ASSERTS_ONLY, res, 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(), JsonbContainerTypeName(), JsonbUnquote(), JsonItemFromDatum(), and JsonPathValue().

◆ JsonbHashScalarValue()

void JsonbHashScalarValue ( const JsonbValue scalarVal,
uint32 hash 
)

Definition at line 1316 of file jsonb_util.c.

1317 {
1318  uint32 tmp;
1319 
1320  /* Compute hash value for scalarVal */
1321  switch (scalarVal->type)
1322  {
1323  case jbvNull:
1324  tmp = 0x01;
1325  break;
1326  case jbvString:
1327  tmp = DatumGetUInt32(hash_any((const unsigned char *) scalarVal->val.string.val,
1328  scalarVal->val.string.len));
1329  break;
1330  case jbvNumeric:
1331  /* Must hash equal numerics to equal hash codes */
1333  NumericGetDatum(scalarVal->val.numeric)));
1334  break;
1335  case jbvBool:
1336  tmp = scalarVal->val.boolean ? 0x02 : 0x04;
1337 
1338  break;
1339  default:
1340  elog(ERROR, "invalid jsonb scalar type");
1341  tmp = 0; /* keep compiler quiet */
1342  break;
1343  }
1344 
1345  /*
1346  * Combine hash values of successive keys, values and elements by rotating
1347  * the previous value left 1 bit, then XOR'ing in the new
1348  * key/value/element's hash value.
1349  */
1350  *hash = pg_rotate_left32(*hash, 1);
1351  *hash ^= tmp;
1352 }
Datum hash_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2694
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:73
static uint32 pg_rotate_left32(uint32 word, int n)
Definition: pg_bitutils.h:404
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:222
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715

References DatumGetUInt32(), DirectFunctionCall1, elog, ERROR, hash(), hash_any(), hash_numeric(), jbvBool, jbvNull, jbvNumeric, jbvString, NumericGetDatum(), pg_rotate_left32(), 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().

◆ JsonbHashScalarValueExtended()

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

Definition at line 1359 of file jsonb_util.c.

1361 {
1362  uint64 tmp;
1363 
1364  switch (scalarVal->type)
1365  {
1366  case jbvNull:
1367  tmp = seed + 0x01;
1368  break;
1369  case jbvString:
1370  tmp = DatumGetUInt64(hash_any_extended((const unsigned char *) scalarVal->val.string.val,
1371  scalarVal->val.string.len,
1372  seed));
1373  break;
1374  case jbvNumeric:
1376  NumericGetDatum(scalarVal->val.numeric),
1377  UInt64GetDatum(seed)));
1378  break;
1379  case jbvBool:
1380  if (seed)
1382  BoolGetDatum(scalarVal->val.boolean),
1383  UInt64GetDatum(seed)));
1384  else
1385  tmp = scalarVal->val.boolean ? 0x02 : 0x04;
1386 
1387  break;
1388  default:
1389  elog(ERROR, "invalid jsonb scalar type");
1390  break;
1391  }
1392 
1394  *hash ^= tmp;
1395 }
Datum hash_numeric_extended(PG_FUNCTION_ARGS)
Definition: numeric.c:2774
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define ROTATE_HIGH_AND_LOW_32BITS(v)
Definition: hashfn.h:18
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
Datum hashcharextended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:53
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:419
static Datum UInt64GetDatum(uint64 X)
Definition: postgres.h:436
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102

References BoolGetDatum(), DatumGetUInt64(), DirectFunctionCall2, elog, ERROR, hash(), 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().

◆ JsonbIteratorInit()

◆ JsonbIteratorNext()

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

Definition at line 853 of file jsonb_util.c.

854 {
855  if (*it == NULL)
856  return WJB_DONE;
857 
858  /*
859  * When stepping into a nested container, we jump back here to start
860  * processing the child. We will not recurse further in one call, because
861  * processing the child will always begin in JBI_ARRAY_START or
862  * JBI_OBJECT_START state.
863  */
864 recurse:
865  switch ((*it)->state)
866  {
867  case JBI_ARRAY_START:
868  /* Set v to array on first array call */
869  val->type = jbvArray;
870  val->val.array.nElems = (*it)->nElems;
871 
872  /*
873  * v->val.array.elems is not actually set, because we aren't doing
874  * a full conversion
875  */
876  val->val.array.rawScalar = (*it)->isScalar;
877  (*it)->curIndex = 0;
878  (*it)->curDataOffset = 0;
879  (*it)->curValueOffset = 0; /* not actually used */
880  /* Set state for next call */
881  (*it)->state = JBI_ARRAY_ELEM;
882  return WJB_BEGIN_ARRAY;
883 
884  case JBI_ARRAY_ELEM:
885  if ((*it)->curIndex >= (*it)->nElems)
886  {
887  /*
888  * All elements within array already processed. Report this
889  * to caller, and give it back original parent iterator (which
890  * independently tracks iteration progress at its level of
891  * nesting).
892  */
893  *it = freeAndGetParent(*it);
894  return WJB_END_ARRAY;
895  }
896 
897  fillJsonbValue((*it)->container, (*it)->curIndex,
898  (*it)->dataProper, (*it)->curDataOffset,
899  val);
900 
901  JBE_ADVANCE_OFFSET((*it)->curDataOffset,
902  (*it)->children[(*it)->curIndex]);
903  (*it)->curIndex++;
904 
905  if (!IsAJsonbScalar(val) && !skipNested)
906  {
907  /* Recurse into container. */
908  *it = iteratorFromContainer(val->val.binary.data, *it);
909  goto recurse;
910  }
911  else
912  {
913  /*
914  * Scalar item in array, or a container and caller didn't want
915  * us to recurse into it.
916  */
917  return WJB_ELEM;
918  }
919 
920  case JBI_OBJECT_START:
921  /* Set v to object on first object call */
922  val->type = jbvObject;
923  val->val.object.nPairs = (*it)->nElems;
924 
925  /*
926  * v->val.object.pairs is not actually set, because we aren't
927  * doing a full conversion
928  */
929  (*it)->curIndex = 0;
930  (*it)->curDataOffset = 0;
931  (*it)->curValueOffset = getJsonbOffset((*it)->container,
932  (*it)->nElems);
933  /* Set state for next call */
934  (*it)->state = JBI_OBJECT_KEY;
935  return WJB_BEGIN_OBJECT;
936 
937  case JBI_OBJECT_KEY:
938  if ((*it)->curIndex >= (*it)->nElems)
939  {
940  /*
941  * All pairs within object already processed. Report this to
942  * caller, and give it back original containing iterator
943  * (which independently tracks iteration progress at its level
944  * of nesting).
945  */
946  *it = freeAndGetParent(*it);
947  return WJB_END_OBJECT;
948  }
949  else
950  {
951  /* Return key of a key/value pair. */
952  fillJsonbValue((*it)->container, (*it)->curIndex,
953  (*it)->dataProper, (*it)->curDataOffset,
954  val);
955  if (val->type != jbvString)
956  elog(ERROR, "unexpected jsonb type as object key");
957 
958  /* Set state for next call */
959  (*it)->state = JBI_OBJECT_VALUE;
960  return WJB_KEY;
961  }
962 
963  case JBI_OBJECT_VALUE:
964  /* Set state for next call */
965  (*it)->state = JBI_OBJECT_KEY;
966 
967  fillJsonbValue((*it)->container, (*it)->curIndex + (*it)->nElems,
968  (*it)->dataProper, (*it)->curValueOffset,
969  val);
970 
971  JBE_ADVANCE_OFFSET((*it)->curDataOffset,
972  (*it)->children[(*it)->curIndex]);
973  JBE_ADVANCE_OFFSET((*it)->curValueOffset,
974  (*it)->children[(*it)->curIndex + (*it)->nElems]);
975  (*it)->curIndex++;
976 
977  /*
978  * Value may be a container, in which case we recurse with new,
979  * child iterator (unless the caller asked not to, by passing
980  * skipNested).
981  */
982  if (!IsAJsonbScalar(val) && !skipNested)
983  {
984  *it = iteratorFromContainer(val->val.binary.data, *it);
985  goto recurse;
986  }
987  else
988  return WJB_VALUE;
989  }
990 
991  elog(ERROR, "invalid iterator state");
992  return -1;
993 }
static JsonbIterator * freeAndGetParent(JsonbIterator *it)
Definition: jsonb_util.c:1041

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, val, WJB_BEGIN_ARRAY, WJB_BEGIN_OBJECT, WJB_DONE, WJB_ELEM, WJB_END_ARRAY, WJB_END_OBJECT, WJB_KEY, and WJB_VALUE.

Referenced by compareJsonbContainers(), datum_to_jsonb_internal(), each_worker_jsonb(), elements_worker_jsonb(), executeAnyItem(), executeKeyValueMethod(), gin_extract_jsonb(), gin_extract_jsonb_path(), iterate_jsonb_values(), IteratorConcat(), jsonb_agg_transfn_worker(), jsonb_delete(), jsonb_delete_array(), jsonb_delete_idx(), jsonb_hash(), jsonb_hash_extended(), jsonb_object_agg_transfn_worker(), 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().

◆ JsonbPGetDatum()

◆ JsonbToCString()

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

Definition at line 473 of file jsonb.c.

474 {
475  return JsonbToCStringWorker(out, in, estimated_len, false);
476 }
static char * JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent)
Definition: jsonb.c:491

References JsonbToCStringWorker().

Referenced by jsonb_get_element(), jsonb_out(), jsonb_send(), JsonbUnquote(), JsonbValueAsText(), and populate_scalar().

◆ JsonbToCStringIndent()

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

Definition at line 482 of file jsonb.c.

483 {
484  return JsonbToCStringWorker(out, in, estimated_len, true);
485 }

References JsonbToCStringWorker().

Referenced by jsonb_pretty().

◆ JsonbToJsonbValue()

void JsonbToJsonbValue ( Jsonb jsonb,
JsonbValue val 
)

Definition at line 72 of file jsonb_util.c.

73 {
74  val->type = jbvBinary;
75  val->val.binary.data = &jsonb->root;
76  val->val.binary.len = VARSIZE(jsonb) - VARHDRSZ;
77 }
#define VARHDRSZ
Definition: c.h:695

References jbvBinary, Jsonb::root, val, VARHDRSZ, and VARSIZE.

Referenced by jsonb_insert(), jsonb_set(), and jsonb_subscript_assign().

◆ JsonbTypeName()

const char* JsonbTypeName ( JsonbValue val)

Definition at line 180 of file jsonb.c.

181 {
182  switch (val->type)
183  {
184  case jbvBinary:
185  return JsonbContainerTypeName(val->val.binary.data);
186  case jbvObject:
187  return "object";
188  case jbvArray:
189  return "array";
190  case jbvNumeric:
191  return "number";
192  case jbvString:
193  return "string";
194  case jbvBool:
195  return "boolean";
196  case jbvNull:
197  return "null";
198  case jbvDatetime:
199  switch (val->val.datetime.typid)
200  {
201  case DATEOID:
202  return "date";
203  case TIMEOID:
204  return "time without time zone";
205  case TIMETZOID:
206  return "time with time zone";
207  case TIMESTAMPOID:
208  return "timestamp without time zone";
209  case TIMESTAMPTZOID:
210  return "timestamp with time zone";
211  default:
212  elog(ERROR, "unrecognized jsonb value datetime type: %d",
213  val->val.datetime.typid);
214  }
215  return "unknown";
216  default:
217  elog(ERROR, "unrecognized jsonb value type: %d", val->type);
218  return "unknown";
219  }
220 }
static const char * JsonbContainerTypeName(JsonbContainer *jbc)
Definition: jsonb.c:159

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

Referenced by executeItemOptUnwrapTarget(), and JsonbContainerTypeName().

◆ JsonbUnquote()

char* JsonbUnquote ( Jsonb jb)

Definition at line 2166 of file jsonb.c.

2167 {
2168  if (JB_ROOT_IS_SCALAR(jb))
2169  {
2170  JsonbValue v;
2171 
2172  (void) JsonbExtractScalar(&jb->root, &v);
2173 
2174  if (v.type == jbvString)
2175  return pnstrdup(v.val.string.val, v.val.string.len);
2176  else if (v.type == jbvBool)
2177  return pstrdup(v.val.boolean ? "true" : "false");
2178  else if (v.type == jbvNumeric)
2180  PointerGetDatum(v.val.numeric)));
2181  else if (v.type == jbvNull)
2182  return pstrdup("null");
2183  else
2184  {
2185  elog(ERROR, "unrecognized jsonb value type %d", v.type);
2186  return NULL;
2187  }
2188  }
2189  else
2190  return JsonbToCString(NULL, &jb->root, VARSIZE(jb));
2191 }
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:815
bool JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res)
Definition: jsonb.c:1968
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1707
char * pstrdup(const char *in)
Definition: mcxt.c:1696
static char * DatumGetCString(Datum X)
Definition: postgres.h:335

References DatumGetCString(), DirectFunctionCall1, elog, ERROR, JB_ROOT_IS_SCALAR, jbvBool, jbvNull, jbvNumeric, jbvString, JsonbExtractScalar(), JsonbToCString(), numeric_out(), pnstrdup(), PointerGetDatum(), pstrdup(), Jsonb::root, JsonbValue::type, JsonbValue::val, and VARSIZE.

Referenced by json_populate_type().

◆ JsonbValueToJsonb()

Jsonb* JsonbValueToJsonb ( JsonbValue val)

Definition at line 92 of file jsonb_util.c.

93 {
94  Jsonb *out;
95 
96  if (IsAJsonbScalar(val))
97  {
98  /* Scalar value */
99  JsonbParseState *pstate = NULL;
100  JsonbValue *res;
101  JsonbValue scalarArray;
102 
103  scalarArray.type = jbvArray;
104  scalarArray.val.array.rawScalar = true;
105  scalarArray.val.array.nElems = 1;
106 
107  pushJsonbValue(&pstate, WJB_BEGIN_ARRAY, &scalarArray);
108  pushJsonbValue(&pstate, WJB_ELEM, val);
109  res = pushJsonbValue(&pstate, WJB_END_ARRAY, NULL);
110 
111  out = convertToJsonb(res);
112  }
113  else if (val->type == jbvObject || val->type == jbvArray)
114  {
115  out = convertToJsonb(val);
116  }
117  else
118  {
119  Assert(val->type == jbvBinary);
120  out = palloc(VARHDRSZ + val->val.binary.len);
121  SET_VARSIZE(out, VARHDRSZ + val->val.binary.len);
122  memcpy(VARDATA(out), val->val.binary.data, val->val.binary.len);
123  }
124 
125  return out;
126 }
static Jsonb * convertToJsonb(JsonbValue *val)
Definition: jsonb_util.c:1548
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

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

Referenced by datum_to_jsonb(), each_worker_jsonb(), elements_worker_jsonb(), ExecEvalJsonExprPath(), ExecGetJsonValueItemString(), executeKeyValueMethod(), hstore_to_jsonb(), hstore_to_jsonb_loose(), jsonb_agg_finalfn(), jsonb_agg_transfn_worker(), jsonb_array_element(), jsonb_build_array_noargs(), jsonb_build_array_worker(), jsonb_build_object_noargs(), jsonb_build_object_worker(), jsonb_concat(), jsonb_delete(), jsonb_delete_array(), jsonb_delete_idx(), jsonb_delete_path(), jsonb_from_cstring(), jsonb_get_element(), jsonb_insert(), jsonb_object(), jsonb_object_agg_finalfn(), jsonb_object_agg_transfn_worker(), jsonb_object_field(), jsonb_object_two_arg(), jsonb_path_query_array_internal(), jsonb_path_query_first_internal(), jsonb_path_query_internal(), jsonb_set(), jsonb_set_element(), jsonb_strip_nulls(), jsonb_subscript_assign(), JsonPathQuery(), JsonTablePlanScanNextRow(), plperl_to_jsonb(), plpython_to_jsonb(), populate_scalar(), and transform_jsonb_string_values().

◆ pushJsonbValue()

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

Definition at line 566 of file jsonb_util.c.

568 {
569  JsonbIterator *it;
570  JsonbValue *res = NULL;
571  JsonbValue v;
572  JsonbIteratorToken tok;
573  int i;
574 
575  if (jbval && (seq == WJB_ELEM || seq == WJB_VALUE) && jbval->type == jbvObject)
576  {
577  pushJsonbValue(pstate, WJB_BEGIN_OBJECT, NULL);
578  for (i = 0; i < jbval->val.object.nPairs; i++)
579  {
580  pushJsonbValue(pstate, WJB_KEY, &jbval->val.object.pairs[i].key);
581  pushJsonbValue(pstate, WJB_VALUE, &jbval->val.object.pairs[i].value);
582  }
583 
584  return pushJsonbValue(pstate, WJB_END_OBJECT, NULL);
585  }
586 
587  if (jbval && (seq == WJB_ELEM || seq == WJB_VALUE) && jbval->type == jbvArray)
588  {
589  pushJsonbValue(pstate, WJB_BEGIN_ARRAY, NULL);
590  for (i = 0; i < jbval->val.array.nElems; i++)
591  {
592  pushJsonbValue(pstate, WJB_ELEM, &jbval->val.array.elems[i]);
593  }
594 
595  return pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
596  }
597 
598  if (!jbval || (seq != WJB_ELEM && seq != WJB_VALUE) ||
599  jbval->type != jbvBinary)
600  {
601  /* drop through */
602  return pushJsonbValueScalar(pstate, seq, jbval);
603  }
604 
605  /* unpack the binary and add each piece to the pstate */
606  it = JsonbIteratorInit(jbval->val.binary.data);
607 
608  if ((jbval->val.binary.data->header & JB_FSCALAR) && *pstate)
609  {
610  tok = JsonbIteratorNext(&it, &v, true);
611  Assert(tok == WJB_BEGIN_ARRAY);
612  Assert(v.type == jbvArray && v.val.array.rawScalar);
613 
614  tok = JsonbIteratorNext(&it, &v, true);
615  Assert(tok == WJB_ELEM);
616 
617  res = pushJsonbValueScalar(pstate, seq, &v);
618 
619  tok = JsonbIteratorNext(&it, &v, true);
620  Assert(tok == WJB_END_ARRAY);
621  Assert(it == NULL);
622 
623  return res;
624  }
625 
626  while ((tok = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
627  res = pushJsonbValueScalar(pstate, tok,
628  tok < WJB_BEGIN_ARRAY ||
629  (tok == WJB_BEGIN_ARRAY &&
630  v.val.array.rawScalar) ? &v : NULL);
631 
632  return res;
633 }
#define JB_FSCALAR
Definition: jsonb.h:201
static JsonbValue * pushJsonbValueScalar(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *scalarVal)
Definition: jsonb_util.c:640

References Assert, i, JB_FSCALAR, jbvArray, jbvBinary, jbvObject, JsonbIteratorInit(), JsonbIteratorNext(), pushJsonbValueScalar(), res, 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 array_dim_to_jsonb(), array_to_jsonb_internal(), AV_to_JsonbValue(), composite_to_jsonb(), datum_to_jsonb_internal(), executeKeyValueMethod(), hstore_to_jsonb(), hstore_to_jsonb_loose(), HV_to_JsonbValue(), IteratorConcat(), jsonb_agg_finalfn(), jsonb_agg_transfn_worker(), jsonb_build_array_noargs(), jsonb_build_array_worker(), jsonb_build_object_noargs(), jsonb_build_object_worker(), 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_worker(), jsonb_object_two_arg(), jsonb_strip_nulls(), JsonbValueToJsonb(), PLyMapping_ToJsonbValue(), PLyObject_ToJsonbValue(), PLySequence_ToJsonbValue(), push_null_elements(), push_path(), setPath(), setPathArray(), setPathObject(), SV_to_JsonbValue(), transform_jsonb_string_values(), and wrapItemsInArray().

◆ to_jsonb_is_immutable()

bool to_jsonb_is_immutable ( Oid  typoid)

Definition at line 1049 of file jsonb.c.

1050 {
1051  JsonTypeCategory tcategory;
1052  Oid outfuncoid;
1053 
1054  json_categorize_type(typoid, true, &tcategory, &outfuncoid);
1055 
1056  switch (tcategory)
1057  {
1058  case JSONTYPE_NULL:
1059  case JSONTYPE_BOOL:
1060  case JSONTYPE_JSON:
1061  case JSONTYPE_JSONB:
1062  return true;
1063 
1064  case JSONTYPE_DATE:
1065  case JSONTYPE_TIMESTAMP:
1066  case JSONTYPE_TIMESTAMPTZ:
1067  return false;
1068 
1069  case JSONTYPE_ARRAY:
1070  return false; /* TODO recurse into elements */
1071 
1072  case JSONTYPE_COMPOSITE:
1073  return false; /* TODO recurse into fields */
1074 
1075  case JSONTYPE_NUMERIC:
1076  case JSONTYPE_CAST:
1077  case JSONTYPE_OTHER:
1078  return func_volatile(outfuncoid) == PROVOLATILE_IMMUTABLE;
1079  }
1080 
1081  return false; /* not reached */
1082 }
void json_categorize_type(Oid typoid, bool is_jsonb, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: jsonfuncs.c:5976
JsonTypeCategory
Definition: jsonfuncs.h:69
@ JSONTYPE_JSON
Definition: jsonfuncs.h:76
@ JSONTYPE_NULL
Definition: jsonfuncs.h:70
@ JSONTYPE_TIMESTAMP
Definition: jsonfuncs.h:74
@ JSONTYPE_NUMERIC
Definition: jsonfuncs.h:72
@ JSONTYPE_DATE
Definition: jsonfuncs.h:73
@ JSONTYPE_BOOL
Definition: jsonfuncs.h:71
@ JSONTYPE_OTHER
Definition: jsonfuncs.h:81
@ JSONTYPE_CAST
Definition: jsonfuncs.h:80
@ JSONTYPE_COMPOSITE
Definition: jsonfuncs.h:79
@ JSONTYPE_ARRAY
Definition: jsonfuncs.h:78
@ JSONTYPE_TIMESTAMPTZ
Definition: jsonfuncs.h:75
@ JSONTYPE_JSONB
Definition: jsonfuncs.h:77
char func_volatile(Oid funcid)
Definition: lsyscache.c:1780
unsigned int Oid
Definition: postgres_ext.h:31

References func_volatile(), json_categorize_type(), JSONTYPE_ARRAY, JSONTYPE_BOOL, JSONTYPE_CAST, JSONTYPE_COMPOSITE, JSONTYPE_DATE, JSONTYPE_JSON, JSONTYPE_JSONB, JSONTYPE_NULL, JSONTYPE_NUMERIC, JSONTYPE_OTHER, JSONTYPE_TIMESTAMP, and JSONTYPE_TIMESTAMPTZ.

Referenced by contain_mutable_functions_walker().