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 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 70 of file jsonb.h.

◆ JsonbParseState

◆ JsonbValue

typedef struct JsonbValue JsonbValue

Definition at line 71 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,
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{
23 WJB_KEY,
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
250 /*
251 * There should be an "else" here, to prevent us from
252 * overriding the above, but we can't change the sort
253 * order now, so there is a mild anomaly that an empty
254 * top level array sorts less than null.
255 */
256 if (va.val.array.nElems != vb.val.array.nElems)
257 res = (va.val.array.nElems > vb.val.array.nElems) ? 1 : -1;
258 break;
259 case jbvObject:
260 if (va.val.object.nPairs != vb.val.object.nPairs)
261 res = (va.val.object.nPairs > vb.val.object.nPairs) ? 1 : -1;
262 break;
263 case jbvBinary:
264 elog(ERROR, "unexpected jbvBinary value");
265 break;
266 case jbvDatetime:
267 elog(ERROR, "unexpected jbvDatetime value");
268 break;
269 }
270 }
271 else
272 {
273 /* Type-defined order */
274 res = (va.type > vb.type) ? 1 : -1;
275 }
276 }
277 else
278 {
279 /*
280 * It's safe to assume that the types differed, and that the va
281 * and vb values passed were set.
282 *
283 * If the two values were of the same container type, then there'd
284 * have been a chance to observe the variation in the number of
285 * elements/pairs (when processing WJB_BEGIN_OBJECT, say). They're
286 * either two heterogeneously-typed containers, or a container and
287 * some scalar type.
288 *
289 * We don't have to consider the WJB_END_ARRAY and WJB_END_OBJECT
290 * cases here, because we would have seen the corresponding
291 * WJB_BEGIN_ARRAY and WJB_BEGIN_OBJECT tokens first, and
292 * concluded that they don't match.
293 */
294 Assert(ra != WJB_END_ARRAY && ra != WJB_END_OBJECT);
295 Assert(rb != WJB_END_ARRAY && rb != WJB_END_OBJECT);
296
297 Assert(va.type != vb.type);
298 Assert(va.type != jbvBinary);
299 Assert(vb.type != jbvBinary);
300 /* Type-defined order */
301 res = (va.type > vb.type) ? 1 : -1;
302 }
303 }
304 while (res == 0);
305
306 while (ita != NULL)
307 {
308 JsonbIterator *i = ita->parent;
309
310 pfree(ita);
311 ita = i;
312 }
313 while (itb != NULL)
314 {
315 JsonbIterator *i = itb->parent;
316
317 pfree(itb);
318 itb = i;
319 }
320
321 return res;
322}
#define Assert(condition)
Definition: c.h:815
#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:824
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:860
static int compareJsonbScalarValue(JsonbValue *a, JsonbValue *b)
Definition: jsonb_util.c:1440
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()

◆ 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 351 of file jsonb_util.c.

353{
354 JEntry *children = container->children;
355 int count = JsonContainerSize(container);
356
357 Assert((flags & ~(JB_FARRAY | JB_FOBJECT)) == 0);
358
359 /* Quick out without a palloc cycle if object/array is empty */
360 if (count <= 0)
361 return NULL;
362
363 if ((flags & JB_FARRAY) && JsonContainerIsArray(container))
364 {
365 JsonbValue *result = palloc(sizeof(JsonbValue));
366 char *base_addr = (char *) (children + count);
367 uint32 offset = 0;
368 int i;
369
370 for (i = 0; i < count; i++)
371 {
372 fillJsonbValue(container, i, base_addr, offset, result);
373
374 if (key->type == result->type)
375 {
376 if (equalsJsonbScalarValue(key, result))
377 return result;
378 }
379
380 JBE_ADVANCE_OFFSET(offset, children[i]);
381 }
382
383 pfree(result);
384 }
385 else if ((flags & JB_FOBJECT) && JsonContainerIsObject(container))
386 {
387 /* Object key passed by caller must be a string */
388 Assert(key->type == jbvString);
389
390 return getKeyJsonValueFromContainer(container, key->val.string.val,
391 key->val.string.len, NULL);
392 }
393
394 /* Not found */
395 return NULL;
396}
uint32_t uint32
Definition: c.h:488
#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:512
JsonbValue * getKeyJsonValueFromContainer(JsonbContainer *container, const char *keyVal, int keyLen, JsonbValue *res)
Definition: jsonb_util.c:405
static bool equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b)
Definition: jsonb_util.c:1408
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 475 of file jsonb_util.c.

