24#include "utils/fmgrprotos.h"
39#define JSONB_MAX_ELEMS (Min(MaxAllocSize / sizeof(JsonbValue), JB_CMASK))
40#define JSONB_MAX_PAIRS (Min(MaxAllocSize / sizeof(JsonbPair), JB_CMASK))
67 const char *
val2,
int len2);
79 val->val.binary.data = &jsonb->
root;
232 if (
va.type ==
vb.type)
250 if (
va.val.array.rawScalar !=
vb.val.array.rawScalar)
251 res = (
va.val.array.rawScalar) ? -1 : 1;
259 if (
va.val.array.nElems !=
vb.val.array.nElems)
260 res = (
va.val.array.nElems >
vb.val.array.nElems) ? 1 : -1;
263 if (
va.val.object.nPairs !=
vb.val.object.nPairs)
264 res = (
va.val.object.nPairs >
vb.val.object.nPairs) ? 1 : -1;
267 elog(
ERROR,
"unexpected jbvBinary value");
270 elog(
ERROR,
"unexpected jbvDatetime value");
277 res = (
va.type >
vb.type) ? 1 : -1;
298 res = (
va.type >
vb.type) ? 1 : -1;
363 char *
base_addr = (
char *) (children + count);
367 for (
i = 0;
i < count;
i++)
371 if (key->type == result->
type)
388 key->val.string.len,
NULL);
421 baseAddr = (
char *) (children + count * 2);
534 result->
val.boolean =
true;
539 result->
val.boolean =
false;
606 for (
i = 0;
i <
jbval->val.object.nPairs;
i++)
618 for (
i = 0;
i <
jbval->val.array.nElems;
i++)
655 v.
val.array.rawScalar) ? &v :
NULL);
677 val->val.array.nElems = 0;
700 val->val.object.nPairs = 0;
748 elog(
ERROR,
"invalid jsonb container type");
755 elog(
ERROR,
"unrecognized jsonb sequential processing token");
794 if (object->
val.object.nPairs >=
ppstate->size)
799 errmsg(
"number of jsonb object pairs exceeds the maximum allowed (%zu)",
802 object->val.object.pairs =
repalloc(object->
val.object.pairs,
806 pair = &
object->val.object.pairs[
object->val.object.nPairs];
808 pair->order =
object->val.object.nPairs;
825 pair = &
object->val.object.pairs[
object->val.object.nPairs];
827 object->val.object.nPairs++;
850 errmsg(
"number of jsonb array elements exceeds the maximum allowed (%zu)",
857 elem = &array->
val.array.elems[array->
val.array.nElems];
859 array->
val.array.nElems++;
878 if (outcontext ==
NULL)
903 switch (v->
val.datetime.typid)
919 elog(
ERROR,
"unexpected jsonb datetime type oid %u",
920 v->
val.datetime.typid);
924 elog(
ERROR,
"invalid jsonb scalar type");
988 switch ((*it)->state)
993 val->val.array.nElems = (*it)->nElems;
999 val->val.array.rawScalar = (*it)->isScalar;
1000 (*it)->curIndex = 0;
1001 (*it)->curDataOffset = 0;
1002 (*it)->curValueOffset = 0;
1008 if ((*it)->curIndex >= (*it)->nElems)
1022 (*it)->dataProper, (*it)->curDataOffset,
1026 (*it)->children[(*it)->curIndex]);
1047 val->val.object.nPairs = (*it)->nElems;
1053 (*it)->curIndex = 0;
1054 (*it)->curDataOffset = 0;
1062 if ((*it)->curIndex >= (*it)->nElems)
1078 (*it)->dataProper, (*it)->curDataOffset,
1081 elog(
ERROR,
"unexpected jsonb type as object key");
1092 fillJsonbValue((*it)->container, (*it)->curIndex + (*it)->nElems,
1093 (*it)->dataProper, (*it)->curValueOffset,
1097 (*it)->children[(*it)->curIndex]);
1099 (*it)->children[(*it)->curIndex + (*it)->nElems]);
1116 elog(
ERROR,
"invalid jsonb iterator state");
1131 it->container = container;
1132 it->parent = parent;
1142 (
char *)
it->children +
it->nElems *
sizeof(
JEntry);
1152 (
char *)
it->children +
it->nElems *
sizeof(
JEntry) * 2;
1157 elog(
ERROR,
"unknown type of jsonb container");
1428 elog(
ERROR,
"invalid jsonb container type");
1431 elog(
ERROR,
"unexpectedly fell off end of jsonb container");
1463 tmp =
scalarVal->val.boolean ? 0x02 : 0x04;
1467 elog(
ERROR,
"invalid jsonb scalar type");
1512 tmp =
scalarVal->val.boolean ? 0x02 : 0x04;
1516 elog(
ERROR,
"invalid jsonb scalar type");
1530 if (
a->type ==
b->type)
1543 return a->val.boolean ==
b->val.boolean;
1546 elog(
ERROR,
"invalid jsonb scalar type");
1549 elog(
ERROR,
"jsonb scalar type mismatch");
1562 if (
a->type ==
b->type)
1579 if (
a->val.boolean ==
b->val.boolean)
1581 else if (
a->val.boolean >
b->val.boolean)
1586 elog(
ERROR,
"invalid jsonb scalar type");
1589 elog(
ERROR,
"jsonb scalar type mismatch");
1613 offset = buffer->
len;
1622 buffer->
data[buffer->
len] =
'\0';
1665 for (p = 0; p <
padlen; p++)
1666 buffer->
data[offset + p] =
'\0';
1738 elog(
ERROR,
"unknown type of jsonb container to convert");
1749 int nElems =
val->val.array.nElems;
1762 if (
val->val.array.rawScalar)
1775 for (
i = 0;
i < nElems;
i++)
1798 errmsg(
"total size of jsonb array elements exceeds the maximum of %d bytes",
1818 errmsg(
"total size of jsonb array elements exceeds the maximum of %d bytes",
1833 int nPairs =
val->val.object.nPairs;
1856 for (
i = 0;
i < nPairs;
i++)
1879 errmsg(
"total size of jsonb object elements exceeds the maximum of %d bytes",
1891 for (
i = 0;
i < nPairs;
i++)
1914 errmsg(
"total size of jsonb object elements exceeds the maximum of %d bytes",
1934 errmsg(
"total size of jsonb object elements exceeds the maximum of %d bytes",
1990 elog(
ERROR,
"invalid jsonb scalar type");
2016 vb->val.string.val,
vb->val.string.len);
2031 return len1 > len2 ? 1 : -1;
2061 res = (
pa->order >
pb->order) ? -1 : 1;
2072 JsonbPair *pairs =
object->val.object.pairs;
2073 int nPairs =
object->val.object.nPairs;
2085 errmsg(
"duplicate JSON object key value"));
2091 for (
int i = 0;
i < nPairs;
i++)
Datum hash_numeric(PG_FUNCTION_ARGS)
Datum hash_numeric_extended(PG_FUNCTION_ARGS)
Datum numeric_cmp(PG_FUNCTION_ARGS)
Datum numeric_eq(PG_FUNCTION_ARGS)
#define Assert(condition)
Datum datumCopy(Datum value, bool typByVal, int typLen)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define palloc_object(type)
#define palloc_array(type, count)
#define palloc0_object(type)
#define DirectFunctionCall2(func, arg1, arg2)
#define DirectFunctionCall1(func, arg1)
Datum difference(PG_FUNCTION_ARGS)
#define ROTATE_HIGH_AND_LOW_32BITS(v)
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
static Datum hash_any(const unsigned char *k, int keylen)
Datum hashcharextended(PG_FUNCTION_ARGS)
char * JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp)
#define JsonContainerIsScalar(jc)
#define JsonContainerIsArray(jc)
#define JsonContainerSize(jc)
#define JBE_ISBOOL_TRUE(je_)
#define IsAJsonbScalar(jsonbval)
#define JENTRY_OFFLENMASK
#define JBE_ISNUMERIC(je_)
#define JBE_ISBOOL_FALSE(je_)
#define JENTRY_ISCONTAINER
#define JBE_ISSTRING(je_)
#define JENTRY_ISBOOL_FALSE
#define JBE_OFFLENFLD(je_)
#define JsonContainerIsObject(jc)
#define JENTRY_ISBOOL_TRUE
#define JBE_ISCONTAINER(je_)
#define JBE_ADVANCE_OFFSET(offset, je)
static void appendKey(JsonbInState *pstate, JsonbValue *string, bool needCopy)
static int lengthCompareJsonbString(const char *val1, int len1, const char *val2, int len2)
static void convertJsonbScalar(StringInfo buffer, JEntry *header, JsonbValue *scalarVal)
static void convertJsonbObject(StringInfo buffer, JEntry *header, JsonbValue *val, int level)
static int lengthCompareJsonbStringValue(const void *a, const void *b)
void pushJsonbValue(JsonbInState *pstate, JsonbIteratorToken seq, JsonbValue *jbval)
static void appendElement(JsonbInState *pstate, JsonbValue *scalarVal, bool needCopy)
static JsonbIterator * iteratorFromContainer(JsonbContainer *container, JsonbIterator *parent)
static void fillJsonbValue(JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result)
static void copyToBuffer(StringInfo buffer, int offset, const void *data, int len)
static void convertJsonbArray(StringInfo buffer, JEntry *header, JsonbValue *val, int level)
static void appendToBuffer(StringInfo buffer, const void *data, int len)
static void uniqueifyJsonbObject(JsonbValue *object, bool unique_keys, bool skip_nulls)
JsonbValue * getKeyJsonValueFromContainer(JsonbContainer *container, const char *keyVal, int keyLen, JsonbValue *res)
int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
uint32 getJsonbLength(const JsonbContainer *jc, int index)
static short padBufferToInt(StringInfo buffer)
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
static void pushJsonbValueScalar(JsonbInState *pstate, JsonbIteratorToken seq, JsonbValue *scalarVal)
void JsonbHashScalarValue(const JsonbValue *scalarVal, uint32 *hash)
void JsonbToJsonbValue(Jsonb *jsonb, JsonbValue *val)
JsonbValue * findJsonbValueFromContainer(JsonbContainer *container, uint32 flags, JsonbValue *key)
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
static int compareJsonbScalarValue(JsonbValue *a, JsonbValue *b)
static bool equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b)
uint32 getJsonbOffset(const JsonbContainer *jc, int index)
void JsonbHashScalarValueExtended(const JsonbValue *scalarVal, uint64 *hash, uint64 seed)
static Jsonb * convertToJsonb(JsonbValue *val)
JsonbValue * getIthJsonbValueFromContainer(JsonbContainer *container, uint32 i)
Jsonb * JsonbValueToJsonb(JsonbValue *val)
static int lengthCompareJsonbPair(const void *a, const void *b, void *binequal)
bool JsonbDeepContains(JsonbIterator **val, JsonbIterator **mContained)
static void appendValue(JsonbInState *pstate, JsonbValue *scalarVal, bool needCopy)
static void copyScalarSubstructure(JsonbValue *v, MemoryContext outcontext)
static JsonbParseState * pushState(JsonbInState *pstate)
static JsonbIterator * freeAndGetParent(JsonbIterator *it)
static void convertJsonbValue(StringInfo buffer, JEntry *header, JsonbValue *val, int level)
static int reserveFromBuffer(StringInfo buffer, int len)
void * MemoryContextAlloc(MemoryContext context, Size size)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
MemoryContext CurrentMemoryContext
static Numeric DatumGetNumeric(Datum X)
struct NumericData * Numeric
static Datum NumericGetDatum(Numeric X)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static uint32 pg_rotate_left32(uint32 word, int n)
static char buf[DEFAULT_XLOG_SEG_SIZE]
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
static uint32 DatumGetUInt32(Datum X)
static uint64 DatumGetUInt64(Datum X)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
static Datum UInt64GetDatum(uint64 X)
static Datum BoolGetDatum(bool X)
static int32 DatumGetInt32(Datum X)
static unsigned hash(unsigned *uv, int n)
void check_stack_depth(void)
void enlargeStringInfo(StringInfo str, int needed)
void initStringInfo(StringInfo str)
JEntry children[FLEXIBLE_ARRAY_MEMBER]
JsonbParseState * parseState
struct JsonbIterator * parent
static Size VARSIZE_ANY(const void *PTR)
static Size VARSIZE(const void *PTR)
static char * VARDATA(const void *PTR)
static void SET_VARSIZE(void *PTR, Size len)
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)