77 Datum *vals,
bool *nulls,
int *valcount,
87 Oid val_type,
bool key_scalar);
122 elog(
ERROR,
"unsupported jsonb version number %d", version);
211 elog(
ERROR,
"unknown jsonb scalar type");
232 memset(&state, 0,
sizeof(state));
233 memset(&sem, 0,
sizeof(sem));
256 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
257 errmsg(
"string too long to represent as jsonb string"),
258 errdetail(
"Due to an implementation restriction, jsonb strings cannot exceed %d bytes.",
305 v.
val.string.val = fname;
313 switch (scalarVal->
type)
327 if (scalarVal->
val.boolean)
333 elog(
ERROR,
"unknown jsonb scalar type");
353 v.
val.string.val = token;
368 v.
val.boolean =
true;
373 v.
val.boolean =
false;
391 va.
val.array.rawScalar =
true;
392 va.
val.array.nElems = 1;
411 elog(
ERROR,
"unexpected parent of nested structure");
453 bool redo_switch =
false;
456 int ispaces = indent ? 1 : 2;
462 bool use_indent =
false;
463 bool raw_scalar =
false;
464 bool last_was_key =
false;
473 while (redo_switch ||
483 if (!v.
val.array.rawScalar)
485 add_indent(out, use_indent && !last_was_key, level);
498 add_indent(out, use_indent && !last_was_key, level);
558 elog(
ERROR,
"unknown jsonb iterator token type");
561 last_was_key = redo_switch;
577 for (i = 0; i < level; i++)
670 *outfuncoid = castfunc;
705 bool scalar_jsonb =
false;
715 else if (key_scalar &&
723 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
724 errmsg(
"key value must be scalar, not array, composite, or json")));
744 jb.
val.string.len = strlen(outputstr);
745 jb.
val.string.val = outputstr;
759 jb.
val.string.len = strlen(outputstr);
760 jb.
val.string.val = outputstr;
769 numeric_error = (strchr(outputstr,
'N') !=
NULL ||
770 strchr(outputstr,
'n') !=
NULL);
773 jb.
type = jbvNumeric;
781 jb.
val.string.len = strlen(outputstr);
782 jb.
val.string.val = outputstr;
803 jb.
val.string.len = strlen(buf);
822 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
823 errmsg(
"timestamp out of range")));
825 jb.
val.string.len = strlen(buf);
835 const char *tzn =
NULL;
846 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
847 errmsg(
"timestamp out of range")));
849 jb.
val.string.len = strlen(buf);
863 memset(&sem, 0,
sizeof(sem));
914 jb.
val.string.val = outputstr;
920 if (!is_null && !scalar_jsonb &&
932 va.
val.array.rawScalar =
true;
933 va.
val.array.nElems = 1;
954 elog(
ERROR,
"unexpected parent of nested structure");
975 for (i = 1; i <= dims[dim]; i++)
977 if (dim + 1 == ndims)
979 datum_to_jsonb(vals[*valcount], nulls[*valcount], result, tcategory,
986 valcount, tcategory, outfuncoid);
1025 &typlen, &typbyval, &typalign);
1028 &tcategory, &outfuncoid);
1031 typalign, &elements, &nulls,
1069 for (i = 0; i < tupdesc->
natts; i++)
1078 if (tupdesc->
attrs[i]->attisdropped)
1085 v.
val.string.len = strlen(attname);
1086 v.
val.string.val = attname;
1099 &tcategory, &outfuncoid);
1101 datum_to_jsonb(val, isnull, result, tcategory, outfuncoid,
false);
1118 Oid val_type,
bool key_scalar)
1125 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1126 errmsg(
"could not determine input data type")));
1135 &tcategory, &outfuncoid);
1137 datum_to_jsonb(val, is_null, result, tcategory, outfuncoid, key_scalar);
1154 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1155 errmsg(
"could not determine input data type")));
1158 &tcategory, &outfuncoid);
1162 datum_to_jsonb(val,
false, &result, tcategory, outfuncoid,
false);
1181 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1182 errmsg(
"invalid number of arguments: object must be matched key value pairs")));
1188 for (i = 0; i < nargs; i += 2)
1194 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1195 errmsg(
"argument %d: key must not be null", i + 1)));
1213 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1214 errmsg(
"argument %d: could not determine data type", i + 1)));
1216 add_jsonb(arg,
false, &result, val_type,
true);
1236 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1237 errmsg(
"argument %d: could not determine data type", i + 2)));
1278 for (i = 0; i < nargs; i++)
1296 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1297 errmsg(
"argument %d: could not determine data type", i + 1)));
1355 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1356 errmsg(
"array must have even number of elements")));
1362 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1363 errmsg(
"array must have two columns")));
1368 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1369 errmsg(
"wrong number of array subscripts")));
1374 &in_datums, &in_nulls, &in_count);
1376 count = in_count / 2;
1378 for (i = 0; i < count; ++
i)
1384 if (in_nulls[i * 2])
1386 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1387 errmsg(
"null value not allowed for object key")));
1394 v.
val.string.len = len;
1395 v.
val.string.val = str;
1399 if (in_nulls[i * 2 + 1])
1410 v.
val.string.len = len;
1411 v.
val.string.val = str;
1452 if (nkdims > 1 || nkdims != nvdims)
1454 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1455 errmsg(
"wrong number of array subscripts")));
1462 &key_datums, &key_nulls, &key_count);
1466 &val_datums, &val_nulls, &val_count);
1468 if (key_count != val_count)
1470 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1471 errmsg(
"mismatched array dimensions")));
1473 for (i = 0; i < key_count; ++
i)
1481 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1482 errmsg(
"null value not allowed for object key")));
1489 v.
val.string.len = len;
1490 v.
val.string.val = str;
1505 v.
val.string.len = len;
1506 v.
val.string.val = str;
1546 icursor = icursor->
next;
1547 if (icursor ==
NULL)
1550 ocursor = ocursor->
next;
1570 bool single_scalar =
false;
1579 elog(
ERROR,
"jsonb_agg_transfn called in non-aggregate context");
1590 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1591 errmsg(
"could not determine input data type")));
1596 state->
res = result;
1607 result = state->
res;
1632 if (v.
val.array.rawScalar)
1633 single_scalar =
true;
1651 if (v.
type == jbvString)
1659 else if (v.
type == jbvNumeric)
1670 elog(
ERROR,
"unknown jsonb iterator token type");
1733 elog(
ERROR,
"jsonb_object_agg_transfn called in non-aggregate context");
1745 state->
res = result;
1754 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1755 errmsg(
"could not determine input data type")));
1764 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1765 errmsg(
"could not determine input data type")));
1773 result = state->
res;
1780 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1781 errmsg(
"field name must not be null")));
1818 if (!v.
val.array.rawScalar)
1819 elog(
ERROR,
"unexpected structure for key");
1822 if (v.
type == jbvString)
1833 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1834 errmsg(
"object keys must be strings")));
1842 elog(
ERROR,
"unexpected structure for key");
1849 single_scalar =
false;
1862 if (v.
val.array.rawScalar)
1863 single_scalar =
true;
1881 if (v.
type == jbvString)
1889 else if (v.
type == jbvNumeric)
1901 elog(
ERROR,
"unknown jsonb iterator token type");
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
json_struct_action array_end
static void array_to_jsonb_internal(Datum array, JsonbInState *result)
JsonbParseState * parseState
#define PG_RETURN_POINTER(x)
static void jsonb_in_object_end(void *pstate)
#define DatumGetDateADT(X)
#define JB_ROOT_IS_OBJECT(jbp_)
Datum jsonb_typeof(PG_FUNCTION_ARGS)
char * pnstrdup(const char *in, Size len)
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Datum numeric_uplus(PG_FUNCTION_ARGS)
static Datum jsonb_from_cstring(char *json, int len)
static char * JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent)
void escape_json(StringInfo buf, const char *str)
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
json_struct_action object_end
Datum jsonb_build_object(PG_FUNCTION_ARGS)
Datum jsonb_object(PG_FUNCTION_ARGS)
static void jsonb_in_array_start(void *pstate)
Oid get_element_type(Oid typid)
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
#define PointerGetDatum(X)
#define PG_GETARG_DATUM(n)
char * pstrdup(const char *in)
static JsonbParseState * clone_parse_state(JsonbParseState *state)
#define NumericGetDatum(X)
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
StringInfo makeStringInfo(void)
StringInfoData * StringInfo
int ArrayGetNItems(int ndim, const int *dims)
Form_pg_attribute * attrs
Datum numeric_out(PG_FUNCTION_ARGS)
json_struct_action object_start
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static void add_jsonb(Datum val, bool is_null, JsonbInState *result, Oid val_type, bool key_scalar)
int errcode(int sqlerrcode)
json_scalar_action scalar
static void jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
#define JENTRY_OFFLENMASK
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PG_GETARG_POINTER(n)
void EncodeSpecialTimestamp(Timestamp dt, char *str)
#define DirectFunctionCall1(func, arg1)
#define PG_RETURN_BYTEA_P(x)
#define JB_ROOT_IS_SCALAR(jbp_)
struct JsonbParseState * next
#define OidIsValid(objectId)
#define DatumGetHeapTupleHeader(X)
#define FirstNormalObjectId
char * JsonbToCStringIndent(StringInfo out, JsonbContainer *in, int estimated_len)
static void jsonb_in_array_end(void *pstate)
#define HeapTupleHeaderGetTypMod(tup)
JsonbTypeCategory val_category
static void datum_to_jsonb(Datum val, bool is_null, JsonbInState *result, JsonbTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
#define PG_GETARG_ARRAYTYPE_P(n)
#define appendStringInfoCharMacro(str, ch)
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
#define TIMESTAMP_NOT_FINITE(j)
void pfree(void *pointer)
Datum jsonb_send(PG_FUNCTION_ARGS)
Datum jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
Datum jsonb_object_agg_transfn(PG_FUNCTION_ARGS)
#define DatumGetCString(X)
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Datum jsonb_build_object_noargs(PG_FUNCTION_ARGS)
#define JB_ROOT_IS_ARRAY(jbp_)
#define DATE_NOT_FINITE(j)
void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
#define OidFunctionCall1(functionId, arg1)
static size_t checkStringLen(size_t len)
void appendStringInfoString(StringInfo str, const char *s)
Datum to_jsonb(PG_FUNCTION_ARGS)
void check_stack_depth(void)
#define DatumGetTimestampTz(X)
int errdetail(const char *fmt,...)
#define CStringGetDatum(X)
void enlargeStringInfo(StringInfo str, int needed)
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
static void jsonb_categorize_type(Oid typoid, JsonbTypeCategory *tcategory, Oid *outfuncoid)
void EncodeSpecialDate(DateADT dt, char *str)
Datum numeric_in(PG_FUNCTION_ARGS)
bool type_is_rowtype(Oid typid)
Datum jsonb_recv(PG_FUNCTION_ARGS)
static void array_dim_to_jsonb(JsonbInState *result, int dim, int ndims, int *dims, Datum *vals, bool *nulls, int *valcount, JsonbTypeCategory tcategory, Oid outfuncoid)
#define ereport(elevel, rest)
void j2date(int jd, int *year, int *month, int *day)
struct JsonbAggState JsonbAggState
#define DirectFunctionCall3(func, arg1, arg2, arg3)
#define heap_getattr(tup, attnum, tupleDesc, isnull)
#define TextDatumGetCString(d)
void * palloc0(Size size)
static void composite_to_jsonb(Datum composite, JsonbInState *result)
Datum jsonb_object_two_arg(PG_FUNCTION_ARGS)
#define HeapTupleHeaderGetTypeId(tup)
static void jsonb_put_escaped_value(StringInfo out, JsonbValue *scalarVal)
static void add_indent(StringInfo out, bool indent, int level)
Datum jsonb_agg_transfn(PG_FUNCTION_ARGS)
#define PG_RETURN_TEXT_P(x)
text * cstring_to_text(const char *s)
#define DatumGetNumeric(X)
#define Assert(condition)
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
json_struct_action array_start
Datum jsonb_out(PG_FUNCTION_ARGS)
#define PG_RETURN_CSTRING(x)
char * JsonbToCString(StringInfo out, JsonbContainer *in, int estimated_len)
struct JsonbInState JsonbInState
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Datum jsonb_build_array(PG_FUNCTION_ARGS)
JsonbTypeCategory key_category
static void jsonb_in_object_start(void *pstate)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Datum jsonb_build_array_noargs(PG_FUNCTION_ARGS)
#define PG_GETARG_JSONB(x)
Datum jsonb_in(PG_FUNCTION_ARGS)
char * OidOutputFunctionCall(Oid functionId, Datum val)
int errmsg(const char *fmt,...)
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
#define CStringGetTextDatum(s)
#define PG_GETARG_CSTRING(n)
#define POSTGRES_EPOCH_JDATE
json_ofield_action object_field_start
static void jsonb_in_object_field_start(void *pstate, char *fname, bool isnull)
Datum jsonb_agg_finalfn(PG_FUNCTION_ARGS)
#define ReleaseTupleDesc(tupdesc)
Oid getBaseType(Oid typid)
enum JsonbValue::@70 type
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
#define DatumGetTimestamp(X)
#define HeapTupleHeaderGetDatumLength(tup)
#define DatumGetArrayTypeP(X)