22#include "utils/fmgroids.h"
50 const Datum *vals,
const bool *nulls,
int *valcount,
57 Oid val_type,
bool key_scalar);
91 elog(
ERROR,
"unsupported jsonb version number %d", version);
192 switch (
val->val.datetime.typid)
197 return "time without time zone";
199 return "time with time zone";
201 return "timestamp without time zone";
203 return "timestamp with time zone";
205 elog(
ERROR,
"unrecognized jsonb value datetime type: %d",
206 val->val.datetime.typid);
210 elog(
ERROR,
"unrecognized jsonb value type: %d",
val->type);
248 memset(&
sem, 0,
sizeof(
sem));
251 state.unique_keys = unique_keys;
252 state.escontext = escontext;
274 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
275 errmsg(
"string too long to represent as jsonb string"),
276 errdetail(
"Due to an implementation restriction, jsonb strings cannot exceed %d bytes.",
331 v.
val.string.len = strlen(fname);
334 v.
val.string.val = fname;
344 switch (scalarVal->
type)
358 if (scalarVal->
val.boolean)
364 elog(
ERROR,
"unknown jsonb scalar type");
406 v.
val.boolean =
true;
410 v.
val.boolean =
false;
427 va.
val.array.rawScalar =
true;
428 va.
val.array.nElems = 1;
447 elog(
ERROR,
"unexpected parent of nested structure");
491 bool redo_switch =
false;
494 int ispaces = indent ? 1 : 2;
500 bool use_indent =
false;
501 bool raw_scalar =
false;
502 bool last_was_key =
false;
511 while (redo_switch ||
521 if (!v.
val.array.rawScalar)
523 add_indent(out, use_indent && !last_was_key, level);
536 add_indent(out, use_indent && !last_was_key, level);
596 elog(
ERROR,
"unknown jsonb iterator token type");
599 last_was_key = redo_switch;
637 bool numeric_to_string;
639 bool scalar_jsonb =
false;
649 else if (key_scalar &&
657 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
658 errmsg(
"key value must be scalar, not array, composite, or json")));
675 jb.
val.string.len = strlen(outputstr);
676 jb.
val.string.val = outputstr;
688 numeric_to_string =
true;
741 if (numeric_to_string)
745 jb.
val.string.len = strlen(outputstr);
746 jb.
val.string.val = outputstr;
751 jb.
val.numeric = numeric_val;
758 jb.
val.string.len = strlen(jb.
val.string.val);
764 jb.
val.string.len = strlen(jb.
val.string.val);
769 TIMESTAMPTZOID, NULL);
770 jb.
val.string.len = strlen(jb.
val.string.val);
785 memset(&
sem, 0,
sizeof(
sem));
832 if (outfuncoid == F_TEXTOUT ||
833 outfuncoid == F_VARCHAROUT ||
834 outfuncoid == F_BPCHAROUT)
844 jb.
val.string.len = strlen(outputstr);
845 jb.
val.string.val = outputstr;
854 if (!is_null && !scalar_jsonb &&
866 va.
val.array.rawScalar =
true;
867 va.
val.array.nElems = 1;
888 elog(
ERROR,
"unexpected parent of nested structure");
909 for (
i = 1;
i <= dims[dim];
i++)
911 if (dim + 1 == ndims)
920 valcount, tcategory, outfuncoid);
962 &tcategory, &outfuncoid);
1003 for (
i = 0;
i < tupdesc->
natts;
i++)
1013 if (att->attisdropped)
1054 Oid val_type,
bool key_scalar)
1061 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1062 errmsg(
"could not determine input data type")));
1071 &tcategory, &outfuncoid);
1134 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1135 errmsg(
"could not determine input data type")));
1138 &tcategory, &outfuncoid);
1163 bool absent_on_null,
bool unique_keys)
1170 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1171 errmsg(
"argument list must have even number of elements"),
1173 errhint(
"The arguments of %s must consist of alternating keys and values.",
1174 "jsonb_build_object()")));
1182 for (
i = 0;
i < nargs;
i += 2)
1189 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1190 errmsg(
"argument %d: key must not be null",
i + 1)));
1193 skip = absent_on_null && nulls[
i + 1];
1196 if (
skip && !unique_keys)
1248 bool absent_on_null)
1257 for (
i = 0;
i < nargs;
i++)
1259 if (absent_on_null && nulls[
i])
1340 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1341 errmsg(
"array must have even number of elements")));
1347 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1348 errmsg(
"array must have two columns")));
1353 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1354 errmsg(
"wrong number of array subscripts")));
1359 count = in_count / 2;
1361 for (
i = 0;
i < count; ++
i)
1367 if (in_nulls[
i * 2])
1369 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1370 errmsg(
"null value not allowed for object key")));
1382 if (in_nulls[
i * 2 + 1])
1435 if (nkdims > 1 || nkdims != nvdims)
1437 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1438 errmsg(
"wrong number of array subscripts")));
1446 if (key_count != val_count)
1448 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1449 errmsg(
"mismatched array dimensions")));
1451 for (
i = 0;
i < key_count; ++
i)
1459 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1460 errmsg(
"null value not allowed for object key")));
1517 elog(
ERROR,
"jsonb_agg_transfn called in non-aggregate context");
1528 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1529 errmsg(
"could not determine input data type")));
1532 result = &
state->pstate;
1537 &
state->val_output_func);
1542 result = &
state->pstate;
1557 state->val_output_func,
false);
1601 result =
arg->pstate;
1615 bool absent_on_null,
bool unique_keys)
1626 elog(
ERROR,
"jsonb_object_agg_transfn called in non-aggregate context");
1636 result = &
state->pstate;
1646 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1647 errmsg(
"could not determine input data type")));
1650 &
state->key_output_func);
1656 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1657 errmsg(
"could not determine input data type")));
1660 &
state->val_output_func);
1665 result = &
state->pstate;
1670 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1671 errmsg(
"field name must not be null")));
1679 if (
skip && !unique_keys)
1691 state->key_output_func,
true);
1696 state->val_output_func,
false);
1761 result =
arg->pstate;
1799 Assert(tmp.
val.array.nElems == 1 && tmp.
val.array.rawScalar);
1840 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1841 errmsg(messages[
i].msg, sqltype)));
2050 return pstrdup(v.
val.boolean ?
"true" :
"false");
#define PG_GETARG_ARRAYTYPE_P(n)
#define DatumGetArrayTypeP(X)
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
void deconstruct_array(const ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
int ArrayGetNItems(int ndim, const int *dims)
Datum float8_numeric(PG_FUNCTION_ARGS)
Numeric int64_to_numeric(int64 val)
Datum float4_numeric(PG_FUNCTION_ARGS)
Datum numeric_int8(PG_FUNCTION_ARGS)
Datum numeric_int4(PG_FUNCTION_ARGS)
Datum numeric_out(PG_FUNCTION_ARGS)
Datum numeric_int2(PG_FUNCTION_ARGS)
Datum numeric_float8(PG_FUNCTION_ARGS)
Datum numeric_in(PG_FUNCTION_ARGS)
bool numeric_is_nan(Numeric num)
Datum numeric_float4(PG_FUNCTION_ARGS)
bool numeric_is_inf(Numeric num)
#define TextDatumGetCString(d)
#define PG_USED_FOR_ASSERTS_ONLY
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereturn(context, dummy_value,...)
#define ereport(elevel,...)
bool DirectInputFunctionCallSafe(PGFunction func, char *str, Oid typioparam, int32 typmod, Node *escontext, Datum *result)
char * OidOutputFunctionCall(Oid functionId, Datum val)
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
#define OidFunctionCall1(functionId, arg1)
#define PG_FREE_IF_COPY(ptr, n)
#define PG_RETURN_BYTEA_P(x)
#define DatumGetHeapTupleHeader(X)
#define DatumGetTextPP(X)
#define PG_GETARG_POINTER(n)
#define PG_RETURN_CSTRING(x)
#define DirectFunctionCall1(func, arg1)
#define PG_GETARG_DATUM(n)
#define PG_GETARG_CSTRING(n)
#define PG_RETURN_TEXT_P(x)
#define PG_RETURN_DATUM(x)
#define DirectFunctionCall3(func, arg1, arg2, arg3)
#define PG_RETURN_POINTER(x)
#define PG_RETURN_BOOL(x)
int extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
Assert(PointerIsAligned(start, uint64))
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
static int32 HeapTupleHeaderGetTypMod(const HeapTupleHeaderData *tup)
static uint32 HeapTupleHeaderGetDatumLength(const HeapTupleHeaderData *tup)
static Oid HeapTupleHeaderGetTypeId(const HeapTupleHeaderData *tup)
char * JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp)
void escape_json_with_len(StringInfo buf, const char *str, int len)
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, const char *json, size_t len, int encoding, bool need_escapes)
void freeJsonLexContext(JsonLexContext *lex)
Datum jsonb_from_text(text *js, bool unique_keys)
static Datum jsonb_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null)
static JsonParseErrorType jsonb_in_object_start(void *pstate)
Datum jsonb_int4(PG_FUNCTION_ARGS)
Datum jsonb_recv(PG_FUNCTION_ARGS)
Datum jsonb_object_agg_strict_transfn(PG_FUNCTION_ARGS)
Datum to_jsonb(PG_FUNCTION_ARGS)
char * JsonbUnquote(Jsonb *jb)
Datum jsonb_build_array_worker(int nargs, const Datum *args, const bool *nulls, const Oid *types, bool absent_on_null)
static Datum jsonb_from_cstring(char *json, int len, bool unique_keys, Node *escontext)
static void datum_to_jsonb_internal(Datum val, bool is_null, JsonbInState *result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Datum jsonb_build_array_noargs(PG_FUNCTION_ARGS)
Datum jsonb_object_agg_unique_strict_transfn(PG_FUNCTION_ARGS)
Datum jsonb_in(PG_FUNCTION_ARGS)
Datum jsonb_build_object_noargs(PG_FUNCTION_ARGS)
Datum jsonb_agg_transfn(PG_FUNCTION_ARGS)
Datum jsonb_object_two_arg(PG_FUNCTION_ARGS)
static const char * JsonbContainerTypeName(JsonbContainer *jbc)
Datum jsonb_object_agg_transfn(PG_FUNCTION_ARGS)
static JsonParseErrorType jsonb_in_array_end(void *pstate)
static char * JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent)
Datum jsonb_build_array(PG_FUNCTION_ARGS)
Datum jsonb_int8(PG_FUNCTION_ARGS)
static void jsonb_put_escaped_value(StringInfo out, JsonbValue *scalarVal)
static void cannotCastJsonbValue(enum jbvType type, const char *sqltype)
static void add_jsonb(Datum val, bool is_null, JsonbInState *result, Oid val_type, bool key_scalar)
static JsonParseErrorType jsonb_in_object_field_start(void *pstate, char *fname, bool isnull)
char * JsonbToCString(StringInfo out, JsonbContainer *in, int estimated_len)
Datum jsonb_object_agg_unique_transfn(PG_FUNCTION_ARGS)
Datum jsonb_out(PG_FUNCTION_ARGS)
Datum jsonb_numeric(PG_FUNCTION_ARGS)
static void array_to_jsonb_internal(Datum array, JsonbInState *result)
const char * JsonbTypeName(JsonbValue *val)
struct JsonbAggState JsonbAggState
Datum jsonb_object(PG_FUNCTION_ARGS)
Datum jsonb_build_object_worker(int nargs, const Datum *args, const bool *nulls, const Oid *types, bool absent_on_null, bool unique_keys)
Datum jsonb_int2(PG_FUNCTION_ARGS)
static void composite_to_jsonb(Datum composite, JsonbInState *result)
Datum jsonb_float4(PG_FUNCTION_ARGS)
Datum jsonb_agg_strict_transfn(PG_FUNCTION_ARGS)
static void add_indent(StringInfo out, bool indent, int level)
Datum jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
static JsonParseErrorType jsonb_in_array_start(void *pstate)
bool JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res)
Datum datum_to_jsonb(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
Datum jsonb_build_object(PG_FUNCTION_ARGS)
static Datum jsonb_object_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null, bool unique_keys)
Datum jsonb_typeof(PG_FUNCTION_ARGS)
Datum jsonb_bool(PG_FUNCTION_ARGS)
char * JsonbToCStringIndent(StringInfo out, JsonbContainer *in, int estimated_len)
static JsonParseErrorType jsonb_in_object_end(void *pstate)
static JsonParseErrorType jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
Datum jsonb_agg_finalfn(PG_FUNCTION_ARGS)
Datum jsonb_float8(PG_FUNCTION_ARGS)
bool to_jsonb_is_immutable(Oid typoid)
static bool checkStringLen(size_t len, Node *escontext)
Datum jsonb_send(PG_FUNCTION_ARGS)
static void array_dim_to_jsonb(JsonbInState *result, int dim, int ndims, int *dims, const Datum *vals, const bool *nulls, int *valcount, JsonTypeCategory tcategory, Oid outfuncoid)
#define JsonContainerIsScalar(jc)
#define JsonContainerIsArray(jc)
static Datum JsonbPGetDatum(const Jsonb *p)
#define IsAJsonbScalar(jsonbval)
#define JENTRY_OFFLENMASK
#define PG_GETARG_JSONB_P(x)
#define JsonContainerIsObject(jc)
static Jsonb * DatumGetJsonbP(Datum d)
#define JB_ROOT_IS_SCALAR(jbp_)
void pushJsonbValue(JsonbInState *pstate, JsonbIteratorToken seq, JsonbValue *jbval)
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Jsonb * JsonbValueToJsonb(JsonbValue *val)
JsonLexContext * makeJsonLexContext(JsonLexContext *lex, text *json, bool need_escapes)
void json_categorize_type(Oid typoid, bool is_jsonb, JsonTypeCategory *tcategory, Oid *outfuncoid)
bool pg_parse_json_or_errsave(JsonLexContext *lex, const JsonSemAction *sem, Node *escontext)
#define pg_parse_json_or_ereport(lex, sem)
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
char func_volatile(Oid funcid)
int GetDatabaseEncoding(void)
void * MemoryContextAllocZero(MemoryContext context, Size size)
char * pstrdup(const char *in)
void pfree(void *pointer)
char * pnstrdup(const char *in, Size len)
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
static Numeric DatumGetNumericCopy(Datum X)
static Numeric DatumGetNumeric(Datum X)
#define PG_RETURN_NUMERIC(x)
static Datum NumericGetDatum(Numeric X)
FormData_pg_attribute * Form_pg_attribute
static const struct exclude_list_item skip[]
static char buf[DEFAULT_XLOG_SEG_SIZE]
static bool DatumGetBool(Datum X)
static int64 DatumGetInt64(Datum X)
static Datum PointerGetDatum(const void *X)
static Datum ObjectIdGetDatum(Oid X)
static char * DatumGetCString(Datum X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
static int16 DatumGetInt16(Datum X)
static int32 DatumGetInt32(Datum X)
void check_stack_depth(void)
struct StringInfoData * StringInfo
StringInfo makeStringInfo(void)
void enlargeStringInfo(StringInfo str, int needed)
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
void appendStringInfoSpaces(StringInfo str, int count)
void appendStringInfoString(StringInfo str, const char *s)
void initStringInfo(StringInfo str)
#define appendStringInfoCharMacro(str, ch)
json_struct_action array_end
json_struct_action object_start
json_ofield_action object_field_start
json_scalar_action scalar
json_struct_action array_start
json_struct_action object_end
JsonTypeCategory val_category
JsonTypeCategory key_category
JsonbParseState * parseState
#define ReleaseTupleDesc(tupdesc)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
static Size VARSIZE_ANY_EXHDR(const void *PTR)
static Size VARSIZE(const void *PTR)
static char * VARDATA_ANY(const void *PTR)
text * cstring_to_text(const char *s)