476{
477 JsonbValue *result;
478 char *base_addr;
479 uint32 nelements;
480
481 if (!JsonContainerIsArray(container))
482 elog(ERROR, "not a jsonb array");
483
484 nelements = JsonContainerSize(container);
485 base_addr = (char *) &container->children[nelements];
486
487 if (i >= nelements)
488 return NULL;
489
490 result = palloc(sizeof(JsonbValue));
491
492 fillJsonbValue(container, i, base_addr,
493 getJsonbOffset(container, i),
494 result);
495
496 return result;
497}
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:96

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 405 of file jsonb_util.c.

407{
408 JEntry *children = container->children;
409 int count = JsonContainerSize(container);
410 char *baseAddr;
411 uint32 stopLow,
412 stopHigh;
413
414 Assert(JsonContainerIsObject(container));
415
416 /* Quick out without a palloc cycle if object is empty */
417 if (count <= 0)
418 return NULL;
419
420 /*
421 * Binary search the container. Since we know this is an object, account
422 * for *Pairs* of Jentrys
423 */
424 baseAddr = (char *) (children + count * 2);
425 stopLow = 0;
426 stopHigh = count;
427 while (stopLow < stopHigh)
428 {
429 uint32 stopMiddle;
430 int difference;
431 const char *candidateVal;
432 int candidateLen;
433
434 stopMiddle = stopLow + (stopHigh - stopLow) / 2;
435
436 candidateVal = baseAddr + getJsonbOffset(container, stopMiddle);
437 candidateLen = getJsonbLength(container, stopMiddle);
438
439 difference = lengthCompareJsonbString(candidateVal, candidateLen,
440 keyVal, keyLen);
441
442 if (difference == 0)
443 {
444 /* Found our key, return corresponding value */
445 int index = stopMiddle + count;
446
447 if (!res)
448 res = palloc(sizeof(JsonbValue));
449
450 fillJsonbValue(container, index, baseAddr,
451 getJsonbOffset(container, index),
452 res);
453
454 return res;
455 }
456 else
457 {
458 if (difference < 0)
459 stopLow = stopMiddle + 1;
460 else
461 stopHigh = stopMiddle;
462 }
463 }
464
465 /* Not found */
466 return NULL;
467}
Datum difference(PG_FUNCTION_ARGS)
static int lengthCompareJsonbString(const char *val1, int len1, const char *val2, int len2)
Definition: jsonb_util.c:1906
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:30
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
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:573
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:92
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 */
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:475
static text * JsonbValueAsText(JsonbValue *v)
Definition: jsonfuncs.c:1804
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
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:644
#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 1069 of file jsonb_util.c.

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

References Assert, check_stack_depth(), elog, equalsJsonbScalarValue(), ERROR, findJsonbValueFromContainer(), getKeyJsonValueFromContainer(), i, IsAJsonbScalar, j, JB_FARRAY, jbvArray, jbvBinary, jbvObject, jbvString, JsonbDeepContains(), 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(), jsonb_contains(), and JsonbDeepContains().

◆ JsonbExtractScalar()

bool JsonbExtractScalar ( JsonbContainer jbc,
JsonbValue res 
)

Definition at line 1968 of file jsonb.c.

