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 int compareJsonbScalarValue(JsonbValue *a, JsonbValue *b);
38 static int lexicalCompareJsonbStringValue(const void *a, const void *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. formIterIsContainer() does this, so
66 * that clients of the iteration code don't have to directly deal with the
67 * binary representation (JsonbDeepContains() is a notable exception, although
68 * all exceptions are internal to this module). In general, functions that
69 * accept a JsonbValue argument are concerned with the manipulation of scalar
70 * values, or simple containers of scalar values, where it would be
71 * inconvenient to 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);
141 * To a limited extent we'll redundantly iterate over an array/object
142 * while re-performing the same test without any reasonable
143 * expectation of the same container types having differing lengths
144 * (as when we process a WJB_BEGIN_OBJECT, and later the corresponding
145 * WJB_END_OBJECT), but no matter.
151 /* Decisively equal */
155 if (va.type == vb.type)
160 res = lexicalCompareJsonbStringValue(&va, &vb);
165 res = compareJsonbScalarValue(&va, &vb);
170 * This could be a "raw scalar" pseudo array. That's
171 * a special case here though, since we still want the
172 * general type-based comparisons to apply, and as far
173 * as we're concerned a pseudo array is just a scalar.
175 if (va.val.array.rawScalar != vb.val.array.rawScalar)
176 res = (va.val.array.rawScalar) ? -1 : 1;
177 if (va.val.array.nElems != vb.val.array.nElems)
178 res = (va.val.array.nElems > vb.val.array.nElems) ? 1 : -1;
181 if (va.val.object.nPairs != vb.val.object.nPairs)
182 res = (va.val.object.nPairs > vb.val.object.nPairs) ? 1 : -1;
185 elog(ERROR, "unexpected jbvBinary value");
190 /* Type-defined order */
191 res = (va.type > vb.type) ? 1 : -1;
197 * It's safe to assume that the types differed.
199 * If the two values were the same container type, then there'd
200 * have been a chance to observe the variation in the number of
201 * elements/pairs (when processing WJB_BEGIN_OBJECT, say). They
202 * can't be scalar types either, because then they'd have to be
203 * contained in containers already ruled unequal due to differing
204 * numbers of pairs/elements, or already directly ruled unequal
205 * with a call to the underlying type's comparator.
207 Assert(va.type != vb.type);
208 Assert(va.type == jbvArray || va.type == jbvObject);
209 Assert(vb.type == jbvArray || vb.type == jbvObject);
210 /* Type-defined order */
211 res = (va.type > vb.type) ? 1 : -1;
218 JsonbIterator *i = ita->parent;
225 JsonbIterator *i = itb->parent;
235 * Find value in object (i.e. the "value" part of some key/value pair in an
236 * object), or find a matching element if we're looking through an array. Do
237 * so on the basis of equality of the object keys only, or alternatively
238 * element values only, with a caller-supplied value "key". The "flags"
239 * argument allows the caller to specify which container types are of interest.
241 * This exported utility function exists to facilitate various cases concerned
242 * with "containment". If asked to look through an object, the caller had
243 * better pass a Jsonb String, because their keys can only be strings.
244 * Otherwise, for an array, any type of JsonbValue will do.
246 * In order to proceed with the search, it is necessary for callers to have
247 * both specified an interest in exactly one particular container type with an
248 * appropriate flag, as well as having the pointed-to Jsonb container be of
249 * one of those same container types at the top level. (Actually, we just do
250 * whichever makes sense to save callers the trouble of figuring it out - at
251 * most one can make sense, because the container either points to an array
252 * (possibly a "raw scalar" pseudo array) or an object.)
254 * Note that we can return a jbvBinary JsonbValue if this is called on an
255 * object, but we never do so on an array. If the caller asks to look through
256 * a container type that is not of the type pointed to by the container,
257 * immediately fall through and return NULL. If we cannot find the value,
258 * return NULL. Otherwise, return palloc()'d copy of value.
261 findJsonbValueFromContainer(JsonbContainer *container, uint32 flags,
264 JEntry *children = container->children;
265 int count = (container->header & JB_CMASK);
266 JsonbValue *result = palloc(sizeof(JsonbValue));
268 Assert((flags & ~(JB_FARRAY | JB_FOBJECT)) == 0);
270 if (flags & JB_FARRAY & container->header)
272 char *base_addr = (char *) (children + count);
275 for (i = 0; i < count; i++)
277 fillJsonbValue(children, i, base_addr, result);
279 if (key->type == result->type)
281 if (compareJsonbScalarValue(key, result) == 0)
286 else if (flags & JB_FOBJECT & container->header)
288 /* Since this is an object, account for *Pairs* of Jentrys */
289 char *base_addr = (char *) (children + count * 2);
293 /* Object key past by caller must be a string */
294 Assert(key->type == jbvString);
296 /* Binary search on object/pair keys *only* */
297 while (stopLow < count)
301 JsonbValue candidate;
304 * Note how we compensate for the fact that we're iterating
305 * through pairs (not entries) throughout.
307 stopMiddle = stopLow + (count - stopLow) / 2;
309 index = stopMiddle * 2;
311 candidate.type = jbvString;
312 candidate.val.string.val = base_addr + JBE_OFF(children, index);
313 candidate.val.string.len = JBE_LEN(children, index);
315 difference = lengthCompareJsonbStringValue(&candidate, key);
319 /* Found our key, return value */
320 fillJsonbValue(children, index + 1, base_addr, result);
327 stopLow = stopMiddle + 1;
340 * Get i-th value of a Jsonb array.
342 * Returns palloc()'d copy of the value, or NULL if it does not exist.
345 getIthJsonbValueFromContainer(JsonbContainer *container, uint32 i)
351 if ((container->header & JB_FARRAY) == 0)
352 elog(ERROR, "not a jsonb array");
354 nelements = container->header & JB_CMASK;
355 base_addr = (char *) &container->children[nelements];
360 result = palloc(sizeof(JsonbValue));
362 fillJsonbValue(container->children, i, base_addr, result);
368 * A helper function to fill in a JsonbValue to represent an element of an
369 * array, or a key or value of an object.
371 * A nested array or object will be returned as jbvBinary, ie. it won't be
375 fillJsonbValue(JEntry *children, int index, char *base_addr, JsonbValue *result)
377 JEntry entry = children[index];
379 if (JBE_ISNULL(entry))
381 result->type = jbvNull;
383 else if (JBE_ISSTRING(entry))
385 result->type = jbvString;
386 result->val.string.val = base_addr + JBE_OFF(children, index);
387 result->val.string.len = JBE_LEN(children, index);
388 Assert(result->val.string.len >= 0);
390 else if (JBE_ISNUMERIC(entry))
392 result->type = jbvNumeric;
393 result->val.numeric = (Numeric) (base_addr + INTALIGN(JBE_OFF(children, index)));
395 else if (JBE_ISBOOL_TRUE(entry))
397 result->type = jbvBool;
398 result->val.boolean = true;
400 else if (JBE_ISBOOL_FALSE(entry))
402 result->type = jbvBool;
403 result->val.boolean = false;
407 Assert(JBE_ISCONTAINER(entry));
408 result->type = jbvBinary;
409 result->val.binary.data = (JsonbContainer *) (base_addr + INTALIGN(JBE_OFF(children, index)));
410 result->val.binary.len = JBE_LEN(children, index) - (INTALIGN(JBE_OFF(children, index)) - JBE_OFF(children, index));
415 * Push JsonbValue into JsonbParseState.
417 * Used when parsing JSON tokens to form Jsonb, or when converting an in-memory
418 * JsonbValue to a Jsonb.
420 * Initial state of *JsonbParseState is NULL, since it'll be allocated here
421 * originally (caller will get JsonbParseState back by reference).
423 * Only sequential tokens pertaining to non-container types should pass a
424 * JsonbValue. There is one exception -- WJB_BEGIN_ARRAY callers may pass a
425 * "raw scalar" pseudo array to append that.
428 pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq,
429 JsonbValue *scalarVal)
431 JsonbValue *result = NULL;
435 case WJB_BEGIN_ARRAY:
436 Assert(!scalarVal || scalarVal->val.array.rawScalar);
437 *pstate = pushState(pstate);
438 result = &(*pstate)->contVal;
439 (*pstate)->contVal.type = jbvArray;
440 (*pstate)->contVal.val.array.nElems = 0;
441 (*pstate)->contVal.val.array.rawScalar = (scalarVal &&
442 scalarVal->val.array.rawScalar);
443 if (scalarVal && scalarVal->val.array.nElems > 0)
445 /* Assume that this array is still really a scalar */
446 Assert(scalarVal->type == jbvArray);
447 (*pstate)->size = scalarVal->val.array.nElems;
453 (*pstate)->contVal.val.array.elems = palloc(sizeof(JsonbValue) *
456 case WJB_BEGIN_OBJECT:
458 *pstate = pushState(pstate);
459 result = &(*pstate)->contVal;
460 (*pstate)->contVal.type = jbvObject;
461 (*pstate)->contVal.val.object.nPairs = 0;
463 (*pstate)->contVal.val.object.pairs = palloc(sizeof(JsonbPair) *
467 Assert(scalarVal->type == jbvString);
468 appendKey(*pstate, scalarVal);
471 Assert(IsAJsonbScalar(scalarVal) ||
472 scalarVal->type == jbvBinary);
473 appendValue(*pstate, scalarVal);
476 Assert(IsAJsonbScalar(scalarVal) ||
477 scalarVal->type == jbvBinary);
478 appendElement(*pstate, scalarVal);
481 uniqueifyJsonbObject(&(*pstate)->contVal);
484 /* Steps here common to WJB_END_OBJECT case */
486 result = &(*pstate)->contVal;
489 * Pop stack and push current array/object as value in parent
492 *pstate = (*pstate)->next;
495 switch ((*pstate)->contVal.type)
498 appendElement(*pstate, result);
501 appendValue(*pstate, result);
504 elog(ERROR, "invalid jsonb container type");
509 elog(ERROR, "unrecognized jsonb sequential processing token");
516 * pushJsonbValue() worker: Iteration-like forming of Jsonb
518 static JsonbParseState *
519 pushState(JsonbParseState **pstate)
521 JsonbParseState *ns = palloc(sizeof(JsonbParseState));
528 * pushJsonbValue() worker: Append a pair key to state when generating a Jsonb
531 appendKey(JsonbParseState *pstate, JsonbValue *string)
533 JsonbValue *object = &pstate->contVal;
535 Assert(object->type == jbvObject);
536 Assert(string->type == jbvString);
538 if (object->val.object.nPairs >= JSONB_MAX_PAIRS)
540 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
541 errmsg("number of jsonb object pairs exceeds the maximum allowed (%zu)",
544 if (object->val.object.nPairs >= pstate->size)
547 object->val.object.pairs = repalloc(object->val.object.pairs,
548 sizeof(JsonbPair) * pstate->size);
551 object->val.object.pairs[object->val.object.nPairs].key = *string;
552 object->val.object.pairs[object->val.object.nPairs].order = object->val.object.nPairs;
556 * pushJsonbValue() worker: Append a pair value to state when generating a
560 appendValue(JsonbParseState *pstate, JsonbValue *scalarVal)
562 JsonbValue *object = &pstate->contVal;
564 Assert(object->type == jbvObject);
566 object->val.object.pairs[object->val.object.nPairs++].value = *scalarVal;
570 * pushJsonbValue() worker: Append an element to state when generating a Jsonb
573 appendElement(JsonbParseState *pstate, JsonbValue *scalarVal)
575 JsonbValue *array = &pstate->contVal;
577 Assert(array->type == jbvArray);
579 if (array->val.array.nElems >= JSONB_MAX_ELEMS)
581 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
582 errmsg("number of jsonb array elements exceeds the maximum allowed (%zu)",
585 if (array->val.array.nElems >= pstate->size)
588 array->val.array.elems = repalloc(array->val.array.elems,
589 sizeof(JsonbValue) * pstate->size);
592 array->val.array.elems[array->val.array.nElems++] = *scalarVal;
596 * Given a JsonbContainer, expand to JsonbIterator to iterate over items
597 * fully expanded to in-memory representation for manipulation.
599 * See JsonbIteratorNext() for notes on memory management.
602 JsonbIteratorInit(JsonbContainer *container)
604 return iteratorFromContainer(container, NULL);
608 * Get next JsonbValue while iterating
610 * Caller should initially pass their own, original iterator. They may get
611 * back a child iterator palloc()'d here instead. The function can be relied
612 * on to free those child iterators, lest the memory allocated for highly
613 * nested objects become unreasonable, but only if callers don't end iteration
614 * early (by breaking upon having found something in a search, for example).
616 * Callers in such a scenario, that are particularly sensitive to leaking
617 * memory in a long-lived context may walk the ancestral tree from the final
618 * iterator we left them with to its oldest ancestor, pfree()ing as they go.
619 * They do not have to free any other memory previously allocated for iterators
620 * but not accessible as direct ancestors of the iterator they're last passed
623 * Returns "Jsonb sequential processing" token value. Iterator "state"
624 * reflects the current stage of the process in a less granular fashion, and is
625 * mostly used here to track things internally with respect to particular
628 * Clients of this function should not have to handle any jbvBinary values
629 * (since recursive calls will deal with this), provided skipNested is false.
630 * It is our job to expand the jbvBinary representation without bothering them
631 * with it. However, clients should not take it upon themselves to touch array
632 * or Object element/pair buffers, since their element/pair pointers are
636 JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
642 * When stepping into a nested container, we jump back here to start
643 * processing the child. We will not recurse further in one call, because
644 * processing the child will always begin in JBI_ARRAY_START or
645 * JBI_OBJECT_START state.
648 switch ((*it)->state)
650 case JBI_ARRAY_START:
651 /* Set v to array on first array call */
652 val->type = jbvArray;
653 val->val.array.nElems = (*it)->nElems;
656 * v->val.array.elems is not actually set, because we aren't doing
659 val->val.array.rawScalar = (*it)->isScalar;
661 /* Set state for next call */
662 (*it)->state = JBI_ARRAY_ELEM;
663 return WJB_BEGIN_ARRAY;
666 if ((*it)->i >= (*it)->nElems)
669 * All elements within array already processed. Report this
670 * to caller, and give it back original parent iterator (which
671 * independently tracks iteration progress at its level of
674 *it = freeAndGetParent(*it);
675 return WJB_END_ARRAY;
678 fillJsonbValue((*it)->children, (*it)->i++, (*it)->dataProper, val);
680 if (!IsAJsonbScalar(val) && !skipNested)
682 /* Recurse into container. */
683 *it = iteratorFromContainer(val->val.binary.data, *it);
689 * Scalar item in array (or a container and caller didn't
690 * want us to recurse into it.
695 case JBI_OBJECT_START:
696 /* Set v to object on first object call */
697 val->type = jbvObject;
698 val->val.object.nPairs = (*it)->nElems;
701 * v->val.object.pairs is not actually set, because we aren't
702 * doing a full conversion
705 /* Set state for next call */
706 (*it)->state = JBI_OBJECT_KEY;
707 return WJB_BEGIN_OBJECT;
710 if ((*it)->i >= (*it)->nElems)
713 * All pairs within object already processed. Report this to
714 * caller, and give it back original containing iterator
715 * (which independently tracks iteration progress at its level
718 *it = freeAndGetParent(*it);
719 return WJB_END_OBJECT;
723 /* Return key of a key/value pair. */
724 fillJsonbValue((*it)->children, (*it)->i * 2, (*it)->dataProper, val);
725 if (val->type != jbvString)
726 elog(ERROR, "unexpected jsonb type as object key");
728 /* Set state for next call */
729 (*it)->state = JBI_OBJECT_VALUE;
733 case JBI_OBJECT_VALUE:
734 /* Set state for next call */
735 (*it)->state = JBI_OBJECT_KEY;
737 fillJsonbValue((*it)->children, ((*it)->i++) * 2 + 1,
738 (*it)->dataProper, val);
741 * Value may be a container, in which case we recurse with new,
742 * child iterator (unless the caller asked not to, by passing
745 if (!IsAJsonbScalar(val) && !skipNested)
747 *it = iteratorFromContainer(val->val.binary.data, *it);
754 elog(ERROR, "invalid iterator state");
759 * Initialize an iterator for iterating all elements in a container.
761 static JsonbIterator *
762 iteratorFromContainer(JsonbContainer *container, JsonbIterator *parent)
766 it = palloc(sizeof(JsonbIterator));
767 it->container = container;
769 it->nElems = container->header & JB_CMASK;
771 /* Array starts just after header */
772 it->children = container->children;
774 switch (container->header & (JB_FARRAY | JB_FOBJECT))
778 (char *) it->children + it->nElems * sizeof(JEntry);
779 it->isScalar = (container->header & JB_FSCALAR) != 0;
780 /* This is either a "raw scalar", or an array */
781 Assert(!it->isScalar || it->nElems == 1);
783 it->state = JBI_ARRAY_START;
789 * Offset reflects that nElems indicates JsonbPairs in an object.
790 * Each key and each value contain Jentry metadata just the same.
793 (char *) it->children + it->nElems * sizeof(JEntry) * 2;
794 it->state = JBI_OBJECT_START;
798 elog(ERROR, "unknown type of jsonb container");
805 * JsonbIteratorNext() worker: Return parent, while freeing memory for current
808 static JsonbIterator *
809 freeAndGetParent(JsonbIterator *it)
811 JsonbIterator *v = it->parent;
818 * Worker for "contains" operator's function
820 * Formally speaking, containment is top-down, unordered subtree isomorphism.
822 * Takes iterators that belong to some container type. These iterators
823 * "belong" to those values in the sense that they've just been initialized in
824 * respect of them by the caller (perhaps in a nested fashion).
826 * "val" is lhs Jsonb, and mContained is rhs Jsonb when called from top level.
827 * We determine if mContained is contained within val.
830 JsonbDeepContains(JsonbIterator **val, JsonbIterator **mContained)
838 * Guard against stack overflow due to overly complex Jsonb.
840 * Functions called here independently take this precaution, but that
841 * might not be sufficient since this is also a recursive function.
845 rval = JsonbIteratorNext(val, &vval, false);
846 rcont = JsonbIteratorNext(mContained, &vcontained, false);
851 * The differing return values can immediately be taken as indicating
852 * two differing container types at this nesting level, which is
853 * sufficient reason to give up entirely (but it should be the case
854 * that they're both some container type).
856 Assert(rval == WJB_BEGIN_OBJECT || rval == WJB_BEGIN_ARRAY);
857 Assert(rcont == WJB_BEGIN_OBJECT || rcont == WJB_BEGIN_ARRAY);
860 else if (rcont == WJB_BEGIN_OBJECT)
862 JsonbValue *lhsVal; /* lhsVal is from pair in lhs object */
864 Assert(vcontained.type == jbvObject);
866 /* Work through rhs "is it contained within?" object */
869 rcont = JsonbIteratorNext(mContained, &vcontained, false);
872 * When we get through caller's rhs "is it contained within?"
873 * object without failing to find one of its values, it's
876 if (rcont == WJB_END_OBJECT)
879 Assert(rcont == WJB_KEY);
881 /* First, find value by key... */
882 lhsVal = findJsonbValueFromContainer((*val)->container,
890 * ...at this stage it is apparent that there is at least a key
891 * match for this rhs pair.
893 rcont = JsonbIteratorNext(mContained, &vcontained, true);
895 Assert(rcont == WJB_VALUE);
898 * Compare rhs pair's value with lhs pair's value just found using
901 if (lhsVal->type != vcontained.type)
905 else if (IsAJsonbScalar(lhsVal))
907 if (compareJsonbScalarValue(lhsVal, &vcontained) != 0)
912 /* Nested container value (object or array) */
913 JsonbIterator *nestval,
916 Assert(lhsVal->type == jbvBinary);
917 Assert(vcontained.type == jbvBinary);
919 nestval = JsonbIteratorInit(lhsVal->val.binary.data);
920 nestContained = JsonbIteratorInit(vcontained.val.binary.data);
923 * Match "value" side of rhs datum object's pair recursively.
924 * It's a nested structure.
926 * Note that nesting still has to "match up" at the right
927 * nesting sub-levels. However, there need only be zero or
928 * more matching pairs (or elements) at each nesting level
929 * (provided the *rhs* pairs/elements *all* match on each
930 * level), which enables searching nested structures for a
931 * single String or other primitive type sub-datum quite
932 * effectively (provided the user constructed the rhs nested
933 * structure such that we "know where to look").
935 * In other words, the mapping of container nodes in the rhs
936 * "vcontained" Jsonb to internal nodes on the lhs is
937 * injective, and parent-child edges on the rhs must be mapped
938 * to parent-child edges on the lhs to satisfy the condition
939 * of containment (plus of course the mapped nodes must be
942 if (!JsonbDeepContains(&nestval, &nestContained))
947 else if (rcont == WJB_BEGIN_ARRAY)
949 JsonbValue *lhsConts = NULL;
950 uint32 nLhsElems = vval.val.array.nElems;
952 Assert(vcontained.type == jbvArray);
955 * Handle distinction between "raw scalar" pseudo arrays, and real
958 * A raw scalar may contain another raw scalar, and an array may
959 * contain a raw scalar, but a raw scalar may not contain an array. We
960 * don't do something like this for the object case, since objects can
961 * only contain pairs, never raw scalars (a pair is represented by an
962 * rhs object argument with a single contained pair).
964 if (vval.val.array.rawScalar && !vcontained.val.array.rawScalar)
967 /* Work through rhs "is it contained within?" array */
970 rcont = JsonbIteratorNext(mContained, &vcontained, true);
973 * When we get through caller's rhs "is it contained within?"
974 * array without failing to find one of its values, it's
977 if (rcont == WJB_END_ARRAY)
980 Assert(rcont == WJB_ELEM);
982 if (IsAJsonbScalar(&vcontained))
984 if (!findJsonbValueFromContainer((*val)->container,
994 * If this is first container found in rhs array (at this
995 * depth), initialize temp lhs array of containers
997 if (lhsConts == NULL)
1001 /* Make room for all possible values */
1002 lhsConts = palloc(sizeof(JsonbValue) * nLhsElems);
1004 for (i = 0; i < nLhsElems; i++)
1006 /* Store all lhs elements in temp array */
1007 rcont = JsonbIteratorNext(val, &vval, true);
1008 Assert(rcont == WJB_ELEM);
1010 if (vval.type == jbvBinary)
1011 lhsConts[j++] = vval;
1014 /* No container elements in temp array, so give up now */
1018 /* We may have only partially filled array */
1022 /* XXX: Nested array containment is O(N^2) */
1023 for (i = 0; i < nLhsElems; i++)
1025 /* Nested container value (object or array) */
1026 JsonbIterator *nestval,
1030 nestval = JsonbIteratorInit(lhsConts[i].val.binary.data);
1031 nestContained = JsonbIteratorInit(vcontained.val.binary.data);
1033 contains = JsonbDeepContains(&nestval, &nestContained);
1038 pfree(nestContained);
1044 * Report rhs container value is not contained if couldn't
1045 * match rhs container to *some* lhs cont
1054 elog(ERROR, "invalid jsonb container type");
1057 elog(ERROR, "unexpectedly fell off end of jsonb container");
1062 * Hash a JsonbValue scalar value, mixing the hash value into an existing
1063 * hash provided by the caller.
1065 * Some callers may wish to independently XOR in JB_FOBJECT and JB_FARRAY
1069 JsonbHashScalarValue(const JsonbValue *scalarVal, uint32 *hash)
1073 /* Compute hash value for scalarVal */
1074 switch (scalarVal->type)
1080 tmp = DatumGetUInt32(hash_any((const unsigned char *) scalarVal->val.string.val,
1081 scalarVal->val.string.len));
1084 /* Must hash equal numerics to equal hash codes */
1085 tmp = DatumGetUInt32(DirectFunctionCall1(hash_numeric,
1086 NumericGetDatum(scalarVal->val.numeric)));
1089 tmp = scalarVal->val.boolean ? 0x02 : 0x04;
1092 elog(ERROR, "invalid jsonb scalar type");
1093 tmp = 0; /* keep compiler quiet */
1098 * Combine hash values of successive keys, values and elements by rotating
1099 * the previous value left 1 bit, then XOR'ing in the new
1100 * key/value/element's hash value.
1102 *hash = (*hash << 1) | (*hash >> 31);
1107 * Are two scalar JsonbValues of the same type a and b equal?
1109 * Does not use lexical comparisons. Therefore, it is essentially that this
1110 * never be used against Strings for anything other than searching for values
1111 * within a single jsonb.
1114 compareJsonbScalarValue(JsonbValue *aScalar, JsonbValue *bScalar)
1116 if (aScalar->type == bScalar->type)
1118 switch (aScalar->type)
1123 return lengthCompareJsonbStringValue(aScalar, bScalar);
1125 return DatumGetInt32(DirectFunctionCall2(numeric_cmp,
1126 PointerGetDatum(aScalar->val.numeric),
1127 PointerGetDatum(bScalar->val.numeric)));
1129 if (aScalar->val.boolean != bScalar->val.boolean)
1130 return (aScalar->val.boolean > bScalar->val.boolean) ? 1 : -1;
1134 elog(ERROR, "invalid jsonb scalar type");
1137 elog(ERROR, "jsonb scalar type mismatch");
1142 * Standard lexical qsort() comparator of jsonb strings.
1144 * Sorts strings lexically, using the default database collation. Used by
1145 * B-Tree operators, where a lexical sort order is generally expected.
1148 lexicalCompareJsonbStringValue(const void *a, const void *b)
1150 const JsonbValue *va = (const JsonbValue *) a;
1151 const JsonbValue *vb = (const JsonbValue *) b;
1153 Assert(va->type == jbvString);
1154 Assert(vb->type == jbvString);
1156 return varstr_cmp(va->val.string.val, va->val.string.len, vb->val.string.val,
1157 vb->val.string.len, DEFAULT_COLLATION_OID);
1162 * Functions for manipulating the resizeable buffer used by convertJsonb and
1167 * Reserve 'len' bytes, at the end of the buffer, enlarging it if necessary.
1168 * Returns the offset to the reserved area. The caller is expected to fill
1169 * the reserved area later with copyToBuffer().
1172 reserveFromBuffer(StringInfo buffer, int len)
1176 /* Make more room if needed */
1177 enlargeStringInfo(buffer, len);
1179 /* remember current offset */
1180 offset = buffer->len;
1182 /* reserve the space */
1186 * Keep a trailing null in place, even though it's not useful for us;
1187 * it seems best to preserve the invariants of StringInfos.
1189 buffer->data[buffer->len] = '\0';
1195 * Copy 'len' bytes to a previously reserved area in buffer.
1198 copyToBuffer(StringInfo buffer, int offset, const char *data, int len)
1200 memcpy(buffer->data + offset, data, len);
1204 * A shorthand for reserveFromBuffer + copyToBuffer.
1207 appendToBuffer(StringInfo buffer, const char *data, int len)
1211 offset = reserveFromBuffer(buffer, len);
1212 copyToBuffer(buffer, offset, data, len);
1217 * Append padding, so that the length of the StringInfo is int-aligned.
1218 * Returns the number of padding bytes appended.
1221 padBufferToInt(StringInfo buffer)
1227 padlen = INTALIGN(buffer->len) - buffer->len;
1229 offset = reserveFromBuffer(buffer, padlen);
1231 /* padlen must be small, so this is probably faster than a memset */
1232 for (p = 0; p < padlen; p++)
1233 buffer->data[offset + p] = '\0';
1239 * Given a JsonbValue, convert to Jsonb. The result is palloc'd.
1242 convertToJsonb(JsonbValue *val)
1244 StringInfoData buffer;
1248 /* Should not already have binary representation */
1249 Assert(val->type != jbvBinary);
1251 /* Allocate an output buffer. It will be enlarged as needed */
1252 initStringInfo(&buffer);
1254 /* Make room for the varlena header */
1255 reserveFromBuffer(&buffer, sizeof(VARHDRSZ));
1257 convertJsonbValue(&buffer, &jentry, val, 0);
1260 * Note: the JEntry of the root is discarded. Therefore the root
1261 * JsonbContainer struct must contain enough information to tell what
1262 * kind of value it is.
1265 res = (Jsonb *) buffer.data;
1267 SET_VARSIZE(res, buffer.len);
1273 * Subroutine of convertJsonb: serialize a single JsonbValue into buffer.
1275 * The JEntry header for this node is returned in *header. It is filled in
1276 * with the length of this value, but if it is stored in an array or an
1277 * object (which is always, except for the root node), it is the caller's
1278 * responsibility to adjust it with the offset within the container.
1280 * If the value is an array or an object, this recurses. 'level' is only used
1281 * for debugging purposes.
1284 convertJsonbValue(StringInfo buffer, JEntry *header, JsonbValue *val, int level)
1286 check_stack_depth();
1291 if (IsAJsonbScalar(val) || val->type == jbvBinary)
1292 convertJsonbScalar(buffer, header, val);
1293 else if (val->type == jbvArray)
1294 convertJsonbArray(buffer, header, val, level);
1295 else if (val->type == jbvObject)
1296 convertJsonbObject(buffer, header, val, level);
1298 elog(ERROR, "unknown type of jsonb container");
1302 convertJsonbArray(StringInfo buffer, JEntry *pheader, JsonbValue *val, int level)
1310 /* Initialize pointer into conversion buffer at this level */
1311 offset = buffer->len;
1313 padBufferToInt(buffer);
1316 * Construct the header Jentry, stored in the beginning of the variable-
1319 header = val->val.array.nElems | JB_FARRAY;
1320 if (val->val.array.rawScalar)
1322 Assert(val->val.array.nElems == 1);
1324 header |= JB_FSCALAR;
1327 appendToBuffer(buffer, (char *) &header, sizeof(uint32));
1328 /* reserve space for the JEntries of the elements. */
1329 metaoffset = reserveFromBuffer(buffer, sizeof(JEntry) * val->val.array.nElems);
1332 for (i = 0; i < val->val.array.nElems; i++)
1334 JsonbValue *elem = &val->val.array.elems[i];
1338 convertJsonbValue(buffer, &meta, elem, level + 1);
1339 len = meta & JENTRY_POSMASK;
1342 if (totallen > JENTRY_POSMASK)
1344 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1345 errmsg("total size of jsonb array elements exceeds the maximum of %u bytes",
1349 meta = (meta & ~JENTRY_POSMASK) | totallen;
1350 copyToBuffer(buffer, metaoffset, (char *) &meta, sizeof(JEntry));
1351 metaoffset += sizeof(JEntry);
1354 totallen = buffer->len - offset;
1356 /* Initialize the header of this node, in the container's JEntry array */
1357 *pheader = JENTRY_ISCONTAINER | totallen;
1361 convertJsonbObject(StringInfo buffer, JEntry *pheader, JsonbValue *val, int level)
1369 /* Initialize pointer into conversion buffer at this level */
1370 offset = buffer->len;
1372 padBufferToInt(buffer);
1374 /* Initialize header */
1375 header = val->val.object.nPairs | JB_FOBJECT;
1376 appendToBuffer(buffer, (char *) &header, sizeof(uint32));
1378 /* reserve space for the JEntries of the keys and values */
1379 metaoffset = reserveFromBuffer(buffer, sizeof(JEntry) * val->val.object.nPairs * 2);
1382 for (i = 0; i < val->val.object.nPairs; i++)
1384 JsonbPair *pair = &val->val.object.pairs[i];
1389 convertJsonbScalar(buffer, &meta, &pair->key);
1391 len = meta & JENTRY_POSMASK;
1394 if (totallen > JENTRY_POSMASK)
1396 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1397 errmsg("total size of jsonb array elements exceeds the maximum of %u bytes",
1401 meta = (meta & ~JENTRY_POSMASK) | totallen;
1402 copyToBuffer(buffer, metaoffset, (char *) &meta, sizeof(JEntry));
1403 metaoffset += sizeof(JEntry);
1405 convertJsonbValue(buffer, &meta, &pair->value, level);
1406 len = meta & JENTRY_POSMASK;
1409 if (totallen > JENTRY_POSMASK)
1411 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1412 errmsg("total size of jsonb array elements exceeds the maximum of %u bytes",
1415 meta = (meta & ~JENTRY_POSMASK) | totallen;
1416 copyToBuffer(buffer, metaoffset, (char *) &meta, sizeof(JEntry));
1417 metaoffset += sizeof(JEntry);
1420 totallen = buffer->len - offset;
1422 *pheader = JENTRY_ISCONTAINER | totallen;
1426 convertJsonbScalar(StringInfo buffer, JEntry *jentry, JsonbValue *scalarVal)
1431 switch (scalarVal->type)
1434 *jentry = JENTRY_ISNULL;
1438 appendToBuffer(buffer, scalarVal->val.string.val, scalarVal->val.string.len);
1440 *jentry = scalarVal->val.string.len;
1444 numlen = VARSIZE_ANY(scalarVal->val.numeric);
1445 padlen = padBufferToInt(buffer);
1447 appendToBuffer(buffer, (char *) scalarVal->val.numeric, numlen);
1449 *jentry = JENTRY_ISNUMERIC | (padlen + numlen);
1453 *jentry = (scalarVal->val.boolean) ?
1454 JENTRY_ISBOOL_TRUE : JENTRY_ISBOOL_FALSE;
1458 elog(ERROR, "invalid jsonb scalar type");
1463 * Compare two jbvString JsonbValue values, a and b.
1465 * This is a special qsort() comparator used to sort strings in certain
1466 * internal contexts where it is sufficient to have a well-defined sort order.
1467 * In particular, object pair keys are sorted according to this criteria to
1468 * facilitate cheap binary searches where we don't care about lexical sort
1471 * a and b are first sorted based on their length. If a tie-breaker is
1472 * required, only then do we consider string binary equality.
1475 lengthCompareJsonbStringValue(const void *a, const void *b)
1477 const JsonbValue *va = (const JsonbValue *) a;
1478 const JsonbValue *vb = (const JsonbValue *) b;
1481 Assert(va->type == jbvString);
1482 Assert(vb->type == jbvString);
1484 if (va->val.string.len == vb->val.string.len)
1486 res = memcmp(va->val.string.val, vb->val.string.val, va->val.string.len);
1490 res = (va->val.string.len > vb->val.string.len) ? 1 : -1;
1497 * qsort_arg() comparator to compare JsonbPair values.
1499 * Third argument 'binequal' may point to a bool. If it's set, *binequal is set
1500 * to true iff a and b have full binary equality, since some callers have an
1501 * interest in whether the two values are equal or merely equivalent.
1503 * N.B: String comparisons here are "length-wise"
1505 * Pairs with equals keys are ordered such that the order field is respected.
1508 lengthCompareJsonbPair(const void *a, const void *b, void *binequal)
1510 const JsonbPair *pa = (const JsonbPair *) a;
1511 const JsonbPair *pb = (const JsonbPair *) b;
1514 res = lengthCompareJsonbStringValue(&pa->key, &pb->key);
1515 if (res == 0 && binequal)
1516 *((bool *) binequal) = true;
1519 * Guarantee keeping order of equal pair. Unique algorithm will prefer
1520 * first element as value.
1523 res = (pa->order > pb->order) ? -1 : 1;
1529 * Sort and unique-ify pairs in JsonbValue object
1532 uniqueifyJsonbObject(JsonbValue *object)
1534 bool hasNonUniq = false;
1536 Assert(object->type == jbvObject);
1538 if (object->val.object.nPairs > 1)
1539 qsort_arg(object->val.object.pairs, object->val.object.nPairs, sizeof(JsonbPair),
1540 lengthCompareJsonbPair, &hasNonUniq);
1544 JsonbPair *ptr = object->val.object.pairs + 1,
1545 *res = object->val.object.pairs;
1547 while (ptr - object->val.object.pairs < object->val.object.nPairs)
1549 /* Avoid copying over duplicate */
1550 if (lengthCompareJsonbStringValue(ptr, res) != 0)
1554 memcpy(res, ptr, sizeof(JsonbPair));
1559 object->val.object.nPairs = res + 1 - object->val.object.pairs;