1 /*-------------------------------------------------------------------------
4 * converting between Jsonb and JsonbValues, and iterating.
6 * Copyright (c) 2014, PostgreSQL Global Development Group
10 * src/backend/utils/adt/jsonb_util.c
12 *-------------------------------------------------------------------------
16 #include "access/hash.h"
17 #include "catalog/pg_collation.h"
18 #include "miscadmin.h"
19 #include "utils/builtins.h"
20 #include "utils/jsonb.h"
21 #include "utils/memutils.h"
24 * Maximum number of elements in an array (or key/value pairs in an object).
25 * This is limited by two things: the size of the JEntry array must fit
26 * in MaxAllocSize, and the number of elements (or pairs) must fit in the bits
27 * reserved for that in the JsonbContainer.header field.
29 * (the total size of an array's elements is also limited by JENTRY_POSMASK,
30 * but we're not concerned about that here)
32 #define JSONB_MAX_ELEMS (Min(MaxAllocSize / sizeof(JsonbValue), JB_CMASK))
33 #define JSONB_MAX_PAIRS (Min(MaxAllocSize / sizeof(JsonbPair), JB_CMASK))
35 static void fillJsonbValue(JEntry *array, int index, char *base_addr,
37 static bool equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b);
38 static int compareJsonbScalarValue(JsonbValue *a, JsonbValue *b);
39 static Jsonb *convertToJsonb(JsonbValue *val);
40 static void convertJsonbValue(StringInfo buffer, JEntry *header, JsonbValue *val, int level);
41 static void convertJsonbArray(StringInfo buffer, JEntry *header, JsonbValue *val, int level);
42 static void convertJsonbObject(StringInfo buffer, JEntry *header, JsonbValue *val, int level);
43 static void convertJsonbScalar(StringInfo buffer, JEntry *header, JsonbValue *scalarVal);
45 static int reserveFromBuffer(StringInfo buffer, int len);
46 static void appendToBuffer(StringInfo buffer, const char *data, int len);
47 static void copyToBuffer(StringInfo buffer, int offset, const char *data, int len);
48 static short padBufferToInt(StringInfo buffer);
50 static JsonbIterator *iteratorFromContainer(JsonbContainer *container, JsonbIterator *parent);
51 static JsonbIterator *freeAndGetParent(JsonbIterator *it);
52 static JsonbParseState *pushState(JsonbParseState **pstate);
53 static void appendKey(JsonbParseState *pstate, JsonbValue *scalarVal);
54 static void appendValue(JsonbParseState *pstate, JsonbValue *scalarVal);
55 static void appendElement(JsonbParseState *pstate, JsonbValue *scalarVal);
56 static int lengthCompareJsonbStringValue(const void *a, const void *b);
57 static int lengthCompareJsonbPair(const void *a, const void *b, void *arg);
58 static void uniqueifyJsonbObject(JsonbValue *object);
61 * Turn an in-memory JsonbValue into a Jsonb for on-disk storage.
63 * There isn't a JsonbToJsonbValue(), because generally we find it more
64 * convenient to directly iterate through the Jsonb representation and only
65 * really convert nested scalar values. JsonbIteratorNext() does this, so that
66 * clients of the iteration code don't have to directly deal with the binary
67 * representation (JsonbDeepContains() is a notable exception, although all
68 * exceptions are internal to this module). In general, functions that accept
69 * a JsonbValue argument are concerned with the manipulation of scalar values,
70 * or simple containers of scalar values, where it would be inconvenient to
71 * deal with a great amount of other state.
74 JsonbValueToJsonb(JsonbValue *val)
78 if (IsAJsonbScalar(val))
81 JsonbParseState *pstate = NULL;
83 JsonbValue scalarArray;
85 scalarArray.type = jbvArray;
86 scalarArray.val.array.rawScalar = true;
87 scalarArray.val.array.nElems = 1;
89 pushJsonbValue(&pstate, WJB_BEGIN_ARRAY, &scalarArray);
90 pushJsonbValue(&pstate, WJB_ELEM, val);
91 res = pushJsonbValue(&pstate, WJB_END_ARRAY, NULL);
93 out = convertToJsonb(res);
95 else if (val->type == jbvObject || val->type == jbvArray)
97 out = convertToJsonb(val);
101 Assert(val->type == jbvBinary);
102 out = palloc(VARHDRSZ + val->val.binary.len);
103 SET_VARSIZE(out, VARHDRSZ + val->val.binary.len);
104 memcpy(VARDATA(out), val->val.binary.data, val->val.binary.len);
111 * BT comparator worker function. Returns an integer less than, equal to, or
112 * greater than zero, indicating whether a is less than, equal to, or greater
113 * than b. Consistent with the requirements for a B-Tree operator class
115 * Strings are compared lexically, in contrast with other places where we use a
116 * much simpler comparator logic for searching through Strings. Since this is
117 * called from B-Tree support function 1, we're careful about not leaking
121 compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
127 ita = JsonbIteratorInit(a);
128 itb = JsonbIteratorInit(b);
137 ra = JsonbIteratorNext(&ita, &va, false);
138 rb = JsonbIteratorNext(&itb, &vb, false);
144 /* Decisively equal */
148 if (ra == WJB_END_ARRAY || ra == WJB_END_OBJECT)
151 * There is no array or object to compare at this stage of
152 * processing. jbvArray/jbvObject values are compared
153 * initially, at the WJB_BEGIN_ARRAY and WJB_BEGIN_OBJECT
159 if (va.type == vb.type)
167 res = compareJsonbScalarValue(&va, &vb);
172 * This could be a "raw scalar" pseudo array. That's
173 * a special case here though, since we still want the
174 * general type-based comparisons to apply, and as far
175 * as we're concerned a pseudo array is just a scalar.
177 if (va.val.array.rawScalar != vb.val.array.rawScalar)
178 res = (va.val.array.rawScalar) ? -1 : 1;
179 if (va.val.array.nElems != vb.val.array.nElems)
180 res = (va.val.array.nElems > vb.val.array.nElems) ? 1 : -1;
183 if (va.val.object.nPairs != vb.val.object.nPairs)
184 res = (va.val.object.nPairs > vb.val.object.nPairs) ? 1 : -1;
187 elog(ERROR, "unexpected jbvBinary value");
192 /* Type-defined order */
193 res = (va.type > vb.type) ? 1 : -1;
199 * It's safe to assume that the types differed, and that the va
200 * and vb values passed were set.
202 * If the two values were of the same container type, then there'd
203 * have been a chance to observe the variation in the number of
204 * elements/pairs (when processing WJB_BEGIN_OBJECT, say). They're
205 * either two heterogeneously-typed containers, or a container and
208 * We don't have to consider the WJB_END_ARRAY and WJB_END_OBJECT
209 * cases here, because we would have seen the corresponding
210 * WJB_BEGIN_ARRAY and WJB_BEGIN_OBJECT tokens first, and
211 * concluded that they don't match.
213 Assert(ra != WJB_END_ARRAY && ra != WJB_END_OBJECT);
214 Assert(rb != WJB_END_ARRAY && rb != WJB_END_OBJECT);
216 Assert(va.type != vb.type);
217 Assert(va.type != jbvBinary);
218 Assert(vb.type != jbvBinary);
219 /* Type-defined order */
220 res = (va.type > vb.type) ? 1 : -1;
227 JsonbIterator *i = ita->parent;
234 JsonbIterator *i = itb->parent;
244 * Find value in object (i.e. the "value" part of some key/value pair in an
245 * object), or find a matching element if we're looking through an array. Do
246 * so on the basis of equality of the object keys only, or alternatively
247 * element values only, with a caller-supplied value "key". The "flags"
248 * argument allows the caller to specify which container types are of interest.
250 * This exported utility function exists to facilitate various cases concerned
251 * with "containment". If asked to look through an object, the caller had
252 * better pass a Jsonb String, because their keys can only be strings.
253 * Otherwise, for an array, any type of JsonbValue will do.
255 * In order to proceed with the search, it is necessary for callers to have
256 * both specified an interest in exactly one particular container type with an
257 * appropriate flag, as well as having the pointed-to Jsonb container be of
258 * one of those same container types at the top level. (Actually, we just do
259 * whichever makes sense to save callers the trouble of figuring it out - at
260 * most one can make sense, because the container either points to an array
261 * (possibly a "raw scalar" pseudo array) or an object.)
263 * Note that we can return a jbvBinary JsonbValue if this is called on an
264 * object, but we never do so on an array. If the caller asks to look through
265 * a container type that is not of the type pointed to by the container,
266 * immediately fall through and return NULL. If we cannot find the value,
267 * return NULL. Otherwise, return palloc()'d copy of value.
270 findJsonbValueFromContainer(JsonbContainer *container, uint32 flags,
273 JEntry *children = container->children;
274 int count = (container->header & JB_CMASK);
275 JsonbValue *result = palloc(sizeof(JsonbValue));
277 Assert((flags & ~(JB_FARRAY | JB_FOBJECT)) == 0);
279 if (flags & JB_FARRAY & container->header)
281 char *base_addr = (char *) (children + count);
284 for (i = 0; i < count; i++)
286 fillJsonbValue(children, i, base_addr, result);
288 if (key->type == result->type)
290 if (equalsJsonbScalarValue(key, result))
295 else if (flags & JB_FOBJECT & container->header)
297 /* Since this is an object, account for *Pairs* of Jentrys */
298 char *base_addr = (char *) (children + count * 2);
302 /* Object key past by caller must be a string */
303 Assert(key->type == jbvString);
305 /* Binary search on object/pair keys *only* */
306 while (stopLow < count)
310 JsonbValue candidate;
313 * Note how we compensate for the fact that we're iterating
314 * through pairs (not entries) throughout.
316 stopMiddle = stopLow + (count - stopLow) / 2;
318 index = stopMiddle * 2;
320 candidate.type = jbvString;
321 candidate.val.string.val = base_addr + JBE_OFF(children, index);
322 candidate.val.string.len = JBE_LEN(children, index);
324 difference = lengthCompareJsonbStringValue(&candidate, key);
328 /* Found our key, return value */
329 fillJsonbValue(children, index + 1, base_addr, result);
336 stopLow = stopMiddle + 1;
349 * Get i-th value of a Jsonb array.
351 * Returns palloc()'d copy of the value, or NULL if it does not exist.
354 getIthJsonbValueFromContainer(JsonbContainer *container, uint32 i)
360 if ((container->header & JB_FARRAY) == 0)
361 elog(ERROR, "not a jsonb array");
363 nelements = container->header & JB_CMASK;
364 base_addr = (char *) &container->children[nelements];
369 result = palloc(sizeof(JsonbValue));
371 fillJsonbValue(container->children, i, base_addr, result);
377 * A helper function to fill in a JsonbValue to represent an element of an
378 * array, or a key or value of an object.
380 * A nested array or object will be returned as jbvBinary, ie. it won't be
384 fillJsonbValue(JEntry *children, int index, char *base_addr, JsonbValue *result)
386 JEntry entry = children[index];
388 if (JBE_ISNULL(entry))
390 result->type = jbvNull;
392 else if (JBE_ISSTRING(entry))
394 result->type = jbvString;
395 result->val.string.val = base_addr + JBE_OFF(children, index);
396 result->val.string.len = JBE_LEN(children, index);
397 Assert(result->val.string.len >= 0);
399 else if (JBE_ISNUMERIC(entry))
401 result->type = jbvNumeric;
402 result->val.numeric = (Numeric) (base_addr + INTALIGN(JBE_OFF(children, index)));
404 else if (JBE_ISBOOL_TRUE(entry))
406 result->type = jbvBool;
407 result->val.boolean = true;
409 else if (JBE_ISBOOL_FALSE(entry))
411 result->type = jbvBool;
412 result->val.boolean = false;
416 Assert(JBE_ISCONTAINER(entry));
417 result->type = jbvBinary;
418 result->val.binary.data = (JsonbContainer *) (base_addr + INTALIGN(JBE_OFF(children, index)));
419 result->val.binary.len = JBE_LEN(children, index) - (INTALIGN(JBE_OFF(children, index)) - JBE_OFF(children, index));
424 * Push JsonbValue into JsonbParseState.
426 * Used when parsing JSON tokens to form Jsonb, or when converting an in-memory
427 * JsonbValue to a Jsonb.
429 * Initial state of *JsonbParseState is NULL, since it'll be allocated here
430 * originally (caller will get JsonbParseState back by reference).
432 * Only sequential tokens pertaining to non-container types should pass a
433 * JsonbValue. There is one exception -- WJB_BEGIN_ARRAY callers may pass a
434 * "raw scalar" pseudo array to append that.
437 pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq,
438 JsonbValue *scalarVal)
440 JsonbValue *result = NULL;
444 case WJB_BEGIN_ARRAY:
445 Assert(!scalarVal || scalarVal->val.array.rawScalar);
446 *pstate = pushState(pstate);
447 result = &(*pstate)->contVal;
448 (*pstate)->contVal.type = jbvArray;
449 (*pstate)->contVal.val.array.nElems = 0;
450 (*pstate)->contVal.val.array.rawScalar = (scalarVal &&
451 scalarVal->val.array.rawScalar);
452 if (scalarVal && scalarVal->val.array.nElems > 0)
454 /* Assume that this array is still really a scalar */
455 Assert(scalarVal->type == jbvArray);
456 (*pstate)->size = scalarVal->val.array.nElems;
462 (*pstate)->contVal.val.array.elems = palloc(sizeof(JsonbValue) *
465 case WJB_BEGIN_OBJECT:
467 *pstate = pushState(pstate);
468 result = &(*pstate)->contVal;
469 (*pstate)->contVal.type = jbvObject;
470 (*pstate)->contVal.val.object.nPairs = 0;
472 (*pstate)->contVal.val.object.pairs = palloc(sizeof(JsonbPair) *
476 Assert(scalarVal->type == jbvString);
477 appendKey(*pstate, scalarVal);
480 Assert(IsAJsonbScalar(scalarVal) ||
481 scalarVal->type == jbvBinary);
482 appendValue(*pstate, scalarVal);
485 Assert(IsAJsonbScalar(scalarVal) ||
486 scalarVal->type == jbvBinary);
487 appendElement(*pstate, scalarVal);
490 uniqueifyJsonbObject(&(*pstate)->contVal);
493 /* Steps here common to WJB_END_OBJECT case */
495 result = &(*pstate)->contVal;
498 * Pop stack and push current array/object as value in parent
501 *pstate = (*pstate)->next;
504 switch ((*pstate)->contVal.type)
507 appendElement(*pstate, result);
510 appendValue(*pstate, result);
513 elog(ERROR, "invalid jsonb container type");
518 elog(ERROR, "unrecognized jsonb sequential processing token");
525 * pushJsonbValue() worker: Iteration-like forming of Jsonb
527 static JsonbParseState *
528 pushState(JsonbParseState **pstate)
530 JsonbParseState *ns = palloc(sizeof(JsonbParseState));
537 * pushJsonbValue() worker: Append a pair key to state when generating a Jsonb
540 appendKey(JsonbParseState *pstate, JsonbValue *string)
542 JsonbValue *object = &pstate->contVal;
544 Assert(object->type == jbvObject);
545 Assert(string->type == jbvString);
547 if (object->val.object.nPairs >= JSONB_MAX_PAIRS)
549 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
550 errmsg("number of jsonb object pairs exceeds the maximum allowed (%zu)",
553 if (object->val.object.nPairs >= pstate->size)
556 object->val.object.pairs = repalloc(object->val.object.pairs,
557 sizeof(JsonbPair) * pstate->size);
560 object->val.object.pairs[object->val.object.nPairs].key = *string;
561 object->val.object.pairs[object->val.object.nPairs].order = object->val.object.nPairs;
565 * pushJsonbValue() worker: Append a pair value to state when generating a
569 appendValue(JsonbParseState *pstate, JsonbValue *scalarVal)
571 JsonbValue *object = &pstate->contVal;
573 Assert(object->type == jbvObject);
575 object->val.object.pairs[object->val.object.nPairs++].value = *scalarVal;
579 * pushJsonbValue() worker: Append an element to state when generating a Jsonb
582 appendElement(JsonbParseState *pstate, JsonbValue *scalarVal)
584 JsonbValue *array = &pstate->contVal;
586 Assert(array->type == jbvArray);
588 if (array->val.array.nElems >= JSONB_MAX_ELEMS)
590 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
591 errmsg("number of jsonb array elements exceeds the maximum allowed (%zu)",
594 if (array->val.array.nElems >= pstate->size)
597 array->val.array.elems = repalloc(array->val.array.elems,
598 sizeof(JsonbValue) * pstate->size);
601 array->val.array.elems[array->val.array.nElems++] = *scalarVal;
605 * Given a JsonbContainer, expand to JsonbIterator to iterate over items
606 * fully expanded to in-memory representation for manipulation.
608 * See JsonbIteratorNext() for notes on memory management.
611 JsonbIteratorInit(JsonbContainer *container)
613 return iteratorFromContainer(container, NULL);
617 * Get next JsonbValue while iterating
619 * Caller should initially pass their own, original iterator. They may get
620 * back a child iterator palloc()'d here instead. The function can be relied
621 * on to free those child iterators, lest the memory allocated for highly
622 * nested objects become unreasonable, but only if callers don't end iteration
623 * early (by breaking upon having found something in a search, for example).
625 * Callers in such a scenario, that are particularly sensitive to leaking
626 * memory in a long-lived context may walk the ancestral tree from the final
627 * iterator we left them with to its oldest ancestor, pfree()ing as they go.
628 * They do not have to free any other memory previously allocated for iterators
629 * but not accessible as direct ancestors of the iterator they're last passed
632 * Returns "Jsonb sequential processing" token value. Iterator "state"
633 * reflects the current stage of the process in a less granular fashion, and is
634 * mostly used here to track things internally with respect to particular
637 * Clients of this function should not have to handle any jbvBinary values
638 * (since recursive calls will deal with this), provided skipNested is false.
639 * It is our job to expand the jbvBinary representation without bothering them
640 * with it. However, clients should not take it upon themselves to touch array
641 * or Object element/pair buffers, since their element/pair pointers are
642 * garbage. Also, *val will not be set when returning WJB_END_ARRAY or
643 * WJB_END_OBJECT, on the assumption that it's only useful to access values
647 JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
653 * When stepping into a nested container, we jump back here to start
654 * processing the child. We will not recurse further in one call, because
655 * processing the child will always begin in JBI_ARRAY_START or
656 * JBI_OBJECT_START state.
659 switch ((*it)->state)
661 case JBI_ARRAY_START:
662 /* Set v to array on first array call */
663 val->type = jbvArray;
664 val->val.array.nElems = (*it)->nElems;
667 * v->val.array.elems is not actually set, because we aren't doing
670 val->val.array.rawScalar = (*it)->isScalar;
672 /* Set state for next call */
673 (*it)->state = JBI_ARRAY_ELEM;
674 return WJB_BEGIN_ARRAY;
677 if ((*it)->i >= (*it)->nElems)
680 * All elements within array already processed. Report this
681 * to caller, and give it back original parent iterator (which
682 * independently tracks iteration progress at its level of
685 *it = freeAndGetParent(*it);
686 return WJB_END_ARRAY;
689 fillJsonbValue((*it)->children, (*it)->i++, (*it)->dataProper, val);
691 if (!IsAJsonbScalar(val) && !skipNested)
693 /* Recurse into container. */
694 *it = iteratorFromContainer(val->val.binary.data, *it);
700 * Scalar item in array, or a container and caller didn't
701 * want us to recurse into it.
706 case JBI_OBJECT_START:
707 /* Set v to object on first object call */
708 val->type = jbvObject;
709 val->val.object.nPairs = (*it)->nElems;
712 * v->val.object.pairs is not actually set, because we aren't
713 * doing a full conversion
716 /* Set state for next call */
717 (*it)->state = JBI_OBJECT_KEY;
718 return WJB_BEGIN_OBJECT;
721 if ((*it)->i >= (*it)->nElems)
724 * All pairs within object already processed. Report this to
725 * caller, and give it back original containing iterator
726 * (which independently tracks iteration progress at its level
729 *it = freeAndGetParent(*it);
730 return WJB_END_OBJECT;
734 /* Return key of a key/value pair. */
735 fillJsonbValue((*it)->children, (*it)->i * 2, (*it)->dataProper, val);
736 if (val->type != jbvString)
737 elog(ERROR, "unexpected jsonb type as object key");
739 /* Set state for next call */
740 (*it)->state = JBI_OBJECT_VALUE;
744 case JBI_OBJECT_VALUE:
745 /* Set state for next call */
746 (*it)->state = JBI_OBJECT_KEY;
748 fillJsonbValue((*it)->children, ((*it)->i++) * 2 + 1,
749 (*it)->dataProper, val);
752 * Value may be a container, in which case we recurse with new,
753 * child iterator (unless the caller asked not to, by passing
756 if (!IsAJsonbScalar(val) && !skipNested)
758 *it = iteratorFromContainer(val->val.binary.data, *it);
765 elog(ERROR, "invalid iterator state");
770 * Initialize an iterator for iterating all elements in a container.
772 static JsonbIterator *
773 iteratorFromContainer(JsonbContainer *container, JsonbIterator *parent)
777 it = palloc(sizeof(JsonbIterator));
778 it->container = container;
780 it->nElems = container->header & JB_CMASK;
782 /* Array starts just after header */
783 it->children = container->children;
785 switch (container->header & (JB_FARRAY | JB_FOBJECT))
789 (char *) it->children + it->nElems * sizeof(JEntry);
790 it->isScalar = (container->header & JB_FSCALAR) != 0;
791 /* This is either a "raw scalar", or an array */
792 Assert(!it->isScalar || it->nElems == 1);
794 it->state = JBI_ARRAY_START;
800 * Offset reflects that nElems indicates JsonbPairs in an object.
801 * Each key and each value contain Jentry metadata just the same.
804 (char *) it->children + it->nElems * sizeof(JEntry) * 2;
805 it->state = JBI_OBJECT_START;
809 elog(ERROR, "unknown type of jsonb container");
816 * JsonbIteratorNext() worker: Return parent, while freeing memory for current
819 static JsonbIterator *
820 freeAndGetParent(JsonbIterator *it)
822 JsonbIterator *v = it->parent;
829 * Worker for "contains" operator's function
831 * Formally speaking, containment is top-down, unordered subtree isomorphism.
833 * Takes iterators that belong to some container type. These iterators
834 * "belong" to those values in the sense that they've just been initialized in
835 * respect of them by the caller (perhaps in a nested fashion).
837 * "val" is lhs Jsonb, and mContained is rhs Jsonb when called from top level.
838 * We determine if mContained is contained within val.
841 JsonbDeepContains(JsonbIterator **val, JsonbIterator **mContained)
849 * Guard against stack overflow due to overly complex Jsonb.
851 * Functions called here independently take this precaution, but that
852 * might not be sufficient since this is also a recursive function.
856 rval = JsonbIteratorNext(val, &vval, false);
857 rcont = JsonbIteratorNext(mContained, &vcontained, false);
862 * The differing return values can immediately be taken as indicating
863 * two differing container types at this nesting level, which is
864 * sufficient reason to give up entirely (but it should be the case
865 * that they're both some container type).
867 Assert(rval == WJB_BEGIN_OBJECT || rval == WJB_BEGIN_ARRAY);
868 Assert(rcont == WJB_BEGIN_OBJECT || rcont == WJB_BEGIN_ARRAY);
871 else if (rcont == WJB_BEGIN_OBJECT)
873 JsonbValue *lhsVal; /* lhsVal is from pair in lhs object */
875 Assert(vcontained.type == jbvObject);
877 /* Work through rhs "is it contained within?" object */
880 rcont = JsonbIteratorNext(mContained, &vcontained, false);
883 * When we get through caller's rhs "is it contained within?"
884 * object without failing to find one of its values, it's
887 if (rcont == WJB_END_OBJECT)
890 Assert(rcont == WJB_KEY);
892 /* First, find value by key... */
893 lhsVal = findJsonbValueFromContainer((*val)->container,
901 * ...at this stage it is apparent that there is at least a key
902 * match for this rhs pair.
904 rcont = JsonbIteratorNext(mContained, &vcontained, true);
906 Assert(rcont == WJB_VALUE);
909 * Compare rhs pair's value with lhs pair's value just found using
912 if (lhsVal->type != vcontained.type)
916 else if (IsAJsonbScalar(lhsVal))
918 if (!equalsJsonbScalarValue(lhsVal, &vcontained))
923 /* Nested container value (object or array) */
924 JsonbIterator *nestval,
927 Assert(lhsVal->type == jbvBinary);
928 Assert(vcontained.type == jbvBinary);
930 nestval = JsonbIteratorInit(lhsVal->val.binary.data);
931 nestContained = JsonbIteratorInit(vcontained.val.binary.data);
934 * Match "value" side of rhs datum object's pair recursively.
935 * It's a nested structure.
937 * Note that nesting still has to "match up" at the right
938 * nesting sub-levels. However, there need only be zero or
939 * more matching pairs (or elements) at each nesting level
940 * (provided the *rhs* pairs/elements *all* match on each
941 * level), which enables searching nested structures for a
942 * single String or other primitive type sub-datum quite
943 * effectively (provided the user constructed the rhs nested
944 * structure such that we "know where to look").
946 * In other words, the mapping of container nodes in the rhs
947 * "vcontained" Jsonb to internal nodes on the lhs is
948 * injective, and parent-child edges on the rhs must be mapped
949 * to parent-child edges on the lhs to satisfy the condition
950 * of containment (plus of course the mapped nodes must be
953 if (!JsonbDeepContains(&nestval, &nestContained))
958 else if (rcont == WJB_BEGIN_ARRAY)
960 JsonbValue *lhsConts = NULL;
961 uint32 nLhsElems = vval.val.array.nElems;
963 Assert(vcontained.type == jbvArray);
966 * Handle distinction between "raw scalar" pseudo arrays, and real
969 * A raw scalar may contain another raw scalar, and an array may
970 * contain a raw scalar, but a raw scalar may not contain an array. We
971 * don't do something like this for the object case, since objects can
972 * only contain pairs, never raw scalars (a pair is represented by an
973 * rhs object argument with a single contained pair).
975 if (vval.val.array.rawScalar && !vcontained.val.array.rawScalar)
978 /* Work through rhs "is it contained within?" array */
981 rcont = JsonbIteratorNext(mContained, &vcontained, true);
984 * When we get through caller's rhs "is it contained within?"
985 * array without failing to find one of its values, it's
988 if (rcont == WJB_END_ARRAY)
991 Assert(rcont == WJB_ELEM);
993 if (IsAJsonbScalar(&vcontained))
995 if (!findJsonbValueFromContainer((*val)->container,
1005 * If this is first container found in rhs array (at this
1006 * depth), initialize temp lhs array of containers
1008 if (lhsConts == NULL)
1012 /* Make room for all possible values */
1013 lhsConts = palloc(sizeof(JsonbValue) * nLhsElems);
1015 for (i = 0; i < nLhsElems; i++)
1017 /* Store all lhs elements in temp array */
1018 rcont = JsonbIteratorNext(val, &vval, true);
1019 Assert(rcont == WJB_ELEM);
1021 if (vval.type == jbvBinary)
1022 lhsConts[j++] = vval;
1025 /* No container elements in temp array, so give up now */
1029 /* We may have only partially filled array */
1033 /* XXX: Nested array containment is O(N^2) */
1034 for (i = 0; i < nLhsElems; i++)
1036 /* Nested container value (object or array) */
1037 JsonbIterator *nestval,
1041 nestval = JsonbIteratorInit(lhsConts[i].val.binary.data);
1042 nestContained = JsonbIteratorInit(vcontained.val.binary.data);
1044 contains = JsonbDeepContains(&nestval, &nestContained);
1049 pfree(nestContained);
1055 * Report rhs container value is not contained if couldn't
1056 * match rhs container to *some* lhs cont
1065 elog(ERROR, "invalid jsonb container type");
1068 elog(ERROR, "unexpectedly fell off end of jsonb container");
1073 * Hash a JsonbValue scalar value, mixing the hash value into an existing
1074 * hash provided by the caller.
1076 * Some callers may wish to independently XOR in JB_FOBJECT and JB_FARRAY
1080 JsonbHashScalarValue(const JsonbValue *scalarVal, uint32 *hash)
1084 /* Compute hash value for scalarVal */
1085 switch (scalarVal->type)
1091 tmp = DatumGetUInt32(hash_any((const unsigned char *) scalarVal->val.string.val,
1092 scalarVal->val.string.len));
1095 /* Must hash equal numerics to equal hash codes */
1096 tmp = DatumGetUInt32(DirectFunctionCall1(hash_numeric,
1097 NumericGetDatum(scalarVal->val.numeric)));
1100 tmp = scalarVal->val.boolean ? 0x02 : 0x04;
1103 elog(ERROR, "invalid jsonb scalar type");
1104 tmp = 0; /* keep compiler quiet */
1109 * Combine hash values of successive keys, values and elements by rotating
1110 * the previous value left 1 bit, then XOR'ing in the new
1111 * key/value/element's hash value.
1113 *hash = (*hash << 1) | (*hash >> 31);
1118 * Are two scalar JsonbValues of the same type a and b equal?
1121 equalsJsonbScalarValue(JsonbValue *aScalar, JsonbValue *bScalar)
1123 if (aScalar->type == bScalar->type)
1125 switch (aScalar->type)
1130 return lengthCompareJsonbStringValue(aScalar, bScalar) == 0;
1132 return DatumGetBool(DirectFunctionCall2(numeric_eq,
1133 PointerGetDatum(aScalar->val.numeric),
1134 PointerGetDatum(bScalar->val.numeric)));
1136 return aScalar->val.boolean == bScalar->val.boolean;
1139 elog(ERROR, "invalid jsonb scalar type");
1142 elog(ERROR, "jsonb scalar type mismatch");
1147 * Compare two scalar JsonbValues, returning -1, 0, or 1.
1149 * Strings are compared using the default collation. Used by B-tree
1150 * operators, where a lexical sort order is generally expected.
1153 compareJsonbScalarValue(JsonbValue *aScalar, JsonbValue *bScalar)
1155 if (aScalar->type == bScalar->type)
1157 switch (aScalar->type)
1162 return varstr_cmp(aScalar->val.string.val,
1163 aScalar->val.string.len,
1164 bScalar->val.string.val,
1165 bScalar->val.string.len,
1166 DEFAULT_COLLATION_OID);
1168 return DatumGetInt32(DirectFunctionCall2(numeric_cmp,
1169 PointerGetDatum(aScalar->val.numeric),
1170 PointerGetDatum(bScalar->val.numeric)));
1172 if (aScalar->val.boolean == bScalar->val.boolean)
1174 else if (aScalar->val.boolean > bScalar->val.boolean)
1179 elog(ERROR, "invalid jsonb scalar type");
1182 elog(ERROR, "jsonb scalar type mismatch");
1188 * Functions for manipulating the resizeable buffer used by convertJsonb and
1193 * Reserve 'len' bytes, at the end of the buffer, enlarging it if necessary.
1194 * Returns the offset to the reserved area. The caller is expected to fill
1195 * the reserved area later with copyToBuffer().
1198 reserveFromBuffer(StringInfo buffer, int len)
1202 /* Make more room if needed */
1203 enlargeStringInfo(buffer, len);
1205 /* remember current offset */
1206 offset = buffer->len;
1208 /* reserve the space */
1212 * Keep a trailing null in place, even though it's not useful for us;
1213 * it seems best to preserve the invariants of StringInfos.
1215 buffer->data[buffer->len] = '\0';
1221 * Copy 'len' bytes to a previously reserved area in buffer.
1224 copyToBuffer(StringInfo buffer, int offset, const char *data, int len)
1226 memcpy(buffer->data + offset, data, len);
1230 * A shorthand for reserveFromBuffer + copyToBuffer.
1233 appendToBuffer(StringInfo buffer, const char *data, int len)
1237 offset = reserveFromBuffer(buffer, len);
1238 copyToBuffer(buffer, offset, data, len);
1243 * Append padding, so that the length of the StringInfo is int-aligned.
1244 * Returns the number of padding bytes appended.
1247 padBufferToInt(StringInfo buffer)
1253 padlen = INTALIGN(buffer->len) - buffer->len;
1255 offset = reserveFromBuffer(buffer, padlen);
1257 /* padlen must be small, so this is probably faster than a memset */
1258 for (p = 0; p < padlen; p++)
1259 buffer->data[offset + p] = '\0';
1265 * Given a JsonbValue, convert to Jsonb. The result is palloc'd.
1268 convertToJsonb(JsonbValue *val)
1270 StringInfoData buffer;
1274 /* Should not already have binary representation */
1275 Assert(val->type != jbvBinary);
1277 /* Allocate an output buffer. It will be enlarged as needed */
1278 initStringInfo(&buffer);
1280 /* Make room for the varlena header */
1281 reserveFromBuffer(&buffer, sizeof(VARHDRSZ));
1283 convertJsonbValue(&buffer, &jentry, val, 0);
1286 * Note: the JEntry of the root is discarded. Therefore the root
1287 * JsonbContainer struct must contain enough information to tell what
1288 * kind of value it is.
1291 res = (Jsonb *) buffer.data;
1293 SET_VARSIZE(res, buffer.len);
1299 * Subroutine of convertJsonb: serialize a single JsonbValue into buffer.
1301 * The JEntry header for this node is returned in *header. It is filled in
1302 * with the length of this value, but if it is stored in an array or an
1303 * object (which is always, except for the root node), it is the caller's
1304 * responsibility to adjust it with the offset within the container.
1306 * If the value is an array or an object, this recurses. 'level' is only used
1307 * for debugging purposes.
1310 convertJsonbValue(StringInfo buffer, JEntry *header, JsonbValue *val, int level)
1312 check_stack_depth();
1317 if (IsAJsonbScalar(val) || val->type == jbvBinary)
1318 convertJsonbScalar(buffer, header, val);
1319 else if (val->type == jbvArray)
1320 convertJsonbArray(buffer, header, val, level);
1321 else if (val->type == jbvObject)
1322 convertJsonbObject(buffer, header, val, level);
1324 elog(ERROR, "unknown type of jsonb container");
1328 convertJsonbArray(StringInfo buffer, JEntry *pheader, JsonbValue *val, int level)
1336 /* Initialize pointer into conversion buffer at this level */
1337 offset = buffer->len;
1339 padBufferToInt(buffer);
1342 * Construct the header Jentry, stored in the beginning of the variable-
1345 header = val->val.array.nElems | JB_FARRAY;
1346 if (val->val.array.rawScalar)
1348 Assert(val->val.array.nElems == 1);
1350 header |= JB_FSCALAR;
1353 appendToBuffer(buffer, (char *) &header, sizeof(uint32));
1354 /* reserve space for the JEntries of the elements. */
1355 metaoffset = reserveFromBuffer(buffer, sizeof(JEntry) * val->val.array.nElems);
1358 for (i = 0; i < val->val.array.nElems; i++)
1360 JsonbValue *elem = &val->val.array.elems[i];
1364 convertJsonbValue(buffer, &meta, elem, level + 1);
1365 len = meta & JENTRY_POSMASK;
1368 if (totallen > JENTRY_POSMASK)
1370 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1371 errmsg("total size of jsonb array elements exceeds the maximum of %u bytes",
1375 meta = (meta & ~JENTRY_POSMASK) | totallen;
1376 copyToBuffer(buffer, metaoffset, (char *) &meta, sizeof(JEntry));
1377 metaoffset += sizeof(JEntry);
1380 totallen = buffer->len - offset;
1382 /* Initialize the header of this node, in the container's JEntry array */
1383 *pheader = JENTRY_ISCONTAINER | totallen;
1387 convertJsonbObject(StringInfo buffer, JEntry *pheader, JsonbValue *val, int level)
1395 /* Initialize pointer into conversion buffer at this level */
1396 offset = buffer->len;
1398 padBufferToInt(buffer);
1400 /* Initialize header */
1401 header = val->val.object.nPairs | JB_FOBJECT;
1402 appendToBuffer(buffer, (char *) &header, sizeof(uint32));
1404 /* reserve space for the JEntries of the keys and values */
1405 metaoffset = reserveFromBuffer(buffer, sizeof(JEntry) * val->val.object.nPairs * 2);
1408 for (i = 0; i < val->val.object.nPairs; i++)
1410 JsonbPair *pair = &val->val.object.pairs[i];
1415 convertJsonbScalar(buffer, &meta, &pair->key);
1417 len = meta & JENTRY_POSMASK;
1420 if (totallen > JENTRY_POSMASK)
1422 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1423 errmsg("total size of jsonb array elements exceeds the maximum of %u bytes",
1427 meta = (meta & ~JENTRY_POSMASK) | totallen;
1428 copyToBuffer(buffer, metaoffset, (char *) &meta, sizeof(JEntry));
1429 metaoffset += sizeof(JEntry);
1431 convertJsonbValue(buffer, &meta, &pair->value, level);
1432 len = meta & JENTRY_POSMASK;
1435 if (totallen > JENTRY_POSMASK)
1437 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1438 errmsg("total size of jsonb array elements exceeds the maximum of %u bytes",
1441 meta = (meta & ~JENTRY_POSMASK) | totallen;
1442 copyToBuffer(buffer, metaoffset, (char *) &meta, sizeof(JEntry));
1443 metaoffset += sizeof(JEntry);
1446 totallen = buffer->len - offset;
1448 *pheader = JENTRY_ISCONTAINER | totallen;
1452 convertJsonbScalar(StringInfo buffer, JEntry *jentry, JsonbValue *scalarVal)
1457 switch (scalarVal->type)
1460 *jentry = JENTRY_ISNULL;
1464 appendToBuffer(buffer, scalarVal->val.string.val, scalarVal->val.string.len);
1466 *jentry = scalarVal->val.string.len;
1470 numlen = VARSIZE_ANY(scalarVal->val.numeric);
1471 padlen = padBufferToInt(buffer);
1473 appendToBuffer(buffer, (char *) scalarVal->val.numeric, numlen);
1475 *jentry = JENTRY_ISNUMERIC | (padlen + numlen);
1479 *jentry = (scalarVal->val.boolean) ?
1480 JENTRY_ISBOOL_TRUE : JENTRY_ISBOOL_FALSE;
1484 elog(ERROR, "invalid jsonb scalar type");
1489 * Compare two jbvString JsonbValue values, a and b.
1491 * This is a special qsort() comparator used to sort strings in certain
1492 * internal contexts where it is sufficient to have a well-defined sort order.
1493 * In particular, object pair keys are sorted according to this criteria to
1494 * facilitate cheap binary searches where we don't care about lexical sort
1497 * a and b are first sorted based on their length. If a tie-breaker is
1498 * required, only then do we consider string binary equality.
1501 lengthCompareJsonbStringValue(const void *a, const void *b)
1503 const JsonbValue *va = (const JsonbValue *) a;
1504 const JsonbValue *vb = (const JsonbValue *) b;
1507 Assert(va->type == jbvString);
1508 Assert(vb->type == jbvString);
1510 if (va->val.string.len == vb->val.string.len)
1512 res = memcmp(va->val.string.val, vb->val.string.val, va->val.string.len);
1516 res = (va->val.string.len > vb->val.string.len) ? 1 : -1;
1523 * qsort_arg() comparator to compare JsonbPair values.
1525 * Third argument 'binequal' may point to a bool. If it's set, *binequal is set
1526 * to true iff a and b have full binary equality, since some callers have an
1527 * interest in whether the two values are equal or merely equivalent.
1529 * N.B: String comparisons here are "length-wise"
1531 * Pairs with equals keys are ordered such that the order field is respected.
1534 lengthCompareJsonbPair(const void *a, const void *b, void *binequal)
1536 const JsonbPair *pa = (const JsonbPair *) a;
1537 const JsonbPair *pb = (const JsonbPair *) b;
1540 res = lengthCompareJsonbStringValue(&pa->key, &pb->key);
1541 if (res == 0 && binequal)
1542 *((bool *) binequal) = true;
1545 * Guarantee keeping order of equal pair. Unique algorithm will prefer
1546 * first element as value.
1549 res = (pa->order > pb->order) ? -1 : 1;
1555 * Sort and unique-ify pairs in JsonbValue object
1558 uniqueifyJsonbObject(JsonbValue *object)
1560 bool hasNonUniq = false;
1562 Assert(object->type == jbvObject);
1564 if (object->val.object.nPairs > 1)
1565 qsort_arg(object->val.object.pairs, object->val.object.nPairs, sizeof(JsonbPair),
1566 lengthCompareJsonbPair, &hasNonUniq);
1570 JsonbPair *ptr = object->val.object.pairs + 1,
1571 *res = object->val.object.pairs;
1573 while (ptr - object->val.object.pairs < object->val.object.nPairs)
1575 /* Avoid copying over duplicate */
1576 if (lengthCompareJsonbStringValue(ptr, res) != 0)
1580 memcpy(res, ptr, sizeof(JsonbPair));
1585 object->val.object.nPairs = res + 1 - object->val.object.pairs;