57 const Datum *vals,
const bool *nulls,
int *valcount,
64 Oid val_type,
bool key_scalar);
99 elog(
ERROR,
"unsupported jsonb version number %d", version);
199 switch (
val->val.datetime.typid)
204 return "time without time zone";
206 return "time with time zone";
208 return "timestamp without time zone";
210 return "timestamp with time zone";
212 elog(
ERROR,
"unrecognized jsonb value datetime type: %d",
213 val->val.datetime.typid);
217 elog(
ERROR,
"unrecognized jsonb value type: %d",
val->type);
255 memset(&
sem, 0,
sizeof(
sem));
258 state.unique_keys = unique_keys;
259 state.escontext = escontext;
281 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
282 errmsg(
"string too long to represent as jsonb string"),
283 errdetail(
"Due to an implementation restriction, jsonb strings cannot exceed %d bytes.",
338 v.
val.string.len = strlen(fname);
341 v.
val.string.val = fname;
351 switch (scalarVal->
type)
365 if (scalarVal->
val.boolean)
371 elog(
ERROR,
"unknown jsonb scalar type");
413 v.
val.boolean =
true;
417 v.
val.boolean =
false;
434 va.
val.array.rawScalar =
true;
435 va.
val.array.nElems = 1;
454 elog(
ERROR,
"unexpected parent of nested structure");
498 bool redo_switch =
false;
501 int ispaces = indent ? 1 : 2;
507 bool use_indent =
false;
508 bool raw_scalar =
false;
509 bool last_was_key =
false;
518 while (redo_switch ||
528 if (!v.
val.array.rawScalar)
530 add_indent(out, use_indent && !last_was_key, level);
543 add_indent(out, use_indent && !last_was_key, level);
603 elog(
ERROR,
"unknown jsonb iterator token type");
606 last_was_key = redo_switch;
645 bool scalar_jsonb =
false;
655 else if (key_scalar &&
663 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
664 errmsg(
"key value must be scalar, not array, composite, or json")));
684 jb.
val.string.len = strlen(outputstr);
685 jb.
val.string.val = outputstr;
699 jb.
val.string.len = strlen(outputstr);
700 jb.
val.string.val = outputstr;
709 numeric_error = (strchr(outputstr,
'N') != NULL ||
710 strchr(outputstr,
'n') != NULL);
726 jb.
val.string.len = strlen(outputstr);
727 jb.
val.string.val = outputstr;
735 jb.
val.string.len = strlen(jb.
val.string.val);
741 jb.
val.string.len = strlen(jb.
val.string.val);
746 TIMESTAMPTZOID, NULL);
747 jb.
val.string.len = strlen(jb.
val.string.val);
759 memset(&
sem, 0,
sizeof(
sem));
809 jb.
val.string.len = strlen(outputstr);
811 jb.
val.string.val = outputstr;
817 if (!is_null && !scalar_jsonb &&
829 va.
val.array.rawScalar =
true;
830 va.
val.array.nElems = 1;
851 elog(
ERROR,
"unexpected parent of nested structure");
872 for (
i = 1;
i <= dims[dim];
i++)
874 if (dim + 1 == ndims)
883 valcount, tcategory, outfuncoid);
925 &tcategory, &outfuncoid);
966 for (
i = 0;
i < tupdesc->
natts;
i++)
976 if (att->attisdropped)
1017 Oid val_type,
bool key_scalar)
1024 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1025 errmsg(
"could not determine input data type")));
1034 &tcategory, &outfuncoid);
1097 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1098 errmsg(
"could not determine input data type")));
1101 &tcategory, &outfuncoid);
1126 bool absent_on_null,
bool unique_keys)
1133 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1134 errmsg(
"argument list must have even number of elements"),
1136 errhint(
"The arguments of %s must consist of alternating keys and values.",
1137 "jsonb_build_object()")));
1145 for (
i = 0;
i < nargs;
i += 2)
1152 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1153 errmsg(
"argument %d: key must not be null",
i + 1)));
1156 skip = absent_on_null && nulls[
i + 1];
1159 if (
skip && !unique_keys)
1211 bool absent_on_null)
1220 for (
i = 0;
i < nargs;
i++)
1222 if (absent_on_null && nulls[
i])
1303 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1304 errmsg(
"array must have even number of elements")));
1310 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1311 errmsg(
"array must have two columns")));
1316 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1317 errmsg(
"wrong number of array subscripts")));
1322 count = in_count / 2;
1324 for (
i = 0;
i < count; ++
i)
1330 if (in_nulls[
i * 2])
1332 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1333 errmsg(
"null value not allowed for object key")));
1345 if (in_nulls[
i * 2 + 1])
1398 if (nkdims > 1 || nkdims != nvdims)
1400 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1401 errmsg(
"wrong number of array subscripts")));
1409 if (key_count != val_count)
1411 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1412 errmsg(
"mismatched array dimensions")));
1414 for (
i = 0;
i < key_count; ++
i)
1422 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1423 errmsg(
"null value not allowed for object key")));
1489 icursor = icursor->
next;
1490 if (icursor == NULL)
1493 ocursor = ocursor->
next;
1495 ocursor->
next = NULL;
1509 bool single_scalar =
false;
1518 elog(
ERROR,
"jsonb_agg_transfn called in non-aggregate context");
1529 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1530 errmsg(
"could not determine input data type")));
1535 state->res = result;
1541 &
state->val_output_func);
1546 result =
state->res;
1559 state->val_output_func,
false);
1574 if (v.
val.array.rawScalar)
1575 single_scalar =
true;
1612 elog(
ERROR,
"unknown jsonb iterator token type");
1674 bool absent_on_null,
bool unique_keys)
1693 elog(
ERROR,
"jsonb_object_agg_transfn called in non-aggregate context");
1705 state->res = result;
1717 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1718 errmsg(
"could not determine input data type")));
1721 &
state->key_output_func);
1727 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1728 errmsg(
"could not determine input data type")));
1731 &
state->val_output_func);
1736 result =
state->res;
1743 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1744 errmsg(
"field name must not be null")));
1752 if (
skip && !unique_keys)
1760 state->key_output_func,
true);
1769 state->val_output_func,
false);
1790 if (!v.
val.array.rawScalar)
1791 elog(
ERROR,
"unexpected structure for key");
1805 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1806 errmsg(
"object keys must be strings")));
1824 elog(
ERROR,
"unexpected structure for key");
1831 single_scalar =
false;
1844 if (v.
val.array.rawScalar)
1845 single_scalar =
true;
1883 elog(
ERROR,
"unknown jsonb iterator token type");
1989 Assert(tmp.
val.array.nElems == 1 && tmp.
val.array.rawScalar);
2030 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2031 errmsg(messages[
i].msg, sqltype)));
2177 return pstrdup(v.
val.boolean ?
"true" :
"false");
#define PG_GETARG_ARRAYTYPE_P(n)
#define DatumGetArrayTypeP(X)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
int ArrayGetNItems(int ndim, const int *dims)
Datum numeric_int8(PG_FUNCTION_ARGS)
Datum numeric_uplus(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)
Datum numeric_float4(PG_FUNCTION_ARGS)
#define TextDatumGetCString(d)
#define PG_USED_FOR_ASSERTS_ONLY
#define Assert(condition)
static void PGresult * res
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,...)
char * OidOutputFunctionCall(Oid functionId, Datum val)
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
bool DirectInputFunctionCallSafe(PGFunction func, char *str, Oid typioparam, int32 typmod, fmNodePtr escontext, Datum *result)
#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)
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
#define HeapTupleHeaderGetTypMod(tup)
#define HeapTupleHeaderGetTypeId(tup)
#define HeapTupleHeaderGetDatumLength(tup)
void escape_json_with_len(StringInfo buf, const char *str, int len)
char * JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp)
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)
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)
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 JsonbParseState * clone_parse_state(JsonbParseState *state)
char * JsonbToCStringIndent(StringInfo out, JsonbContainer *in, int estimated_len)
static const char * JsonbContainerTypeName(JsonbContainer *jbc)
static JsonParseErrorType jsonb_in_object_field_start(void *pstate, char *fname, bool isnull)
const char * JsonbTypeName(JsonbValue *val)
Datum jsonb_object_agg_unique_transfn(PG_FUNCTION_ARGS)
Datum jsonb_out(PG_FUNCTION_ARGS)
char * JsonbUnquote(Jsonb *jb)
Datum jsonb_numeric(PG_FUNCTION_ARGS)
static void array_to_jsonb_internal(Datum array, JsonbInState *result)
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)
struct JsonbInState JsonbInState
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)
static JsonParseErrorType jsonb_in_object_end(void *pstate)
char * JsonbToCString(StringInfo out, JsonbContainer *in, int estimated_len)
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 Jsonb * DatumGetJsonbP(Datum d)
static Datum JsonbPGetDatum(const Jsonb *p)
#define IsAJsonbScalar(jsonbval)
#define JENTRY_OFFLENMASK
#define PG_GETARG_JSONB_P(x)
#define JsonContainerIsObject(jc)
#define JB_ROOT_IS_SCALAR(jbp_)
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Jsonb * JsonbValueToJsonb(JsonbValue *val)
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
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)
char * pnstrdup(const char *in, Size len)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
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[]
void check_stack_depth(void)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
static char * DatumGetCString(Datum X)
static Datum ObjectIdGetDatum(Oid X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
MemoryContextSwitchTo(old_ctx)
void destroyStringInfo(StringInfo str)
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)
StringInfoData * StringInfo
#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
struct JsonbParseState * next
#define ReleaseTupleDesc(tupdesc)
#define TupleDescAttr(tupdesc, i)
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
#define VARSIZE_ANY_EXHDR(PTR)
text * cstring_to_text(const char *s)