]> git.8kb.co.uk Git - postgresql/pg_jsonb_opx/blob - expected/jsonb_opx.out
Fix some funny behaviour in hstore 1.0 style operators and cease doing anything more...
[postgresql/pg_jsonb_opx] / expected / jsonb_opx.out
1 CREATE EXTENSION jsonb_opx VERSION '1.1';
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 elements
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 -- function is strict, so - null returns null - assume SQL nulls and jsonb nulls are not equal anyway
156 SELECT '["1", "2", true, null]'::jsonb - null::text;
157  ?column? 
158 ----------
159  
160 (1 row)
161
162 -------------------------------------------------------------------------------
163 -- Tests for jsonb - numeric
164 -------------------------------------------------------------------------------
165 -- Only matches numeric array element types
166 SELECT '[1, "1", "2", 2]'::jsonb - 2;
167    ?column?    
168 ---------------
169  [1, "1", "2"]
170 (1 row)
171
172 SELECT '[2]'::jsonb - 2;
173  ?column? 
174 ----------
175  []
176 (1 row)
177
178 SELECT '2'::jsonb - 2;
179  ?column? 
180 ----------
181  []
182 (1 row)
183
184 -- Does nothing for objects
185 SELECT '{"2":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - 2;
186                   ?column?                  
187 --------------------------------------------
188  {"2": {"b": 3, "c": [1, 2, 3, 4]}, "d": 2}
189 (1 row)
190
191 -------------------------------------------------------------------------------
192 -- Tests for jsonb - boolean
193 -------------------------------------------------------------------------------
194 -- Only matches boolean array element types
195 SELECT '[1, "1", false, true, null]'::jsonb - false;
196        ?column?       
197 ----------------------
198  [1, "1", true, null]
199 (1 row)
200
201 SELECT '[1, "1", false, true, null]'::jsonb - true;
202        ?column?        
203 -----------------------
204  [1, "1", false, null]
205 (1 row)
206
207 -------------------------------------------------------------------------------
208 -- Tests for jsonb - text[]
209 -------------------------------------------------------------------------------
210 -- text deletion from array containers will only delete string types currently
211 SELECT '[1, "1", "2", 2]'::jsonb - array['1','2'];
212  ?column? 
213 ----------
214  [1, 2]
215 (1 row)
216
217 -- simple text[] deletion from an object container
218 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a','b'];
219  ?column? 
220 ----------
221  {"c": 3}
222 (1 row)
223
224 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a ','b ',' c'];
225          ?column?         
226 --------------------------
227  {"a": 1, "b": 2, "c": 3}
228 (1 row)
229
230 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a','b','c'];
231  ?column? 
232 ----------
233  {}
234 (1 row)
235
236 SELECT '{"a": 1, "b": 2, "c": {"b": 3}}'::jsonb - ARRAY['a','b']; 
237     ?column?     
238 -----------------
239  {"c": {"b": 3}}
240 (1 row)
241
242 SELECT '{"a": 1, "b": 2, "c": {"b": [1,2,3]}}'::jsonb - ARRAY['a','b'];
243         ?column?         
244 -------------------------
245  {"c": {"b": [1, 2, 3]}}
246 (1 row)
247
248 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - ARRAY['a','b'];
249      ?column?     
250 ------------------
251  {"c": [1, 2, 3]}
252 (1 row)
253
254 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - ARRAY['a','c'];
255  ?column? 
256 ----------
257  {"b": 2}
258 (1 row)
259
260 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY['b','d'];
261               ?column?              
262 ------------------------------------
263  {"a": {"b": 3, "c": [1, 2, 3, 4]}}
264 (1 row)
265
266 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY['b','a'];
267  ?column? 
268 ----------
269  {"d": 2}
270 (1 row)
271
272 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY['a','d'];
273  ?column? 
274 ----------
275  {}
276 (1 row)
277
278 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb - ARRAY['a','d'];
279  ?column? 
280 ----------
281  {}
282 (1 row)
283
284 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'];
285                       ?column?                      
286 ----------------------------------------------------
287  {"d": [2, [2], [1, 2, {"a": 2}, {"b": [1, [2]]}]]}
288 (1 row)
289
290 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'];
291                           ?column?                           
292 -------------------------------------------------------------
293  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}}
294 (1 row)
295
296 SELECT '{"a":{"b":{"c":[1,[2,3,[4]],{"d":1}]}, "c":[1,2,3,4]}, "d":2}'::jsonb - '{a}'::text[]; 
297  ?column? 
298 ----------
299  {"d": 2}
300 (1 row)
301
302 -- simple text[] deletion from an object container should only match keys
303 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['1',' 2'];
304          ?column?         
305 --------------------------
306  {"a": 1, "b": 2, "c": 3}
307 (1 row)
308
309 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '{a}'::text[];
310        ?column?        
311 -----------------------
312  [2, {"a": 1, "b": 2}]
313 (1 row)
314
315 -- SQL nulls and jsonb nulls are not equal
316 SELECT '["1",null,2]'::jsonb - ARRAY[null];
317     ?column?    
318 ----------------
319  ["1", null, 2]
320 (1 row)
321
322 SELECT '["1",2]'::jsonb - ARRAY[null];
323  ?column? 
324 ----------
325  ["1", 2]
326 (1 row)
327
328 -------------------------------------------------------------------------------
329 -- Tests for jsonb - numeric[]
330 -------------------------------------------------------------------------------
331 -- Only matches numeric array element types
332 SELECT '[1, "1", "2", 2]'::jsonb - ARRAY[2];
333    ?column?    
334 ---------------
335  [1, "1", "2"]
336 (1 row)
337
338 SELECT '[1, "1", "2", 2]'::jsonb - ARRAY[1,2];
339   ?column?  
340 ------------
341  ["1", "2"]
342 (1 row)
343
344 SELECT '[2]'::jsonb  - ARRAY[1,2];
345  ?column? 
346 ----------
347  []
348 (1 row)
349
350 SELECT '2'::jsonb  - ARRAY[1,2];
351  ?column? 
352 ----------
353  []
354 (1 row)
355
356 -- Does nothing for objects
357 SELECT '{"2":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY[1,2];
358                   ?column?                  
359 --------------------------------------------
360  {"2": {"b": 3, "c": [1, 2, 3, 4]}, "d": 2}
361 (1 row)
362
363 -------------------------------------------------------------------------------
364 -- Tests for jsonb - boolean[]
365 -------------------------------------------------------------------------------
366 -- Only matches boolean array element types
367 SELECT '[1, "1", false, true, null]'::jsonb - ARRAY[false];
368        ?column?       
369 ----------------------
370  [1, "1", true, null]
371 (1 row)
372
373 SELECT '[1, "1", false, true, null]'::jsonb - ARRAY[true];
374        ?column?        
375 -----------------------
376  [1, "1", false, null]
377 (1 row)
378
379 SELECT '[1, "1", false, true, null]'::jsonb - ARRAY[true, false];
380     ?column?    
381 ----------------
382  [1, "1", null]
383 (1 row)
384
385 -- Again nulls are not equal
386 SELECT '[1, "1", false, true, null]'::jsonb - ARRAY[true, false, null];
387     ?column?    
388 ----------------
389  [1, "1", null]
390 (1 row)
391
392 -------------------------------------------------------------------------------
393 -- Tests for jsonb - jsonb
394 -------------------------------------------------------------------------------
395 -- jsonb deletion from an object should match on key/value
396 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
397      ?column?     
398 ------------------
399  {"a": 1, "c": 3}
400 (1 row)
401
402 -- jsonb deletion from an array should only match on key
403 SELECT '["a", "b", "c"]'::jsonb - '{"a": 4, "b": 2}'::jsonb;
404     ?column?     
405 -----------------
406  ["a", "b", "c"]
407 (1 row)
408
409 -- jsonb deletion from nested objects should not be part matched
410 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
411         ?column?         
412 -------------------------
413  {"c": 3, "d": {"a": 4}}
414 (1 row)
415
416 -- but a match of all nested values should
417 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"d": {"a": 4}, "b": 2}'::jsonb;
418      ?column?     
419 ------------------
420  {"a": 4, "c": 3}
421 (1 row)
422
423 SELECT '{"a":{"b":{"c":[1,[2,3,[4]],{"d":1}]}, "c":[1,2,3,4]}, "d":2}'::jsonb - '{"d":2}'::jsonb;
424                               ?column?                              
425 --------------------------------------------------------------------
426  {"a": {"b": {"c": [1, [2, 3, [4]], {"d": 1}]}, "c": [1, 2, 3, 4]}}
427 (1 row)
428
429 -- jsonb nulls are equal
430 SELECT '{"a": 1, "b": 2, "c": null}'::jsonb - '{"a": 4, "c": null}'::jsonb;
431      ?column?     
432 ------------------
433  {"a": 1, "b": 2}
434 (1 row)
435
436 -- others
437 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": false}}'::jsonb - '{"d": {"a": false}, "b": 2}'::jsonb;
438      ?column?     
439 ------------------
440  {"a": 4, "c": 3}
441 (1 row)
442
443 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}}'::jsonb - '{"a": "test2", "c": {"a": false}, "b": 2.2}'::jsonb;
444    ?column?    
445 ---------------
446  {"a": "test"}
447 (1 row)
448
449 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;
450                                 ?column?                                 
451 -------------------------------------------------------------------------
452  {"a": "test", "b": 2.2, "c": {"a": false}, "d": true, "e": [1, 2, "a"]}
453 (1 row)
454
455 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;
456                           ?column?                          
457 ------------------------------------------------------------
458  {"b": 2.2, "c": {"a": false}, "d": true, "e": [1, 2, "a"]}
459 (1 row)
460
461 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;
462                            ?column?                            
463 ---------------------------------------------------------------
464  {"a": "test", "c": {"a": false}, "d": true, "e": [1, 2, "a"]}
465 (1 row)
466
467 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;
468                        ?column?                       
469 ------------------------------------------------------
470  {"a": "test", "b": 2.2, "d": true, "e": [1, 2, "a"]}
471 (1 row)
472
473 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;
474                            ?column?                           
475 --------------------------------------------------------------
476  {"a": "test", "b": 2.2, "c": {"a": false}, "e": [1, 2, "a"]}
477 (1 row)
478
479 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;
480                                 ?column?                                 
481 -------------------------------------------------------------------------
482  {"a": "test", "b": 2.2, "c": {"a": false}, "d": true, "e": [1, 2, "a"]}
483 (1 row)
484
485 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;
486                        ?column?                        
487 -------------------------------------------------------
488  {"a": "test", "b": 2.2, "c": {"a": false}, "d": true}
489 (1 row)
490
491 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;
492                      ?column?                     
493 --------------------------------------------------
494  {"c": {"a": false}, "d": true, "e": [1, 2, "a"]}
495 (1 row)
496
497 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;
498            ?column?            
499 -------------------------------
500  {"d": true, "e": [1, 2, "a"]}
501 (1 row)
502
503 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;
504       ?column?      
505 --------------------
506  {"e": [1, 2, "a"]}
507 (1 row)
508
509 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;
510  ?column? 
511 ----------
512  {}
513 (1 row)
514
515 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[[1]]'::jsonb;
516           ?column?          
517 ----------------------------
518  ["a", 2, {"a": 1, "b": 2}]
519 (1 row)
520
521 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[{"a":1}]'::jsonb;
522           ?column?          
523 ----------------------------
524  ["a", 2, {"a": 1, "b": 2}]
525 (1 row)
526
527 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[{"a":1, "b":2}]'::jsonb;
528  ?column? 
529 ----------
530  ["a", 2]
531 (1 row)
532
533 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '["a"]'::jsonb;
534        ?column?        
535 -----------------------
536  [2, {"a": 1, "b": 2}]
537 (1 row)
538
539 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[2]'::jsonb;
540         ?column?         
541 -------------------------
542  ["a", {"a": 1, "b": 2}]
543 (1 row)
544
545 -------------------------------------------------------------------------------
546 -- Tests for jsonb || jsonb
547 -------------------------------------------------------------------------------
548 -- duplicates should automatically be removed by lower level logic
549 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb || '{"a": 4, "b": 2, "d": 4}'::jsonb;
550              ?column?             
551 ----------------------------------
552  {"a": 4, "b": 2, "c": 3, "d": 4}
553 (1 row)
554
555 SELECT '{"a": 1, "b": null, "c": 3}'::jsonb || '{"a": 4, "b": null, "d": 4}'::jsonb;
556               ?column?               
557 -------------------------------------
558  {"a": 4, "b": null, "c": 3, "d": 4}
559 (1 row)
560
561 -- concatentation of arrays
562 SELECT '["a", "b"]'::jsonb || '["c"]'::jsonb;
563     ?column?     
564 -----------------
565  ["a", "b", "c"]
566 (1 row)
567
568 -- concatentation of scalars and arrays should be wrapped into arrays
569 SELECT '["a", "b"]'::jsonb || '"c"'::jsonb;
570     ?column?     
571 -----------------
572  ["a", "b", "c"]
573 (1 row)
574
575 -- likewise concatentation of objects and arrays should be wrapped into arrays
576 SELECT '["a", "b"]'::jsonb || '{"a": 4, "b": 2}'::jsonb;
577            ?column?           
578 ------------------------------
579  ["a", "b", {"a": 4, "b": 2}]
580 (1 row)
581
582 -- and all concatentation should be in natural order supplied
583 SELECT '{"a": 4, "b": 2}'::jsonb || '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
584                 ?column?                
585 ----------------------------------------
586  [{"a": 4, "b": 2}, "a", "b", "c", "d"]
587 (1 row)
588
589 -- others
590 SELECT 'false'::jsonb || '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
591           ?column?           
592 -----------------------------
593  [false, "a", "b", "c", "d"]
594 (1 row)
595
596 SELECT '["a","b"]'::jsonb || '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb;
597                         ?column?                        
598 --------------------------------------------------------
599  ["a", "b", {"a": {"b": 3, "c": [1, 2, 3, 4]}, "d": 2}]
600 (1 row)
601
602 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb || '["a","b"]'::jsonb;
603                         ?column?                        
604 --------------------------------------------------------
605  [{"a": {"b": 3, "c": [1, 2, 3, 4]}, "d": 2}, "a", "b"]
606 (1 row)
607
608 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;
609                                                                 ?column?                                                                
610 ----------------------------------------------------------------------------------------------------------------------------------------
611  [{"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}]
612 (1 row)
613
614 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;
615                                                               ?column?                                                              
616 ------------------------------------------------------------------------------------------------------------------------------------
617  ["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}]
618 (1 row)
619
620 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;
621                               ?column?                               
622 ---------------------------------------------------------------------
623  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
624 (1 row)
625
626 -------------------------------------------------------------------------------
627 -- Tests for jsonb #= jsonb
628 -------------------------------------------------------------------------------
629 -- any keys existing in left argument have values replaced with those from righ 
630 -- argument
631 SELECT '{"a": 1}'::jsonb #= '{"a": [1,2,3,4]}'::jsonb;
632       ?column?       
633 ---------------------
634  {"a": [1, 2, 3, 4]}
635 (1 row)
636
637 SELECT '{"a": 1}'::jsonb #= '{"a": [1,2,3,4], "b":2}'::jsonb;
638       ?column?       
639 ---------------------
640  {"a": [1, 2, 3, 4]}
641 (1 row)
642
643 SELECT '{"a": 1, "b":1}'::jsonb #= '{"a": [1,2,3,4], "b":2}'::jsonb;
644           ?column?           
645 -----------------------------
646  {"a": [1, 2, 3, 4], "b": 2}
647 (1 row)
648
649 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;
650                                       ?column?                                      
651 ------------------------------------------------------------------------------------
652  {"a": [1, 2, 3, 4], "b": {"f": 100, "j": {"k": 200}}, "c": 4, "d": {"test": true}}
653 (1 row)
654
655 -- note that as we are matching only keys and replacing values operation on an 
656 -- scalar/array elements effectively does nothing 
657 SELECT '{"a":[1,2], "b":2, "c":12}'::jsonb #= '["a","b","c"]'::jsonb;
658             ?column?            
659 --------------------------------
660  {"a": [1, 2], "b": 2, "c": 12}
661 (1 row)
662
663 SELECT '{"a":[1,2], "b":2, "c":12}'::jsonb #= '[1,2,3]'::jsonb;
664             ?column?            
665 --------------------------------
666  {"a": [1, 2], "b": 2, "c": 12}
667 (1 row)
668
669 SELECT '[1,2,3]'::jsonb #= '[1,2,3,4]'::jsonb;
670  ?column?  
671 -----------
672  [1, 2, 3]
673 (1 row)
674
675 SELECT '"a"'::jsonb #= '{"a":1, "b":2}'::jsonb;
676  ?column? 
677 ----------
678  "a"
679 (1 row)
680
681 SELECT '{"a":1, "b":2}'::jsonb #= '"a"'::jsonb;
682      ?column?     
683 ------------------
684  {"a": 1, "b": 2}
685 (1 row)
686
687 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;
688                               ?column?                               
689 ---------------------------------------------------------------------
690  {"a": {"b": 3, "c": [1, {"r": [true, {"u": 2}]}, 3, 4, 5]}, "d": 2}
691 (1 row)
692
693 SELECT '["a","b","c"]'::jsonb #= '{"a":1}'::jsonb;
694     ?column?     
695 -----------------
696  ["a", "b", "c"]
697 (1 row)
698
699 -------------------------------------------------------------------------------
700 -- Tests for jsonb #- text[] 
701 -------------------------------------------------------------------------------
702 SELECT '"a"'::jsonb #- ARRAY['b'];
703  ?column? 
704 ----------
705  "a"
706 (1 row)
707
708 SELECT '["a"]'::jsonb #- ARRAY['b'];
709  ?column? 
710 ----------
711  ["a"]
712 (1 row)
713
714 SELECT '{"a":1}'::jsonb #- ARRAY['b'];
715  ?column? 
716 ----------
717  {"a": 1}
718 (1 row)
719
720 SELECT '"a"'::jsonb #- ARRAY['a'];
721  ?column? 
722 ----------
723  []
724 (1 row)
725
726 SELECT '["a"]'::jsonb #- ARRAY['a'];
727  ?column? 
728 ----------
729  []
730 (1 row)
731
732 SELECT '{"a":1}'::jsonb #- ARRAY['a'];
733  ?column? 
734 ----------
735  {}
736 (1 row)
737
738 SELECT '["a", "b"]'::jsonb #- ARRAY['a'];
739  ?column? 
740 ----------
741  ["b"]
742 (1 row)
743
744 SELECT '{"a":1, "b":2}'::jsonb #- ARRAY['a'];
745  ?column? 
746 ----------
747  {"b": 2}
748 (1 row)
749
750 SELECT '{"a":{"b":1}, "c":2}'::jsonb #- ARRAY['a'];
751  ?column? 
752 ----------
753  {"c": 2}
754 (1 row)
755
756 SELECT '{"a":[1,2,3,4], "b":2}'::jsonb #- ARRAY['a'];
757  ?column? 
758 ----------
759  {"b": 2}
760 (1 row)
761
762 SELECT '{"a":[1,2,3,4], "b":2}'::jsonb #- ARRAY['b'];
763       ?column?       
764 ---------------------
765  {"a": [1, 2, 3, 4]}
766 (1 row)
767
768 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['c'];
769               ?column?               
770 -------------------------------------
771  {"a": {"b": [1, 2, 3, ["a", "b"]]}}
772 (1 row)
773
774 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a'];
775  ?column? 
776 ----------
777  {"c": 2}
778 (1 row)
779
780 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a','c'];
781                   ?column?                   
782 ---------------------------------------------
783  {"a": {"b": [1, 2, 3, ["a", "b"]]}, "c": 2}
784 (1 row)
785
786 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a','b'];
787      ?column?      
788 -------------------
789  {"a": {}, "c": 2}
790 (1 row)
791
792 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a','b','c'];
793                   ?column?                   
794 ---------------------------------------------
795  {"a": {"b": [1, 2, 3, ["a", "b"]]}, "c": 2}
796 (1 row)
797
798 SELECT '{"a":{"b":{"c":1}, "c":[1,2,3,["a","b"]]}, "d":3}'::jsonb #- ARRAY['a','b','c'];
799                        ?column?                       
800 ------------------------------------------------------
801  {"a": {"b": {}, "c": [1, 2, 3, ["a", "b"]]}, "d": 3}
802 (1 row)
803
804 SELECT '{"a":{"b":[1,2,3,["a","b"]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a','b'];
805               ?column?              
806 ------------------------------------
807  {"a": {"c": [1, 2, 3, 4]}, "d": 2}
808 (1 row)
809
810 SELECT '{"a":{"b":[1,2,3,["a","b"]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a','c'];
811                   ?column?                   
812 ---------------------------------------------
813  {"a": {"b": [1, 2, 3, ["a", "b"]]}, "d": 2}
814 (1 row)
815
816 SELECT '{"a":{"b":[1,2,3,["a","b"]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a',null];
817                             ?column?                            
818 ----------------------------------------------------------------
819  {"a": {"b": [1, 2, 3, ["a", "b"]], "c": [1, 2, 3, 4]}, "d": 2}
820 (1 row)
821
822 SELECT '{"a":{"b":[1,2,3,["a",{"b":3}]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a','b'];
823               ?column?              
824 ------------------------------------
825  {"a": {"c": [1, 2, 3, 4]}, "d": 2}
826 (1 row)
827
828 SELECT '{"a":{"b":3, "d":[1,{"Z":[1,[2,3]]}]}}'::jsonb #- ARRAY['a','d'];
829     ?column?     
830 -----------------
831  {"a": {"b": 3}}
832 (1 row)
833
834 SELECT '["a", {"b":[1,2,3,4,5]}, 1, "c"]'::jsonb #- ARRAY['a'];
835              ?column?             
836 ----------------------------------
837  [{"b": [1, 2, 3, 4, 5]}, 1, "c"]
838 (1 row)
839
840 SELECT '["a", {"b":[1,2,3,4,5]}, 1, "c"]'::jsonb #- ARRAY['c'];
841              ?column?             
842 ----------------------------------
843  ["a", {"b": [1, 2, 3, 4, 5]}, 1]
844 (1 row)
845
846 SELECT '[1,[2,[3,[4,[5,6,7]]]],"a","b"]'::jsonb #- ARRAY['b'];
847               ?column?              
848 ------------------------------------
849  [1, [2, [3, [4, [5, 6, 7]]]], "a"]
850 (1 row)
851
852 SELECT '[1,[2,[3,[4,[5,6,7]]]],"a","b"]'::jsonb #- ARRAY['a'];
853               ?column?              
854 ------------------------------------
855  [1, [2, [3, [4, [5, 6, 7]]]], "b"]
856 (1 row)
857
858 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','b']; 
859                           ?column?                           
860 -------------------------------------------------------------
861  {"a": {"c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
862 (1 row)
863
864 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','c']; 
865         ?column?         
866 -------------------------
867  {"a": {"b": 3}, "d": 2}
868 (1 row)
869
870 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','d'];
871                               ?column?                               
872 ---------------------------------------------------------------------
873  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
874 (1 row)
875
876 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['d'];
877                           ?column?                           
878 -------------------------------------------------------------
879  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}}
880 (1 row)
881
882 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','c','r']; 
883                               ?column?                               
884 ---------------------------------------------------------------------
885  {"a": {"b": 3, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
886 (1 row)
887
888 SELECT '{"a":{"b":3, "c":{"r":{"u":1}}}, "d":2}'::jsonb #- ARRAY['a','c','r'];  
889              ?column?             
890 ----------------------------------
891  {"a": {"b": 3, "c": {}}, "d": 2}
892 (1 row)
893
894 SELECT '{"a":{"b":3, "c":{"r":{"u":1}}}, "d":2}'::jsonb #- ARRAY['a','c','r','u'];
895                 ?column?                 
896 -----------------------------------------
897  {"a": {"b": 3, "c": {"r": {}}}, "d": 2}
898 (1 row)
899
900 -- expected limitation: cannot call with path deeper than 1 on a non-object
901 SELECT '["a", "b"]'::jsonb #- ARRAY['a','b'];
902 ERROR:  cannot call with path deeper than 1 on a non-object
903 -------------------------------------------------------------------------------
904 -- Tests for jsonb_replace_path jsonb text[] 
905 -------------------------------------------------------------------------------
906 -- if the replacement on an object/array is passed as a scalar/array the value/element
907 -- is replaced
908 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '3'::jsonb);
909  jsonb_replace_path 
910 --------------------
911  {"a": 3, "b": 2}
912 (1 row)
913
914 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '[3]'::jsonb);
915  jsonb_replace_path 
916 --------------------
917  {"a": [3], "b": 2}
918 (1 row)
919
920 SELECT jsonb_replace_path('["a", "b"]', ARRAY['a'], '3'::jsonb);
921  jsonb_replace_path 
922 --------------------
923  [3, "b"]
924 (1 row)
925
926 SELECT jsonb_replace_path('["a", "b"]', ARRAY['a'], '[3]'::jsonb);
927  jsonb_replace_path 
928 --------------------
929  [3, "b"]
930 (1 row)
931
932 -- if the replacement on an object/array is passed as an object the whole key-value
933 -- pair is replaced.  This difference is perhaps confusing, but otherwise there is 
934 -- no way to directly replace a key without deletion and concatenation.
935 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '{"z":3}'::jsonb);
936  jsonb_replace_path 
937 --------------------
938  {"b": 2, "z": 3}
939 (1 row)
940
941 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '{"a":{"z":3}}'::jsonb);
942    jsonb_replace_path    
943 -------------------------
944  {"a": {"z": 3}, "b": 2}
945 (1 row)
946
947 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '{"f":3}'::jsonb);
948  jsonb_replace_path 
949 --------------------
950  {"b": 2, "f": 3}
951 (1 row)
952
953 SELECT jsonb_replace_path('{"a":{"b":1}}', ARRAY['a'], '{"f":3}'::jsonb);
954  jsonb_replace_path 
955 --------------------
956  {"f": 3}
957 (1 row)
958
959 SELECT jsonb_replace_path('{"a":{"b":1}}', ARRAY['a','b'], '{"f":3}'::jsonb);
960  jsonb_replace_path 
961 --------------------
962  {"a": {"f": 3}}
963 (1 row)
964
965 SELECT jsonb_replace_path('{"a":{"b":1, "c":1}}', ARRAY['a','b'], '{"f":3}'::jsonb);
966    jsonb_replace_path    
967 -------------------------
968  {"a": {"c": 1, "f": 3}}
969 (1 row)
970
971 SELECT jsonb_replace_path('{"a":{"b":1, "c":1}}', ARRAY['a','c'], '{"f":3}'::jsonb);
972    jsonb_replace_path    
973 -------------------------
974  {"a": {"b": 1, "f": 3}}
975 (1 row)
976
977 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,2]}}', ARRAY['a','b'], '{"f":3}'::jsonb);
978           jsonb_replace_path          
979 --------------------------------------
980  {"a": {"c": 2, "d": [1, 2], "f": 3}}
981 (1 row)
982
983 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,2]}}', ARRAY['a','c'], '{"f":3}'::jsonb);
984           jsonb_replace_path          
985 --------------------------------------
986  {"a": {"b": 1, "d": [1, 2], "f": 3}}
987 (1 row)
988
989 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,2]}}', ARRAY['a','d'], '{"f":3}'::jsonb);
990        jsonb_replace_path        
991 ---------------------------------
992  {"a": {"b": 1, "c": 2, "f": 3}}
993 (1 row)
994
995 SELECT jsonb_replace_path('{"a":{"d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','d'], '{"f":3}'::jsonb);
996  jsonb_replace_path 
997 --------------------
998  {"a": {"f": 3}}
999 (1 row)
1000
1001 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','b'], '{"f":3}'::jsonb);
1002                   jsonb_replace_path                   
1003 -------------------------------------------------------
1004  {"a": {"c": 2, "d": [1, {"Z": [1, [2, 3]]}], "f": 3}}
1005 (1 row)
1006
1007 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','c'], '{"f":3}'::jsonb);
1008                   jsonb_replace_path                   
1009 -------------------------------------------------------
1010  {"a": {"b": 1, "d": [1, {"Z": [1, [2, 3]]}], "f": 3}}
1011 (1 row)
1012
1013 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','d'], '{"f":3}'::jsonb);
1014        jsonb_replace_path        
1015 ---------------------------------
1016  {"a": {"b": 1, "c": 2, "f": 3}}
1017 (1 row)
1018
1019 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}}}');
1020                                         jsonb_replace_path                                         
1021 ---------------------------------------------------------------------------------------------------
1022  {"a": 1, "b": null, "e": {"20": [100, "c"]}, "f": [[1], 2], "g": "test", "h": {"i": {"j": null}}}
1023 (1 row)
1024
1025 SELECT jsonb_replace_path('"a"', ARRAY['a'], '{"f":10}'::jsonb); 
1026  jsonb_replace_path 
1027 --------------------
1028  {"f": 10}
1029 (1 row)
1030
1031 SELECT jsonb_replace_path('"a"', ARRAY['z'], '{"f":10}'::jsonb); 
1032  jsonb_replace_path 
1033 --------------------
1034  "a"
1035 (1 row)
1036
1037 SELECT jsonb_replace_path('[null, "a"]', ARRAY[null], '"b"'::jsonb); 
1038  jsonb_replace_path 
1039 --------------------
1040  [null, "a"]
1041 (1 row)
1042
1043 SELECT jsonb_replace_path('[1,2,3,"4"]', ARRAY['4'], '"5"'::jsonb); 
1044  jsonb_replace_path 
1045 --------------------
1046  [1, 2, 3, "5"]
1047 (1 row)
1048
1049 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);
1050                              jsonb_replace_path                             
1051 ----------------------------------------------------------------------------
1052  {"a": {"b": 3, "c": {"r": {"a": {"b": 3, "c": {"r": {"u": 1}}}}}}, "d": 2}
1053 (1 row)
1054
1055 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);
1056                                         jsonb_replace_path                                        
1057 --------------------------------------------------------------------------------------------------
1058  {"a": {"a": {"b": 3, "c": {"r": {"u": 1}}}, "c": [1, {"r": [null, {"u": 1}]}, 2, 3, 4]}, "d": 2}
1059 (1 row)
1060