1969{
1970 JsonbIterator *it;
1972 JsonbValue tmp;
1973
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:204

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 1323 of file jsonb_util.c.

1324{
1325 uint32 tmp;
1326
1327 /* Compute hash value for scalarVal */
1328 switch (scalarVal->type)
1329 {
1330 case jbvNull:
1331 tmp = 0x01;
1332 break;
1333 case jbvString:
1334 tmp = DatumGetUInt32(hash_any((const unsigned char *) scalarVal->val.string.val,
1335 scalarVal->val.string.len));
1336 break;
1337 case jbvNumeric:
1338 /* Must hash equal numerics to equal hash codes */
1340 NumericGetDatum(scalarVal->val.numeric)));
1341 break;
1342 case jbvBool:
1343 tmp = scalarVal->val.boolean ? 0x02 : 0x04;
1344
1345 break;
1346 default:
1347 elog(ERROR, "invalid jsonb scalar type");
1348 tmp = 0; /* keep compiler quiet */
1349 break;
1350 }
1351
1352 /*
1353 * Combine hash values of successive keys, values and elements by rotating
1354 * the previous value left 1 bit, then XOR'ing in the new
1355 * key/value/element's hash value.
1356 */
1357 *hash = pg_rotate_left32(*hash, 1);
1358 *hash ^= tmp;
1359}
Datum hash_numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:2815
#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:402
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:227
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 1366 of file jsonb_util.c.

1368{
1369 uint64 tmp;
1370
1371 switch (scalarVal->type)
1372 {
1373 case jbvNull:
1374 tmp = seed + 0x01;
1375 break;
1376 case jbvString:
1377 tmp = DatumGetUInt64(hash_any_extended((const unsigned char *) scalarVal->val.string.val,
1378 scalarVal->val.string.len,
1379 seed));
1380 break;
1381 case jbvNumeric:
1383 NumericGetDatum(scalarVal->val.numeric),
1384 UInt64GetDatum(seed)));
1385 break;
1386 case jbvBool:
1387 if (seed)
1389 BoolGetDatum(scalarVal->val.boolean),
1390 UInt64GetDatum(seed)));
1391 else
1392 tmp = scalarVal->val.boolean ? 0x02 : 0x04;
1393
1394 break;
1395 default:
1396 elog(ERROR, "invalid jsonb scalar type");
1397 break;
1398 }
1399
1401 *hash ^= tmp;
1402}
Datum hash_numeric_extended(PG_FUNCTION_ARGS)
Definition: numeric.c:2895
uint64_t uint64
Definition: c.h:489
#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:424
static Datum UInt64GetDatum(uint64 X)
Definition: postgres.h:441
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107

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 860 of file jsonb_util.c.

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

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:649

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 2229 of file jsonb.c.

2230{
2231 if (JB_ROOT_IS_SCALAR(jb))
2232 {
2233 JsonbValue v;
2234
2235 (void) JsonbExtractScalar(&jb->root, &v);
2236
2237 if (v.type == jbvString)
2238 return pnstrdup(v.val.string.val, v.val.string.len);
2239 else if (v.type == jbvBool)
2240 return pstrdup(v.val.boolean ? "true" : "false");
2241 else if (v.type == jbvNumeric)
2243 PointerGetDatum(v.val.numeric)));
2244 else if (v.type == jbvNull)
2245 return pstrdup("null");
2246 else
2247 {
2248 elog(ERROR, "unrecognized jsonb value type %d", v.type);
2249 return NULL;
2250 }
2251 }
2252 else
2253 return JsonbToCString(NULL, &jb->root, VARSIZE(jb));
2254}
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:816
bool JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res)
Definition: jsonb.c:1968
char * pstrdup(const char *in)
Definition: mcxt.c:1696
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1707
static char * DatumGetCString(Datum X)
Definition: postgres.h:340

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
97 {
98 /* Scalar value */
99 JsonbParseState *pstate = NULL;
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:1555
#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 573 of file jsonb_util.c.

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

References Assert, i, JB_FSCALAR, jbvArray, jbvBinary, jbvObject, JsonbIteratorInit(), JsonbIteratorNext(), pushJsonbValue(), 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(), pushJsonbValue(), 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:
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:1807
unsigned int Oid
Definition: postgres_ext.h:32

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