]> git.8kb.co.uk Git - postgresql/pg_jsonb_opx/blob - sql/jsonb_opx.sql
Fix some logic when modifying scalars and simplify simple text deletion.
[postgresql/pg_jsonb_opx] / sql / jsonb_opx.sql
1 CREATE EXTENSION jsonb_opx;
2
3 -------------------------------------------------------------------------------
4 -- Tests for jsonb - text
5 -------------------------------------------------------------------------------
6
7 SELECT '["a", "b"]'::jsonb - 'b'::text;
8 SELECT '["a"]'::jsonb - 'b'::text;
9 SELECT '"a"'::jsonb - 'b'::text;
10 SELECT '[1]'::jsonb - 'b'::text;
11 SELECT '1'::jsonb - 'b'::text;
12 SELECT '["a", {"a":1}, "b"]'::jsonb - 'b'::text;
13
14 -- text deletion from array containers will only delete string types currently
15 SELECT '[1, "1", "2", 2]'::jsonb - '2'::text;
16
17 -- simple text deletion from an object container
18 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - 'b'::text;
19 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - 'b '::text;
20 SELECT '{"a": 1, "b": 2, "c": {"b": 3}}'::jsonb - 'b'::text;
21 SELECT '{"a": 1, "b": 2, "c": {"b": [1,2,3]}}'::jsonb - 'b'::text;
22 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - 'b'::text;
23 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - 'c'::text;
24
25 -- simple text deletion from an object container should only match keys
26 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - '3'::text;
27
28 -- deleting everything always results in an empty container
29 SELECT '["a"]'::jsonb - 'a'::text;
30 SELECT '{"a":1}'::jsonb - 'a'::text;
31
32 -- even for scalars, but this should perhaps error
33 SELECT '"a"'::jsonb - 'a'::text;
34
35 -- others
36 SELECT '["1", "2", true, false]'::jsonb - '2'::text;
37 SELECT '["1", "2", "2", "2"]'::jsonb - '2'::text;
38 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - 'a'::text;
39 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - 'a'::text;
40 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - 'd'::text;
41 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - 'b'::text;
42 SELECT '{"a":{"b":{"c":[1,[2,3,[4]],{"d":1}]}, "c":[1,2,3,4]}, "d":2}'::jsonb - 'a'::text;
43
44 -------------------------------------------------------------------------------
45 -- Tests for jsonb - text[]
46 -------------------------------------------------------------------------------
47
48 -- text deletion from array containers will only delete string types currently
49 SELECT '[1, "1", "2", 2]'::jsonb - array['1','2'];
50
51 -- simple text[] deletion from an object container
52 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a','b'];
53 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a ','b ',' c'];
54 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a','b','c'];
55 SELECT '{"a": 1, "b": 2, "c": {"b": 3}}'::jsonb - ARRAY['a','b']; 
56 SELECT '{"a": 1, "b": 2, "c": {"b": [1,2,3]}}'::jsonb - ARRAY['a','b'];
57 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - ARRAY['a','b'];
58 SELECT '{"a": 1, "b": 2, "c":[1,2,3]}'::jsonb - ARRAY['a','c'];
59 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY['b','d'];
60 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY['b','a'];
61 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb - ARRAY['a','d'];
62 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb - ARRAY['a','d'];
63 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'];
64 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'];
65 SELECT '{"a":{"b":{"c":[1,[2,3,[4]],{"d":1}]}, "c":[1,2,3,4]}, "d":2}'::jsonb - '{a}'::text[]; 
66
67 -- simple text[] deletion from an object container should only match keys or nulls
68 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['1',' 2'];
69 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '{a}'::text[];
70
71 -- here we treat SQL nulls and json nulls as equal - bad?
72 SELECT '["1",null,2]'::jsonb - ARRAY[null];
73 SELECT '["1",2]'::jsonb - ARRAY[null];
74
75 -------------------------------------------------------------------------------
76 -- Tests for jsonb - jsonb
77 -------------------------------------------------------------------------------
78
79 -- jsonb deletion from an object should match on key/value
80 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
81
82 -- jsonb deletion from an array should only match on key
83 SELECT '["a", "b", "c"]'::jsonb - '{"a": 4, "b": 2}'::jsonb;
84
85 -- jsonb deletion from nested objects should not be part matched
86 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
87
88 -- but a match of all nested values should
89 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"d": {"a": 4}, "b": 2}'::jsonb;
90 SELECT '{"a":{"b":{"c":[1,[2,3,[4]],{"d":1}]}, "c":[1,2,3,4]}, "d":2}'::jsonb - '{"d":2}'::jsonb;
91
92 -- jsonb nulls are equal
93 SELECT '{"a": 1, "b": 2, "c": null}'::jsonb - '{"a": 4, "c": null}'::jsonb;
94
95 -- others
96 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": false}}'::jsonb - '{"d": {"a": false}, "b": 2}'::jsonb;
97 SELECT '{"a": "test", "b": 2.2, "c": {"a": false}}'::jsonb - '{"a": "test2", "c": {"a": false}, "b": 2.2}'::jsonb;
98 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;
99 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;
100 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;
101 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;
102 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;
103 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;
104 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;
105 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;
106 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;
107 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;
108 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;
109
110 -- known issues !!!!
111 -- lookups of lhs values in rhs jsonb use findJsonbValueFromContainer which does 
112 -- not allow looking up non-scalar elements resulting in "invalid jsonb scalar type"
113 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[[1]]'::jsonb;
114 SELECT '["a",2,{"a":1, "b":2}]'::jsonb - '[{"a":1}]'::jsonb;
115
116 -------------------------------------------------------------------------------
117 -- Tests for jsonb || jsonb
118 -------------------------------------------------------------------------------
119
120 -- duplicates should automatically be removed by lower level logic
121 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb || '{"a": 4, "b": 2, "d": 4}'::jsonb;
122 SELECT '{"a": 1, "b": null, "c": 3}'::jsonb || '{"a": 4, "b": null, "d": 4}'::jsonb;
123
124 -- concatentation of arrays
125 SELECT '["a", "b"]'::jsonb || '["c"]'::jsonb;
126
127 -- concatentation of scalars and arrays should be wrapped into arrays
128 SELECT '["a", "b"]'::jsonb || '"c"'::jsonb;
129
130 -- likewise concatentation of objects and arrays should be wrapped into arrays
131 SELECT '["a", "b"]'::jsonb || '{"a": 4, "b": 2}'::jsonb;
132
133 -- and all concatentation should be in natural order supplied
134 SELECT '{"a": 4, "b": 2}'::jsonb || '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
135
136 -- others
137 SELECT 'false'::jsonb || '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
138 SELECT '["a","b"]'::jsonb || '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb;
139 SELECT '{"a":{"b":3, "c":[1,2,3,4]}, "d":2}'::jsonb || '["a","b"]'::jsonb;
140 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;
141 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;
142 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;
143
144 -------------------------------------------------------------------------------
145 -- Tests for jsonb #= jsonb
146 -------------------------------------------------------------------------------
147
148 -- any keys existing in left argument have values replaced with those from righ 
149 -- argument
150 SELECT '{"a": 1}'::jsonb #= '{"a": [1,2,3,4]}'::jsonb;
151 SELECT '{"a": 1}'::jsonb #= '{"a": [1,2,3,4], "b":2}'::jsonb;
152 SELECT '{"a": 1, "b":1}'::jsonb #= '{"a": [1,2,3,4], "b":2}'::jsonb;
153
154 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;
155
156 -- note that as we are matching only keys and replacing values operation on an 
157 -- scalar/array elements effectively does nothing 
158 SELECT '{"a":[1,2], "b":2, "c":12}'::jsonb #= '["a","b","c"]'::jsonb;
159 SELECT '{"a":[1,2], "b":2, "c":12}'::jsonb #= '[1,2,3]'::jsonb;
160 SELECT '[1,2,3]'::jsonb #= '[1,2,3,4]'::jsonb;
161 SELECT '"a"'::jsonb #= '{"a":1, "b":2}'::jsonb;
162 SELECT '{"a":1, "b":2}'::jsonb #= '"a"'::jsonb;
163 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;
164
165
166 -------------------------------------------------------------------------------
167 -- Tests for jsonb #- text[] 
168 -------------------------------------------------------------------------------
169 SELECT '"a"'::jsonb #- ARRAY['b'];
170 SELECT '["a"]'::jsonb #- ARRAY['b'];
171 SELECT '{"a":1}'::jsonb #- ARRAY['b'];
172
173 SELECT '"a"'::jsonb #- ARRAY['a'];
174 SELECT '["a"]'::jsonb #- ARRAY['a'];
175 SELECT '{"a":1}'::jsonb #- ARRAY['a'];
176
177 SELECT '["a", "b"]'::jsonb #- ARRAY['a'];
178 SELECT '{"a":1, "b":2}'::jsonb #- ARRAY['a'];
179 SELECT '{"a":{"b":1}, "c":2}'::jsonb #- ARRAY['a'];
180
181 SELECT '{"a":[1,2,3,4], "b":2}'::jsonb #- ARRAY['a'];
182 SELECT '{"a":[1,2,3,4], "b":2}'::jsonb #- ARRAY['b'];
183
184 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['c'];
185 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a'];
186 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a','c'];
187 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a','b'];
188 SELECT '{"a":{"b":[1,2,3,["a","b"]]}, "c":2}'::jsonb #- ARRAY['a','b','c'];
189 SELECT '{"a":{"b":{"c":1}, "c":[1,2,3,["a","b"]]}, "d":3}'::jsonb #- ARRAY['a','b','c'];
190
191 SELECT '{"a":{"b":[1,2,3,["a","b"]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a','b'];
192 SELECT '{"a":{"b":[1,2,3,["a","b"]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a','c'];
193 SELECT '{"a":{"b":[1,2,3,["a","b"]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a',null];
194
195 SELECT '{"a":{"b":[1,2,3,["a",{"b":3}]], "c":[1,2,3,4]}, "d":2}'::jsonb #- ARRAY['a','b'];
196
197 SELECT '{"a":{"b":3, "d":[1,{"Z":[1,[2,3]]}]}}'::jsonb #- ARRAY['a','d'];
198
199 SELECT '["a", {"b":[1,2,3,4,5]}, 1, "c"]'::jsonb #- ARRAY['a'];
200 SELECT '["a", {"b":[1,2,3,4,5]}, 1, "c"]'::jsonb #- ARRAY['c'];
201 SELECT '[1,[2,[3,[4,[5,6,7]]]],"a","b"]'::jsonb #- ARRAY['b'];
202 SELECT '[1,[2,[3,[4,[5,6,7]]]],"a","b"]'::jsonb #- ARRAY['a'];
203 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','b']; 
204 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','c']; 
205 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','d'];
206 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['d'];
207 SELECT '{"a":{"b":3, "c":[1,{"r":[null,{"u":1}]},2,3,4]}, "d":2}'::jsonb #- ARRAY['a','c','r']; 
208 SELECT '{"a":{"b":3, "c":{"r":{"u":1}}}, "d":2}'::jsonb #- ARRAY['a','c','r'];  
209 SELECT '{"a":{"b":3, "c":{"r":{"u":1}}}, "d":2}'::jsonb #- ARRAY['a','c','r','u'];
210
211 -- expected limitation: cannot call with path deeper than 1 on a non-object
212 SELECT '["a", "b"]'::jsonb #- ARRAY['a','b'];
213
214 -------------------------------------------------------------------------------
215 -- Tests for jsonb_replace_path jsonb text[] 
216 -------------------------------------------------------------------------------
217
218 -- if the replacement on an object/array is passed as a scalar/array the value/element
219 -- is replaced
220 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '3'::jsonb);
221 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '[3]'::jsonb);
222 SELECT jsonb_replace_path('["a", "b"]', ARRAY['a'], '3'::jsonb);
223 SELECT jsonb_replace_path('["a", "b"]', ARRAY['a'], '[3]'::jsonb);
224 -- if the replacement on an object/array is passed as an object the whole key-value
225 -- pair is replaced.  This difference is perhaps confusing, but otherwise there is 
226 -- no way to directly replace a key without deletion and concatenation.
227 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '{"z":3}'::jsonb);
228 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '{"a":{"z":3}}'::jsonb);
229
230 SELECT jsonb_replace_path('{"a":1, "b":2}', ARRAY['a'], '{"f":3}'::jsonb);
231 SELECT jsonb_replace_path('{"a":{"b":1}}', ARRAY['a'], '{"f":3}'::jsonb);
232 SELECT jsonb_replace_path('{"a":{"b":1}}', ARRAY['a','b'], '{"f":3}'::jsonb);
233 SELECT jsonb_replace_path('{"a":{"b":1, "c":1}}', ARRAY['a','b'], '{"f":3}'::jsonb);
234 SELECT jsonb_replace_path('{"a":{"b":1, "c":1}}', ARRAY['a','c'], '{"f":3}'::jsonb);
235
236 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,2]}}', ARRAY['a','b'], '{"f":3}'::jsonb);
237 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,2]}}', ARRAY['a','c'], '{"f":3}'::jsonb);
238 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,2]}}', ARRAY['a','d'], '{"f":3}'::jsonb);
239
240 SELECT jsonb_replace_path('{"a":{"d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','d'], '{"f":3}'::jsonb);
241 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','b'], '{"f":3}'::jsonb);
242 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','c'], '{"f":3}'::jsonb);
243 SELECT jsonb_replace_path('{"a":{"b":1, "c":2, "d":[1,{"Z":[1,[2,3]]}]}}', ARRAY['a','d'], '{"f":3}'::jsonb);
244 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}}}');
245
246 SELECT jsonb_replace_path('"a"', ARRAY['a'], '{"f":10}'::jsonb); 
247 SELECT jsonb_replace_path('"a"', ARRAY['z'], '{"f":10}'::jsonb); 
248 SELECT jsonb_replace_path('[null, "a"]', ARRAY[null], '"b"'::jsonb); 
249 SELECT jsonb_replace_path('[1,2,3,"4"]', ARRAY['4'], '"5"'::jsonb); 
250
251 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);
252 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);