Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * json.c
4 : : * JSON data type support.
5 : : *
6 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/utils/adt/json.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : : #include "postgres.h"
15 : :
16 : : #include "catalog/pg_proc.h"
17 : : #include "catalog/pg_type.h"
18 : : #include "common/hashfn.h"
19 : : #include "funcapi.h"
20 : : #include "libpq/pqformat.h"
21 : : #include "miscadmin.h"
22 : : #include "port/simd.h"
23 : : #include "utils/array.h"
24 : : #include "utils/builtins.h"
25 : : #include "utils/date.h"
26 : : #include "utils/datetime.h"
27 : : #include "utils/fmgroids.h"
28 : : #include "utils/json.h"
29 : : #include "utils/jsonfuncs.h"
30 : : #include "utils/lsyscache.h"
31 : : #include "utils/typcache.h"
32 : :
33 : :
34 : : /*
35 : : * Support for fast key uniqueness checking.
36 : : *
37 : : * We maintain a hash table of used keys in JSON objects for fast detection
38 : : * of duplicates.
39 : : */
40 : : /* Common context for key uniqueness check */
41 : : typedef struct HTAB *JsonUniqueCheckState; /* hash table for key names */
42 : :
43 : : /* Hash entry for JsonUniqueCheckState */
44 : : typedef struct JsonUniqueHashEntry
45 : : {
46 : : const char *key;
47 : : int key_len;
48 : : int object_id;
49 : : } JsonUniqueHashEntry;
50 : :
51 : : /* Stack element for key uniqueness check during JSON parsing */
52 : : typedef struct JsonUniqueStackEntry
53 : : {
54 : : struct JsonUniqueStackEntry *parent;
55 : : int object_id;
56 : : } JsonUniqueStackEntry;
57 : :
58 : : /* Context struct for key uniqueness check during JSON parsing */
59 : : typedef struct JsonUniqueParsingState
60 : : {
61 : : JsonLexContext *lex;
62 : : JsonUniqueCheckState check;
63 : : JsonUniqueStackEntry *stack;
64 : : int id_counter;
65 : : bool unique;
66 : : } JsonUniqueParsingState;
67 : :
68 : : /* Context struct for key uniqueness check during JSON building */
69 : : typedef struct JsonUniqueBuilderState
70 : : {
71 : : JsonUniqueCheckState check; /* unique check */
72 : : StringInfoData skipped_keys; /* skipped keys with NULL values */
73 : : MemoryContext mcxt; /* context for saving skipped keys */
74 : : } JsonUniqueBuilderState;
75 : :
76 : :
77 : : /* State struct for JSON aggregation */
78 : : typedef struct JsonAggState
79 : : {
80 : : StringInfo str;
81 : : JsonTypeCategory key_category;
82 : : Oid key_output_func;
83 : : JsonTypeCategory val_category;
84 : : Oid val_output_func;
85 : : JsonUniqueBuilderState unique_check;
86 : : } JsonAggState;
87 : :
88 : : static void composite_to_json(Datum composite, StringInfo result,
89 : : bool use_line_feeds);
90 : : static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
91 : : Datum *vals, bool *nulls, int *valcount,
92 : : JsonTypeCategory tcategory, Oid outfuncoid,
93 : : bool use_line_feeds);
94 : : static void array_to_json_internal(Datum array, StringInfo result,
95 : : bool use_line_feeds);
96 : : static void datum_to_json_internal(Datum val, bool is_null, StringInfo result,
97 : : JsonTypeCategory tcategory, Oid outfuncoid,
98 : : bool key_scalar);
99 : : static void add_json(Datum val, bool is_null, StringInfo result,
100 : : Oid val_type, bool key_scalar);
101 : : static text *catenate_stringinfo_string(StringInfo buffer, const char *addon);
102 : :
103 : : /*
104 : : * Input.
105 : : */
106 : : Datum
4967 rhaas@postgresql.org 107 :CBC 3158 : json_in(PG_FUNCTION_ARGS)
108 : : {
4544 andrew@dunslane.net 109 : 3158 : char *json = PG_GETARG_CSTRING(0);
110 : 3158 : text *result = cstring_to_text(json);
111 : : JsonLexContext lex;
112 : :
113 : : /* validate it */
702 alvherre@alvh.no-ip. 114 : 3158 : makeJsonLexContext(&lex, result, false);
115 [ + + ]: 3158 : if (!pg_parse_json_or_errsave(&lex, &nullSemAction, fcinfo->context))
1000 tgl@sss.pgh.pa.us 116 : 6 : PG_RETURN_NULL();
117 : :
118 : : /* Internal representation is the same as text */
4544 andrew@dunslane.net 119 : 3056 : PG_RETURN_TEXT_P(result);
120 : : }
121 : :
122 : : /*
123 : : * Output.
124 : : */
125 : : Datum
4967 rhaas@postgresql.org 126 : 2830 : json_out(PG_FUNCTION_ARGS)
127 : : {
128 : : /* we needn't detoast because text_to_cstring will handle that */
4836 bruce@momjian.us 129 : 2830 : Datum txt = PG_GETARG_DATUM(0);
130 : :
4967 rhaas@postgresql.org 131 : 2830 : PG_RETURN_CSTRING(TextDatumGetCString(txt));
132 : : }
133 : :
134 : : /*
135 : : * Binary send.
136 : : */
137 : : Datum
4967 rhaas@postgresql.org 138 :UBC 0 : json_send(PG_FUNCTION_ARGS)
139 : : {
4836 bruce@momjian.us 140 : 0 : text *t = PG_GETARG_TEXT_PP(0);
141 : : StringInfoData buf;
142 : :
4967 rhaas@postgresql.org 143 : 0 : pq_begintypsend(&buf);
144 [ # # # # : 0 : pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
# # # # #
# # # ]
145 : 0 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
146 : : }
147 : :
148 : : /*
149 : : * Binary receive.
150 : : */
151 : : Datum
152 : 0 : json_recv(PG_FUNCTION_ARGS)
153 : : {
154 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
155 : : char *str;
156 : : int nbytes;
157 : : JsonLexContext lex;
158 : :
159 : 0 : str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
160 : :
161 : : /* Validate it. */
702 alvherre@alvh.no-ip. 162 : 0 : makeJsonLexContextCstringLen(&lex, str, nbytes, GetDatabaseEncoding(),
163 : : false);
164 : 0 : pg_parse_json_or_ereport(&lex, &nullSemAction);
165 : :
4185 andrew@dunslane.net 166 : 0 : PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));
167 : : }
168 : :
169 : : /*
170 : : * Turn a Datum into JSON text, appending the string to "result".
171 : : *
172 : : * tcategory and outfuncoid are from a previous call to json_categorize_type,
173 : : * except that if is_null is true then they can be invalid.
174 : : *
175 : : * If key_scalar is true, the value is being printed as a key, so insist
176 : : * it's of an acceptable type, and force it to be quoted.
177 : : */
178 : : static void
778 amitlan@postgresql.o 179 :CBC 4230 : datum_to_json_internal(Datum val, bool is_null, StringInfo result,
180 : : JsonTypeCategory tcategory, Oid outfuncoid,
181 : : bool key_scalar)
182 : : {
183 : : char *outputstr;
184 : : text *jsontext;
185 : :
3624 noah@leadboat.com 186 : 4230 : check_stack_depth();
187 : :
188 : : /* callers are expected to ensure that null keys are not passed in */
3931 tgl@sss.pgh.pa.us 189 [ + + - + ]: 4230 : Assert(!(key_scalar && is_null));
190 : :
4944 andrew@dunslane.net 191 [ + + ]: 4230 : if (is_null)
192 : : {
628 nathan@postgresql.or 193 : 159 : appendBinaryStringInfo(result, "null", strlen("null"));
4964 andrew@dunslane.net 194 : 159 : return;
195 : : }
196 : :
4138 tgl@sss.pgh.pa.us 197 [ + + + + ]: 4071 : if (key_scalar &&
198 [ + + ]: 994 : (tcategory == JSONTYPE_ARRAY ||
199 [ + + ]: 991 : tcategory == JSONTYPE_COMPOSITE ||
200 [ - + ]: 985 : tcategory == JSONTYPE_JSON ||
201 : : tcategory == JSONTYPE_CAST))
202 [ + - ]: 18 : ereport(ERROR,
203 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
204 : : errmsg("key value must be scalar, not array, composite, or json")));
205 : :
4964 andrew@dunslane.net 206 [ + + + + : 4053 : switch (tcategory)
+ + + + +
+ ]
207 : : {
4138 tgl@sss.pgh.pa.us 208 : 162 : case JSONTYPE_ARRAY:
4964 andrew@dunslane.net 209 : 162 : array_to_json_internal(val, result, false);
210 : 162 : break;
4138 tgl@sss.pgh.pa.us 211 : 250 : case JSONTYPE_COMPOSITE:
3995 sfrost@snowman.net 212 : 250 : composite_to_json(val, result, false);
4964 andrew@dunslane.net 213 : 250 : break;
4138 tgl@sss.pgh.pa.us 214 : 19 : case JSONTYPE_BOOL:
215 [ - + ]: 19 : if (key_scalar)
628 nathan@postgresql.or 216 :UBC 0 : appendStringInfoChar(result, '"');
628 nathan@postgresql.or 217 [ + + ]:CBC 19 : if (DatumGetBool(val))
218 : 13 : appendBinaryStringInfo(result, "true", strlen("true"));
219 : : else
220 : 6 : appendBinaryStringInfo(result, "false", strlen("false"));
221 [ - + ]: 19 : if (key_scalar)
628 nathan@postgresql.or 222 :UBC 0 : appendStringInfoChar(result, '"');
4964 andrew@dunslane.net 223 :CBC 19 : break;
4138 tgl@sss.pgh.pa.us 224 : 2565 : case JSONTYPE_NUMERIC:
225 : 2565 : outputstr = OidOutputFunctionCall(outfuncoid, val);
226 : :
227 : : /*
228 : : * Don't quote a non-key if it's a valid JSON number (i.e., not
229 : : * "Infinity", "-Infinity", or "NaN"). Since we know this is a
230 : : * numeric data type's output, we simplify and open-code the
231 : : * validation for better performance.
232 : : */
638 nathan@postgresql.or 233 [ + + ]: 2565 : if (!key_scalar &&
234 [ + + + + ]: 1815 : ((*outputstr >= '0' && *outputstr <= '9') ||
235 [ + + ]: 33 : (*outputstr == '-' &&
236 [ + - + + ]: 27 : (outputstr[1] >= '0' && outputstr[1] <= '9'))))
3932 andrew@dunslane.net 237 : 1806 : appendStringInfoString(result, outputstr);
238 : : else
239 : : {
638 nathan@postgresql.or 240 : 759 : appendStringInfoChar(result, '"');
241 : 759 : appendStringInfoString(result, outputstr);
242 : 759 : appendStringInfoChar(result, '"');
243 : : }
4947 andrew@dunslane.net 244 : 2565 : pfree(outputstr);
245 : 2565 : break;
4038 tgl@sss.pgh.pa.us 246 : 12 : case JSONTYPE_DATE:
247 : : {
248 : : char buf[MAXDATELEN + 1];
249 : :
2173 akorotkov@postgresql 250 : 12 : JsonEncodeDateTime(buf, val, DATEOID, NULL);
638 nathan@postgresql.or 251 : 12 : appendStringInfoChar(result, '"');
252 : 12 : appendStringInfoString(result, buf);
253 : 12 : appendStringInfoChar(result, '"');
254 : : }
2790 andrew@dunslane.net 255 : 12 : break;
256 : 13 : case JSONTYPE_TIMESTAMP:
257 : : {
258 : : char buf[MAXDATELEN + 1];
259 : :
2173 akorotkov@postgresql 260 : 13 : JsonEncodeDateTime(buf, val, TIMESTAMPOID, NULL);
638 nathan@postgresql.or 261 : 13 : appendStringInfoChar(result, '"');
262 : 13 : appendStringInfoString(result, buf);
263 : 13 : appendStringInfoChar(result, '"');
264 : : }
2790 andrew@dunslane.net 265 : 13 : break;
266 : 12 : case JSONTYPE_TIMESTAMPTZ:
267 : : {
268 : : char buf[MAXDATELEN + 1];
269 : :
2173 akorotkov@postgresql 270 : 12 : JsonEncodeDateTime(buf, val, TIMESTAMPTZOID, NULL);
638 nathan@postgresql.or 271 : 12 : appendStringInfoChar(result, '"');
272 : 12 : appendStringInfoString(result, buf);
273 : 12 : appendStringInfoChar(result, '"');
274 : : }
2790 andrew@dunslane.net 275 : 12 : break;
276 : 411 : case JSONTYPE_JSON:
277 : : /* JSON and JSONB output will already be escaped */
278 : 411 : outputstr = OidOutputFunctionCall(outfuncoid, val);
279 : 411 : appendStringInfoString(result, outputstr);
280 : 411 : pfree(outputstr);
281 : 411 : break;
282 : 2 : case JSONTYPE_CAST:
283 : : /* outfuncoid refers to a cast function, not an output function */
284 : 2 : jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
628 nathan@postgresql.or 285 [ - + ]: 2 : appendBinaryStringInfo(result, VARDATA_ANY(jsontext),
286 [ - + - - : 2 : VARSIZE_ANY_EXHDR(jsontext));
- - - - -
+ ]
2790 andrew@dunslane.net 287 : 2 : pfree(jsontext);
288 : 2 : break;
289 : 607 : default:
290 : : /* special-case text types to save useless palloc/memcpy cycles */
406 drowley@postgresql.o 291 [ + + + - : 607 : if (outfuncoid == F_TEXTOUT || outfuncoid == F_VARCHAROUT ||
- + ]
292 : : outfuncoid == F_BPCHAROUT)
293 : 456 : escape_json_text(result, (text *) DatumGetPointer(val));
294 : : else
295 : : {
296 : 151 : outputstr = OidOutputFunctionCall(outfuncoid, val);
297 : 151 : escape_json(result, outputstr);
298 : 151 : pfree(outputstr);
299 : : }
2790 andrew@dunslane.net 300 : 607 : break;
301 : : }
302 : : }
303 : :
304 : : /*
305 : : * Encode 'value' of datetime type 'typid' into JSON string in ISO format using
306 : : * optionally preallocated buffer 'buf'. Optional 'tzp' determines time-zone
307 : : * offset (in seconds) in which we want to show timestamptz.
308 : : */
309 : : char *
2173 akorotkov@postgresql 310 : 865 : JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp)
311 : : {
2790 andrew@dunslane.net 312 [ + + ]: 865 : if (!buf)
313 : 30 : buf = palloc(MAXDATELEN + 1);
314 : :
315 [ + + + + : 865 : switch (typid)
+ - ]
316 : : {
317 : 120 : case DATEOID:
318 : : {
319 : : DateADT date;
320 : : struct pg_tm tm;
321 : :
322 : 120 : date = DatumGetDateADT(value);
323 : :
324 : : /* Same as date_out(), but forcing DateStyle */
4038 tgl@sss.pgh.pa.us 325 [ + + + + ]: 120 : if (DATE_NOT_FINITE(date))
3609 326 : 12 : EncodeSpecialDate(date, buf);
327 : : else
328 : : {
4038 329 : 108 : j2date(date + POSTGRES_EPOCH_JDATE,
330 : : &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
331 : 108 : EncodeDateOnly(&tm, USE_XSD_DATES, buf);
332 : : }
333 : : }
334 : 120 : break;
2790 andrew@dunslane.net 335 : 135 : case TIMEOID:
336 : : {
337 : 135 : TimeADT time = DatumGetTimeADT(value);
338 : : struct pg_tm tt,
339 : 135 : *tm = &tt;
340 : : fsec_t fsec;
341 : :
342 : : /* Same as time_out(), but forcing DateStyle */
343 : 135 : time2tm(time, tm, &fsec);
344 : 135 : EncodeTimeOnly(tm, fsec, false, 0, USE_XSD_DATES, buf);
345 : : }
346 : 135 : break;
347 : 171 : case TIMETZOID:
348 : : {
349 : 171 : TimeTzADT *time = DatumGetTimeTzADTP(value);
350 : : struct pg_tm tt,
351 : 171 : *tm = &tt;
352 : : fsec_t fsec;
353 : : int tz;
354 : :
355 : : /* Same as timetz_out(), but forcing DateStyle */
356 : 171 : timetz2tm(time, tm, &fsec, &tz);
357 : 171 : EncodeTimeOnly(tm, fsec, true, tz, USE_XSD_DATES, buf);
358 : : }
359 : 171 : break;
360 : 184 : case TIMESTAMPOID:
361 : : {
362 : : Timestamp timestamp;
363 : : struct pg_tm tm;
364 : : fsec_t fsec;
365 : :
366 : 184 : timestamp = DatumGetTimestamp(value);
367 : : /* Same as timestamp_out(), but forcing DateStyle */
4113 368 [ + + + + ]: 184 : if (TIMESTAMP_NOT_FINITE(timestamp))
3609 tgl@sss.pgh.pa.us 369 : 12 : EncodeSpecialTimestamp(timestamp, buf);
4113 andrew@dunslane.net 370 [ + - ]: 172 : else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
371 : 172 : EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
372 : : else
4113 andrew@dunslane.net 373 [ # # ]:UBC 0 : ereport(ERROR,
374 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
375 : : errmsg("timestamp out of range")));
376 : : }
4113 andrew@dunslane.net 377 :CBC 184 : break;
2790 378 : 255 : case TIMESTAMPTZOID:
379 : : {
380 : : TimestampTz timestamp;
381 : : struct pg_tm tm;
382 : : int tz;
383 : : fsec_t fsec;
4113 384 : 255 : const char *tzn = NULL;
385 : :
2790 386 : 255 : timestamp = DatumGetTimestampTz(value);
387 : :
388 : : /*
389 : : * If a time zone is specified, we apply the time-zone shift,
390 : : * convert timestamptz to pg_tm as if it were without a time
391 : : * zone, and then use the specified time zone for converting
392 : : * the timestamp into a string.
393 : : */
2173 akorotkov@postgresql 394 [ + + ]: 255 : if (tzp)
395 : : {
396 : 231 : tz = *tzp;
397 : 231 : timestamp -= (TimestampTz) tz * USECS_PER_SEC;
398 : : }
399 : :
400 : : /* Same as timestamptz_out(), but forcing DateStyle */
4113 andrew@dunslane.net 401 [ + + + + ]: 255 : if (TIMESTAMP_NOT_FINITE(timestamp))
3609 tgl@sss.pgh.pa.us 402 : 12 : EncodeSpecialTimestamp(timestamp, buf);
2173 akorotkov@postgresql 403 [ + + + + : 243 : else if (timestamp2tm(timestamp, tzp ? NULL : &tz, &tm, &fsec,
+ - ]
404 : : tzp ? NULL : &tzn, NULL) == 0)
405 : : {
406 [ + + ]: 243 : if (tzp)
407 : 231 : tm.tm_isdst = 1; /* set time-zone presence flag */
408 : :
4113 andrew@dunslane.net 409 : 243 : EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
410 : : }
411 : : else
4113 andrew@dunslane.net 412 [ # # ]:UBC 0 : ereport(ERROR,
413 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
414 : : errmsg("timestamp out of range")));
415 : : }
4113 andrew@dunslane.net 416 :CBC 255 : break;
4964 andrew@dunslane.net 417 :UBC 0 : default:
1865 michael@paquier.xyz 418 [ # # ]: 0 : elog(ERROR, "unknown jsonb value datetime type oid %u", typid);
419 : : return NULL;
420 : : }
421 : :
2790 andrew@dunslane.net 422 :CBC 865 : return buf;
423 : : }
424 : :
425 : : /*
426 : : * Process a single dimension of an array.
427 : : * If it's the innermost dimension, output the values, otherwise call
428 : : * ourselves recursively to process the next dimension.
429 : : */
430 : : static void
4836 bruce@momjian.us 431 : 189 : array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals,
432 : : bool *nulls, int *valcount, JsonTypeCategory tcategory,
433 : : Oid outfuncoid, bool use_line_feeds)
434 : : {
435 : : int i;
436 : : const char *sep;
437 : :
4964 andrew@dunslane.net 438 [ - + ]: 189 : Assert(dim < ndims);
439 : :
440 [ + + ]: 189 : sep = use_line_feeds ? ",\n " : ",";
441 : :
442 : 189 : appendStringInfoChar(result, '[');
443 : :
444 [ + + ]: 705 : for (i = 1; i <= dims[dim]; i++)
445 : : {
446 [ + + ]: 516 : if (i > 1)
4836 bruce@momjian.us 447 : 327 : appendStringInfoString(result, sep);
448 : :
4964 andrew@dunslane.net 449 [ + + ]: 516 : if (dim + 1 == ndims)
450 : : {
778 amitlan@postgresql.o 451 : 510 : datum_to_json_internal(vals[*valcount], nulls[*valcount],
452 : : result, tcategory,
453 : : outfuncoid, false);
4964 andrew@dunslane.net 454 : 510 : (*valcount)++;
455 : : }
456 : : else
457 : : {
458 : : /*
459 : : * Do we want line feeds on inner dimensions of arrays? For now
460 : : * we'll say no.
461 : : */
4836 bruce@momjian.us 462 : 6 : array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
463 : : valcount, tcategory, outfuncoid, false);
464 : : }
465 : : }
466 : :
4964 andrew@dunslane.net 467 : 189 : appendStringInfoChar(result, ']');
468 : 189 : }
469 : :
470 : : /*
471 : : * Turn an array into JSON.
472 : : */
473 : : static void
474 : 183 : array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
475 : : {
476 : 183 : ArrayType *v = DatumGetArrayTypeP(array);
477 : 183 : Oid element_type = ARR_ELEMTYPE(v);
478 : : int *dim;
479 : : int ndim;
480 : : int nitems;
4836 bruce@momjian.us 481 : 183 : int count = 0;
482 : : Datum *elements;
483 : : bool *nulls;
484 : : int16 typlen;
485 : : bool typbyval;
486 : : char typalign;
487 : : JsonTypeCategory tcategory;
488 : : Oid outfuncoid;
489 : :
4964 andrew@dunslane.net 490 : 183 : ndim = ARR_NDIM(v);
491 : 183 : dim = ARR_DIMS(v);
492 : 183 : nitems = ArrayGetNItems(ndim, dim);
493 : :
494 [ - + ]: 183 : if (nitems <= 0)
495 : : {
4836 bruce@momjian.us 496 :UBC 0 : appendStringInfoString(result, "[]");
4964 andrew@dunslane.net 497 : 0 : return;
498 : : }
499 : :
4138 tgl@sss.pgh.pa.us 500 :CBC 183 : get_typlenbyvalalign(element_type,
501 : : &typlen, &typbyval, &typalign);
502 : :
779 amitlan@postgresql.o 503 : 183 : json_categorize_type(element_type, false,
504 : : &tcategory, &outfuncoid);
505 : :
4964 andrew@dunslane.net 506 : 183 : deconstruct_array(v, element_type, typlen, typbyval,
507 : : typalign, &elements, &nulls,
508 : : &nitems);
509 : :
4944 510 : 183 : array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
511 : : outfuncoid, use_line_feeds);
512 : :
4964 513 : 183 : pfree(elements);
514 : 183 : pfree(nulls);
515 : : }
516 : :
517 : : /*
518 : : * Turn a composite / record into JSON.
519 : : */
520 : : static void
3995 sfrost@snowman.net 521 : 628 : composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
522 : : {
523 : : HeapTupleHeader td;
524 : : Oid tupType;
525 : : int32 tupTypmod;
526 : : TupleDesc tupdesc;
527 : : HeapTupleData tmptup,
528 : : *tuple;
529 : : int i;
4836 bruce@momjian.us 530 : 628 : bool needsep = false;
531 : : const char *sep;
532 : : int seplen;
533 : :
534 : : /*
535 : : * We can avoid expensive strlen() calls by precalculating the separator
536 : : * length.
537 : : */
4964 andrew@dunslane.net 538 [ + + ]: 628 : sep = use_line_feeds ? ",\n " : ",";
638 nathan@postgresql.or 539 [ + + ]: 628 : seplen = use_line_feeds ? strlen(",\n ") : strlen(",");
540 : :
4836 bruce@momjian.us 541 : 628 : td = DatumGetHeapTupleHeader(composite);
542 : :
543 : : /* Extract rowtype info and find a tupdesc */
544 : 628 : tupType = HeapTupleHeaderGetTypeId(td);
545 : 628 : tupTypmod = HeapTupleHeaderGetTypMod(td);
546 : 628 : tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
547 : :
548 : : /* Build a temporary HeapTuple control structure */
549 : 628 : tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
550 : 628 : tmptup.t_data = td;
4964 andrew@dunslane.net 551 : 628 : tuple = &tmptup;
552 : :
4836 bruce@momjian.us 553 : 628 : appendStringInfoChar(result, '{');
554 : :
555 [ + + ]: 1854 : for (i = 0; i < tupdesc->natts; i++)
556 : : {
557 : : Datum val;
558 : : bool isnull;
559 : : char *attname;
560 : : JsonTypeCategory tcategory;
561 : : Oid outfuncoid;
2939 andres@anarazel.de 562 : 1226 : Form_pg_attribute att = TupleDescAttr(tupdesc, i);
563 : :
564 [ - + ]: 1226 : if (att->attisdropped)
4836 bruce@momjian.us 565 :UBC 0 : continue;
566 : :
4964 andrew@dunslane.net 567 [ + + ]:CBC 1226 : if (needsep)
638 nathan@postgresql.or 568 : 598 : appendBinaryStringInfo(result, sep, seplen);
4964 andrew@dunslane.net 569 : 1226 : needsep = true;
570 : :
2939 andres@anarazel.de 571 : 1226 : attname = NameStr(att->attname);
4836 bruce@momjian.us 572 : 1226 : escape_json(result, attname);
573 : 1226 : appendStringInfoChar(result, ':');
574 : :
3995 sfrost@snowman.net 575 : 1226 : val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
576 : :
4138 tgl@sss.pgh.pa.us 577 [ + + ]: 1226 : if (isnull)
578 : : {
579 : 90 : tcategory = JSONTYPE_NULL;
580 : 90 : outfuncoid = InvalidOid;
581 : : }
582 : : else
779 amitlan@postgresql.o 583 : 1136 : json_categorize_type(att->atttypid, false, &tcategory,
584 : : &outfuncoid);
585 : :
778 586 : 1226 : datum_to_json_internal(val, isnull, result, tcategory, outfuncoid,
587 : : false);
588 : : }
589 : :
4836 bruce@momjian.us 590 : 628 : appendStringInfoChar(result, '}');
591 [ + - ]: 628 : ReleaseTupleDesc(tupdesc);
4964 andrew@dunslane.net 592 : 628 : }
593 : :
594 : : /*
595 : : * Append JSON text for "val" to "result".
596 : : *
597 : : * This is just a thin wrapper around datum_to_json. If the same type will be
598 : : * printed many times, avoid using this; better to do the json_categorize_type
599 : : * lookups only once.
600 : : */
601 : : static void
4138 tgl@sss.pgh.pa.us 602 : 682 : add_json(Datum val, bool is_null, StringInfo result,
603 : : Oid val_type, bool key_scalar)
604 : : {
605 : : JsonTypeCategory tcategory;
606 : : Oid outfuncoid;
607 : :
4239 andrew@dunslane.net 608 [ - + ]: 682 : if (val_type == InvalidOid)
4239 andrew@dunslane.net 609 [ # # ]:UBC 0 : ereport(ERROR,
610 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
611 : : errmsg("could not determine input data type")));
612 : :
4138 tgl@sss.pgh.pa.us 613 [ + + ]:CBC 682 : if (is_null)
614 : : {
615 : 36 : tcategory = JSONTYPE_NULL;
616 : 36 : outfuncoid = InvalidOid;
617 : : }
618 : : else
779 amitlan@postgresql.o 619 : 646 : json_categorize_type(val_type, false,
620 : : &tcategory, &outfuncoid);
621 : :
778 622 : 682 : datum_to_json_internal(val, is_null, result, tcategory, outfuncoid,
623 : : key_scalar);
4239 andrew@dunslane.net 624 : 664 : }
625 : :
626 : : /*
627 : : * SQL function array_to_json(row)
628 : : */
629 : : Datum
4013 sfrost@snowman.net 630 : 9 : array_to_json(PG_FUNCTION_ARGS)
631 : : {
3995 632 : 9 : Datum array = PG_GETARG_DATUM(0);
633 : : StringInfo result;
634 : :
635 : 9 : result = makeStringInfo();
636 : :
637 : 9 : array_to_json_internal(array, result, false);
638 : :
639 : 9 : PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
640 : : }
641 : :
642 : : /*
643 : : * SQL function array_to_json(row, prettybool)
644 : : */
645 : : Datum
646 : 12 : array_to_json_pretty(PG_FUNCTION_ARGS)
647 : : {
4836 bruce@momjian.us 648 : 12 : Datum array = PG_GETARG_DATUM(0);
649 : 12 : bool use_line_feeds = PG_GETARG_BOOL(1);
650 : : StringInfo result;
651 : :
4964 andrew@dunslane.net 652 : 12 : result = makeStringInfo();
653 : :
654 : 12 : array_to_json_internal(array, result, use_line_feeds);
655 : :
4310 rhaas@postgresql.org 656 : 12 : PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
657 : : }
658 : :
659 : : /*
660 : : * SQL function row_to_json(row)
661 : : */
662 : : Datum
4964 andrew@dunslane.net 663 : 354 : row_to_json(PG_FUNCTION_ARGS)
664 : : {
3995 sfrost@snowman.net 665 : 354 : Datum array = PG_GETARG_DATUM(0);
666 : : StringInfo result;
667 : :
668 : 354 : result = makeStringInfo();
669 : :
670 : 354 : composite_to_json(array, result, false);
671 : :
672 : 354 : PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
673 : : }
674 : :
675 : : /*
676 : : * SQL function row_to_json(row, prettybool)
677 : : */
678 : : Datum
679 : 24 : row_to_json_pretty(PG_FUNCTION_ARGS)
680 : : {
4836 bruce@momjian.us 681 : 24 : Datum array = PG_GETARG_DATUM(0);
682 : 24 : bool use_line_feeds = PG_GETARG_BOOL(1);
683 : : StringInfo result;
684 : :
4964 andrew@dunslane.net 685 : 24 : result = makeStringInfo();
686 : :
3995 sfrost@snowman.net 687 : 24 : composite_to_json(array, result, use_line_feeds);
688 : :
4310 rhaas@postgresql.org 689 : 24 : PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
690 : : }
691 : :
692 : : /*
693 : : * Is the given type immutable when coming out of a JSON context?
694 : : *
695 : : * At present, datetimes are all considered mutable, because they
696 : : * depend on timezone. XXX we should also drill down into objects
697 : : * and arrays, but do not.
698 : : */
699 : : bool
892 alvherre@alvh.no-ip. 700 :UBC 0 : to_json_is_immutable(Oid typoid)
701 : : {
702 : : JsonTypeCategory tcategory;
703 : : Oid outfuncoid;
704 : :
779 amitlan@postgresql.o 705 : 0 : json_categorize_type(typoid, false, &tcategory, &outfuncoid);
706 : :
892 alvherre@alvh.no-ip. 707 [ # # # # : 0 : switch (tcategory)
# # ]
708 : : {
709 : 0 : case JSONTYPE_BOOL:
710 : : case JSONTYPE_JSON:
711 : : case JSONTYPE_JSONB:
712 : : case JSONTYPE_NULL:
713 : 0 : return true;
714 : :
715 : 0 : case JSONTYPE_DATE:
716 : : case JSONTYPE_TIMESTAMP:
717 : : case JSONTYPE_TIMESTAMPTZ:
718 : 0 : return false;
719 : :
720 : 0 : case JSONTYPE_ARRAY:
721 : 0 : return false; /* TODO recurse into elements */
722 : :
723 : 0 : case JSONTYPE_COMPOSITE:
724 : 0 : return false; /* TODO recurse into fields */
725 : :
726 : 0 : case JSONTYPE_NUMERIC:
727 : : case JSONTYPE_CAST:
728 : : case JSONTYPE_OTHER:
729 : 0 : return func_volatile(outfuncoid) == PROVOLATILE_IMMUTABLE;
730 : : }
731 : :
732 : 0 : return false; /* not reached */
733 : : }
734 : :
735 : : /*
736 : : * SQL function to_json(anyvalue)
737 : : */
738 : : Datum
4563 andrew@dunslane.net 739 :CBC 90 : to_json(PG_FUNCTION_ARGS)
740 : : {
4325 tgl@sss.pgh.pa.us 741 : 90 : Datum val = PG_GETARG_DATUM(0);
4563 andrew@dunslane.net 742 : 90 : Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
743 : : JsonTypeCategory tcategory;
744 : : Oid outfuncoid;
745 : :
746 [ - + ]: 90 : if (val_type == InvalidOid)
4563 andrew@dunslane.net 747 [ # # ]:UBC 0 : ereport(ERROR,
748 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
749 : : errmsg("could not determine input data type")));
750 : :
779 amitlan@postgresql.o 751 :CBC 90 : json_categorize_type(val_type, false,
752 : : &tcategory, &outfuncoid);
753 : :
778 754 : 90 : PG_RETURN_DATUM(datum_to_json(val, tcategory, outfuncoid));
755 : : }
756 : :
757 : : /*
758 : : * Turn a Datum into JSON text.
759 : : *
760 : : * tcategory and outfuncoid are from a previous call to json_categorize_type.
761 : : */
762 : : Datum
763 : 125 : datum_to_json(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
764 : : {
765 : 125 : StringInfo result = makeStringInfo();
766 : :
767 : 125 : datum_to_json_internal(val, false, result, tcategory, outfuncoid,
768 : : false);
769 : :
770 : 125 : return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
771 : : }
772 : :
773 : : /*
774 : : * json_agg transition function
775 : : *
776 : : * aggregate input column as a json array value.
777 : : */
778 : : static Datum
892 alvherre@alvh.no-ip. 779 : 277 : json_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null)
780 : : {
781 : : MemoryContext aggcontext,
782 : : oldcontext;
783 : : JsonAggState *state;
784 : : Datum val;
785 : :
4563 andrew@dunslane.net 786 [ - + ]: 277 : if (!AggCheckCallContext(fcinfo, &aggcontext))
787 : : {
788 : : /* cannot be called directly because of internal-type argument */
4563 andrew@dunslane.net 789 [ # # ]:UBC 0 : elog(ERROR, "json_agg_transfn called in non-aggregate context");
790 : : }
791 : :
4563 andrew@dunslane.net 792 [ + + ]:CBC 277 : if (PG_ARGISNULL(0))
793 : : {
3609 tgl@sss.pgh.pa.us 794 : 62 : Oid arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
795 : :
3641 andrew@dunslane.net 796 [ - + ]: 62 : if (arg_type == InvalidOid)
3641 andrew@dunslane.net 797 [ # # ]:UBC 0 : ereport(ERROR,
798 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
799 : : errmsg("could not determine input data type")));
800 : :
801 : : /*
802 : : * Make this state object in a context where it will persist for the
803 : : * duration of the aggregate call. MemoryContextSwitchTo is only
804 : : * needed the first time, as the StringInfo routines make sure they
805 : : * use the right context to enlarge the object if necessary.
806 : : */
4563 andrew@dunslane.net 807 :CBC 62 : oldcontext = MemoryContextSwitchTo(aggcontext);
3641 808 : 62 : state = (JsonAggState *) palloc(sizeof(JsonAggState));
809 : 62 : state->str = makeStringInfo();
4563 810 : 62 : MemoryContextSwitchTo(oldcontext);
811 : :
3641 812 : 62 : appendStringInfoChar(state->str, '[');
779 amitlan@postgresql.o 813 : 62 : json_categorize_type(arg_type, false, &state->val_category,
814 : : &state->val_output_func);
815 : : }
816 : : else
817 : : {
3641 andrew@dunslane.net 818 : 215 : state = (JsonAggState *) PG_GETARG_POINTER(0);
819 : : }
820 : :
892 alvherre@alvh.no-ip. 821 [ + + + + ]: 277 : if (absent_on_null && PG_ARGISNULL(1))
822 : 45 : PG_RETURN_POINTER(state);
823 : :
824 [ + + ]: 232 : if (state->str->len > 1)
825 : 173 : appendStringInfoString(state->str, ", ");
826 : :
827 : : /* fast path for NULLs */
4563 andrew@dunslane.net 828 [ + + ]: 232 : if (PG_ARGISNULL(1))
829 : : {
778 amitlan@postgresql.o 830 : 27 : datum_to_json_internal((Datum) 0, true, state->str, JSONTYPE_NULL,
831 : : InvalidOid, false);
4563 andrew@dunslane.net 832 : 27 : PG_RETURN_POINTER(state);
833 : : }
834 : :
4325 tgl@sss.pgh.pa.us 835 : 205 : val = PG_GETARG_DATUM(1);
836 : :
837 : : /* add some whitespace if structured type and not first item */
892 alvherre@alvh.no-ip. 838 [ + + + + ]: 205 : if (!PG_ARGISNULL(0) && state->str->len > 1 &&
3641 andrew@dunslane.net 839 [ + + ]: 152 : (state->val_category == JSONTYPE_ARRAY ||
840 [ + + ]: 149 : state->val_category == JSONTYPE_COMPOSITE))
841 : : {
842 : 56 : appendStringInfoString(state->str, "\n ");
843 : : }
844 : :
778 amitlan@postgresql.o 845 : 205 : datum_to_json_internal(val, false, state->str, state->val_category,
846 : : state->val_output_func, false);
847 : :
848 : : /*
849 : : * The transition type for json_agg() is declared to be "internal", which
850 : : * is a pass-by-value type the same size as a pointer. So we can safely
851 : : * pass the JsonAggState pointer through nodeAgg.c's machinations.
852 : : */
4563 andrew@dunslane.net 853 : 205 : PG_RETURN_POINTER(state);
854 : : }
855 : :
856 : :
857 : : /*
858 : : * json_agg aggregate function
859 : : */
860 : : Datum
892 alvherre@alvh.no-ip. 861 : 79 : json_agg_transfn(PG_FUNCTION_ARGS)
862 : : {
863 : 79 : return json_agg_transfn_worker(fcinfo, false);
864 : : }
865 : :
866 : : /*
867 : : * json_agg_strict aggregate function
868 : : */
869 : : Datum
870 : 198 : json_agg_strict_transfn(PG_FUNCTION_ARGS)
871 : : {
872 : 198 : return json_agg_transfn_worker(fcinfo, true);
873 : : }
874 : :
875 : : /*
876 : : * json_agg final function
877 : : */
878 : : Datum
4563 andrew@dunslane.net 879 : 68 : json_agg_finalfn(PG_FUNCTION_ARGS)
880 : : {
881 : : JsonAggState *state;
882 : :
883 : : /* cannot be called directly because of internal-type argument */
884 [ - + ]: 68 : Assert(AggCheckCallContext(fcinfo, NULL));
885 : :
3641 886 : 136 : state = PG_ARGISNULL(0) ?
887 [ + + ]: 68 : NULL :
888 : 62 : (JsonAggState *) PG_GETARG_POINTER(0);
889 : :
890 : : /* NULL result for no rows in, as is standard with aggregates */
4563 891 [ + + ]: 68 : if (state == NULL)
892 : 6 : PG_RETURN_NULL();
893 : :
894 : : /* Else return state with appropriate array terminator added */
3641 895 : 62 : PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, "]"));
896 : : }
897 : :
898 : : /* Functions implementing hash table for key uniqueness check */
899 : : static uint32
892 alvherre@alvh.no-ip. 900 : 831 : json_unique_hash(const void *key, Size keysize)
901 : : {
902 : 831 : const JsonUniqueHashEntry *entry = (JsonUniqueHashEntry *) key;
903 : 831 : uint32 hash = hash_bytes_uint32(entry->object_id);
904 : :
905 : 831 : hash ^= hash_bytes((const unsigned char *) entry->key, entry->key_len);
906 : :
29 peter@eisentraut.org 907 :GNC 831 : return hash;
908 : : }
909 : :
910 : : static int
892 alvherre@alvh.no-ip. 911 :CBC 57 : json_unique_hash_match(const void *key1, const void *key2, Size keysize)
912 : : {
913 : 57 : const JsonUniqueHashEntry *entry1 = (const JsonUniqueHashEntry *) key1;
914 : 57 : const JsonUniqueHashEntry *entry2 = (const JsonUniqueHashEntry *) key2;
915 : :
916 [ - + ]: 57 : if (entry1->object_id != entry2->object_id)
892 alvherre@alvh.no-ip. 917 [ # # ]:UBC 0 : return entry1->object_id > entry2->object_id ? 1 : -1;
918 : :
892 alvherre@alvh.no-ip. 919 [ - + ]:CBC 57 : if (entry1->key_len != entry2->key_len)
892 alvherre@alvh.no-ip. 920 [ # # ]:UBC 0 : return entry1->key_len > entry2->key_len ? 1 : -1;
921 : :
892 alvherre@alvh.no-ip. 922 :CBC 57 : return strncmp(entry1->key, entry2->key, entry1->key_len);
923 : : }
924 : :
925 : : /*
926 : : * Uniqueness detection support.
927 : : *
928 : : * In order to detect uniqueness during building or parsing of a JSON
929 : : * object, we maintain a hash table of key names already seen.
930 : : */
931 : : static void
932 : 159 : json_unique_check_init(JsonUniqueCheckState *cxt)
933 : : {
934 : : HASHCTL ctl;
935 : :
936 : 159 : memset(&ctl, 0, sizeof(ctl));
937 : 159 : ctl.keysize = sizeof(JsonUniqueHashEntry);
938 : 159 : ctl.entrysize = sizeof(JsonUniqueHashEntry);
939 : 159 : ctl.hcxt = CurrentMemoryContext;
940 : 159 : ctl.hash = json_unique_hash;
941 : 159 : ctl.match = json_unique_hash_match;
942 : :
943 : 159 : *cxt = hash_create("json object hashtable",
944 : : 32,
945 : : &ctl,
946 : : HASH_ELEM | HASH_CONTEXT | HASH_FUNCTION | HASH_COMPARE);
947 : 159 : }
948 : :
949 : : static void
950 : 43 : json_unique_builder_init(JsonUniqueBuilderState *cxt)
951 : : {
952 : 43 : json_unique_check_init(&cxt->check);
953 : 43 : cxt->mcxt = CurrentMemoryContext;
954 : 43 : cxt->skipped_keys.data = NULL;
955 : 43 : }
956 : :
957 : : static bool
958 : 831 : json_unique_check_key(JsonUniqueCheckState *cxt, const char *key, int object_id)
959 : : {
960 : : JsonUniqueHashEntry entry;
961 : : bool found;
962 : :
963 : 831 : entry.key = key;
964 : 831 : entry.key_len = strlen(key);
965 : 831 : entry.object_id = object_id;
966 : :
967 : 831 : (void) hash_search(*cxt, &entry, HASH_ENTER, &found);
968 : :
969 : 831 : return !found;
970 : : }
971 : :
972 : : /*
973 : : * On-demand initialization of a throwaway StringInfo. This is used to
974 : : * read a key name that we don't need to store in the output object, for
975 : : * duplicate key detection when the value is NULL.
976 : : */
977 : : static StringInfo
978 : 21 : json_unique_builder_get_throwawaybuf(JsonUniqueBuilderState *cxt)
979 : : {
980 : 21 : StringInfo out = &cxt->skipped_keys;
981 : :
982 [ + + ]: 21 : if (!out->data)
983 : : {
984 : 15 : MemoryContext oldcxt = MemoryContextSwitchTo(cxt->mcxt);
985 : :
986 : 15 : initStringInfo(out);
987 : 15 : MemoryContextSwitchTo(oldcxt);
988 : : }
989 : : else
990 : : /* Just reset the string to empty */
991 : 6 : out->len = 0;
992 : :
993 : 21 : return out;
994 : : }
995 : :
996 : : /*
997 : : * json_object_agg transition function.
998 : : *
999 : : * aggregate two input columns as a single json object value.
1000 : : */
1001 : : static Datum
1002 : 759 : json_object_agg_transfn_worker(FunctionCallInfo fcinfo,
1003 : : bool absent_on_null, bool unique_keys)
1004 : : {
1005 : : MemoryContext aggcontext,
1006 : : oldcontext;
1007 : : JsonAggState *state;
1008 : : StringInfo out;
1009 : : Datum arg;
1010 : : bool skip;
1011 : : int key_offset;
1012 : :
4239 andrew@dunslane.net 1013 [ - + ]: 759 : if (!AggCheckCallContext(fcinfo, &aggcontext))
1014 : : {
1015 : : /* cannot be called directly because of internal-type argument */
3931 tgl@sss.pgh.pa.us 1016 [ # # ]:UBC 0 : elog(ERROR, "json_object_agg_transfn called in non-aggregate context");
1017 : : }
1018 : :
4239 andrew@dunslane.net 1019 [ + + ]:CBC 759 : if (PG_ARGISNULL(0))
1020 : : {
1021 : : Oid arg_type;
1022 : :
1023 : : /*
1024 : : * Make the StringInfo in a context where it will persist for the
1025 : : * duration of the aggregate call. Switching context is only needed
1026 : : * for this initial step, as the StringInfo and dynahash routines make
1027 : : * sure they use the right context to enlarge the object if necessary.
1028 : : */
1029 : 67 : oldcontext = MemoryContextSwitchTo(aggcontext);
3641 1030 : 67 : state = (JsonAggState *) palloc(sizeof(JsonAggState));
1031 : 67 : state->str = makeStringInfo();
892 alvherre@alvh.no-ip. 1032 [ + + ]: 67 : if (unique_keys)
1033 : 27 : json_unique_builder_init(&state->unique_check);
1034 : : else
1035 : 40 : memset(&state->unique_check, 0, sizeof(state->unique_check));
4239 andrew@dunslane.net 1036 : 67 : MemoryContextSwitchTo(oldcontext);
1037 : :
3641 1038 : 67 : arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1039 : :
1040 [ - + ]: 67 : if (arg_type == InvalidOid)
3641 andrew@dunslane.net 1041 [ # # ]:UBC 0 : ereport(ERROR,
1042 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1043 : : errmsg("could not determine data type for argument %d", 1)));
1044 : :
779 amitlan@postgresql.o 1045 :CBC 67 : json_categorize_type(arg_type, false, &state->key_category,
1046 : : &state->key_output_func);
1047 : :
3641 andrew@dunslane.net 1048 : 67 : arg_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
1049 : :
1050 [ - + ]: 67 : if (arg_type == InvalidOid)
3641 andrew@dunslane.net 1051 [ # # ]:UBC 0 : ereport(ERROR,
1052 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1053 : : errmsg("could not determine data type for argument %d", 2)));
1054 : :
779 amitlan@postgresql.o 1055 :CBC 67 : json_categorize_type(arg_type, false, &state->val_category,
1056 : : &state->val_output_func);
1057 : :
3641 andrew@dunslane.net 1058 : 67 : appendStringInfoString(state->str, "{ ");
1059 : : }
1060 : : else
1061 : : {
1062 : 692 : state = (JsonAggState *) PG_GETARG_POINTER(0);
1063 : : }
1064 : :
1065 : : /*
1066 : : * Note: since json_object_agg() is declared as taking type "any", the
1067 : : * parser will not do any type conversion on unknown-type literals (that
1068 : : * is, undecorated strings or NULLs). Such values will arrive here as
1069 : : * type UNKNOWN, which fortunately does not matter to us, since
1070 : : * unknownout() works fine.
1071 : : */
1072 : :
4046 tgl@sss.pgh.pa.us 1073 [ + + ]: 759 : if (PG_ARGISNULL(1))
1074 [ + - ]: 6 : ereport(ERROR,
1075 : : (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1076 : : errmsg("null value not allowed for object key")));
1077 : :
1078 : : /* Skip null values if absent_on_null */
892 alvherre@alvh.no-ip. 1079 [ + + + + ]: 753 : skip = absent_on_null && PG_ARGISNULL(2);
1080 : :
1081 [ + + ]: 753 : if (skip)
1082 : : {
1083 : : /*
1084 : : * We got a NULL value and we're not storing those; if we're not
1085 : : * testing key uniqueness, we're done. If we are, use the throwaway
1086 : : * buffer to store the key name so that we can check it.
1087 : : */
1088 [ + + ]: 27 : if (!unique_keys)
1089 : 12 : PG_RETURN_POINTER(state);
1090 : :
1091 : 15 : out = json_unique_builder_get_throwawaybuf(&state->unique_check);
1092 : : }
1093 : : else
1094 : : {
1095 : 726 : out = state->str;
1096 : :
1097 : : /*
1098 : : * Append comma delimiter only if we have already output some fields
1099 : : * after the initial string "{ ".
1100 : : */
1101 [ + + ]: 726 : if (out->len > 2)
1102 : 662 : appendStringInfoString(out, ", ");
1103 : : }
1104 : :
4046 tgl@sss.pgh.pa.us 1105 : 741 : arg = PG_GETARG_DATUM(1);
1106 : :
892 alvherre@alvh.no-ip. 1107 : 741 : key_offset = out->len;
1108 : :
778 amitlan@postgresql.o 1109 : 741 : datum_to_json_internal(arg, false, out, state->key_category,
1110 : : state->key_output_func, true);
1111 : :
892 alvherre@alvh.no-ip. 1112 [ + + ]: 741 : if (unique_keys)
1113 : : {
1114 : : /*
1115 : : * Copy the key first, instead of pointing into the buffer. It will be
1116 : : * added to the hash table, but the buffer may get reallocated as
1117 : : * we're appending more data to it. That would invalidate pointers to
1118 : : * keys in the current buffer.
1119 : : */
360 tomas.vondra@postgre 1120 : 654 : const char *key = MemoryContextStrdup(aggcontext,
1121 : 654 : &out->data[key_offset]);
1122 : :
892 alvherre@alvh.no-ip. 1123 [ + + ]: 654 : if (!json_unique_check_key(&state->unique_check.check, key, 0))
1124 [ + - ]: 18 : ereport(ERROR,
1125 : : errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1126 : : errmsg("duplicate JSON object key value: %s", key));
1127 : :
1128 [ + + ]: 636 : if (skip)
1129 : 9 : PG_RETURN_POINTER(state);
1130 : : }
1131 : :
3641 andrew@dunslane.net 1132 : 714 : appendStringInfoString(state->str, " : ");
1133 : :
4046 tgl@sss.pgh.pa.us 1134 [ + + ]: 714 : if (PG_ARGISNULL(2))
1135 : 6 : arg = (Datum) 0;
1136 : : else
1137 : 708 : arg = PG_GETARG_DATUM(2);
1138 : :
778 amitlan@postgresql.o 1139 : 714 : datum_to_json_internal(arg, PG_ARGISNULL(2), state->str,
1140 : : state->val_category,
1141 : : state->val_output_func, false);
1142 : :
4239 andrew@dunslane.net 1143 : 714 : PG_RETURN_POINTER(state);
1144 : : }
1145 : :
1146 : : /*
1147 : : * json_object_agg aggregate function
1148 : : */
1149 : : Datum
892 alvherre@alvh.no-ip. 1150 : 75 : json_object_agg_transfn(PG_FUNCTION_ARGS)
1151 : : {
1152 : 75 : return json_object_agg_transfn_worker(fcinfo, false, false);
1153 : : }
1154 : :
1155 : : /*
1156 : : * json_object_agg_strict aggregate function
1157 : : */
1158 : : Datum
1159 : 30 : json_object_agg_strict_transfn(PG_FUNCTION_ARGS)
1160 : : {
1161 : 30 : return json_object_agg_transfn_worker(fcinfo, true, false);
1162 : : }
1163 : :
1164 : : /*
1165 : : * json_object_agg_unique aggregate function
1166 : : */
1167 : : Datum
1168 : 627 : json_object_agg_unique_transfn(PG_FUNCTION_ARGS)
1169 : : {
1170 : 627 : return json_object_agg_transfn_worker(fcinfo, false, true);
1171 : : }
1172 : :
1173 : : /*
1174 : : * json_object_agg_unique_strict aggregate function
1175 : : */
1176 : : Datum
1177 : 27 : json_object_agg_unique_strict_transfn(PG_FUNCTION_ARGS)
1178 : : {
1179 : 27 : return json_object_agg_transfn_worker(fcinfo, true, true);
1180 : : }
1181 : :
1182 : : /*
1183 : : * json_object_agg final function.
1184 : : */
1185 : : Datum
4239 andrew@dunslane.net 1186 : 52 : json_object_agg_finalfn(PG_FUNCTION_ARGS)
1187 : : {
1188 : : JsonAggState *state;
1189 : :
1190 : : /* cannot be called directly because of internal-type argument */
1191 [ - + ]: 52 : Assert(AggCheckCallContext(fcinfo, NULL));
1192 : :
3641 1193 [ + + ]: 52 : state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0);
1194 : :
1195 : : /* NULL result for no rows in, as is standard with aggregates */
4239 1196 [ + + ]: 52 : if (state == NULL)
3999 1197 : 3 : PG_RETURN_NULL();
1198 : :
1199 : : /* Else return state with appropriate object terminator added */
3641 1200 : 49 : PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }"));
1201 : : }
1202 : :
1203 : : /*
1204 : : * Helper function for aggregates: return given StringInfo's contents plus
1205 : : * specified trailing string, as a text datum. We need this because aggregate
1206 : : * final functions are not allowed to modify the aggregate state.
1207 : : */
1208 : : static text *
3931 tgl@sss.pgh.pa.us 1209 : 111 : catenate_stringinfo_string(StringInfo buffer, const char *addon)
1210 : : {
1211 : : /* custom version of cstring_to_text_with_len */
1212 : 111 : int buflen = buffer->len;
1213 : 111 : int addlen = strlen(addon);
1214 : 111 : text *result = (text *) palloc(buflen + addlen + VARHDRSZ);
1215 : :
1216 : 111 : SET_VARSIZE(result, buflen + addlen + VARHDRSZ);
1217 : 111 : memcpy(VARDATA(result), buffer->data, buflen);
1218 : 111 : memcpy(VARDATA(result) + buflen, addon, addlen);
1219 : :
1220 : 111 : return result;
1221 : : }
1222 : :
1223 : : Datum
697 peter@eisentraut.org 1224 : 217 : json_build_object_worker(int nargs, const Datum *args, const bool *nulls, const Oid *types,
1225 : : bool absent_on_null, bool unique_keys)
1226 : : {
1227 : : int i;
4046 tgl@sss.pgh.pa.us 1228 : 217 : const char *sep = "";
1229 : : StringInfo result;
1230 : : JsonUniqueBuilderState unique_check;
1231 : :
4239 andrew@dunslane.net 1232 [ + + ]: 217 : if (nargs % 2 != 0)
1233 [ + - ]: 9 : ereport(ERROR,
1234 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1235 : : errmsg("argument list must have even number of elements"),
1236 : : /* translator: %s is a SQL function name */
1237 : : errhint("The arguments of %s must consist of alternating keys and values.",
1238 : : "json_build_object()")));
1239 : :
1240 : 208 : result = makeStringInfo();
1241 : :
1242 : 208 : appendStringInfoChar(result, '{');
1243 : :
892 alvherre@alvh.no-ip. 1244 [ + + ]: 208 : if (unique_keys)
1245 : 16 : json_unique_builder_init(&unique_check);
1246 : :
4239 andrew@dunslane.net 1247 [ + + ]: 443 : for (i = 0; i < nargs; i += 2)
1248 : : {
1249 : : StringInfo out;
1250 : : bool skip;
1251 : : int key_offset;
1252 : :
1253 : : /* Skip null values if absent_on_null */
892 alvherre@alvh.no-ip. 1254 [ + + + + ]: 281 : skip = absent_on_null && nulls[i + 1];
1255 : :
1256 [ + + ]: 281 : if (skip)
1257 : : {
1258 : : /* If key uniqueness check is needed we must save skipped keys */
1259 [ + + ]: 13 : if (!unique_keys)
1260 : 7 : continue;
1261 : :
1262 : 6 : out = json_unique_builder_get_throwawaybuf(&unique_check);
1263 : : }
1264 : : else
1265 : : {
1266 : 268 : appendStringInfoString(result, sep);
1267 : 268 : sep = ", ";
1268 : 268 : out = result;
1269 : : }
1270 : :
1271 : : /* process key */
2873 andrew@dunslane.net 1272 [ + + ]: 274 : if (nulls[i])
4239 1273 [ + - ]: 12 : ereport(ERROR,
1274 : : (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1275 : : errmsg("null value not allowed for object key")));
1276 : :
1277 : : /* save key offset before appending it */
892 alvherre@alvh.no-ip. 1278 : 262 : key_offset = out->len;
1279 : :
1280 : 262 : add_json(args[i], false, out, types[i], true);
1281 : :
1282 [ + + ]: 244 : if (unique_keys)
1283 : : {
1284 : : /*
1285 : : * check key uniqueness after key appending
1286 : : *
1287 : : * Copy the key first, instead of pointing into the buffer. It
1288 : : * will be added to the hash table, but the buffer may get
1289 : : * reallocated as we're appending more data to it. That would
1290 : : * invalidate pointers to keys in the current buffer.
1291 : : */
360 tomas.vondra@postgre 1292 : 47 : const char *key = pstrdup(&out->data[key_offset]);
1293 : :
892 alvherre@alvh.no-ip. 1294 [ + + ]: 47 : if (!json_unique_check_key(&unique_check.check, key, 0))
1295 [ + - ]: 16 : ereport(ERROR,
1296 : : errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1297 : : errmsg("duplicate JSON object key value: %s", key));
1298 : :
1299 [ + + ]: 31 : if (skip)
1300 : 3 : continue;
1301 : : }
1302 : :
4239 andrew@dunslane.net 1303 : 225 : appendStringInfoString(result, " : ");
1304 : :
1305 : : /* process value */
2873 1306 : 225 : add_json(args[i + 1], nulls[i + 1], result, types[i + 1], false);
1307 : : }
1308 : :
4239 1309 : 162 : appendStringInfoChar(result, '}');
1310 : :
892 alvherre@alvh.no-ip. 1311 : 162 : return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
1312 : : }
1313 : :
1314 : : /*
1315 : : * SQL function json_build_object(variadic "any")
1316 : : */
1317 : : Datum
1318 : 78 : json_build_object(PG_FUNCTION_ARGS)
1319 : : {
1320 : : Datum *args;
1321 : : bool *nulls;
1322 : : Oid *types;
1323 : :
1324 : : /* build argument values to build the object */
1325 : 78 : int nargs = extract_variadic_args(fcinfo, 0, true,
1326 : : &args, &types, &nulls);
1327 : :
1328 [ + + ]: 78 : if (nargs < 0)
1329 : 3 : PG_RETURN_NULL();
1330 : :
1331 : 75 : PG_RETURN_DATUM(json_build_object_worker(nargs, args, nulls, types, false, false));
1332 : : }
1333 : :
1334 : : /*
1335 : : * degenerate case of json_build_object where it gets 0 arguments.
1336 : : */
1337 : : Datum
4239 andrew@dunslane.net 1338 : 3 : json_build_object_noargs(PG_FUNCTION_ARGS)
1339 : : {
1340 : 3 : PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
1341 : : }
1342 : :
1343 : : Datum
697 peter@eisentraut.org 1344 : 115 : json_build_array_worker(int nargs, const Datum *args, const bool *nulls, const Oid *types,
1345 : : bool absent_on_null)
1346 : : {
1347 : : int i;
4046 tgl@sss.pgh.pa.us 1348 : 115 : const char *sep = "";
1349 : : StringInfo result;
1350 : :
4239 andrew@dunslane.net 1351 : 115 : result = makeStringInfo();
1352 : :
1353 : 115 : appendStringInfoChar(result, '[');
1354 : :
1355 [ + + ]: 319 : for (i = 0; i < nargs; i++)
1356 : : {
892 alvherre@alvh.no-ip. 1357 [ + + + + ]: 204 : if (absent_on_null && nulls[i])
1358 : 9 : continue;
1359 : :
4046 tgl@sss.pgh.pa.us 1360 : 195 : appendStringInfoString(result, sep);
1361 : 195 : sep = ", ";
2873 andrew@dunslane.net 1362 : 195 : add_json(args[i], nulls[i], result, types[i], false);
1363 : : }
1364 : :
4239 1365 : 115 : appendStringInfoChar(result, ']');
1366 : :
892 alvherre@alvh.no-ip. 1367 : 115 : return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
1368 : : }
1369 : :
1370 : : /*
1371 : : * SQL function json_build_array(variadic "any")
1372 : : */
1373 : : Datum
1374 : 27 : json_build_array(PG_FUNCTION_ARGS)
1375 : : {
1376 : : Datum *args;
1377 : : bool *nulls;
1378 : : Oid *types;
1379 : :
1380 : : /* build argument values to build the object */
1381 : 27 : int nargs = extract_variadic_args(fcinfo, 0, true,
1382 : : &args, &types, &nulls);
1383 : :
1384 [ + + ]: 27 : if (nargs < 0)
1385 : 3 : PG_RETURN_NULL();
1386 : :
1387 : 24 : PG_RETURN_DATUM(json_build_array_worker(nargs, args, nulls, types, false));
1388 : : }
1389 : :
1390 : : /*
1391 : : * degenerate case of json_build_array where it gets 0 arguments.
1392 : : */
1393 : : Datum
4239 andrew@dunslane.net 1394 : 3 : json_build_array_noargs(PG_FUNCTION_ARGS)
1395 : : {
1396 : 3 : PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2));
1397 : : }
1398 : :
1399 : : /*
1400 : : * SQL function json_object(text[])
1401 : : *
1402 : : * take a one or two dimensional array of text as key/value pairs
1403 : : * for a json object.
1404 : : */
1405 : : Datum
1406 : 24 : json_object(PG_FUNCTION_ARGS)
1407 : : {
1408 : 24 : ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
1409 : 24 : int ndims = ARR_NDIM(in_array);
1410 : : StringInfoData result;
1411 : : Datum *in_datums;
1412 : : bool *in_nulls;
1413 : : int in_count,
1414 : : count,
1415 : : i;
1416 : : text *rval;
1417 : :
1418 [ + + + + ]: 24 : switch (ndims)
1419 : : {
1420 : 3 : case 0:
1421 : 3 : PG_RETURN_DATUM(CStringGetTextDatum("{}"));
1422 : : break;
1423 : :
1424 : 9 : case 1:
1425 [ + + ]: 9 : if ((ARR_DIMS(in_array)[0]) % 2)
1426 [ + - ]: 3 : ereport(ERROR,
1427 : : (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1428 : : errmsg("array must have even number of elements")));
1429 : 6 : break;
1430 : :
1431 : 9 : case 2:
1432 [ + + ]: 9 : if ((ARR_DIMS(in_array)[1]) != 2)
1433 [ + - ]: 6 : ereport(ERROR,
1434 : : (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1435 : : errmsg("array must have two columns")));
1436 : 3 : break;
1437 : :
1438 : 3 : default:
1439 [ + - ]: 3 : ereport(ERROR,
1440 : : (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1441 : : errmsg("wrong number of array subscripts")));
1442 : : }
1443 : :
1163 peter@eisentraut.org 1444 : 9 : deconstruct_array_builtin(in_array, TEXTOID, &in_datums, &in_nulls, &in_count);
1445 : :
4239 andrew@dunslane.net 1446 : 9 : count = in_count / 2;
1447 : :
1448 : 9 : initStringInfo(&result);
1449 : :
1450 : 9 : appendStringInfoChar(&result, '{');
1451 : :
1452 [ + + ]: 933 : for (i = 0; i < count; ++i)
1453 : : {
1454 [ - + ]: 924 : if (in_nulls[i * 2])
4239 andrew@dunslane.net 1455 [ # # ]:UBC 0 : ereport(ERROR,
1456 : : (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1457 : : errmsg("null value not allowed for object key")));
1458 : :
4239 andrew@dunslane.net 1459 [ + + ]:CBC 924 : if (i > 0)
1460 : 915 : appendStringInfoString(&result, ", ");
406 drowley@postgresql.o 1461 : 924 : escape_json_text(&result, (text *) DatumGetPointer(in_datums[i * 2]));
4239 andrew@dunslane.net 1462 : 924 : appendStringInfoString(&result, " : ");
1463 [ + + ]: 924 : if (in_nulls[i * 2 + 1])
1464 : 6 : appendStringInfoString(&result, "null");
1465 : : else
1466 : : {
406 drowley@postgresql.o 1467 : 918 : escape_json_text(&result,
1468 : 918 : (text *) DatumGetPointer(in_datums[i * 2 + 1]));
1469 : : }
1470 : : }
1471 : :
4239 andrew@dunslane.net 1472 : 9 : appendStringInfoChar(&result, '}');
1473 : :
1474 : 9 : pfree(in_datums);
1475 : 9 : pfree(in_nulls);
1476 : :
1477 : 9 : rval = cstring_to_text_with_len(result.data, result.len);
1478 : 9 : pfree(result.data);
1479 : :
1480 : 9 : PG_RETURN_TEXT_P(rval);
1481 : : }
1482 : :
1483 : : /*
1484 : : * SQL function json_object(text[], text[])
1485 : : *
1486 : : * take separate key and value arrays of text to construct a json object
1487 : : * pairwise.
1488 : : */
1489 : : Datum
1490 : 21 : json_object_two_arg(PG_FUNCTION_ARGS)
1491 : : {
1492 : 21 : ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
1493 : 21 : ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
1494 : 21 : int nkdims = ARR_NDIM(key_array);
1495 : 21 : int nvdims = ARR_NDIM(val_array);
1496 : : StringInfoData result;
1497 : : Datum *key_datums,
1498 : : *val_datums;
1499 : : bool *key_nulls,
1500 : : *val_nulls;
1501 : : int key_count,
1502 : : val_count,
1503 : : i;
1504 : : text *rval;
1505 : :
1506 [ + + - + ]: 21 : if (nkdims > 1 || nkdims != nvdims)
1507 [ + - ]: 3 : ereport(ERROR,
1508 : : (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1509 : : errmsg("wrong number of array subscripts")));
1510 : :
1511 [ + + ]: 18 : if (nkdims == 0)
1512 : 3 : PG_RETURN_DATUM(CStringGetTextDatum("{}"));
1513 : :
1163 peter@eisentraut.org 1514 : 15 : deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count);
1515 : 15 : deconstruct_array_builtin(val_array, TEXTOID, &val_datums, &val_nulls, &val_count);
1516 : :
4239 andrew@dunslane.net 1517 [ + + ]: 15 : if (key_count != val_count)
1518 [ + - ]: 6 : ereport(ERROR,
1519 : : (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1520 : : errmsg("mismatched array dimensions")));
1521 : :
1522 : 9 : initStringInfo(&result);
1523 : :
1524 : 9 : appendStringInfoChar(&result, '{');
1525 : :
1526 [ + + ]: 39 : for (i = 0; i < key_count; ++i)
1527 : : {
1528 [ + + ]: 33 : if (key_nulls[i])
1529 [ + - ]: 3 : ereport(ERROR,
1530 : : (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1531 : : errmsg("null value not allowed for object key")));
1532 : :
1533 [ + + ]: 30 : if (i > 0)
1534 : 21 : appendStringInfoString(&result, ", ");
406 drowley@postgresql.o 1535 : 30 : escape_json_text(&result, (text *) DatumGetPointer(key_datums[i]));
4239 andrew@dunslane.net 1536 : 30 : appendStringInfoString(&result, " : ");
1537 [ - + ]: 30 : if (val_nulls[i])
4239 andrew@dunslane.net 1538 :UBC 0 : appendStringInfoString(&result, "null");
1539 : : else
406 drowley@postgresql.o 1540 :CBC 30 : escape_json_text(&result,
1541 : 30 : (text *) DatumGetPointer(val_datums[i]));
1542 : : }
1543 : :
4239 andrew@dunslane.net 1544 : 6 : appendStringInfoChar(&result, '}');
1545 : :
1546 : 6 : pfree(key_datums);
1547 : 6 : pfree(key_nulls);
1548 : 6 : pfree(val_datums);
1549 : 6 : pfree(val_nulls);
1550 : :
1551 : 6 : rval = cstring_to_text_with_len(result.data, result.len);
1552 : 6 : pfree(result.data);
1553 : :
1554 : 6 : PG_RETURN_TEXT_P(rval);
1555 : : }
1556 : :
1557 : : /*
1558 : : * escape_json_char
1559 : : * Inline helper function for escape_json* functions
1560 : : */
1561 : : static pg_attribute_always_inline void
406 drowley@postgresql.o 1562 : 2207716 : escape_json_char(StringInfo buf, char c)
1563 : : {
1564 [ + + + + : 2207716 : switch (c)
+ + + + ]
1565 : : {
1566 : 9 : case '\b':
1567 : 9 : appendStringInfoString(buf, "\\b");
1568 : 9 : break;
1569 : 3 : case '\f':
1570 : 3 : appendStringInfoString(buf, "\\f");
1571 : 3 : break;
1572 : 12 : case '\n':
1573 : 12 : appendStringInfoString(buf, "\\n");
1574 : 12 : break;
1575 : 3 : case '\r':
1576 : 3 : appendStringInfoString(buf, "\\r");
1577 : 3 : break;
1578 : 90 : case '\t':
1579 : 90 : appendStringInfoString(buf, "\\t");
1580 : 90 : break;
1581 : 315 : case '"':
1582 : 315 : appendStringInfoString(buf, "\\\"");
1583 : 315 : break;
1584 : 45 : case '\\':
1585 : 45 : appendStringInfoString(buf, "\\\\");
1586 : 45 : break;
1587 : 2207239 : default:
1588 [ + + ]: 2207239 : if ((unsigned char) c < ' ')
1589 : 3 : appendStringInfo(buf, "\\u%04x", (int) c);
1590 : : else
1591 [ + + ]: 2207236 : appendStringInfoCharMacro(buf, c);
1592 : 2207239 : break;
1593 : : }
1594 : 2207716 : }
1595 : :
1596 : : /*
1597 : : * escape_json
1598 : : * Produce a JSON string literal, properly escaping the NUL-terminated
1599 : : * cstring.
1600 : : */
1601 : : void
4964 andrew@dunslane.net 1602 : 19740 : escape_json(StringInfo buf, const char *str)
1603 : : {
406 drowley@postgresql.o 1604 [ + + ]: 19740 : appendStringInfoCharMacro(buf, '"');
1605 : :
1606 [ + + ]: 274626 : for (; *str != '\0'; str++)
1607 : 254886 : escape_json_char(buf, *str);
1608 : :
3546 peter_e@gmx.net 1609 [ + + ]: 19740 : appendStringInfoCharMacro(buf, '"');
406 drowley@postgresql.o 1610 : 19740 : }
1611 : :
1612 : : /*
1613 : : * Define the number of bytes that escape_json_with_len will look ahead in the
1614 : : * input string before flushing the input string to the destination buffer.
1615 : : * Looking ahead too far could result in cachelines being evicted that will
1616 : : * need to be reloaded in order to perform the appendBinaryStringInfo call.
1617 : : * Smaller values will result in a larger number of calls to
1618 : : * appendBinaryStringInfo and introduce additional function call overhead.
1619 : : * Values larger than the size of L1d cache will likely result in worse
1620 : : * performance.
1621 : : */
1622 : : #define ESCAPE_JSON_FLUSH_AFTER 512
1623 : :
1624 : : /*
1625 : : * escape_json_with_len
1626 : : * Produce a JSON string literal, properly escaping the possibly not
1627 : : * NUL-terminated characters in 'str'. 'len' defines the number of bytes
1628 : : * from 'str' to process.
1629 : : */
1630 : : void
1631 : 198025 : escape_json_with_len(StringInfo buf, const char *str, int len)
1632 : : {
1633 : : int vlen;
1634 : :
397 1635 [ - + ]: 198025 : Assert(len >= 0);
1636 : :
1637 : : /*
1638 : : * Since we know the minimum length we'll need to append, let's just
1639 : : * enlarge the buffer now rather than incrementally making more space when
1640 : : * we run out. Add two extra bytes for the enclosing quotes.
1641 : : */
1642 : 198025 : enlargeStringInfo(buf, len + 2);
1643 : :
1644 : : /*
1645 : : * Figure out how many bytes to process using SIMD. Round 'len' down to
1646 : : * the previous multiple of sizeof(Vector8), assuming that's a power-of-2.
1647 : : */
1648 : 198025 : vlen = len & (int) (~(sizeof(Vector8) - 1));
1649 : :
3546 peter_e@gmx.net 1650 [ - + ]: 198025 : appendStringInfoCharMacro(buf, '"');
1651 : :
397 drowley@postgresql.o 1652 : 198025 : for (int i = 0, copypos = 0;;)
1653 : : {
1654 : : /*
1655 : : * To speed this up, try searching sizeof(Vector8) bytes at once for
1656 : : * special characters that we need to escape. When we find one, we
1657 : : * fall out of the Vector8 loop and copy the portion we've vector
1658 : : * searched and then we process sizeof(Vector8) bytes one byte at a
1659 : : * time. Once done, come back and try doing vector searching again.
1660 : : * We'll also process any remaining bytes at the tail end of the
1661 : : * string byte-by-byte. This optimization assumes that most chunks of
1662 : : * sizeof(Vector8) bytes won't contain any special characters.
1663 : : */
1664 [ + + ]: 212562 : for (; i < vlen; i += sizeof(Vector8))
1665 : : {
1666 : : Vector8 chunk;
1667 : :
1668 : 14537 : vector8_load(&chunk, (const uint8 *) &str[i]);
1669 : :
1670 : : /*
1671 : : * Break on anything less than ' ' or if we find a '"' or '\\'.
1672 : : * Those need special handling. That's done in the per-byte loop.
1673 : : */
1674 [ + + + + ]: 29038 : if (vector8_has_le(chunk, (unsigned char) 0x1F) ||
1675 [ + + ]: 28999 : vector8_has(chunk, (unsigned char) '"') ||
1676 : 14498 : vector8_has(chunk, (unsigned char) '\\'))
1677 : : break;
1678 : :
1679 : : #ifdef ESCAPE_JSON_FLUSH_AFTER
1680 : :
1681 : : /*
1682 : : * Flush what's been checked so far out to the destination buffer
1683 : : * every so often to avoid having to re-read cachelines when
1684 : : * escaping large strings.
1685 : : */
1686 [ + + ]: 14480 : if (i - copypos >= ESCAPE_JSON_FLUSH_AFTER)
1687 : : {
1688 : 15 : appendBinaryStringInfo(buf, &str[copypos], i - copypos);
1689 : 15 : copypos = i;
1690 : : }
1691 : : #endif
1692 : : }
1693 : :
1694 : : /*
1695 : : * Write to the destination up to the point that we've vector searched
1696 : : * so far. Do this only when switching into per-byte mode rather than
1697 : : * once every sizeof(Vector8) bytes.
1698 : : */
1699 [ + + ]: 198082 : if (copypos < i)
1700 : : {
1701 : 13261 : appendBinaryStringInfo(buf, &str[copypos], i - copypos);
1702 : 13261 : copypos = i;
1703 : : }
1704 : :
1705 : : /*
1706 : : * Per-byte loop for Vector8s containing special chars and for
1707 : : * processing the tail of the string.
1708 : : */
1709 [ + + ]: 2150912 : for (int b = 0; b < sizeof(Vector8); b++)
1710 : : {
1711 : : /* check if we've finished */
1712 [ + + ]: 2150855 : if (i == len)
1713 : 198025 : goto done;
1714 : :
1715 [ - + ]: 1952830 : Assert(i < len);
1716 : :
1717 : 1952830 : escape_json_char(buf, str[i++]);
1718 : : }
1719 : :
1720 : 57 : copypos = i;
1721 : : /* We're not done yet. Try the vector search again. */
1722 : : }
1723 : :
1724 : 198025 : done:
406 1725 [ - + ]: 198025 : appendStringInfoCharMacro(buf, '"');
1726 : 198025 : }
1727 : :
1728 : : /*
1729 : : * escape_json_text
1730 : : * Append 'txt' onto 'buf' and escape using escape_json_with_len.
1731 : : *
1732 : : * This is more efficient than calling text_to_cstring and appending the
1733 : : * result as that could require an additional palloc and memcpy.
1734 : : */
1735 : : void
1736 : 2415 : escape_json_text(StringInfo buf, const text *txt)
1737 : : {
1738 : : /* must cast away the const, unfortunately */
1739 : 2415 : text *tunpacked = pg_detoast_datum_packed(unconstify(text *, txt));
1740 [ - + - - : 2415 : int len = VARSIZE_ANY_EXHDR(tunpacked);
- - - - +
+ ]
1741 : : char *str;
1742 : :
1743 [ + + ]: 2415 : str = VARDATA_ANY(tunpacked);
1744 : :
1745 : 2415 : escape_json_with_len(buf, str, len);
1746 : :
1747 : : /* pfree any detoasted values */
1748 [ - + ]: 2415 : if (tunpacked != txt)
406 drowley@postgresql.o 1749 :UBC 0 : pfree(tunpacked);
4964 andrew@dunslane.net 1750 :CBC 2415 : }
1751 : :
1752 : : /* Semantic actions for key uniqueness check */
1753 : : static JsonParseErrorType
890 alvherre@alvh.no-ip. 1754 : 90 : json_unique_object_start(void *_state)
1755 : : {
1756 : 90 : JsonUniqueParsingState *state = _state;
1757 : : JsonUniqueStackEntry *entry;
1758 : :
1759 [ - + ]: 90 : if (!state->unique)
890 alvherre@alvh.no-ip. 1760 :UBC 0 : return JSON_SUCCESS;
1761 : :
1762 : : /* push object entry to stack */
890 alvherre@alvh.no-ip. 1763 :CBC 90 : entry = palloc(sizeof(*entry));
1764 : 90 : entry->object_id = state->id_counter++;
1765 : 90 : entry->parent = state->stack;
1766 : 90 : state->stack = entry;
1767 : :
1768 : 90 : return JSON_SUCCESS;
1769 : : }
1770 : :
1771 : : static JsonParseErrorType
1772 : 87 : json_unique_object_end(void *_state)
1773 : : {
1774 : 87 : JsonUniqueParsingState *state = _state;
1775 : : JsonUniqueStackEntry *entry;
1776 : :
1777 [ + + ]: 87 : if (!state->unique)
1778 : 33 : return JSON_SUCCESS;
1779 : :
1780 : 54 : entry = state->stack;
1781 : 54 : state->stack = entry->parent; /* pop object from stack */
1782 : 54 : pfree(entry);
1783 : 54 : return JSON_SUCCESS;
1784 : : }
1785 : :
1786 : : static JsonParseErrorType
1787 : 130 : json_unique_object_field_start(void *_state, char *field, bool isnull)
1788 : : {
1789 : 130 : JsonUniqueParsingState *state = _state;
1790 : : JsonUniqueStackEntry *entry;
1791 : :
1792 [ - + ]: 130 : if (!state->unique)
890 alvherre@alvh.no-ip. 1793 :UBC 0 : return JSON_SUCCESS;
1794 : :
1795 : : /* find key collision in the current object */
890 alvherre@alvh.no-ip. 1796 [ + + ]:CBC 130 : if (json_unique_check_key(&state->check, field, state->stack->object_id))
1797 : 107 : return JSON_SUCCESS;
1798 : :
1799 : 23 : state->unique = false;
1800 : :
1801 : : /* pop all objects entries */
1802 [ + + ]: 56 : while ((entry = state->stack))
1803 : : {
1804 : 33 : state->stack = entry->parent;
1805 : 33 : pfree(entry);
1806 : : }
1807 : 23 : return JSON_SUCCESS;
1808 : : }
1809 : :
1810 : : /* Validate JSON text and additionally check key uniqueness */
1811 : : bool
1812 : 661 : json_validate(text *json, bool check_unique_keys, bool throw_error)
1813 : : {
1814 : : JsonLexContext lex;
1815 : 661 : JsonSemAction uniqueSemAction = {0};
1816 : : JsonUniqueParsingState state;
1817 : : JsonParseErrorType result;
1818 : :
702 1819 : 661 : makeJsonLexContext(&lex, json, check_unique_keys);
1820 : :
890 1821 [ + + ]: 661 : if (check_unique_keys)
1822 : : {
702 1823 : 116 : state.lex = &lex;
890 1824 : 116 : state.stack = NULL;
1825 : 116 : state.id_counter = 0;
1826 : 116 : state.unique = true;
1827 : 116 : json_unique_check_init(&state.check);
1828 : :
1829 : 116 : uniqueSemAction.semstate = &state;
1830 : 116 : uniqueSemAction.object_start = json_unique_object_start;
1831 : 116 : uniqueSemAction.object_field_start = json_unique_object_field_start;
1832 : 116 : uniqueSemAction.object_end = json_unique_object_end;
1833 : : }
1834 : :
702 1835 [ + + ]: 661 : result = pg_parse_json(&lex, check_unique_keys ? &uniqueSemAction : &nullSemAction);
1836 : :
890 1837 [ + + ]: 661 : if (result != JSON_SUCCESS)
1838 : : {
1839 [ - + ]: 108 : if (throw_error)
702 alvherre@alvh.no-ip. 1840 :UBC 0 : json_errsave_error(result, &lex, NULL);
1841 : :
890 alvherre@alvh.no-ip. 1842 :CBC 108 : return false; /* invalid json */
1843 : : }
1844 : :
1845 [ + + + + ]: 553 : if (check_unique_keys && !state.unique)
1846 : : {
1847 [ + + ]: 23 : if (throw_error)
1848 [ + - ]: 4 : ereport(ERROR,
1849 : : (errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1850 : : errmsg("duplicate JSON object key value")));
1851 : :
1852 : 19 : return false; /* not unique keys */
1853 : : }
1854 : :
702 1855 [ + + ]: 530 : if (check_unique_keys)
1856 : 81 : freeJsonLexContext(&lex);
1857 : :
890 1858 : 530 : return true; /* ok */
1859 : : }
1860 : :
1861 : : /*
1862 : : * SQL function json_typeof(json) -> text
1863 : : *
1864 : : * Returns the type of the outermost JSON value as TEXT. Possible types are
1865 : : * "object", "array", "string", "number", "boolean", and "null".
1866 : : *
1867 : : * Performs a single call to json_lex() to get the first token of the supplied
1868 : : * value. This initial token uniquely determines the value's type. As our
1869 : : * input must already have been validated by json_in() or json_recv(), the
1870 : : * initial token should never be JSON_TOKEN_OBJECT_END, JSON_TOKEN_ARRAY_END,
1871 : : * JSON_TOKEN_COLON, JSON_TOKEN_COMMA, or JSON_TOKEN_END.
1872 : : */
1873 : : Datum
4349 andrew@dunslane.net 1874 : 30 : json_typeof(PG_FUNCTION_ARGS)
1875 : : {
890 alvherre@alvh.no-ip. 1876 : 30 : text *json = PG_GETARG_TEXT_PP(0);
1877 : : JsonLexContext lex;
1878 : : char *type;
1879 : : JsonParseErrorType result;
1880 : :
1881 : : /* Lex exactly one token from the input and check its type. */
702 1882 : 30 : makeJsonLexContext(&lex, json, false);
1883 : 30 : result = json_lex(&lex);
1101 andrew@dunslane.net 1884 [ - + ]: 30 : if (result != JSON_SUCCESS)
702 alvherre@alvh.no-ip. 1885 :UBC 0 : json_errsave_error(result, &lex, NULL);
1886 : :
702 alvherre@alvh.no-ip. 1887 [ + + + + :CBC 30 : switch (lex.token_type)
+ + - ]
1888 : : {
4245 andrew@dunslane.net 1889 : 6 : case JSON_TOKEN_OBJECT_START:
1890 : 6 : type = "object";
1891 : 6 : break;
1892 : 6 : case JSON_TOKEN_ARRAY_START:
1893 : 6 : type = "array";
1894 : 6 : break;
1895 : 3 : case JSON_TOKEN_STRING:
1896 : 3 : type = "string";
1897 : 3 : break;
1898 : 6 : case JSON_TOKEN_NUMBER:
1899 : 6 : type = "number";
1900 : 6 : break;
1901 : 6 : case JSON_TOKEN_TRUE:
1902 : : case JSON_TOKEN_FALSE:
1903 : 6 : type = "boolean";
1904 : 6 : break;
1905 : 3 : case JSON_TOKEN_NULL:
1906 : 3 : type = "null";
1907 : 3 : break;
4245 andrew@dunslane.net 1908 :UBC 0 : default:
702 alvherre@alvh.no-ip. 1909 [ # # ]: 0 : elog(ERROR, "unexpected json token: %d", lex.token_type);
1910 : : }
1911 : :
4318 peter_e@gmx.net 1912 :CBC 30 : PG_RETURN_TEXT_P(cstring_to_text(type));
1913 : : }
|