2 SELECT '""'::jsonb; -- OK.
8 SELECT $$''$$::jsonb; -- ERROR, single quotes are not allowed
9 ERROR: invalid input syntax for type json
10 LINE 1: SELECT $$''$$::jsonb;
12 DETAIL: Token "'" is invalid.
13 CONTEXT: JSON data, line 1: '...
14 SELECT '"abc"'::jsonb; -- OK
20 SELECT '"abc'::jsonb; -- ERROR, quotes not closed
21 ERROR: invalid input syntax for type json
22 LINE 1: SELECT '"abc'::jsonb;
24 DETAIL: Token ""abc" is invalid.
25 CONTEXT: JSON data, line 1: "abc
27 def"'::jsonb; -- ERROR, unescaped newline in string constant
28 ERROR: invalid input syntax for type json
31 DETAIL: Character with value 0x0a must be escaped.
32 CONTEXT: JSON data, line 1: "abc
33 SELECT '"\n\"\\"'::jsonb; -- OK, legal escapes
39 SELECT '"\v"'::jsonb; -- ERROR, not a valid JSON escape
40 ERROR: invalid input syntax for type json
41 LINE 1: SELECT '"\v"'::jsonb;
43 DETAIL: Escape sequence "\v" is invalid.
44 CONTEXT: JSON data, line 1: "\v...
45 -- see json_encoding test for input with unicode escapes
47 SELECT '1'::jsonb; -- OK
53 SELECT '0'::jsonb; -- OK
59 SELECT '01'::jsonb; -- ERROR, not valid according to JSON spec
60 ERROR: invalid input syntax for type json
61 LINE 1: SELECT '01'::jsonb;
63 DETAIL: Token "01" is invalid.
64 CONTEXT: JSON data, line 1: 01
65 SELECT '0.1'::jsonb; -- OK
71 SELECT '9223372036854775808'::jsonb; -- OK, even though it's too large for int8
77 SELECT '1e100'::jsonb; -- OK
79 -------------------------------------------------------------------------------------------------------
80 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
83 SELECT '1.3e100'::jsonb; -- OK
85 -------------------------------------------------------------------------------------------------------
86 13000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
89 SELECT '1f2'::jsonb; -- ERROR
90 ERROR: invalid input syntax for type json
91 LINE 1: SELECT '1f2'::jsonb;
93 DETAIL: Token "1f2" is invalid.
94 CONTEXT: JSON data, line 1: 1f2
95 SELECT '0.x1'::jsonb; -- ERROR
96 ERROR: invalid input syntax for type json
97 LINE 1: SELECT '0.x1'::jsonb;
99 DETAIL: Token "0.x1" is invalid.
100 CONTEXT: JSON data, line 1: 0.x1
101 SELECT '1.3ex100'::jsonb; -- ERROR
102 ERROR: invalid input syntax for type json
103 LINE 1: SELECT '1.3ex100'::jsonb;
105 DETAIL: Token "1.3ex100" is invalid.
106 CONTEXT: JSON data, line 1: 1.3ex100
108 SELECT '[]'::jsonb; -- OK
114 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::jsonb; -- OK
116 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
117 [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
120 SELECT '[1,2]'::jsonb; -- OK
126 SELECT '[1,2,]'::jsonb; -- ERROR, trailing comma
127 ERROR: invalid input syntax for type json
128 LINE 1: SELECT '[1,2,]'::jsonb;
130 DETAIL: Expected JSON value, but found "]".
131 CONTEXT: JSON data, line 1: [1,2,]
132 SELECT '[1,2'::jsonb; -- ERROR, no closing bracket
133 ERROR: invalid input syntax for type json
134 LINE 1: SELECT '[1,2'::jsonb;
136 DETAIL: The input string ended unexpectedly.
137 CONTEXT: JSON data, line 1: [1,2
138 SELECT '[1,[2]'::jsonb; -- ERROR, no closing bracket
139 ERROR: invalid input syntax for type json
140 LINE 1: SELECT '[1,[2]'::jsonb;
142 DETAIL: The input string ended unexpectedly.
143 CONTEXT: JSON data, line 1: [1,[2]
145 SELECT '{}'::jsonb; -- OK
151 SELECT '{"abc"}'::jsonb; -- ERROR, no value
152 ERROR: invalid input syntax for type json
153 LINE 1: SELECT '{"abc"}'::jsonb;
155 DETAIL: Expected ":", but found "}".
156 CONTEXT: JSON data, line 1: {"abc"}
157 SELECT '{"abc":1}'::jsonb; -- OK
163 SELECT '{1:"abc"}'::jsonb; -- ERROR, keys must be strings
164 ERROR: invalid input syntax for type json
165 LINE 1: SELECT '{1:"abc"}'::jsonb;
167 DETAIL: Expected string or "}", but found "1".
168 CONTEXT: JSON data, line 1: {1...
169 SELECT '{"abc",1}'::jsonb; -- ERROR, wrong separator
170 ERROR: invalid input syntax for type json
171 LINE 1: SELECT '{"abc",1}'::jsonb;
173 DETAIL: Expected ":", but found ",".
174 CONTEXT: JSON data, line 1: {"abc",...
175 SELECT '{"abc"=1}'::jsonb; -- ERROR, totally wrong separator
176 ERROR: invalid input syntax for type json
177 LINE 1: SELECT '{"abc"=1}'::jsonb;
179 DETAIL: Token "=" is invalid.
180 CONTEXT: JSON data, line 1: {"abc"=...
181 SELECT '{"abc"::1}'::jsonb; -- ERROR, another wrong separator
182 ERROR: invalid input syntax for type json
183 LINE 1: SELECT '{"abc"::1}'::jsonb;
185 DETAIL: Expected JSON value, but found ":".
186 CONTEXT: JSON data, line 1: {"abc"::...
187 SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::jsonb; -- OK
189 --------------------------------------------------------------------
190 {"abc": 1, "def": 2, "ghi": [3, 4], "hij": {"klm": 5, "nop": [6]}}
193 SELECT '{"abc":1:2}'::jsonb; -- ERROR, colon in wrong spot
194 ERROR: invalid input syntax for type json
195 LINE 1: SELECT '{"abc":1:2}'::jsonb;
197 DETAIL: Expected "," or "}", but found ":".
198 CONTEXT: JSON data, line 1: {"abc":1:...
199 SELECT '{"abc":1,3}'::jsonb; -- ERROR, no value
200 ERROR: invalid input syntax for type json
201 LINE 1: SELECT '{"abc":1,3}'::jsonb;
203 DETAIL: Expected string, but found "3".
204 CONTEXT: JSON data, line 1: {"abc":1,3...
206 SET max_stack_depth = '100kB';
207 SELECT repeat('[', 10000)::jsonb;
208 ERROR: stack depth limit exceeded
209 HINT: Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
210 SELECT repeat('{"a":', 10000)::jsonb;
211 ERROR: stack depth limit exceeded
212 HINT: Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
213 RESET max_stack_depth;
214 -- Miscellaneous stuff.
215 SELECT 'true'::jsonb; -- OK
221 SELECT 'false'::jsonb; -- OK
227 SELECT 'null'::jsonb; -- OK
233 SELECT ' true '::jsonb; -- OK, even with extra whitespace
239 SELECT 'true false'::jsonb; -- ERROR, too many values
240 ERROR: invalid input syntax for type json
241 LINE 1: SELECT 'true false'::jsonb;
243 DETAIL: Expected end of input, but found "false".
244 CONTEXT: JSON data, line 1: true false
245 SELECT 'true, false'::jsonb; -- ERROR, too many values
246 ERROR: invalid input syntax for type json
247 LINE 1: SELECT 'true, false'::jsonb;
249 DETAIL: Expected end of input, but found ",".
250 CONTEXT: JSON data, line 1: true,...
251 SELECT 'truf'::jsonb; -- ERROR, not a keyword
252 ERROR: invalid input syntax for type json
253 LINE 1: SELECT 'truf'::jsonb;
255 DETAIL: Token "truf" is invalid.
256 CONTEXT: JSON data, line 1: truf
257 SELECT 'trues'::jsonb; -- ERROR, not a keyword
258 ERROR: invalid input syntax for type json
259 LINE 1: SELECT 'trues'::jsonb;
261 DETAIL: Token "trues" is invalid.
262 CONTEXT: JSON data, line 1: trues
263 SELECT ''::jsonb; -- ERROR, no value
264 ERROR: invalid input syntax for type json
265 LINE 1: SELECT ''::jsonb;
267 DETAIL: The input string ended unexpectedly.
268 CONTEXT: JSON data, line 1:
269 SELECT ' '::jsonb; -- ERROR, no value
270 ERROR: invalid input syntax for type json
271 LINE 1: SELECT ' '::jsonb;
273 DETAIL: The input string ended unexpectedly.
274 CONTEXT: JSON data, line 1:
275 -- Multi-line JSON input to check ERROR reporting
282 -----------------------------------------
283 {"one": 1, "two": "two", "three": true}
288 "two":,"two", -- ERROR extraneous comma before field "two"
291 ERROR: invalid input syntax for type json
294 DETAIL: Expected JSON value, but found ",".
295 CONTEXT: JSON data, line 3: "two":,...
299 "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::jsonb;
300 ERROR: invalid input syntax for type json
303 DETAIL: Expected JSON value, but found "}".
304 CONTEXT: JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":}
305 -- ERROR missing value for last field
306 -- make sure jsonb is passed through json generators without being escaped
307 SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
309 --------------------------
310 [{"a": 1},{"b": [2, 3]}]
314 CREATE TEMP TABLE rows AS
315 SELECT x, 'txt' || x as y
316 FROM generate_series(1,3) AS x;
318 select attname, to_jsonb(histogram_bounds) histogram_bounds
320 where tablename = 'rows' and
321 schemaname = pg_my_temp_schema()::regnamespace::text
323 attname | histogram_bounds
324 ---------+--------------------------
326 y | ["txt1", "txt2", "txt3"]
329 -- to_jsonb, timestamps
330 select to_jsonb(timestamp '2014-05-28 12:22:35.614298');
332 ------------------------------
333 "2014-05-28T12:22:35.614298"
337 SET LOCAL TIME ZONE 10.5;
338 select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
340 ------------------------------------
341 "2014-05-29T02:52:35.614298+10:30"
344 SET LOCAL TIME ZONE -8;
345 select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
347 ------------------------------------
348 "2014-05-28T08:22:35.614298-08:00"
352 select to_jsonb(date '2014-05-28');
358 select to_jsonb(date 'Infinity');
364 select to_jsonb(date '-Infinity');
370 select to_jsonb(timestamp 'Infinity');
376 select to_jsonb(timestamp '-Infinity');
382 select to_jsonb(timestamptz 'Infinity');
388 select to_jsonb(timestamptz '-Infinity');
396 FROM ( SELECT $$a$$ || x AS b, y AS c,
397 ARRAY[ROW(x.*,ARRAY[1,2,3]),
398 ROW(y.*,ARRAY[4,5,6])] AS z
399 FROM generate_series(1,2) x,
400 generate_series(4,5) y) q;
402 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
403 [{"b": "a1", "c": 4, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]}, {"b": "a1", "c": 5, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]}, {"b": "a2", "c": 4, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]}, {"b": "a2", "c": 5, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]}]
406 SELECT jsonb_agg(q ORDER BY x, y)
409 -----------------------------------------------------------------------
410 [{"x": 1, "y": "txt1"}, {"x": 2, "y": "txt2"}, {"x": 3, "y": "txt3"}]
413 UPDATE rows SET x = NULL WHERE x = 1;
414 SELECT jsonb_agg(q ORDER BY x NULLS FIRST, y)
417 --------------------------------------------------------------------------
418 [{"x": null, "y": "txt1"}, {"x": 2, "y": "txt2"}, {"x": 3, "y": "txt3"}]
421 -- jsonb extraction functions
422 CREATE TEMP TABLE test_jsonb (
426 INSERT INTO test_jsonb VALUES
427 ('scalar','"a scalar"'),
428 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
429 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
430 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar';
436 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
442 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
448 SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
454 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
460 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
466 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
472 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
478 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
484 SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
490 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
496 SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
502 SELECT test_json ->> 7 FROM test_jsonb WHERE json_type = 'array';
508 SELECT test_json ->> 'field4' FROM test_jsonb WHERE json_type = 'object';
514 SELECT test_json ->> 'field5' FROM test_jsonb WHERE json_type = 'object';
520 SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
526 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
532 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
538 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
544 SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar';
545 ERROR: cannot call jsonb_object_keys on a scalar
546 SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array';
547 ERROR: cannot call jsonb_object_keys on an array
548 SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'object';
560 SELECT (test_json->'field3') IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'object';
566 SELECT (test_json->>'field3') IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'object';
572 SELECT (test_json->3) IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'array';
578 SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'array';
585 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
591 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
597 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
603 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
609 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
615 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
621 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
627 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
633 select '{"a": "c", "b": null}'::jsonb -> 'b';
639 select '"foo"'::jsonb -> 1;
645 select '"foo"'::jsonb -> 'z';
651 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
657 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
663 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
669 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
675 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
681 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
687 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
693 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
699 select '{"a": "c", "b": null}'::jsonb ->> 'b';
705 select '"foo"'::jsonb ->> 1;
711 select '"foo"'::jsonb ->> 'z';
717 -- equality and inequality
718 SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
724 SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb;
730 SELECT '{"x":"y"}'::jsonb <> '{"x":"y"}'::jsonb;
736 SELECT '{"x":"y"}'::jsonb <> '{"x":"z"}'::jsonb;
743 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
749 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":null}');
755 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "g":null}');
761 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"g":null}');
767 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"c"}');
773 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
779 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":"q"}');
785 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
791 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":null}';
797 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "g":null}';
803 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"g":null}';
809 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"c"}';
815 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
821 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":"q"}';
827 SELECT '[1,2]'::jsonb @> '[1,2,2]'::jsonb;
833 SELECT '[1,1,2]'::jsonb @> '[1,2,2]'::jsonb;
839 SELECT '[[1,2]]'::jsonb @> '[[1,2,2]]'::jsonb;
845 SELECT '[1,2,2]'::jsonb <@ '[1,2]'::jsonb;
851 SELECT '[1,2,2]'::jsonb <@ '[1,1,2]'::jsonb;
857 SELECT '[[1,2,2]]'::jsonb <@ '[[1,2]]'::jsonb;
863 SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
869 SELECT jsonb_contained('{"a":"b", "c":null}', '{"a":"b", "b":1, "c":null}');
875 SELECT jsonb_contained('{"a":"b", "g":null}', '{"a":"b", "b":1, "c":null}');
881 SELECT jsonb_contained('{"g":null}', '{"a":"b", "b":1, "c":null}');
887 SELECT jsonb_contained('{"a":"c"}', '{"a":"b", "b":1, "c":null}');
893 SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
899 SELECT jsonb_contained('{"a":"b", "c":"q"}', '{"a":"b", "b":1, "c":null}');
905 SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
911 SELECT '{"a":"b", "c":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
917 SELECT '{"a":"b", "g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
923 SELECT '{"g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
929 SELECT '{"a":"c"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
935 SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
941 SELECT '{"a":"b", "c":"q"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
947 -- Raw scalar may contain another raw scalar, array may contain a raw scalar
948 SELECT '[5]'::jsonb @> '[5]';
954 SELECT '5'::jsonb @> '5';
960 SELECT '[5]'::jsonb @> '5';
966 -- But a raw scalar cannot contain an array
967 SELECT '5'::jsonb @> '[5]';
973 -- In general, one thing should always contain itself. Test array containment:
974 SELECT '["9", ["7", "3"], 1]'::jsonb @> '["9", ["7", "3"], 1]'::jsonb;
980 SELECT '["9", ["7", "3"], ["1"]]'::jsonb @> '["9", ["7", "3"], ["1"]]'::jsonb;
986 -- array containment string matching confusion bug
987 SELECT '{ "name": "Bob", "tags": [ "enim", "qui"]}'::jsonb @> '{"tags":["qu"]}';
994 SELECT jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
1000 SELECT jsonb_array_length('[]');
1002 --------------------
1006 SELECT jsonb_array_length('{"f1":1,"f2":[5,6]}');
1007 ERROR: cannot get array length of a non-array
1008 SELECT jsonb_array_length('4');
1009 ERROR: cannot get array length of a scalar
1011 SELECT jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
1013 --------------------
1019 SELECT jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
1021 ------------------------------------------------------
1023 (a,"{""1"": ""first"", ""b"": ""c"", ""c"": ""b""}")
1029 SELECT * FROM jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
1039 SELECT * FROM jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
1041 -----+------------------------------------
1043 a | {"1": "first", "b": "c", "c": "b"}
1049 SELECT jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
1051 --------------------
1058 SELECT jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
1060 ------------------------------------------------------
1062 (a,"{""1"": ""first"", ""b"": ""c"", ""c"": ""b""}")
1068 SELECT * FROM jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
1078 SELECT * FROM jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
1080 -----+------------------------------------
1082 a | {"1": "first", "b": "c", "c": "b"}
1089 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'a');
1095 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'b');
1101 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'c');
1107 SELECT jsonb_exists('{"a":"null", "b":"qq"}', 'a');
1113 SELECT jsonb '{"a":null, "b":"qq"}' ? 'a';
1119 SELECT jsonb '{"a":null, "b":"qq"}' ? 'b';
1125 SELECT jsonb '{"a":null, "b":"qq"}' ? 'c';
1131 SELECT jsonb '{"a":"null", "b":"qq"}' ? 'a';
1137 -- array exists - array elements should behave as keys
1138 SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
1144 -- type sensitive array exists - should return no rows (since "exists" only
1145 -- matches strings that are either object keys or array elements)
1146 SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
1152 -- However, a raw scalar is *contained* within the array
1153 SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
1159 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['a','b']);
1165 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['b','a']);
1171 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','a']);
1177 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','d']);
1183 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', '{}'::text[]);
1189 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['a','b'];
1195 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['b','a'];
1201 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','a'];
1207 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','d'];
1213 SELECT jsonb '{"a":null, "b":"qq"}' ?| '{}'::text[];
1219 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['a','b']);
1225 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['b','a']);
1231 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','a']);
1237 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','d']);
1243 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', '{}'::text[]);
1249 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','b'];
1255 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['b','a'];
1261 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','a'];
1267 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','d'];
1273 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','a', 'b', 'b', 'b'];
1279 SELECT jsonb '{"a":null, "b":"qq"}' ?& '{}'::text[];
1286 SELECT jsonb_typeof('{}') AS object;
1292 SELECT jsonb_typeof('{"c":3,"p":"o"}') AS object;
1298 SELECT jsonb_typeof('[]') AS array;
1304 SELECT jsonb_typeof('["a", 1]') AS array;
1310 SELECT jsonb_typeof('null') AS "null";
1316 SELECT jsonb_typeof('1') AS number;
1322 SELECT jsonb_typeof('-1') AS number;
1328 SELECT jsonb_typeof('1.0') AS number;
1334 SELECT jsonb_typeof('1e2') AS number;
1340 SELECT jsonb_typeof('-1.0') AS number;
1346 SELECT jsonb_typeof('true') AS boolean;
1352 SELECT jsonb_typeof('false') AS boolean;
1358 SELECT jsonb_typeof('"hello"') AS string;
1364 SELECT jsonb_typeof('"true"') AS string;
1370 SELECT jsonb_typeof('"1.0"') AS string;
1376 -- jsonb_build_array, jsonb_build_object, jsonb_object_agg
1377 SELECT jsonb_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1379 -------------------------------------------------------------------------
1380 ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1, 2, 3]}]
1383 SELECT jsonb_build_array('a', NULL); -- ok
1389 SELECT jsonb_build_array(VARIADIC NULL::text[]); -- ok
1395 SELECT jsonb_build_array(VARIADIC '{}'::text[]); -- ok
1401 SELECT jsonb_build_array(VARIADIC '{a,b,c}'::text[]); -- ok
1407 SELECT jsonb_build_array(VARIADIC ARRAY['a', NULL]::text[]); -- ok
1413 SELECT jsonb_build_array(VARIADIC '{1,2,3,4}'::text[]); -- ok
1415 ----------------------
1416 ["1", "2", "3", "4"]
1419 SELECT jsonb_build_array(VARIADIC '{1,2,3,4}'::int[]); -- ok
1425 SELECT jsonb_build_array(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
1427 --------------------
1431 SELECT jsonb_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1433 -------------------------------------------------------------------------
1434 {"a": 1, "b": 1.2, "c": true, "d": null, "e": {"x": 3, "y": [1, 2, 3]}}
1437 SELECT jsonb_build_object(
1438 'a', jsonb_build_object('b',false,'c',99),
1439 'd', jsonb_build_object('e',array[9,8,7]::int[],
1440 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1442 ------------------------------------------------------------------------------------------------
1443 {"a": {"b": false, "c": 99}, "d": {"e": [9, 8, 7], "f": {"name": "pg_class", "relkind": "r"}}}
1446 SELECT jsonb_build_object('{a,b,c}'::text[]); -- error
1447 ERROR: argument list must have even number of elements
1448 HINT: The arguments of jsonb_build_object() must consist of alternating keys and values.
1449 SELECT jsonb_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array
1450 ERROR: key value must be scalar, not array, composite, or json
1451 SELECT jsonb_build_object('a', 'b', 'c'); -- error
1452 ERROR: argument list must have even number of elements
1453 HINT: The arguments of jsonb_build_object() must consist of alternating keys and values.
1454 SELECT jsonb_build_object(NULL, 'a'); -- error, key cannot be NULL
1455 ERROR: argument 1: key must not be null
1456 SELECT jsonb_build_object('a', NULL); -- ok
1458 --------------------
1462 SELECT jsonb_build_object(VARIADIC NULL::text[]); -- ok
1464 --------------------
1468 SELECT jsonb_build_object(VARIADIC '{}'::text[]); -- ok
1470 --------------------
1474 SELECT jsonb_build_object(VARIADIC '{a,b,c}'::text[]); -- error
1475 ERROR: argument list must have even number of elements
1476 HINT: The arguments of jsonb_build_object() must consist of alternating keys and values.
1477 SELECT jsonb_build_object(VARIADIC ARRAY['a', NULL]::text[]); -- ok
1479 --------------------
1483 SELECT jsonb_build_object(VARIADIC ARRAY[NULL, 'a']::text[]); -- error, key cannot be NULL
1484 ERROR: argument 1: key must not be null
1485 SELECT jsonb_build_object(VARIADIC '{1,2,3,4}'::text[]); -- ok
1487 ----------------------
1488 {"1": "2", "3": "4"}
1491 SELECT jsonb_build_object(VARIADIC '{1,2,3,4}'::int[]); -- ok
1493 --------------------
1497 SELECT jsonb_build_object(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
1499 --------------------------
1500 {"1": 4, "2": 5, "3": 6}
1503 -- empty objects/arrays
1504 SELECT jsonb_build_array();
1510 SELECT jsonb_build_object();
1512 --------------------
1516 -- make sure keys are quoted
1517 SELECT jsonb_build_object(1,2);
1519 --------------------
1523 -- keys must be scalar and not null
1524 SELECT jsonb_build_object(null,2);
1525 ERROR: argument 1: key must not be null
1526 SELECT jsonb_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1527 ERROR: key value must be scalar, not array, composite, or json
1528 SELECT jsonb_build_object(json '{"a":1,"b":2}', 3);
1529 ERROR: key value must be scalar, not array, composite, or json
1530 SELECT jsonb_build_object('{1,2,3}'::int[], 3);
1531 ERROR: key value must be scalar, not array, composite, or json
1532 -- handling of NULL values
1533 SELECT jsonb_object_agg(1, NULL::jsonb);
1539 SELECT jsonb_object_agg(NULL, '{"a":1}');
1540 ERROR: field name must not be null
1541 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1542 INSERT INTO foo VALUES (847001,'t15','GE1043');
1543 INSERT INTO foo VALUES (847002,'t16','GE1043');
1544 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1545 SELECT jsonb_build_object('turbines',jsonb_object_agg(serial_num,jsonb_build_object('name',name,'type',type)))
1548 -------------------------------------------------------------------------------------------------------------------------------------------------------------
1549 {"turbines": {"847001": {"name": "t15", "type": "GE1043"}, "847002": {"name": "t16", "type": "GE1043"}, "847003": {"name": "sub-alpha", "type": "GESS90"}}}
1552 SELECT jsonb_object_agg(name, type) FROM foo;
1554 -----------------------------------------------------------
1555 {"t15": "GE1043", "t16": "GE1043", "sub-alpha": "GESS90"}
1558 INSERT INTO foo VALUES (999999, NULL, 'bar');
1559 SELECT jsonb_object_agg(name, type) FROM foo;
1560 ERROR: field name must not be null
1562 -- empty object, one dimension
1563 SELECT jsonb_object('{}');
1569 -- empty object, two dimensions
1570 SELECT jsonb_object('{}', '{}');
1577 SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1579 ---------------------------------------------------
1580 {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
1583 -- same but with two dimensions
1584 SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1586 ---------------------------------------------------
1587 {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
1591 SELECT jsonb_object('{a,b,c}');
1592 ERROR: array must have even number of elements
1594 SELECT jsonb_object('{{a},{b}}');
1595 ERROR: array must have two columns
1596 -- too many columns error
1597 SELECT jsonb_object('{{a,b,c},{b,c,d}}');
1598 ERROR: array must have two columns
1599 -- too many dimensions error
1600 SELECT jsonb_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1601 ERROR: wrong number of array subscripts
1602 --two argument form of jsonb_object
1603 select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1605 --------------------------------------------------
1606 {"a": "1", "b": "2", "c": "3", "d e f": "a b c"}
1609 -- too many dimensions
1610 SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}', '{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1611 ERROR: wrong number of array subscripts
1612 -- mismatched dimensions
1613 select jsonb_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1614 ERROR: mismatched array dimensions
1615 select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1616 ERROR: mismatched array dimensions
1618 select jsonb_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1619 ERROR: null value not allowed for object key
1620 -- empty key is allowed
1621 select jsonb_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1623 -------------------------------------------------
1624 {"": "3", "a": "1", "b": "2", "d e f": "a b c"}
1627 -- extract_path, extract_path_as_text
1628 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
1630 --------------------
1634 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
1636 --------------------
1640 SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
1642 --------------------
1646 SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
1648 --------------------
1652 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
1653 jsonb_extract_path_text
1654 -------------------------
1658 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
1659 jsonb_extract_path_text
1660 -------------------------
1664 SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
1665 jsonb_extract_path_text
1666 -------------------------
1670 SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
1671 jsonb_extract_path_text
1672 -------------------------
1676 -- extract_path nulls
1677 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_false;
1683 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_true;
1689 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_false;
1695 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_true;
1701 -- extract_path operators
1702 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f4','f6'];
1708 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2'];
1714 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','0'];
1720 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','1'];
1726 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f4','f6'];
1732 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2'];
1738 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','0'];
1744 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
1750 -- corner cases for same
1751 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
1753 ----------------------------
1754 {"a": {"b": {"c": "foo"}}}
1757 select '[1,2,3]'::jsonb #> '{}';
1763 select '"foo"'::jsonb #> '{}';
1769 select '42'::jsonb #> '{}';
1775 select 'null'::jsonb #> '{}';
1781 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
1783 ---------------------
1787 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
1793 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
1799 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
1805 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c'];
1811 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c','d'];
1817 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','z','c'];
1823 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','1','b'];
1829 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','z','b'];
1835 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['1','b'];
1841 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
1847 select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
1853 select '"foo"'::jsonb #> array['z'];
1859 select '42'::jsonb #> array['f2'];
1865 select '42'::jsonb #> array['0'];
1871 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
1873 ----------------------------
1874 {"a": {"b": {"c": "foo"}}}
1877 select '[1,2,3]'::jsonb #>> '{}';
1883 select '"foo"'::jsonb #>> '{}';
1889 select '42'::jsonb #>> '{}';
1895 select 'null'::jsonb #>> '{}';
1901 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
1903 ---------------------
1907 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
1913 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
1919 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
1925 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c'];
1931 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c','d'];
1937 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','z','c'];
1943 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','1','b'];
1949 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','z','b'];
1955 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['1','b'];
1961 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
1967 select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
1973 select '"foo"'::jsonb #>> array['z'];
1979 select '42'::jsonb #>> array['f2'];
1985 select '42'::jsonb #>> array['0'];
1992 SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
1993 jsonb_array_elements
1994 ----------------------------
1999 {"f1": 1, "f2": [7, 8, 9]}
2003 SELECT * FROM jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]') q;
2005 ----------------------------
2010 {"f1": 1, "f2": [7, 8, 9]}
2014 SELECT jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
2015 jsonb_array_elements_text
2016 ----------------------------
2021 {"f1": 1, "f2": [7, 8, 9]}
2026 SELECT * FROM jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
2028 ----------------------------
2033 {"f1": 1, "f2": [7, 8, 9]}
2039 CREATE TYPE jbpop AS (a text, b int, c timestamp);
2040 CREATE DOMAIN jsb_int_not_null AS int NOT NULL;
2041 CREATE DOMAIN jsb_int_array_1d AS int[] CHECK(array_length(VALUE, 1) = 3);
2042 CREATE DOMAIN jsb_int_array_2d AS int[][] CHECK(array_length(VALUE, 2) = 3);
2043 create type jb_unordered_pair as (x int, y int);
2044 create domain jb_ordered_pair as jb_unordered_pair check((value).x <= (value).y);
2045 CREATE TYPE jsbrec AS (
2051 ia1d jsb_int_array_1d,
2052 ia2d jsb_int_array_2d,
2064 CREATE TYPE jsbrec_i_not_null AS (
2067 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
2073 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
2075 --------+---+--------------------------
2076 blurfl | 3 | Mon Dec 31 15:30:56 2012
2079 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
2085 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
2087 --------+---+--------------------------
2088 blurfl | 3 | Mon Dec 31 15:30:56 2012
2091 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":[100,200,false],"x":43.2}') q;
2093 -------------------+---+---
2094 [100, 200, false] | |
2097 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":[100,200,false],"x":43.2}') q;
2099 -------------------+---+--------------------------
2100 [100, 200, false] | 3 | Mon Dec 31 15:30:56 2012
2103 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"c":[100,200,false],"x":43.2}') q;
2104 ERROR: invalid input syntax for type timestamp: "[100, 200, false]"
2105 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop, '{}') q;
2107 ---+---+--------------------------
2108 x | 3 | Mon Dec 31 15:30:56 2012
2111 SELECT i FROM jsonb_populate_record(NULL::jsbrec_i_not_null, '{"x": 43.2}') q;
2112 ERROR: domain jsb_int_not_null does not allow null values
2113 SELECT i FROM jsonb_populate_record(NULL::jsbrec_i_not_null, '{"i": null}') q;
2114 ERROR: domain jsb_int_not_null does not allow null values
2115 SELECT i FROM jsonb_populate_record(NULL::jsbrec_i_not_null, '{"i": 12345}') q;
2121 SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": null}') q;
2127 SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": 123}') q;
2128 ERROR: expected JSON array
2129 HINT: See the value of key "ia".
2130 SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [1, "2", null, 4]}') q;
2136 SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [[1, 2], [3, 4]]}') q;
2142 SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [[1], 2]}') q;
2143 ERROR: expected JSON array
2144 HINT: See the array element [1] of key "ia".
2145 SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [[1], [2, 3]]}') q;
2146 ERROR: malformed JSON array
2147 DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
2148 SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": "{1,2,3}"}') q;
2154 SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": null}') q;
2160 SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": 123}') q;
2161 ERROR: expected JSON array
2162 HINT: See the value of key "ia1".
2163 SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": [1, "2", null, 4]}') q;
2169 SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": [[1, 2, 3]]}') q;
2175 SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": null}') q;
2181 SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": 123}') q;
2182 ERROR: expected JSON array
2183 HINT: See the value of key "ia1d".
2184 SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": [1, "2", null, 4]}') q;
2185 ERROR: value for domain jsb_int_array_1d violates check constraint "jsb_int_array_1d_check"
2186 SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": [1, "2", null]}') q;
2192 SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [1, "2", null, 4]}') q;
2198 SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[1, 2], [null, 4]]}') q;
2204 SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[], []]}') q;
2210 SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[1, 2], [3]]}') q;
2211 ERROR: malformed JSON array
2212 DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
2213 SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[1, 2], 3, 4]}') q;
2214 ERROR: expected JSON array
2215 HINT: See the array element [1] of key "ia2".
2216 SELECT ia2d FROM jsonb_populate_record(NULL::jsbrec, '{"ia2d": [[1, "2"], [null, 4]]}') q;
2217 ERROR: value for domain jsb_int_array_2d violates check constraint "jsb_int_array_2d_check"
2218 SELECT ia2d FROM jsonb_populate_record(NULL::jsbrec, '{"ia2d": [[1, "2", 3], [null, 5, 6]]}') q;
2220 ----------------------
2221 {{1,2,3},{NULL,5,6}}
2224 SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [1, "2", null, 4]}') q;
2230 SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [[1, 2], [null, 4]]}') q;
2236 SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[], []], [[], []], [[], []] ]}') q;
2242 SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[1, 2]], [[3, 4]] ]}') q;
2248 SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]}') q;
2250 -------------------------------
2251 {{{1,2},{3,4}},{{5,6},{7,8}}}
2254 SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]] ]}') q;
2255 ERROR: malformed JSON array
2256 DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
2257 SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": null}') q;
2263 SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": 123}') q;
2264 ERROR: expected JSON array
2265 HINT: See the value of key "ta".
2266 SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": [1, "2", null, 4]}') q;
2272 SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q;
2273 ERROR: expected JSON array
2274 HINT: See the array element [1] of key "ta".
2275 SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": null}') q;
2281 SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": "aaa"}') q;
2287 SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": "aaaaaaaaaa"}') q;
2293 SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": "aaaaaaaaaaaaa"}') q;
2294 ERROR: value too long for type character(10)
2295 SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": null}') q;
2301 SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": 123}') q;
2302 ERROR: expected JSON array
2303 HINT: See the value of key "ca".
2304 SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": [1, "2", null, 4]}') q;
2306 -----------------------------------------------
2307 {"1 ","2 ",NULL,"4 "}
2310 SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q;
2311 ERROR: value too long for type character(10)
2312 SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q;
2313 ERROR: expected JSON array
2314 HINT: See the array element [1] of key "ca".
2315 SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": null}') q;
2321 SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": true}') q;
2327 SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": 123.45}') q;
2333 SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": "123.45"}') q;
2339 SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": "abc"}') q;
2345 SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": [123, "123", null, {"key": "value"}]}') q;
2347 --------------------------------------
2348 [123, "123", null, {"key": "value"}]
2351 SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q;
2353 --------------------------------------
2354 {"a": "bbb", "b": null, "c": 123.45}
2357 SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": null}') q;
2363 SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": true}') q;
2369 SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": 123.45}') q;
2375 SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": "123.45"}') q;
2381 SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": "abc"}') q;
2387 SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q;
2389 --------------------------------------
2390 [123, "123", null, {"key": "value"}]
2393 SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q;
2395 --------------------------------------
2396 {"a": "bbb", "b": null, "c": 123.45}
2399 SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": null}') q;
2405 SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": 123}') q;
2406 ERROR: expected JSON array
2407 HINT: See the value of key "jsa".
2408 SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": [1, "2", null, 4]}') q;
2410 --------------------
2414 SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": ["aaa", null, [1, 2, "3", {}], { "k" : "v" }]}') q;
2416 -------------------------------------------------------
2417 {"\"aaa\"",NULL,"[1, 2, \"3\", {}]","{\"k\": \"v\"}"}
2420 SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": 123}') q;
2421 ERROR: cannot call populate_composite on a scalar
2422 SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": [1, 2]}') q;
2423 ERROR: cannot call populate_composite on an array
2424 SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
2426 -----------------------------------
2427 (abc,,"Thu Jan 02 00:00:00 2003")
2430 SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": "(abc,42,01.02.2003)"}') q;
2432 -------------------------------------
2433 (abc,42,"Thu Jan 02 00:00:00 2003")
2436 SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": 123}') q;
2437 ERROR: expected JSON array
2438 HINT: See the value of key "reca".
2439 SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [1, 2]}') q;
2440 ERROR: cannot call populate_composite on a scalar
2441 SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
2443 --------------------------------------------------------
2444 {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
2447 SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": ["(abc,42,01.02.2003)"]}') q;
2449 -------------------------------------------
2450 {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
2453 SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": "{\"(abc,42,01.02.2003)\"}"}') q;
2455 -------------------------------------------
2456 {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
2459 SELECT rec FROM jsonb_populate_record(
2460 row(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
2461 row('x',3,'2012-12-31 15:30:56')::jbpop,NULL)::jsbrec,
2462 '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}'
2465 ------------------------------------
2466 (abc,3,"Thu Jan 02 00:00:00 2003")
2469 -- anonymous record type
2470 SELECT jsonb_populate_record(null::record, '{"x": 0, "y": 1}');
2471 ERROR: could not determine row type for result of jsonb_populate_record
2472 HINT: Provide a non-null record argument, or call the function in the FROM clause using a column definition list.
2473 SELECT jsonb_populate_record(row(1,2), '{"f1": 0, "f2": 1}');
2474 jsonb_populate_record
2475 -----------------------
2480 jsonb_populate_record(null::record, '{"x": 776}') AS (x int, y int);
2487 SELECT jsonb_populate_record(null::jb_ordered_pair, '{"x": 0, "y": 1}');
2488 jsonb_populate_record
2489 -----------------------
2493 SELECT jsonb_populate_record(row(1,2)::jb_ordered_pair, '{"x": 0}');
2494 jsonb_populate_record
2495 -----------------------
2499 SELECT jsonb_populate_record(row(1,2)::jb_ordered_pair, '{"x": 1, "y": 0}');
2500 ERROR: value for domain jb_ordered_pair violates check constraint "jb_ordered_pair_check"
2501 -- populate_recordset
2502 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
2504 --------+---+--------------------------
2506 | 3 | Fri Jan 20 10:42:53 2012
2509 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
2511 --------+----+--------------------------
2513 def | 3 | Fri Jan 20 10:42:53 2012
2516 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
2518 --------+---+--------------------------
2520 | 3 | Fri Jan 20 10:42:53 2012
2523 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
2525 --------+----+--------------------------
2527 def | 3 | Fri Jan 20 10:42:53 2012
2530 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
2532 -----------------+----+--------------------------
2533 [100, 200, 300] | 99 |
2534 {"z": true} | 3 | Fri Jan 20 10:42:53 2012
2537 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
2538 ERROR: invalid input syntax for type timestamp: "[100, 200, 300]"
2539 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
2541 --------+---+--------------------------
2543 | 3 | Fri Jan 20 10:42:53 2012
2546 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
2548 --------+----+--------------------------
2550 def | 3 | Fri Jan 20 10:42:53 2012
2553 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
2555 -----------------+----+--------------------------
2556 [100, 200, 300] | 99 |
2557 {"z": true} | 3 | Fri Jan 20 10:42:53 2012
2560 -- anonymous record type
2561 SELECT jsonb_populate_recordset(null::record, '[{"x": 0, "y": 1}]');
2562 ERROR: could not determine row type for result of jsonb_populate_recordset
2563 HINT: Provide a non-null record argument, or call the function in the FROM clause using a column definition list.
2564 SELECT jsonb_populate_recordset(row(1,2), '[{"f1": 0, "f2": 1}]');
2565 jsonb_populate_recordset
2566 --------------------------
2570 SELECT i, jsonb_populate_recordset(row(i,50), '[{"f1":"42"},{"f2":"43"}]')
2571 FROM (VALUES (1),(2)) v(i);
2572 i | jsonb_populate_recordset
2573 ---+--------------------------
2581 jsonb_populate_recordset(null::record, '[{"x": 776}]') AS (x int, y int);
2587 -- empty array is a corner case
2588 SELECT jsonb_populate_recordset(null::record, '[]');
2589 ERROR: could not determine row type for result of jsonb_populate_recordset
2590 HINT: Provide a non-null record argument, or call the function in the FROM clause using a column definition list.
2591 SELECT jsonb_populate_recordset(row(1,2), '[]');
2592 jsonb_populate_recordset
2593 --------------------------
2596 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[]') q;
2602 jsonb_populate_recordset(null::record, '[]') AS (x int, y int);
2608 SELECT jsonb_populate_recordset(null::jb_ordered_pair, '[{"x": 0, "y": 1}]');
2609 jsonb_populate_recordset
2610 --------------------------
2614 SELECT jsonb_populate_recordset(row(1,2)::jb_ordered_pair, '[{"x": 0}, {"y": 3}]');
2615 jsonb_populate_recordset
2616 --------------------------
2621 SELECT jsonb_populate_recordset(row(1,2)::jb_ordered_pair, '[{"x": 1, "y": 0}]');
2622 ERROR: value for domain jb_ordered_pair violates check constraint "jb_ordered_pair_check"
2623 -- negative cases where the wrong record type is supplied
2624 select * from jsonb_populate_recordset(row(0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
2625 ERROR: function return row and query-specified return row do not match
2626 DETAIL: Returned row contains 1 attribute, but query expects 2.
2627 select * from jsonb_populate_recordset(row(0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
2628 ERROR: function return row and query-specified return row do not match
2629 DETAIL: Returned type integer at ordinal position 1, but query expects text.
2630 select * from jsonb_populate_recordset(row(0::int,0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
2631 ERROR: function return row and query-specified return row do not match
2632 DETAIL: Returned row contains 3 attributes, but query expects 2.
2633 select * from jsonb_populate_recordset(row(1000000000::int,50::int),'[{"b":"2"},{"a":"3"}]') q (a text, b text);
2634 ERROR: function return row and query-specified return row do not match
2635 DETAIL: Returned type integer at ordinal position 1, but query expects text.
2636 -- jsonb_to_record and jsonb_to_recordset
2637 select * from jsonb_to_record('{"a":1,"b":"foo","c":"bar"}')
2638 as x(a int, b text, d text);
2644 select * from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
2645 as x(a int, b text, c boolean);
2652 select *, c is null as c_is_null
2653 from jsonb_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8, "ca": ["1 2", 3], "ia": [[1,2],[3,4]], "r": {"a": "aaa", "b": 123}}'::jsonb)
2654 as t(a int, b jsonb, c text, x int, ca char(5)[], ia int[][], r jbpop);
2655 a | b | c | x | ca | ia | r | c_is_null
2656 ---+-------------------+---+---+-------------------+---------------+------------+-----------
2657 1 | {"c": 16, "d": 2} | | 8 | {"1 2 ","3 "} | {{1,2},{3,4}} | (aaa,123,) | t
2660 select *, c is null as c_is_null
2661 from jsonb_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::jsonb)
2662 as t(a int, b jsonb, c text, x int);
2663 a | b | c | x | c_is_null
2664 ---+-------------------+---+---+-----------
2665 1 | {"c": 16, "d": 2} | | 8 | t
2668 select * from jsonb_to_record('{"ia": null}') as x(ia _int4);
2674 select * from jsonb_to_record('{"ia": 123}') as x(ia _int4);
2675 ERROR: expected JSON array
2676 HINT: See the value of key "ia".
2677 select * from jsonb_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4);
2683 select * from jsonb_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4);
2689 select * from jsonb_to_record('{"ia": [[1], 2]}') as x(ia _int4);
2690 ERROR: expected JSON array
2691 HINT: See the array element [1] of key "ia".
2692 select * from jsonb_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4);
2693 ERROR: malformed JSON array
2694 DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
2695 select * from jsonb_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]);
2701 select * from jsonb_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]);
2707 select * from jsonb_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]);
2713 select * from jsonb_to_record('{"out": {"key": 1}}') as x(out json);
2719 select * from jsonb_to_record('{"out": [{"key": 1}]}') as x(out json);
2725 select * from jsonb_to_record('{"out": "{\"key\": 1}"}') as x(out json);
2731 select * from jsonb_to_record('{"out": {"key": 1}}') as x(out jsonb);
2737 select * from jsonb_to_record('{"out": [{"key": 1}]}') as x(out jsonb);
2743 select * from jsonb_to_record('{"out": "{\"key\": 1}"}') as x(out jsonb);
2749 -- test type info caching in jsonb_populate_record()
2750 CREATE TEMP TABLE jsbpoptest (js jsonb);
2751 INSERT INTO jsbpoptest
2753 "jsa": [1, "2", null, 4],
2754 "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
2755 "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
2757 FROM generate_series(1, 3);
2758 SELECT (jsonb_populate_record(NULL::jsbrec, js)).* FROM jsbpoptest;
2759 i | ia | ia1 | ia2 | ia3 | ia1d | ia2d | t | ta | c | ca | ts | js | jsb | jsa | rec | reca
2760 ---+----+-----+-----+-----+------+------+---+----+---+----+----+----+-----+--------------------+-----------------------------------+--------------------------------------------------------
2761 | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
2762 | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
2763 | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
2767 DROP TYPE jsbrec_i_not_null;
2768 DROP DOMAIN jsb_int_not_null;
2769 DROP DOMAIN jsb_int_array_1d;
2770 DROP DOMAIN jsb_int_array_2d;
2771 DROP DOMAIN jb_ordered_pair;
2772 DROP TYPE jb_unordered_pair;
2774 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
2780 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
2786 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
2792 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
2798 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
2804 SELECT count(*) FROM testjsonb WHERE j ? 'public';
2810 SELECT count(*) FROM testjsonb WHERE j ? 'bar';
2816 SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
2822 SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
2828 SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
2834 SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
2840 SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
2846 SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
2852 SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
2858 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
2864 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
2870 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
2876 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
2882 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
2888 SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
2894 SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
2900 SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
2906 SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
2912 SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
2918 SELECT count(*) FROM testjsonb WHERE j @? '$';
2924 SELECT count(*) FROM testjsonb WHERE j @? '$.public';
2930 SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
2936 CREATE INDEX jidx ON testjsonb USING gin (j);
2937 SET enable_seqscan = off;
2938 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
2944 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
2950 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
2956 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
2962 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
2968 SELECT count(*) FROM testjsonb WHERE j @> '{"array":["foo"]}';
2974 SELECT count(*) FROM testjsonb WHERE j @> '{"array":["bar"]}';
2980 -- exercise GIN_SEARCH_MODE_ALL
2981 SELECT count(*) FROM testjsonb WHERE j @> '{}';
2987 SELECT count(*) FROM testjsonb WHERE j ? 'public';
2993 SELECT count(*) FROM testjsonb WHERE j ? 'bar';
2999 SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
3005 SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
3012 SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
3014 -----------------------------------------------------------------
3016 -> Bitmap Heap Scan on testjsonb
3017 Recheck Cond: (j @@ '($."wait" == null)'::jsonpath)
3018 -> Bitmap Index Scan on jidx
3019 Index Cond: (j @@ '($."wait" == null)'::jsonpath)
3022 SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
3028 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
3034 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
3040 SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
3046 SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
3052 SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
3058 SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
3064 SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
3070 SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
3076 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
3082 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
3088 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
3094 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
3100 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
3106 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
3112 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
3118 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
3125 SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
3127 -------------------------------------------------------------------
3129 -> Bitmap Heap Scan on testjsonb
3130 Recheck Cond: (j @? '$."wait"?(@ == null)'::jsonpath)
3131 -> Bitmap Index Scan on jidx
3132 Index Cond: (j @? '$."wait"?(@ == null)'::jsonpath)
3135 SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
3141 SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
3147 SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
3153 SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
3159 SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
3165 SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
3171 SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
3177 SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
3183 SELECT count(*) FROM testjsonb WHERE j @? '$';
3189 SELECT count(*) FROM testjsonb WHERE j @? '$.public';
3195 SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
3201 -- array exists - array elements should behave as keys (for GIN index scans too)
3202 CREATE INDEX jidx_array ON testjsonb USING gin((j->'array'));
3203 SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
3209 -- type sensitive array exists - should return no rows (since "exists" only
3210 -- matches strings that are either object keys or array elements)
3211 SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
3217 -- However, a raw scalar is *contained* within the array
3218 SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
3224 RESET enable_seqscan;
3225 SELECT count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow;
3231 SELECT key, count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow GROUP BY key ORDER BY count DESC, key;
3263 SELECT count(distinct j) FROM testjsonb;
3269 SET enable_hashagg = off;
3270 SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
3276 SET enable_hashagg = on;
3277 SET enable_sort = off;
3278 SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
3284 SELECT distinct * FROM (values (jsonb '{}' || ''::text),('{}')) v(j);
3290 SET enable_sort = on;
3291 RESET enable_hashagg;
3294 DROP INDEX jidx_array;
3296 CREATE INDEX jidx ON testjsonb USING btree (j);
3297 SET enable_seqscan = off;
3298 SELECT count(*) FROM testjsonb WHERE j > '{"p":1}';
3304 SELECT count(*) FROM testjsonb WHERE j = '{"pos":98, "line":371, "node":"CBA", "indexed":true}';
3312 CREATE INDEX jidx ON testjsonb USING gin (j jsonb_path_ops);
3313 SET enable_seqscan = off;
3314 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
3320 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
3326 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
3332 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
3338 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
3344 -- exercise GIN_SEARCH_MODE_ALL
3345 SELECT count(*) FROM testjsonb WHERE j @> '{}';
3351 SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
3357 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
3363 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
3369 SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
3375 SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
3381 SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
3387 SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
3393 SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
3399 SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
3405 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
3411 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
3417 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
3423 SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
3430 SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
3432 -------------------------------------------------------------------
3434 -> Bitmap Heap Scan on testjsonb
3435 Recheck Cond: (j @? '$."wait"?(@ == null)'::jsonpath)
3436 -> Bitmap Index Scan on jidx
3437 Index Cond: (j @? '$."wait"?(@ == null)'::jsonpath)
3440 SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
3446 SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
3452 SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
3458 SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
3464 SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
3470 SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
3476 SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
3482 SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
3488 SELECT count(*) FROM testjsonb WHERE j @? '$';
3494 SELECT count(*) FROM testjsonb WHERE j @? '$.public';
3500 SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
3506 RESET enable_seqscan;
3509 SELECT '{"ff":{"a":12,"b":16}}'::jsonb;
3511 ----------------------------
3512 {"ff": {"a": 12, "b": 16}}
3515 SELECT '{"ff":{"a":12,"b":16},"qq":123}'::jsonb;
3517 ---------------------------------------
3518 {"ff": {"a": 12, "b": 16}, "qq": 123}
3521 SELECT '{"aa":["a","aaa"],"qq":{"a":12,"b":16,"c":["c1","c2"],"d":{"d1":"d1","d2":"d2","d1":"d3"}}}'::jsonb;
3523 --------------------------------------------------------------------------------------------------
3524 {"aa": ["a", "aaa"], "qq": {"a": 12, "b": 16, "c": ["c1", "c2"], "d": {"d1": "d3", "d2": "d2"}}}
3527 SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2"],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
3529 ------------------------------------------------------------------------------------------------------
3530 {"aa": ["a", "aaa"], "qq": {"a": "12", "b": "16", "c": ["c1", "c2"], "d": {"d1": "d1", "d2": "d2"}}}
3533 SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2",["c3"],{"c4":4}],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
3535 -------------------------------------------------------------------------------------------------------------------------
3536 {"aa": ["a", "aaa"], "qq": {"a": "12", "b": "16", "c": ["c1", "c2", ["c3"], {"c4": 4}], "d": {"d1": "d1", "d2": "d2"}}}
3539 SELECT '{"ff":["a","aaa"]}'::jsonb;
3541 ----------------------
3542 {"ff": ["a", "aaa"]}
3546 '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'ff',
3547 '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'qq',
3548 ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'Y') IS NULL AS f,
3549 ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb ->> 'Y') IS NULL AS t,
3550 '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'x';
3551 ?column? | ?column? | f | t | ?column?
3552 --------------------+----------+---+---+----------
3553 {"a": 12, "b": 16} | 123 | f | t | [1, 2]
3556 -- nested containment
3557 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1,2]}';
3563 SELECT '{"a":[2,1],"c":"b"}'::jsonb @> '{"a":[1,2]}';
3569 SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":[1,2]}';
3575 SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":[1,2]}';
3581 SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
3587 SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
3593 SELECT '["a","b"]'::jsonb @> '["a","b","c","b"]';
3599 SELECT '["a","b","c","b"]'::jsonb @> '["a","b"]';
3605 SELECT '["a","b","c",[1,2]]'::jsonb @> '["a",[1,2]]';
3611 SELECT '["a","b","c",[1,2]]'::jsonb @> '["b",[1,2]]';
3617 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1]}';
3623 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[2]}';
3629 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[3]}';
3635 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"c":3}]}';
3641 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}';
3647 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}';
3653 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}';
3659 -- check some corner cases for indexed nested containment (bug #13756)
3660 create temp table nestjsonb (j jsonb);
3661 insert into nestjsonb (j) values ('{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}');
3662 insert into nestjsonb (j) values ('[[14,2,3]]');
3663 insert into nestjsonb (j) values ('[1,[14,2,3]]');
3664 create index on nestjsonb using gin(j jsonb_path_ops);
3665 set enable_seqscan = on;
3666 set enable_bitmapscan = off;
3667 select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
3669 ---------------------------------------------------
3670 {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
3673 select * from nestjsonb where j @> '{"c":3}';
3675 ---------------------------------------------------
3676 {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
3679 select * from nestjsonb where j @> '[[14]]';
3686 set enable_seqscan = off;
3687 set enable_bitmapscan = on;
3688 select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
3690 ---------------------------------------------------
3691 {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
3694 select * from nestjsonb where j @> '{"c":3}';
3696 ---------------------------------------------------
3697 {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
3700 select * from nestjsonb where j @> '[[14]]';
3707 reset enable_seqscan;
3708 reset enable_bitmapscan;
3709 -- nested object field / array index lookup
3710 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n';
3716 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a';
3722 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'b';
3728 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'c';
3734 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd';
3740 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd' -> '1';
3746 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
3752 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
3758 SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
3764 SELECT '["a","b","c",[1,2],null]'::jsonb -> 1;
3770 SELECT '["a","b","c",[1,2],null]'::jsonb -> 2;
3776 SELECT '["a","b","c",[1,2],null]'::jsonb -> 3;
3782 SELECT '["a","b","c",[1,2],null]'::jsonb -> 3 -> 1;
3788 SELECT '["a","b","c",[1,2],null]'::jsonb -> 4;
3794 SELECT '["a","b","c",[1,2],null]'::jsonb -> 5;
3800 SELECT '["a","b","c",[1,2],null]'::jsonb -> -1;
3806 SELECT '["a","b","c",[1,2],null]'::jsonb -> -5;
3812 SELECT '["a","b","c",[1,2],null]'::jsonb -> -6;
3818 --nested path extraction
3819 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{0}';
3825 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{a}';
3831 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c}';
3837 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,0}';
3843 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,1}';
3849 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,2}';
3855 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,3}';
3861 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-1}';
3867 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-3}';
3873 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-4}';
3879 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{0}';
3885 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{3}';
3891 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4}';
3897 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4,5}';
3904 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'n';
3910 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'a';
3916 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'b';
3922 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'c';
3928 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'd';
3934 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'e';
3940 -- jsonb_strip_nulls
3941 select jsonb_strip_nulls(null);
3947 select jsonb_strip_nulls('1');
3953 select jsonb_strip_nulls('"a string"');
3959 select jsonb_strip_nulls('null');
3965 select jsonb_strip_nulls('[1,2,null,3,4]');
3967 --------------------
3971 select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
3973 --------------------------------------------
3974 {"a": 1, "c": [2, null, 3], "d": {"e": 4}}
3977 select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
3979 --------------------------
3980 [1, {"a": 1, "c": 2}, 3]
3983 -- an empty object is not null and should not be stripped
3984 select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
3986 --------------------
3990 select jsonb_pretty('{"a": "test", "b": [1, 2, 3], "c": "test3", "d":{"dd": "test4", "dd2":{"ddd": "test5"}}}');
3992 ----------------------------
4010 select jsonb_pretty('[{"f1":1,"f2":null},2,null,[[{"x":true},6,7],8],3]');
4012 ---------------------------
4034 select jsonb_pretty('{"a":["b", "c"], "d": {"e":"f"}}');
4048 select jsonb_concat('{"d": "test", "a": [1, 2]}', '{"g": "test2", "c": {"c1":1, "c2":2}}');
4050 -------------------------------------------------------------------
4051 {"a": [1, 2], "c": {"c1": 1, "c2": 2}, "d": "test", "g": "test2"}
4054 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
4056 ---------------------------------------------
4057 {"b": "g", "aa": 1, "cq": "l", "fg": false}
4060 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aq":"l"}';
4062 ---------------------------------------
4063 {"b": 2, "aa": 1, "aq": "l", "cq": 3}
4066 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aa":"l"}';
4068 ------------------------------
4069 {"b": 2, "aa": "l", "cq": 3}
4072 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{}';
4074 ----------------------------
4075 {"b": 2, "aa": 1, "cq": 3}
4078 select '["a", "b"]'::jsonb || '["c"]';
4084 select '["a", "b"]'::jsonb || '["c", "d"]';
4086 ----------------------
4087 ["a", "b", "c", "d"]
4090 select '["c"]' || '["a", "b"]'::jsonb;
4096 select '["a", "b"]'::jsonb || '"c"';
4102 select '"c"' || '["a", "b"]'::jsonb;
4108 select '[]'::jsonb || '["a"]'::jsonb;
4114 select '[]'::jsonb || '"a"'::jsonb;
4120 select '"b"'::jsonb || '"a"'::jsonb;
4126 select '{}'::jsonb || '{"a":"b"}'::jsonb;
4132 select '[]'::jsonb || '{"a":"b"}'::jsonb;
4138 select '{"a":"b"}'::jsonb || '[]'::jsonb;
4144 select '"a"'::jsonb || '{"a":1}';
4150 select '{"a":1}' || '"a"'::jsonb;
4156 select '[3]'::jsonb || '{}'::jsonb;
4162 select '3'::jsonb || '[]'::jsonb;
4168 select '3'::jsonb || '4'::jsonb;
4174 select '3'::jsonb || '{}'::jsonb;
4180 select '["a", "b"]'::jsonb || '{"c":1}';
4182 ----------------------
4183 ["a", "b", {"c": 1}]
4186 select '{"c": 1}'::jsonb || '["a", "b"]';
4188 ----------------------
4189 [{"c": 1}, "a", "b"]
4192 select '{}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
4194 ------------------------------------
4195 {"b": "g", "cq": "l", "fg": false}
4198 select pg_column_size('{}'::jsonb || '{}'::jsonb) = pg_column_size('{}'::jsonb);
4204 select pg_column_size('{"aa":1}'::jsonb || '{"b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
4210 select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
4216 select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
4222 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a');
4228 select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a');
4234 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b');
4240 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c');
4246 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd');
4248 --------------------------
4249 {"a": 1, "b": 2, "c": 3}
4252 select '{"a":1 , "b":2, "c":3}'::jsonb - 'a';
4258 select '{"a":null , "b":2, "c":3}'::jsonb - 'a';
4264 select '{"a":1 , "b":2, "c":3}'::jsonb - 'b';
4270 select '{"a":1 , "b":2, "c":3}'::jsonb - 'c';
4276 select '{"a":1 , "b":2, "c":3}'::jsonb - 'd';
4278 --------------------------
4279 {"a": 1, "b": 2, "c": 3}
4282 select pg_column_size('{"a":1 , "b":2, "c":3}'::jsonb - 'b') = pg_column_size('{"a":1, "b":2}'::jsonb);
4288 select '["a","b","c"]'::jsonb - 3;
4294 select '["a","b","c"]'::jsonb - 2;
4300 select '["a","b","c"]'::jsonb - 1;
4306 select '["a","b","c"]'::jsonb - 0;
4312 select '["a","b","c"]'::jsonb - -1;
4318 select '["a","b","c"]'::jsonb - -2;
4324 select '["a","b","c"]'::jsonb - -3;
4330 select '["a","b","c"]'::jsonb - -4;
4336 select '{"a":1 , "b":2, "c":3}'::jsonb - '{b}'::text[];
4342 select '{"a":1 , "b":2, "c":3}'::jsonb - '{c,b}'::text[];
4348 select '{"a":1 , "b":2, "c":3}'::jsonb - '{}'::text[];
4350 --------------------------
4351 {"a": 1, "b": 2, "c": 3}
4354 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
4356 --------------------------------------------------------------------------
4357 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": [1, 2, 3]}
4360 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
4362 -----------------------------------------------------------------------------
4363 {"a": 1, "b": [1, [1, 2, 3]], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
4366 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
4368 -----------------------------------------------------------------------------
4369 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [[1, 2, 3], 3]}, "n": null}
4372 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
4373 ERROR: path element at position 2 is null
4374 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
4376 -------------------------------------------------------------------------
4377 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": {"1": 2}}
4380 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
4382 ----------------------------------------------------------------------------
4383 {"a": 1, "b": [1, {"1": 2}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
4386 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
4388 ----------------------------------------------------------------------------
4389 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [{"1": 2}, 3]}, "n": null}
4392 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
4393 ERROR: path element at position 2 is null
4394 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
4396 --------------------------------------------------------------------------
4397 {"a": 1, "b": [1, "test"], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
4400 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
4402 ---------------------------------------------------------------------------------
4403 {"a": 1, "b": [1, {"f": "test"}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
4406 select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}');
4408 ----------------------------------------------------------
4409 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
4412 select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}');
4414 ------------------------------------------------------------------
4415 {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
4418 select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{d,1,0}');
4420 ------------------------------------------------------------------
4421 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
4424 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{n}';
4426 ----------------------------------------------------------
4427 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
4430 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1}';
4432 ------------------------------------------------------------------
4433 {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
4436 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript
4437 ERROR: path element at position 2 is not an integer: "-1e"
4438 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}';
4440 ------------------------------------------------------------------
4441 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
4444 -- empty structure and error conditions for delete and replace
4445 select '"a"'::jsonb - 'a'; -- error
4446 ERROR: cannot delete from scalar
4447 select '{}'::jsonb - 'a';
4453 select '[]'::jsonb - 'a';
4459 select '"a"'::jsonb - 1; -- error
4460 ERROR: cannot delete from scalar
4461 select '{}'::jsonb - 1; -- error
4462 ERROR: cannot delete from object using integer index
4463 select '[]'::jsonb - 1;
4469 select '"a"'::jsonb #- '{a}'; -- error
4470 ERROR: cannot delete path in scalar
4471 select '{}'::jsonb #- '{a}';
4477 select '[]'::jsonb #- '{a}';
4483 select jsonb_set('"a"','{a}','"b"'); --error
4484 ERROR: cannot set path in scalar
4485 select jsonb_set('{}','{a}','"b"', false);
4491 select jsonb_set('[]','{1}','"b"', false);
4497 select jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0}','[2,3,4]', false);
4499 -------------------------
4500 [[2, 3, 4], 2, null, 3]
4503 -- jsonb_set adding instead of replacing
4505 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,-33}','{"foo":123}');
4507 -------------------------------------------------------
4508 {"a": 1, "b": [{"foo": 123}, 0, 1, 2], "c": {"d": 4}}
4512 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,33}','{"foo":123}');
4514 -------------------------------------------------------
4515 {"a": 1, "b": [0, 1, 2, {"foo": 123}], "c": {"d": 4}}
4518 -- check nesting levels addition
4519 select jsonb_set('{"a":1,"b":[4,5,[0,1,2],6,7],"c":{"d":4}}','{b,2,33}','{"foo":123}');
4521 ---------------------------------------------------------------------
4522 {"a": 1, "b": [4, 5, [0, 1, 2, {"foo": 123}], 6, 7], "c": {"d": 4}}
4526 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{c,e}','{"foo":123}');
4528 ------------------------------------------------------------
4529 {"a": 1, "b": [0, 1, 2], "c": {"d": 4, "e": {"foo": 123}}}
4532 -- adding doesn't do anything if elements before last aren't present
4533 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,-33}','{"foo":123}');
4535 -----------------------------------------
4536 {"a": 1, "b": [0, 1, 2], "c": {"d": 4}}
4539 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,y}','{"foo":123}');
4541 -----------------------------------------
4542 {"a": 1, "b": [0, 1, 2], "c": {"d": 4}}
4545 -- add to empty object
4546 select jsonb_set('{}','{x}','{"foo":123}');
4548 ---------------------
4552 --add to empty array
4553 select jsonb_set('[]','{0}','{"foo":123}');
4559 select jsonb_set('[]','{99}','{"foo":123}');
4565 select jsonb_set('[]','{-99}','{"foo":123}');
4571 select jsonb_set('{"a": [1, 2, 3]}', '{a, non_integer}', '"new_value"');
4572 ERROR: path element at position 2 is not an integer: "non_integer"
4573 select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, non_integer}', '"new_value"');
4574 ERROR: path element at position 3 is not an integer: "non_integer"
4575 select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, NULL}', '"new_value"');
4576 ERROR: path element at position 3 is null
4579 -- pass though non nulls to jsonb_set
4580 select jsonb_set_lax('{"a":1,"b":2}','{b}','5') ;
4586 select jsonb_set_lax('{"a":1,"b":2}','{d}','6', true) ;
4588 --------------------------
4589 {"a": 1, "b": 2, "d": 6}
4592 -- using the default treatment
4593 select jsonb_set_lax('{"a":1,"b":2}','{b}',null);
4595 ---------------------
4599 select jsonb_set_lax('{"a":1,"b":2}','{d}',null,true);
4601 -----------------------------
4602 {"a": 1, "b": 2, "d": null}
4606 select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, null);
4607 ERROR: null_value_treatment must be "delete_key", "return_target", "use_json_null", or "raise_exception"
4608 select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, 'no_such_treatment');
4609 ERROR: null_value_treatment must be "delete_key", "return_target", "use_json_null", or "raise_exception"
4610 -- explicit treatments
4611 select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'raise_exception') as raise_exception;
4612 ERROR: JSON value must not be null
4613 DETAIL: Exception was raised because null_value_treatment is "raise_exception".
4614 HINT: To avoid, either change the null_value_treatment argument or ensure that an SQL NULL is not passed.
4615 select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'return_target') as return_target;
4621 select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'delete_key') as delete_key;
4627 select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'use_json_null') as use_json_null;
4629 ---------------------
4635 select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"');
4637 -------------------------------
4638 {"a": [0, "new_value", 1, 2]}
4641 select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true);
4643 -------------------------------
4644 {"a": [0, 1, "new_value", 2]}
4647 select jsonb_insert('{"a": {"b": {"c": [0, 1, "test1", "test2"]}}}', '{a, b, c, 2}', '"new_value"');
4649 ------------------------------------------------------------
4650 {"a": {"b": {"c": [0, 1, "new_value", "test1", "test2"]}}}
4653 select jsonb_insert('{"a": {"b": {"c": [0, 1, "test1", "test2"]}}}', '{a, b, c, 2}', '"new_value"', true);
4655 ------------------------------------------------------------
4656 {"a": {"b": {"c": [0, 1, "test1", "new_value", "test2"]}}}
4659 select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '{"b": "value"}');
4661 ----------------------------------
4662 {"a": [0, {"b": "value"}, 1, 2]}
4665 select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '["value1", "value2"]');
4667 ----------------------------------------
4668 {"a": [0, ["value1", "value2"], 1, 2]}
4672 select jsonb_insert('{"a": [0,1,2]}', '{a, 0}', '"new_value"');
4674 -------------------------------
4675 {"a": ["new_value", 0, 1, 2]}
4678 select jsonb_insert('{"a": [0,1,2]}', '{a, 0}', '"new_value"', true);
4680 -------------------------------
4681 {"a": [0, "new_value", 1, 2]}
4684 select jsonb_insert('{"a": [0,1,2]}', '{a, 2}', '"new_value"');
4686 -------------------------------
4687 {"a": [0, 1, "new_value", 2]}
4690 select jsonb_insert('{"a": [0,1,2]}', '{a, 2}', '"new_value"', true);
4692 -------------------------------
4693 {"a": [0, 1, 2, "new_value"]}
4696 select jsonb_insert('{"a": [0,1,2]}', '{a, -1}', '"new_value"');
4698 -------------------------------
4699 {"a": [0, 1, "new_value", 2]}
4702 select jsonb_insert('{"a": [0,1,2]}', '{a, -1}', '"new_value"', true);
4704 -------------------------------
4705 {"a": [0, 1, 2, "new_value"]}
4708 select jsonb_insert('[]', '{1}', '"new_value"');
4714 select jsonb_insert('[]', '{1}', '"new_value"', true);
4720 select jsonb_insert('{"a": []}', '{a, 1}', '"new_value"');
4722 ----------------------
4723 {"a": ["new_value"]}
4726 select jsonb_insert('{"a": []}', '{a, 1}', '"new_value"', true);
4728 ----------------------
4729 {"a": ["new_value"]}
4732 select jsonb_insert('{"a": [0,1,2]}', '{a, 10}', '"new_value"');
4734 -------------------------------
4735 {"a": [0, 1, 2, "new_value"]}
4738 select jsonb_insert('{"a": [0,1,2]}', '{a, -10}', '"new_value"');
4740 -------------------------------
4741 {"a": ["new_value", 0, 1, 2]}
4744 -- jsonb_insert should be able to insert new value for objects, but not to replace
4745 select jsonb_insert('{"a": {"b": "value"}}', '{a, c}', '"new_value"');
4747 -----------------------------------------
4748 {"a": {"b": "value", "c": "new_value"}}
4751 select jsonb_insert('{"a": {"b": "value"}}', '{a, c}', '"new_value"', true);
4753 -----------------------------------------
4754 {"a": {"b": "value", "c": "new_value"}}
4757 select jsonb_insert('{"a": {"b": "value"}}', '{a, b}', '"new_value"');
4758 ERROR: cannot replace existing key
4759 HINT: Try using the function jsonb_set to replace key value.
4760 select jsonb_insert('{"a": {"b": "value"}}', '{a, b}', '"new_value"', true);
4761 ERROR: cannot replace existing key
4762 HINT: Try using the function jsonb_set to replace key value.
4764 select ('123'::jsonb)['a'];
4770 select ('123'::jsonb)[0];
4776 select ('123'::jsonb)[NULL];
4782 select ('{"a": 1}'::jsonb)['a'];
4788 select ('{"a": 1}'::jsonb)[0];
4794 select ('{"a": 1}'::jsonb)['not_exist'];
4800 select ('{"a": 1}'::jsonb)[NULL];
4806 select ('[1, "2", null]'::jsonb)['a'];
4812 select ('[1, "2", null]'::jsonb)[0];
4818 select ('[1, "2", null]'::jsonb)['1'];
4824 select ('[1, "2", null]'::jsonb)[1.0];
4825 ERROR: subscript type is not supported
4826 LINE 1: select ('[1, "2", null]'::jsonb)[1.0];
4828 HINT: Jsonb subscript must be coerced to either integer or text
4829 select ('[1, "2", null]'::jsonb)[2];
4835 select ('[1, "2", null]'::jsonb)[3];
4841 select ('[1, "2", null]'::jsonb)[-2];
4847 select ('[1, "2", null]'::jsonb)[1]['a'];
4853 select ('[1, "2", null]'::jsonb)[1][0];
4859 select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['b'];
4865 select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d'];
4871 select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d'][1];
4877 select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d']['a'];
4883 select ('{"a": {"a1": {"a2": "aaa"}}, "b": "bbb", "c": "ccc"}'::jsonb)['a']['a1'];
4889 select ('{"a": {"a1": {"a2": "aaa"}}, "b": "bbb", "c": "ccc"}'::jsonb)['a']['a1']['a2'];
4895 select ('{"a": {"a1": {"a2": "aaa"}}, "b": "bbb", "c": "ccc"}'::jsonb)['a']['a1']['a2']['a3'];
4901 select ('{"a": ["a1", {"b1": ["aaa", "bbb", "ccc"]}], "b": "bb"}'::jsonb)['a'][1]['b1'];
4903 -----------------------
4904 ["aaa", "bbb", "ccc"]
4907 select ('{"a": ["a1", {"b1": ["aaa", "bbb", "ccc"]}], "b": "bb"}'::jsonb)['a'][1]['b1'][2];
4913 -- slices are not supported
4914 select ('{"a": 1}'::jsonb)['a':'b'];
4915 ERROR: jsonb subscript does not support slices
4916 LINE 1: select ('{"a": 1}'::jsonb)['a':'b'];
4918 select ('[1, "2", null]'::jsonb)[1:2];
4919 ERROR: jsonb subscript does not support slices
4920 LINE 1: select ('[1, "2", null]'::jsonb)[1:2];
4922 select ('[1, "2", null]'::jsonb)[:2];
4923 ERROR: jsonb subscript does not support slices
4924 LINE 1: select ('[1, "2", null]'::jsonb)[:2];
4926 select ('[1, "2", null]'::jsonb)[1:];
4927 ERROR: jsonb subscript does not support slices
4928 LINE 1: select ('[1, "2", null]'::jsonb)[1:];
4930 select ('[1, "2", null]'::jsonb)[:];
4931 ERROR: jsonb subscript does not support slices
4932 create TEMP TABLE test_jsonb_subscript (
4936 insert into test_jsonb_subscript values
4937 (1, '{}'), -- empty jsonb
4938 (2, '{"key": "value"}'); -- jsonb with data
4939 -- update empty jsonb
4940 update test_jsonb_subscript set test_json['a'] = '1' where id = 1;
4941 select * from test_jsonb_subscript;
4943 ----+------------------
4944 2 | {"key": "value"}
4948 -- update jsonb with some data
4949 update test_jsonb_subscript set test_json['a'] = '1' where id = 2;
4950 select * from test_jsonb_subscript;
4952 ----+--------------------------
4954 2 | {"a": 1, "key": "value"}
4958 update test_jsonb_subscript set test_json['a'] = '"test"';
4959 select * from test_jsonb_subscript;
4961 ----+-------------------------------
4963 2 | {"a": "test", "key": "value"}
4966 -- replace by object
4967 update test_jsonb_subscript set test_json['a'] = '{"b": 1}'::jsonb;
4968 select * from test_jsonb_subscript;
4970 ----+---------------------------------
4972 2 | {"a": {"b": 1}, "key": "value"}
4976 update test_jsonb_subscript set test_json['a'] = '[1, 2, 3]'::jsonb;
4977 select * from test_jsonb_subscript;
4979 ----+----------------------------------
4980 1 | {"a": [1, 2, 3]}
4981 2 | {"a": [1, 2, 3], "key": "value"}
4984 -- use jsonb subscription in where clause
4985 select * from test_jsonb_subscript where test_json['key'] = '"value"';
4987 ----+----------------------------------
4988 2 | {"a": [1, 2, 3], "key": "value"}
4991 select * from test_jsonb_subscript where test_json['key_doesnt_exists'] = '"value"';
4996 select * from test_jsonb_subscript where test_json['key'] = '"wrong_value"';
5002 update test_jsonb_subscript set test_json[NULL] = '1';
5003 ERROR: jsonb subscript in assignment must not be null
5004 update test_jsonb_subscript set test_json['another_key'] = NULL;
5005 select * from test_jsonb_subscript;
5007 ----+-------------------------------------------------------
5008 1 | {"a": [1, 2, 3], "another_key": null}
5009 2 | {"a": [1, 2, 3], "key": "value", "another_key": null}
5012 -- NULL as jsonb source
5013 insert into test_jsonb_subscript values (3, NULL);
5014 update test_jsonb_subscript set test_json['a'] = '1' where id = 3;
5015 select * from test_jsonb_subscript;
5017 ----+-------------------------------------------------------
5018 1 | {"a": [1, 2, 3], "another_key": null}
5019 2 | {"a": [1, 2, 3], "key": "value", "another_key": null}
5023 update test_jsonb_subscript set test_json = NULL where id = 3;
5024 update test_jsonb_subscript set test_json[0] = '1';
5025 select * from test_jsonb_subscript;
5027 ----+---------------------------------------------------------------
5028 1 | {"0": 1, "a": [1, 2, 3], "another_key": null}
5029 2 | {"0": 1, "a": [1, 2, 3], "key": "value", "another_key": null}
5033 -- Fill the gaps logic
5034 delete from test_jsonb_subscript;
5035 insert into test_jsonb_subscript values (1, '[0]');
5036 update test_jsonb_subscript set test_json[5] = '1';
5037 select * from test_jsonb_subscript;
5039 ----+--------------------------------
5040 1 | [0, null, null, null, null, 1]
5043 update test_jsonb_subscript set test_json[-4] = '1';
5044 select * from test_jsonb_subscript;
5046 ----+-----------------------------
5047 1 | [0, null, 1, null, null, 1]
5050 update test_jsonb_subscript set test_json[-8] = '1';
5051 ERROR: path element at position 1 is out of range: -8
5052 select * from test_jsonb_subscript;
5054 ----+-----------------------------
5055 1 | [0, null, 1, null, null, 1]
5058 -- keep consistent values position
5059 delete from test_jsonb_subscript;
5060 insert into test_jsonb_subscript values (1, '[]');
5061 update test_jsonb_subscript set test_json[5] = '1';
5062 select * from test_jsonb_subscript;
5064 ----+-----------------------------------
5065 1 | [null, null, null, null, null, 1]
5068 -- create the whole path
5069 delete from test_jsonb_subscript;
5070 insert into test_jsonb_subscript values (1, '{}');
5071 update test_jsonb_subscript set test_json['a'][0]['b'][0]['c'] = '1';
5072 select * from test_jsonb_subscript;
5074 ----+----------------------------
5075 1 | {"a": [{"b": [{"c": 1}]}]}
5078 delete from test_jsonb_subscript;
5079 insert into test_jsonb_subscript values (1, '{}');
5080 update test_jsonb_subscript set test_json['a'][2]['b'][2]['c'][2] = '1';
5081 select * from test_jsonb_subscript;
5083 ----+------------------------------------------------------------------
5084 1 | {"a": [null, null, {"b": [null, null, {"c": [null, null, 1]}]}]}
5087 -- create the whole path with already existing keys
5088 delete from test_jsonb_subscript;
5089 insert into test_jsonb_subscript values (1, '{"b": 1}');
5090 update test_jsonb_subscript set test_json['a'][0] = '2';
5091 select * from test_jsonb_subscript;
5093 ----+--------------------
5094 1 | {"a": [2], "b": 1}
5097 -- the start jsonb is an object, first subscript is treated as a key
5098 delete from test_jsonb_subscript;
5099 insert into test_jsonb_subscript values (1, '{}');
5100 update test_jsonb_subscript set test_json[0]['a'] = '1';
5101 select * from test_jsonb_subscript;
5103 ----+-----------------
5107 -- the start jsonb is an array
5108 delete from test_jsonb_subscript;
5109 insert into test_jsonb_subscript values (1, '[]');
5110 update test_jsonb_subscript set test_json[0]['a'] = '1';
5111 update test_jsonb_subscript set test_json[2]['b'] = '2';
5112 select * from test_jsonb_subscript;
5114 ----+----------------------------
5115 1 | [{"a": 1}, null, {"b": 2}]
5118 -- overwriting an existing path
5119 delete from test_jsonb_subscript;
5120 insert into test_jsonb_subscript values (1, '{}');
5121 update test_jsonb_subscript set test_json['a']['b'][1] = '1';
5122 update test_jsonb_subscript set test_json['a']['b'][10] = '1';
5123 select * from test_jsonb_subscript;
5125 ----+----------------------------------------------------------------------------
5126 1 | {"a": {"b": [null, 1, null, null, null, null, null, null, null, null, 1]}}
5129 delete from test_jsonb_subscript;
5130 insert into test_jsonb_subscript values (1, '[]');
5131 update test_jsonb_subscript set test_json[0][0][0] = '1';
5132 update test_jsonb_subscript set test_json[0][0][1] = '1';
5133 select * from test_jsonb_subscript;
5139 delete from test_jsonb_subscript;
5140 insert into test_jsonb_subscript values (1, '{}');
5141 update test_jsonb_subscript set test_json['a']['b'][10] = '1';
5142 update test_jsonb_subscript set test_json['a'][10][10] = '1';
5143 select * from test_jsonb_subscript;
5145 ----+------------------------------------------------------------------------------------------------------------------------------------------------------
5146 1 | {"a": {"b": [null, null, null, null, null, null, null, null, null, null, 1], "10": [null, null, null, null, null, null, null, null, null, null, 1]}}
5149 -- an empty sub element
5150 delete from test_jsonb_subscript;
5151 insert into test_jsonb_subscript values (1, '{"a": {}}');
5152 update test_jsonb_subscript set test_json['a']['b']['c'][2] = '1';
5153 select * from test_jsonb_subscript;
5155 ----+--------------------------------------
5156 1 | {"a": {"b": {"c": [null, null, 1]}}}
5159 delete from test_jsonb_subscript;
5160 insert into test_jsonb_subscript values (1, '{"a": []}');
5161 update test_jsonb_subscript set test_json['a'][1]['c'][2] = '1';
5162 select * from test_jsonb_subscript;
5164 ----+---------------------------------------
5165 1 | {"a": [null, {"c": [null, null, 1]}]}
5168 -- trying replace assuming a composite object, but it's an element or a value
5169 delete from test_jsonb_subscript;
5170 insert into test_jsonb_subscript values (1, '{"a": 1}');
5171 update test_jsonb_subscript set test_json['a']['b'] = '1';
5172 ERROR: cannot replace existing key
5173 DETAIL: The path assumes key is a composite object, but it is a scalar value.
5174 update test_jsonb_subscript set test_json['a']['b']['c'] = '1';
5175 ERROR: cannot replace existing key
5176 DETAIL: The path assumes key is a composite object, but it is a scalar value.
5177 update test_jsonb_subscript set test_json['a'][0] = '1';
5178 ERROR: cannot replace existing key
5179 DETAIL: The path assumes key is a composite object, but it is a scalar value.
5180 update test_jsonb_subscript set test_json['a'][0]['c'] = '1';
5181 ERROR: cannot replace existing key
5182 DETAIL: The path assumes key is a composite object, but it is a scalar value.
5183 update test_jsonb_subscript set test_json['a'][0][0] = '1';
5184 ERROR: cannot replace existing key
5185 DETAIL: The path assumes key is a composite object, but it is a scalar value.
5186 -- trying replace assuming a composite object, but it's a raw scalar
5187 delete from test_jsonb_subscript;
5188 insert into test_jsonb_subscript values (1, 'null');
5189 update test_jsonb_subscript set test_json[0] = '1';
5190 ERROR: cannot replace existing key
5191 DETAIL: The path assumes key is a composite object, but it is a scalar value.
5192 update test_jsonb_subscript set test_json[0][0] = '1';
5193 ERROR: cannot replace existing key
5194 DETAIL: The path assumes key is a composite object, but it is a scalar value.
5195 -- jsonb to tsvector
5196 select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::jsonb);
5198 ---------------------------------------------------------------------------
5199 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
5202 -- jsonb to tsvector with config
5203 select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::jsonb);
5205 ---------------------------------------------------------------------------
5206 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
5209 -- jsonb to tsvector with stop words
5210 select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::jsonb);
5212 ----------------------------------------------------------------------------
5213 'aaa':1 'bbb':3 'ccc':5 'ddd':4 'eee':8 'fff':9 'ggg':10 'hhh':12 'iii':13
5216 -- jsonb to tsvector with numeric values
5217 select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": 123, "c": 456}'::jsonb);
5219 ---------------------------------
5220 'aaa':1 'bbb':3 'ccc':5 'ddd':4
5223 -- jsonb_to_tsvector
5224 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"all"');
5226 ----------------------------------------------------------------------------------------
5227 '123':8 '456':12 'aaa':2 'b':6 'bbb':4 'c':10 'd':14 'f':18 'fals':20 'g':22 'true':16
5230 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"key"');
5232 --------------------------------
5233 'b':2 'c':4 'd':6 'f':8 'g':10
5236 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"string"');
5242 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"numeric"');
5248 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"boolean"');
5254 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '["string", "numeric"]');
5256 ---------------------------------
5257 '123':5 '456':7 'aaa':1 'bbb':3
5260 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"all"');
5262 ----------------------------------------------------------------------------------------
5263 '123':8 '456':12 'aaa':2 'b':6 'bbb':4 'c':10 'd':14 'f':18 'fals':20 'g':22 'true':16
5266 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"key"');
5268 --------------------------------
5269 'b':2 'c':4 'd':6 'f':8 'g':10
5272 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"string"');
5278 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"numeric"');
5284 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"boolean"');
5290 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '["string", "numeric"]');
5292 ---------------------------------
5293 '123':5 '456':7 'aaa':1 'bbb':3
5296 -- to_tsvector corner cases
5297 select to_tsvector('""'::jsonb);
5303 select to_tsvector('{}'::jsonb);
5309 select to_tsvector('[]'::jsonb);
5315 select to_tsvector('null'::jsonb);
5321 -- jsonb_to_tsvector corner cases
5322 select jsonb_to_tsvector('""'::jsonb, '"all"');
5328 select jsonb_to_tsvector('{}'::jsonb, '"all"');
5334 select jsonb_to_tsvector('[]'::jsonb, '"all"');
5340 select jsonb_to_tsvector('null'::jsonb, '"all"');
5346 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '""');
5347 ERROR: wrong flag in flag array: ""
5348 HINT: Possible values are: "string", "numeric", "boolean", "key", and "all".
5349 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '{}');
5350 ERROR: wrong flag type, only arrays and scalars are allowed
5351 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '[]');
5357 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, 'null');
5358 ERROR: flag array element is not a string
5359 HINT: Possible values are: "string", "numeric", "boolean", "key", and "all".
5360 select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '["all", null]');
5361 ERROR: flag array element is not a string
5362 HINT: Possible values are: "string", "numeric", "boolean", "key", and "all".
5363 -- ts_headline for jsonb
5364 select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'));
5366 ------------------------------------------------------------------------------------------------------------------
5367 {"a": "aaa <b>bbb</b>", "b": {"c": "ccc <b>ddd</b> fff", "c1": "ccc1 ddd1"}, "d": ["ggg <b>hhh</b>", "iii jjj"]}
5370 select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'));
5372 -----------------------------------------------------------------------------------------------
5373 {"a": "aaa <b>bbb</b>", "b": {"c": "ccc <b>ddd</b> fff"}, "d": ["ggg <b>hhh</b>", "iii jjj"]}
5376 select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
5378 ---------------------------------------------------------------------------------------------------
5379 {"a": "aaa <bbb>", "b": {"c": "ccc <ddd> fff", "c1": "ccc1 ddd1"}, "d": ["ggg <hhh>", "iii jjj"]}
5382 select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
5384 ---------------------------------------------------------------------------------------------------
5385 {"a": "aaa <bbb>", "b": {"c": "ccc <ddd> fff", "c1": "ccc1 ddd1"}, "d": ["ggg <hhh>", "iii jjj"]}
5388 -- corner cases for ts_headline with jsonb
5389 select ts_headline('null'::jsonb, tsquery('aaa & bbb'));
5395 select ts_headline('{}'::jsonb, tsquery('aaa & bbb'));
5401 select ts_headline('[]'::jsonb, tsquery('aaa & bbb'));
5408 select 'true'::jsonb::bool;
5414 select '[]'::jsonb::bool;
5415 ERROR: cannot cast jsonb array to type boolean
5416 select '1.0'::jsonb::float;
5422 select '[1.0]'::jsonb::float;
5423 ERROR: cannot cast jsonb array to type double precision
5424 select '12345'::jsonb::int4;
5430 select '"hello"'::jsonb::int4;
5431 ERROR: cannot cast jsonb string to type integer
5432 select '12345'::jsonb::numeric;
5438 select '{}'::jsonb::numeric;
5439 ERROR: cannot cast jsonb object to type numeric
5440 select '12345.05'::jsonb::numeric;
5446 select '12345.05'::jsonb::float4;
5452 select '12345.05'::jsonb::float8;
5458 select '12345.05'::jsonb::int2;
5464 select '12345.05'::jsonb::int4;
5470 select '12345.05'::jsonb::int8;
5476 select '12345.0000000000000000000000000000000000000000000005'::jsonb::numeric;
5478 ------------------------------------------------------
5479 12345.0000000000000000000000000000000000000000000005
5482 select '12345.0000000000000000000000000000000000000000000005'::jsonb::float4;
5488 select '12345.0000000000000000000000000000000000000000000005'::jsonb::float8;
5494 select '12345.0000000000000000000000000000000000000000000005'::jsonb::int2;
5500 select '12345.0000000000000000000000000000000000000000000005'::jsonb::int4;
5506 select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8;