78 Datum *vals,
bool *nulls,
int *valcount,
88 Oid val_type,
bool key_scalar);
123 elog(
ERROR,
"unsupported jsonb version number %d", version);
210 switch (jbv->
val.datetime.typid)
215 return "time without time zone";
217 return "time with time zone";
219 return "timestamp without time zone";
221 return "timestamp with time zone";
223 elog(
ERROR,
"unrecognized jsonb value datetime type: %d",
224 jbv->
val.datetime.typid);
262 memset(&state, 0,
sizeof(state));
263 memset(&sem, 0,
sizeof(sem));
286 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
287 errmsg(
"string too long to represent as jsonb string"),
288 errdetail(
"Due to an implementation restriction, jsonb strings cannot exceed %d bytes.",
335 v.
val.string.val = fname;
343 switch (scalarVal->
type)
357 if (scalarVal->
val.boolean)
363 elog(
ERROR,
"unknown jsonb scalar type");
384 v.
val.string.val = token;
402 v.
val.boolean =
true;
406 v.
val.boolean =
false;
423 va.
val.array.rawScalar =
true;
424 va.
val.array.nElems = 1;
443 elog(
ERROR,
"unexpected parent of nested structure");
485 bool redo_switch =
false;
488 int ispaces = indent ? 1 : 2;
494 bool use_indent =
false;
495 bool raw_scalar =
false;
496 bool last_was_key =
false;
505 while (redo_switch ||
515 if (!v.
val.array.rawScalar)
517 add_indent(out, use_indent && !last_was_key, level);
530 add_indent(out, use_indent && !last_was_key, level);
590 elog(
ERROR,
"unknown jsonb iterator token type");
593 last_was_key = redo_switch;
609 for (i = 0; i < level; i++)
680 || typoid == ANYCOMPATIBLEARRAYOID || typoid == RECORDARRAYOID)
703 *outfuncoid = castfunc;
738 bool scalar_jsonb =
false;
748 else if (key_scalar &&
756 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
757 errmsg(
"key value must be scalar, not array, composite, or json")));
777 jb.
val.string.len = strlen(outputstr);
778 jb.
val.string.val = outputstr;
792 jb.
val.string.len = strlen(outputstr);
793 jb.
val.string.val = outputstr;
802 numeric_error = (strchr(outputstr,
'N') != NULL ||
803 strchr(outputstr,
'n') != NULL);
819 jb.
val.string.len = strlen(outputstr);
820 jb.
val.string.val = outputstr;
828 jb.
val.string.len = strlen(jb.
val.string.val);
834 jb.
val.string.len = strlen(jb.
val.string.val);
839 TIMESTAMPTZOID, NULL);
840 jb.
val.string.len = strlen(jb.
val.string.val);
852 memset(&sem, 0,
sizeof(sem));
903 jb.
val.string.val = outputstr;
909 if (!is_null && !scalar_jsonb &&
921 va.
val.array.rawScalar =
true;
922 va.
val.array.nElems = 1;
943 elog(
ERROR,
"unexpected parent of nested structure");
964 for (i = 1; i <= dims[dim]; i++)
966 if (dim + 1 == ndims)
968 datum_to_jsonb(vals[*valcount], nulls[*valcount], result, tcategory,
975 valcount, tcategory, outfuncoid);
1014 &typlen, &typbyval, &typalign);
1017 &tcategory, &outfuncoid);
1020 typalign, &elements, &nulls,
1058 for (i = 0; i < tupdesc->
natts; i++)
1068 if (att->attisdropped)
1071 attname =
NameStr(att->attname);
1075 v.
val.string.len = strlen(attname);
1090 datum_to_jsonb(val, isnull, result, tcategory, outfuncoid,
false);
1107 Oid val_type,
bool key_scalar)
1114 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1115 errmsg(
"could not determine input data type")));
1124 &tcategory, &outfuncoid);
1126 datum_to_jsonb(val, is_null, result, tcategory, outfuncoid, key_scalar);
1143 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1144 errmsg(
"could not determine input data type")));
1147 &tcategory, &outfuncoid);
1151 datum_to_jsonb(val,
false, &result, tcategory, outfuncoid,
false);
1177 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1178 errmsg(
"argument list must have even number of elements"),
1180 errhint(
"The arguments of %s must consist of alternating keys and values.",
1181 "jsonb_build_object()")));
1187 for (i = 0; i < nargs; i += 2)
1192 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1193 errmsg(
"argument %d: key must not be null", i + 1)));
1195 add_jsonb(args[i],
false, &result, types[i],
true);
1198 add_jsonb(args[i + 1], nulls[i + 1], &result, types[i + 1],
false);
1245 for (i = 0; i < nargs; i++)
1246 add_jsonb(args[i], nulls[i], &result, types[i],
false);
1302 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1303 errmsg(
"array must have even number of elements")));
1309 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1310 errmsg(
"array must have two columns")));
1315 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1316 errmsg(
"wrong number of array subscripts")));
1320 TEXTOID, -1,
false, TYPALIGN_INT,
1321 &in_datums, &in_nulls, &in_count);
1323 count = in_count / 2;
1325 for (i = 0; i < count; ++
i)
1331 if (in_nulls[i * 2])
1333 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1334 errmsg(
"null value not allowed for object key")));
1341 v.
val.string.len = len;
1346 if (in_nulls[i * 2 + 1])
1357 v.
val.string.len = len;
1399 if (nkdims > 1 || nkdims != nvdims)
1401 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1402 errmsg(
"wrong number of array subscripts")));
1408 TEXTOID, -1,
false, TYPALIGN_INT,
1409 &key_datums, &key_nulls, &key_count);
1412 TEXTOID, -1,
false, TYPALIGN_INT,
1413 &val_datums, &val_nulls, &val_count);
1415 if (key_count != val_count)
1417 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1418 errmsg(
"mismatched array dimensions")));
1420 for (i = 0; i < key_count; ++
i)
1428 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1429 errmsg(
"null value not allowed for object key")));
1436 v.
val.string.len = len;
1452 v.
val.string.len = len;
1493 icursor = icursor->
next;
1494 if (icursor == NULL)
1497 ocursor = ocursor->
next;
1499 ocursor->
next = NULL;
1517 bool single_scalar =
false;
1526 elog(
ERROR,
"jsonb_agg_transfn called in non-aggregate context");
1537 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1538 errmsg(
"could not determine input data type")));
1543 state->
res = result;
1554 result = state->
res;
1579 if (v.
val.array.rawScalar)
1580 single_scalar =
true;
1617 elog(
ERROR,
"unknown jsonb iterator token type");
1680 elog(
ERROR,
"jsonb_object_agg_transfn called in non-aggregate context");
1692 state->
res = result;
1701 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1702 errmsg(
"could not determine input data type")));
1711 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1712 errmsg(
"could not determine input data type")));
1720 result = state->
res;
1727 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1728 errmsg(
"field name must not be null")));
1765 if (!v.
val.array.rawScalar)
1766 elog(
ERROR,
"unexpected structure for key");
1780 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1781 errmsg(
"object keys must be strings")));
1789 elog(
ERROR,
"unexpected structure for key");
1796 single_scalar =
false;
1809 if (v.
val.array.rawScalar)
1810 single_scalar =
true;
1848 elog(
ERROR,
"unknown jsonb iterator token type");
1916 Assert(tmp.
val.array.nElems == 1 && tmp.
val.array.rawScalar);
1954 for (i = 0; i <
lengthof(messages); i++)
1955 if (messages[i].type == type)
1957 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1958 errmsg(messages[i].msg, sqltype)));
1961 elog(
ERROR,
"unknown jsonb type: %d", (
int) type);
#define PG_RETURN_NUMERIC(x)
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)
Datum jsonb_numeric(PG_FUNCTION_ARGS)
int errhint(const char *fmt,...)
Datum jsonb_typeof(PG_FUNCTION_ARGS)
bool JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res)
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)
int extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
static char * JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent)
void escape_json(StringInfo buf, const char *str)
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)
#define DatumGetJsonbP(d)
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)
#define TupleDescAttr(tupdesc, i)
Datum jsonb_int2(PG_FUNCTION_ARGS)
static JsonbParseState * clone_parse_state(JsonbParseState *state)
#define NumericGetDatum(X)
#define DatumGetTextPP(X)
#define DatumGetNumericCopy(X)
StringInfo makeStringInfo(void)
StringInfoData * StringInfo
Datum numeric_int4(PG_FUNCTION_ARGS)
int ArrayGetNItems(int ndim, const int *dims)
#define JsonContainerIsScalar(jc)
Datum numeric_out(PG_FUNCTION_ARGS)
json_struct_action object_start
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static void add_jsonb(Datum val, bool is_null, JsonbInState *result, Oid val_type, bool key_scalar)
Datum numeric_int8(PG_FUNCTION_ARGS)
int errcode(int sqlerrcode)
json_scalar_action scalar
static void jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
Datum jsonb_float4(PG_FUNCTION_ARGS)
#define JENTRY_OFFLENMASK
#define PG_GETARG_POINTER(n)
#define DirectFunctionCall1(func, arg1)
#define PG_RETURN_BYTEA_P(x)
#define JB_ROOT_IS_SCALAR(jbp_)
static void cannotCastJsonbValue(enum jbvType type, const char *sqltype)
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)
Datum numeric_float4(PG_FUNCTION_ARGS)
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
#define PG_GETARG_ARRAYTYPE_P(n)
#define appendStringInfoCharMacro(str, ch)
void pfree(void *pointer)
Datum numeric_int2(PG_FUNCTION_ARGS)
Datum jsonb_send(PG_FUNCTION_ARGS)
Datum jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
#define ObjectIdGetDatum(X)
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 OidFunctionCall1(functionId, arg1)
static size_t checkStringLen(size_t len)
char * JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp)
void appendStringInfoString(StringInfo str, const char *s)
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, int encoding, bool need_escapes)
const char * JsonbTypeName(JsonbValue *jbv)
Datum to_jsonb(PG_FUNCTION_ARGS)
void check_stack_depth(void)
Datum numeric_float8(PG_FUNCTION_ARGS)
int errdetail(const char *fmt,...)
#define CStringGetDatum(X)
void enlargeStringInfo(StringInfo str, int needed)
FormData_pg_attribute * Form_pg_attribute
static void jsonb_categorize_type(Oid typoid, JsonbTypeCategory *tcategory, Oid *outfuncoid)
Datum numeric_in(PG_FUNCTION_ARGS)
bool type_is_rowtype(Oid typid)
#define JsonContainerIsArray(jc)
Datum jsonb_recv(PG_FUNCTION_ARGS)
Datum jsonb_bool(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)
Datum jsonb_int4(PG_FUNCTION_ARGS)
struct JsonbAggState JsonbAggState
#define DirectFunctionCall3(func, arg1, arg2, arg3)
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Datum jsonb_int8(PG_FUNCTION_ARGS)
void pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem)
#define TextDatumGetCString(d)
void * palloc0(Size size)
static void composite_to_jsonb(Datum composite, JsonbInState *result)
#define PG_RETURN_BOOL(x)
#define JsonContainerIsObject(jc)
#define PG_RETURN_DATUM(x)
int GetDatabaseEncoding(void)
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)
#define ereport(elevel,...)
Datum jsonb_agg_transfn(PG_FUNCTION_ARGS)
static const char * JsonbContainerTypeName(JsonbContainer *jbc)
#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
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Datum jsonb_build_array(PG_FUNCTION_ARGS)
#define IsAJsonbScalar(jsonbval)
#define PG_FREE_IF_COPY(ptr, n)
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)
Datum jsonb_in(PG_FUNCTION_ARGS)
char * OidOutputFunctionCall(Oid functionId, Datum val)
int errmsg(const char *fmt,...)
Datum jsonb_float8(PG_FUNCTION_ARGS)
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
#define PG_GETARG_CSTRING(n)
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)
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
#define PG_USED_FOR_ASSERTS_ONLY
#define PG_GETARG_JSONB_P(x)
#define HeapTupleHeaderGetDatumLength(tup)
#define DatumGetArrayTypeP(X)