21#include "utils/fmgrprotos.h"
36#define JSONB_MAX_ELEMS (Min(MaxAllocSize / sizeof(JsonbValue), JB_CMASK))
37#define JSONB_MAX_PAIRS (Min(MaxAllocSize / sizeof(JsonbPair), JB_CMASK))
40 char *base_addr,
uint32 offset,
63 const char *val2,
int len2);
75 val->val.binary.data = &jsonb->
root;
104 scalarArray.
val.array.rawScalar =
true;
105 scalarArray.
val.array.nElems = 1;
122 memcpy(
VARDATA(out),
val->val.binary.data,
val->val.binary.len);
247 if (va.
val.array.rawScalar != vb.
val.array.rawScalar)
248 res = (va.
val.array.rawScalar) ? -1 : 1;
249 if (va.
val.array.nElems != vb.
val.array.nElems)
250 res = (va.
val.array.nElems > vb.
val.array.nElems) ? 1 : -1;
253 if (va.
val.object.nPairs != vb.
val.object.nPairs)
254 res = (va.
val.object.nPairs > vb.
val.object.nPairs) ? 1 : -1;
257 elog(
ERROR,
"unexpected jbvBinary value");
260 elog(
ERROR,
"unexpected jbvDatetime value");
359 char *base_addr = (
char *) (children + count);
363 for (
i = 0;
i < count;
i++)
384 key->val.string.len, NULL);
417 baseAddr = (
char *) (children + count * 2);
420 while (stopLow < stopHigh)
424 const char *candidateVal;
427 stopMiddle = stopLow + (stopHigh - stopLow) / 2;
438 int index = stopMiddle + count;
452 stopLow = stopMiddle + 1;
454 stopHigh = stopMiddle;
478 base_addr = (
char *) &container->
children[nelements];
506 char *base_addr,
uint32 offset,
518 result->
val.string.val = base_addr + offset;
530 result->
val.boolean =
true;
535 result->
val.boolean =
false;
578 for (
i = 0;
i < jbval->
val.object.nPairs;
i++)
590 for (
i = 0;
i < jbval->
val.array.nElems;
i++)
608 if ((jbval->
val.binary.data->header &
JB_FSCALAR) && *pstate)
630 v.
val.array.rawScalar) ? &v : NULL);
648 Assert(!scalarVal || scalarVal->
val.array.rawScalar);
650 result = &(*pstate)->contVal;
652 (*pstate)->contVal.val.array.nElems = 0;
653 (*pstate)->contVal.val.array.rawScalar = (scalarVal &&
654 scalarVal->
val.array.rawScalar);
655 if (scalarVal && scalarVal->
val.array.nElems > 0)
659 (*pstate)->size = scalarVal->
val.array.nElems;
671 result = &(*pstate)->contVal;
673 (*pstate)->contVal.val.object.nPairs = 0;
692 (*pstate)->unique_keys,
693 (*pstate)->skip_nulls);
698 result = &(*pstate)->contVal;
704 *pstate = (*pstate)->
next;
707 switch ((*pstate)->contVal.type)
716 elog(
ERROR,
"invalid jsonb container type");
721 elog(
ERROR,
"unrecognized jsonb sequential processing token");
755 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
756 errmsg(
"number of jsonb object pairs exceeds the maximum allowed (%zu)",
759 if (object->
val.object.nPairs >= pstate->
size)
762 object->val.object.pairs =
repalloc(object->
val.object.pairs,
766 object->val.object.pairs[
object->val.object.nPairs].key = *
string;
767 object->val.object.pairs[
object->val.object.nPairs].order =
object->val.object.nPairs;
781 object->val.object.pairs[
object->val.object.nPairs++].value = *scalarVal;
796 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
797 errmsg(
"number of jsonb array elements exceeds the maximum allowed (%zu)",
800 if (array->
val.array.nElems >= pstate->
size)
807 array->
val.array.elems[array->
val.array.nElems++] = *scalarVal;
865 switch ((*it)->state)
870 val->val.array.nElems = (*it)->nElems;
876 val->val.array.rawScalar = (*it)->isScalar;
878 (*it)->curDataOffset = 0;
879 (*it)->curValueOffset = 0;
885 if ((*it)->curIndex >= (*it)->nElems)
898 (*it)->dataProper, (*it)->curDataOffset,
902 (*it)->children[(*it)->curIndex]);
923 val->val.object.nPairs = (*it)->nElems;
930 (*it)->curDataOffset = 0;
938 if ((*it)->curIndex >= (*it)->nElems)
953 (*it)->dataProper, (*it)->curDataOffset,
956 elog(
ERROR,
"unexpected jsonb type as object key");
968 (*it)->dataProper, (*it)->curValueOffset,
972 (*it)->children[(*it)->curIndex]);
974 (*it)->children[(*it)->curIndex + (*it)->nElems]);
1030 elog(
ERROR,
"unknown type of jsonb container");
1104 if (vval.
val.object.nPairs < vcontained.
val.object.nPairs)
1129 vcontained.
val.string.val,
1130 vcontained.
val.string.len,
1147 if (lhsVal->
type != vcontained.
type)
1196 uint32 nLhsElems = vval.
val.array.nElems;
1211 if (vval.
val.array.rawScalar && !vcontained.
val.array.rawScalar)
1244 if (lhsConts == NULL)
1251 for (
i = 0;
i < nLhsElems;
i++)
1258 lhsConts[
j++] = vval;
1270 for (
i = 0;
i < nLhsElems;
i++)
1285 pfree(nestContained);
1301 elog(
ERROR,
"invalid jsonb container type");
1304 elog(
ERROR,
"unexpectedly fell off end of jsonb container");
1321 switch (scalarVal->
type)
1328 scalarVal->
val.string.len));
1336 tmp = scalarVal->
val.boolean ? 0x02 : 0x04;
1340 elog(
ERROR,
"invalid jsonb scalar type");
1364 switch (scalarVal->
type)
1371 scalarVal->
val.string.len,
1385 tmp = scalarVal->
val.boolean ? 0x02 : 0x04;
1389 elog(
ERROR,
"invalid jsonb scalar type");
1403 if (
a->type ==
b->type)
1416 return a->val.boolean ==
b->val.boolean;
1419 elog(
ERROR,
"invalid jsonb scalar type");
1422 elog(
ERROR,
"jsonb scalar type mismatch");
1435 if (
a->type ==
b->type)
1446 DEFAULT_COLLATION_OID);
1452 if (
a->val.boolean ==
b->val.boolean)
1454 else if (
a->val.boolean >
b->val.boolean)
1459 elog(
ERROR,
"invalid jsonb scalar type");
1462 elog(
ERROR,
"jsonb scalar type mismatch");
1486 offset = buffer->
len;
1495 buffer->
data[buffer->
len] =
'\0';
1538 for (p = 0; p < padlen; p++)
1539 buffer->
data[offset + p] =
'\0';
1611 elog(
ERROR,
"unknown type of jsonb container to convert");
1622 int nElems =
val->val.array.nElems;
1625 base_offset = buffer->
len;
1635 if (
val->val.array.rawScalar)
1648 for (
i = 0;
i < nElems;
i++)
1670 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1671 errmsg(
"total size of jsonb array elements exceeds the maximum of %d bytes",
1681 jentry_offset +=
sizeof(
JEntry);
1685 totallen = buffer->
len - base_offset;
1690 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1691 errmsg(
"total size of jsonb array elements exceeds the maximum of %d bytes",
1706 int nPairs =
val->val.object.nPairs;
1709 base_offset = buffer->
len;
1729 for (
i = 0;
i < nPairs;
i++)
1751 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1752 errmsg(
"total size of jsonb object elements exceeds the maximum of %d bytes",
1762 jentry_offset +=
sizeof(
JEntry);
1764 for (
i = 0;
i < nPairs;
i++)
1786 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1787 errmsg(
"total size of jsonb object elements exceeds the maximum of %d bytes",
1797 jentry_offset +=
sizeof(
JEntry);
1801 totallen = buffer->
len - base_offset;
1806 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1807 errmsg(
"total size of jsonb object elements exceeds the maximum of %d bytes",
1820 switch (scalarVal->
type)
1829 *header = scalarVal->
val.string.len;
1842 *header = (scalarVal->
val.boolean) ?
1852 scalarVal->
val.datetime.value,
1853 scalarVal->
val.datetime.typid,
1854 &scalarVal->
val.datetime.tz);
1863 elog(
ERROR,
"invalid jsonb scalar type");
1889 vb->
val.string.val, vb->
val.string.len);
1902 return memcmp(val1, val2, len1);
1904 return len1 > len2 ? 1 : -1;
1926 if (
res == 0 && binequal)
1927 *((
bool *) binequal) =
true;
1945 bool hasNonUniq =
false;
1949 if (object->
val.object.nPairs > 1)
1953 if (hasNonUniq && unique_keys)
1955 errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1956 errmsg(
"duplicate JSON object key value"));
1958 if (hasNonUniq || skip_nulls)
1963 while (skip_nulls && object->
val.object.nPairs > 0 &&
1964 object->
val.object.pairs->value.type ==
jbvNull)
1967 object->val.object.pairs++;
1968 object->val.object.nPairs--;
1971 if (object->
val.object.nPairs > 0)
1973 ptr =
object->val.object.pairs + 1;
1974 res =
object->val.object.pairs;
1976 while (ptr - object->
val.object.pairs < object->
val.object.nPairs)
1989 object->val.object.nPairs =
res + 1 -
object->val.object.pairs;
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)
static void PGresult * res
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#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)
if(TABLE==NULL||TABLE_index==NULL)
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 int lengthCompareJsonbString(const char *val1, int len1, const char *val2, int len2)
static void appendElement(JsonbParseState *pstate, JsonbValue *scalarVal)
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)
static JsonbIterator * iteratorFromContainer(JsonbContainer *container, JsonbIterator *parent)
static void fillJsonbValue(JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result)
static void convertJsonbArray(StringInfo buffer, JEntry *header, JsonbValue *val, int level)
static void uniqueifyJsonbObject(JsonbValue *object, bool unique_keys, bool skip_nulls)
static void appendValue(JsonbParseState *pstate, JsonbValue *scalarVal)
static JsonbValue * pushJsonbValueScalar(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *scalarVal)
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)
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
void JsonbHashScalarValue(const JsonbValue *scalarVal, uint32 *hash)
static JsonbParseState * pushState(JsonbParseState **pstate)
void JsonbToJsonbValue(Jsonb *jsonb, JsonbValue *val)
static void appendToBuffer(StringInfo buffer, const char *data, int len)
JsonbValue * findJsonbValueFromContainer(JsonbContainer *container, uint32 flags, JsonbValue *key)
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
static int compareJsonbScalarValue(JsonbValue *a, JsonbValue *b)
static void copyToBuffer(StringInfo buffer, int offset, const char *data, int len)
static void appendKey(JsonbParseState *pstate, JsonbValue *string)
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 JsonbIterator * freeAndGetParent(JsonbIterator *it)
static void convertJsonbValue(StringInfo buffer, JEntry *header, JsonbValue *val, int level)
static int reserveFromBuffer(StringInfo buffer, int len)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
void * palloc0(Size size)
struct NumericData * Numeric
static Datum NumericGetDatum(Numeric X)
static uint32 pg_rotate_left32(uint32 word, int n)
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]
struct JsonbIterator * parent
JsonbContainer * container
struct JsonbParseState * next
#define SET_VARSIZE(PTR, len)
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)