]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/jsonb.sql
f86b7fbf9bf1d6e4da42e9a0efbbe59670a4e442
[postgresql] / src / test / regress / sql / jsonb.sql
1 -- Strings.
2 SELECT '""'::jsonb;                             -- OK.
3 SELECT $$''$$::jsonb;                   -- ERROR, single quotes are not allowed
4 SELECT '"abc"'::jsonb;                  -- OK
5 SELECT '"abc'::jsonb;                   -- ERROR, quotes not closed
6 SELECT '"abc
7 def"'::jsonb;                                   -- ERROR, unescaped newline in string constant
8 SELECT '"\n\"\\"'::jsonb;               -- OK, legal escapes
9 SELECT '"\v"'::jsonb;                   -- ERROR, not a valid JSON escape
10 SELECT '"\u"'::jsonb;                   -- ERROR, incomplete escape
11 SELECT '"\u00"'::jsonb;                 -- ERROR, incomplete escape
12 SELECT '"\u000g"'::jsonb;               -- ERROR, g is not a hex digit
13 SELECT '"\u0045"'::jsonb;               -- OK, legal escape
14 SELECT '"\u0000"'::jsonb;               -- ERROR, we don't support U+0000
15 -- use octet_length here so we don't get an odd unicode char in the
16 -- output
17 SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK
18
19 -- Numbers.
20 SELECT '1'::jsonb;                              -- OK
21 SELECT '0'::jsonb;                              -- OK
22 SELECT '01'::jsonb;                             -- ERROR, not valid according to JSON spec
23 SELECT '0.1'::jsonb;                            -- OK
24 SELECT '9223372036854775808'::jsonb;    -- OK, even though it's too large for int8
25 SELECT '1e100'::jsonb;                  -- OK
26 SELECT '1.3e100'::jsonb;                        -- OK
27 SELECT '1f2'::jsonb;                            -- ERROR
28 SELECT '0.x1'::jsonb;                   -- ERROR
29 SELECT '1.3ex100'::jsonb;               -- ERROR
30
31 -- Arrays.
32 SELECT '[]'::jsonb;                             -- OK
33 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::jsonb;  -- OK
34 SELECT '[1,2]'::jsonb;                  -- OK
35 SELECT '[1,2,]'::jsonb;                 -- ERROR, trailing comma
36 SELECT '[1,2'::jsonb;                   -- ERROR, no closing bracket
37 SELECT '[1,[2]'::jsonb;                 -- ERROR, no closing bracket
38
39 -- Objects.
40 SELECT '{}'::jsonb;                             -- OK
41 SELECT '{"abc"}'::jsonb;                        -- ERROR, no value
42 SELECT '{"abc":1}'::jsonb;              -- OK
43 SELECT '{1:"abc"}'::jsonb;              -- ERROR, keys must be strings
44 SELECT '{"abc",1}'::jsonb;              -- ERROR, wrong separator
45 SELECT '{"abc"=1}'::jsonb;              -- ERROR, totally wrong separator
46 SELECT '{"abc"::1}'::jsonb;             -- ERROR, another wrong separator
47 SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::jsonb; -- OK
48 SELECT '{"abc":1:2}'::jsonb;            -- ERROR, colon in wrong spot
49 SELECT '{"abc":1,3}'::jsonb;            -- ERROR, no value
50
51 -- Recursion.
52 SET max_stack_depth = '100kB';
53 SELECT repeat('[', 1000)::jsonb;
54 SELECT repeat('{"a":', 1000)::jsonb;
55 RESET max_stack_depth;
56
57 -- Miscellaneous stuff.
58 SELECT 'true'::jsonb;                   -- OK
59 SELECT 'false'::jsonb;                  -- OK
60 SELECT 'null'::jsonb;                   -- OK
61 SELECT ' true '::jsonb;                 -- OK, even with extra whitespace
62 SELECT 'true false'::jsonb;             -- ERROR, too many values
63 SELECT 'true, false'::jsonb;            -- ERROR, too many values
64 SELECT 'truf'::jsonb;                   -- ERROR, not a keyword
65 SELECT 'trues'::jsonb;                  -- ERROR, not a keyword
66 SELECT ''::jsonb;                               -- ERROR, no value
67 SELECT '    '::jsonb;                   -- ERROR, no value
68
69 -- make sure jsonb is passed through json generators without being escaped
70 SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
71
72 -- to_jsonb, timestamps
73
74 select to_jsonb(timestamp '2014-05-28 12:22:35.614298');
75
76 BEGIN;
77 SET LOCAL TIME ZONE 10.5;
78 select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
79 SET LOCAL TIME ZONE -8;
80 select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
81 COMMIT;
82
83 select to_jsonb(date '2014-05-28');
84
85 select to_jsonb(date 'Infinity');
86 select to_jsonb(timestamp 'Infinity');
87 select to_jsonb(timestamptz 'Infinity');
88
89 --jsonb_agg
90
91 CREATE TEMP TABLE rows AS
92 SELECT x, 'txt' || x as y
93 FROM generate_series(1,3) AS x;
94
95 SELECT jsonb_agg(q)
96   FROM ( SELECT $$a$$ || x AS b, y AS c,
97                ARRAY[ROW(x.*,ARRAY[1,2,3]),
98                ROW(y.*,ARRAY[4,5,6])] AS z
99          FROM generate_series(1,2) x,
100               generate_series(4,5) y) q;
101
102 SELECT jsonb_agg(q ORDER BY x, y)
103   FROM rows q;
104
105 UPDATE rows SET x = NULL WHERE x = 1;
106
107 SELECT jsonb_agg(q ORDER BY x NULLS FIRST, y)
108   FROM rows q;
109
110 -- jsonb extraction functions
111 CREATE TEMP TABLE test_jsonb (
112        json_type text,
113        test_json jsonb
114 );
115
116 INSERT INTO test_jsonb VALUES
117 ('scalar','"a scalar"'),
118 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
119 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
120
121 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar';
122 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
123 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
124 SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
125
126 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
127 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
128 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
129
130 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
131 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
132 SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
133 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
134
135 SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
136 SELECT test_json ->> 7 FROM test_jsonb WHERE json_type = 'array';
137
138 SELECT test_json ->> 'field4' FROM test_jsonb WHERE json_type = 'object';
139 SELECT test_json ->> 'field5' FROM test_jsonb WHERE json_type = 'object';
140 SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
141
142 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
143 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
144 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
145
146 SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar';
147 SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array';
148 SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'object';
149
150 -- nulls
151 SELECT (test_json->'field3') IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'object';
152 SELECT (test_json->>'field3') IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'object';
153 SELECT (test_json->3) IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'array';
154 SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'array';
155
156 -- corner cases
157 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
158 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
159 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
160 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
161 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
162 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
163 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
164 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
165 select '{"a": "c", "b": null}'::jsonb -> 'b';
166 select '"foo"'::jsonb -> 1;
167 select '"foo"'::jsonb -> 'z';
168
169 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
170 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
171 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
172 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
173 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
174 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
175 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
176 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
177 select '{"a": "c", "b": null}'::jsonb ->> 'b';
178 select '"foo"'::jsonb ->> 1;
179 select '"foo"'::jsonb ->> 'z';
180
181 -- equality and inequality
182 SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
183 SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb;
184
185 SELECT '{"x":"y"}'::jsonb <> '{"x":"y"}'::jsonb;
186 SELECT '{"x":"y"}'::jsonb <> '{"x":"z"}'::jsonb;
187
188 -- containment
189 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
190 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":null}');
191 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "g":null}');
192 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"g":null}');
193 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"c"}');
194 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
195 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":"q"}');
196 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
197 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":null}';
198 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "g":null}';
199 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"g":null}';
200 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"c"}';
201 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
202 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":"q"}';
203
204 SELECT '[1,2]'::jsonb @> '[1,2,2]'::jsonb;
205 SELECT '[1,1,2]'::jsonb @> '[1,2,2]'::jsonb;
206 SELECT '[[1,2]]'::jsonb @> '[[1,2,2]]'::jsonb;
207 SELECT '[1,2,2]'::jsonb <@ '[1,2]'::jsonb;
208 SELECT '[1,2,2]'::jsonb <@ '[1,1,2]'::jsonb;
209 SELECT '[[1,2,2]]'::jsonb <@ '[[1,2]]'::jsonb;
210
211 SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
212 SELECT jsonb_contained('{"a":"b", "c":null}', '{"a":"b", "b":1, "c":null}');
213 SELECT jsonb_contained('{"a":"b", "g":null}', '{"a":"b", "b":1, "c":null}');
214 SELECT jsonb_contained('{"g":null}', '{"a":"b", "b":1, "c":null}');
215 SELECT jsonb_contained('{"a":"c"}', '{"a":"b", "b":1, "c":null}');
216 SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
217 SELECT jsonb_contained('{"a":"b", "c":"q"}', '{"a":"b", "b":1, "c":null}');
218 SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
219 SELECT '{"a":"b", "c":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
220 SELECT '{"a":"b", "g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
221 SELECT '{"g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
222 SELECT '{"a":"c"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
223 SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
224 SELECT '{"a":"b", "c":"q"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
225 -- Raw scalar may contain another raw scalar, array may contain a raw scalar
226 SELECT '[5]'::jsonb @> '[5]';
227 SELECT '5'::jsonb @> '5';
228 SELECT '[5]'::jsonb @> '5';
229 -- But a raw scalar cannot contain an array
230 SELECT '5'::jsonb @> '[5]';
231 -- In general, one thing should always contain itself. Test array containment:
232 SELECT '["9", ["7", "3"], 1]'::jsonb @> '["9", ["7", "3"], 1]'::jsonb;
233 SELECT '["9", ["7", "3"], ["1"]]'::jsonb @> '["9", ["7", "3"], ["1"]]'::jsonb;
234 -- array containment string matching confusion bug
235 SELECT '{ "name": "Bob", "tags": [ "enim", "qui"]}'::jsonb @> '{"tags":["qu"]}';
236
237 -- array length
238 SELECT jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
239 SELECT jsonb_array_length('[]');
240 SELECT jsonb_array_length('{"f1":1,"f2":[5,6]}');
241 SELECT jsonb_array_length('4');
242
243 -- each
244 SELECT jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
245 SELECT jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
246 SELECT * FROM jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
247 SELECT * FROM jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
248
249 SELECT jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
250 SELECT jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
251 SELECT * FROM jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
252 SELECT * FROM jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
253
254 -- exists
255 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'a');
256 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'b');
257 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'c');
258 SELECT jsonb_exists('{"a":"null", "b":"qq"}', 'a');
259 SELECT jsonb '{"a":null, "b":"qq"}' ? 'a';
260 SELECT jsonb '{"a":null, "b":"qq"}' ? 'b';
261 SELECT jsonb '{"a":null, "b":"qq"}' ? 'c';
262 SELECT jsonb '{"a":"null", "b":"qq"}' ? 'a';
263 -- array exists - array elements should behave as keys
264 SELECT count(*) from testjsonb  WHERE j->'array' ? 'bar';
265 -- type sensitive array exists - should return no rows (since "exists" only
266 -- matches strings that are either object keys or array elements)
267 SELECT count(*) from testjsonb  WHERE j->'array' ? '5'::text;
268 -- However, a raw scalar is *contained* within the array
269 SELECT count(*) from testjsonb  WHERE j->'array' @> '5'::jsonb;
270
271 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['a','b']);
272 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['b','a']);
273 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','a']);
274 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','d']);
275 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', '{}'::text[]);
276 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['a','b'];
277 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['b','a'];
278 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','a'];
279 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','d'];
280 SELECT jsonb '{"a":null, "b":"qq"}' ?| '{}'::text[];
281
282 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['a','b']);
283 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['b','a']);
284 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','a']);
285 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','d']);
286 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', '{}'::text[]);
287 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','b'];
288 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['b','a'];
289 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','a'];
290 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','d'];
291 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','a', 'b', 'b', 'b'];
292 SELECT jsonb '{"a":null, "b":"qq"}' ?& '{}'::text[];
293
294 -- typeof
295 SELECT jsonb_typeof('{}') AS object;
296 SELECT jsonb_typeof('{"c":3,"p":"o"}') AS object;
297 SELECT jsonb_typeof('[]') AS array;
298 SELECT jsonb_typeof('["a", 1]') AS array;
299 SELECT jsonb_typeof('null') AS "null";
300 SELECT jsonb_typeof('1') AS number;
301 SELECT jsonb_typeof('-1') AS number;
302 SELECT jsonb_typeof('1.0') AS number;
303 SELECT jsonb_typeof('1e2') AS number;
304 SELECT jsonb_typeof('-1.0') AS number;
305 SELECT jsonb_typeof('true') AS boolean;
306 SELECT jsonb_typeof('false') AS boolean;
307 SELECT jsonb_typeof('"hello"') AS string;
308 SELECT jsonb_typeof('"true"') AS string;
309 SELECT jsonb_typeof('"1.0"') AS string;
310
311 -- jsonb_build_array, jsonb_build_object, jsonb_object_agg
312
313 SELECT jsonb_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
314
315 SELECT jsonb_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
316
317 SELECT jsonb_build_object(
318        'a', jsonb_build_object('b',false,'c',99),
319        'd', jsonb_build_object('e',array[9,8,7]::int[],
320            'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
321
322
323 -- empty objects/arrays
324 SELECT jsonb_build_array();
325
326 SELECT jsonb_build_object();
327
328 -- make sure keys are quoted
329 SELECT jsonb_build_object(1,2);
330
331 -- keys must be scalar and not null
332 SELECT jsonb_build_object(null,2);
333
334 SELECT jsonb_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
335
336 SELECT jsonb_build_object(json '{"a":1,"b":2}', 3);
337
338 SELECT jsonb_build_object('{1,2,3}'::int[], 3);
339
340 CREATE TEMP TABLE foo (serial_num int, name text, type text);
341 INSERT INTO foo VALUES (847001,'t15','GE1043');
342 INSERT INTO foo VALUES (847002,'t16','GE1043');
343 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
344
345 SELECT jsonb_build_object('turbines',jsonb_object_agg(serial_num,jsonb_build_object('name',name,'type',type)))
346 FROM foo;
347
348 SELECT jsonb_object_agg(name, type) FROM foo;
349
350 INSERT INTO foo VALUES (999999, NULL, 'bar');
351 SELECT jsonb_object_agg(name, type) FROM foo;
352
353 -- jsonb_object
354
355 -- one dimension
356 SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
357
358 -- same but with two dimensions
359 SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
360
361 -- odd number error
362 SELECT jsonb_object('{a,b,c}');
363
364 -- one column error
365 SELECT jsonb_object('{{a},{b}}');
366
367 -- too many columns error
368 SELECT jsonb_object('{{a,b,c},{b,c,d}}');
369
370 -- too many dimensions error
371 SELECT jsonb_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
372
373 --two argument form of jsonb_object
374
375 select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
376
377 -- too many dimensions
378 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"}}');
379
380 -- mismatched dimensions
381
382 select jsonb_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
383
384 select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
385
386 -- null key error
387
388 select jsonb_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
389
390 -- empty key is allowed
391
392 select jsonb_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
393
394
395
396 -- extract_path, extract_path_as_text
397 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
398 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
399 SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
400 SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
401 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
402 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
403 SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
404 SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
405
406 -- extract_path nulls
407 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_false;
408 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_true;
409 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_false;
410 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_true;
411
412 -- extract_path operators
413 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f4','f6'];
414 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2'];
415 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','0'];
416 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','1'];
417
418 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f4','f6'];
419 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2'];
420 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','0'];
421 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
422
423 -- corner cases for same
424 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
425 select '[1,2,3]'::jsonb #> '{}';
426 select '"foo"'::jsonb #> '{}';
427 select '42'::jsonb #> '{}';
428 select 'null'::jsonb #> '{}';
429 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
430 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
431 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
432 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
433 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c'];
434 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c','d'];
435 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','z','c'];
436 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','1','b'];
437 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','z','b'];
438 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['1','b'];
439 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
440 select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
441 select '"foo"'::jsonb #> array['z'];
442 select '42'::jsonb #> array['f2'];
443 select '42'::jsonb #> array['0'];
444
445 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
446 select '[1,2,3]'::jsonb #>> '{}';
447 select '"foo"'::jsonb #>> '{}';
448 select '42'::jsonb #>> '{}';
449 select 'null'::jsonb #>> '{}';
450 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
451 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
452 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
453 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
454 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c'];
455 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c','d'];
456 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','z','c'];
457 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','1','b'];
458 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','z','b'];
459 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['1','b'];
460 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
461 select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
462 select '"foo"'::jsonb #>> array['z'];
463 select '42'::jsonb #>> array['f2'];
464 select '42'::jsonb #>> array['0'];
465
466 -- array_elements
467 SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
468 SELECT * FROM jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]') q;
469 SELECT jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
470 SELECT * FROM jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
471
472 -- populate_record
473 CREATE TYPE jbpop AS (a text, b int, c timestamp);
474
475 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
476 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
477
478 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
479 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
480
481 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":[100,200,false],"x":43.2}') q;
482 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":[100,200,false],"x":43.2}') q;
483 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"c":[100,200,false],"x":43.2}') q;
484
485 -- populate_recordset
486 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
487 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;
488 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
489 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;
490 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;
491 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;
492
493 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
494 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;
495 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;
496
497 -- handling of unicode surrogate pairs
498
499 SELECT octet_length((jsonb '{ "a":  "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8;
500 SELECT jsonb '{ "a":  "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
501 SELECT jsonb '{ "a":  "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
502 SELECT jsonb '{ "a":  "\ud83dX" }' -> 'a'; -- orphan high surrogate
503 SELECT jsonb '{ "a":  "\ude04X" }' -> 'a'; -- orphan low surrogate
504
505 -- handling of simple unicode escapes
506
507 SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' as correct_in_utf8;
508 SELECT jsonb '{ "a":  "dollar \u0024 character" }' as correct_everywhere;
509 SELECT jsonb '{ "a":  "dollar \\u0024 character" }' as not_an_escape;
510 SELECT jsonb '{ "a":  "null \u0000 escape" }' as fails;
511 SELECT jsonb '{ "a":  "null \\u0000 escape" }' as not_an_escape;
512
513 SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
514 SELECT jsonb '{ "a":  "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
515 SELECT jsonb '{ "a":  "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
516 SELECT jsonb '{ "a":  "null \u0000 escape" }' ->> 'a' as fails;
517 SELECT jsonb '{ "a":  "null \\u0000 escape" }' ->> 'a' as not_an_escape;
518
519 -- jsonb_to_record and jsonb_to_recordset
520
521 select * from jsonb_to_record('{"a":1,"b":"foo","c":"bar"}')
522     as x(a int, b text, d text);
523
524 select * from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
525     as x(a int, b text, c boolean);
526
527 -- indexing
528 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
529 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
530 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
531 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
532 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
533 SELECT count(*) FROM testjsonb WHERE j ? 'public';
534 SELECT count(*) FROM testjsonb WHERE j ? 'bar';
535 SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
536 SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
537
538 CREATE INDEX jidx ON testjsonb USING gin (j);
539 SET enable_seqscan = off;
540
541 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
542 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
543 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
544 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
545 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
546 SELECT count(*) FROM testjsonb WHERE j @> '{"array":["foo"]}';
547 SELECT count(*) FROM testjsonb WHERE j @> '{"array":["bar"]}';
548 -- exercise GIN_SEARCH_MODE_ALL
549 SELECT count(*) FROM testjsonb WHERE j @> '{}';
550 SELECT count(*) FROM testjsonb WHERE j ? 'public';
551 SELECT count(*) FROM testjsonb WHERE j ? 'bar';
552 SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
553 SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
554
555 -- array exists - array elements should behave as keys (for GIN index scans too)
556 CREATE INDEX jidx_array ON testjsonb USING gin((j->'array'));
557 SELECT count(*) from testjsonb  WHERE j->'array' ? 'bar';
558 -- type sensitive array exists - should return no rows (since "exists" only
559 -- matches strings that are either object keys or array elements)
560 SELECT count(*) from testjsonb  WHERE j->'array' ? '5'::text;
561 -- However, a raw scalar is *contained* within the array
562 SELECT count(*) from testjsonb  WHERE j->'array' @> '5'::jsonb;
563
564 RESET enable_seqscan;
565
566 SELECT count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow;
567 SELECT key, count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow GROUP BY key ORDER BY count DESC, key;
568
569 -- sort/hash
570 SELECT count(distinct j) FROM testjsonb;
571 SET enable_hashagg = off;
572 SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
573 SET enable_hashagg = on;
574 SET enable_sort = off;
575 SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
576 SELECT distinct * FROM (values (jsonb '{}' || ''::text),('{}')) v(j);
577 SET enable_sort = on;
578
579 RESET enable_hashagg;
580 RESET enable_sort;
581
582 DROP INDEX jidx;
583 DROP INDEX jidx_array;
584 -- btree
585 CREATE INDEX jidx ON testjsonb USING btree (j);
586 SET enable_seqscan = off;
587
588 SELECT count(*) FROM testjsonb WHERE j > '{"p":1}';
589 SELECT count(*) FROM testjsonb WHERE j = '{"pos":98, "line":371, "node":"CBA", "indexed":true}';
590
591 --gin path opclass
592 DROP INDEX jidx;
593 CREATE INDEX jidx ON testjsonb USING gin (j jsonb_path_ops);
594 SET enable_seqscan = off;
595
596 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
597 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
598 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
599 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
600 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
601 -- exercise GIN_SEARCH_MODE_ALL
602 SELECT count(*) FROM testjsonb WHERE j @> '{}';
603
604 RESET enable_seqscan;
605 DROP INDEX jidx;
606
607 -- nested tests
608 SELECT '{"ff":{"a":12,"b":16}}'::jsonb;
609 SELECT '{"ff":{"a":12,"b":16},"qq":123}'::jsonb;
610 SELECT '{"aa":["a","aaa"],"qq":{"a":12,"b":16,"c":["c1","c2"],"d":{"d1":"d1","d2":"d2","d1":"d3"}}}'::jsonb;
611 SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2"],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
612 SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2",["c3"],{"c4":4}],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
613 SELECT '{"ff":["a","aaa"]}'::jsonb;
614
615 SELECT
616   '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'ff',
617   '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'qq',
618   ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'Y') IS NULL AS f,
619   ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb ->> 'Y') IS NULL AS t,
620    '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'x';
621
622 -- nested containment
623 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1,2]}';
624 SELECT '{"a":[2,1],"c":"b"}'::jsonb @> '{"a":[1,2]}';
625 SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":[1,2]}';
626 SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":[1,2]}';
627 SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
628 SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
629 SELECT '["a","b"]'::jsonb @> '["a","b","c","b"]';
630 SELECT '["a","b","c","b"]'::jsonb @> '["a","b"]';
631 SELECT '["a","b","c",[1,2]]'::jsonb @> '["a",[1,2]]';
632 SELECT '["a","b","c",[1,2]]'::jsonb @> '["b",[1,2]]';
633
634 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1]}';
635 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[2]}';
636 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[3]}';
637
638 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"c":3}]}';
639 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}';
640 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}';
641 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}';
642
643 -- nested object field / array index lookup
644 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n';
645 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a';
646 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'b';
647 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'c';
648 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd';
649 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd' -> '1';
650 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
651 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
652
653 SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
654 SELECT '["a","b","c",[1,2],null]'::jsonb -> 1;
655 SELECT '["a","b","c",[1,2],null]'::jsonb -> 2;
656 SELECT '["a","b","c",[1,2],null]'::jsonb -> 3;
657 SELECT '["a","b","c",[1,2],null]'::jsonb -> 3 -> 1;
658 SELECT '["a","b","c",[1,2],null]'::jsonb -> 4;
659 SELECT '["a","b","c",[1,2],null]'::jsonb -> 5;
660 SELECT '["a","b","c",[1,2],null]'::jsonb -> -1;
661 SELECT '["a","b","c",[1,2],null]'::jsonb -> -5;
662 SELECT '["a","b","c",[1,2],null]'::jsonb -> -6;
663
664 --nested path extraction
665 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{0}';
666 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{a}';
667 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c}';
668 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,0}';
669 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,1}';
670 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,2}';
671 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,3}';
672 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-1}';
673 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-3}';
674 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-4}';
675
676 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{0}';
677 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{3}';
678 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4}';
679 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4,5}';
680
681 --nested exists
682 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'n';
683 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'a';
684 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'b';
685 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'c';
686 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'd';
687 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'e';
688
689 -- jsonb_strip_nulls
690
691 select jsonb_strip_nulls(null);
692
693 select jsonb_strip_nulls('1');
694
695 select jsonb_strip_nulls('"a string"');
696
697 select jsonb_strip_nulls('null');
698
699 select jsonb_strip_nulls('[1,2,null,3,4]');
700
701 select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
702
703 select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
704
705 -- an empty object is not null and should not be stripped
706 select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
707
708
709 select jsonb_pretty('{"a": "test", "b": [1, 2, 3], "c": "test3", "d":{"dd": "test4", "dd2":{"ddd": "test5"}}}');
710 select jsonb_pretty('[{"f1":1,"f2":null},2,null,[[{"x":true},6,7],8],3]');
711 select jsonb_pretty('{"a":["b", "c"], "d": {"e":"f"}}');
712
713 select jsonb_concat('{"d": "test", "a": [1, 2]}', '{"g": "test2", "c": {"c1":1, "c2":2}}');
714
715 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
716 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aq":"l"}';
717 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aa":"l"}';
718 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{}';
719
720 select '["a", "b"]'::jsonb || '["c"]';
721 select '["a", "b"]'::jsonb || '["c", "d"]';
722 select '["c"]' || '["a", "b"]'::jsonb;
723
724 select '["a", "b"]'::jsonb || '"c"';
725 select '"c"' || '["a", "b"]'::jsonb;
726
727 select '[]'::jsonb || '["a"]'::jsonb;
728 select '[]'::jsonb || '"a"'::jsonb;
729 select '"b"'::jsonb || '"a"'::jsonb;
730 select '{}'::jsonb || '{"a":"b"}'::jsonb;
731 select '[]'::jsonb || '{"a":"b"}'::jsonb;
732 select '{"a":"b"}'::jsonb || '[]'::jsonb;
733
734 select '"a"'::jsonb || '{"a":1}';
735 select '{"a":1}' || '"a"'::jsonb;
736
737 select '["a", "b"]'::jsonb || '{"c":1}';
738 select '{"c": 1}'::jsonb || '["a", "b"]';
739
740 select '{}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
741
742 select pg_column_size('{}'::jsonb || '{}'::jsonb) = pg_column_size('{}'::jsonb);
743 select pg_column_size('{"aa":1}'::jsonb || '{"b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
744 select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
745 select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
746
747 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a');
748 select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a');
749 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b');
750 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c');
751 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd');
752 select '{"a":1 , "b":2, "c":3}'::jsonb - 'a';
753 select '{"a":null , "b":2, "c":3}'::jsonb - 'a';
754 select '{"a":1 , "b":2, "c":3}'::jsonb - 'b';
755 select '{"a":1 , "b":2, "c":3}'::jsonb - 'c';
756 select '{"a":1 , "b":2, "c":3}'::jsonb - 'd';
757 select pg_column_size('{"a":1 , "b":2, "c":3}'::jsonb - 'b') = pg_column_size('{"a":1, "b":2}'::jsonb);
758
759 select '["a","b","c"]'::jsonb - 3;
760 select '["a","b","c"]'::jsonb - 2;
761 select '["a","b","c"]'::jsonb - 1;
762 select '["a","b","c"]'::jsonb - 0;
763 select '["a","b","c"]'::jsonb - -1;
764 select '["a","b","c"]'::jsonb - -2;
765 select '["a","b","c"]'::jsonb - -3;
766 select '["a","b","c"]'::jsonb - -4;
767
768 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
769 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
770 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
771 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
772
773 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
774 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
775 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
776 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
777
778 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
779 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
780
781 select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}');
782 select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}');
783 select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{d,1,0}');
784
785 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{n}';
786 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1}';
787 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript
788 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}';
789
790
791 -- empty structure and error conditions for delete and replace
792
793 select '"a"'::jsonb - 'a'; -- error
794 select '{}'::jsonb - 'a';
795 select '[]'::jsonb - 'a';
796 select '"a"'::jsonb - 1; -- error
797 select '{}'::jsonb -  1; -- error
798 select '[]'::jsonb - 1;
799 select '"a"'::jsonb #- '{a}'; -- error
800 select '{}'::jsonb #- '{a}';
801 select '[]'::jsonb #- '{a}';
802 select jsonb_set('"a"','{a}','"b"'); --error
803 select jsonb_set('{}','{a}','"b"', false);
804 select jsonb_set('[]','{1}','"b"', false);
805
806 -- jsonb_set adding instead of replacing
807
808 -- prepend to array
809 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,-33}','{"foo":123}');
810 -- append to array
811 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,33}','{"foo":123}');
812 -- check nesting levels addition
813 select jsonb_set('{"a":1,"b":[4,5,[0,1,2],6,7],"c":{"d":4}}','{b,2,33}','{"foo":123}');
814 -- add new key
815 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{c,e}','{"foo":123}');
816 -- adding doesn't do anything if elements before last aren't present
817 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,-33}','{"foo":123}');
818 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,y}','{"foo":123}');
819 -- add to empty object
820 select jsonb_set('{}','{x}','{"foo":123}');
821 --add to empty array
822 select jsonb_set('[]','{0}','{"foo":123}');
823 select jsonb_set('[]','{99}','{"foo":123}');
824 select jsonb_set('[]','{-99}','{"foo":123}');
825 select jsonb_set('{"a": [1, 2, 3]}', '{a, non_integer}', '"new_value"');
826 select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, non_integer}', '"new_value"');
827 select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, NULL}', '"new_value"');