]> git.8kb.co.uk Git - postgresql/pg_jsonb_opx/blob - expected/jsonb_opx.out
e11187b6470ab64b103db3be3b8b37556e04b77e
[postgresql/pg_jsonb_opx] / expected / jsonb_opx.out
1 CREATE EXTENSION jsonb_opx;
2 -------------------------------------------------------------------------------
3 -- Tests for jsonb - text
4 -------------------------------------------------------------------------------
5 SELECT '["a", "b"]'::jsonb - 'b'::text;
6  ?column? 
7 ----------
8  ["a"]
9 (1 row)
10
11 SELECT '["a"]'::jsonb - 'b'::text;
12  ?column? 
13 ----------
14  ["a"]
15 (1 row)
16
17 SELECT '"a"'::jsonb - 'b'::text;
18  ?column? 
19 ----------
20  "a"
21 (1 row)
22
23 SELECT '[1]'::jsonb - 'b'::text;
24  ?column? 
25 ----------
26  [1]
27 (1 row)
28
29 SELECT '1'::jsonb - 'b'::text;
30  ?column? 
31 ----------
32  1
33 (1 row)
34
35 SELECT '["a", {"a":1}, "b"]'::jsonb - 'b'::text;
36     ?column?     
37 -----------------
38  ["a", {"a": 1}]
39 (1 row)
40
41 -- text deletion from array containers will only delete string types currently
42 SELECT '[1, "1", "2", 2]'::jsonb - '2'::text;
43   ?column?   
44 -------------
45  [1, "1", 2]
46 (1 row)
47
48 -- simple text deletion from an object container
49 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - 'b'::text;
50      ?column?     
51 ------------------
52  {"a": 1, "c": 3}
53 (1 row)
54
55 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - 'b '::text;
56          ?column?         
57 --------------------------
58  {"a": 1, "b": 2, "c": 3}
59 (1 row)
60
61 SELECT '{"a": 1, "b": 2, "c": {"b": 3}}'::jsonb - 'b'::text;
62         ?column?         
63 -------------------------
64  {"a": 1, "c": {"b": 3}}
65 (1 row)
66
67 SELECT '{"a": 1, "b": 2, "c": {"b": [1,2,3]}}'::jsonb - 'b'::text;
68             ?column?             
69 ---------------------------------
70  {"a": 1, "c": {"b": [1, 2, 3]}}
71 (1 row)
72
73 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - 'b'::text;
74          ?column?         
75 --------------------------
76  {"a": 1, "c": [1, 2, 3]}
77 (1 row)
78
79 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - 'c'::text;
80      ?column?     
81 ------------------
82  {"a": 1, "b": 2}
83 (1 row)
84
85 -- simple text deletion from an object container should only match keys
86 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - '3'::text;
87          ?column?         
88 --------------------------
89  {"a": 1, "b": 2, "c": 3}
90 (1 row)
91
92 -- deleting everything always results in an empty container
93 SELECT '["a"]'::jsonb - 'a'::text;
94  ?column? 
95 ----------
96  []
97 (1 row)
98
99 SELECT '{"a":1}'::jsonb - 'a'::text;
100  ?column? 
101 ----------
102  {}
103 (1 row)
104
105 -- even for scalars, but this should perhaps error
106 SELECT '"a"'::jsonb - 'a'::text;
107  ?column? 
108 ----------
109  []
110 (1 row)
111
112 -- others
113 SELECT '["1", "2", true, false]'::jsonb - '2'::text;
114       ?column?      
115 --------------------
116  ["1", true, false]
117 (1 row)
118
119 SELECT '["1", "2", "2", "2"]'::jsonb - '2'::text;
120  ?column? 
121 ----------
122  ["1"]
123 (1 row)
124
125 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - 'a'::text;
126        ?column?        
127 -----------------------
128  [2, {"a": 1, "b": 2}]
129 (1 row)
130
131 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - 'a'::text;
132  ?column? 
133 ----------
134  {"d": 2}
135 (1 row)
136
137 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - 'd'::text;
138               ?column?              
139 ------------------------------------
140  {"a": {"b": 3, "c": [1, 2, 3, 4]}}
141 (1 row)
142
143 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - 'b'::text;
144                   ?column?                  
145 --------------------------------------------
146  {"a": {"b": 3, "c": [1, 2, 3, 4]}, "d": 2}
147 (1 row)
148
149 SELECT '{"a":{"b":{"c":[1,[2,3,[4]],{"d":1}]}, "c":[1,2,3,4]}, "d":2}'::jsonb - 'a'::text;
150  ?column? 
151 ----------
152  {"d": 2}
153 (1 row)
154
155 -------------------------------------------------------------------------------
156 -- Tests for jsonb - text[]
157 -------------------------------------------------------------------------------
158 -- text deletion from array containers will only delete string types currently
159 SELECT '[1, "1", "2", 2]'::jsonb - array['1','2'];
160  ?column? 
161 ----------
162  [1, 2]
163 (1 row)
164
165 -- simple text[] deletion from an object container
166 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a','b'];
167  ?column? 
168 ----------
169  {"c": 3}
170 (1 row)
171
172 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a ','b ',' c'];
173          ?column?         
174 --------------------------
175  {"a": 1, "b": 2, "c": 3}
176 (1 row)
177
178 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a','b','c'];
179  ?column? 
180 ----------
181  {}
182 (1 row)
183
184 SELECT '{"a": 1, "b": 2, "c": {"b": 3}}'::jsonb - ARRAY['a','b']; 
185     ?column?     
186 -----------------
187  {"c": {"b": 3}}
188 (1 row)
189
190 SELECT '{"a": 1, "b": 2, "c": {"b": [1,2,3]}}'::jsonb - ARRAY['a','b'];
191         ?column?         
192 -------------------------
193  {"c": {"b": [1, 2, 3]}}
194 (1 row)
195
196 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - ARRAY['a','b'];
197      ?column?     
198 ------------------
199  {"c": [1, 2, 3]}
200 (1 row)
201
202 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - ARRAY['a','c'];
203  ?column? 
204 ----------
205  {"b": 2}
206 (1 row)
207
208 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY['b','d'];
209               ?column?              
210 ------------------------------------
211  {"a": {"b": 3, "c": [1, 2, 3, 4]}}
212 (1 row)
213
214 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY['b','a'];
215  ?column? 
216 ----------
217  {"d": 2}
218 (1 row)
219
220 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY['a','d'];
221  ?column? 
222 ----------
223  {}
224 (1 row)
225
226 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb - ARRAY['a','d'];
227  ?column? 
228 ----------
229  {}
230 (1 row)
231
232 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":[2,[2],[1,2,{"a":2},{"b":[1,[2]]}]]}'::jsonb - ARRAY['a'];
233                       ?column?                      
234 ----------------------------------------------------
235  {"d": [2, [2], [1, 2, {"a": 2}, {"b": [1, [2]]}]]}
236 (1 row)
237
238 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":[2,[2],[1,2,{"a":2},{"b":[1,[2]]}]]}'::jsonb - ARRAY['d'];
239                           ?column?                           
240 -------------------------------------------------------------
241  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}}
242 (1 row)
243
244 SELECT '{"a":{"b":{"c":[1,[2,3,[4]],{"d":1}]}, "c":[1,2,3,4]}, "d":2}'::jsonb - '{a}'::text[]; 
245  ?column? 
246 ----------
247  {"d": 2}
248 (1 row)
249
250 -- simple text[] deletion from an object container should only match keys or nulls
251 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['1',' 2'];
252          ?column?         
253 --------------------------
254  {"a": 1, "b": 2, "c": 3}
255 (1 row)
256
257 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '{a}'::text[];
258        ?column?        
259 -----------------------
260  [2, {"a": 1, "b": 2}]
261 (1 row)
262
263 -- here we treat SQL nulls and json nulls as equal - bad?
264 SELECT '["1",null,2]'::jsonb - ARRAY[null];
265  ?column? 
266 ----------
267  ["1", 2]
268 (1 row)
269
270 SELECT '["1",2]'::jsonb - ARRAY[null];
271  ?column? 
272 ----------
273  ["1", 2]
274 (1 row)
275
276 -------------------------------------------------------------------------------
277 -- Tests for jsonb - jsonb
278 -------------------------------------------------------------------------------
279 -- jsonb deletion from an object should match on key/value
280 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
281      ?column?     
282 ------------------
283  {"a": 1, "c": 3}
284 (1 row)
285
286 -- jsonb deletion from an array should only match on key
287 SELECT '["a", "b", "c"]'::jsonb - '{"a": 4, "b": 2}'::jsonb;
288  ?column? 
289 ----------
290  ["c"]
291 (1 row)
292
293 -- jsonb deletion from nested objects should not be part matched
294 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
295         ?column?         
296 -------------------------
297  {"c": 3, "d": {"a": 4}}
298 (1 row)
299
300 -- but a match of all nested values should
301 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"d": {"a": 4}, "b": 2}'::jsonb;
302      ?column?     
303 ------------------
304  {"a": 4, "c": 3}
305 (1 row)
306
307 SELECT '{"a":{"b":{"c":[1,[2,3,[4]],{"d":1}]}, "c":[1,2,3,4]}, "d":2}'::jsonb - '{"d":2}'::jsonb;
308                               ?column?                              
309 --------------------------------------------------------------------
310  {"a": {"b": {"c": [1, [2, 3, [4]], {"d": 1}]}, "c": [1, 2, 3, 4]}}
311 (1 row)
312
313 -- jsonb nulls are equal
314 SELECT '{"a": 1, "b": 2, "c": null}'::jsonb - '{"a": 4, "c": null}'::jsonb;
315      ?column?     
316 ------------------
317  {"a": 1, "b": 2}
318 (1 row)
319
320 -- others
321 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": false}}'::jsonb - '{"d": {"a": false}, "b": 2}'::jsonb;
322      ?column?     
323 ------------------
324  {"a": 4, "c": 3}
325 (1 row)
326
327 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}}'::jsonb - '{"a": "test2", "c": {"a": false}, "b": 2.2}'::jsonb;
328    ?column?    
329 ---------------
330  {"a": "test"}
331 (1 row)
332
333 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": true}, "d":false, "e":[1,2,3]}'::jsonb;
334                                 ?column?                                 
335 -------------------------------------------------------------------------
336  {"a": "test", "b": 2.2, "c": {"a": false}, "d": true, "e": [1, 2, "a"]}
337 (1 row)
338
339 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.3, "c": {"a": true}, "d":false, "e":[1,2,3]}'::jsonb;
340                           ?column?                          
341 ------------------------------------------------------------
342  {"b": 2.2, "c": {"a": false}, "d": true, "e": [1, 2, "a"]}
343 (1 row)
344
345 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.2, "c": {"a": true}, "d":false, "e":[1,2,3]}'::jsonb;
346                            ?column?                            
347 ---------------------------------------------------------------
348  {"a": "test", "c": {"a": false}, "d": true, "e": [1, 2, "a"]}
349 (1 row)
350
351 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": false}, "d":false, "e":[1,2,3]}'::jsonb;
352                        ?column?                       
353 ------------------------------------------------------
354  {"a": "test", "b": 2.2, "d": true, "e": [1, 2, "a"]}
355 (1 row)
356
357 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": true}, "d":true, "e":[1,2,3]}'::jsonb;
358                            ?column?                           
359 --------------------------------------------------------------
360  {"a": "test", "b": 2.2, "c": {"a": false}, "e": [1, 2, "a"]}
361 (1 row)
362
363 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": true}, "d":false, "e":[1,"a",2]}'::jsonb;
364                                 ?column?                                 
365 -------------------------------------------------------------------------
366  {"a": "test", "b": 2.2, "c": {"a": false}, "d": true, "e": [1, 2, "a"]}
367 (1 row)
368
369 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test2", "b": 2.3, "c": {"a": true}, "d":false, "e":[1,2,"a"]}'::jsonb;
370                        ?column?                        
371 -------------------------------------------------------
372  {"a": "test", "b": 2.2, "c": {"a": false}, "d": true}
373 (1 row)
374
375 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.2, "c": {"a": true}, "d":false, "e":[1,2,3]}'::jsonb;
376                      ?column?                     
377 --------------------------------------------------
378  {"c": {"a": false}, "d": true, "e": [1, 2, "a"]}
379 (1 row)
380
381 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.2, "c": {"a": false}, "d":false, "e":[1,2,3]}'::jsonb;
382            ?column?            
383 -------------------------------
384  {"d": true, "e": [1, 2, "a"]}
385 (1 row)
386
387 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,3]}'::jsonb;
388       ?column?      
389 --------------------
390  {"e": [1, 2, "a"]}
391 (1 row)
392
393 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb - '{"a": "test", "b": 2.2, "c": {"a": false}, "d":true, "e":[1,2,"a"]}'::jsonb;
394  ?column? 
395 ----------
396  {}
397 (1 row)
398
399 -- known issues !!!!
400 -- lookups of lhs values in rhs jsonb use findJsonbValueFromContainer which does 
401 -- not allow looking up non-scalar elements resulting in "invalid jsonb scalar type"
402 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[[1]]'::jsonb;
403 ERROR:  invalid jsonb scalar type
404 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[{"a":1}]'::jsonb;
405 ERROR:  invalid jsonb scalar type
406 -------------------------------------------------------------------------------
407 -- Tests for jsonb || jsonb
408 -------------------------------------------------------------------------------
409 -- duplicates should automatically be removed by lower level logic
410 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb || '{"a": 4, "b": 2, "d": 4}'::jsonb;
411              ?column?             
412 ----------------------------------
413  {"a": 4, "b": 2, "c": 3, "d": 4}
414 (1 row)
415
416 SELECT '{"a": 1, "b": null, "c": 3}'::jsonb || '{"a": 4, "b": null, "d": 4}'::jsonb;
417               ?column?               
418 -------------------------------------
419  {"a": 4, "b": null, "c": 3, "d": 4}
420 (1 row)
421
422 -- concatentation of arrays
423 SELECT '["a", "b"]'::jsonb || '["c"]'::jsonb;
424     ?column?     
425 -----------------
426  ["a", "b", "c"]
427 (1 row)
428
429 -- concatentation of scalars and arrays should be wrapped into arrays
430 SELECT '["a", "b"]'::jsonb || '"c"'::jsonb;
431     ?column?     
432 -----------------
433  ["a", "b", "c"]
434 (1 row)
435
436 -- likewise concatentation of objects and arrays should be wrapped into arrays
437 SELECT '["a", "b"]'::jsonb || '{"a": 4, "b": 2}'::jsonb;
438            ?column?           
439 ------------------------------
440  ["a", "b", {"a": 4, "b": 2}]
441 (1 row)
442
443 -- and all concatentation should be in natural order supplied
444 SELECT '{"a": 4, "b": 2}'::jsonb || '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
445                 ?column?                
446 ----------------------------------------
447  [{"a": 4, "b": 2}, "a", "b", "c", "d"]
448 (1 row)
449
450 -- others
451 SELECT 'false'::jsonb || '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
452           ?column?           
453 -----------------------------
454  [false, "a", "b", "c", "d"]
455 (1 row)
456
457 SELECT '["a","b"]'::jsonb || '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb;
458                         ?column?                        
459 --------------------------------------------------------
460  ["a", "b", {"a": {"b": 3, "c": [1, 2, 3, 4]}, "d": 2}]
461 (1 row)
462
463 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb || '["a","b"]'::jsonb;
464                         ?column?                        
465 --------------------------------------------------------
466  [{"a": {"b": 3, "c": [1, 2, 3, 4]}, "d": 2}, "a", "b"]
467 (1 row)
468
469 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb || '["a",["b","c",["3",1,2,[9,3,{"s":"o"},"x"]]],{"f":9}]'::jsonb;
470                                                                 ?column?                                                                
471 ----------------------------------------------------------------------------------------------------------------------------------------
472  [{"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}, "a", ["b", "c", ["3", 1, 2, [9, 3, {"s": "o"}, "x"]]], {"f": 9}]
473 (1 row)
474
475 SELECT'["a",["b","c",["3",1,2,[9,3,{"s":"o"},"x"]]],{"f":9}]'::jsonb || '["a",["b","c",["3",1,2,[9,3,{"s":"o"},"x"]]],{"f":9}]'::jsonb;
476                                                               ?column?                                                              
477 ------------------------------------------------------------------------------------------------------------------------------------
478  ["a", ["b", "c", ["3", 1, 2, [9, 3, {"s": "o"}, "x"]]], {"f": 9}, "a", ["b", "c", ["3", 1, 2, [9, 3, {"s": "o"}, "x"]]], {"f": 9}]
479 (1 row)
480
481 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb || '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb;
482                               ?column?                               
483 ---------------------------------------------------------------------
484  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
485 (1 row)
486
487 -------------------------------------------------------------------------------
488 -- Tests for jsonb #= jsonb
489 -------------------------------------------------------------------------------
490 -- any keys existing in left argument have values replaced with those from righ 
491 -- argument
492 SELECT '{"a": 1}'::jsonb #= '{"a": [1,2,3,4]}'::jsonb;
493       ?column?       
494 ---------------------
495  {"a": [1, 2, 3, 4]}
496 (1 row)
497
498 SELECT '{"a": 1}'::jsonb #= '{"a": [1,2,3,4], "b":2}'::jsonb;
499       ?column?       
500 ---------------------
501  {"a": [1, 2, 3, 4]}
502 (1 row)
503
504 SELECT '{"a": 1, "b":1}'::jsonb #= '{"a": [1,2,3,4], "b":2}'::jsonb;
505           ?column?           
506 -----------------------------
507  {"a": [1, 2, 3, 4], "b": 2}
508 (1 row)
509
510 SELECT '{"a": 1, "b": 2, "c":[1,2,3], "d":{"test":false}}'::jsonb #= '{"a": [1,2,3,4], "b": {"f":100, "j":{"k":200}}, "c": 4, "d":{"test":true}}'::jsonb;
511                                       ?column?                                      
512 ------------------------------------------------------------------------------------
513  {"a": [1, 2, 3, 4], "b": {"f": 100, "j": {"k": 200}}, "c": 4, "d": {"test": true}}
514 (1 row)
515
516 -- note that as we are matching only keys and replacing values operation on an 
517 -- scalar/array elements effectively does nothing 
518 SELECT '{"a":[1,2], "b":2, "c":12}'::jsonb #= '["a","b","c"]'::jsonb;
519             ?column?            
520 --------------------------------
521  {"a": [1, 2], "b": 2, "c": 12}
522 (1 row)
523
524 SELECT '{"a":[1,2], "b":2, "c":12}'::jsonb #= '[1,2,3]'::jsonb;
525             ?column?            
526 --------------------------------
527  {"a": [1, 2], "b": 2, "c": 12}
528 (1 row)
529
530 SELECT '[1,2,3]'::jsonb #= '[1,2,3,4]'::jsonb;
531  ?column?  
532 -----------
533  [1, 2, 3]
534 (1 row)
535
536 SELECT '"a"'::jsonb #= '{"a":1, "b":2}'::jsonb;
537  ?column? 
538 ----------
539  "a"
540 (1 row)
541
542 SELECT '{"a":1, "b":2}'::jsonb #= '"a"'::jsonb;
543      ?column?     
544 ------------------
545  {"a": 1, "b": 2}
546 (1 row)
547
548 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb  #= '{"a":{"b":3, "c":[1,{"r":[true,{"u":2}]},3,4,5]}}'::jsonb;
549                               ?column?                               
550 ---------------------------------------------------------------------
551  {"a": {"b": 3, "c": [1, {"r": [true, {"u": 2}]}, 3, 4, 5]}, "d": 2}
552 (1 row)
553
554 -------------------------------------------------------------------------------
555 -- Tests for jsonb #- text[] 
556 -------------------------------------------------------------------------------
557 SELECT '"a"'::jsonb #- ARRAY['b'];
558  ?column? 
559 ----------
560  "a"
561 (1 row)
562
563 SELECT '["a"]'::jsonb #- ARRAY['b'];
564  ?column? 
565 ----------
566  ["a"]
567 (1 row)
568
569 SELECT '{"a":1}'::jsonb #- ARRAY['b'];
570  ?column? 
571 ----------
572  {"a": 1}
573 (1 row)
574
575 SELECT '"a"'::jsonb #- ARRAY['a'];
576  ?column? 
577 ----------
578  []
579 (1 row)
580
581 SELECT '["a"]'::jsonb #- ARRAY['a'];
582  ?column? 
583 ----------
584  []
585 (1 row)
586
587 SELECT '{"a":1}'::jsonb #- ARRAY['a'];
588  ?column? 
589 ----------
590  {}
591 (1 row)
592
593 SELECT '["a", "b"]'::jsonb #- ARRAY['a'];
594  ?column? 
595 ----------
596  ["b"]
597 (1 row)
598
599 SELECT '{"a":1, "b":2}'::jsonb #- ARRAY['a'];
600  ?column? 
601 ----------
602  {"b": 2}
603 (1 row)
604
605 SELECT '{"a":{"b":1}, "c":2}'::jsonb #- ARRAY['a'];
606  ?column? 
607 ----------
608  {"c": 2}
609 (1 row)
610
611 SELECT '{"a":[1,2,3,4], "b":2}'::jsonb #- ARRAY['a'];
612  ?column? 
613 ----------
614  {"b": 2}
615 (1 row)
616
617 SELECT '{"a":[1,2,3,4], "b":2}'::jsonb #- ARRAY['b'];
618       ?column?       
619 ---------------------
620  {"a": [1, 2, 3, 4]}
621 (1 row)
622
623 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['c'];
624               ?column?               
625 -------------------------------------
626  {"a": {"b": [1, 2, 3, ["a", "b"]]}}
627 (1 row)
628
629 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a'];
630  ?column? 
631 ----------
632  {"c": 2}
633 (1 row)
634
635 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a','c'];
636                   ?column?                   
637 ---------------------------------------------
638  {"a": {"b": [1, 2, 3, ["a", "b"]]}, "c": 2}
639 (1 row)
640
641 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a','b'];
642      ?column?      
643 -------------------
644  {"a": {}, "c": 2}
645 (1 row)
646
647 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a','b','c'];
648                   ?column?                   
649 ---------------------------------------------
650  {"a": {"b": [1, 2, 3, ["a", "b"]]}, "c": 2}
651 (1 row)
652
653 SELECT '{"a":{"b":{"c":1}, "c":[1,2,3,["a","b"]]}, "d":3}'::jsonb #- ARRAY['a','b','c'];
654                        ?column?                       
655 ------------------------------------------------------
656  {"a": {"b": {}, "c": [1, 2, 3, ["a", "b"]]}, "d": 3}
657 (1 row)
658
659 SELECT '{"a":{"b":[1,2,3,["a","b"]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a','b'];
660               ?column?              
661 ------------------------------------
662  {"a": {"c": [1, 2, 3, 4]}, "d": 2}
663 (1 row)
664
665 SELECT '{"a":{"b":[1,2,3,["a","b"]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a','c'];
666                   ?column?                   
667 ---------------------------------------------
668  {"a": {"b": [1, 2, 3, ["a", "b"]]}, "d": 2}
669 (1 row)
670
671 SELECT '{"a":{"b":[1,2,3,["a","b"]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a',null];
672                             ?column?                            
673 ----------------------------------------------------------------
674  {"a": {"b": [1, 2, 3, ["a", "b"]], "c": [1, 2, 3, 4]}, "d": 2}
675 (1 row)
676
677 SELECT '{"a":{"b":[1,2,3,["a",{"b":3}]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a','b'];
678               ?column?              
679 ------------------------------------
680  {"a": {"c": [1, 2, 3, 4]}, "d": 2}
681 (1 row)
682
683 SELECT '{"a":{"b":3, "d":[1,{"Z":[1,[2,3]]}]}}'::jsonb #- ARRAY['a','d'];
684     ?column?     
685 -----------------
686  {"a": {"b": 3}}
687 (1 row)
688
689 SELECT '["a", {"b":[1,2,3,4,5]}, 1, "c"]'::jsonb #- ARRAY['a'];
690              ?column?             
691 ----------------------------------
692  [{"b": [1, 2, 3, 4, 5]}, 1, "c"]
693 (1 row)
694
695 SELECT '["a", {"b":[1,2,3,4,5]}, 1, "c"]'::jsonb #- ARRAY['c'];
696              ?column?             
697 ----------------------------------
698  ["a", {"b": [1, 2, 3, 4, 5]}, 1]
699 (1 row)
700
701 SELECT '[1,[2,[3,[4,[5,6,7]]]],"a","b"]'::jsonb #- ARRAY['b'];
702               ?column?              
703 ------------------------------------
704  [1, [2, [3, [4, [5, 6, 7]]]], "a"]
705 (1 row)
706
707 SELECT '[1,[2,[3,[4,[5,6,7]]]],"a","b"]'::jsonb #- ARRAY['a'];
708               ?column?              
709 ------------------------------------
710  [1, [2, [3, [4, [5, 6, 7]]]], "b"]
711 (1 row)
712
713 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','b']; 
714                           ?column?                           
715 -------------------------------------------------------------
716  {"a": {"c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
717 (1 row)
718
719 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','c']; 
720         ?column?         
721 -------------------------
722  {"a": {"b": 3}, "d": 2}
723 (1 row)
724
725 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','d'];
726                               ?column?                               
727 ---------------------------------------------------------------------
728  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
729 (1 row)
730
731 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['d'];
732                           ?column?                           
733 -------------------------------------------------------------
734  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}}
735 (1 row)
736
737 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','c','r']; 
738                               ?column?                               
739 ---------------------------------------------------------------------
740  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
741 (1 row)
742
743 SELECT '{"a":{"b":3, "c":{"r":{"u":1}}}, "d":2}'::jsonb #- ARRAY['a','c','r'];  
744              ?column?             
745 ----------------------------------
746  {"a": {"b": 3, "c": {}}, "d": 2}
747 (1 row)
748
749 SELECT '{"a":{"b":3, "c":{"r":{"u":1}}}, "d":2}'::jsonb #- ARRAY['a','c','r','u'];
750                 ?column?                 
751 -----------------------------------------
752  {"a": {"b": 3, "c": {"r": {}}}, "d": 2}
753 (1 row)
754
755 -- expected limitation: cannot call with path deeper than 1 on a non-object
756 SELECT '["a", "b"]'::jsonb #- ARRAY['a','b'];
757 ERROR:  cannot call with path deeper than 1 on a non-object
758 -------------------------------------------------------------------------------
759 -- Tests for jsonb_replace_path jsonb text[] 
760 -------------------------------------------------------------------------------
761 -- if the replacement on an object/array is passed as a scalar/array the value/element
762 -- is replaced
763 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '3'::jsonb);
764  jsonb_replace_path 
765 --------------------
766  {"a": 3, "b": 2}
767 (1 row)
768
769 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '[3]'::jsonb);
770  jsonb_replace_path 
771 --------------------
772  {"a": [3], "b": 2}
773 (1 row)
774
775 SELECT jsonb_replace_path('["a", "b"]', ARRAY['a'], '3'::jsonb);
776  jsonb_replace_path 
777 --------------------
778  [3, "b"]
779 (1 row)
780
781 SELECT jsonb_replace_path('["a", "b"]', ARRAY['a'], '[3]'::jsonb);
782  jsonb_replace_path 
783 --------------------
784  [3, "b"]
785 (1 row)
786
787 -- if the replacement on an object/array is passed as an object the whole key-value
788 -- pair is replaced.  This difference is perhaps confusing, but otherwise there is 
789 -- no way to directly replace a key without deletion and concatenation.
790 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '{"z":3}'::jsonb);
791  jsonb_replace_path 
792 --------------------
793  {"b": 2, "z": 3}
794 (1 row)
795
796 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '{"a":{"z":3}}'::jsonb);
797    jsonb_replace_path    
798 -------------------------
799  {"a": {"z": 3}, "b": 2}
800 (1 row)
801
802 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '{"f":3}'::jsonb);
803  jsonb_replace_path 
804 --------------------
805  {"b": 2, "f": 3}
806 (1 row)
807
808 SELECT jsonb_replace_path('{"a":{"b":1}}', ARRAY['a'], '{"f":3}'::jsonb);
809  jsonb_replace_path 
810 --------------------
811  {"f": 3}
812 (1 row)
813
814 SELECT jsonb_replace_path('{"a":{"b":1}}', ARRAY['a','b'], '{"f":3}'::jsonb);
815  jsonb_replace_path 
816 --------------------
817  {"a": {"f": 3}}
818 (1 row)
819
820 SELECT jsonb_replace_path('{"a":{"b":1, "c":1}}', ARRAY['a','b'], '{"f":3}'::jsonb);
821    jsonb_replace_path    
822 -------------------------
823  {"a": {"c": 1, "f": 3}}
824 (1 row)
825
826 SELECT jsonb_replace_path('{"a":{"b":1, "c":1}}', ARRAY['a','c'], '{"f":3}'::jsonb);
827    jsonb_replace_path    
828 -------------------------
829  {"a": {"b": 1, "f": 3}}
830 (1 row)
831
832 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,2]}}', ARRAY['a','b'], '{"f":3}'::jsonb);
833           jsonb_replace_path          
834 --------------------------------------
835  {"a": {"c": 2, "d": [1, 2], "f": 3}}
836 (1 row)
837
838 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,2]}}', ARRAY['a','c'], '{"f":3}'::jsonb);
839           jsonb_replace_path          
840 --------------------------------------
841  {"a": {"b": 1, "d": [1, 2], "f": 3}}
842 (1 row)
843
844 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,2]}}', ARRAY['a','d'], '{"f":3}'::jsonb);
845        jsonb_replace_path        
846 ---------------------------------
847  {"a": {"b": 1, "c": 2, "f": 3}}
848 (1 row)
849
850 SELECT jsonb_replace_path('{"a":{"d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','d'], '{"f":3}'::jsonb);
851  jsonb_replace_path 
852 --------------------
853  {"a": {"f": 3}}
854 (1 row)
855
856 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','b'], '{"f":3}'::jsonb);
857                   jsonb_replace_path                   
858 -------------------------------------------------------
859  {"a": {"c": 2, "d": [1, {"Z": [1, [2, 3]]}], "f": 3}}
860 (1 row)
861
862 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','c'], '{"f":3}'::jsonb);
863                   jsonb_replace_path                   
864 -------------------------------------------------------
865  {"a": {"b": 1, "d": [1, {"Z": [1, [2, 3]]}], "f": 3}}
866 (1 row)
867
868 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','d'], '{"f":3}'::jsonb);
869        jsonb_replace_path        
870 ---------------------------------
871  {"a": {"b": 1, "c": 2, "f": 3}}
872 (1 row)
873
874 SELECT jsonb_replace_path('{"a":1, "b":null, "c":[1,2,null], "c":{"d":11}, "e":{"20":[100,"c"]}}', ARRAY['c'], '{"f":[[1],2], "g":"test", "h":{"i":{"j":null}}}');
875                                         jsonb_replace_path                                         
876 ---------------------------------------------------------------------------------------------------
877  {"a": 1, "b": null, "e": {"20": [100, "c"]}, "f": [[1], 2], "g": "test", "h": {"i": {"j": null}}}
878 (1 row)
879
880 SELECT jsonb_replace_path('"a"', ARRAY['a'], '{"f":10}'::jsonb); 
881  jsonb_replace_path 
882 --------------------
883  {"f": 10}
884 (1 row)
885
886 SELECT jsonb_replace_path('"a"', ARRAY['z'], '{"f":10}'::jsonb); 
887  jsonb_replace_path 
888 --------------------
889  "a"
890 (1 row)
891
892 SELECT jsonb_replace_path('[null, "a"]', ARRAY[null], '"b"'::jsonb); 
893  jsonb_replace_path 
894 --------------------
895  ["b", "a"]
896 (1 row)
897
898 SELECT jsonb_replace_path('[1,2,3,"4"]', ARRAY['4'], '"5"'::jsonb); 
899  jsonb_replace_path 
900 --------------------
901  [1, 2, 3, "5"]
902 (1 row)
903
904 SELECT jsonb_replace_path('{"a":{"b":3, "c":{"r":{"u":1}}}, "d":2}'::jsonb, ARRAY['a','c','r','u'],'{"a":{"b":3, "c":{"r":{"u":1}}}}'::jsonb);
905                              jsonb_replace_path                             
906 ----------------------------------------------------------------------------
907  {"a": {"b": 3, "c": {"r": {"a": {"b": 3, "c": {"r": {"u": 1}}}}}}, "d": 2}
908 (1 row)
909
910 SELECT jsonb_replace_path('{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb, ARRAY['a','b'], '{"a":{"b":3, "c":{"r":{"u":1}}}}'::jsonb);
911                                         jsonb_replace_path                                        
912 --------------------------------------------------------------------------------------------------
913  {"a": {"a": {"b": 3, "c": {"r": {"u": 1}}}, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
914 (1 row)
915