61 Datum *vals,
bool *nulls,
int *valcount,
68 Oid val_type,
bool key_scalar);
103 elog(
ERROR,
"unsupported jsonb version number %d", version);
204 switch (
val->val.datetime.typid)
209 return "time without time zone";
211 return "time with time zone";
213 return "timestamp without time zone";
215 return "timestamp with time zone";
217 elog(
ERROR,
"unrecognized jsonb value datetime type: %d",
218 val->val.datetime.typid);
222 elog(
ERROR,
"unrecognized jsonb value type: %d",
val->type);
260 memset(&sem, 0,
sizeof(sem));
263 state.unique_keys = unique_keys;
264 state.escontext = escontext;
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.",
343 v.
val.string.len = strlen(fname);
346 v.
val.string.val = fname;
356 switch (scalarVal->
type)
370 if (scalarVal->
val.boolean)
376 elog(
ERROR,
"unknown jsonb scalar type");
418 v.
val.boolean =
true;
422 v.
val.boolean =
false;
439 va.
val.array.rawScalar =
true;
440 va.
val.array.nElems = 1;
459 elog(
ERROR,
"unexpected parent of nested structure");
503 bool redo_switch =
false;
506 int ispaces = indent ? 1 : 2;
512 bool use_indent =
false;
513 bool raw_scalar =
false;
514 bool last_was_key =
false;
523 while (redo_switch ||
533 if (!v.
val.array.rawScalar)
535 add_indent(out, use_indent && !last_was_key, level);
548 add_indent(out, use_indent && !last_was_key, level);
608 elog(
ERROR,
"unknown jsonb iterator token type");
611 last_was_key = redo_switch;
650 bool scalar_jsonb =
false;
660 else if (key_scalar &&
668 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
669 errmsg(
"key value must be scalar, not array, composite, or json")));
689 jb.
val.string.len = strlen(outputstr);
690 jb.
val.string.val = outputstr;
704 jb.
val.string.len = strlen(outputstr);
705 jb.
val.string.val = outputstr;
714 numeric_error = (strchr(outputstr,
'N') != NULL ||
715 strchr(outputstr,
'n') != NULL);
731 jb.
val.string.len = strlen(outputstr);
732 jb.
val.string.val = outputstr;
740 jb.
val.string.len = strlen(jb.
val.string.val);
746 jb.
val.string.len = strlen(jb.
val.string.val);
751 TIMESTAMPTZOID, NULL);
752 jb.
val.string.len = strlen(jb.
val.string.val);
764 memset(&sem, 0,
sizeof(sem));
813 jb.
val.string.len = strlen(outputstr);
815 jb.
val.string.val = outputstr;
821 if (!is_null && !scalar_jsonb &&
833 va.
val.array.rawScalar =
true;
834 va.
val.array.nElems = 1;
855 elog(
ERROR,
"unexpected parent of nested structure");
876 for (
i = 1;
i <= dims[dim];
i++)
878 if (dim + 1 == ndims)
887 valcount, tcategory, outfuncoid);
929 &tcategory, &outfuncoid);
970 for (
i = 0;
i < tupdesc->
natts;
i++)
980 if (att->attisdropped)
1021 Oid val_type,
bool key_scalar)
1028 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1029 errmsg(
"could not determine input data type")));
1038 &tcategory, &outfuncoid);
1101 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1102 errmsg(
"could not determine input data type")));
1105 &tcategory, &outfuncoid);
1130 bool absent_on_null,
bool unique_keys)
1137 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1138 errmsg(
"argument list must have even number of elements"),
1140 errhint(
"The arguments of %s must consist of alternating keys and values.",
1141 "jsonb_build_object()")));
1149 for (
i = 0;
i < nargs;
i += 2)
1156 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1157 errmsg(
"argument %d: key must not be null",
i + 1)));
1160 skip = absent_on_null && nulls[
i + 1];
1163 if (
skip && !unique_keys)
1215 bool absent_on_null)
1224 for (
i = 0;
i < nargs;
i++)
1226 if (absent_on_null && nulls[
i])
1307 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1308 errmsg(
"array must have even number of elements")));
1314 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1315 errmsg(
"array must have two columns")));
1320 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1321 errmsg(
"wrong number of array subscripts")));
1326 count = in_count / 2;
1328 for (
i = 0;
i < count; ++
i)
1334 if (in_nulls[
i * 2])
1336 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1337 errmsg(
"null value not allowed for object key")));
1349 if (in_nulls[
i * 2 + 1])
1402 if (nkdims > 1 || nkdims != nvdims)
1404 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1405 errmsg(
"wrong number of array subscripts")));
1413 if (key_count != val_count)
1415 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1416 errmsg(
"mismatched array dimensions")));
1418 for (
i = 0;
i < key_count; ++
i)
1426 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1427 errmsg(
"null value not allowed for object key")));
1493 icursor = icursor->
next;
1494 if (icursor == NULL)
1497 ocursor = ocursor->
next;
1499 ocursor->
next = NULL;
1513 bool single_scalar =
false;
1522 elog(
ERROR,
"jsonb_agg_transfn called in non-aggregate context");
1533 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1534 errmsg(
"could not determine input data type")));
1539 state->res = result;
1545 &
state->val_output_func);
1550 result =
state->res;
1563 state->val_output_func,
false);
1578 if (v.
val.array.rawScalar)
1579 single_scalar =
true;
1616 elog(
ERROR,
"unknown jsonb iterator token type");
1678 bool absent_on_null,
bool unique_keys)
1697 elog(
ERROR,
"jsonb_object_agg_transfn called in non-aggregate context");
1709 state->res = result;
1721 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1722 errmsg(
"could not determine input data type")));
1725 &
state->key_output_func);
1731 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1732 errmsg(
"could not determine input data type")));
1735 &
state->val_output_func);
1740 result =
state->res;
1747 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1748 errmsg(
"field name must not be null")));
1756 if (
skip && !unique_keys)
1764 state->key_output_func,
true);
1773 state->val_output_func,
false);
1794 if (!v.
val.array.rawScalar)
1795 elog(
ERROR,
"unexpected structure for key");
1809 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1810 errmsg(
"object keys must be strings")));
1828 elog(
ERROR,
"unexpected structure for key");
1835 single_scalar =
false;
1848 if (v.
val.array.rawScalar)
1849 single_scalar =
true;
1887 elog(
ERROR,
"unknown jsonb iterator token type");
1993 Assert(tmp.
val.array.nElems == 1 && tmp.
val.array.rawScalar);
2034 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2035 errmsg(messages[
i].msg, sqltype)));
#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
static void PGresult * res
elog(ERROR, "%s: %s", p2, msg)
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)
char * JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp)
void escape_json(StringInfo buf, const char *str)
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, int encoding, bool need_escapes)
Datum jsonb_from_text(text *js, bool unique_keys)
static void array_dim_to_jsonb(JsonbInState *result, int dim, int ndims, int *dims, Datum *vals, bool *nulls, int *valcount, JsonTypeCategory tcategory, Oid outfuncoid)
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)
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_build_array_worker(int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null)
Datum jsonb_build_object_worker(int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null, bool unique_keys)
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)
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)
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)
#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(text *json, bool need_escapes)
bool pg_parse_json_or_errsave(JsonLexContext *lex, JsonSemAction *sem, Node *escontext)
void json_categorize_type(Oid typoid, bool is_jsonb, JsonTypeCategory *tcategory, Oid *outfuncoid)
#define pg_parse_json_or_ereport(lex, sem)
Assert(fmt[strlen(fmt) - 1] !='\n')
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)
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)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
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)
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)