1 /*-------------------------------------------------------------------------
4 * Functions to process JSON data types.
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/utils/adt/jsonfuncs.c
12 *-------------------------------------------------------------------------
19 #include "access/htup_details.h"
20 #include "catalog/pg_type.h"
23 #include "lib/stringinfo.h"
24 #include "mb/pg_wchar.h"
25 #include "miscadmin.h"
26 #include "utils/array.h"
27 #include "utils/builtins.h"
28 #include "utils/hsearch.h"
29 #include "utils/json.h"
30 #include "utils/jsonapi.h"
31 #include "utils/jsonb.h"
32 #include "utils/lsyscache.h"
33 #include "utils/memutils.h"
34 #include "utils/syscache.h"
35 #include "utils/typcache.h"
37 /* Operations available for setPath */
38 #define JB_PATH_CREATE 0x0001
39 #define JB_PATH_DELETE 0x0002
40 #define JB_PATH_REPLACE 0x0004
41 #define JB_PATH_INSERT_BEFORE 0x0008
42 #define JB_PATH_INSERT_AFTER 0x0010
43 #define JB_PATH_CREATE_OR_INSERT \
44 (JB_PATH_INSERT_BEFORE | JB_PATH_INSERT_AFTER | JB_PATH_CREATE)
46 /* state for json_object_keys */
47 typedef struct OkeysState
56 /* state for iterate_json_string_values function */
57 typedef struct IterateJsonStringValuesState
60 JsonIterateStringValuesAction action; /* an action that will be
61 * applied to each json value */
62 void *action_state; /* any necessary context for iteration */
63 } IterateJsonStringValuesState;
65 /* state for transform_json_string_values function */
66 typedef struct TransformJsonStringValuesState
69 StringInfo strval; /* resulting json */
70 JsonTransformStringValuesAction action; /* an action that will be
71 * applied to each json value */
72 void *action_state; /* any necessary context for transformation */
73 } TransformJsonStringValuesState;
75 /* state for json_get* functions */
76 typedef struct GetState
81 bool normalize_results;
83 int npath; /* length of each path-related array */
84 char **path_names; /* field name(s) being sought */
85 int *path_indexes; /* array index(es) being sought */
86 bool *pathok; /* is path matched to current depth? */
87 int *array_cur_index; /* current element index at each path level */
90 /* state for json_array_length */
91 typedef struct AlenState
97 /* state for json_each */
98 typedef struct EachState
101 Tuplestorestate *tuple_store;
103 MemoryContext tmp_cxt;
105 bool normalize_results;
107 char *normalized_scalar;
110 /* state for json_array_elements */
111 typedef struct ElementsState
114 const char *function_name;
115 Tuplestorestate *tuple_store;
117 MemoryContext tmp_cxt;
119 bool normalize_results;
121 char *normalized_scalar;
124 /* state for get_json_object_as_hash */
125 typedef struct JHashState
128 const char *function_name;
131 char *save_json_start;
132 JsonTokenType saved_token_type;
135 /* hashtable element */
136 typedef struct JsonHashEntry
138 char fname[NAMEDATALEN]; /* hash key (MUST BE FIRST) */
143 /* structure to cache type I/O metadata needed for populate_scalar() */
144 typedef struct ScalarIOData
150 /* these two structures are used recursively */
151 typedef struct ColumnIOData ColumnIOData;
152 typedef struct RecordIOData RecordIOData;
154 /* structure to cache metadata needed for populate_array() */
155 typedef struct ArrayIOData
157 ColumnIOData *element_info; /* metadata cache */
158 Oid element_type; /* array element type id */
159 int32 element_typmod; /* array element type modifier */
162 /* structure to cache metadata needed for populate_composite() */
163 typedef struct CompositeIOData
166 * We use pointer to a RecordIOData here because variable-length struct
167 * RecordIOData can't be used directly in ColumnIOData.io union
169 RecordIOData *record_io; /* metadata cache for populate_record() */
170 TupleDesc tupdesc; /* cached tuple descriptor */
173 /* structure to cache metadata needed for populate_domain() */
174 typedef struct DomainIOData
176 ColumnIOData *base_io; /* metadata cache */
177 Oid base_typid; /* base type id */
178 int32 base_typmod; /* base type modifier */
179 void *domain_info; /* opaque cache for domain checks */
182 /* enumeration type categories */
185 TYPECAT_SCALAR = 's',
187 TYPECAT_COMPOSITE = 'c',
191 /* these two are stolen from hstore / record_out, used in populate_record* */
193 /* structure to cache record metadata needed for populate_record_field() */
196 Oid typid; /* column type id */
197 int32 typmod; /* column type modifier */
198 TypeCat typcat; /* column type category */
199 ScalarIOData scalar_io; /* metadata cache for directi conversion
200 * through input function */
204 CompositeIOData composite;
206 } io; /* metadata cache for various column type
210 /* structure to cache record metadata needed for populate_record() */
216 ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
219 /* state for populate_recordset */
220 typedef struct PopulateRecordsetState
223 const char *function_name;
226 char *save_json_start;
227 JsonTokenType saved_token_type;
228 Tuplestorestate *tuple_store;
231 RecordIOData **my_extra;
232 MemoryContext fn_mcxt; /* used to stash IO funcs */
233 } PopulateRecordsetState;
235 /* structure to cache metadata needed for populate_record_worker() */
236 typedef struct PopulateRecordCache
238 Oid argtype; /* verified row type of the first argument */
239 CompositeIOData io; /* metadata cache for populate_composite() */
240 } PopulateRecordCache;
242 /* common data for populate_array_json() and populate_array_dim_jsonb() */
243 typedef struct PopulateArrayContext
245 ArrayBuildState *astate; /* array build state */
246 ArrayIOData *aio; /* metadata cache */
247 MemoryContext acxt; /* array build memory context */
248 MemoryContext mcxt; /* cache memory context */
249 const char *colname; /* for diagnostics only */
250 int *dims; /* dimensions */
251 int *sizes; /* current dimension counters */
252 int ndims; /* number of dimensions */
253 } PopulateArrayContext;
255 /* state for populate_array_json() */
256 typedef struct PopulateArrayState
258 JsonLexContext *lex; /* json lexer */
259 PopulateArrayContext *ctx; /* context */
260 char *element_start; /* start of the current array element */
261 char *element_scalar; /* current array element token if it is a
263 JsonTokenType element_type; /* current array element type */
264 } PopulateArrayState;
266 /* state for json_strip_nulls */
267 typedef struct StripnullState
274 /* structure for generalized json/jsonb value passing */
275 typedef struct JsValue
277 bool is_json; /* json/jsonb */
282 char *str; /* json string */
283 int len; /* json string length or -1 if null-terminated */
284 JsonTokenType type; /* json type */
285 } json; /* json value */
287 JsonbValue *jsonb; /* jsonb value */
291 typedef struct JsObject
293 bool is_json; /* json/jsonb */
297 JsonbContainer *jsonb_cont;
301 /* useful macros for testing JsValue properties */
302 #define JsValueIsNull(jsv) \
304 (!(jsv)->val.json.str || (jsv)->val.json.type == JSON_TOKEN_NULL) : \
305 (!(jsv)->val.jsonb || (jsv)->val.jsonb->type == jbvNull))
307 #define JsValueIsString(jsv) \
308 ((jsv)->is_json ? (jsv)->val.json.type == JSON_TOKEN_STRING \
309 : ((jsv)->val.jsonb && (jsv)->val.jsonb->type == jbvString))
311 #define JsObjectIsEmpty(jso) \
313 ? hash_get_num_entries((jso)->val.json_hash) == 0 \
314 : ((jso)->val.jsonb_cont == NULL || \
315 JsonContainerSize((jso)->val.jsonb_cont) == 0))
317 #define JsObjectFree(jso) \
319 if ((jso)->is_json) \
320 hash_destroy((jso)->val.json_hash); \
323 /* semantic action functions for json_object_keys */
324 static void okeys_object_field_start(void *state, char *fname, bool isnull);
325 static void okeys_array_start(void *state);
326 static void okeys_scalar(void *state, char *token, JsonTokenType tokentype);
328 /* semantic action functions for json_get* functions */
329 static void get_object_start(void *state);
330 static void get_object_end(void *state);
331 static void get_object_field_start(void *state, char *fname, bool isnull);
332 static void get_object_field_end(void *state, char *fname, bool isnull);
333 static void get_array_start(void *state);
334 static void get_array_end(void *state);
335 static void get_array_element_start(void *state, bool isnull);
336 static void get_array_element_end(void *state, bool isnull);
337 static void get_scalar(void *state, char *token, JsonTokenType tokentype);
339 /* common worker function for json getter functions */
340 static Datum get_path_all(FunctionCallInfo fcinfo, bool as_text);
341 static text *get_worker(text *json, char **tpath, int *ipath, int npath,
342 bool normalize_results);
343 static Datum get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text);
345 /* semantic action functions for json_array_length */
346 static void alen_object_start(void *state);
347 static void alen_scalar(void *state, char *token, JsonTokenType tokentype);
348 static void alen_array_element_start(void *state, bool isnull);
350 /* common workers for json{b}_each* functions */
351 static Datum each_worker(FunctionCallInfo fcinfo, bool as_text);
352 static Datum each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
355 /* semantic action functions for json_each */
356 static void each_object_field_start(void *state, char *fname, bool isnull);
357 static void each_object_field_end(void *state, char *fname, bool isnull);
358 static void each_array_start(void *state);
359 static void each_scalar(void *state, char *token, JsonTokenType tokentype);
361 /* common workers for json{b}_array_elements_* functions */
362 static Datum elements_worker(FunctionCallInfo fcinfo, const char *funcname,
364 static Datum elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
367 /* semantic action functions for json_array_elements */
368 static void elements_object_start(void *state);
369 static void elements_array_element_start(void *state, bool isnull);
370 static void elements_array_element_end(void *state, bool isnull);
371 static void elements_scalar(void *state, char *token, JsonTokenType tokentype);
373 /* turn a json object into a hash table */
374 static HTAB *get_json_object_as_hash(char *json, int len, const char *funcname);
376 /* semantic actions for populate_array_json */
377 static void populate_array_object_start(void *_state);
378 static void populate_array_array_end(void *_state);
379 static void populate_array_element_start(void *_state, bool isnull);
380 static void populate_array_element_end(void *_state, bool isnull);
381 static void populate_array_scalar(void *_state, char *token, JsonTokenType tokentype);
383 /* semantic action functions for get_json_object_as_hash */
384 static void hash_object_field_start(void *state, char *fname, bool isnull);
385 static void hash_object_field_end(void *state, char *fname, bool isnull);
386 static void hash_array_start(void *state);
387 static void hash_scalar(void *state, char *token, JsonTokenType tokentype);
389 /* semantic action functions for populate_recordset */
390 static void populate_recordset_object_field_start(void *state, char *fname, bool isnull);
391 static void populate_recordset_object_field_end(void *state, char *fname, bool isnull);
392 static void populate_recordset_scalar(void *state, char *token, JsonTokenType tokentype);
393 static void populate_recordset_object_start(void *state);
394 static void populate_recordset_object_end(void *state);
395 static void populate_recordset_array_start(void *state);
396 static void populate_recordset_array_element_start(void *state, bool isnull);
398 /* semantic action functions for json_strip_nulls */
399 static void sn_object_start(void *state);
400 static void sn_object_end(void *state);
401 static void sn_array_start(void *state);
402 static void sn_array_end(void *state);
403 static void sn_object_field_start(void *state, char *fname, bool isnull);
404 static void sn_array_element_start(void *state, bool isnull);
405 static void sn_scalar(void *state, char *token, JsonTokenType tokentype);
407 /* worker functions for populate_record, to_record, populate_recordset and to_recordset */
408 static Datum populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
409 bool have_record_arg);
410 static Datum populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
411 bool have_record_arg);
413 /* helper functions for populate_record[set] */
414 static HeapTupleHeader populate_record(TupleDesc tupdesc, RecordIOData **record_p,
415 HeapTupleHeader defaultval, MemoryContext mcxt,
417 static Datum populate_record_field(ColumnIOData *col, Oid typid, int32 typmod,
418 const char *colname, MemoryContext mcxt,
419 Datum defaultval, JsValue *jsv, bool *isnull);
420 static void JsValueToJsObject(JsValue *jsv, JsObject *jso);
421 static Datum populate_composite(CompositeIOData *io, Oid typid, int32 typmod,
422 const char *colname, MemoryContext mcxt,
423 HeapTupleHeader defaultval, JsValue *jsv);
424 static Datum populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv);
425 static void prepare_column_cache(ColumnIOData *column, Oid typid, int32 typmod,
426 MemoryContext mcxt, bool json);
427 static Datum populate_record_field(ColumnIOData *col, Oid typid, int32 typmod,
428 const char *colname, MemoryContext mcxt, Datum defaultval,
429 JsValue *jsv, bool *isnull);
430 static RecordIOData *allocate_record_info(MemoryContext mcxt, int ncolumns);
431 static bool JsObjectGetField(JsObject *obj, char *field, JsValue *jsv);
432 static void populate_recordset_record(PopulateRecordsetState *state, JsObject *obj);
433 static void populate_array_json(PopulateArrayContext *ctx, char *json, int len);
434 static void populate_array_dim_jsonb(PopulateArrayContext *ctx, JsonbValue *jbv,
436 static void populate_array_report_expected_array(PopulateArrayContext *ctx, int ndim);
437 static void populate_array_assign_ndims(PopulateArrayContext *ctx, int ndims);
438 static void populate_array_check_dimension(PopulateArrayContext *ctx, int ndim);
439 static void populate_array_element(PopulateArrayContext *ctx, int ndim, JsValue *jsv);
440 static Datum populate_array(ArrayIOData *aio, const char *colname,
441 MemoryContext mcxt, JsValue *jsv);
442 static Datum populate_domain(DomainIOData *io, Oid typid, const char *colname,
443 MemoryContext mcxt, JsValue *jsv, bool isnull);
445 /* Worker that takes care of common setup for us */
446 static JsonbValue *findJsonbValueFromContainerLen(JsonbContainer *container,
451 /* functions supporting jsonb_delete, jsonb_set and jsonb_concat */
452 static JsonbValue *IteratorConcat(JsonbIterator **it1, JsonbIterator **it2,
453 JsonbParseState **state);
454 static JsonbValue *setPath(JsonbIterator **it, Datum *path_elems,
455 bool *path_nulls, int path_len,
456 JsonbParseState **st, int level, Jsonb *newval,
458 static void setPathObject(JsonbIterator **it, Datum *path_elems,
459 bool *path_nulls, int path_len, JsonbParseState **st,
461 Jsonb *newval, uint32 npairs, int op_type);
462 static void setPathArray(JsonbIterator **it, Datum *path_elems,
463 bool *path_nulls, int path_len, JsonbParseState **st,
464 int level, Jsonb *newval, uint32 nelems, int op_type);
465 static void addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb);
467 /* function supporting iterate_json_string_values */
468 static void iterate_string_values_scalar(void *state, char *token, JsonTokenType tokentype);
470 /* functions supporting transform_json_string_values */
471 static void transform_string_values_object_start(void *state);
472 static void transform_string_values_object_end(void *state);
473 static void transform_string_values_array_start(void *state);
474 static void transform_string_values_array_end(void *state);
475 static void transform_string_values_object_field_start(void *state, char *fname, bool isnull);
476 static void transform_string_values_array_element_start(void *state, bool isnull);
477 static void transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype);
480 * SQL function json_object_keys
482 * Returns the set of keys for the object argument.
484 * This SRF operates in value-per-call mode. It processes the
485 * object during the first call, and the keys are simply stashed
486 * in an array, whose size is expanded as necessary. This is probably
487 * safe enough for a list of keys of a single object, since they are
488 * limited in size to NAMEDATALEN and the number of keys is unlikely to
489 * be so huge that it has major memory implications.
492 jsonb_object_keys(PG_FUNCTION_ARGS)
494 FuncCallContext *funcctx;
498 if (SRF_IS_FIRSTCALL())
500 MemoryContext oldcontext;
501 Jsonb *jb = PG_GETARG_JSONB(0);
502 bool skipNested = false;
505 JsonbIteratorToken r;
507 if (JB_ROOT_IS_SCALAR(jb))
509 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
510 errmsg("cannot call %s on a scalar",
511 "jsonb_object_keys")));
512 else if (JB_ROOT_IS_ARRAY(jb))
514 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
515 errmsg("cannot call %s on an array",
516 "jsonb_object_keys")));
518 funcctx = SRF_FIRSTCALL_INIT();
519 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
521 state = palloc(sizeof(OkeysState));
523 state->result_size = JB_ROOT_COUNT(jb);
524 state->result_count = 0;
525 state->sent_count = 0;
526 state->result = palloc(state->result_size * sizeof(char *));
528 it = JsonbIteratorInit(&jb->root);
530 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
538 cstr = palloc(v.val.string.len + 1 * sizeof(char));
539 memcpy(cstr, v.val.string.val, v.val.string.len);
540 cstr[v.val.string.len] = '\0';
541 state->result[state->result_count++] = cstr;
545 MemoryContextSwitchTo(oldcontext);
546 funcctx->user_fctx = (void *) state;
549 funcctx = SRF_PERCALL_SETUP();
550 state = (OkeysState *) funcctx->user_fctx;
552 if (state->sent_count < state->result_count)
554 char *nxt = state->result[state->sent_count++];
556 SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt));
559 /* cleanup to reduce or eliminate memory leaks */
560 for (i = 0; i < state->result_count; i++)
561 pfree(state->result[i]);
562 pfree(state->result);
565 SRF_RETURN_DONE(funcctx);
570 json_object_keys(PG_FUNCTION_ARGS)
572 FuncCallContext *funcctx;
576 if (SRF_IS_FIRSTCALL())
578 text *json = PG_GETARG_TEXT_PP(0);
579 JsonLexContext *lex = makeJsonLexContext(json, true);
581 MemoryContext oldcontext;
583 funcctx = SRF_FIRSTCALL_INIT();
584 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
586 state = palloc(sizeof(OkeysState));
587 sem = palloc0(sizeof(JsonSemAction));
590 state->result_size = 256;
591 state->result_count = 0;
592 state->sent_count = 0;
593 state->result = palloc(256 * sizeof(char *));
595 sem->semstate = (void *) state;
596 sem->array_start = okeys_array_start;
597 sem->scalar = okeys_scalar;
598 sem->object_field_start = okeys_object_field_start;
599 /* remainder are all NULL, courtesy of palloc0 above */
601 pg_parse_json(lex, sem);
602 /* keys are now in state->result */
604 pfree(lex->strval->data);
609 MemoryContextSwitchTo(oldcontext);
610 funcctx->user_fctx = (void *) state;
613 funcctx = SRF_PERCALL_SETUP();
614 state = (OkeysState *) funcctx->user_fctx;
616 if (state->sent_count < state->result_count)
618 char *nxt = state->result[state->sent_count++];
620 SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt));
623 /* cleanup to reduce or eliminate memory leaks */
624 for (i = 0; i < state->result_count; i++)
625 pfree(state->result[i]);
626 pfree(state->result);
629 SRF_RETURN_DONE(funcctx);
633 okeys_object_field_start(void *state, char *fname, bool isnull)
635 OkeysState *_state = (OkeysState *) state;
637 /* only collecting keys for the top level object */
638 if (_state->lex->lex_level != 1)
641 /* enlarge result array if necessary */
642 if (_state->result_count >= _state->result_size)
644 _state->result_size *= 2;
645 _state->result = (char **)
646 repalloc(_state->result, sizeof(char *) * _state->result_size);
649 /* save a copy of the field name */
650 _state->result[_state->result_count++] = pstrdup(fname);
654 okeys_array_start(void *state)
656 OkeysState *_state = (OkeysState *) state;
658 /* top level must be a json object */
659 if (_state->lex->lex_level == 0)
661 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
662 errmsg("cannot call %s on an array",
663 "json_object_keys")));
667 okeys_scalar(void *state, char *token, JsonTokenType tokentype)
669 OkeysState *_state = (OkeysState *) state;
671 /* top level must be a json object */
672 if (_state->lex->lex_level == 0)
674 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
675 errmsg("cannot call %s on a scalar",
676 "json_object_keys")));
680 * json and jsonb getter functions
681 * these implement the -> ->> #> and #>> operators
682 * and the json{b?}_extract_path*(json, text, ...) functions
687 json_object_field(PG_FUNCTION_ARGS)
689 text *json = PG_GETARG_TEXT_PP(0);
690 text *fname = PG_GETARG_TEXT_PP(1);
691 char *fnamestr = text_to_cstring(fname);
694 result = get_worker(json, &fnamestr, NULL, 1, false);
697 PG_RETURN_TEXT_P(result);
703 jsonb_object_field(PG_FUNCTION_ARGS)
705 Jsonb *jb = PG_GETARG_JSONB(0);
706 text *key = PG_GETARG_TEXT_PP(1);
709 if (!JB_ROOT_IS_OBJECT(jb))
712 v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT,
714 VARSIZE_ANY_EXHDR(key));
717 PG_RETURN_JSONB(JsonbValueToJsonb(v));
723 json_object_field_text(PG_FUNCTION_ARGS)
725 text *json = PG_GETARG_TEXT_PP(0);
726 text *fname = PG_GETARG_TEXT_PP(1);
727 char *fnamestr = text_to_cstring(fname);
730 result = get_worker(json, &fnamestr, NULL, 1, true);
733 PG_RETURN_TEXT_P(result);
739 jsonb_object_field_text(PG_FUNCTION_ARGS)
741 Jsonb *jb = PG_GETARG_JSONB(0);
742 text *key = PG_GETARG_TEXT_PP(1);
745 if (!JB_ROOT_IS_OBJECT(jb))
748 v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT,
750 VARSIZE_ANY_EXHDR(key));
761 result = cstring_to_text(v->val.boolean ? "true" : "false");
764 result = cstring_to_text_with_len(v->val.string.val, v->val.string.len);
767 result = cstring_to_text(DatumGetCString(DirectFunctionCall1(numeric_out,
768 PointerGetDatum(v->val.numeric))));
772 StringInfo jtext = makeStringInfo();
774 (void) JsonbToCString(jtext, v->val.binary.data, -1);
775 result = cstring_to_text_with_len(jtext->data, jtext->len);
779 elog(ERROR, "unrecognized jsonb type: %d", (int) v->type);
783 PG_RETURN_TEXT_P(result);
790 json_array_element(PG_FUNCTION_ARGS)
792 text *json = PG_GETARG_TEXT_PP(0);
793 int element = PG_GETARG_INT32(1);
796 result = get_worker(json, NULL, &element, 1, false);
799 PG_RETURN_TEXT_P(result);
805 jsonb_array_element(PG_FUNCTION_ARGS)
807 Jsonb *jb = PG_GETARG_JSONB(0);
808 int element = PG_GETARG_INT32(1);
811 if (!JB_ROOT_IS_ARRAY(jb))
814 /* Handle negative subscript */
817 uint32 nelements = JB_ROOT_COUNT(jb);
819 if (-element > nelements)
822 element += nelements;
825 v = getIthJsonbValueFromContainer(&jb->root, element);
827 PG_RETURN_JSONB(JsonbValueToJsonb(v));
833 json_array_element_text(PG_FUNCTION_ARGS)
835 text *json = PG_GETARG_TEXT_PP(0);
836 int element = PG_GETARG_INT32(1);
839 result = get_worker(json, NULL, &element, 1, true);
842 PG_RETURN_TEXT_P(result);
848 jsonb_array_element_text(PG_FUNCTION_ARGS)
850 Jsonb *jb = PG_GETARG_JSONB(0);
851 int element = PG_GETARG_INT32(1);
854 if (!JB_ROOT_IS_ARRAY(jb))
857 /* Handle negative subscript */
860 uint32 nelements = JB_ROOT_COUNT(jb);
862 if (-element > nelements)
865 element += nelements;
868 v = getIthJsonbValueFromContainer(&jb->root, element);
878 result = cstring_to_text(v->val.boolean ? "true" : "false");
881 result = cstring_to_text_with_len(v->val.string.val, v->val.string.len);
884 result = cstring_to_text(DatumGetCString(DirectFunctionCall1(numeric_out,
885 PointerGetDatum(v->val.numeric))));
889 StringInfo jtext = makeStringInfo();
891 (void) JsonbToCString(jtext, v->val.binary.data, -1);
892 result = cstring_to_text_with_len(jtext->data, jtext->len);
896 elog(ERROR, "unrecognized jsonb type: %d", (int) v->type);
900 PG_RETURN_TEXT_P(result);
907 json_extract_path(PG_FUNCTION_ARGS)
909 return get_path_all(fcinfo, false);
913 json_extract_path_text(PG_FUNCTION_ARGS)
915 return get_path_all(fcinfo, true);
919 * common routine for extract_path functions
922 get_path_all(FunctionCallInfo fcinfo, bool as_text)
924 text *json = PG_GETARG_TEXT_PP(0);
925 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
935 * If the array contains any null elements, return NULL, on the grounds
936 * that you'd have gotten NULL if any RHS value were NULL in a nested
937 * series of applications of the -> operator. (Note: because we also
938 * return NULL for error cases such as no-such-field, this is true
939 * regardless of the contents of the rest of the array.)
941 if (array_contains_nulls(path))
944 deconstruct_array(path, TEXTOID, -1, false, 'i',
945 &pathtext, &pathnulls, &npath);
947 tpath = palloc(npath * sizeof(char *));
948 ipath = palloc(npath * sizeof(int));
950 for (i = 0; i < npath; i++)
952 Assert(!pathnulls[i]);
953 tpath[i] = TextDatumGetCString(pathtext[i]);
956 * we have no idea at this stage what structure the document is so
957 * just convert anything in the path that we can to an integer and set
958 * all the other integers to INT_MIN which will never match.
960 if (*tpath[i] != '\0')
966 ind = strtol(tpath[i], &endptr, 10);
967 if (*endptr == '\0' && errno == 0 && ind <= INT_MAX && ind >= INT_MIN)
968 ipath[i] = (int) ind;
976 result = get_worker(json, tpath, ipath, npath, as_text);
979 PG_RETURN_TEXT_P(result);
987 * common worker for all the json getter functions
989 * json: JSON object (in text form)
990 * tpath[]: field name(s) to extract
991 * ipath[]: array index(es) (zero-based) to extract, accepts negatives
992 * npath: length of tpath[] and/or ipath[]
993 * normalize_results: true to de-escape string and null scalars
995 * tpath can be NULL, or any one tpath[] entry can be NULL, if an object
996 * field is not to be matched at that nesting level. Similarly, ipath can
997 * be NULL, or any one ipath[] entry can be INT_MIN if an array element is
998 * not to be matched at that nesting level (a json datum should never be
999 * large enough to have -INT_MIN elements due to MaxAllocSize restriction).
1002 get_worker(text *json,
1006 bool normalize_results)
1008 JsonLexContext *lex = makeJsonLexContext(json, true);
1009 JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
1010 GetState *state = palloc0(sizeof(GetState));
1015 /* is it "_as_text" variant? */
1016 state->normalize_results = normalize_results;
1017 state->npath = npath;
1018 state->path_names = tpath;
1019 state->path_indexes = ipath;
1020 state->pathok = palloc0(sizeof(bool) * npath);
1021 state->array_cur_index = palloc(sizeof(int) * npath);
1024 state->pathok[0] = true;
1026 sem->semstate = (void *) state;
1029 * Not all variants need all the semantic routines. Only set the ones that
1030 * are actually needed for maximum efficiency.
1032 sem->scalar = get_scalar;
1035 sem->object_start = get_object_start;
1036 sem->object_end = get_object_end;
1037 sem->array_start = get_array_start;
1038 sem->array_end = get_array_end;
1042 sem->object_field_start = get_object_field_start;
1043 sem->object_field_end = get_object_field_end;
1047 sem->array_start = get_array_start;
1048 sem->array_element_start = get_array_element_start;
1049 sem->array_element_end = get_array_element_end;
1052 pg_parse_json(lex, sem);
1054 return state->tresult;
1058 get_object_start(void *state)
1060 GetState *_state = (GetState *) state;
1061 int lex_level = _state->lex->lex_level;
1063 if (lex_level == 0 && _state->npath == 0)
1066 * Special case: we should match the entire object. We only need this
1067 * at outermost level because at nested levels the match will have
1068 * been started by the outer field or array element callback.
1070 _state->result_start = _state->lex->token_start;
1075 get_object_end(void *state)
1077 GetState *_state = (GetState *) state;
1078 int lex_level = _state->lex->lex_level;
1080 if (lex_level == 0 && _state->npath == 0)
1082 /* Special case: return the entire object */
1083 char *start = _state->result_start;
1084 int len = _state->lex->prev_token_terminator - start;
1086 _state->tresult = cstring_to_text_with_len(start, len);
1091 get_object_field_start(void *state, char *fname, bool isnull)
1093 GetState *_state = (GetState *) state;
1094 bool get_next = false;
1095 int lex_level = _state->lex->lex_level;
1097 if (lex_level <= _state->npath &&
1098 _state->pathok[lex_level - 1] &&
1099 _state->path_names != NULL &&
1100 _state->path_names[lex_level - 1] != NULL &&
1101 strcmp(fname, _state->path_names[lex_level - 1]) == 0)
1103 if (lex_level < _state->npath)
1105 /* if not at end of path just mark path ok */
1106 _state->pathok[lex_level] = true;
1110 /* end of path, so we want this value */
1117 /* this object overrides any previous matching object */
1118 _state->tresult = NULL;
1119 _state->result_start = NULL;
1121 if (_state->normalize_results &&
1122 _state->lex->token_type == JSON_TOKEN_STRING)
1124 /* for as_text variants, tell get_scalar to set it for us */
1125 _state->next_scalar = true;
1129 /* for non-as_text variants, just note the json starting point */
1130 _state->result_start = _state->lex->token_start;
1136 get_object_field_end(void *state, char *fname, bool isnull)
1138 GetState *_state = (GetState *) state;
1139 bool get_last = false;
1140 int lex_level = _state->lex->lex_level;
1142 /* same tests as in get_object_field_start */
1143 if (lex_level <= _state->npath &&
1144 _state->pathok[lex_level - 1] &&
1145 _state->path_names != NULL &&
1146 _state->path_names[lex_level - 1] != NULL &&
1147 strcmp(fname, _state->path_names[lex_level - 1]) == 0)
1149 if (lex_level < _state->npath)
1151 /* done with this field so reset pathok */
1152 _state->pathok[lex_level] = false;
1156 /* end of path, so we want this value */
1161 /* for as_text scalar case, our work is already done */
1162 if (get_last && _state->result_start != NULL)
1165 * make a text object from the string from the prevously noted json
1166 * start up to the end of the previous token (the lexer is by now
1167 * ahead of us on whatever came after what we're interested in).
1169 if (isnull && _state->normalize_results)
1170 _state->tresult = (text *) NULL;
1173 char *start = _state->result_start;
1174 int len = _state->lex->prev_token_terminator - start;
1176 _state->tresult = cstring_to_text_with_len(start, len);
1179 /* this should be unnecessary but let's do it for cleanliness: */
1180 _state->result_start = NULL;
1185 get_array_start(void *state)
1187 GetState *_state = (GetState *) state;
1188 int lex_level = _state->lex->lex_level;
1190 if (lex_level < _state->npath)
1192 /* Initialize counting of elements in this array */
1193 _state->array_cur_index[lex_level] = -1;
1195 /* INT_MIN value is reserved to represent invalid subscript */
1196 if (_state->path_indexes[lex_level] < 0 &&
1197 _state->path_indexes[lex_level] != INT_MIN)
1199 /* Negative subscript -- convert to positive-wise subscript */
1200 int nelements = json_count_array_elements(_state->lex);
1202 if (-_state->path_indexes[lex_level] <= nelements)
1203 _state->path_indexes[lex_level] += nelements;
1206 else if (lex_level == 0 && _state->npath == 0)
1209 * Special case: we should match the entire array. We only need this
1210 * at the outermost level because at nested levels the match will have
1211 * been started by the outer field or array element callback.
1213 _state->result_start = _state->lex->token_start;
1218 get_array_end(void *state)
1220 GetState *_state = (GetState *) state;
1221 int lex_level = _state->lex->lex_level;
1223 if (lex_level == 0 && _state->npath == 0)
1225 /* Special case: return the entire array */
1226 char *start = _state->result_start;
1227 int len = _state->lex->prev_token_terminator - start;
1229 _state->tresult = cstring_to_text_with_len(start, len);
1234 get_array_element_start(void *state, bool isnull)
1236 GetState *_state = (GetState *) state;
1237 bool get_next = false;
1238 int lex_level = _state->lex->lex_level;
1240 /* Update array element counter */
1241 if (lex_level <= _state->npath)
1242 _state->array_cur_index[lex_level - 1]++;
1244 if (lex_level <= _state->npath &&
1245 _state->pathok[lex_level - 1] &&
1246 _state->path_indexes != NULL &&
1247 _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1])
1249 if (lex_level < _state->npath)
1251 /* if not at end of path just mark path ok */
1252 _state->pathok[lex_level] = true;
1256 /* end of path, so we want this value */
1261 /* same logic as for objects */
1264 _state->tresult = NULL;
1265 _state->result_start = NULL;
1267 if (_state->normalize_results &&
1268 _state->lex->token_type == JSON_TOKEN_STRING)
1270 _state->next_scalar = true;
1274 _state->result_start = _state->lex->token_start;
1280 get_array_element_end(void *state, bool isnull)
1282 GetState *_state = (GetState *) state;
1283 bool get_last = false;
1284 int lex_level = _state->lex->lex_level;
1286 /* same tests as in get_array_element_start */
1287 if (lex_level <= _state->npath &&
1288 _state->pathok[lex_level - 1] &&
1289 _state->path_indexes != NULL &&
1290 _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1])
1292 if (lex_level < _state->npath)
1294 /* done with this element so reset pathok */
1295 _state->pathok[lex_level] = false;
1299 /* end of path, so we want this value */
1304 /* same logic as for objects */
1305 if (get_last && _state->result_start != NULL)
1307 if (isnull && _state->normalize_results)
1308 _state->tresult = (text *) NULL;
1311 char *start = _state->result_start;
1312 int len = _state->lex->prev_token_terminator - start;
1314 _state->tresult = cstring_to_text_with_len(start, len);
1317 _state->result_start = NULL;
1322 get_scalar(void *state, char *token, JsonTokenType tokentype)
1324 GetState *_state = (GetState *) state;
1325 int lex_level = _state->lex->lex_level;
1327 /* Check for whole-object match */
1328 if (lex_level == 0 && _state->npath == 0)
1330 if (_state->normalize_results && tokentype == JSON_TOKEN_STRING)
1332 /* we want the de-escaped string */
1333 _state->next_scalar = true;
1335 else if (_state->normalize_results && tokentype == JSON_TOKEN_NULL)
1337 _state->tresult = (text *) NULL;
1342 * This is a bit hokey: we will suppress whitespace after the
1343 * scalar token, but not whitespace before it. Probably not worth
1344 * doing our own space-skipping to avoid that.
1346 char *start = _state->lex->input;
1347 int len = _state->lex->prev_token_terminator - start;
1349 _state->tresult = cstring_to_text_with_len(start, len);
1353 if (_state->next_scalar)
1355 /* a de-escaped text value is wanted, so supply it */
1356 _state->tresult = cstring_to_text(token);
1357 /* make sure the next call to get_scalar doesn't overwrite it */
1358 _state->next_scalar = false;
1363 jsonb_extract_path(PG_FUNCTION_ARGS)
1365 return get_jsonb_path_all(fcinfo, false);
1369 jsonb_extract_path_text(PG_FUNCTION_ARGS)
1371 return get_jsonb_path_all(fcinfo, true);
1375 get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
1377 Jsonb *jb = PG_GETARG_JSONB(0);
1378 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
1384 bool have_object = false,
1386 JsonbValue *jbvp = NULL;
1388 JsonbContainer *container;
1391 * If the array contains any null elements, return NULL, on the grounds
1392 * that you'd have gotten NULL if any RHS value were NULL in a nested
1393 * series of applications of the -> operator. (Note: because we also
1394 * return NULL for error cases such as no-such-field, this is true
1395 * regardless of the contents of the rest of the array.)
1397 if (array_contains_nulls(path))
1400 deconstruct_array(path, TEXTOID, -1, false, 'i',
1401 &pathtext, &pathnulls, &npath);
1403 /* Identify whether we have object, array, or scalar at top-level */
1404 container = &jb->root;
1406 if (JB_ROOT_IS_OBJECT(jb))
1408 else if (JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb))
1412 Assert(JB_ROOT_IS_ARRAY(jb) && JB_ROOT_IS_SCALAR(jb));
1413 /* Extract the scalar value, if it is what we'll return */
1415 jbvp = getIthJsonbValueFromContainer(container, 0);
1419 * If the array is empty, return the entire LHS object, on the grounds
1420 * that we should do zero field or element extractions. For the
1421 * non-scalar case we can just hand back the object without much work. For
1422 * the scalar case, fall through and deal with the value below the loop.
1423 * (This inconsistency arises because there's no easy way to generate a
1424 * JsonbValue directly for root-level containers.)
1426 if (npath <= 0 && jbvp == NULL)
1430 PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL,
1436 /* not text mode - just hand back the jsonb */
1437 PG_RETURN_JSONB(jb);
1441 for (i = 0; i < npath; i++)
1445 jbvp = findJsonbValueFromContainerLen(container,
1447 VARDATA(pathtext[i]),
1448 VARSIZE(pathtext[i]) - VARHDRSZ);
1450 else if (have_array)
1454 char *indextext = TextDatumGetCString(pathtext[i]);
1458 lindex = strtol(indextext, &endptr, 10);
1459 if (endptr == indextext || *endptr != '\0' || errno != 0 ||
1460 lindex > INT_MAX || lindex < INT_MIN)
1465 index = (uint32) lindex;
1469 /* Handle negative subscript */
1472 /* Container must be array, but make sure */
1473 if (!JsonContainerIsArray(container))
1474 elog(ERROR, "not a jsonb array");
1476 nelements = JsonContainerSize(container);
1478 if (-lindex > nelements)
1481 index = nelements + lindex;
1484 jbvp = getIthJsonbValueFromContainer(container, index);
1488 /* scalar, extraction yields a null */
1494 else if (i == npath - 1)
1497 if (jbvp->type == jbvBinary)
1499 JsonbIterator *it = JsonbIteratorInit((JsonbContainer *) jbvp->val.binary.data);
1500 JsonbIteratorToken r;
1502 r = JsonbIteratorNext(&it, &tv, true);
1503 container = (JsonbContainer *) jbvp->val.binary.data;
1504 have_object = r == WJB_BEGIN_OBJECT;
1505 have_array = r == WJB_BEGIN_ARRAY;
1509 have_object = jbvp->type == jbvObject;
1510 have_array = jbvp->type == jbvArray;
1516 /* special-case outputs for string and null values */
1517 if (jbvp->type == jbvString)
1518 PG_RETURN_TEXT_P(cstring_to_text_with_len(jbvp->val.string.val,
1519 jbvp->val.string.len));
1520 if (jbvp->type == jbvNull)
1524 res = JsonbValueToJsonb(jbvp);
1528 PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL,
1534 /* not text mode - just hand back the jsonb */
1535 PG_RETURN_JSONB(res);
1540 * SQL function json_array_length(json) -> int
1543 json_array_length(PG_FUNCTION_ARGS)
1545 text *json = PG_GETARG_TEXT_PP(0);
1547 JsonLexContext *lex;
1550 lex = makeJsonLexContext(json, false);
1551 state = palloc0(sizeof(AlenState));
1552 sem = palloc0(sizeof(JsonSemAction));
1554 /* palloc0 does this for us */
1560 sem->semstate = (void *) state;
1561 sem->object_start = alen_object_start;
1562 sem->scalar = alen_scalar;
1563 sem->array_element_start = alen_array_element_start;
1565 pg_parse_json(lex, sem);
1567 PG_RETURN_INT32(state->count);
1571 jsonb_array_length(PG_FUNCTION_ARGS)
1573 Jsonb *jb = PG_GETARG_JSONB(0);
1575 if (JB_ROOT_IS_SCALAR(jb))
1577 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1578 errmsg("cannot get array length of a scalar")));
1579 else if (!JB_ROOT_IS_ARRAY(jb))
1581 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1582 errmsg("cannot get array length of a non-array")));
1584 PG_RETURN_INT32(JB_ROOT_COUNT(jb));
1588 * These next two checks ensure that the json is an array (since it can't be
1589 * a scalar or an object).
1593 alen_object_start(void *state)
1595 AlenState *_state = (AlenState *) state;
1597 /* json structure check */
1598 if (_state->lex->lex_level == 0)
1600 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1601 errmsg("cannot get array length of a non-array")));
1605 alen_scalar(void *state, char *token, JsonTokenType tokentype)
1607 AlenState *_state = (AlenState *) state;
1609 /* json structure check */
1610 if (_state->lex->lex_level == 0)
1612 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1613 errmsg("cannot get array length of a scalar")));
1617 alen_array_element_start(void *state, bool isnull)
1619 AlenState *_state = (AlenState *) state;
1621 /* just count up all the level 1 elements */
1622 if (_state->lex->lex_level == 1)
1627 * SQL function json_each and json_each_text
1629 * decompose a json object into key value pairs.
1631 * Unlike json_object_keys() these SRFs operate in materialize mode,
1632 * stashing results into a Tuplestore object as they go.
1633 * The construction of tuples is done using a temporary memory context
1634 * that is cleared out after each tuple is built.
1637 json_each(PG_FUNCTION_ARGS)
1639 return each_worker(fcinfo, false);
1643 jsonb_each(PG_FUNCTION_ARGS)
1645 return each_worker_jsonb(fcinfo, "jsonb_each", false);
1649 json_each_text(PG_FUNCTION_ARGS)
1651 return each_worker(fcinfo, true);
1655 jsonb_each_text(PG_FUNCTION_ARGS)
1657 return each_worker_jsonb(fcinfo, "jsonb_each_text", true);
1661 each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
1663 Jsonb *jb = PG_GETARG_JSONB(0);
1665 Tuplestorestate *tuple_store;
1667 TupleDesc ret_tdesc;
1668 MemoryContext old_cxt,
1670 bool skipNested = false;
1673 JsonbIteratorToken r;
1675 if (!JB_ROOT_IS_OBJECT(jb))
1677 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1678 errmsg("cannot call %s on a non-object",
1681 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1683 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1684 (rsi->allowedModes & SFRM_Materialize) == 0 ||
1685 rsi->expectedDesc == NULL)
1687 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1688 errmsg("set-valued function called in context that "
1689 "cannot accept a set")));
1691 rsi->returnMode = SFRM_Materialize;
1693 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1695 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1696 errmsg("function returning record called in context "
1697 "that cannot accept type record")));
1699 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
1701 ret_tdesc = CreateTupleDescCopy(tupdesc);
1702 BlessTupleDesc(ret_tdesc);
1704 tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
1707 MemoryContextSwitchTo(old_cxt);
1709 tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
1710 "jsonb_each temporary cxt",
1711 ALLOCSET_DEFAULT_SIZES);
1713 it = JsonbIteratorInit(&jb->root);
1715 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
1724 bool nulls[2] = {false, false};
1726 /* Use the tmp context so we can clean up after each tuple is done */
1727 old_cxt = MemoryContextSwitchTo(tmp_cxt);
1729 key = cstring_to_text_with_len(v.val.string.val, v.val.string.len);
1732 * The next thing the iterator fetches should be the value, no
1733 * matter what shape it is.
1735 r = JsonbIteratorNext(&it, &v, skipNested);
1737 values[0] = PointerGetDatum(key);
1741 if (v.type == jbvNull)
1743 /* a json null is an sql null in text mode */
1745 values[1] = (Datum) NULL;
1751 if (v.type == jbvString)
1753 /* In text mode, scalar strings should be dequoted */
1754 sv = cstring_to_text_with_len(v.val.string.val, v.val.string.len);
1758 /* Turn anything else into a json string */
1759 StringInfo jtext = makeStringInfo();
1760 Jsonb *jb = JsonbValueToJsonb(&v);
1762 (void) JsonbToCString(jtext, &jb->root, 0);
1763 sv = cstring_to_text_with_len(jtext->data, jtext->len);
1766 values[1] = PointerGetDatum(sv);
1771 /* Not in text mode, just return the Jsonb */
1772 Jsonb *val = JsonbValueToJsonb(&v);
1774 values[1] = PointerGetDatum(val);
1777 tuple = heap_form_tuple(ret_tdesc, values, nulls);
1779 tuplestore_puttuple(tuple_store, tuple);
1781 /* clean up and switch back */
1782 MemoryContextSwitchTo(old_cxt);
1783 MemoryContextReset(tmp_cxt);
1787 MemoryContextDelete(tmp_cxt);
1789 rsi->setResult = tuple_store;
1790 rsi->setDesc = ret_tdesc;
1797 each_worker(FunctionCallInfo fcinfo, bool as_text)
1799 text *json = PG_GETARG_TEXT_PP(0);
1800 JsonLexContext *lex;
1803 MemoryContext old_cxt;
1807 lex = makeJsonLexContext(json, true);
1808 state = palloc0(sizeof(EachState));
1809 sem = palloc0(sizeof(JsonSemAction));
1811 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1813 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1814 (rsi->allowedModes & SFRM_Materialize) == 0 ||
1815 rsi->expectedDesc == NULL)
1817 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1818 errmsg("set-valued function called in context that "
1819 "cannot accept a set")));
1821 rsi->returnMode = SFRM_Materialize;
1823 (void) get_call_result_type(fcinfo, NULL, &tupdesc);
1825 /* make these in a sufficiently long-lived memory context */
1826 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
1828 state->ret_tdesc = CreateTupleDescCopy(tupdesc);
1829 BlessTupleDesc(state->ret_tdesc);
1830 state->tuple_store =
1831 tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
1834 MemoryContextSwitchTo(old_cxt);
1836 sem->semstate = (void *) state;
1837 sem->array_start = each_array_start;
1838 sem->scalar = each_scalar;
1839 sem->object_field_start = each_object_field_start;
1840 sem->object_field_end = each_object_field_end;
1842 state->normalize_results = as_text;
1843 state->next_scalar = false;
1845 state->tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
1846 "json_each temporary cxt",
1847 ALLOCSET_DEFAULT_SIZES);
1849 pg_parse_json(lex, sem);
1851 MemoryContextDelete(state->tmp_cxt);
1853 rsi->setResult = state->tuple_store;
1854 rsi->setDesc = state->ret_tdesc;
1861 each_object_field_start(void *state, char *fname, bool isnull)
1863 EachState *_state = (EachState *) state;
1865 /* save a pointer to where the value starts */
1866 if (_state->lex->lex_level == 1)
1869 * next_scalar will be reset in the object_field_end handler, and
1870 * since we know the value is a scalar there is no danger of it being
1871 * on while recursing down the tree.
1873 if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING)
1874 _state->next_scalar = true;
1876 _state->result_start = _state->lex->token_start;
1881 each_object_field_end(void *state, char *fname, bool isnull)
1883 EachState *_state = (EachState *) state;
1884 MemoryContext old_cxt;
1889 bool nulls[2] = {false, false};
1891 /* skip over nested objects */
1892 if (_state->lex->lex_level != 1)
1895 /* use the tmp context so we can clean up after each tuple is done */
1896 old_cxt = MemoryContextSwitchTo(_state->tmp_cxt);
1898 values[0] = CStringGetTextDatum(fname);
1900 if (isnull && _state->normalize_results)
1903 values[1] = (Datum) 0;
1905 else if (_state->next_scalar)
1907 values[1] = CStringGetTextDatum(_state->normalized_scalar);
1908 _state->next_scalar = false;
1912 len = _state->lex->prev_token_terminator - _state->result_start;
1913 val = cstring_to_text_with_len(_state->result_start, len);
1914 values[1] = PointerGetDatum(val);
1917 tuple = heap_form_tuple(_state->ret_tdesc, values, nulls);
1919 tuplestore_puttuple(_state->tuple_store, tuple);
1921 /* clean up and switch back */
1922 MemoryContextSwitchTo(old_cxt);
1923 MemoryContextReset(_state->tmp_cxt);
1927 each_array_start(void *state)
1929 EachState *_state = (EachState *) state;
1931 /* json structure check */
1932 if (_state->lex->lex_level == 0)
1934 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1935 errmsg("cannot deconstruct an array as an object")));
1939 each_scalar(void *state, char *token, JsonTokenType tokentype)
1941 EachState *_state = (EachState *) state;
1943 /* json structure check */
1944 if (_state->lex->lex_level == 0)
1946 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1947 errmsg("cannot deconstruct a scalar")));
1949 /* supply de-escaped value if required */
1950 if (_state->next_scalar)
1951 _state->normalized_scalar = token;
1955 * SQL functions json_array_elements and json_array_elements_text
1957 * get the elements from a json array
1959 * a lot of this processing is similar to the json_each* functions
1963 jsonb_array_elements(PG_FUNCTION_ARGS)
1965 return elements_worker_jsonb(fcinfo, "jsonb_array_elements", false);
1969 jsonb_array_elements_text(PG_FUNCTION_ARGS)
1971 return elements_worker_jsonb(fcinfo, "jsonb_array_elements_text", true);
1975 elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
1978 Jsonb *jb = PG_GETARG_JSONB(0);
1980 Tuplestorestate *tuple_store;
1982 TupleDesc ret_tdesc;
1983 MemoryContext old_cxt,
1985 bool skipNested = false;
1988 JsonbIteratorToken r;
1990 if (JB_ROOT_IS_SCALAR(jb))
1992 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1993 errmsg("cannot extract elements from a scalar")));
1994 else if (!JB_ROOT_IS_ARRAY(jb))
1996 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1997 errmsg("cannot extract elements from an object")));
1999 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
2001 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
2002 (rsi->allowedModes & SFRM_Materialize) == 0 ||
2003 rsi->expectedDesc == NULL)
2005 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2006 errmsg("set-valued function called in context that "
2007 "cannot accept a set")));
2009 rsi->returnMode = SFRM_Materialize;
2011 /* it's a simple type, so don't use get_call_result_type() */
2012 tupdesc = rsi->expectedDesc;
2014 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
2016 ret_tdesc = CreateTupleDescCopy(tupdesc);
2017 BlessTupleDesc(ret_tdesc);
2019 tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
2022 MemoryContextSwitchTo(old_cxt);
2024 tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
2025 "jsonb_array_elements temporary cxt",
2026 ALLOCSET_DEFAULT_SIZES);
2028 it = JsonbIteratorInit(&jb->root);
2030 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
2038 bool nulls[1] = {false};
2040 /* use the tmp context so we can clean up after each tuple is done */
2041 old_cxt = MemoryContextSwitchTo(tmp_cxt);
2045 Jsonb *val = JsonbValueToJsonb(&v);
2047 values[0] = PointerGetDatum(val);
2051 if (v.type == jbvNull)
2053 /* a json null is an sql null in text mode */
2055 values[0] = (Datum) NULL;
2061 if (v.type == jbvString)
2063 /* in text mode scalar strings should be dequoted */
2064 sv = cstring_to_text_with_len(v.val.string.val, v.val.string.len);
2068 /* turn anything else into a json string */
2069 StringInfo jtext = makeStringInfo();
2070 Jsonb *jb = JsonbValueToJsonb(&v);
2072 (void) JsonbToCString(jtext, &jb->root, 0);
2073 sv = cstring_to_text_with_len(jtext->data, jtext->len);
2076 values[0] = PointerGetDatum(sv);
2080 tuple = heap_form_tuple(ret_tdesc, values, nulls);
2082 tuplestore_puttuple(tuple_store, tuple);
2084 /* clean up and switch back */
2085 MemoryContextSwitchTo(old_cxt);
2086 MemoryContextReset(tmp_cxt);
2090 MemoryContextDelete(tmp_cxt);
2092 rsi->setResult = tuple_store;
2093 rsi->setDesc = ret_tdesc;
2099 json_array_elements(PG_FUNCTION_ARGS)
2101 return elements_worker(fcinfo, "json_array_elements", false);
2105 json_array_elements_text(PG_FUNCTION_ARGS)
2107 return elements_worker(fcinfo, "json_array_elements_text", true);
2111 elements_worker(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
2113 text *json = PG_GETARG_TEXT_PP(0);
2115 /* elements only needs escaped strings when as_text */
2116 JsonLexContext *lex = makeJsonLexContext(json, as_text);
2119 MemoryContext old_cxt;
2121 ElementsState *state;
2123 state = palloc0(sizeof(ElementsState));
2124 sem = palloc0(sizeof(JsonSemAction));
2126 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
2128 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
2129 (rsi->allowedModes & SFRM_Materialize) == 0 ||
2130 rsi->expectedDesc == NULL)
2132 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2133 errmsg("set-valued function called in context that "
2134 "cannot accept a set")));
2136 rsi->returnMode = SFRM_Materialize;
2138 /* it's a simple type, so don't use get_call_result_type() */
2139 tupdesc = rsi->expectedDesc;
2141 /* make these in a sufficiently long-lived memory context */
2142 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
2144 state->ret_tdesc = CreateTupleDescCopy(tupdesc);
2145 BlessTupleDesc(state->ret_tdesc);
2146 state->tuple_store =
2147 tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
2150 MemoryContextSwitchTo(old_cxt);
2152 sem->semstate = (void *) state;
2153 sem->object_start = elements_object_start;
2154 sem->scalar = elements_scalar;
2155 sem->array_element_start = elements_array_element_start;
2156 sem->array_element_end = elements_array_element_end;
2158 state->function_name = funcname;
2159 state->normalize_results = as_text;
2160 state->next_scalar = false;
2162 state->tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
2163 "json_array_elements temporary cxt",
2164 ALLOCSET_DEFAULT_SIZES);
2166 pg_parse_json(lex, sem);
2168 MemoryContextDelete(state->tmp_cxt);
2170 rsi->setResult = state->tuple_store;
2171 rsi->setDesc = state->ret_tdesc;
2177 elements_array_element_start(void *state, bool isnull)
2179 ElementsState *_state = (ElementsState *) state;
2181 /* save a pointer to where the value starts */
2182 if (_state->lex->lex_level == 1)
2185 * next_scalar will be reset in the array_element_end handler, and
2186 * since we know the value is a scalar there is no danger of it being
2187 * on while recursing down the tree.
2189 if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING)
2190 _state->next_scalar = true;
2192 _state->result_start = _state->lex->token_start;
2197 elements_array_element_end(void *state, bool isnull)
2199 ElementsState *_state = (ElementsState *) state;
2200 MemoryContext old_cxt;
2205 bool nulls[1] = {false};
2207 /* skip over nested objects */
2208 if (_state->lex->lex_level != 1)
2211 /* use the tmp context so we can clean up after each tuple is done */
2212 old_cxt = MemoryContextSwitchTo(_state->tmp_cxt);
2214 if (isnull && _state->normalize_results)
2217 values[0] = (Datum) NULL;
2219 else if (_state->next_scalar)
2221 values[0] = CStringGetTextDatum(_state->normalized_scalar);
2222 _state->next_scalar = false;
2226 len = _state->lex->prev_token_terminator - _state->result_start;
2227 val = cstring_to_text_with_len(_state->result_start, len);
2228 values[0] = PointerGetDatum(val);
2231 tuple = heap_form_tuple(_state->ret_tdesc, values, nulls);
2233 tuplestore_puttuple(_state->tuple_store, tuple);
2235 /* clean up and switch back */
2236 MemoryContextSwitchTo(old_cxt);
2237 MemoryContextReset(_state->tmp_cxt);
2241 elements_object_start(void *state)
2243 ElementsState *_state = (ElementsState *) state;
2245 /* json structure check */
2246 if (_state->lex->lex_level == 0)
2248 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2249 errmsg("cannot call %s on a non-array",
2250 _state->function_name)));
2254 elements_scalar(void *state, char *token, JsonTokenType tokentype)
2256 ElementsState *_state = (ElementsState *) state;
2258 /* json structure check */
2259 if (_state->lex->lex_level == 0)
2261 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2262 errmsg("cannot call %s on a scalar",
2263 _state->function_name)));
2265 /* supply de-escaped value if required */
2266 if (_state->next_scalar)
2267 _state->normalized_scalar = token;
2271 * SQL function json_populate_record
2273 * set fields in a record from the argument json
2275 * Code adapted shamelessly from hstore's populate_record
2276 * which is in turn partly adapted from record_out.
2278 * The json is decomposed into a hash table, in which each
2279 * field in the record is then looked up by name. For jsonb
2280 * we fetch the values direct from the object.
2283 jsonb_populate_record(PG_FUNCTION_ARGS)
2285 return populate_record_worker(fcinfo, "jsonb_populate_record", true);
2289 jsonb_to_record(PG_FUNCTION_ARGS)
2291 return populate_record_worker(fcinfo, "jsonb_to_record", false);
2295 json_populate_record(PG_FUNCTION_ARGS)
2297 return populate_record_worker(fcinfo, "json_populate_record", true);
2301 json_to_record(PG_FUNCTION_ARGS)
2303 return populate_record_worker(fcinfo, "json_to_record", false);
2306 /* helper function for diagnostics */
2308 populate_array_report_expected_array(PopulateArrayContext *ctx, int ndim)
2314 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2315 errmsg("expected json array"),
2316 errhint("see the value of key \"%s\"", ctx->colname)));
2319 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2320 errmsg("expected json array")));
2324 StringInfoData indices;
2327 initStringInfo(&indices);
2329 Assert(ctx->ndims > 0 && ndim < ctx->ndims);
2331 for (i = 0; i < ndim; i++)
2332 appendStringInfo(&indices, "[%d]", ctx->sizes[i]);
2336 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2337 errmsg("expected json array"),
2338 errhint("see the array element %s of key \"%s\"",
2339 indices.data, ctx->colname)));
2342 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2343 errmsg("expected json array"),
2344 errhint("see the array element %s",
2349 /* set the number of dimensions of the populated array when it becomes known */
2351 populate_array_assign_ndims(PopulateArrayContext *ctx, int ndims)
2355 Assert(ctx->ndims <= 0);
2358 populate_array_report_expected_array(ctx, ndims);
2361 ctx->dims = palloc(sizeof(int) * ndims);
2362 ctx->sizes = palloc0(sizeof(int) * ndims);
2364 for (i = 0; i < ndims; i++)
2365 ctx->dims[i] = -1; /* dimensions are unknown yet */
2368 /* check the populated subarray dimension */
2370 populate_array_check_dimension(PopulateArrayContext *ctx, int ndim)
2372 int dim = ctx->sizes[ndim]; /* current dimension counter */
2374 if (ctx->dims[ndim] == -1)
2375 ctx->dims[ndim] = dim; /* assign dimension if not yet known */
2376 else if (ctx->dims[ndim] != dim)
2378 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2379 errmsg("malformed json array"),
2380 errdetail("Multidimensional arrays must have "
2381 "sub-arrays with matching dimensions.")));
2383 /* reset the current array dimension size counter */
2384 ctx->sizes[ndim] = 0;
2386 /* increment the parent dimension counter if it is a nested sub-array */
2388 ctx->sizes[ndim - 1]++;
2392 populate_array_element(PopulateArrayContext *ctx, int ndim, JsValue *jsv)
2395 bool element_isnull;
2397 /* populate the array element */
2398 element = populate_record_field(ctx->aio->element_info,
2399 ctx->aio->element_type,
2400 ctx->aio->element_typmod,
2401 NULL, ctx->mcxt, PointerGetDatum(NULL),
2402 jsv, &element_isnull);
2404 accumArrayResult(ctx->astate, element, element_isnull,
2405 ctx->aio->element_type, ctx->acxt);
2408 ctx->sizes[ndim - 1]++; /* increment current dimension counter */
2411 /* json object start handler for populate_array_json() */
2413 populate_array_object_start(void *_state)
2415 PopulateArrayState *state = (PopulateArrayState *) _state;
2416 int ndim = state->lex->lex_level;
2418 if (state->ctx->ndims <= 0)
2419 populate_array_assign_ndims(state->ctx, ndim);
2420 else if (ndim < state->ctx->ndims)
2421 populate_array_report_expected_array(state->ctx, ndim);
2424 /* json array end handler for populate_array_json() */
2426 populate_array_array_end(void *_state)
2428 PopulateArrayState *state = (PopulateArrayState *) _state;
2429 PopulateArrayContext *ctx = state->ctx;
2430 int ndim = state->lex->lex_level;
2432 if (ctx->ndims <= 0)
2433 populate_array_assign_ndims(ctx, ndim + 1);
2435 if (ndim < ctx->ndims)
2436 populate_array_check_dimension(ctx, ndim);
2439 /* json array element start handler for populate_array_json() */
2441 populate_array_element_start(void *_state, bool isnull)
2443 PopulateArrayState *state = (PopulateArrayState *) _state;
2444 int ndim = state->lex->lex_level;
2446 if (state->ctx->ndims <= 0 || ndim == state->ctx->ndims)
2448 /* remember current array element start */
2449 state->element_start = state->lex->token_start;
2450 state->element_type = state->lex->token_type;
2451 state->element_scalar = NULL;
2455 /* json array element end handler for populate_array_json() */
2457 populate_array_element_end(void *_state, bool isnull)
2459 PopulateArrayState *state = (PopulateArrayState *) _state;
2460 PopulateArrayContext *ctx = state->ctx;
2461 int ndim = state->lex->lex_level;
2463 Assert(ctx->ndims > 0);
2465 if (ndim == ctx->ndims)
2470 jsv.val.json.type = state->element_type;
2474 Assert(jsv.val.json.type == JSON_TOKEN_NULL);
2475 jsv.val.json.str = NULL;
2476 jsv.val.json.len = 0;
2478 else if (state->element_scalar)
2480 jsv.val.json.str = state->element_scalar;
2481 jsv.val.json.len = -1; /* null-terminated */
2485 jsv.val.json.str = state->element_start;
2486 jsv.val.json.len = (state->lex->prev_token_terminator -
2487 state->element_start) * sizeof(char);
2490 populate_array_element(ctx, ndim, &jsv);
2494 /* json scalar handler for populate_array_json() */
2496 populate_array_scalar(void *_state, char *token, JsonTokenType tokentype)
2498 PopulateArrayState *state = (PopulateArrayState *) _state;
2499 PopulateArrayContext *ctx = state->ctx;
2500 int ndim = state->lex->lex_level;
2502 if (ctx->ndims <= 0)
2503 populate_array_assign_ndims(ctx, ndim);
2504 else if (ndim < ctx->ndims)
2505 populate_array_report_expected_array(ctx, ndim);
2507 if (ndim == ctx->ndims)
2509 /* remember the scalar element token */
2510 state->element_scalar = token;
2511 /* element_type must already be set in populate_array_element_start() */
2512 Assert(state->element_type == tokentype);
2516 /* parse a json array and populate array */
2518 populate_array_json(PopulateArrayContext *ctx, char *json, int len)
2520 PopulateArrayState state;
2523 state.lex = makeJsonLexContextCstringLen(json, len, true);
2526 memset(&sem, 0, sizeof(sem));
2527 sem.semstate = (void *) &state;
2528 sem.object_start = populate_array_object_start;
2529 sem.array_end = populate_array_array_end;
2530 sem.array_element_start = populate_array_element_start;
2531 sem.array_element_end = populate_array_element_end;
2532 sem.scalar = populate_array_scalar;
2534 pg_parse_json(state.lex, &sem);
2536 /* number of dimensions should be already known */
2537 Assert(ctx->ndims > 0 && ctx->dims);
2543 * populate_array_dim_jsonb() -- Iterate recursively through jsonb sub-array
2544 * elements and accumulate result using given ArrayBuildState.
2547 populate_array_dim_jsonb(PopulateArrayContext *ctx, /* context */
2548 JsonbValue *jbv, /* jsonb sub-array */
2549 int ndim) /* current dimension */
2551 JsonbContainer *jbc = jbv->val.binary.data;
2553 JsonbIteratorToken tok;
2557 check_stack_depth();
2559 if (jbv->type != jbvBinary || !JsonContainerIsArray(jbc))
2560 populate_array_report_expected_array(ctx, ndim - 1);
2562 Assert(!JsonContainerIsScalar(jbc));
2564 it = JsonbIteratorInit(jbc);
2566 tok = JsonbIteratorNext(&it, &val, true);
2567 Assert(tok == WJB_BEGIN_ARRAY);
2569 tok = JsonbIteratorNext(&it, &val, true);
2572 * If the number of dimensions is not yet known and we have found end of
2573 * the array, or the first child element is not an array, then assign the
2574 * number of dimensions now.
2576 if (ctx->ndims <= 0 &&
2577 (tok == WJB_END_ARRAY ||
2579 (val.type != jbvBinary ||
2580 !JsonContainerIsArray(val.val.binary.data)))))
2581 populate_array_assign_ndims(ctx, ndim);
2583 jsv.is_json = false;
2584 jsv.val.jsonb = &val;
2586 /* process all the array elements */
2587 while (tok == WJB_ELEM)
2590 * Recurse only if the dimensions of dimensions is still unknown or if
2591 * it is not the innermost dimension.
2593 if (ctx->ndims > 0 && ndim >= ctx->ndims)
2594 populate_array_element(ctx, ndim, &jsv);
2597 /* populate child sub-array */
2598 populate_array_dim_jsonb(ctx, &val, ndim + 1);
2600 /* number of dimensions should be already known */
2601 Assert(ctx->ndims > 0 && ctx->dims);
2603 populate_array_check_dimension(ctx, ndim);
2606 tok = JsonbIteratorNext(&it, &val, true);
2609 Assert(tok == WJB_END_ARRAY);
2611 /* free iterator, iterating until WJB_DONE */
2612 tok = JsonbIteratorNext(&it, &val, true);
2613 Assert(tok == WJB_DONE && !it);
2616 /* recursively populate an array from json/jsonb */
2618 populate_array(ArrayIOData *aio,
2619 const char *colname,
2623 PopulateArrayContext ctx;
2630 ctx.acxt = CurrentMemoryContext;
2631 ctx.astate = initArrayResult(aio->element_type, ctx.acxt, true);
2632 ctx.colname = colname;
2633 ctx.ndims = 0; /* unknown yet */
2638 populate_array_json(&ctx, jsv->val.json.str,
2639 jsv->val.json.len >= 0 ? jsv->val.json.len
2640 : strlen(jsv->val.json.str));
2643 populate_array_dim_jsonb(&ctx, jsv->val.jsonb, 1);
2644 ctx.dims[0] = ctx.sizes[0];
2647 Assert(ctx.ndims > 0);
2649 lbs = palloc(sizeof(int) * ctx.ndims);
2651 for (i = 0; i < ctx.ndims; i++)
2654 result = makeMdArrayResult(ctx.astate, ctx.ndims, ctx.dims, lbs,
2665 JsValueToJsObject(JsValue *jsv, JsObject *jso)
2667 jso->is_json = jsv->is_json;
2671 /* convert plain-text json into a hash table */
2672 jso->val.json_hash =
2673 get_json_object_as_hash(jsv->val.json.str,
2674 jsv->val.json.len >= 0
2676 : strlen(jsv->val.json.str),
2677 "populate_composite");
2681 JsonbValue *jbv = jsv->val.jsonb;
2683 if (jbv->type == jbvBinary &&
2684 JsonContainerIsObject(jbv->val.binary.data))
2685 jso->val.jsonb_cont = jbv->val.binary.data;
2687 jso->val.jsonb_cont = NULL;
2691 /* recursively populate a composite (row type) value from json/jsonb */
2693 populate_composite(CompositeIOData *io,
2696 const char *colname,
2698 HeapTupleHeader defaultval,
2701 HeapTupleHeader tuple;
2704 /* acquire cached tuple descriptor */
2706 io->tupdesc->tdtypeid != typid ||
2707 io->tupdesc->tdtypmod != typmod)
2709 TupleDesc tupdesc = lookup_rowtype_tupdesc(typid, typmod);
2710 MemoryContext oldcxt;
2713 FreeTupleDesc(io->tupdesc);
2715 /* copy tuple desc without constraints into cache memory context */
2716 oldcxt = MemoryContextSwitchTo(mcxt);
2717 io->tupdesc = CreateTupleDescCopy(tupdesc);
2718 MemoryContextSwitchTo(oldcxt);
2720 ReleaseTupleDesc(tupdesc);
2723 /* prepare input value */
2724 JsValueToJsObject(jsv, &jso);
2726 /* populate resulting record tuple */
2727 tuple = populate_record(io->tupdesc, &io->record_io,
2728 defaultval, mcxt, &jso);
2732 return HeapTupleHeaderGetDatum(tuple);
2735 /* populate non-null scalar value from json/jsonb value */
2737 populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv)
2745 int len = jsv->val.json.len;
2747 json = jsv->val.json.str;
2750 /* already done the hard work in the json case */
2751 if ((typid == JSONOID || typid == JSONBOID) &&
2752 jsv->val.json.type == JSON_TOKEN_STRING)
2755 * Add quotes around string value (should be already escaped) if
2756 * converting to json/jsonb.
2762 str = palloc(len + sizeof(char) * 3);
2764 memcpy(&str[1], json, len);
2766 str[len + 2] = '\0';
2770 /* Need to copy non-null-terminated string */
2771 str = palloc(len + 1 * sizeof(char));
2772 memcpy(str, json, len);
2776 str = json; /* null-terminated string */
2780 JsonbValue *jbv = jsv->val.jsonb;
2782 if (typid == JSONBOID)
2784 Jsonb *jsonb = JsonbValueToJsonb(jbv); /* directly use jsonb */
2786 return JsonbGetDatum(jsonb);
2788 /* convert jsonb to string for typio call */
2789 else if (typid == JSONOID && jbv->type != jbvBinary)
2792 * Convert scalar jsonb (non-scalars are passed here as jbvBinary)
2793 * to json string, preserving quotes around top-level strings.
2795 Jsonb *jsonb = JsonbValueToJsonb(jbv);
2797 str = JsonbToCString(NULL, &jsonb->root, VARSIZE(jsonb));
2799 else if (jbv->type == jbvString) /* quotes are stripped */
2800 str = pnstrdup(jbv->val.string.val, jbv->val.string.len);
2801 else if (jbv->type == jbvBool)
2802 str = pstrdup(jbv->val.boolean ? "true" : "false");
2803 else if (jbv->type == jbvNumeric)
2804 str = DatumGetCString(DirectFunctionCall1(numeric_out,
2805 PointerGetDatum(jbv->val.numeric)));
2806 else if (jbv->type == jbvBinary)
2807 str = JsonbToCString(NULL, jbv->val.binary.data,
2808 jbv->val.binary.len);
2810 elog(ERROR, "unrecognized jsonb type: %d", (int) jbv->type);
2813 res = InputFunctionCall(&io->typiofunc, str, io->typioparam, typmod);
2815 /* free temporary buffer */
2823 populate_domain(DomainIOData *io,
2825 const char *colname,
2836 res = populate_record_field(io->base_io,
2837 io->base_typid, io->base_typmod,
2838 colname, mcxt, PointerGetDatum(NULL),
2843 domain_check(res, isnull, typid, &io->domain_info, mcxt);
2848 /* prepare column metadata cache for the given type */
2850 prepare_column_cache(ColumnIOData *column,
2859 column->typid = typid;
2860 column->typmod = typmod;
2862 tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
2863 if (!HeapTupleIsValid(tup))
2864 elog(ERROR, "cache lookup failed for type %u", typid);
2866 type = (Form_pg_type) GETSTRUCT(tup);
2868 if (type->typtype == TYPTYPE_DOMAIN)
2870 column->typcat = TYPECAT_DOMAIN;
2871 column->io.domain.base_typid = type->typbasetype;
2872 column->io.domain.base_typmod = type->typtypmod;
2873 column->io.domain.base_io = MemoryContextAllocZero(mcxt,
2874 sizeof(ColumnIOData));
2875 column->io.domain.domain_info = NULL;
2877 else if (type->typtype == TYPTYPE_COMPOSITE || typid == RECORDOID)
2879 column->typcat = TYPECAT_COMPOSITE;
2880 column->io.composite.record_io = NULL;
2881 column->io.composite.tupdesc = NULL;
2883 else if (type->typlen == -1 && OidIsValid(type->typelem))
2885 column->typcat = TYPECAT_ARRAY;
2886 column->io.array.element_info = MemoryContextAllocZero(mcxt,
2887 sizeof(ColumnIOData));
2888 column->io.array.element_type = type->typelem;
2889 /* array element typemod stored in attribute's typmod */
2890 column->io.array.element_typmod = typmod;
2893 column->typcat = TYPECAT_SCALAR;
2895 /* don't need input function when converting from jsonb to jsonb */
2896 if (json || typid != JSONBOID)
2900 getTypeInputInfo(typid, &typioproc, &column->scalar_io.typioparam);
2901 fmgr_info_cxt(typioproc, &column->scalar_io.typiofunc, mcxt);
2904 ReleaseSysCache(tup);
2907 /* recursively populate a record field or an array element from a json/jsonb value */
2909 populate_record_field(ColumnIOData *col,
2912 const char *colname,
2920 check_stack_depth();
2922 /* prepare column metadata cache for the given type */
2923 if (col->typid != typid || col->typmod != typmod)
2924 prepare_column_cache(col, typid, typmod, mcxt, jsv->is_json);
2926 *isnull = JsValueIsNull(jsv);
2928 typcat = col->typcat;
2930 /* try to convert json string to a non-scalar type through input function */
2931 if (JsValueIsString(jsv) &&
2932 (typcat == TYPECAT_ARRAY || typcat == TYPECAT_COMPOSITE))
2933 typcat = TYPECAT_SCALAR;
2935 /* we must perform domain checks for NULLs */
2936 if (*isnull && typcat != TYPECAT_DOMAIN)
2941 case TYPECAT_SCALAR:
2942 return populate_scalar(&col->scalar_io, typid, typmod, jsv);
2945 return populate_array(&col->io.array, colname, mcxt, jsv);
2947 case TYPECAT_COMPOSITE:
2948 return populate_composite(&col->io.composite, typid, typmod,
2950 DatumGetPointer(defaultval)
2951 ? DatumGetHeapTupleHeader(defaultval)
2955 case TYPECAT_DOMAIN:
2956 return populate_domain(&col->io.domain, typid, colname, mcxt,
2960 elog(ERROR, "unrecognized type category '%c'", typcat);
2965 static RecordIOData *
2966 allocate_record_info(MemoryContext mcxt, int ncolumns)
2968 RecordIOData *data = (RecordIOData *)
2969 MemoryContextAlloc(mcxt,
2970 offsetof(RecordIOData, columns) +
2971 ncolumns * sizeof(ColumnIOData));
2973 data->record_type = InvalidOid;
2974 data->record_typmod = 0;
2975 data->ncolumns = ncolumns;
2976 MemSet(data->columns, 0, sizeof(ColumnIOData) * ncolumns);
2982 JsObjectGetField(JsObject *obj, char *field, JsValue *jsv)
2984 jsv->is_json = obj->is_json;
2988 JsonHashEntry *hashentry = hash_search(obj->val.json_hash, field,
2991 jsv->val.json.type = hashentry ? hashentry->type : JSON_TOKEN_NULL;
2992 jsv->val.json.str = jsv->val.json.type == JSON_TOKEN_NULL ? NULL :
2994 jsv->val.json.len = jsv->val.json.str ? -1 : 0; /* null-terminated */
2996 return hashentry != NULL;
3000 jsv->val.jsonb = !obj->val.jsonb_cont ? NULL :
3001 findJsonbValueFromContainerLen(obj->val.jsonb_cont, JB_FOBJECT,
3002 field, strlen(field));
3004 return jsv->val.jsonb != NULL;
3008 /* populate a record tuple from json/jsonb value */
3009 static HeapTupleHeader
3010 populate_record(TupleDesc tupdesc,
3011 RecordIOData **record_p,
3012 HeapTupleHeader defaultval,
3016 RecordIOData *record = *record_p;
3020 int ncolumns = tupdesc->natts;
3024 * if the input json is empty, we can only skip the rest if we were passed
3025 * in a non-null record, since otherwise there may be issues with domain
3028 if (defaultval && JsObjectIsEmpty(obj))
3031 /* (re)allocate metadata cache */
3032 if (record == NULL ||
3033 record->ncolumns != ncolumns)
3034 *record_p = record = allocate_record_info(mcxt, ncolumns);
3036 /* invalidate metadata cache if the record type has changed */
3037 if (record->record_type != tupdesc->tdtypeid ||
3038 record->record_typmod != tupdesc->tdtypmod)
3040 MemSet(record, 0, offsetof(RecordIOData, columns) +
3041 ncolumns * sizeof(ColumnIOData));
3042 record->record_type = tupdesc->tdtypeid;
3043 record->record_typmod = tupdesc->tdtypmod;
3044 record->ncolumns = ncolumns;
3047 values = (Datum *) palloc(ncolumns * sizeof(Datum));
3048 nulls = (bool *) palloc(ncolumns * sizeof(bool));
3052 HeapTupleData tuple;
3054 /* Build a temporary HeapTuple control structure */
3055 tuple.t_len = HeapTupleHeaderGetDatumLength(defaultval);
3056 ItemPointerSetInvalid(&(tuple.t_self));
3057 tuple.t_tableOid = InvalidOid;
3058 tuple.t_data = defaultval;
3060 /* Break down the tuple into fields */
3061 heap_deform_tuple(&tuple, tupdesc, values, nulls);
3065 for (i = 0; i < ncolumns; ++i)
3067 values[i] = (Datum) 0;
3072 for (i = 0; i < ncolumns; ++i)
3074 Form_pg_attribute att = tupdesc->attrs[i];
3075 char *colname = NameStr(att->attname);
3076 JsValue field = {0};
3079 /* Ignore dropped columns in datatype */
3080 if (att->attisdropped)
3086 found = JsObjectGetField(obj, colname, &field);
3089 * we can't just skip here if the key wasn't found since we might have
3090 * a domain to deal with. If we were passed in a non-null record
3091 * datum, we assume that the existing values are valid (if they're
3092 * not, then it's not our fault), but if we were passed in a null,
3093 * then every field which we don't populate needs to be run through
3094 * the input function just in case it's a domain type.
3096 if (defaultval && !found)
3099 values[i] = populate_record_field(&record->columns[i],
3104 nulls[i] ? (Datum) 0 : values[i],
3109 res = heap_form_tuple(tupdesc, values, nulls);
3118 populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
3119 bool have_record_arg)
3121 int json_arg_num = have_record_arg ? 1 : 0;
3122 Oid jtype = get_fn_expr_argtype(fcinfo->flinfo, json_arg_num);
3124 HeapTupleHeader rec = NULL;
3127 TupleDesc tupdesc = NULL;
3130 MemoryContext fnmcxt = fcinfo->flinfo->fn_mcxt;
3131 PopulateRecordCache *cache = fcinfo->flinfo->fn_extra;
3133 Assert(jtype == JSONOID || jtype == JSONBOID);
3136 * We arrange to look up the needed I/O info just once per series of
3137 * calls, assuming the record type doesn't change underneath us.
3140 fcinfo->flinfo->fn_extra = cache =
3141 MemoryContextAllocZero(fnmcxt, sizeof(*cache));
3143 if (have_record_arg)
3145 Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
3147 if (cache->argtype != argtype)
3149 if (!type_is_rowtype(argtype))
3151 (errcode(ERRCODE_DATATYPE_MISMATCH),
3152 errmsg("first argument of %s must be a row type",
3155 cache->argtype = argtype;
3158 if (PG_ARGISNULL(0))
3160 if (PG_ARGISNULL(1))
3164 * We have no tuple to look at, so the only source of type info is
3165 * the argtype. The lookup_rowtype_tupdesc call below will error
3166 * out if we don't have a known composite type oid here.
3173 rec = PG_GETARG_HEAPTUPLEHEADER(0);
3175 if (PG_ARGISNULL(1))
3176 PG_RETURN_POINTER(rec);
3178 /* Extract type info from the tuple itself */
3179 tupType = HeapTupleHeaderGetTypeId(rec);
3180 tupTypmod = HeapTupleHeaderGetTypMod(rec);
3185 /* json{b}_to_record case */
3186 if (PG_ARGISNULL(0))
3189 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
3191 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3192 errmsg("function returning record called in context "
3193 "that cannot accept type record"),
3194 errhint("Try calling the function in the FROM clause "
3195 "using a column definition list.")));
3200 * Add tupdesc to the cache and set the appropriate values of
3201 * tupType/tupTypmod for proper cache usage in populate_composite().
3203 cache->io.tupdesc = tupdesc;
3205 tupType = tupdesc->tdtypeid;
3206 tupTypmod = tupdesc->tdtypmod;
3209 jsv.is_json = jtype == JSONOID;
3213 text *json = PG_GETARG_TEXT_PP(json_arg_num);
3215 jsv.val.json.str = VARDATA_ANY(json);
3216 jsv.val.json.len = VARSIZE_ANY_EXHDR(json);
3217 jsv.val.json.type = JSON_TOKEN_INVALID; /* not used in
3218 * populate_composite() */
3222 Jsonb *jb = PG_GETARG_JSONB(json_arg_num);
3224 jsv.val.jsonb = &jbv;
3226 /* fill binary jsonb value pointing to jb */
3227 jbv.type = jbvBinary;
3228 jbv.val.binary.data = &jb->root;
3229 jbv.val.binary.len = VARSIZE(jb) - VARHDRSZ;
3232 rettuple = populate_composite(&cache->io, tupType, tupTypmod,
3233 NULL, fnmcxt, rec, &jsv);
3237 cache->io.tupdesc = NULL;
3238 ReleaseTupleDesc(tupdesc);
3241 PG_RETURN_DATUM(rettuple);
3245 * get_json_object_as_hash
3247 * decompose a json object into a hash table.
3250 get_json_object_as_hash(char *json, int len, const char *funcname)
3255 JsonLexContext *lex = makeJsonLexContextCstringLen(json, len, true);
3258 memset(&ctl, 0, sizeof(ctl));
3259 ctl.keysize = NAMEDATALEN;
3260 ctl.entrysize = sizeof(JsonHashEntry);
3261 ctl.hcxt = CurrentMemoryContext;
3262 tab = hash_create("json object hashtable",
3265 HASH_ELEM | HASH_CONTEXT);
3267 state = palloc0(sizeof(JHashState));
3268 sem = palloc0(sizeof(JsonSemAction));
3270 state->function_name = funcname;
3274 sem->semstate = (void *) state;
3275 sem->array_start = hash_array_start;
3276 sem->scalar = hash_scalar;
3277 sem->object_field_start = hash_object_field_start;
3278 sem->object_field_end = hash_object_field_end;
3280 pg_parse_json(lex, sem);
3286 hash_object_field_start(void *state, char *fname, bool isnull)
3288 JHashState *_state = (JHashState *) state;
3290 if (_state->lex->lex_level > 1)
3293 /* remember token type */
3294 _state->saved_token_type = _state->lex->token_type;
3296 if (_state->lex->token_type == JSON_TOKEN_ARRAY_START ||
3297 _state->lex->token_type == JSON_TOKEN_OBJECT_START)
3299 /* remember start position of the whole text of the subobject */
3300 _state->save_json_start = _state->lex->token_start;
3304 /* must be a scalar */
3305 _state->save_json_start = NULL;
3310 hash_object_field_end(void *state, char *fname, bool isnull)
3312 JHashState *_state = (JHashState *) state;
3313 JsonHashEntry *hashentry;
3317 * Ignore nested fields.
3319 if (_state->lex->lex_level > 1)
3323 * Ignore field names >= NAMEDATALEN - they can't match a record field.
3324 * (Note: without this test, the hash code would truncate the string at
3325 * NAMEDATALEN-1, and could then match against a similarly-truncated
3326 * record field name. That would be a reasonable behavior, but this code
3327 * has previously insisted on exact equality, so we keep this behavior.)
3329 if (strlen(fname) >= NAMEDATALEN)
3332 hashentry = hash_search(_state->hash, fname, HASH_ENTER, &found);
3335 * found being true indicates a duplicate. We don't do anything about
3336 * that, a later field with the same name overrides the earlier field.
3339 hashentry->type = _state->saved_token_type;
3340 Assert(isnull == (hashentry->type == JSON_TOKEN_NULL));
3342 if (_state->save_json_start != NULL)
3344 int len = _state->lex->prev_token_terminator - _state->save_json_start;
3345 char *val = palloc((len + 1) * sizeof(char));
3347 memcpy(val, _state->save_json_start, len);
3349 hashentry->val = val;
3353 /* must have had a scalar instead */
3354 hashentry->val = _state->saved_scalar;
3359 hash_array_start(void *state)
3361 JHashState *_state = (JHashState *) state;
3363 if (_state->lex->lex_level == 0)
3365 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3366 errmsg("cannot call %s on an array", _state->function_name)));
3370 hash_scalar(void *state, char *token, JsonTokenType tokentype)
3372 JHashState *_state = (JHashState *) state;
3374 if (_state->lex->lex_level == 0)
3376 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3377 errmsg("cannot call %s on a scalar", _state->function_name)));
3379 if (_state->lex->lex_level == 1)
3381 _state->saved_scalar = token;
3382 /* saved_token_type must already be set in hash_object_field_start() */
3383 Assert(_state->saved_token_type == tokentype);
3389 * SQL function json_populate_recordset
3391 * set fields in a set of records from the argument json,
3392 * which must be an array of objects.
3394 * similar to json_populate_record, but the tuple-building code
3395 * is pushed down into the semantic action handlers so it's done
3396 * per object in the array.
3399 jsonb_populate_recordset(PG_FUNCTION_ARGS)
3401 return populate_recordset_worker(fcinfo, "jsonb_populate_recordset", true);
3405 jsonb_to_recordset(PG_FUNCTION_ARGS)
3407 return populate_recordset_worker(fcinfo, "jsonb_to_recordset", false);
3411 json_populate_recordset(PG_FUNCTION_ARGS)
3413 return populate_recordset_worker(fcinfo, "json_populate_recordset", true);
3417 json_to_recordset(PG_FUNCTION_ARGS)
3419 return populate_recordset_worker(fcinfo, "json_to_recordset", false);
3423 populate_recordset_record(PopulateRecordsetState *state, JsObject *obj)
3425 HeapTupleData tuple;
3426 HeapTupleHeader tuphead = populate_record(state->ret_tdesc,
3432 tuple.t_len = HeapTupleHeaderGetDatumLength(tuphead);
3433 ItemPointerSetInvalid(&(tuple.t_self));
3434 tuple.t_tableOid = InvalidOid;
3435 tuple.t_data = tuphead;
3437 tuplestore_puttuple(state->tuple_store, &tuple);
3441 * common worker for json_populate_recordset() and json_to_recordset()
3444 populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
3445 bool have_record_arg)
3447 int json_arg_num = have_record_arg ? 1 : 0;
3448 Oid jtype = get_fn_expr_argtype(fcinfo->flinfo, json_arg_num);
3450 MemoryContext old_cxt;
3451 HeapTupleHeader rec;
3453 PopulateRecordsetState *state;
3455 if (have_record_arg)
3457 Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
3459 if (!type_is_rowtype(argtype))
3461 (errcode(ERRCODE_DATATYPE_MISMATCH),
3462 errmsg("first argument of %s must be a row type",
3466 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
3468 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
3469 (rsi->allowedModes & SFRM_Materialize) == 0 ||
3470 rsi->expectedDesc == NULL)
3472 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3473 errmsg("set-valued function called in context that "
3474 "cannot accept a set")));
3476 rsi->returnMode = SFRM_Materialize;
3479 * get the tupdesc from the result set info - it must be a record type
3480 * because we already checked that arg1 is a record type, or we're in a
3481 * to_record function which returns a setof record.
3483 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
3485 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3486 errmsg("function returning record called in context "
3487 "that cannot accept type record")));
3489 /* if the json is null send back an empty set */
3490 if (PG_ARGISNULL(json_arg_num))
3493 if (!have_record_arg || PG_ARGISNULL(0))
3496 rec = PG_GETARG_HEAPTUPLEHEADER(0);
3498 state = palloc0(sizeof(PopulateRecordsetState));
3500 /* make these in a sufficiently long-lived memory context */
3501 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
3502 state->ret_tdesc = CreateTupleDescCopy(tupdesc);
3503 BlessTupleDesc(state->ret_tdesc);
3504 state->tuple_store = tuplestore_begin_heap(rsi->allowedModes &
3505 SFRM_Materialize_Random,
3507 MemoryContextSwitchTo(old_cxt);
3509 state->function_name = funcname;
3510 state->my_extra = (RecordIOData **) &fcinfo->flinfo->fn_extra;
3512 state->fn_mcxt = fcinfo->flinfo->fn_mcxt;
3514 if (jtype == JSONOID)
3516 text *json = PG_GETARG_TEXT_PP(json_arg_num);
3517 JsonLexContext *lex;
3520 sem = palloc0(sizeof(JsonSemAction));
3522 lex = makeJsonLexContext(json, true);
3524 sem->semstate = (void *) state;
3525 sem->array_start = populate_recordset_array_start;
3526 sem->array_element_start = populate_recordset_array_element_start;
3527 sem->scalar = populate_recordset_scalar;
3528 sem->object_field_start = populate_recordset_object_field_start;
3529 sem->object_field_end = populate_recordset_object_field_end;
3530 sem->object_start = populate_recordset_object_start;
3531 sem->object_end = populate_recordset_object_end;
3535 pg_parse_json(lex, sem);
3539 Jsonb *jb = PG_GETARG_JSONB(json_arg_num);
3542 bool skipNested = false;
3543 JsonbIteratorToken r;
3545 Assert(jtype == JSONBOID);
3547 if (JB_ROOT_IS_SCALAR(jb) || !JB_ROOT_IS_ARRAY(jb))
3549 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3550 errmsg("cannot call %s on a non-array",
3553 it = JsonbIteratorInit(&jb->root);
3555 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
3563 if (v.type != jbvBinary ||
3564 !JsonContainerIsObject(v.val.binary.data))
3566 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3567 errmsg("argument of %s must be an array of objects",
3570 obj.is_json = false;
3571 obj.val.jsonb_cont = v.val.binary.data;
3573 populate_recordset_record(state, &obj);
3578 rsi->setResult = state->tuple_store;
3579 rsi->setDesc = state->ret_tdesc;
3585 populate_recordset_object_start(void *state)
3587 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3588 int lex_level = _state->lex->lex_level;
3591 /* Reject object at top level: we must have an array at level 0 */
3594 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3595 errmsg("cannot call %s on an object",
3596 _state->function_name)));
3598 /* Nested objects require no special processing */
3602 /* Object at level 1: set up a new hash table for this object */
3603 memset(&ctl, 0, sizeof(ctl));
3604 ctl.keysize = NAMEDATALEN;
3605 ctl.entrysize = sizeof(JsonHashEntry);
3606 ctl.hcxt = CurrentMemoryContext;
3607 _state->json_hash = hash_create("json object hashtable",
3610 HASH_ELEM | HASH_CONTEXT);
3614 populate_recordset_object_end(void *state)
3616 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3619 /* Nested objects require no special processing */
3620 if (_state->lex->lex_level > 1)
3624 obj.val.json_hash = _state->json_hash;
3626 /* Otherwise, construct and return a tuple based on this level-1 object */
3627 populate_recordset_record(_state, &obj);
3629 /* Done with hash for this object */
3630 hash_destroy(_state->json_hash);
3631 _state->json_hash = NULL;
3635 populate_recordset_array_element_start(void *state, bool isnull)
3637 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3639 if (_state->lex->lex_level == 1 &&
3640 _state->lex->token_type != JSON_TOKEN_OBJECT_START)
3642 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3643 errmsg("argument of %s must be an array of objects",
3644 _state->function_name)));
3648 populate_recordset_array_start(void *state)
3654 populate_recordset_scalar(void *state, char *token, JsonTokenType tokentype)
3656 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3658 if (_state->lex->lex_level == 0)
3660 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3661 errmsg("cannot call %s on a scalar",
3662 _state->function_name)));
3664 if (_state->lex->lex_level == 2)
3665 _state->saved_scalar = token;
3669 populate_recordset_object_field_start(void *state, char *fname, bool isnull)
3671 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3673 if (_state->lex->lex_level > 2)
3676 _state->saved_token_type = _state->lex->token_type;
3678 if (_state->lex->token_type == JSON_TOKEN_ARRAY_START ||
3679 _state->lex->token_type == JSON_TOKEN_OBJECT_START)
3681 _state->save_json_start = _state->lex->token_start;
3685 _state->save_json_start = NULL;
3690 populate_recordset_object_field_end(void *state, char *fname, bool isnull)
3692 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3693 JsonHashEntry *hashentry;
3697 * Ignore nested fields.
3699 if (_state->lex->lex_level > 2)
3703 * Ignore field names >= NAMEDATALEN - they can't match a record field.
3704 * (Note: without this test, the hash code would truncate the string at
3705 * NAMEDATALEN-1, and could then match against a similarly-truncated
3706 * record field name. That would be a reasonable behavior, but this code
3707 * has previously insisted on exact equality, so we keep this behavior.)
3709 if (strlen(fname) >= NAMEDATALEN)
3712 hashentry = hash_search(_state->json_hash, fname, HASH_ENTER, &found);
3715 * found being true indicates a duplicate. We don't do anything about
3716 * that, a later field with the same name overrides the earlier field.
3719 hashentry->type = _state->saved_token_type;
3720 Assert(isnull == (hashentry->type == JSON_TOKEN_NULL));
3722 if (_state->save_json_start != NULL)
3724 int len = _state->lex->prev_token_terminator - _state->save_json_start;
3725 char *val = palloc((len + 1) * sizeof(char));
3727 memcpy(val, _state->save_json_start, len);
3729 hashentry->val = val;
3733 /* must have had a scalar instead */
3734 hashentry->val = _state->saved_scalar;
3739 * findJsonbValueFromContainer() wrapper that sets up JsonbValue key string.
3742 findJsonbValueFromContainerLen(JsonbContainer *container, uint32 flags,
3743 char *key, uint32 keylen)
3748 k.val.string.val = key;
3749 k.val.string.len = keylen;
3751 return findJsonbValueFromContainer(container, flags, &k);
3755 * Semantic actions for json_strip_nulls.
3757 * Simply repeat the input on the output unless we encounter
3758 * a null object field. State for this is set when the field
3759 * is started and reset when the scalar action (which must be next)
3764 sn_object_start(void *state)
3766 StripnullState *_state = (StripnullState *) state;
3768 appendStringInfoCharMacro(_state->strval, '{');
3772 sn_object_end(void *state)
3774 StripnullState *_state = (StripnullState *) state;
3776 appendStringInfoCharMacro(_state->strval, '}');
3780 sn_array_start(void *state)
3782 StripnullState *_state = (StripnullState *) state;
3784 appendStringInfoCharMacro(_state->strval, '[');
3788 sn_array_end(void *state)
3790 StripnullState *_state = (StripnullState *) state;
3792 appendStringInfoCharMacro(_state->strval, ']');
3796 sn_object_field_start(void *state, char *fname, bool isnull)
3798 StripnullState *_state = (StripnullState *) state;
3803 * The next thing must be a scalar or isnull couldn't be true, so
3804 * there is no danger of this state being carried down into a nested
3805 * object or array. The flag will be reset in the scalar action.
3807 _state->skip_next_null = true;
3811 if (_state->strval->data[_state->strval->len - 1] != '{')
3812 appendStringInfoCharMacro(_state->strval, ',');
3815 * Unfortunately we don't have the quoted and escaped string any more, so
3816 * we have to re-escape it.
3818 escape_json(_state->strval, fname);
3820 appendStringInfoCharMacro(_state->strval, ':');
3824 sn_array_element_start(void *state, bool isnull)
3826 StripnullState *_state = (StripnullState *) state;
3828 if (_state->strval->data[_state->strval->len - 1] != '[')
3829 appendStringInfoCharMacro(_state->strval, ',');
3833 sn_scalar(void *state, char *token, JsonTokenType tokentype)
3835 StripnullState *_state = (StripnullState *) state;
3837 if (_state->skip_next_null)
3839 Assert(tokentype == JSON_TOKEN_NULL);
3840 _state->skip_next_null = false;
3844 if (tokentype == JSON_TOKEN_STRING)
3845 escape_json(_state->strval, token);
3847 appendStringInfoString(_state->strval, token);
3851 * SQL function json_strip_nulls(json) -> json
3854 json_strip_nulls(PG_FUNCTION_ARGS)
3856 text *json = PG_GETARG_TEXT_PP(0);
3857 StripnullState *state;
3858 JsonLexContext *lex;
3861 lex = makeJsonLexContext(json, true);
3862 state = palloc0(sizeof(StripnullState));
3863 sem = palloc0(sizeof(JsonSemAction));
3865 state->strval = makeStringInfo();
3866 state->skip_next_null = false;
3869 sem->semstate = (void *) state;
3870 sem->object_start = sn_object_start;
3871 sem->object_end = sn_object_end;
3872 sem->array_start = sn_array_start;
3873 sem->array_end = sn_array_end;
3874 sem->scalar = sn_scalar;
3875 sem->array_element_start = sn_array_element_start;
3876 sem->object_field_start = sn_object_field_start;
3878 pg_parse_json(lex, sem);
3880 PG_RETURN_TEXT_P(cstring_to_text_with_len(state->strval->data,
3881 state->strval->len));
3886 * SQL function jsonb_strip_nulls(jsonb) -> jsonb
3889 jsonb_strip_nulls(PG_FUNCTION_ARGS)
3891 Jsonb *jb = PG_GETARG_JSONB(0);
3893 JsonbParseState *parseState = NULL;
3894 JsonbValue *res = NULL;
3897 JsonbIteratorToken type;
3898 bool last_was_key = false;
3900 if (JB_ROOT_IS_SCALAR(jb))
3901 PG_RETURN_POINTER(jb);
3903 it = JsonbIteratorInit(&jb->root);
3905 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
3907 Assert(!(type == WJB_KEY && last_was_key));
3909 if (type == WJB_KEY)
3911 /* stash the key until we know if it has a null value */
3913 last_was_key = true;
3919 /* if the last element was a key this one can't be */
3920 last_was_key = false;
3922 /* skip this field if value is null */
3923 if (type == WJB_VALUE && v.type == jbvNull)
3926 /* otherwise, do a delayed push of the key */
3927 (void) pushJsonbValue(&parseState, WJB_KEY, &k);
3930 if (type == WJB_VALUE || type == WJB_ELEM)
3931 res = pushJsonbValue(&parseState, type, &v);
3933 res = pushJsonbValue(&parseState, type, NULL);
3936 Assert(res != NULL);
3938 PG_RETURN_POINTER(JsonbValueToJsonb(res));
3942 * Add values from the jsonb to the parse state.
3944 * If the parse state container is an object, the jsonb is pushed as
3945 * a value, not a key.
3947 * This needs to be done using an iterator because pushJsonbValue doesn't
3948 * like getting jbvBinary values, so we can't just push jb as a whole.
3951 addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb)
3954 JsonbValue *o = &(*jbps)->contVal;
3956 JsonbIteratorToken type;
3958 it = JsonbIteratorInit(&jb->root);
3960 Assert(o->type == jbvArray || o->type == jbvObject);
3962 if (JB_ROOT_IS_SCALAR(jb))
3964 (void) JsonbIteratorNext(&it, &v, false); /* skip array header */
3965 (void) JsonbIteratorNext(&it, &v, false); /* fetch scalar value */
3970 (void) pushJsonbValue(jbps, WJB_ELEM, &v);
3973 (void) pushJsonbValue(jbps, WJB_VALUE, &v);
3976 elog(ERROR, "unexpected parent of nested structure");
3981 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
3983 if (type == WJB_KEY || type == WJB_VALUE || type == WJB_ELEM)
3984 (void) pushJsonbValue(jbps, type, &v);
3986 (void) pushJsonbValue(jbps, type, NULL);
3993 * SQL function jsonb_pretty (jsonb)
3995 * Pretty-printed text for the jsonb
3998 jsonb_pretty(PG_FUNCTION_ARGS)
4000 Jsonb *jb = PG_GETARG_JSONB(0);
4001 StringInfo str = makeStringInfo();
4003 JsonbToCStringIndent(str, &jb->root, VARSIZE(jb));
4005 PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len));
4009 * SQL function jsonb_concat (jsonb, jsonb)
4011 * function for || operator
4014 jsonb_concat(PG_FUNCTION_ARGS)
4016 Jsonb *jb1 = PG_GETARG_JSONB(0);
4017 Jsonb *jb2 = PG_GETARG_JSONB(1);
4018 JsonbParseState *state = NULL;
4024 * If one of the jsonb is empty, just return the other if it's not scalar
4025 * and both are of the same kind. If it's a scalar or they are of
4026 * different kinds we need to perform the concatenation even if one is
4029 if (JB_ROOT_IS_OBJECT(jb1) == JB_ROOT_IS_OBJECT(jb2))
4031 if (JB_ROOT_COUNT(jb1) == 0 && !JB_ROOT_IS_SCALAR(jb2))
4032 PG_RETURN_JSONB(jb2);
4033 else if (JB_ROOT_COUNT(jb2) == 0 && !JB_ROOT_IS_SCALAR(jb1))
4034 PG_RETURN_JSONB(jb1);
4037 it1 = JsonbIteratorInit(&jb1->root);
4038 it2 = JsonbIteratorInit(&jb2->root);
4040 res = IteratorConcat(&it1, &it2, &state);
4042 Assert(res != NULL);
4044 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4049 * SQL function jsonb_delete (jsonb, text)
4051 * return a copy of the jsonb with the indicated item
4055 jsonb_delete(PG_FUNCTION_ARGS)
4057 Jsonb *in = PG_GETARG_JSONB(0);
4058 text *key = PG_GETARG_TEXT_PP(1);
4059 char *keyptr = VARDATA_ANY(key);
4060 int keylen = VARSIZE_ANY_EXHDR(key);
4061 JsonbParseState *state = NULL;
4065 bool skipNested = false;
4066 JsonbIteratorToken r;
4068 if (JB_ROOT_IS_SCALAR(in))
4070 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4071 errmsg("cannot delete from scalar")));
4073 if (JB_ROOT_COUNT(in) == 0)
4074 PG_RETURN_JSONB(in);
4076 it = JsonbIteratorInit(&in->root);
4078 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
4082 if ((r == WJB_ELEM || r == WJB_KEY) &&
4083 (v.type == jbvString && keylen == v.val.string.len &&
4084 memcmp(keyptr, v.val.string.val, keylen) == 0))
4086 /* skip corresponding value as well */
4088 JsonbIteratorNext(&it, &v, true);
4093 res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4096 Assert(res != NULL);
4098 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4102 * SQL function jsonb_delete (jsonb, variadic text[])
4104 * return a copy of the jsonb with the indicated items
4108 jsonb_delete_array(PG_FUNCTION_ARGS)
4110 Jsonb *in = PG_GETARG_JSONB(0);
4111 ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
4115 JsonbParseState *state = NULL;
4119 bool skipNested = false;
4120 JsonbIteratorToken r;
4122 if (ARR_NDIM(keys) > 1)
4124 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4125 errmsg("wrong number of array subscripts")));
4127 if (JB_ROOT_IS_SCALAR(in))
4129 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4130 errmsg("cannot delete from scalar")));
4132 if (JB_ROOT_COUNT(in) == 0)
4133 PG_RETURN_JSONB(in);
4135 deconstruct_array(keys, TEXTOID, -1, false, 'i',
4136 &keys_elems, &keys_nulls, &keys_len);
4139 PG_RETURN_JSONB(in);
4141 it = JsonbIteratorInit(&in->root);
4143 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
4147 if ((r == WJB_ELEM || r == WJB_KEY) && v.type == jbvString)
4152 for (i = 0; i < keys_len; i++)
4160 keyptr = VARDATA_ANY(keys_elems[i]);
4161 keylen = VARSIZE_ANY_EXHDR(keys_elems[i]);
4162 if (keylen == v.val.string.len &&
4163 memcmp(keyptr, v.val.string.val, keylen) == 0)
4171 /* skip corresponding value as well */
4173 JsonbIteratorNext(&it, &v, true);
4179 res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4182 Assert(res != NULL);
4184 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4188 * SQL function jsonb_delete (jsonb, int)
4190 * return a copy of the jsonb with the indicated item
4191 * removed. Negative int means count back from the
4195 jsonb_delete_idx(PG_FUNCTION_ARGS)
4197 Jsonb *in = PG_GETARG_JSONB(0);
4198 int idx = PG_GETARG_INT32(1);
4199 JsonbParseState *state = NULL;
4205 JsonbIteratorToken r;
4207 if (JB_ROOT_IS_SCALAR(in))
4209 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4210 errmsg("cannot delete from scalar")));
4212 if (JB_ROOT_IS_OBJECT(in))
4214 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4215 errmsg("cannot delete from object using integer index")));
4217 if (JB_ROOT_COUNT(in) == 0)
4218 PG_RETURN_JSONB(in);
4220 it = JsonbIteratorInit(&in->root);
4222 r = JsonbIteratorNext(&it, &v, false);
4223 Assert(r == WJB_BEGIN_ARRAY);
4224 n = v.val.array.nElems;
4235 PG_RETURN_JSONB(in);
4237 pushJsonbValue(&state, r, NULL);
4239 while ((r = JsonbIteratorNext(&it, &v, true)) != 0)
4247 res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4250 Assert(res != NULL);
4252 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4256 * SQL function jsonb_set(jsonb, text[], jsonb, boolean)
4260 jsonb_set(PG_FUNCTION_ARGS)
4262 Jsonb *in = PG_GETARG_JSONB(0);
4263 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
4264 Jsonb *newval = PG_GETARG_JSONB(2);
4265 bool create = PG_GETARG_BOOL(3);
4266 JsonbValue *res = NULL;
4271 JsonbParseState *st = NULL;
4273 if (ARR_NDIM(path) > 1)
4275 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4276 errmsg("wrong number of array subscripts")));
4278 if (JB_ROOT_IS_SCALAR(in))
4280 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4281 errmsg("cannot set path in scalar")));
4283 if (JB_ROOT_COUNT(in) == 0 && !create)
4284 PG_RETURN_JSONB(in);
4286 deconstruct_array(path, TEXTOID, -1, false, 'i',
4287 &path_elems, &path_nulls, &path_len);
4290 PG_RETURN_JSONB(in);
4292 it = JsonbIteratorInit(&in->root);
4294 res = setPath(&it, path_elems, path_nulls, path_len, &st,
4295 0, newval, create ? JB_PATH_CREATE : JB_PATH_REPLACE);
4297 Assert(res != NULL);
4299 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4304 * SQL function jsonb_delete_path(jsonb, text[])
4307 jsonb_delete_path(PG_FUNCTION_ARGS)
4309 Jsonb *in = PG_GETARG_JSONB(0);
4310 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
4311 JsonbValue *res = NULL;
4316 JsonbParseState *st = NULL;
4318 if (ARR_NDIM(path) > 1)
4320 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4321 errmsg("wrong number of array subscripts")));
4323 if (JB_ROOT_IS_SCALAR(in))
4325 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4326 errmsg("cannot delete path in scalar")));
4328 if (JB_ROOT_COUNT(in) == 0)
4329 PG_RETURN_JSONB(in);
4331 deconstruct_array(path, TEXTOID, -1, false, 'i',
4332 &path_elems, &path_nulls, &path_len);
4335 PG_RETURN_JSONB(in);
4337 it = JsonbIteratorInit(&in->root);
4339 res = setPath(&it, path_elems, path_nulls, path_len, &st,
4340 0, NULL, JB_PATH_DELETE);
4342 Assert(res != NULL);
4344 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4348 * SQL function jsonb_insert(jsonb, text[], jsonb, boolean)
4352 jsonb_insert(PG_FUNCTION_ARGS)
4354 Jsonb *in = PG_GETARG_JSONB(0);
4355 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
4356 Jsonb *newval = PG_GETARG_JSONB(2);
4357 bool after = PG_GETARG_BOOL(3);
4358 JsonbValue *res = NULL;
4363 JsonbParseState *st = NULL;
4365 if (ARR_NDIM(path) > 1)
4367 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4368 errmsg("wrong number of array subscripts")));
4370 if (JB_ROOT_IS_SCALAR(in))
4372 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4373 errmsg("cannot set path in scalar")));
4375 deconstruct_array(path, TEXTOID, -1, false, 'i',
4376 &path_elems, &path_nulls, &path_len);
4379 PG_RETURN_JSONB(in);
4381 it = JsonbIteratorInit(&in->root);
4383 res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, newval,
4384 after ? JB_PATH_INSERT_AFTER : JB_PATH_INSERT_BEFORE);
4386 Assert(res != NULL);
4388 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4392 * Iterate over all jsonb objects and merge them into one.
4393 * The logic of this function copied from the same hstore function,
4394 * except the case, when it1 & it2 represents jbvObject.
4395 * In that case we just append the content of it2 to it1 without any
4399 IteratorConcat(JsonbIterator **it1, JsonbIterator **it2,
4400 JsonbParseState **state)
4405 JsonbIteratorToken r1,
4410 r1 = rk1 = JsonbIteratorNext(it1, &v1, false);
4411 r2 = rk2 = JsonbIteratorNext(it2, &v2, false);
4414 * Both elements are objects.
4416 if (rk1 == WJB_BEGIN_OBJECT && rk2 == WJB_BEGIN_OBJECT)
4419 * Append the all tokens from v1 to res, except last WJB_END_OBJECT
4420 * (because res will not be finished yet).
4422 pushJsonbValue(state, r1, NULL);
4423 while ((r1 = JsonbIteratorNext(it1, &v1, true)) != WJB_END_OBJECT)
4424 pushJsonbValue(state, r1, &v1);
4427 * Append the all tokens from v2 to res, include last WJB_END_OBJECT
4428 * (the concatenation will be completed).
4430 while ((r2 = JsonbIteratorNext(it2, &v2, true)) != 0)
4431 res = pushJsonbValue(state, r2, r2 != WJB_END_OBJECT ? &v2 : NULL);
4435 * Both elements are arrays (either can be scalar).
4437 else if (rk1 == WJB_BEGIN_ARRAY && rk2 == WJB_BEGIN_ARRAY)
4439 pushJsonbValue(state, r1, NULL);
4441 while ((r1 = JsonbIteratorNext(it1, &v1, true)) != WJB_END_ARRAY)
4443 Assert(r1 == WJB_ELEM);
4444 pushJsonbValue(state, r1, &v1);
4447 while ((r2 = JsonbIteratorNext(it2, &v2, true)) != WJB_END_ARRAY)
4449 Assert(r2 == WJB_ELEM);
4450 pushJsonbValue(state, WJB_ELEM, &v2);
4453 res = pushJsonbValue(state, WJB_END_ARRAY, NULL /* signal to sort */ );
4455 /* have we got array || object or object || array? */
4456 else if (((rk1 == WJB_BEGIN_ARRAY && !(*it1)->isScalar) && rk2 == WJB_BEGIN_OBJECT) ||
4457 (rk1 == WJB_BEGIN_OBJECT && (rk2 == WJB_BEGIN_ARRAY && !(*it2)->isScalar)))
4460 JsonbIterator **it_array = rk1 == WJB_BEGIN_ARRAY ? it1 : it2;
4461 JsonbIterator **it_object = rk1 == WJB_BEGIN_OBJECT ? it1 : it2;
4463 bool prepend = (rk1 == WJB_BEGIN_OBJECT);
4465 pushJsonbValue(state, WJB_BEGIN_ARRAY, NULL);
4469 pushJsonbValue(state, WJB_BEGIN_OBJECT, NULL);
4470 while ((r1 = JsonbIteratorNext(it_object, &v1, true)) != 0)
4471 pushJsonbValue(state, r1, r1 != WJB_END_OBJECT ? &v1 : NULL);
4473 while ((r2 = JsonbIteratorNext(it_array, &v2, true)) != 0)
4474 res = pushJsonbValue(state, r2, r2 != WJB_END_ARRAY ? &v2 : NULL);
4478 while ((r1 = JsonbIteratorNext(it_array, &v1, true)) != WJB_END_ARRAY)
4479 pushJsonbValue(state, r1, &v1);
4481 pushJsonbValue(state, WJB_BEGIN_OBJECT, NULL);
4482 while ((r2 = JsonbIteratorNext(it_object, &v2, true)) != 0)
4483 pushJsonbValue(state, r2, r2 != WJB_END_OBJECT ? &v2 : NULL);
4485 res = pushJsonbValue(state, WJB_END_ARRAY, NULL);
4491 * This must be scalar || object or object || scalar, as that's all
4492 * that's left. Both of these make no sense, so error out.
4495 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4496 errmsg("invalid concatenation of jsonb objects")));
4503 * Do most of the heavy work for jsonb_set/jsonb_insert
4505 * If JB_PATH_DELETE bit is set in op_type, the element is to be removed.
4507 * If any bit mentioned in JB_PATH_CREATE_OR_INSERT is set in op_type,
4508 * we create the new value if the key or array index does not exist.
4510 * Bits JB_PATH_INSERT_BEFORE and JB_PATH_INSERT_AFTER in op_type
4511 * behave as JB_PATH_CREATE if new value is inserted in JsonbObject.
4513 * All path elements before the last must already exist
4514 * whatever bits in op_type are set, or nothing is done.
4517 setPath(JsonbIterator **it, Datum *path_elems,
4518 bool *path_nulls, int path_len,
4519 JsonbParseState **st, int level, Jsonb *newval, int op_type)
4522 JsonbIteratorToken r;
4525 check_stack_depth();
4527 if (path_nulls[level])
4529 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4530 errmsg("path element at position %d is null",
4533 r = JsonbIteratorNext(it, &v, false);
4537 case WJB_BEGIN_ARRAY:
4538 (void) pushJsonbValue(st, r, NULL);
4539 setPathArray(it, path_elems, path_nulls, path_len, st, level,
4540 newval, v.val.array.nElems, op_type);
4541 r = JsonbIteratorNext(it, &v, false);
4542 Assert(r == WJB_END_ARRAY);
4543 res = pushJsonbValue(st, r, NULL);
4545 case WJB_BEGIN_OBJECT:
4546 (void) pushJsonbValue(st, r, NULL);
4547 setPathObject(it, path_elems, path_nulls, path_len, st, level,
4548 newval, v.val.object.nPairs, op_type);
4549 r = JsonbIteratorNext(it, &v, true);
4550 Assert(r == WJB_END_OBJECT);
4551 res = pushJsonbValue(st, r, NULL);
4555 res = pushJsonbValue(st, r, &v);
4558 elog(ERROR, "unrecognized iterator result: %d", (int) r);
4559 res = NULL; /* keep compiler quiet */
4567 * Object walker for setPath
4570 setPathObject(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
4571 int path_len, JsonbParseState **st, int level,
4572 Jsonb *newval, uint32 npairs, int op_type)
4579 if (level >= path_len || path_nulls[level])
4582 /* empty object is a special case for create */
4583 if ((npairs == 0) && (op_type & JB_PATH_CREATE_OR_INSERT) &&
4584 (level == path_len - 1))
4588 newkey.type = jbvString;
4589 newkey.val.string.len = VARSIZE_ANY_EXHDR(path_elems[level]);
4590 newkey.val.string.val = VARDATA_ANY(path_elems[level]);
4592 (void) pushJsonbValue(st, WJB_KEY, &newkey);
4593 addJsonbToParseState(st, newval);
4596 for (i = 0; i < npairs; i++)
4598 JsonbIteratorToken r = JsonbIteratorNext(it, &k, true);
4600 Assert(r == WJB_KEY);
4603 k.val.string.len == VARSIZE_ANY_EXHDR(path_elems[level]) &&
4604 memcmp(k.val.string.val, VARDATA_ANY(path_elems[level]),
4605 k.val.string.len) == 0)
4607 if (level == path_len - 1)
4610 * called from jsonb_insert(), it forbids redefining an
4613 if (op_type & (JB_PATH_INSERT_BEFORE | JB_PATH_INSERT_AFTER))
4615 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4616 errmsg("cannot replace existing key"),
4617 errhint("Try using the function jsonb_set "
4618 "to replace key value.")));
4620 r = JsonbIteratorNext(it, &v, true); /* skip value */
4621 if (!(op_type & JB_PATH_DELETE))
4623 (void) pushJsonbValue(st, WJB_KEY, &k);
4624 addJsonbToParseState(st, newval);
4630 (void) pushJsonbValue(st, r, &k);
4631 setPath(it, path_elems, path_nulls, path_len,
4632 st, level + 1, newval, op_type);
4637 if ((op_type & JB_PATH_CREATE_OR_INSERT) && !done &&
4638 level == path_len - 1 && i == npairs - 1)
4642 newkey.type = jbvString;
4643 newkey.val.string.len = VARSIZE_ANY_EXHDR(path_elems[level]);
4644 newkey.val.string.val = VARDATA_ANY(path_elems[level]);
4646 (void) pushJsonbValue(st, WJB_KEY, &newkey);
4647 addJsonbToParseState(st, newval);
4650 (void) pushJsonbValue(st, r, &k);
4651 r = JsonbIteratorNext(it, &v, false);
4652 (void) pushJsonbValue(st, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4653 if (r == WJB_BEGIN_ARRAY || r == WJB_BEGIN_OBJECT)
4655 int walking_level = 1;
4657 while (walking_level != 0)
4659 r = JsonbIteratorNext(it, &v, false);
4661 if (r == WJB_BEGIN_ARRAY || r == WJB_BEGIN_OBJECT)
4663 if (r == WJB_END_ARRAY || r == WJB_END_OBJECT)
4666 (void) pushJsonbValue(st, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4674 * Array walker for setPath
4677 setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
4678 int path_len, JsonbParseState **st, int level,
4679 Jsonb *newval, uint32 nelems, int op_type)
4686 /* pick correct index */
4687 if (level < path_len && !path_nulls[level])
4689 char *c = TextDatumGetCString(path_elems[level]);
4694 lindex = strtol(c, &badp, 10);
4695 if (errno != 0 || badp == c || *badp != '\0' || lindex > INT_MAX ||
4698 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
4699 errmsg("path element at position %d is not an integer: \"%s\"",
4714 if (idx > 0 && idx > nelems)
4718 * if we're creating, and idx == INT_MIN, we prepend the new value to the
4719 * array also if the array is empty - in which case we don't really care
4720 * what the idx value is
4723 if ((idx == INT_MIN || nelems == 0) && (level == path_len - 1) &&
4724 (op_type & JB_PATH_CREATE_OR_INSERT))
4726 Assert(newval != NULL);
4727 addJsonbToParseState(st, newval);
4731 /* iterate over the array elements */
4732 for (i = 0; i < nelems; i++)
4734 JsonbIteratorToken r;
4736 if (i == idx && level < path_len)
4738 if (level == path_len - 1)
4740 r = JsonbIteratorNext(it, &v, true); /* skip */
4742 if (op_type & (JB_PATH_INSERT_BEFORE | JB_PATH_CREATE))
4743 addJsonbToParseState(st, newval);
4746 * We should keep current value only in case of
4747 * JB_PATH_INSERT_BEFORE or JB_PATH_INSERT_AFTER because
4748 * otherwise it should be deleted or replaced
4750 if (op_type & (JB_PATH_INSERT_AFTER | JB_PATH_INSERT_BEFORE))
4751 (void) pushJsonbValue(st, r, &v);
4753 if (op_type & (JB_PATH_INSERT_AFTER | JB_PATH_REPLACE))
4754 addJsonbToParseState(st, newval);
4759 (void) setPath(it, path_elems, path_nulls, path_len,
4760 st, level + 1, newval, op_type);
4764 r = JsonbIteratorNext(it, &v, false);
4766 (void) pushJsonbValue(st, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4768 if (r == WJB_BEGIN_ARRAY || r == WJB_BEGIN_OBJECT)
4770 int walking_level = 1;
4772 while (walking_level != 0)
4774 r = JsonbIteratorNext(it, &v, false);
4776 if (r == WJB_BEGIN_ARRAY || r == WJB_BEGIN_OBJECT)
4778 if (r == WJB_END_ARRAY || r == WJB_END_OBJECT)
4781 (void) pushJsonbValue(st, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4785 if ((op_type & JB_PATH_CREATE_OR_INSERT) && !done &&
4786 level == path_len - 1 && i == nelems - 1)
4788 addJsonbToParseState(st, newval);
4795 * Iterate over jsonb string values or elements, and pass them together with an
4796 * iteration state to a specified JsonIterateStringValuesAction.
4799 iterate_jsonb_string_values(Jsonb *jb, void *state, JsonIterateStringValuesAction action)
4803 JsonbIteratorToken type;
4805 it = JsonbIteratorInit(&jb->root);
4807 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
4809 if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
4811 action(state, v.val.string.val, v.val.string.len);
4817 * Iterate over json string values or elements, and pass them together with an
4818 * iteration state to a specified JsonIterateStringValuesAction.
4821 iterate_json_string_values(text *json, void *action_state, JsonIterateStringValuesAction action)
4823 JsonLexContext *lex = makeJsonLexContext(json, true);
4824 JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
4825 IterateJsonStringValuesState *state = palloc0(sizeof(IterateJsonStringValuesState));
4828 state->action = action;
4829 state->action_state = action_state;
4831 sem->semstate = (void *) state;
4832 sem->scalar = iterate_string_values_scalar;
4834 pg_parse_json(lex, sem);
4838 * An auxiliary function for iterate_json_string_values to invoke a specified
4839 * JsonIterateStringValuesAction.
4842 iterate_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
4844 IterateJsonStringValuesState *_state = (IterateJsonStringValuesState *) state;
4846 if (tokentype == JSON_TOKEN_STRING)
4847 (*_state->action) (_state->action_state, token, strlen(token));
4851 * Iterate over a jsonb, and apply a specified JsonTransformStringValuesAction
4852 * to every string value or element. Any necessary context for a
4853 * JsonTransformStringValuesAction can be passed in the action_state variable.
4854 * Function returns a copy of an original jsonb object with transformed values.
4857 transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
4858 JsonTransformStringValuesAction transform_action)
4863 JsonbIteratorToken type;
4864 JsonbParseState *st = NULL;
4866 bool is_scalar = false;
4868 it = JsonbIteratorInit(&jsonb->root);
4869 is_scalar = it->isScalar;
4871 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
4873 if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
4875 out = transform_action(action_state, v.val.string.val, v.val.string.len);
4876 v.val.string.val = VARDATA_ANY(out);
4877 v.val.string.len = VARSIZE_ANY_EXHDR(out);
4878 res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL);
4882 res = pushJsonbValue(&st, type, (type == WJB_KEY ||
4883 type == WJB_VALUE ||
4884 type == WJB_ELEM) ? &v : NULL);
4888 if (res->type == jbvArray)
4889 res->val.array.rawScalar = is_scalar;
4891 return JsonbValueToJsonb(res);
4895 * Iterate over a json, and apply a specified JsonTransformStringValuesAction
4896 * to every string value or element. Any necessary context for a
4897 * JsonTransformStringValuesAction can be passed in the action_state variable.
4898 * Function returns a StringInfo, which is a copy of an original json with
4899 * transformed values.
4902 transform_json_string_values(text *json, void *action_state,
4903 JsonTransformStringValuesAction transform_action)
4905 JsonLexContext *lex = makeJsonLexContext(json, true);
4906 JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
4907 TransformJsonStringValuesState *state = palloc0(sizeof(TransformJsonStringValuesState));
4910 state->strval = makeStringInfo();
4911 state->action = transform_action;
4912 state->action_state = action_state;
4914 sem->semstate = (void *) state;
4915 sem->scalar = transform_string_values_scalar;
4916 sem->object_start = transform_string_values_object_start;
4917 sem->object_end = transform_string_values_object_end;
4918 sem->array_start = transform_string_values_array_start;
4919 sem->array_end = transform_string_values_array_end;
4920 sem->scalar = transform_string_values_scalar;
4921 sem->array_element_start = transform_string_values_array_element_start;
4922 sem->object_field_start = transform_string_values_object_field_start;
4924 pg_parse_json(lex, sem);
4926 return cstring_to_text_with_len(state->strval->data, state->strval->len);
4930 * Set of auxiliary functions for transform_json_string_values to invoke a
4931 * specified JsonTransformStringValuesAction for all values and left everything
4935 transform_string_values_object_start(void *state)
4937 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4939 appendStringInfoCharMacro(_state->strval, '{');
4943 transform_string_values_object_end(void *state)
4945 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4947 appendStringInfoCharMacro(_state->strval, '}');
4951 transform_string_values_array_start(void *state)
4953 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4955 appendStringInfoCharMacro(_state->strval, '[');
4959 transform_string_values_array_end(void *state)
4961 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4963 appendStringInfoCharMacro(_state->strval, ']');
4967 transform_string_values_object_field_start(void *state, char *fname, bool isnull)
4969 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4971 if (_state->strval->data[_state->strval->len - 1] != '{')
4972 appendStringInfoCharMacro(_state->strval, ',');
4975 * Unfortunately we don't have the quoted and escaped string any more, so
4976 * we have to re-escape it.
4978 escape_json(_state->strval, fname);
4979 appendStringInfoCharMacro(_state->strval, ':');
4983 transform_string_values_array_element_start(void *state, bool isnull)
4985 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4987 if (_state->strval->data[_state->strval->len - 1] != '[')
4988 appendStringInfoCharMacro(_state->strval, ',');
4992 transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
4994 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4996 if (tokentype == JSON_TOKEN_STRING)
4998 text *out = (*_state->action) (_state->action_state, token, strlen(token));
5000 escape_json(_state->strval, text_to_cstring(out));
5003 appendStringInfoString(_state->strval, token);