103 bool use_line_feeds);
105 Datum *vals,
bool *nulls,
int *valcount,
107 bool use_line_feeds);
109 bool use_line_feeds);
117 Oid val_type,
bool key_scalar);
247 || typoid == ANYCOMPATIBLEARRAYOID || typoid == RECORDARRAYOID)
267 *outfuncoid = castfunc;
305 Assert(!(key_scalar && is_null));
319 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
320 errmsg(
"key value must be scalar, not array, composite, or json")));
466 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
467 errmsg(
"timestamp out of range")));
476 const char *tzn = NULL;
496 tzp ? NULL : &tzn, NULL) == 0)
505 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
506 errmsg(
"timestamp out of range")));
510 elog(
ERROR,
"unknown jsonb value datetime type oid %u", typid);
525 Oid outfuncoid,
bool use_line_feeds)
532 sep = use_line_feeds ?
",\n " :
",";
536 for (
i = 1;
i <= dims[dim];
i++)
541 if (dim + 1 == ndims)
543 datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
554 valcount, tcategory, outfuncoid,
false);
595 &tcategory, &outfuncoid);
602 outfuncoid, use_line_feeds);
621 bool needsep =
false;
624 sep = use_line_feeds ?
",\n " :
",";
640 for (
i = 0;
i < tupdesc->
natts;
i++)
649 if (att->attisdropped)
686 Oid val_type,
bool key_scalar)
693 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
694 errmsg(
"could not determine input data type")));
703 &tcategory, &outfuncoid);
830 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
831 errmsg(
"could not determine input data type")));
834 &tcategory, &outfuncoid);
859 elog(
ERROR,
"json_agg_transfn called in non-aggregate context");
868 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
869 errmsg(
"could not determine input data type")));
884 &
state->val_output_func);
894 if (
state->str->len > 1)
916 state->val_output_func,
false);
1006 memset(&ctl, 0,
sizeof(ctl));
1073 bool absent_on_null,
bool unique_keys)
1086 elog(
ERROR,
"json_object_agg_transfn called in non-aggregate context");
1105 memset(&
state->unique_check, 0,
sizeof(
state->unique_check));
1112 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1113 errmsg(
"could not determine data type for argument %d", 1)));
1116 &
state->key_output_func);
1122 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1123 errmsg(
"could not determine data type for argument %d", 2)));
1126 &
state->val_output_func);
1145 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1146 errmsg(
"null value not allowed for object key")));
1177 key_offset = out->
len;
1180 state->key_output_func,
true);
1184 const char *
key = &out->
data[key_offset];
1188 errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1203 state->val_output_func,
false);
1274 int buflen = buffer->
len;
1275 int addlen = strlen(addon);
1280 memcpy(
VARDATA(result) + buflen, addon, addlen);
1287 bool absent_on_null,
bool unique_keys)
1290 const char *sep =
"";
1296 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1297 errmsg(
"argument list must have even number of elements"),
1299 errhint(
"The arguments of %s must consist of alternating keys and values.",
1300 "json_build_object()")));
1309 for (
i = 0;
i < nargs;
i += 2)
1316 skip = absent_on_null && nulls[
i + 1];
1336 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1337 errmsg(
"null value not allowed for object key")));
1340 key_offset = out->
len;
1347 const char *
key = &out->
data[key_offset];
1351 errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1400 bool absent_on_null)
1403 const char *sep =
"";
1410 for (
i = 0;
i < nargs;
i++)
1412 if (absent_on_null && nulls[
i])
1483 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1484 errmsg(
"array must have even number of elements")));
1490 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1491 errmsg(
"array must have two columns")));
1496 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1497 errmsg(
"wrong number of array subscripts")));
1502 count = in_count / 2;
1508 for (
i = 0;
i < count; ++
i)
1510 if (in_nulls[
i * 2])
1512 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1513 errmsg(
"null value not allowed for object key")));
1521 if (in_nulls[
i * 2 + 1])
1566 if (nkdims > 1 || nkdims != nvdims)
1568 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1569 errmsg(
"wrong number of array subscripts")));
1577 if (key_count != val_count)
1579 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1580 errmsg(
"mismatched array dimensions")));
1586 for (
i = 0;
i < key_count; ++
i)
1590 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1591 errmsg(
"null value not allowed for object key")));
1632 for (p =
str; *p; p++)
1658 if ((
unsigned char) *p <
' ')
1679 entry =
palloc(
sizeof(*entry));
1682 state->stack = entry;
1696 entry =
state->stack;
1715 state->unique =
false;
1718 while ((entry =
state->stack))
1735 if (check_unique_keys)
1739 state.id_counter = 0;
1740 state.unique =
true;
1759 if (check_unique_keys && !
state.unique)
1763 (
errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1764 errmsg(
"duplicate JSON object key value")));
1821 elog(
ERROR,
"unexpected json token: %d", tok);
#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)
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
void j2date(int jd, int *year, int *month, int *day)
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
void EncodeSpecialTimestamp(Timestamp dt, char *str)
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
#define CStringGetTextDatum(s)
#define TextDatumGetCString(d)
#define OidIsValid(objectId)
#define TIMESTAMP_NOT_FINITE(j)
#define POSTGRES_EPOCH_JDATE
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
void EncodeSpecialDate(DateADT dt, char *str)
#define DATE_NOT_FINITE(j)
static DateADT DatumGetDateADT(Datum X)
static TimeADT DatumGetTimeADT(Datum X)
static TimeTzADT * DatumGetTimeTzADTP(Datum X)
elog(ERROR, "%s: %s", p2, msg)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
char * OidOutputFunctionCall(Oid functionId, Datum val)
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
#define OidFunctionCall1(functionId, arg1)
#define PG_GETARG_TEXT_PP(n)
#define PG_RETURN_BYTEA_P(x)
#define DatumGetHeapTupleHeader(X)
#define DatumGetTextPP(X)
#define PG_GETARG_POINTER(n)
#define PG_RETURN_CSTRING(x)
#define PG_GETARG_DATUM(n)
#define PG_GETARG_CSTRING(n)
#define PG_RETURN_TEXT_P(x)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_DATUM(x)
#define PG_RETURN_POINTER(x)
int extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
uint32 hash_bytes_uint32(uint32 k)
uint32 hash_bytes(const unsigned char *k, int keylen)
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
#define HeapTupleHeaderGetTypMod(tup)
#define HeapTupleHeaderGetTypeId(tup)
#define HeapTupleHeaderGetDatumLength(tup)
Datum row_to_json(PG_FUNCTION_ARGS)
struct JsonUniqueStackEntry JsonUniqueStackEntry
static JsonParseErrorType json_unique_object_start(void *_state)
static uint32 json_unique_hash(const void *key, Size keysize)
Datum json_build_object_noargs(PG_FUNCTION_ARGS)
static int json_unique_hash_match(const void *key1, const void *key2, Size keysize)
static void json_unique_check_init(JsonUniqueCheckState *cxt)
static text * catenate_stringinfo_string(StringInfo buffer, const char *addon)
struct HTAB * JsonUniqueCheckState
Datum json_agg_strict_transfn(PG_FUNCTION_ARGS)
struct JsonUniqueBuilderState JsonUniqueBuilderState
Datum json_in(PG_FUNCTION_ARGS)
static StringInfo json_unique_builder_get_throwawaybuf(JsonUniqueBuilderState *cxt)
static bool json_unique_check_key(JsonUniqueCheckState *cxt, const char *key, int object_id)
struct JsonUniqueParsingState JsonUniqueParsingState
Datum json_out(PG_FUNCTION_ARGS)
Datum json_agg_transfn(PG_FUNCTION_ARGS)
Datum to_json(PG_FUNCTION_ARGS)
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Datum json_object_agg_finalfn(PG_FUNCTION_ARGS)
Datum row_to_json_pretty(PG_FUNCTION_ARGS)
struct JsonUniqueHashEntry JsonUniqueHashEntry
Datum json_send(PG_FUNCTION_ARGS)
static Datum json_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null)
Datum array_to_json_pretty(PG_FUNCTION_ARGS)
Datum json_object_agg_unique_transfn(PG_FUNCTION_ARGS)
Datum json_object_two_arg(PG_FUNCTION_ARGS)
Datum json_object_agg_unique_strict_transfn(PG_FUNCTION_ARGS)
Datum json_build_array_noargs(PG_FUNCTION_ARGS)
Datum json_object_agg_transfn(PG_FUNCTION_ARGS)
Datum array_to_json(PG_FUNCTION_ARGS)
Datum json_build_array_worker(int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null)
Datum json_object_agg_strict_transfn(PG_FUNCTION_ARGS)
Datum json_build_array(PG_FUNCTION_ARGS)
static void json_unique_builder_init(JsonUniqueBuilderState *cxt)
Datum json_build_object_worker(int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null, bool unique_keys)
static void add_json(Datum val, bool is_null, StringInfo result, Oid val_type, bool key_scalar)
char * JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp)
Datum json_agg_finalfn(PG_FUNCTION_ARGS)
static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals, bool *nulls, int *valcount, JsonTypeCategory tcategory, Oid outfuncoid, bool use_line_feeds)
Datum json_recv(PG_FUNCTION_ARGS)
bool json_validate(text *json, bool check_unique_keys, bool throw_error)
Datum json_typeof(PG_FUNCTION_ARGS)
struct JsonAggState JsonAggState
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
static Datum json_object_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null, bool unique_keys)
Datum json_object(PG_FUNCTION_ARGS)
Datum json_build_object(PG_FUNCTION_ARGS)
void escape_json(StringInfo buf, const char *str)
bool to_json_is_immutable(Oid typoid)
static JsonParseErrorType json_unique_object_field_start(void *_state, char *field, bool isnull)
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
static JsonParseErrorType json_unique_object_end(void *_state)
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, int encoding, bool need_escapes)
bool IsValidJsonNumber(const char *str, int len)
JsonSemAction nullSemAction
JsonParseErrorType pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
JsonParseErrorType json_lex(JsonLexContext *lex)
@ JSON_TOKEN_OBJECT_START
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
bool pg_parse_json_or_errsave(JsonLexContext *lex, JsonSemAction *sem, Node *escontext)
void json_errsave_error(JsonParseErrorType error, JsonLexContext *lex, Node *escontext)
#define pg_parse_json_or_ereport(lex, sem)
Assert(fmt[strlen(fmt) - 1] !='\n')
Oid get_element_type(Oid typid)
bool type_is_rowtype(Oid typid)
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
char func_volatile(Oid funcid)
Oid getBaseType(Oid typid)
int GetDatabaseEncoding(void)
void pfree(void *pointer)
MemoryContext CurrentMemoryContext
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
FormData_pg_attribute * Form_pg_attribute
static const struct exclude_list_item skip[]
void check_stack_depth(void)
static uint32 DatumGetUInt32(Datum X)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
static unsigned hash(unsigned *uv, int n)
StringInfo makeStringInfo(void)
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendStringInfoString(StringInfo str, const char *s)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
StringInfoData * StringInfo
#define appendStringInfoCharMacro(str, ch)
JsonUniqueBuilderState unique_check
JsonTypeCategory key_category
JsonTypeCategory val_category
json_struct_action object_start
json_ofield_action object_field_start
json_struct_action object_end
JsonUniqueCheckState check
StringInfoData skipped_keys
JsonUniqueCheckState check
JsonUniqueStackEntry * stack
struct JsonUniqueStackEntry * parent
#define FirstNormalObjectId
#define ReleaseTupleDesc(tupdesc)
#define TupleDescAttr(tupdesc, i)
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
static Timestamp DatumGetTimestamp(Datum X)
static TimestampTz DatumGetTimestampTz(Datum X)
#define SET_VARSIZE(PTR, len)
#define VARSIZE_ANY_EXHDR(PTR)
char * text_to_cstring(const text *t)
text * cstring_to_text_with_len(const char *s, int len)
text * cstring_to_text(const char *s)