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 applied
61 * 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 applied
71 * 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
91 /* state for json_array_length */
92 typedef struct AlenState
98 /* state for json_each */
99 typedef struct EachState
102 Tuplestorestate *tuple_store;
104 MemoryContext tmp_cxt;
106 bool normalize_results;
108 char *normalized_scalar;
111 /* state for json_array_elements */
112 typedef struct ElementsState
115 const char *function_name;
116 Tuplestorestate *tuple_store;
118 MemoryContext tmp_cxt;
120 bool normalize_results;
122 char *normalized_scalar;
125 /* state for get_json_object_as_hash */
126 typedef struct JHashState
129 const char *function_name;
132 char *save_json_start;
133 JsonTokenType saved_token_type;
136 /* hashtable element */
137 typedef struct JsonHashEntry
139 char fname[NAMEDATALEN]; /* hash key (MUST BE FIRST) */
144 /* structure to cache type I/O metadata needed for populate_scalar() */
145 typedef struct ScalarIOData
151 /* these two structures are used recursively */
152 typedef struct ColumnIOData ColumnIOData;
153 typedef struct RecordIOData RecordIOData;
155 /* structure to cache metadata needed for populate_array() */
156 typedef struct ArrayIOData
158 ColumnIOData *element_info; /* metadata cache */
159 Oid element_type; /* array element type id */
160 int32 element_typmod; /* array element type modifier */
163 /* structure to cache metadata needed for populate_composite() */
164 typedef struct CompositeIOData
167 * We use pointer to a RecordIOData here because variable-length struct
168 * RecordIOData can't be used directly in ColumnIOData.io union
170 RecordIOData *record_io; /* metadata cache for populate_record() */
171 TupleDesc tupdesc; /* cached tuple descriptor */
174 /* structure to cache metadata needed for populate_domain() */
175 typedef struct DomainIOData
177 ColumnIOData *base_io; /* metadata cache */
178 Oid base_typid; /* base type id */
179 int32 base_typmod; /* base type modifier */
180 void *domain_info; /* opaque cache for domain checks */
183 /* enumeration type categories */
186 TYPECAT_SCALAR = 's',
188 TYPECAT_COMPOSITE = 'c',
192 /* these two are stolen from hstore / record_out, used in populate_record* */
194 /* structure to cache record metadata needed for populate_record_field() */
197 Oid typid; /* column type id */
198 int32 typmod; /* column type modifier */
199 TypeCat typcat; /* column type category */
200 ScalarIOData scalar_io; /* metadata cache for directi conversion
201 * through input function */
205 CompositeIOData composite;
207 } io; /* metadata cache for various column type
211 /* structure to cache record metadata needed for populate_record() */
217 ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
220 /* state for populate_recordset */
221 typedef struct PopulateRecordsetState
224 const char *function_name;
227 char *save_json_start;
228 JsonTokenType saved_token_type;
229 Tuplestorestate *tuple_store;
232 RecordIOData **my_extra;
233 MemoryContext fn_mcxt; /* used to stash IO funcs */
234 } PopulateRecordsetState;
236 /* structure to cache metadata needed for populate_record_worker() */
237 typedef struct PopulateRecordCache
239 Oid argtype; /* verified row type of the first argument */
240 CompositeIOData io; /* metadata cache for populate_composite() */
241 } PopulateRecordCache;
243 /* common data for populate_array_json() and populate_array_dim_jsonb() */
244 typedef struct PopulateArrayContext
246 ArrayBuildState *astate; /* array build state */
247 ArrayIOData *aio; /* metadata cache */
248 MemoryContext acxt; /* array build memory context */
249 MemoryContext mcxt; /* cache memory context */
250 const char *colname; /* for diagnostics only */
251 int *dims; /* dimensions */
252 int *sizes; /* current dimension counters */
253 int ndims; /* number of dimensions */
254 } PopulateArrayContext;
256 /* state for populate_array_json() */
257 typedef struct PopulateArrayState
259 JsonLexContext *lex; /* json lexer */
260 PopulateArrayContext *ctx; /* context */
261 char *element_start; /* start of the current array element */
262 char *element_scalar; /* current array element token if it is a
264 JsonTokenType element_type; /* current array element type */
265 } PopulateArrayState;
267 /* state for json_strip_nulls */
268 typedef struct StripnullState
275 /* structure for generalized json/jsonb value passing */
276 typedef struct JsValue
278 bool is_json; /* json/jsonb */
283 char *str; /* json string */
284 int len; /* json string length or -1 if null-terminated */
285 JsonTokenType type; /* json type */
286 } json; /* json value */
288 JsonbValue *jsonb; /* jsonb value */
292 typedef struct JsObject
294 bool is_json; /* json/jsonb */
298 JsonbContainer *jsonb_cont;
302 /* useful macros for testing JsValue properties */
303 #define JsValueIsNull(jsv) \
305 (!(jsv)->val.json.str || (jsv)->val.json.type == JSON_TOKEN_NULL) : \
306 (!(jsv)->val.jsonb || (jsv)->val.jsonb->type == jbvNull))
308 #define JsValueIsString(jsv) \
309 ((jsv)->is_json ? (jsv)->val.json.type == JSON_TOKEN_STRING \
310 : ((jsv)->val.jsonb && (jsv)->val.jsonb->type == jbvString))
312 #define JsObjectIsEmpty(jso) \
314 ? hash_get_num_entries((jso)->val.json_hash) == 0 \
315 : ((jso)->val.jsonb_cont == NULL || \
316 JsonContainerSize((jso)->val.jsonb_cont) == 0))
318 #define JsObjectFree(jso) \
320 if ((jso)->is_json) \
321 hash_destroy((jso)->val.json_hash); \
324 /* semantic action functions for json_object_keys */
325 static void okeys_object_field_start(void *state, char *fname, bool isnull);
326 static void okeys_array_start(void *state);
327 static void okeys_scalar(void *state, char *token, JsonTokenType tokentype);
329 /* semantic action functions for json_get* functions */
330 static void get_object_start(void *state);
331 static void get_object_end(void *state);
332 static void get_object_field_start(void *state, char *fname, bool isnull);
333 static void get_object_field_end(void *state, char *fname, bool isnull);
334 static void get_array_start(void *state);
335 static void get_array_end(void *state);
336 static void get_array_element_start(void *state, bool isnull);
337 static void get_array_element_end(void *state, bool isnull);
338 static void get_scalar(void *state, char *token, JsonTokenType tokentype);
340 /* common worker function for json getter functions */
341 static Datum get_path_all(FunctionCallInfo fcinfo, bool as_text);
342 static text *get_worker(text *json, char **tpath, int *ipath, int npath,
343 bool normalize_results);
344 static Datum get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text);
346 /* semantic action functions for json_array_length */
347 static void alen_object_start(void *state);
348 static void alen_scalar(void *state, char *token, JsonTokenType tokentype);
349 static void alen_array_element_start(void *state, bool isnull);
351 /* common workers for json{b}_each* functions */
352 static Datum each_worker(FunctionCallInfo fcinfo, bool as_text);
353 static Datum each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
356 /* semantic action functions for json_each */
357 static void each_object_field_start(void *state, char *fname, bool isnull);
358 static void each_object_field_end(void *state, char *fname, bool isnull);
359 static void each_array_start(void *state);
360 static void each_scalar(void *state, char *token, JsonTokenType tokentype);
362 /* common workers for json{b}_array_elements_* functions */
363 static Datum elements_worker(FunctionCallInfo fcinfo, const char *funcname,
365 static Datum elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
368 /* semantic action functions for json_array_elements */
369 static void elements_object_start(void *state);
370 static void elements_array_element_start(void *state, bool isnull);
371 static void elements_array_element_end(void *state, bool isnull);
372 static void elements_scalar(void *state, char *token, JsonTokenType tokentype);
374 /* turn a json object into a hash table */
375 static HTAB *get_json_object_as_hash(char *json, int len, const char *funcname);
377 /* semantic actions for populate_array_json */
378 static void populate_array_object_start(void *_state);
379 static void populate_array_array_end(void *_state);
380 static void populate_array_element_start(void *_state, bool isnull);
381 static void populate_array_element_end(void *_state, bool isnull);
382 static void populate_array_scalar(void *_state, char *token, JsonTokenType tokentype);
384 /* semantic action functions for get_json_object_as_hash */
385 static void hash_object_field_start(void *state, char *fname, bool isnull);
386 static void hash_object_field_end(void *state, char *fname, bool isnull);
387 static void hash_array_start(void *state);
388 static void hash_scalar(void *state, char *token, JsonTokenType tokentype);
390 /* semantic action functions for populate_recordset */
391 static void populate_recordset_object_field_start(void *state, char *fname, bool isnull);
392 static void populate_recordset_object_field_end(void *state, char *fname, bool isnull);
393 static void populate_recordset_scalar(void *state, char *token, JsonTokenType tokentype);
394 static void populate_recordset_object_start(void *state);
395 static void populate_recordset_object_end(void *state);
396 static void populate_recordset_array_start(void *state);
397 static void populate_recordset_array_element_start(void *state, bool isnull);
399 /* semantic action functions for json_strip_nulls */
400 static void sn_object_start(void *state);
401 static void sn_object_end(void *state);
402 static void sn_array_start(void *state);
403 static void sn_array_end(void *state);
404 static void sn_object_field_start(void *state, char *fname, bool isnull);
405 static void sn_array_element_start(void *state, bool isnull);
406 static void sn_scalar(void *state, char *token, JsonTokenType tokentype);
408 /* worker functions for populate_record, to_record, populate_recordset and to_recordset */
409 static Datum populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
410 bool have_record_arg);
411 static Datum populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
412 bool have_record_arg);
414 /* helper functions for populate_record[set] */
415 static HeapTupleHeader populate_record(TupleDesc tupdesc, RecordIOData **record_p,
416 HeapTupleHeader defaultval, MemoryContext mcxt,
418 static Datum populate_record_field(ColumnIOData *col, Oid typid, int32 typmod,
419 const char *colname, MemoryContext mcxt,
420 Datum defaultval, JsValue *jsv, bool *isnull);
421 static void JsValueToJsObject(JsValue *jsv, JsObject *jso);
422 static Datum populate_composite(CompositeIOData *io, Oid typid, int32 typmod,
423 const char *colname, MemoryContext mcxt,
424 HeapTupleHeader defaultval, JsValue *jsv);
425 static Datum populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv);
426 static void prepare_column_cache(ColumnIOData *column, Oid typid, int32 typmod,
427 MemoryContext mcxt, bool json);
428 static Datum populate_record_field(ColumnIOData *col, Oid typid, int32 typmod,
429 const char *colname, MemoryContext mcxt, Datum defaultval,
430 JsValue *jsv, bool *isnull);
431 static RecordIOData *allocate_record_info(MemoryContext mcxt, int ncolumns);
432 static bool JsObjectGetField(JsObject *obj, char *field, JsValue *jsv);
433 static void populate_recordset_record(PopulateRecordsetState *state, JsObject *obj);
434 static void populate_array_json(PopulateArrayContext *ctx, char *json, int len);
435 static void populate_array_dim_jsonb(PopulateArrayContext *ctx, JsonbValue *jbv,
437 static void populate_array_report_expected_array(PopulateArrayContext *ctx, int ndim);
438 static void populate_array_assign_ndims(PopulateArrayContext *ctx, int ndims);
439 static void populate_array_check_dimension(PopulateArrayContext *ctx, int ndim);
440 static void populate_array_element(PopulateArrayContext *ctx, int ndim, JsValue *jsv);
441 static Datum populate_array(ArrayIOData *aio, const char *colname,
442 MemoryContext mcxt, JsValue *jsv);
443 static Datum populate_domain(DomainIOData *io, Oid typid, const char *colname,
444 MemoryContext mcxt, JsValue *jsv, bool isnull);
446 /* Worker that takes care of common setup for us */
447 static JsonbValue *findJsonbValueFromContainerLen(JsonbContainer *container,
452 /* functions supporting jsonb_delete, jsonb_set and jsonb_concat */
453 static JsonbValue *IteratorConcat(JsonbIterator **it1, JsonbIterator **it2,
454 JsonbParseState **state);
455 static JsonbValue *setPath(JsonbIterator **it, Datum *path_elems,
456 bool *path_nulls, int path_len,
457 JsonbParseState **st, int level, Jsonb *newval,
459 static void setPathObject(JsonbIterator **it, Datum *path_elems,
460 bool *path_nulls, int path_len, JsonbParseState **st,
462 Jsonb *newval, uint32 npairs, int op_type);
463 static void setPathArray(JsonbIterator **it, Datum *path_elems,
464 bool *path_nulls, int path_len, JsonbParseState **st,
465 int level, Jsonb *newval, uint32 nelems, int op_type);
466 static void addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb);
468 /* function supporting iterate_json_string_values */
469 static void iterate_string_values_scalar(void *state, char *token, JsonTokenType tokentype);
471 /* functions supporting transform_json_string_values */
472 static void transform_string_values_object_start(void *state);
473 static void transform_string_values_object_end(void *state);
474 static void transform_string_values_array_start(void *state);
475 static void transform_string_values_array_end(void *state);
476 static void transform_string_values_object_field_start(void *state, char *fname, bool isnull);
477 static void transform_string_values_array_element_start(void *state, bool isnull);
478 static void transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype);
481 * SQL function json_object_keys
483 * Returns the set of keys for the object argument.
485 * This SRF operates in value-per-call mode. It processes the
486 * object during the first call, and the keys are simply stashed
487 * in an array, whose size is expanded as necessary. This is probably
488 * safe enough for a list of keys of a single object, since they are
489 * limited in size to NAMEDATALEN and the number of keys is unlikely to
490 * be so huge that it has major memory implications.
493 jsonb_object_keys(PG_FUNCTION_ARGS)
495 FuncCallContext *funcctx;
499 if (SRF_IS_FIRSTCALL())
501 MemoryContext oldcontext;
502 Jsonb *jb = PG_GETARG_JSONB(0);
503 bool skipNested = false;
506 JsonbIteratorToken r;
508 if (JB_ROOT_IS_SCALAR(jb))
510 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
511 errmsg("cannot call %s on a scalar",
512 "jsonb_object_keys")));
513 else if (JB_ROOT_IS_ARRAY(jb))
515 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
516 errmsg("cannot call %s on an array",
517 "jsonb_object_keys")));
519 funcctx = SRF_FIRSTCALL_INIT();
520 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
522 state = palloc(sizeof(OkeysState));
524 state->result_size = JB_ROOT_COUNT(jb);
525 state->result_count = 0;
526 state->sent_count = 0;
527 state->result = palloc(state->result_size * sizeof(char *));
529 it = JsonbIteratorInit(&jb->root);
531 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
539 cstr = palloc(v.val.string.len + 1 * sizeof(char));
540 memcpy(cstr, v.val.string.val, v.val.string.len);
541 cstr[v.val.string.len] = '\0';
542 state->result[state->result_count++] = cstr;
546 MemoryContextSwitchTo(oldcontext);
547 funcctx->user_fctx = (void *) state;
550 funcctx = SRF_PERCALL_SETUP();
551 state = (OkeysState *) funcctx->user_fctx;
553 if (state->sent_count < state->result_count)
555 char *nxt = state->result[state->sent_count++];
557 SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt));
560 /* cleanup to reduce or eliminate memory leaks */
561 for (i = 0; i < state->result_count; i++)
562 pfree(state->result[i]);
563 pfree(state->result);
566 SRF_RETURN_DONE(funcctx);
571 json_object_keys(PG_FUNCTION_ARGS)
573 FuncCallContext *funcctx;
577 if (SRF_IS_FIRSTCALL())
579 text *json = PG_GETARG_TEXT_PP(0);
580 JsonLexContext *lex = makeJsonLexContext(json, true);
582 MemoryContext oldcontext;
584 funcctx = SRF_FIRSTCALL_INIT();
585 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
587 state = palloc(sizeof(OkeysState));
588 sem = palloc0(sizeof(JsonSemAction));
591 state->result_size = 256;
592 state->result_count = 0;
593 state->sent_count = 0;
594 state->result = palloc(256 * sizeof(char *));
596 sem->semstate = (void *) state;
597 sem->array_start = okeys_array_start;
598 sem->scalar = okeys_scalar;
599 sem->object_field_start = okeys_object_field_start;
600 /* remainder are all NULL, courtesy of palloc0 above */
602 pg_parse_json(lex, sem);
603 /* keys are now in state->result */
605 pfree(lex->strval->data);
610 MemoryContextSwitchTo(oldcontext);
611 funcctx->user_fctx = (void *) state;
614 funcctx = SRF_PERCALL_SETUP();
615 state = (OkeysState *) funcctx->user_fctx;
617 if (state->sent_count < state->result_count)
619 char *nxt = state->result[state->sent_count++];
621 SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(nxt));
624 /* cleanup to reduce or eliminate memory leaks */
625 for (i = 0; i < state->result_count; i++)
626 pfree(state->result[i]);
627 pfree(state->result);
630 SRF_RETURN_DONE(funcctx);
634 okeys_object_field_start(void *state, char *fname, bool isnull)
636 OkeysState *_state = (OkeysState *) state;
638 /* only collecting keys for the top level object */
639 if (_state->lex->lex_level != 1)
642 /* enlarge result array if necessary */
643 if (_state->result_count >= _state->result_size)
645 _state->result_size *= 2;
646 _state->result = (char **)
647 repalloc(_state->result, sizeof(char *) * _state->result_size);
650 /* save a copy of the field name */
651 _state->result[_state->result_count++] = pstrdup(fname);
655 okeys_array_start(void *state)
657 OkeysState *_state = (OkeysState *) state;
659 /* top level must be a json object */
660 if (_state->lex->lex_level == 0)
662 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
663 errmsg("cannot call %s on an array",
664 "json_object_keys")));
668 okeys_scalar(void *state, char *token, JsonTokenType tokentype)
670 OkeysState *_state = (OkeysState *) state;
672 /* top level must be a json object */
673 if (_state->lex->lex_level == 0)
675 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
676 errmsg("cannot call %s on a scalar",
677 "json_object_keys")));
681 * json and jsonb getter functions
682 * these implement the -> ->> #> and #>> operators
683 * and the json{b?}_extract_path*(json, text, ...) functions
688 json_object_field(PG_FUNCTION_ARGS)
690 text *json = PG_GETARG_TEXT_PP(0);
691 text *fname = PG_GETARG_TEXT_PP(1);
692 char *fnamestr = text_to_cstring(fname);
695 result = get_worker(json, &fnamestr, NULL, 1, false);
698 PG_RETURN_TEXT_P(result);
704 jsonb_object_field(PG_FUNCTION_ARGS)
706 Jsonb *jb = PG_GETARG_JSONB(0);
707 text *key = PG_GETARG_TEXT_PP(1);
710 if (!JB_ROOT_IS_OBJECT(jb))
713 v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT,
715 VARSIZE_ANY_EXHDR(key));
718 PG_RETURN_JSONB(JsonbValueToJsonb(v));
724 json_object_field_text(PG_FUNCTION_ARGS)
726 text *json = PG_GETARG_TEXT_PP(0);
727 text *fname = PG_GETARG_TEXT_PP(1);
728 char *fnamestr = text_to_cstring(fname);
731 result = get_worker(json, &fnamestr, NULL, 1, true);
734 PG_RETURN_TEXT_P(result);
740 jsonb_object_field_text(PG_FUNCTION_ARGS)
742 Jsonb *jb = PG_GETARG_JSONB(0);
743 text *key = PG_GETARG_TEXT_PP(1);
746 if (!JB_ROOT_IS_OBJECT(jb))
749 v = findJsonbValueFromContainerLen(&jb->root, JB_FOBJECT,
751 VARSIZE_ANY_EXHDR(key));
762 result = cstring_to_text(v->val.boolean ? "true" : "false");
765 result = cstring_to_text_with_len(v->val.string.val, v->val.string.len);
768 result = cstring_to_text(DatumGetCString(DirectFunctionCall1(numeric_out,
769 PointerGetDatum(v->val.numeric))));
773 StringInfo jtext = makeStringInfo();
775 (void) JsonbToCString(jtext, v->val.binary.data, -1);
776 result = cstring_to_text_with_len(jtext->data, jtext->len);
780 elog(ERROR, "unrecognized jsonb type: %d", (int) v->type);
784 PG_RETURN_TEXT_P(result);
791 json_array_element(PG_FUNCTION_ARGS)
793 text *json = PG_GETARG_TEXT_PP(0);
794 int element = PG_GETARG_INT32(1);
797 result = get_worker(json, NULL, &element, 1, false);
800 PG_RETURN_TEXT_P(result);
806 jsonb_array_element(PG_FUNCTION_ARGS)
808 Jsonb *jb = PG_GETARG_JSONB(0);
809 int element = PG_GETARG_INT32(1);
812 if (!JB_ROOT_IS_ARRAY(jb))
815 /* Handle negative subscript */
818 uint32 nelements = JB_ROOT_COUNT(jb);
820 if (-element > nelements)
823 element += nelements;
826 v = getIthJsonbValueFromContainer(&jb->root, element);
828 PG_RETURN_JSONB(JsonbValueToJsonb(v));
834 json_array_element_text(PG_FUNCTION_ARGS)
836 text *json = PG_GETARG_TEXT_PP(0);
837 int element = PG_GETARG_INT32(1);
840 result = get_worker(json, NULL, &element, 1, true);
843 PG_RETURN_TEXT_P(result);
849 jsonb_array_element_text(PG_FUNCTION_ARGS)
851 Jsonb *jb = PG_GETARG_JSONB(0);
852 int element = PG_GETARG_INT32(1);
855 if (!JB_ROOT_IS_ARRAY(jb))
858 /* Handle negative subscript */
861 uint32 nelements = JB_ROOT_COUNT(jb);
863 if (-element > nelements)
866 element += nelements;
869 v = getIthJsonbValueFromContainer(&jb->root, element);
879 result = cstring_to_text(v->val.boolean ? "true" : "false");
882 result = cstring_to_text_with_len(v->val.string.val, v->val.string.len);
885 result = cstring_to_text(DatumGetCString(DirectFunctionCall1(numeric_out,
886 PointerGetDatum(v->val.numeric))));
890 StringInfo jtext = makeStringInfo();
892 (void) JsonbToCString(jtext, v->val.binary.data, -1);
893 result = cstring_to_text_with_len(jtext->data, jtext->len);
897 elog(ERROR, "unrecognized jsonb type: %d", (int) v->type);
901 PG_RETURN_TEXT_P(result);
908 json_extract_path(PG_FUNCTION_ARGS)
910 return get_path_all(fcinfo, false);
914 json_extract_path_text(PG_FUNCTION_ARGS)
916 return get_path_all(fcinfo, true);
920 * common routine for extract_path functions
923 get_path_all(FunctionCallInfo fcinfo, bool as_text)
925 text *json = PG_GETARG_TEXT_PP(0);
926 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
936 * If the array contains any null elements, return NULL, on the grounds
937 * that you'd have gotten NULL if any RHS value were NULL in a nested
938 * series of applications of the -> operator. (Note: because we also
939 * return NULL for error cases such as no-such-field, this is true
940 * regardless of the contents of the rest of the array.)
942 if (array_contains_nulls(path))
945 deconstruct_array(path, TEXTOID, -1, false, 'i',
946 &pathtext, &pathnulls, &npath);
948 tpath = palloc(npath * sizeof(char *));
949 ipath = palloc(npath * sizeof(int));
951 for (i = 0; i < npath; i++)
953 Assert(!pathnulls[i]);
954 tpath[i] = TextDatumGetCString(pathtext[i]);
957 * we have no idea at this stage what structure the document is so
958 * just convert anything in the path that we can to an integer and set
959 * all the other integers to INT_MIN which will never match.
961 if (*tpath[i] != '\0')
967 ind = strtol(tpath[i], &endptr, 10);
968 if (*endptr == '\0' && errno == 0 && ind <= INT_MAX && ind >= INT_MIN)
969 ipath[i] = (int) ind;
977 result = get_worker(json, tpath, ipath, npath, as_text);
980 PG_RETURN_TEXT_P(result);
988 * common worker for all the json getter functions
990 * json: JSON object (in text form)
991 * tpath[]: field name(s) to extract
992 * ipath[]: array index(es) (zero-based) to extract, accepts negatives
993 * npath: length of tpath[] and/or ipath[]
994 * normalize_results: true to de-escape string and null scalars
996 * tpath can be NULL, or any one tpath[] entry can be NULL, if an object
997 * field is not to be matched at that nesting level. Similarly, ipath can
998 * be NULL, or any one ipath[] entry can be INT_MIN if an array element is
999 * not to be matched at that nesting level (a json datum should never be
1000 * large enough to have -INT_MIN elements due to MaxAllocSize restriction).
1003 get_worker(text *json,
1007 bool normalize_results)
1009 JsonLexContext *lex = makeJsonLexContext(json, true);
1010 JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
1011 GetState *state = palloc0(sizeof(GetState));
1016 /* is it "_as_text" variant? */
1017 state->normalize_results = normalize_results;
1018 state->npath = npath;
1019 state->path_names = tpath;
1020 state->path_indexes = ipath;
1021 state->pathok = palloc0(sizeof(bool) * npath);
1022 state->array_cur_index = palloc(sizeof(int) * npath);
1025 state->pathok[0] = true;
1027 sem->semstate = (void *) state;
1030 * Not all variants need all the semantic routines. Only set the ones that
1031 * are actually needed for maximum efficiency.
1033 sem->scalar = get_scalar;
1036 sem->object_start = get_object_start;
1037 sem->object_end = get_object_end;
1038 sem->array_start = get_array_start;
1039 sem->array_end = get_array_end;
1043 sem->object_field_start = get_object_field_start;
1044 sem->object_field_end = get_object_field_end;
1048 sem->array_start = get_array_start;
1049 sem->array_element_start = get_array_element_start;
1050 sem->array_element_end = get_array_element_end;
1053 pg_parse_json(lex, sem);
1055 return state->tresult;
1059 get_object_start(void *state)
1061 GetState *_state = (GetState *) state;
1062 int lex_level = _state->lex->lex_level;
1064 if (lex_level == 0 && _state->npath == 0)
1067 * Special case: we should match the entire object. We only need this
1068 * at outermost level because at nested levels the match will have
1069 * been started by the outer field or array element callback.
1071 _state->result_start = _state->lex->token_start;
1076 get_object_end(void *state)
1078 GetState *_state = (GetState *) state;
1079 int lex_level = _state->lex->lex_level;
1081 if (lex_level == 0 && _state->npath == 0)
1083 /* Special case: return the entire object */
1084 char *start = _state->result_start;
1085 int len = _state->lex->prev_token_terminator - start;
1087 _state->tresult = cstring_to_text_with_len(start, len);
1092 get_object_field_start(void *state, char *fname, bool isnull)
1094 GetState *_state = (GetState *) state;
1095 bool get_next = false;
1096 int lex_level = _state->lex->lex_level;
1098 if (lex_level <= _state->npath &&
1099 _state->pathok[lex_level - 1] &&
1100 _state->path_names != NULL &&
1101 _state->path_names[lex_level - 1] != NULL &&
1102 strcmp(fname, _state->path_names[lex_level - 1]) == 0)
1104 if (lex_level < _state->npath)
1106 /* if not at end of path just mark path ok */
1107 _state->pathok[lex_level] = true;
1111 /* end of path, so we want this value */
1118 /* this object overrides any previous matching object */
1119 _state->tresult = NULL;
1120 _state->result_start = NULL;
1122 if (_state->normalize_results &&
1123 _state->lex->token_type == JSON_TOKEN_STRING)
1125 /* for as_text variants, tell get_scalar to set it for us */
1126 _state->next_scalar = true;
1130 /* for non-as_text variants, just note the json starting point */
1131 _state->result_start = _state->lex->token_start;
1137 get_object_field_end(void *state, char *fname, bool isnull)
1139 GetState *_state = (GetState *) state;
1140 bool get_last = false;
1141 int lex_level = _state->lex->lex_level;
1143 /* same tests as in get_object_field_start */
1144 if (lex_level <= _state->npath &&
1145 _state->pathok[lex_level - 1] &&
1146 _state->path_names != NULL &&
1147 _state->path_names[lex_level - 1] != NULL &&
1148 strcmp(fname, _state->path_names[lex_level - 1]) == 0)
1150 if (lex_level < _state->npath)
1152 /* done with this field so reset pathok */
1153 _state->pathok[lex_level] = false;
1157 /* end of path, so we want this value */
1162 /* for as_text scalar case, our work is already done */
1163 if (get_last && _state->result_start != NULL)
1166 * make a text object from the string from the prevously noted json
1167 * start up to the end of the previous token (the lexer is by now
1168 * ahead of us on whatever came after what we're interested in).
1170 if (isnull && _state->normalize_results)
1171 _state->tresult = (text *) NULL;
1174 char *start = _state->result_start;
1175 int len = _state->lex->prev_token_terminator - start;
1177 _state->tresult = cstring_to_text_with_len(start, len);
1180 /* this should be unnecessary but let's do it for cleanliness: */
1181 _state->result_start = NULL;
1186 get_array_start(void *state)
1188 GetState *_state = (GetState *) state;
1189 int lex_level = _state->lex->lex_level;
1191 if (lex_level < _state->npath)
1193 /* Initialize counting of elements in this array */
1194 _state->array_cur_index[lex_level] = -1;
1196 /* INT_MIN value is reserved to represent invalid subscript */
1197 if (_state->path_indexes[lex_level] < 0 &&
1198 _state->path_indexes[lex_level] != INT_MIN)
1200 /* Negative subscript -- convert to positive-wise subscript */
1201 int nelements = json_count_array_elements(_state->lex);
1203 if (-_state->path_indexes[lex_level] <= nelements)
1204 _state->path_indexes[lex_level] += nelements;
1207 else if (lex_level == 0 && _state->npath == 0)
1210 * Special case: we should match the entire array. We only need this
1211 * at the outermost level because at nested levels the match will have
1212 * been started by the outer field or array element callback.
1214 _state->result_start = _state->lex->token_start;
1219 get_array_end(void *state)
1221 GetState *_state = (GetState *) state;
1222 int lex_level = _state->lex->lex_level;
1224 if (lex_level == 0 && _state->npath == 0)
1226 /* Special case: return the entire array */
1227 char *start = _state->result_start;
1228 int len = _state->lex->prev_token_terminator - start;
1230 _state->tresult = cstring_to_text_with_len(start, len);
1235 get_array_element_start(void *state, bool isnull)
1237 GetState *_state = (GetState *) state;
1238 bool get_next = false;
1239 int lex_level = _state->lex->lex_level;
1241 /* Update array element counter */
1242 if (lex_level <= _state->npath)
1243 _state->array_cur_index[lex_level - 1]++;
1245 if (lex_level <= _state->npath &&
1246 _state->pathok[lex_level - 1] &&
1247 _state->path_indexes != NULL &&
1248 _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1])
1250 if (lex_level < _state->npath)
1252 /* if not at end of path just mark path ok */
1253 _state->pathok[lex_level] = true;
1257 /* end of path, so we want this value */
1262 /* same logic as for objects */
1265 _state->tresult = NULL;
1266 _state->result_start = NULL;
1268 if (_state->normalize_results &&
1269 _state->lex->token_type == JSON_TOKEN_STRING)
1271 _state->next_scalar = true;
1275 _state->result_start = _state->lex->token_start;
1281 get_array_element_end(void *state, bool isnull)
1283 GetState *_state = (GetState *) state;
1284 bool get_last = false;
1285 int lex_level = _state->lex->lex_level;
1287 /* same tests as in get_array_element_start */
1288 if (lex_level <= _state->npath &&
1289 _state->pathok[lex_level - 1] &&
1290 _state->path_indexes != NULL &&
1291 _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1])
1293 if (lex_level < _state->npath)
1295 /* done with this element so reset pathok */
1296 _state->pathok[lex_level] = false;
1300 /* end of path, so we want this value */
1305 /* same logic as for objects */
1306 if (get_last && _state->result_start != NULL)
1308 if (isnull && _state->normalize_results)
1309 _state->tresult = (text *) NULL;
1312 char *start = _state->result_start;
1313 int len = _state->lex->prev_token_terminator - start;
1315 _state->tresult = cstring_to_text_with_len(start, len);
1318 _state->result_start = NULL;
1323 get_scalar(void *state, char *token, JsonTokenType tokentype)
1325 GetState *_state = (GetState *) state;
1326 int lex_level = _state->lex->lex_level;
1328 /* Check for whole-object match */
1329 if (lex_level == 0 && _state->npath == 0)
1331 if (_state->normalize_results && tokentype == JSON_TOKEN_STRING)
1333 /* we want the de-escaped string */
1334 _state->next_scalar = true;
1336 else if (_state->normalize_results && tokentype == JSON_TOKEN_NULL)
1338 _state->tresult = (text *) NULL;
1343 * This is a bit hokey: we will suppress whitespace after the
1344 * scalar token, but not whitespace before it. Probably not worth
1345 * doing our own space-skipping to avoid that.
1347 char *start = _state->lex->input;
1348 int len = _state->lex->prev_token_terminator - start;
1350 _state->tresult = cstring_to_text_with_len(start, len);
1354 if (_state->next_scalar)
1356 /* a de-escaped text value is wanted, so supply it */
1357 _state->tresult = cstring_to_text(token);
1358 /* make sure the next call to get_scalar doesn't overwrite it */
1359 _state->next_scalar = false;
1364 jsonb_extract_path(PG_FUNCTION_ARGS)
1366 return get_jsonb_path_all(fcinfo, false);
1370 jsonb_extract_path_text(PG_FUNCTION_ARGS)
1372 return get_jsonb_path_all(fcinfo, true);
1376 get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
1378 Jsonb *jb = PG_GETARG_JSONB(0);
1379 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
1385 bool have_object = false,
1387 JsonbValue *jbvp = NULL;
1389 JsonbContainer *container;
1392 * If the array contains any null elements, return NULL, on the grounds
1393 * that you'd have gotten NULL if any RHS value were NULL in a nested
1394 * series of applications of the -> operator. (Note: because we also
1395 * return NULL for error cases such as no-such-field, this is true
1396 * regardless of the contents of the rest of the array.)
1398 if (array_contains_nulls(path))
1401 deconstruct_array(path, TEXTOID, -1, false, 'i',
1402 &pathtext, &pathnulls, &npath);
1404 /* Identify whether we have object, array, or scalar at top-level */
1405 container = &jb->root;
1407 if (JB_ROOT_IS_OBJECT(jb))
1409 else if (JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb))
1413 Assert(JB_ROOT_IS_ARRAY(jb) && JB_ROOT_IS_SCALAR(jb));
1414 /* Extract the scalar value, if it is what we'll return */
1416 jbvp = getIthJsonbValueFromContainer(container, 0);
1420 * If the array is empty, return the entire LHS object, on the grounds
1421 * that we should do zero field or element extractions. For the
1422 * non-scalar case we can just hand back the object without much work. For
1423 * the scalar case, fall through and deal with the value below the loop.
1424 * (This inconsistency arises because there's no easy way to generate a
1425 * JsonbValue directly for root-level containers.)
1427 if (npath <= 0 && jbvp == NULL)
1431 PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL,
1437 /* not text mode - just hand back the jsonb */
1438 PG_RETURN_JSONB(jb);
1442 for (i = 0; i < npath; i++)
1446 jbvp = findJsonbValueFromContainerLen(container,
1448 VARDATA(pathtext[i]),
1449 VARSIZE(pathtext[i]) - VARHDRSZ);
1451 else if (have_array)
1455 char *indextext = TextDatumGetCString(pathtext[i]);
1459 lindex = strtol(indextext, &endptr, 10);
1460 if (endptr == indextext || *endptr != '\0' || errno != 0 ||
1461 lindex > INT_MAX || lindex < INT_MIN)
1466 index = (uint32) lindex;
1470 /* Handle negative subscript */
1473 /* Container must be array, but make sure */
1474 if (!JsonContainerIsArray(container))
1475 elog(ERROR, "not a jsonb array");
1477 nelements = JsonContainerSize(container);
1479 if (-lindex > nelements)
1482 index = nelements + lindex;
1485 jbvp = getIthJsonbValueFromContainer(container, index);
1489 /* scalar, extraction yields a null */
1495 else if (i == npath - 1)
1498 if (jbvp->type == jbvBinary)
1500 JsonbIterator *it = JsonbIteratorInit((JsonbContainer *) jbvp->val.binary.data);
1501 JsonbIteratorToken r;
1503 r = JsonbIteratorNext(&it, &tv, true);
1504 container = (JsonbContainer *) jbvp->val.binary.data;
1505 have_object = r == WJB_BEGIN_OBJECT;
1506 have_array = r == WJB_BEGIN_ARRAY;
1510 have_object = jbvp->type == jbvObject;
1511 have_array = jbvp->type == jbvArray;
1517 /* special-case outputs for string and null values */
1518 if (jbvp->type == jbvString)
1519 PG_RETURN_TEXT_P(cstring_to_text_with_len(jbvp->val.string.val,
1520 jbvp->val.string.len));
1521 if (jbvp->type == jbvNull)
1525 res = JsonbValueToJsonb(jbvp);
1529 PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL,
1535 /* not text mode - just hand back the jsonb */
1536 PG_RETURN_JSONB(res);
1541 * SQL function json_array_length(json) -> int
1544 json_array_length(PG_FUNCTION_ARGS)
1546 text *json = PG_GETARG_TEXT_PP(0);
1548 JsonLexContext *lex;
1551 lex = makeJsonLexContext(json, false);
1552 state = palloc0(sizeof(AlenState));
1553 sem = palloc0(sizeof(JsonSemAction));
1555 /* palloc0 does this for us */
1561 sem->semstate = (void *) state;
1562 sem->object_start = alen_object_start;
1563 sem->scalar = alen_scalar;
1564 sem->array_element_start = alen_array_element_start;
1566 pg_parse_json(lex, sem);
1568 PG_RETURN_INT32(state->count);
1572 jsonb_array_length(PG_FUNCTION_ARGS)
1574 Jsonb *jb = PG_GETARG_JSONB(0);
1576 if (JB_ROOT_IS_SCALAR(jb))
1578 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1579 errmsg("cannot get array length of a scalar")));
1580 else if (!JB_ROOT_IS_ARRAY(jb))
1582 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1583 errmsg("cannot get array length of a non-array")));
1585 PG_RETURN_INT32(JB_ROOT_COUNT(jb));
1589 * These next two checks ensure that the json is an array (since it can't be
1590 * a scalar or an object).
1594 alen_object_start(void *state)
1596 AlenState *_state = (AlenState *) state;
1598 /* json structure check */
1599 if (_state->lex->lex_level == 0)
1601 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1602 errmsg("cannot get array length of a non-array")));
1606 alen_scalar(void *state, char *token, JsonTokenType tokentype)
1608 AlenState *_state = (AlenState *) state;
1610 /* json structure check */
1611 if (_state->lex->lex_level == 0)
1613 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1614 errmsg("cannot get array length of a scalar")));
1618 alen_array_element_start(void *state, bool isnull)
1620 AlenState *_state = (AlenState *) state;
1622 /* just count up all the level 1 elements */
1623 if (_state->lex->lex_level == 1)
1628 * SQL function json_each and json_each_text
1630 * decompose a json object into key value pairs.
1632 * Unlike json_object_keys() these SRFs operate in materialize mode,
1633 * stashing results into a Tuplestore object as they go.
1634 * The construction of tuples is done using a temporary memory context
1635 * that is cleared out after each tuple is built.
1638 json_each(PG_FUNCTION_ARGS)
1640 return each_worker(fcinfo, false);
1644 jsonb_each(PG_FUNCTION_ARGS)
1646 return each_worker_jsonb(fcinfo, "jsonb_each", false);
1650 json_each_text(PG_FUNCTION_ARGS)
1652 return each_worker(fcinfo, true);
1656 jsonb_each_text(PG_FUNCTION_ARGS)
1658 return each_worker_jsonb(fcinfo, "jsonb_each_text", true);
1662 each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
1664 Jsonb *jb = PG_GETARG_JSONB(0);
1666 Tuplestorestate *tuple_store;
1668 TupleDesc ret_tdesc;
1669 MemoryContext old_cxt,
1671 bool skipNested = false;
1674 JsonbIteratorToken r;
1676 if (!JB_ROOT_IS_OBJECT(jb))
1678 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1679 errmsg("cannot call %s on a non-object",
1682 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1684 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1685 (rsi->allowedModes & SFRM_Materialize) == 0 ||
1686 rsi->expectedDesc == NULL)
1688 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1689 errmsg("set-valued function called in context that "
1690 "cannot accept a set")));
1692 rsi->returnMode = SFRM_Materialize;
1694 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1696 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1697 errmsg("function returning record called in context "
1698 "that cannot accept type record")));
1700 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
1702 ret_tdesc = CreateTupleDescCopy(tupdesc);
1703 BlessTupleDesc(ret_tdesc);
1705 tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
1708 MemoryContextSwitchTo(old_cxt);
1710 tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
1711 "jsonb_each temporary cxt",
1712 ALLOCSET_DEFAULT_SIZES);
1714 it = JsonbIteratorInit(&jb->root);
1716 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
1725 bool nulls[2] = {false, false};
1727 /* Use the tmp context so we can clean up after each tuple is done */
1728 old_cxt = MemoryContextSwitchTo(tmp_cxt);
1730 key = cstring_to_text_with_len(v.val.string.val, v.val.string.len);
1733 * The next thing the iterator fetches should be the value, no
1734 * matter what shape it is.
1736 r = JsonbIteratorNext(&it, &v, skipNested);
1738 values[0] = PointerGetDatum(key);
1742 if (v.type == jbvNull)
1744 /* a json null is an sql null in text mode */
1746 values[1] = (Datum) NULL;
1752 if (v.type == jbvString)
1754 /* In text mode, scalar strings should be dequoted */
1755 sv = cstring_to_text_with_len(v.val.string.val, v.val.string.len);
1759 /* Turn anything else into a json string */
1760 StringInfo jtext = makeStringInfo();
1761 Jsonb *jb = JsonbValueToJsonb(&v);
1763 (void) JsonbToCString(jtext, &jb->root, 0);
1764 sv = cstring_to_text_with_len(jtext->data, jtext->len);
1767 values[1] = PointerGetDatum(sv);
1772 /* Not in text mode, just return the Jsonb */
1773 Jsonb *val = JsonbValueToJsonb(&v);
1775 values[1] = PointerGetDatum(val);
1778 tuple = heap_form_tuple(ret_tdesc, values, nulls);
1780 tuplestore_puttuple(tuple_store, tuple);
1782 /* clean up and switch back */
1783 MemoryContextSwitchTo(old_cxt);
1784 MemoryContextReset(tmp_cxt);
1788 MemoryContextDelete(tmp_cxt);
1790 rsi->setResult = tuple_store;
1791 rsi->setDesc = ret_tdesc;
1798 each_worker(FunctionCallInfo fcinfo, bool as_text)
1800 text *json = PG_GETARG_TEXT_PP(0);
1801 JsonLexContext *lex;
1804 MemoryContext old_cxt;
1808 lex = makeJsonLexContext(json, true);
1809 state = palloc0(sizeof(EachState));
1810 sem = palloc0(sizeof(JsonSemAction));
1812 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1814 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1815 (rsi->allowedModes & SFRM_Materialize) == 0 ||
1816 rsi->expectedDesc == NULL)
1818 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1819 errmsg("set-valued function called in context that "
1820 "cannot accept a set")));
1822 rsi->returnMode = SFRM_Materialize;
1824 (void) get_call_result_type(fcinfo, NULL, &tupdesc);
1826 /* make these in a sufficiently long-lived memory context */
1827 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
1829 state->ret_tdesc = CreateTupleDescCopy(tupdesc);
1830 BlessTupleDesc(state->ret_tdesc);
1831 state->tuple_store =
1832 tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
1835 MemoryContextSwitchTo(old_cxt);
1837 sem->semstate = (void *) state;
1838 sem->array_start = each_array_start;
1839 sem->scalar = each_scalar;
1840 sem->object_field_start = each_object_field_start;
1841 sem->object_field_end = each_object_field_end;
1843 state->normalize_results = as_text;
1844 state->next_scalar = false;
1846 state->tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
1847 "json_each temporary cxt",
1848 ALLOCSET_DEFAULT_SIZES);
1850 pg_parse_json(lex, sem);
1852 MemoryContextDelete(state->tmp_cxt);
1854 rsi->setResult = state->tuple_store;
1855 rsi->setDesc = state->ret_tdesc;
1862 each_object_field_start(void *state, char *fname, bool isnull)
1864 EachState *_state = (EachState *) state;
1866 /* save a pointer to where the value starts */
1867 if (_state->lex->lex_level == 1)
1870 * next_scalar will be reset in the object_field_end handler, and
1871 * since we know the value is a scalar there is no danger of it being
1872 * on while recursing down the tree.
1874 if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING)
1875 _state->next_scalar = true;
1877 _state->result_start = _state->lex->token_start;
1882 each_object_field_end(void *state, char *fname, bool isnull)
1884 EachState *_state = (EachState *) state;
1885 MemoryContext old_cxt;
1890 bool nulls[2] = {false, false};
1892 /* skip over nested objects */
1893 if (_state->lex->lex_level != 1)
1896 /* use the tmp context so we can clean up after each tuple is done */
1897 old_cxt = MemoryContextSwitchTo(_state->tmp_cxt);
1899 values[0] = CStringGetTextDatum(fname);
1901 if (isnull && _state->normalize_results)
1904 values[1] = (Datum) 0;
1906 else if (_state->next_scalar)
1908 values[1] = CStringGetTextDatum(_state->normalized_scalar);
1909 _state->next_scalar = false;
1913 len = _state->lex->prev_token_terminator - _state->result_start;
1914 val = cstring_to_text_with_len(_state->result_start, len);
1915 values[1] = PointerGetDatum(val);
1918 tuple = heap_form_tuple(_state->ret_tdesc, values, nulls);
1920 tuplestore_puttuple(_state->tuple_store, tuple);
1922 /* clean up and switch back */
1923 MemoryContextSwitchTo(old_cxt);
1924 MemoryContextReset(_state->tmp_cxt);
1928 each_array_start(void *state)
1930 EachState *_state = (EachState *) state;
1932 /* json structure check */
1933 if (_state->lex->lex_level == 0)
1935 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1936 errmsg("cannot deconstruct an array as an object")));
1940 each_scalar(void *state, char *token, JsonTokenType tokentype)
1942 EachState *_state = (EachState *) state;
1944 /* json structure check */
1945 if (_state->lex->lex_level == 0)
1947 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1948 errmsg("cannot deconstruct a scalar")));
1950 /* supply de-escaped value if required */
1951 if (_state->next_scalar)
1952 _state->normalized_scalar = token;
1956 * SQL functions json_array_elements and json_array_elements_text
1958 * get the elements from a json array
1960 * a lot of this processing is similar to the json_each* functions
1964 jsonb_array_elements(PG_FUNCTION_ARGS)
1966 return elements_worker_jsonb(fcinfo, "jsonb_array_elements", false);
1970 jsonb_array_elements_text(PG_FUNCTION_ARGS)
1972 return elements_worker_jsonb(fcinfo, "jsonb_array_elements_text", true);
1976 elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
1979 Jsonb *jb = PG_GETARG_JSONB(0);
1981 Tuplestorestate *tuple_store;
1983 TupleDesc ret_tdesc;
1984 MemoryContext old_cxt,
1986 bool skipNested = false;
1989 JsonbIteratorToken r;
1991 if (JB_ROOT_IS_SCALAR(jb))
1993 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1994 errmsg("cannot extract elements from a scalar")));
1995 else if (!JB_ROOT_IS_ARRAY(jb))
1997 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1998 errmsg("cannot extract elements from an object")));
2000 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
2002 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
2003 (rsi->allowedModes & SFRM_Materialize) == 0 ||
2004 rsi->expectedDesc == NULL)
2006 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2007 errmsg("set-valued function called in context that "
2008 "cannot accept a set")));
2010 rsi->returnMode = SFRM_Materialize;
2012 /* it's a simple type, so don't use get_call_result_type() */
2013 tupdesc = rsi->expectedDesc;
2015 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
2017 ret_tdesc = CreateTupleDescCopy(tupdesc);
2018 BlessTupleDesc(ret_tdesc);
2020 tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
2023 MemoryContextSwitchTo(old_cxt);
2025 tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
2026 "jsonb_array_elements temporary cxt",
2027 ALLOCSET_DEFAULT_SIZES);
2029 it = JsonbIteratorInit(&jb->root);
2031 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
2039 bool nulls[1] = {false};
2041 /* use the tmp context so we can clean up after each tuple is done */
2042 old_cxt = MemoryContextSwitchTo(tmp_cxt);
2046 Jsonb *val = JsonbValueToJsonb(&v);
2048 values[0] = PointerGetDatum(val);
2052 if (v.type == jbvNull)
2054 /* a json null is an sql null in text mode */
2056 values[0] = (Datum) NULL;
2062 if (v.type == jbvString)
2064 /* in text mode scalar strings should be dequoted */
2065 sv = cstring_to_text_with_len(v.val.string.val, v.val.string.len);
2069 /* turn anything else into a json string */
2070 StringInfo jtext = makeStringInfo();
2071 Jsonb *jb = JsonbValueToJsonb(&v);
2073 (void) JsonbToCString(jtext, &jb->root, 0);
2074 sv = cstring_to_text_with_len(jtext->data, jtext->len);
2077 values[0] = PointerGetDatum(sv);
2081 tuple = heap_form_tuple(ret_tdesc, values, nulls);
2083 tuplestore_puttuple(tuple_store, tuple);
2085 /* clean up and switch back */
2086 MemoryContextSwitchTo(old_cxt);
2087 MemoryContextReset(tmp_cxt);
2091 MemoryContextDelete(tmp_cxt);
2093 rsi->setResult = tuple_store;
2094 rsi->setDesc = ret_tdesc;
2100 json_array_elements(PG_FUNCTION_ARGS)
2102 return elements_worker(fcinfo, "json_array_elements", false);
2106 json_array_elements_text(PG_FUNCTION_ARGS)
2108 return elements_worker(fcinfo, "json_array_elements_text", true);
2112 elements_worker(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
2114 text *json = PG_GETARG_TEXT_PP(0);
2116 /* elements only needs escaped strings when as_text */
2117 JsonLexContext *lex = makeJsonLexContext(json, as_text);
2120 MemoryContext old_cxt;
2122 ElementsState *state;
2124 state = palloc0(sizeof(ElementsState));
2125 sem = palloc0(sizeof(JsonSemAction));
2127 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
2129 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
2130 (rsi->allowedModes & SFRM_Materialize) == 0 ||
2131 rsi->expectedDesc == NULL)
2133 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2134 errmsg("set-valued function called in context that "
2135 "cannot accept a set")));
2137 rsi->returnMode = SFRM_Materialize;
2139 /* it's a simple type, so don't use get_call_result_type() */
2140 tupdesc = rsi->expectedDesc;
2142 /* make these in a sufficiently long-lived memory context */
2143 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
2145 state->ret_tdesc = CreateTupleDescCopy(tupdesc);
2146 BlessTupleDesc(state->ret_tdesc);
2147 state->tuple_store =
2148 tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
2151 MemoryContextSwitchTo(old_cxt);
2153 sem->semstate = (void *) state;
2154 sem->object_start = elements_object_start;
2155 sem->scalar = elements_scalar;
2156 sem->array_element_start = elements_array_element_start;
2157 sem->array_element_end = elements_array_element_end;
2159 state->function_name = funcname;
2160 state->normalize_results = as_text;
2161 state->next_scalar = false;
2163 state->tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
2164 "json_array_elements temporary cxt",
2165 ALLOCSET_DEFAULT_SIZES);
2167 pg_parse_json(lex, sem);
2169 MemoryContextDelete(state->tmp_cxt);
2171 rsi->setResult = state->tuple_store;
2172 rsi->setDesc = state->ret_tdesc;
2178 elements_array_element_start(void *state, bool isnull)
2180 ElementsState *_state = (ElementsState *) state;
2182 /* save a pointer to where the value starts */
2183 if (_state->lex->lex_level == 1)
2186 * next_scalar will be reset in the array_element_end handler, and
2187 * since we know the value is a scalar there is no danger of it being
2188 * on while recursing down the tree.
2190 if (_state->normalize_results && _state->lex->token_type == JSON_TOKEN_STRING)
2191 _state->next_scalar = true;
2193 _state->result_start = _state->lex->token_start;
2198 elements_array_element_end(void *state, bool isnull)
2200 ElementsState *_state = (ElementsState *) state;
2201 MemoryContext old_cxt;
2206 bool nulls[1] = {false};
2208 /* skip over nested objects */
2209 if (_state->lex->lex_level != 1)
2212 /* use the tmp context so we can clean up after each tuple is done */
2213 old_cxt = MemoryContextSwitchTo(_state->tmp_cxt);
2215 if (isnull && _state->normalize_results)
2218 values[0] = (Datum) NULL;
2220 else if (_state->next_scalar)
2222 values[0] = CStringGetTextDatum(_state->normalized_scalar);
2223 _state->next_scalar = false;
2227 len = _state->lex->prev_token_terminator - _state->result_start;
2228 val = cstring_to_text_with_len(_state->result_start, len);
2229 values[0] = PointerGetDatum(val);
2232 tuple = heap_form_tuple(_state->ret_tdesc, values, nulls);
2234 tuplestore_puttuple(_state->tuple_store, tuple);
2236 /* clean up and switch back */
2237 MemoryContextSwitchTo(old_cxt);
2238 MemoryContextReset(_state->tmp_cxt);
2242 elements_object_start(void *state)
2244 ElementsState *_state = (ElementsState *) state;
2246 /* json structure check */
2247 if (_state->lex->lex_level == 0)
2249 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2250 errmsg("cannot call %s on a non-array",
2251 _state->function_name)));
2255 elements_scalar(void *state, char *token, JsonTokenType tokentype)
2257 ElementsState *_state = (ElementsState *) state;
2259 /* json structure check */
2260 if (_state->lex->lex_level == 0)
2262 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2263 errmsg("cannot call %s on a scalar",
2264 _state->function_name)));
2266 /* supply de-escaped value if required */
2267 if (_state->next_scalar)
2268 _state->normalized_scalar = token;
2272 * SQL function json_populate_record
2274 * set fields in a record from the argument json
2276 * Code adapted shamelessly from hstore's populate_record
2277 * which is in turn partly adapted from record_out.
2279 * The json is decomposed into a hash table, in which each
2280 * field in the record is then looked up by name. For jsonb
2281 * we fetch the values direct from the object.
2284 jsonb_populate_record(PG_FUNCTION_ARGS)
2286 return populate_record_worker(fcinfo, "jsonb_populate_record", true);
2290 jsonb_to_record(PG_FUNCTION_ARGS)
2292 return populate_record_worker(fcinfo, "jsonb_to_record", false);
2296 json_populate_record(PG_FUNCTION_ARGS)
2298 return populate_record_worker(fcinfo, "json_populate_record", true);
2302 json_to_record(PG_FUNCTION_ARGS)
2304 return populate_record_worker(fcinfo, "json_to_record", false);
2307 /* helper function for diagnostics */
2309 populate_array_report_expected_array(PopulateArrayContext *ctx, int ndim)
2315 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2316 errmsg("expected json array"),
2317 errhint("see the value of key \"%s\"", ctx->colname)));
2320 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2321 errmsg("expected json array")));
2325 StringInfoData indices;
2328 initStringInfo(&indices);
2330 Assert(ctx->ndims > 0 && ndim < ctx->ndims);
2332 for (i = 0; i < ndim; i++)
2333 appendStringInfo(&indices, "[%d]", ctx->sizes[i]);
2337 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2338 errmsg("expected json array"),
2339 errhint("see the array element %s of key \"%s\"",
2340 indices.data, ctx->colname)));
2343 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2344 errmsg("expected json array"),
2345 errhint("see the array element %s",
2350 /* set the number of dimensions of the populated array when it becomes known */
2352 populate_array_assign_ndims(PopulateArrayContext *ctx, int ndims)
2356 Assert(ctx->ndims <= 0);
2359 populate_array_report_expected_array(ctx, ndims);
2362 ctx->dims = palloc(sizeof(int) * ndims);
2363 ctx->sizes = palloc0(sizeof(int) * ndims);
2365 for (i = 0; i < ndims; i++)
2366 ctx->dims[i] = -1; /* dimensions are unknown yet */
2369 /* check the populated subarray dimension */
2371 populate_array_check_dimension(PopulateArrayContext *ctx, int ndim)
2373 int dim = ctx->sizes[ndim]; /* current dimension counter */
2375 if (ctx->dims[ndim] == -1)
2376 ctx->dims[ndim] = dim; /* assign dimension if not yet known */
2377 else if (ctx->dims[ndim] != dim)
2379 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2380 errmsg("malformed json array"),
2381 errdetail("Multidimensional arrays must have "
2382 "sub-arrays with matching dimensions.")));
2384 /* reset the current array dimension size counter */
2385 ctx->sizes[ndim] = 0;
2387 /* increment the parent dimension counter if it is a nested sub-array */
2389 ctx->sizes[ndim - 1]++;
2393 populate_array_element(PopulateArrayContext *ctx, int ndim, JsValue *jsv)
2396 bool element_isnull;
2398 /* populate the array element */
2399 element = populate_record_field(ctx->aio->element_info,
2400 ctx->aio->element_type,
2401 ctx->aio->element_typmod,
2402 NULL, ctx->mcxt, PointerGetDatum(NULL),
2403 jsv, &element_isnull);
2405 accumArrayResult(ctx->astate, element, element_isnull,
2406 ctx->aio->element_type, ctx->acxt);
2409 ctx->sizes[ndim - 1]++; /* increment current dimension counter */
2412 /* json object start handler for populate_array_json() */
2414 populate_array_object_start(void *_state)
2416 PopulateArrayState *state = (PopulateArrayState *) _state;
2417 int ndim = state->lex->lex_level;
2419 if (state->ctx->ndims <= 0)
2420 populate_array_assign_ndims(state->ctx, ndim);
2421 else if (ndim < state->ctx->ndims)
2422 populate_array_report_expected_array(state->ctx, ndim);
2425 /* json array end handler for populate_array_json() */
2427 populate_array_array_end(void *_state)
2429 PopulateArrayState *state = (PopulateArrayState *) _state;
2430 PopulateArrayContext *ctx = state->ctx;
2431 int ndim = state->lex->lex_level;
2433 if (ctx->ndims <= 0)
2434 populate_array_assign_ndims(ctx, ndim + 1);
2436 if (ndim < ctx->ndims)
2437 populate_array_check_dimension(ctx, ndim);
2440 /* json array element start handler for populate_array_json() */
2442 populate_array_element_start(void *_state, bool isnull)
2444 PopulateArrayState *state = (PopulateArrayState *) _state;
2445 int ndim = state->lex->lex_level;
2447 if (state->ctx->ndims <= 0 || ndim == state->ctx->ndims)
2449 /* remember current array element start */
2450 state->element_start = state->lex->token_start;
2451 state->element_type = state->lex->token_type;
2452 state->element_scalar = NULL;
2456 /* json array element end handler for populate_array_json() */
2458 populate_array_element_end(void *_state, bool isnull)
2460 PopulateArrayState *state = (PopulateArrayState *) _state;
2461 PopulateArrayContext *ctx = state->ctx;
2462 int ndim = state->lex->lex_level;
2464 Assert(ctx->ndims > 0);
2466 if (ndim == ctx->ndims)
2471 jsv.val.json.type = state->element_type;
2475 Assert(jsv.val.json.type == JSON_TOKEN_NULL);
2476 jsv.val.json.str = NULL;
2477 jsv.val.json.len = 0;
2479 else if (state->element_scalar)
2481 jsv.val.json.str = state->element_scalar;
2482 jsv.val.json.len = -1; /* null-terminated */
2486 jsv.val.json.str = state->element_start;
2487 jsv.val.json.len = (state->lex->prev_token_terminator -
2488 state->element_start) * sizeof(char);
2491 populate_array_element(ctx, ndim, &jsv);
2495 /* json scalar handler for populate_array_json() */
2497 populate_array_scalar(void *_state, char *token, JsonTokenType tokentype)
2499 PopulateArrayState *state = (PopulateArrayState *) _state;
2500 PopulateArrayContext *ctx = state->ctx;
2501 int ndim = state->lex->lex_level;
2503 if (ctx->ndims <= 0)
2504 populate_array_assign_ndims(ctx, ndim);
2505 else if (ndim < ctx->ndims)
2506 populate_array_report_expected_array(ctx, ndim);
2508 if (ndim == ctx->ndims)
2510 /* remember the scalar element token */
2511 state->element_scalar = token;
2512 /* element_type must already be set in populate_array_element_start() */
2513 Assert(state->element_type == tokentype);
2517 /* parse a json array and populate array */
2519 populate_array_json(PopulateArrayContext *ctx, char *json, int len)
2521 PopulateArrayState state;
2524 state.lex = makeJsonLexContextCstringLen(json, len, true);
2527 memset(&sem, 0, sizeof(sem));
2528 sem.semstate = (void *) &state;
2529 sem.object_start = populate_array_object_start;
2530 sem.array_end = populate_array_array_end;
2531 sem.array_element_start = populate_array_element_start;
2532 sem.array_element_end = populate_array_element_end;
2533 sem.scalar = populate_array_scalar;
2535 pg_parse_json(state.lex, &sem);
2537 /* number of dimensions should be already known */
2538 Assert(ctx->ndims > 0 && ctx->dims);
2544 * populate_array_dim_jsonb() -- Iterate recursively through jsonb sub-array
2545 * elements and accumulate result using given ArrayBuildState.
2548 populate_array_dim_jsonb(PopulateArrayContext *ctx, /* context */
2549 JsonbValue *jbv, /* jsonb sub-array */
2550 int ndim) /* current dimension */
2552 JsonbContainer *jbc = jbv->val.binary.data;
2554 JsonbIteratorToken tok;
2558 check_stack_depth();
2560 if (jbv->type != jbvBinary || !JsonContainerIsArray(jbc))
2561 populate_array_report_expected_array(ctx, ndim - 1);
2563 Assert(!JsonContainerIsScalar(jbc));
2565 it = JsonbIteratorInit(jbc);
2567 tok = JsonbIteratorNext(&it, &val, true);
2568 Assert(tok == WJB_BEGIN_ARRAY);
2570 tok = JsonbIteratorNext(&it, &val, true);
2573 * If the number of dimensions is not yet known and we have found end of
2574 * the array, or the first child element is not an array, then assign the
2575 * number of dimensions now.
2577 if (ctx->ndims <= 0 &&
2578 (tok == WJB_END_ARRAY ||
2580 (val.type != jbvBinary ||
2581 !JsonContainerIsArray(val.val.binary.data)))))
2582 populate_array_assign_ndims(ctx, ndim);
2584 jsv.is_json = false;
2585 jsv.val.jsonb = &val;
2587 /* process all the array elements */
2588 while (tok == WJB_ELEM)
2591 * Recurse only if the dimensions of dimensions is still unknown or if
2592 * it is not the innermost dimension.
2594 if (ctx->ndims > 0 && ndim >= ctx->ndims)
2595 populate_array_element(ctx, ndim, &jsv);
2598 /* populate child sub-array */
2599 populate_array_dim_jsonb(ctx, &val, ndim + 1);
2601 /* number of dimensions should be already known */
2602 Assert(ctx->ndims > 0 && ctx->dims);
2604 populate_array_check_dimension(ctx, ndim);
2607 tok = JsonbIteratorNext(&it, &val, true);
2610 Assert(tok == WJB_END_ARRAY);
2612 /* free iterator, iterating until WJB_DONE */
2613 tok = JsonbIteratorNext(&it, &val, true);
2614 Assert(tok == WJB_DONE && !it);
2617 /* recursively populate an array from json/jsonb */
2619 populate_array(ArrayIOData *aio,
2620 const char *colname,
2624 PopulateArrayContext ctx;
2631 ctx.acxt = CurrentMemoryContext;
2632 ctx.astate = initArrayResult(aio->element_type, ctx.acxt, true);
2633 ctx.colname = colname;
2634 ctx.ndims = 0; /* unknown yet */
2639 populate_array_json(&ctx, jsv->val.json.str,
2640 jsv->val.json.len >= 0 ? jsv->val.json.len
2641 : strlen(jsv->val.json.str));
2644 populate_array_dim_jsonb(&ctx, jsv->val.jsonb, 1);
2645 ctx.dims[0] = ctx.sizes[0];
2648 Assert(ctx.ndims > 0);
2650 lbs = palloc(sizeof(int) * ctx.ndims);
2652 for (i = 0; i < ctx.ndims; i++)
2655 result = makeMdArrayResult(ctx.astate, ctx.ndims, ctx.dims, lbs,
2666 JsValueToJsObject(JsValue *jsv, JsObject *jso)
2668 jso->is_json = jsv->is_json;
2672 /* convert plain-text json into a hash table */
2673 jso->val.json_hash =
2674 get_json_object_as_hash(jsv->val.json.str,
2675 jsv->val.json.len >= 0
2677 : strlen(jsv->val.json.str),
2678 "populate_composite");
2682 JsonbValue *jbv = jsv->val.jsonb;
2684 if (jbv->type == jbvBinary &&
2685 JsonContainerIsObject(jbv->val.binary.data))
2687 jso->val.jsonb_cont = jbv->val.binary.data;
2693 is_scalar = IsAJsonbScalar(jbv) ||
2694 (jbv->type == jbvBinary &&
2695 JsonContainerIsScalar(jbv->val.binary.data));
2697 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2699 ? errmsg("cannot call %s on a scalar",
2700 "populate_composite")
2701 : errmsg("cannot call %s on an array",
2702 "populate_composite")));
2707 /* recursively populate a composite (row type) value from json/jsonb */
2709 populate_composite(CompositeIOData *io,
2712 const char *colname,
2714 HeapTupleHeader defaultval,
2717 HeapTupleHeader tuple;
2720 /* acquire cached tuple descriptor */
2722 io->tupdesc->tdtypeid != typid ||
2723 io->tupdesc->tdtypmod != typmod)
2725 TupleDesc tupdesc = lookup_rowtype_tupdesc(typid, typmod);
2726 MemoryContext oldcxt;
2729 FreeTupleDesc(io->tupdesc);
2731 /* copy tuple desc without constraints into cache memory context */
2732 oldcxt = MemoryContextSwitchTo(mcxt);
2733 io->tupdesc = CreateTupleDescCopy(tupdesc);
2734 MemoryContextSwitchTo(oldcxt);
2736 ReleaseTupleDesc(tupdesc);
2739 /* prepare input value */
2740 JsValueToJsObject(jsv, &jso);
2742 /* populate resulting record tuple */
2743 tuple = populate_record(io->tupdesc, &io->record_io,
2744 defaultval, mcxt, &jso);
2748 return HeapTupleHeaderGetDatum(tuple);
2751 /* populate non-null scalar value from json/jsonb value */
2753 populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv)
2761 int len = jsv->val.json.len;
2763 json = jsv->val.json.str;
2766 /* already done the hard work in the json case */
2767 if ((typid == JSONOID || typid == JSONBOID) &&
2768 jsv->val.json.type == JSON_TOKEN_STRING)
2771 * Add quotes around string value (should be already escaped) if
2772 * converting to json/jsonb.
2778 str = palloc(len + sizeof(char) * 3);
2780 memcpy(&str[1], json, len);
2782 str[len + 2] = '\0';
2786 /* Need to copy non-null-terminated string */
2787 str = palloc(len + 1 * sizeof(char));
2788 memcpy(str, json, len);
2792 str = json; /* null-terminated string */
2796 JsonbValue *jbv = jsv->val.jsonb;
2798 if (typid == JSONBOID)
2800 Jsonb *jsonb = JsonbValueToJsonb(jbv); /* directly use jsonb */
2802 return JsonbGetDatum(jsonb);
2804 /* convert jsonb to string for typio call */
2805 else if (typid == JSONOID && jbv->type != jbvBinary)
2808 * Convert scalar jsonb (non-scalars are passed here as jbvBinary)
2809 * to json string, preserving quotes around top-level strings.
2811 Jsonb *jsonb = JsonbValueToJsonb(jbv);
2813 str = JsonbToCString(NULL, &jsonb->root, VARSIZE(jsonb));
2815 else if (jbv->type == jbvString) /* quotes are stripped */
2816 str = pnstrdup(jbv->val.string.val, jbv->val.string.len);
2817 else if (jbv->type == jbvBool)
2818 str = pstrdup(jbv->val.boolean ? "true" : "false");
2819 else if (jbv->type == jbvNumeric)
2820 str = DatumGetCString(DirectFunctionCall1(numeric_out,
2821 PointerGetDatum(jbv->val.numeric)));
2822 else if (jbv->type == jbvBinary)
2823 str = JsonbToCString(NULL, jbv->val.binary.data,
2824 jbv->val.binary.len);
2826 elog(ERROR, "unrecognized jsonb type: %d", (int) jbv->type);
2829 res = InputFunctionCall(&io->typiofunc, str, io->typioparam, typmod);
2831 /* free temporary buffer */
2839 populate_domain(DomainIOData *io,
2841 const char *colname,
2852 res = populate_record_field(io->base_io,
2853 io->base_typid, io->base_typmod,
2854 colname, mcxt, PointerGetDatum(NULL),
2859 domain_check(res, isnull, typid, &io->domain_info, mcxt);
2864 /* prepare column metadata cache for the given type */
2866 prepare_column_cache(ColumnIOData *column,
2875 column->typid = typid;
2876 column->typmod = typmod;
2878 tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
2879 if (!HeapTupleIsValid(tup))
2880 elog(ERROR, "cache lookup failed for type %u", typid);
2882 type = (Form_pg_type) GETSTRUCT(tup);
2884 if (type->typtype == TYPTYPE_DOMAIN)
2886 column->typcat = TYPECAT_DOMAIN;
2887 column->io.domain.base_typid = type->typbasetype;
2888 column->io.domain.base_typmod = type->typtypmod;
2889 column->io.domain.base_io = MemoryContextAllocZero(mcxt,
2890 sizeof(ColumnIOData));
2891 column->io.domain.domain_info = NULL;
2893 else if (type->typtype == TYPTYPE_COMPOSITE || typid == RECORDOID)
2895 column->typcat = TYPECAT_COMPOSITE;
2896 column->io.composite.record_io = NULL;
2897 column->io.composite.tupdesc = NULL;
2899 else if (type->typlen == -1 && OidIsValid(type->typelem))
2901 column->typcat = TYPECAT_ARRAY;
2902 column->io.array.element_info = MemoryContextAllocZero(mcxt,
2903 sizeof(ColumnIOData));
2904 column->io.array.element_type = type->typelem;
2905 /* array element typemod stored in attribute's typmod */
2906 column->io.array.element_typmod = typmod;
2909 column->typcat = TYPECAT_SCALAR;
2911 /* don't need input function when converting from jsonb to jsonb */
2912 if (json || typid != JSONBOID)
2916 getTypeInputInfo(typid, &typioproc, &column->scalar_io.typioparam);
2917 fmgr_info_cxt(typioproc, &column->scalar_io.typiofunc, mcxt);
2920 ReleaseSysCache(tup);
2923 /* recursively populate a record field or an array element from a json/jsonb value */
2925 populate_record_field(ColumnIOData *col,
2928 const char *colname,
2936 check_stack_depth();
2938 /* prepare column metadata cache for the given type */
2939 if (col->typid != typid || col->typmod != typmod)
2940 prepare_column_cache(col, typid, typmod, mcxt, jsv->is_json);
2942 *isnull = JsValueIsNull(jsv);
2944 typcat = col->typcat;
2946 /* try to convert json string to a non-scalar type through input function */
2947 if (JsValueIsString(jsv) &&
2948 (typcat == TYPECAT_ARRAY || typcat == TYPECAT_COMPOSITE))
2949 typcat = TYPECAT_SCALAR;
2951 /* we must perform domain checks for NULLs */
2952 if (*isnull && typcat != TYPECAT_DOMAIN)
2957 case TYPECAT_SCALAR:
2958 return populate_scalar(&col->scalar_io, typid, typmod, jsv);
2961 return populate_array(&col->io.array, colname, mcxt, jsv);
2963 case TYPECAT_COMPOSITE:
2964 return populate_composite(&col->io.composite, typid, typmod,
2966 DatumGetPointer(defaultval)
2967 ? DatumGetHeapTupleHeader(defaultval)
2971 case TYPECAT_DOMAIN:
2972 return populate_domain(&col->io.domain, typid, colname, mcxt,
2976 elog(ERROR, "unrecognized type category '%c'", typcat);
2981 static RecordIOData *
2982 allocate_record_info(MemoryContext mcxt, int ncolumns)
2984 RecordIOData *data = (RecordIOData *)
2985 MemoryContextAlloc(mcxt,
2986 offsetof(RecordIOData, columns) +
2987 ncolumns * sizeof(ColumnIOData));
2989 data->record_type = InvalidOid;
2990 data->record_typmod = 0;
2991 data->ncolumns = ncolumns;
2992 MemSet(data->columns, 0, sizeof(ColumnIOData) * ncolumns);
2998 JsObjectGetField(JsObject *obj, char *field, JsValue *jsv)
3000 jsv->is_json = obj->is_json;
3004 JsonHashEntry *hashentry = hash_search(obj->val.json_hash, field,
3007 jsv->val.json.type = hashentry ? hashentry->type : JSON_TOKEN_NULL;
3008 jsv->val.json.str = jsv->val.json.type == JSON_TOKEN_NULL ? NULL :
3010 jsv->val.json.len = jsv->val.json.str ? -1 : 0; /* null-terminated */
3012 return hashentry != NULL;
3016 jsv->val.jsonb = !obj->val.jsonb_cont ? NULL :
3017 findJsonbValueFromContainerLen(obj->val.jsonb_cont, JB_FOBJECT,
3018 field, strlen(field));
3020 return jsv->val.jsonb != NULL;
3024 /* populate a record tuple from json/jsonb value */
3025 static HeapTupleHeader
3026 populate_record(TupleDesc tupdesc,
3027 RecordIOData **record_p,
3028 HeapTupleHeader defaultval,
3032 RecordIOData *record = *record_p;
3036 int ncolumns = tupdesc->natts;
3040 * if the input json is empty, we can only skip the rest if we were passed
3041 * in a non-null record, since otherwise there may be issues with domain
3044 if (defaultval && JsObjectIsEmpty(obj))
3047 /* (re)allocate metadata cache */
3048 if (record == NULL ||
3049 record->ncolumns != ncolumns)
3050 *record_p = record = allocate_record_info(mcxt, ncolumns);
3052 /* invalidate metadata cache if the record type has changed */
3053 if (record->record_type != tupdesc->tdtypeid ||
3054 record->record_typmod != tupdesc->tdtypmod)
3056 MemSet(record, 0, offsetof(RecordIOData, columns) +
3057 ncolumns * sizeof(ColumnIOData));
3058 record->record_type = tupdesc->tdtypeid;
3059 record->record_typmod = tupdesc->tdtypmod;
3060 record->ncolumns = ncolumns;
3063 values = (Datum *) palloc(ncolumns * sizeof(Datum));
3064 nulls = (bool *) palloc(ncolumns * sizeof(bool));
3068 HeapTupleData tuple;
3070 /* Build a temporary HeapTuple control structure */
3071 tuple.t_len = HeapTupleHeaderGetDatumLength(defaultval);
3072 ItemPointerSetInvalid(&(tuple.t_self));
3073 tuple.t_tableOid = InvalidOid;
3074 tuple.t_data = defaultval;
3076 /* Break down the tuple into fields */
3077 heap_deform_tuple(&tuple, tupdesc, values, nulls);
3081 for (i = 0; i < ncolumns; ++i)
3083 values[i] = (Datum) 0;
3088 for (i = 0; i < ncolumns; ++i)
3090 Form_pg_attribute att = tupdesc->attrs[i];
3091 char *colname = NameStr(att->attname);
3092 JsValue field = {0};
3095 /* Ignore dropped columns in datatype */
3096 if (att->attisdropped)
3102 found = JsObjectGetField(obj, colname, &field);
3105 * we can't just skip here if the key wasn't found since we might have
3106 * a domain to deal with. If we were passed in a non-null record
3107 * datum, we assume that the existing values are valid (if they're
3108 * not, then it's not our fault), but if we were passed in a null,
3109 * then every field which we don't populate needs to be run through
3110 * the input function just in case it's a domain type.
3112 if (defaultval && !found)
3115 values[i] = populate_record_field(&record->columns[i],
3120 nulls[i] ? (Datum) 0 : values[i],
3125 res = heap_form_tuple(tupdesc, values, nulls);
3134 populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
3135 bool have_record_arg)
3137 int json_arg_num = have_record_arg ? 1 : 0;
3138 Oid jtype = get_fn_expr_argtype(fcinfo->flinfo, json_arg_num);
3140 HeapTupleHeader rec = NULL;
3143 TupleDesc tupdesc = NULL;
3146 MemoryContext fnmcxt = fcinfo->flinfo->fn_mcxt;
3147 PopulateRecordCache *cache = fcinfo->flinfo->fn_extra;
3149 Assert(jtype == JSONOID || jtype == JSONBOID);
3152 * We arrange to look up the needed I/O info just once per series of
3153 * calls, assuming the record type doesn't change underneath us.
3156 fcinfo->flinfo->fn_extra = cache =
3157 MemoryContextAllocZero(fnmcxt, sizeof(*cache));
3159 if (have_record_arg)
3161 Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
3163 if (cache->argtype != argtype)
3165 if (!type_is_rowtype(argtype))
3167 (errcode(ERRCODE_DATATYPE_MISMATCH),
3168 errmsg("first argument of %s must be a row type",
3171 cache->argtype = argtype;
3174 if (PG_ARGISNULL(0))
3176 if (PG_ARGISNULL(1))
3180 * We have no tuple to look at, so the only source of type info is
3181 * the argtype. The lookup_rowtype_tupdesc call below will error
3182 * out if we don't have a known composite type oid here.
3189 rec = PG_GETARG_HEAPTUPLEHEADER(0);
3191 if (PG_ARGISNULL(1))
3192 PG_RETURN_POINTER(rec);
3194 /* Extract type info from the tuple itself */
3195 tupType = HeapTupleHeaderGetTypeId(rec);
3196 tupTypmod = HeapTupleHeaderGetTypMod(rec);
3201 /* json{b}_to_record case */
3202 if (PG_ARGISNULL(0))
3205 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
3207 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3208 errmsg("function returning record called in context "
3209 "that cannot accept type record"),
3210 errhint("Try calling the function in the FROM clause "
3211 "using a column definition list.")));
3216 * Add tupdesc to the cache and set the appropriate values of
3217 * tupType/tupTypmod for proper cache usage in populate_composite().
3219 cache->io.tupdesc = tupdesc;
3221 tupType = tupdesc->tdtypeid;
3222 tupTypmod = tupdesc->tdtypmod;
3225 jsv.is_json = jtype == JSONOID;
3229 text *json = PG_GETARG_TEXT_PP(json_arg_num);
3231 jsv.val.json.str = VARDATA_ANY(json);
3232 jsv.val.json.len = VARSIZE_ANY_EXHDR(json);
3233 jsv.val.json.type = JSON_TOKEN_INVALID; /* not used in
3234 * populate_composite() */
3238 Jsonb *jb = PG_GETARG_JSONB(json_arg_num);
3240 jsv.val.jsonb = &jbv;
3242 /* fill binary jsonb value pointing to jb */
3243 jbv.type = jbvBinary;
3244 jbv.val.binary.data = &jb->root;
3245 jbv.val.binary.len = VARSIZE(jb) - VARHDRSZ;
3248 rettuple = populate_composite(&cache->io, tupType, tupTypmod,
3249 NULL, fnmcxt, rec, &jsv);
3253 cache->io.tupdesc = NULL;
3254 ReleaseTupleDesc(tupdesc);
3257 PG_RETURN_DATUM(rettuple);
3261 * get_json_object_as_hash
3263 * decompose a json object into a hash table.
3266 get_json_object_as_hash(char *json, int len, const char *funcname)
3271 JsonLexContext *lex = makeJsonLexContextCstringLen(json, len, true);
3274 memset(&ctl, 0, sizeof(ctl));
3275 ctl.keysize = NAMEDATALEN;
3276 ctl.entrysize = sizeof(JsonHashEntry);
3277 ctl.hcxt = CurrentMemoryContext;
3278 tab = hash_create("json object hashtable",
3281 HASH_ELEM | HASH_CONTEXT);
3283 state = palloc0(sizeof(JHashState));
3284 sem = palloc0(sizeof(JsonSemAction));
3286 state->function_name = funcname;
3290 sem->semstate = (void *) state;
3291 sem->array_start = hash_array_start;
3292 sem->scalar = hash_scalar;
3293 sem->object_field_start = hash_object_field_start;
3294 sem->object_field_end = hash_object_field_end;
3296 pg_parse_json(lex, sem);
3302 hash_object_field_start(void *state, char *fname, bool isnull)
3304 JHashState *_state = (JHashState *) state;
3306 if (_state->lex->lex_level > 1)
3309 /* remember token type */
3310 _state->saved_token_type = _state->lex->token_type;
3312 if (_state->lex->token_type == JSON_TOKEN_ARRAY_START ||
3313 _state->lex->token_type == JSON_TOKEN_OBJECT_START)
3315 /* remember start position of the whole text of the subobject */
3316 _state->save_json_start = _state->lex->token_start;
3320 /* must be a scalar */
3321 _state->save_json_start = NULL;
3326 hash_object_field_end(void *state, char *fname, bool isnull)
3328 JHashState *_state = (JHashState *) state;
3329 JsonHashEntry *hashentry;
3333 * Ignore nested fields.
3335 if (_state->lex->lex_level > 1)
3339 * Ignore field names >= NAMEDATALEN - they can't match a record field.
3340 * (Note: without this test, the hash code would truncate the string at
3341 * NAMEDATALEN-1, and could then match against a similarly-truncated
3342 * record field name. That would be a reasonable behavior, but this code
3343 * has previously insisted on exact equality, so we keep this behavior.)
3345 if (strlen(fname) >= NAMEDATALEN)
3348 hashentry = hash_search(_state->hash, fname, HASH_ENTER, &found);
3351 * found being true indicates a duplicate. We don't do anything about
3352 * that, a later field with the same name overrides the earlier field.
3355 hashentry->type = _state->saved_token_type;
3356 Assert(isnull == (hashentry->type == JSON_TOKEN_NULL));
3358 if (_state->save_json_start != NULL)
3360 int len = _state->lex->prev_token_terminator - _state->save_json_start;
3361 char *val = palloc((len + 1) * sizeof(char));
3363 memcpy(val, _state->save_json_start, len);
3365 hashentry->val = val;
3369 /* must have had a scalar instead */
3370 hashentry->val = _state->saved_scalar;
3375 hash_array_start(void *state)
3377 JHashState *_state = (JHashState *) state;
3379 if (_state->lex->lex_level == 0)
3381 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3382 errmsg("cannot call %s on an array", _state->function_name)));
3386 hash_scalar(void *state, char *token, JsonTokenType tokentype)
3388 JHashState *_state = (JHashState *) state;
3390 if (_state->lex->lex_level == 0)
3392 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3393 errmsg("cannot call %s on a scalar", _state->function_name)));
3395 if (_state->lex->lex_level == 1)
3397 _state->saved_scalar = token;
3398 /* saved_token_type must already be set in hash_object_field_start() */
3399 Assert(_state->saved_token_type == tokentype);
3405 * SQL function json_populate_recordset
3407 * set fields in a set of records from the argument json,
3408 * which must be an array of objects.
3410 * similar to json_populate_record, but the tuple-building code
3411 * is pushed down into the semantic action handlers so it's done
3412 * per object in the array.
3415 jsonb_populate_recordset(PG_FUNCTION_ARGS)
3417 return populate_recordset_worker(fcinfo, "jsonb_populate_recordset", true);
3421 jsonb_to_recordset(PG_FUNCTION_ARGS)
3423 return populate_recordset_worker(fcinfo, "jsonb_to_recordset", false);
3427 json_populate_recordset(PG_FUNCTION_ARGS)
3429 return populate_recordset_worker(fcinfo, "json_populate_recordset", true);
3433 json_to_recordset(PG_FUNCTION_ARGS)
3435 return populate_recordset_worker(fcinfo, "json_to_recordset", false);
3439 populate_recordset_record(PopulateRecordsetState *state, JsObject *obj)
3441 HeapTupleData tuple;
3442 HeapTupleHeader tuphead = populate_record(state->ret_tdesc,
3448 tuple.t_len = HeapTupleHeaderGetDatumLength(tuphead);
3449 ItemPointerSetInvalid(&(tuple.t_self));
3450 tuple.t_tableOid = InvalidOid;
3451 tuple.t_data = tuphead;
3453 tuplestore_puttuple(state->tuple_store, &tuple);
3457 * common worker for json_populate_recordset() and json_to_recordset()
3460 populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
3461 bool have_record_arg)
3463 int json_arg_num = have_record_arg ? 1 : 0;
3464 Oid jtype = get_fn_expr_argtype(fcinfo->flinfo, json_arg_num);
3466 MemoryContext old_cxt;
3467 HeapTupleHeader rec;
3469 PopulateRecordsetState *state;
3471 if (have_record_arg)
3473 Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
3475 if (!type_is_rowtype(argtype))
3477 (errcode(ERRCODE_DATATYPE_MISMATCH),
3478 errmsg("first argument of %s must be a row type",
3482 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
3484 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
3485 (rsi->allowedModes & SFRM_Materialize) == 0 ||
3486 rsi->expectedDesc == NULL)
3488 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3489 errmsg("set-valued function called in context that "
3490 "cannot accept a set")));
3492 rsi->returnMode = SFRM_Materialize;
3495 * get the tupdesc from the result set info - it must be a record type
3496 * because we already checked that arg1 is a record type, or we're in a
3497 * to_record function which returns a setof record.
3499 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
3501 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3502 errmsg("function returning record called in context "
3503 "that cannot accept type record")));
3505 /* if the json is null send back an empty set */
3506 if (PG_ARGISNULL(json_arg_num))
3509 if (!have_record_arg || PG_ARGISNULL(0))
3512 rec = PG_GETARG_HEAPTUPLEHEADER(0);
3514 state = palloc0(sizeof(PopulateRecordsetState));
3516 /* make these in a sufficiently long-lived memory context */
3517 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
3518 state->ret_tdesc = CreateTupleDescCopy(tupdesc);
3519 BlessTupleDesc(state->ret_tdesc);
3520 state->tuple_store = tuplestore_begin_heap(rsi->allowedModes &
3521 SFRM_Materialize_Random,
3523 MemoryContextSwitchTo(old_cxt);
3525 state->function_name = funcname;
3526 state->my_extra = (RecordIOData **) &fcinfo->flinfo->fn_extra;
3528 state->fn_mcxt = fcinfo->flinfo->fn_mcxt;
3530 if (jtype == JSONOID)
3532 text *json = PG_GETARG_TEXT_PP(json_arg_num);
3533 JsonLexContext *lex;
3536 sem = palloc0(sizeof(JsonSemAction));
3538 lex = makeJsonLexContext(json, true);
3540 sem->semstate = (void *) state;
3541 sem->array_start = populate_recordset_array_start;
3542 sem->array_element_start = populate_recordset_array_element_start;
3543 sem->scalar = populate_recordset_scalar;
3544 sem->object_field_start = populate_recordset_object_field_start;
3545 sem->object_field_end = populate_recordset_object_field_end;
3546 sem->object_start = populate_recordset_object_start;
3547 sem->object_end = populate_recordset_object_end;
3551 pg_parse_json(lex, sem);
3555 Jsonb *jb = PG_GETARG_JSONB(json_arg_num);
3558 bool skipNested = false;
3559 JsonbIteratorToken r;
3561 Assert(jtype == JSONBOID);
3563 if (JB_ROOT_IS_SCALAR(jb) || !JB_ROOT_IS_ARRAY(jb))
3565 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3566 errmsg("cannot call %s on a non-array",
3569 it = JsonbIteratorInit(&jb->root);
3571 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
3579 if (v.type != jbvBinary ||
3580 !JsonContainerIsObject(v.val.binary.data))
3582 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3583 errmsg("argument of %s must be an array of objects",
3586 obj.is_json = false;
3587 obj.val.jsonb_cont = v.val.binary.data;
3589 populate_recordset_record(state, &obj);
3594 rsi->setResult = state->tuple_store;
3595 rsi->setDesc = state->ret_tdesc;
3601 populate_recordset_object_start(void *state)
3603 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3604 int lex_level = _state->lex->lex_level;
3607 /* Reject object at top level: we must have an array at level 0 */
3610 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3611 errmsg("cannot call %s on an object",
3612 _state->function_name)));
3614 /* Nested objects require no special processing */
3618 /* Object at level 1: set up a new hash table for this object */
3619 memset(&ctl, 0, sizeof(ctl));
3620 ctl.keysize = NAMEDATALEN;
3621 ctl.entrysize = sizeof(JsonHashEntry);
3622 ctl.hcxt = CurrentMemoryContext;
3623 _state->json_hash = hash_create("json object hashtable",
3626 HASH_ELEM | HASH_CONTEXT);
3630 populate_recordset_object_end(void *state)
3632 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3635 /* Nested objects require no special processing */
3636 if (_state->lex->lex_level > 1)
3640 obj.val.json_hash = _state->json_hash;
3642 /* Otherwise, construct and return a tuple based on this level-1 object */
3643 populate_recordset_record(_state, &obj);
3645 /* Done with hash for this object */
3646 hash_destroy(_state->json_hash);
3647 _state->json_hash = NULL;
3651 populate_recordset_array_element_start(void *state, bool isnull)
3653 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3655 if (_state->lex->lex_level == 1 &&
3656 _state->lex->token_type != JSON_TOKEN_OBJECT_START)
3658 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3659 errmsg("argument of %s must be an array of objects",
3660 _state->function_name)));
3664 populate_recordset_array_start(void *state)
3670 populate_recordset_scalar(void *state, char *token, JsonTokenType tokentype)
3672 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3674 if (_state->lex->lex_level == 0)
3676 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3677 errmsg("cannot call %s on a scalar",
3678 _state->function_name)));
3680 if (_state->lex->lex_level == 2)
3681 _state->saved_scalar = token;
3685 populate_recordset_object_field_start(void *state, char *fname, bool isnull)
3687 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3689 if (_state->lex->lex_level > 2)
3692 _state->saved_token_type = _state->lex->token_type;
3694 if (_state->lex->token_type == JSON_TOKEN_ARRAY_START ||
3695 _state->lex->token_type == JSON_TOKEN_OBJECT_START)
3697 _state->save_json_start = _state->lex->token_start;
3701 _state->save_json_start = NULL;
3706 populate_recordset_object_field_end(void *state, char *fname, bool isnull)
3708 PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
3709 JsonHashEntry *hashentry;
3713 * Ignore nested fields.
3715 if (_state->lex->lex_level > 2)
3719 * Ignore field names >= NAMEDATALEN - they can't match a record field.
3720 * (Note: without this test, the hash code would truncate the string at
3721 * NAMEDATALEN-1, and could then match against a similarly-truncated
3722 * record field name. That would be a reasonable behavior, but this code
3723 * has previously insisted on exact equality, so we keep this behavior.)
3725 if (strlen(fname) >= NAMEDATALEN)
3728 hashentry = hash_search(_state->json_hash, fname, HASH_ENTER, &found);
3731 * found being true indicates a duplicate. We don't do anything about
3732 * that, a later field with the same name overrides the earlier field.
3735 hashentry->type = _state->saved_token_type;
3736 Assert(isnull == (hashentry->type == JSON_TOKEN_NULL));
3738 if (_state->save_json_start != NULL)
3740 int len = _state->lex->prev_token_terminator - _state->save_json_start;
3741 char *val = palloc((len + 1) * sizeof(char));
3743 memcpy(val, _state->save_json_start, len);
3745 hashentry->val = val;
3749 /* must have had a scalar instead */
3750 hashentry->val = _state->saved_scalar;
3755 * findJsonbValueFromContainer() wrapper that sets up JsonbValue key string.
3758 findJsonbValueFromContainerLen(JsonbContainer *container, uint32 flags,
3759 char *key, uint32 keylen)
3764 k.val.string.val = key;
3765 k.val.string.len = keylen;
3767 return findJsonbValueFromContainer(container, flags, &k);
3771 * Semantic actions for json_strip_nulls.
3773 * Simply repeat the input on the output unless we encounter
3774 * a null object field. State for this is set when the field
3775 * is started and reset when the scalar action (which must be next)
3780 sn_object_start(void *state)
3782 StripnullState *_state = (StripnullState *) state;
3784 appendStringInfoCharMacro(_state->strval, '{');
3788 sn_object_end(void *state)
3790 StripnullState *_state = (StripnullState *) state;
3792 appendStringInfoCharMacro(_state->strval, '}');
3796 sn_array_start(void *state)
3798 StripnullState *_state = (StripnullState *) state;
3800 appendStringInfoCharMacro(_state->strval, '[');
3804 sn_array_end(void *state)
3806 StripnullState *_state = (StripnullState *) state;
3808 appendStringInfoCharMacro(_state->strval, ']');
3812 sn_object_field_start(void *state, char *fname, bool isnull)
3814 StripnullState *_state = (StripnullState *) state;
3819 * The next thing must be a scalar or isnull couldn't be true, so
3820 * there is no danger of this state being carried down into a nested
3821 * object or array. The flag will be reset in the scalar action.
3823 _state->skip_next_null = true;
3827 if (_state->strval->data[_state->strval->len - 1] != '{')
3828 appendStringInfoCharMacro(_state->strval, ',');
3831 * Unfortunately we don't have the quoted and escaped string any more, so
3832 * we have to re-escape it.
3834 escape_json(_state->strval, fname);
3836 appendStringInfoCharMacro(_state->strval, ':');
3840 sn_array_element_start(void *state, bool isnull)
3842 StripnullState *_state = (StripnullState *) state;
3844 if (_state->strval->data[_state->strval->len - 1] != '[')
3845 appendStringInfoCharMacro(_state->strval, ',');
3849 sn_scalar(void *state, char *token, JsonTokenType tokentype)
3851 StripnullState *_state = (StripnullState *) state;
3853 if (_state->skip_next_null)
3855 Assert(tokentype == JSON_TOKEN_NULL);
3856 _state->skip_next_null = false;
3860 if (tokentype == JSON_TOKEN_STRING)
3861 escape_json(_state->strval, token);
3863 appendStringInfoString(_state->strval, token);
3867 * SQL function json_strip_nulls(json) -> json
3870 json_strip_nulls(PG_FUNCTION_ARGS)
3872 text *json = PG_GETARG_TEXT_PP(0);
3873 StripnullState *state;
3874 JsonLexContext *lex;
3877 lex = makeJsonLexContext(json, true);
3878 state = palloc0(sizeof(StripnullState));
3879 sem = palloc0(sizeof(JsonSemAction));
3881 state->strval = makeStringInfo();
3882 state->skip_next_null = false;
3885 sem->semstate = (void *) state;
3886 sem->object_start = sn_object_start;
3887 sem->object_end = sn_object_end;
3888 sem->array_start = sn_array_start;
3889 sem->array_end = sn_array_end;
3890 sem->scalar = sn_scalar;
3891 sem->array_element_start = sn_array_element_start;
3892 sem->object_field_start = sn_object_field_start;
3894 pg_parse_json(lex, sem);
3896 PG_RETURN_TEXT_P(cstring_to_text_with_len(state->strval->data,
3897 state->strval->len));
3902 * SQL function jsonb_strip_nulls(jsonb) -> jsonb
3905 jsonb_strip_nulls(PG_FUNCTION_ARGS)
3907 Jsonb *jb = PG_GETARG_JSONB(0);
3909 JsonbParseState *parseState = NULL;
3910 JsonbValue *res = NULL;
3913 JsonbIteratorToken type;
3914 bool last_was_key = false;
3916 if (JB_ROOT_IS_SCALAR(jb))
3917 PG_RETURN_POINTER(jb);
3919 it = JsonbIteratorInit(&jb->root);
3921 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
3923 Assert(!(type == WJB_KEY && last_was_key));
3925 if (type == WJB_KEY)
3927 /* stash the key until we know if it has a null value */
3929 last_was_key = true;
3935 /* if the last element was a key this one can't be */
3936 last_was_key = false;
3938 /* skip this field if value is null */
3939 if (type == WJB_VALUE && v.type == jbvNull)
3942 /* otherwise, do a delayed push of the key */
3943 (void) pushJsonbValue(&parseState, WJB_KEY, &k);
3946 if (type == WJB_VALUE || type == WJB_ELEM)
3947 res = pushJsonbValue(&parseState, type, &v);
3949 res = pushJsonbValue(&parseState, type, NULL);
3952 Assert(res != NULL);
3954 PG_RETURN_POINTER(JsonbValueToJsonb(res));
3958 * Add values from the jsonb to the parse state.
3960 * If the parse state container is an object, the jsonb is pushed as
3961 * a value, not a key.
3963 * This needs to be done using an iterator because pushJsonbValue doesn't
3964 * like getting jbvBinary values, so we can't just push jb as a whole.
3967 addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb)
3970 JsonbValue *o = &(*jbps)->contVal;
3972 JsonbIteratorToken type;
3974 it = JsonbIteratorInit(&jb->root);
3976 Assert(o->type == jbvArray || o->type == jbvObject);
3978 if (JB_ROOT_IS_SCALAR(jb))
3980 (void) JsonbIteratorNext(&it, &v, false); /* skip array header */
3981 (void) JsonbIteratorNext(&it, &v, false); /* fetch scalar value */
3986 (void) pushJsonbValue(jbps, WJB_ELEM, &v);
3989 (void) pushJsonbValue(jbps, WJB_VALUE, &v);
3992 elog(ERROR, "unexpected parent of nested structure");
3997 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
3999 if (type == WJB_KEY || type == WJB_VALUE || type == WJB_ELEM)
4000 (void) pushJsonbValue(jbps, type, &v);
4002 (void) pushJsonbValue(jbps, type, NULL);
4009 * SQL function jsonb_pretty (jsonb)
4011 * Pretty-printed text for the jsonb
4014 jsonb_pretty(PG_FUNCTION_ARGS)
4016 Jsonb *jb = PG_GETARG_JSONB(0);
4017 StringInfo str = makeStringInfo();
4019 JsonbToCStringIndent(str, &jb->root, VARSIZE(jb));
4021 PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len));
4025 * SQL function jsonb_concat (jsonb, jsonb)
4027 * function for || operator
4030 jsonb_concat(PG_FUNCTION_ARGS)
4032 Jsonb *jb1 = PG_GETARG_JSONB(0);
4033 Jsonb *jb2 = PG_GETARG_JSONB(1);
4034 JsonbParseState *state = NULL;
4040 * If one of the jsonb is empty, just return the other if it's not scalar
4041 * and both are of the same kind. If it's a scalar or they are of
4042 * different kinds we need to perform the concatenation even if one is
4045 if (JB_ROOT_IS_OBJECT(jb1) == JB_ROOT_IS_OBJECT(jb2))
4047 if (JB_ROOT_COUNT(jb1) == 0 && !JB_ROOT_IS_SCALAR(jb2))
4048 PG_RETURN_JSONB(jb2);
4049 else if (JB_ROOT_COUNT(jb2) == 0 && !JB_ROOT_IS_SCALAR(jb1))
4050 PG_RETURN_JSONB(jb1);
4053 it1 = JsonbIteratorInit(&jb1->root);
4054 it2 = JsonbIteratorInit(&jb2->root);
4056 res = IteratorConcat(&it1, &it2, &state);
4058 Assert(res != NULL);
4060 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4065 * SQL function jsonb_delete (jsonb, text)
4067 * return a copy of the jsonb with the indicated item
4071 jsonb_delete(PG_FUNCTION_ARGS)
4073 Jsonb *in = PG_GETARG_JSONB(0);
4074 text *key = PG_GETARG_TEXT_PP(1);
4075 char *keyptr = VARDATA_ANY(key);
4076 int keylen = VARSIZE_ANY_EXHDR(key);
4077 JsonbParseState *state = NULL;
4081 bool skipNested = false;
4082 JsonbIteratorToken r;
4084 if (JB_ROOT_IS_SCALAR(in))
4086 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4087 errmsg("cannot delete from scalar")));
4089 if (JB_ROOT_COUNT(in) == 0)
4090 PG_RETURN_JSONB(in);
4092 it = JsonbIteratorInit(&in->root);
4094 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
4098 if ((r == WJB_ELEM || r == WJB_KEY) &&
4099 (v.type == jbvString && keylen == v.val.string.len &&
4100 memcmp(keyptr, v.val.string.val, keylen) == 0))
4102 /* skip corresponding value as well */
4104 JsonbIteratorNext(&it, &v, true);
4109 res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4112 Assert(res != NULL);
4114 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4118 * SQL function jsonb_delete (jsonb, variadic text[])
4120 * return a copy of the jsonb with the indicated items
4124 jsonb_delete_array(PG_FUNCTION_ARGS)
4126 Jsonb *in = PG_GETARG_JSONB(0);
4127 ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
4131 JsonbParseState *state = NULL;
4135 bool skipNested = false;
4136 JsonbIteratorToken r;
4138 if (ARR_NDIM(keys) > 1)
4140 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4141 errmsg("wrong number of array subscripts")));
4143 if (JB_ROOT_IS_SCALAR(in))
4145 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4146 errmsg("cannot delete from scalar")));
4148 if (JB_ROOT_COUNT(in) == 0)
4149 PG_RETURN_JSONB(in);
4151 deconstruct_array(keys, TEXTOID, -1, false, 'i',
4152 &keys_elems, &keys_nulls, &keys_len);
4155 PG_RETURN_JSONB(in);
4157 it = JsonbIteratorInit(&in->root);
4159 while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
4163 if ((r == WJB_ELEM || r == WJB_KEY) && v.type == jbvString)
4168 for (i = 0; i < keys_len; i++)
4176 keyptr = VARDATA_ANY(keys_elems[i]);
4177 keylen = VARSIZE_ANY_EXHDR(keys_elems[i]);
4178 if (keylen == v.val.string.len &&
4179 memcmp(keyptr, v.val.string.val, keylen) == 0)
4187 /* skip corresponding value as well */
4189 JsonbIteratorNext(&it, &v, true);
4195 res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4198 Assert(res != NULL);
4200 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4204 * SQL function jsonb_delete (jsonb, int)
4206 * return a copy of the jsonb with the indicated item
4207 * removed. Negative int means count back from the
4211 jsonb_delete_idx(PG_FUNCTION_ARGS)
4213 Jsonb *in = PG_GETARG_JSONB(0);
4214 int idx = PG_GETARG_INT32(1);
4215 JsonbParseState *state = NULL;
4221 JsonbIteratorToken r;
4223 if (JB_ROOT_IS_SCALAR(in))
4225 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4226 errmsg("cannot delete from scalar")));
4228 if (JB_ROOT_IS_OBJECT(in))
4230 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4231 errmsg("cannot delete from object using integer index")));
4233 if (JB_ROOT_COUNT(in) == 0)
4234 PG_RETURN_JSONB(in);
4236 it = JsonbIteratorInit(&in->root);
4238 r = JsonbIteratorNext(&it, &v, false);
4239 Assert(r == WJB_BEGIN_ARRAY);
4240 n = v.val.array.nElems;
4251 PG_RETURN_JSONB(in);
4253 pushJsonbValue(&state, r, NULL);
4255 while ((r = JsonbIteratorNext(&it, &v, true)) != 0)
4263 res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4266 Assert(res != NULL);
4268 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4272 * SQL function jsonb_set(jsonb, text[], jsonb, boolean)
4276 jsonb_set(PG_FUNCTION_ARGS)
4278 Jsonb *in = PG_GETARG_JSONB(0);
4279 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
4280 Jsonb *newval = PG_GETARG_JSONB(2);
4281 bool create = PG_GETARG_BOOL(3);
4282 JsonbValue *res = NULL;
4287 JsonbParseState *st = NULL;
4289 if (ARR_NDIM(path) > 1)
4291 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4292 errmsg("wrong number of array subscripts")));
4294 if (JB_ROOT_IS_SCALAR(in))
4296 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4297 errmsg("cannot set path in scalar")));
4299 if (JB_ROOT_COUNT(in) == 0 && !create)
4300 PG_RETURN_JSONB(in);
4302 deconstruct_array(path, TEXTOID, -1, false, 'i',
4303 &path_elems, &path_nulls, &path_len);
4306 PG_RETURN_JSONB(in);
4308 it = JsonbIteratorInit(&in->root);
4310 res = setPath(&it, path_elems, path_nulls, path_len, &st,
4311 0, newval, create ? JB_PATH_CREATE : JB_PATH_REPLACE);
4313 Assert(res != NULL);
4315 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4320 * SQL function jsonb_delete_path(jsonb, text[])
4323 jsonb_delete_path(PG_FUNCTION_ARGS)
4325 Jsonb *in = PG_GETARG_JSONB(0);
4326 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
4327 JsonbValue *res = NULL;
4332 JsonbParseState *st = NULL;
4334 if (ARR_NDIM(path) > 1)
4336 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4337 errmsg("wrong number of array subscripts")));
4339 if (JB_ROOT_IS_SCALAR(in))
4341 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4342 errmsg("cannot delete path in scalar")));
4344 if (JB_ROOT_COUNT(in) == 0)
4345 PG_RETURN_JSONB(in);
4347 deconstruct_array(path, TEXTOID, -1, false, 'i',
4348 &path_elems, &path_nulls, &path_len);
4351 PG_RETURN_JSONB(in);
4353 it = JsonbIteratorInit(&in->root);
4355 res = setPath(&it, path_elems, path_nulls, path_len, &st,
4356 0, NULL, JB_PATH_DELETE);
4358 Assert(res != NULL);
4360 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4364 * SQL function jsonb_insert(jsonb, text[], jsonb, boolean)
4368 jsonb_insert(PG_FUNCTION_ARGS)
4370 Jsonb *in = PG_GETARG_JSONB(0);
4371 ArrayType *path = PG_GETARG_ARRAYTYPE_P(1);
4372 Jsonb *newval = PG_GETARG_JSONB(2);
4373 bool after = PG_GETARG_BOOL(3);
4374 JsonbValue *res = NULL;
4379 JsonbParseState *st = NULL;
4381 if (ARR_NDIM(path) > 1)
4383 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4384 errmsg("wrong number of array subscripts")));
4386 if (JB_ROOT_IS_SCALAR(in))
4388 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4389 errmsg("cannot set path in scalar")));
4391 deconstruct_array(path, TEXTOID, -1, false, 'i',
4392 &path_elems, &path_nulls, &path_len);
4395 PG_RETURN_JSONB(in);
4397 it = JsonbIteratorInit(&in->root);
4399 res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, newval,
4400 after ? JB_PATH_INSERT_AFTER : JB_PATH_INSERT_BEFORE);
4402 Assert(res != NULL);
4404 PG_RETURN_JSONB(JsonbValueToJsonb(res));
4408 * Iterate over all jsonb objects and merge them into one.
4409 * The logic of this function copied from the same hstore function,
4410 * except the case, when it1 & it2 represents jbvObject.
4411 * In that case we just append the content of it2 to it1 without any
4415 IteratorConcat(JsonbIterator **it1, JsonbIterator **it2,
4416 JsonbParseState **state)
4421 JsonbIteratorToken r1,
4426 r1 = rk1 = JsonbIteratorNext(it1, &v1, false);
4427 r2 = rk2 = JsonbIteratorNext(it2, &v2, false);
4430 * Both elements are objects.
4432 if (rk1 == WJB_BEGIN_OBJECT && rk2 == WJB_BEGIN_OBJECT)
4435 * Append the all tokens from v1 to res, except last WJB_END_OBJECT
4436 * (because res will not be finished yet).
4438 pushJsonbValue(state, r1, NULL);
4439 while ((r1 = JsonbIteratorNext(it1, &v1, true)) != WJB_END_OBJECT)
4440 pushJsonbValue(state, r1, &v1);
4443 * Append the all tokens from v2 to res, include last WJB_END_OBJECT
4444 * (the concatenation will be completed).
4446 while ((r2 = JsonbIteratorNext(it2, &v2, true)) != 0)
4447 res = pushJsonbValue(state, r2, r2 != WJB_END_OBJECT ? &v2 : NULL);
4451 * Both elements are arrays (either can be scalar).
4453 else if (rk1 == WJB_BEGIN_ARRAY && rk2 == WJB_BEGIN_ARRAY)
4455 pushJsonbValue(state, r1, NULL);
4457 while ((r1 = JsonbIteratorNext(it1, &v1, true)) != WJB_END_ARRAY)
4459 Assert(r1 == WJB_ELEM);
4460 pushJsonbValue(state, r1, &v1);
4463 while ((r2 = JsonbIteratorNext(it2, &v2, true)) != WJB_END_ARRAY)
4465 Assert(r2 == WJB_ELEM);
4466 pushJsonbValue(state, WJB_ELEM, &v2);
4469 res = pushJsonbValue(state, WJB_END_ARRAY, NULL /* signal to sort */ );
4471 /* have we got array || object or object || array? */
4472 else if (((rk1 == WJB_BEGIN_ARRAY && !(*it1)->isScalar) && rk2 == WJB_BEGIN_OBJECT) ||
4473 (rk1 == WJB_BEGIN_OBJECT && (rk2 == WJB_BEGIN_ARRAY && !(*it2)->isScalar)))
4476 JsonbIterator **it_array = rk1 == WJB_BEGIN_ARRAY ? it1 : it2;
4477 JsonbIterator **it_object = rk1 == WJB_BEGIN_OBJECT ? it1 : it2;
4479 bool prepend = (rk1 == WJB_BEGIN_OBJECT);
4481 pushJsonbValue(state, WJB_BEGIN_ARRAY, NULL);
4485 pushJsonbValue(state, WJB_BEGIN_OBJECT, NULL);
4486 while ((r1 = JsonbIteratorNext(it_object, &v1, true)) != 0)
4487 pushJsonbValue(state, r1, r1 != WJB_END_OBJECT ? &v1 : NULL);
4489 while ((r2 = JsonbIteratorNext(it_array, &v2, true)) != 0)
4490 res = pushJsonbValue(state, r2, r2 != WJB_END_ARRAY ? &v2 : NULL);
4494 while ((r1 = JsonbIteratorNext(it_array, &v1, true)) != WJB_END_ARRAY)
4495 pushJsonbValue(state, r1, &v1);
4497 pushJsonbValue(state, WJB_BEGIN_OBJECT, NULL);
4498 while ((r2 = JsonbIteratorNext(it_object, &v2, true)) != 0)
4499 pushJsonbValue(state, r2, r2 != WJB_END_OBJECT ? &v2 : NULL);
4501 res = pushJsonbValue(state, WJB_END_ARRAY, NULL);
4507 * This must be scalar || object or object || scalar, as that's all
4508 * that's left. Both of these make no sense, so error out.
4511 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4512 errmsg("invalid concatenation of jsonb objects")));
4519 * Do most of the heavy work for jsonb_set/jsonb_insert
4521 * If JB_PATH_DELETE bit is set in op_type, the element is to be removed.
4523 * If any bit mentioned in JB_PATH_CREATE_OR_INSERT is set in op_type,
4524 * we create the new value if the key or array index does not exist.
4526 * Bits JB_PATH_INSERT_BEFORE and JB_PATH_INSERT_AFTER in op_type
4527 * behave as JB_PATH_CREATE if new value is inserted in JsonbObject.
4529 * All path elements before the last must already exist
4530 * whatever bits in op_type are set, or nothing is done.
4533 setPath(JsonbIterator **it, Datum *path_elems,
4534 bool *path_nulls, int path_len,
4535 JsonbParseState **st, int level, Jsonb *newval, int op_type)
4538 JsonbIteratorToken r;
4541 check_stack_depth();
4543 if (path_nulls[level])
4545 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4546 errmsg("path element at position %d is null",
4549 r = JsonbIteratorNext(it, &v, false);
4553 case WJB_BEGIN_ARRAY:
4554 (void) pushJsonbValue(st, r, NULL);
4555 setPathArray(it, path_elems, path_nulls, path_len, st, level,
4556 newval, v.val.array.nElems, op_type);
4557 r = JsonbIteratorNext(it, &v, false);
4558 Assert(r == WJB_END_ARRAY);
4559 res = pushJsonbValue(st, r, NULL);
4561 case WJB_BEGIN_OBJECT:
4562 (void) pushJsonbValue(st, r, NULL);
4563 setPathObject(it, path_elems, path_nulls, path_len, st, level,
4564 newval, v.val.object.nPairs, op_type);
4565 r = JsonbIteratorNext(it, &v, true);
4566 Assert(r == WJB_END_OBJECT);
4567 res = pushJsonbValue(st, r, NULL);
4571 res = pushJsonbValue(st, r, &v);
4574 elog(ERROR, "unrecognized iterator result: %d", (int) r);
4575 res = NULL; /* keep compiler quiet */
4583 * Object walker for setPath
4586 setPathObject(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
4587 int path_len, JsonbParseState **st, int level,
4588 Jsonb *newval, uint32 npairs, int op_type)
4595 if (level >= path_len || path_nulls[level])
4598 /* empty object is a special case for create */
4599 if ((npairs == 0) && (op_type & JB_PATH_CREATE_OR_INSERT) &&
4600 (level == path_len - 1))
4604 newkey.type = jbvString;
4605 newkey.val.string.len = VARSIZE_ANY_EXHDR(path_elems[level]);
4606 newkey.val.string.val = VARDATA_ANY(path_elems[level]);
4608 (void) pushJsonbValue(st, WJB_KEY, &newkey);
4609 addJsonbToParseState(st, newval);
4612 for (i = 0; i < npairs; i++)
4614 JsonbIteratorToken r = JsonbIteratorNext(it, &k, true);
4616 Assert(r == WJB_KEY);
4619 k.val.string.len == VARSIZE_ANY_EXHDR(path_elems[level]) &&
4620 memcmp(k.val.string.val, VARDATA_ANY(path_elems[level]),
4621 k.val.string.len) == 0)
4623 if (level == path_len - 1)
4626 * called from jsonb_insert(), it forbids redefining an
4629 if (op_type & (JB_PATH_INSERT_BEFORE | JB_PATH_INSERT_AFTER))
4631 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4632 errmsg("cannot replace existing key"),
4633 errhint("Try using the function jsonb_set "
4634 "to replace key value.")));
4636 r = JsonbIteratorNext(it, &v, true); /* skip value */
4637 if (!(op_type & JB_PATH_DELETE))
4639 (void) pushJsonbValue(st, WJB_KEY, &k);
4640 addJsonbToParseState(st, newval);
4646 (void) pushJsonbValue(st, r, &k);
4647 setPath(it, path_elems, path_nulls, path_len,
4648 st, level + 1, newval, op_type);
4653 if ((op_type & JB_PATH_CREATE_OR_INSERT) && !done &&
4654 level == path_len - 1 && i == npairs - 1)
4658 newkey.type = jbvString;
4659 newkey.val.string.len = VARSIZE_ANY_EXHDR(path_elems[level]);
4660 newkey.val.string.val = VARDATA_ANY(path_elems[level]);
4662 (void) pushJsonbValue(st, WJB_KEY, &newkey);
4663 addJsonbToParseState(st, newval);
4666 (void) pushJsonbValue(st, r, &k);
4667 r = JsonbIteratorNext(it, &v, false);
4668 (void) pushJsonbValue(st, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4669 if (r == WJB_BEGIN_ARRAY || r == WJB_BEGIN_OBJECT)
4671 int walking_level = 1;
4673 while (walking_level != 0)
4675 r = JsonbIteratorNext(it, &v, false);
4677 if (r == WJB_BEGIN_ARRAY || r == WJB_BEGIN_OBJECT)
4679 if (r == WJB_END_ARRAY || r == WJB_END_OBJECT)
4682 (void) pushJsonbValue(st, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4690 * Array walker for setPath
4693 setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
4694 int path_len, JsonbParseState **st, int level,
4695 Jsonb *newval, uint32 nelems, int op_type)
4702 /* pick correct index */
4703 if (level < path_len && !path_nulls[level])
4705 char *c = TextDatumGetCString(path_elems[level]);
4710 lindex = strtol(c, &badp, 10);
4711 if (errno != 0 || badp == c || *badp != '\0' || lindex > INT_MAX ||
4714 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
4715 errmsg("path element at position %d is not an integer: \"%s\"",
4730 if (idx > 0 && idx > nelems)
4734 * if we're creating, and idx == INT_MIN, we prepend the new value to the
4735 * array also if the array is empty - in which case we don't really care
4736 * what the idx value is
4739 if ((idx == INT_MIN || nelems == 0) && (level == path_len - 1) &&
4740 (op_type & JB_PATH_CREATE_OR_INSERT))
4742 Assert(newval != NULL);
4743 addJsonbToParseState(st, newval);
4747 /* iterate over the array elements */
4748 for (i = 0; i < nelems; i++)
4750 JsonbIteratorToken r;
4752 if (i == idx && level < path_len)
4754 if (level == path_len - 1)
4756 r = JsonbIteratorNext(it, &v, true); /* skip */
4758 if (op_type & (JB_PATH_INSERT_BEFORE | JB_PATH_CREATE))
4759 addJsonbToParseState(st, newval);
4762 * We should keep current value only in case of
4763 * JB_PATH_INSERT_BEFORE or JB_PATH_INSERT_AFTER because
4764 * otherwise it should be deleted or replaced
4766 if (op_type & (JB_PATH_INSERT_AFTER | JB_PATH_INSERT_BEFORE))
4767 (void) pushJsonbValue(st, r, &v);
4769 if (op_type & (JB_PATH_INSERT_AFTER | JB_PATH_REPLACE))
4770 addJsonbToParseState(st, newval);
4775 (void) setPath(it, path_elems, path_nulls, path_len,
4776 st, level + 1, newval, op_type);
4780 r = JsonbIteratorNext(it, &v, false);
4782 (void) pushJsonbValue(st, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4784 if (r == WJB_BEGIN_ARRAY || r == WJB_BEGIN_OBJECT)
4786 int walking_level = 1;
4788 while (walking_level != 0)
4790 r = JsonbIteratorNext(it, &v, false);
4792 if (r == WJB_BEGIN_ARRAY || r == WJB_BEGIN_OBJECT)
4794 if (r == WJB_END_ARRAY || r == WJB_END_OBJECT)
4797 (void) pushJsonbValue(st, r, r < WJB_BEGIN_ARRAY ? &v : NULL);
4801 if ((op_type & JB_PATH_CREATE_OR_INSERT) && !done &&
4802 level == path_len - 1 && i == nelems - 1)
4804 addJsonbToParseState(st, newval);
4811 * Iterate over jsonb string values or elements, and pass them together with an
4812 * iteration state to a specified JsonIterateStringValuesAction.
4815 iterate_jsonb_string_values(Jsonb *jb, void *state, JsonIterateStringValuesAction action)
4819 JsonbIteratorToken type;
4821 it = JsonbIteratorInit(&jb->root);
4823 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
4825 if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
4827 action(state, v.val.string.val, v.val.string.len);
4833 * Iterate over json string values or elements, and pass them together with an
4834 * iteration state to a specified JsonIterateStringValuesAction.
4837 iterate_json_string_values(text *json, void *action_state, JsonIterateStringValuesAction action)
4839 JsonLexContext *lex = makeJsonLexContext(json, true);
4840 JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
4841 IterateJsonStringValuesState *state = palloc0(sizeof(IterateJsonStringValuesState));
4844 state->action = action;
4845 state->action_state = action_state;
4847 sem->semstate = (void *) state;
4848 sem->scalar = iterate_string_values_scalar;
4850 pg_parse_json(lex, sem);
4854 * An auxiliary function for iterate_json_string_values to invoke a specified
4855 * JsonIterateStringValuesAction.
4858 iterate_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
4860 IterateJsonStringValuesState *_state = (IterateJsonStringValuesState *) state;
4862 if (tokentype == JSON_TOKEN_STRING)
4863 (*_state->action) (_state->action_state, token, strlen(token));
4867 * Iterate over a jsonb, and apply a specified JsonTransformStringValuesAction
4868 * to every string value or element. Any necessary context for a
4869 * JsonTransformStringValuesAction can be passed in the action_state variable.
4870 * Function returns a copy of an original jsonb object with transformed values.
4873 transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
4874 JsonTransformStringValuesAction transform_action)
4879 JsonbIteratorToken type;
4880 JsonbParseState *st = NULL;
4882 bool is_scalar = false;
4884 it = JsonbIteratorInit(&jsonb->root);
4885 is_scalar = it->isScalar;
4887 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
4889 if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
4891 out = transform_action(action_state, v.val.string.val, v.val.string.len);
4892 v.val.string.val = VARDATA_ANY(out);
4893 v.val.string.len = VARSIZE_ANY_EXHDR(out);
4894 res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL);
4898 res = pushJsonbValue(&st, type, (type == WJB_KEY ||
4899 type == WJB_VALUE ||
4900 type == WJB_ELEM) ? &v : NULL);
4904 if (res->type == jbvArray)
4905 res->val.array.rawScalar = is_scalar;
4907 return JsonbValueToJsonb(res);
4911 * Iterate over a json, and apply a specified JsonTransformStringValuesAction
4912 * to every string value or element. Any necessary context for a
4913 * JsonTransformStringValuesAction can be passed in the action_state variable.
4914 * Function returns a StringInfo, which is a copy of an original json with
4915 * transformed values.
4918 transform_json_string_values(text *json, void *action_state,
4919 JsonTransformStringValuesAction transform_action)
4921 JsonLexContext *lex = makeJsonLexContext(json, true);
4922 JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
4923 TransformJsonStringValuesState *state = palloc0(sizeof(TransformJsonStringValuesState));
4926 state->strval = makeStringInfo();
4927 state->action = transform_action;
4928 state->action_state = action_state;
4930 sem->semstate = (void *) state;
4931 sem->scalar = transform_string_values_scalar;
4932 sem->object_start = transform_string_values_object_start;
4933 sem->object_end = transform_string_values_object_end;
4934 sem->array_start = transform_string_values_array_start;
4935 sem->array_end = transform_string_values_array_end;
4936 sem->scalar = transform_string_values_scalar;
4937 sem->array_element_start = transform_string_values_array_element_start;
4938 sem->object_field_start = transform_string_values_object_field_start;
4940 pg_parse_json(lex, sem);
4942 return cstring_to_text_with_len(state->strval->data, state->strval->len);
4946 * Set of auxiliary functions for transform_json_string_values to invoke a
4947 * specified JsonTransformStringValuesAction for all values and left everything
4951 transform_string_values_object_start(void *state)
4953 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4955 appendStringInfoCharMacro(_state->strval, '{');
4959 transform_string_values_object_end(void *state)
4961 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4963 appendStringInfoCharMacro(_state->strval, '}');
4967 transform_string_values_array_start(void *state)
4969 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4971 appendStringInfoCharMacro(_state->strval, '[');
4975 transform_string_values_array_end(void *state)
4977 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4979 appendStringInfoCharMacro(_state->strval, ']');
4983 transform_string_values_object_field_start(void *state, char *fname, bool isnull)
4985 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
4987 if (_state->strval->data[_state->strval->len - 1] != '{')
4988 appendStringInfoCharMacro(_state->strval, ',');
4991 * Unfortunately we don't have the quoted and escaped string any more, so
4992 * we have to re-escape it.
4994 escape_json(_state->strval, fname);
4995 appendStringInfoCharMacro(_state->strval, ':');
4999 transform_string_values_array_element_start(void *state, bool isnull)
5001 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
5003 if (_state->strval->data[_state->strval->len - 1] != '[')
5004 appendStringInfoCharMacro(_state->strval, ',');
5008 transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
5010 TransformJsonStringValuesState *_state = (TransformJsonStringValuesState *) state;
5012 if (tokentype == JSON_TOKEN_STRING)
5014 text *out = (*_state->action) (_state->action_state, token, strlen(token));
5016 escape_json(_state->strval, text_to_cstring(out));
5019 appendStringInfoString(_state->strval, token);