]> git.8kb.co.uk Git - postgresql/pg_jsonb_opx/blob - expected/jsonb_opx.out
b79bde99a58632fa7e98cd7d81c65c40f41b038a
[postgresql/pg_jsonb_opx] / expected / jsonb_opx.out
1 \i /usr/local/pgsql/share/contrib/jsonb_opx.sql
2 -- CREATE OR REPLACE FUNCTION public.jsonb_delete (jsonb, text) 
3 -- RETURNS jsonb
4 --     AS 'SELECT jsonb_delete($1, ARRAY[$2]);'
5 -- LANGUAGE SQL IMMUTABLE STRICT; 
6 -- COMMENT ON FUNCTION public.jsonb_delete(jsonb, text) IS 'delete key in second argument from first argument';
7 CREATE OR REPLACE FUNCTION public.jsonb_delete (jsonb, text) 
8 RETURNS jsonb
9     AS '$libdir/jsonb_opx', 'jsonb_delete_key'
10 LANGUAGE C IMMUTABLE STRICT; 
11 COMMENT ON FUNCTION public.jsonb_delete(jsonb, text) IS 'delete key in second argument from first argument';
12 -- DROP OPERATOR - (jsonb, text);
13 CREATE OPERATOR - ( PROCEDURE = public.jsonb_delete, LEFTARG = jsonb, RIGHTARG = text);
14 COMMENT ON OPERATOR - (jsonb, text) IS 'delete key from left operand';
15 --
16 CREATE OR REPLACE FUNCTION public.jsonb_delete(jsonb, text[]) 
17 RETURNS jsonb
18         AS '$libdir/jsonb_opx', 'jsonb_delete_keys'
19 LANGUAGE C IMMUTABLE STRICT;
20 COMMENT ON FUNCTION public.jsonb_delete(jsonb, text[]) IS 'delete keys in second argument from first argument';
21 -- DROP OPERATOR - (jsonb, text[]);
22 CREATE OPERATOR - ( PROCEDURE = public.jsonb_delete, LEFTARG = jsonb, RIGHTARG = text[]);
23 COMMENT ON OPERATOR - (jsonb, text[]) IS 'delete keys from left operand';
24 --
25 CREATE OR REPLACE FUNCTION public.jsonb_delete(jsonb, jsonb) 
26 RETURNS jsonb
27         AS '$libdir/jsonb_opx', 'jsonb_delete_jsonb'
28 LANGUAGE C IMMUTABLE STRICT;
29 COMMENT ON FUNCTION public.jsonb_delete(jsonb, jsonb) IS 'delete matching pairs in second argument from first argument';
30 -- DROP OPERATOR - (jsonb, jsonb);
31 CREATE OPERATOR - ( PROCEDURE = public.jsonb_delete, LEFTARG = jsonb, RIGHTARG = jsonb);
32 COMMENT ON OPERATOR - (jsonb, jsonb) IS 'delete matching pairs from left operand';
33 --
34 CREATE OR REPLACE FUNCTION public.jsonb_concat(jsonb, jsonb)
35 RETURNS jsonb
36     AS '$libdir/jsonb_opx', 'jsonb_concat_jsonb'
37 LANGUAGE C IMMUTABLE STRICT;
38 COMMENT ON FUNCTION public.jsonb_concat(jsonb, jsonb) IS 'concatenate first and second jsonb arguments';
39 -- DROP OPERATOR || (jsonb, jsonb);
40 CREATE OPERATOR || ( PROCEDURE = public.jsonb_concat, LEFTARG = jsonb, RIGHTARG = jsonb);
41 COMMENT ON OPERATOR || (jsonb, jsonb) IS 'concatenate jsonb types';
42 -------------------------------------------------------------------------------
43 -- Tests for jsonb - text
44 -------------------------------------------------------------------------------
45 -- text deletion from array containers will only delete string types as only strings can be keys:
46 SELECT '[1, "1", "2", 2]'::jsonb - '2'::text;
47   ?column?   
48 -------------
49  [1, "1", 2]
50 (1 row)
51
52 -- simple text deletion from an object container
53 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - 'b'::text;
54      ?column?     
55 ------------------
56  {"a": 1, "c": 3}
57 (1 row)
58
59 -- simple text deletion from an object container should only match keys
60 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - '3'::text;
61          ?column?         
62 --------------------------
63  {"a": 1, "b": 2, "c": 3}
64 (1 row)
65
66 -- others
67 SELECT '["1", "2", true, false]'::jsonb - '2'::text;
68       ?column?      
69 --------------------
70  ["1", true, false]
71 (1 row)
72
73 SELECT '["1", "2", "2", "2"]'::jsonb - '2'::text;
74  ?column? 
75 ----------
76  ["1"]
77 (1 row)
78
79 -------------------------------------------------------------------------------
80 -- Tests for jsonb - text[]
81 -------------------------------------------------------------------------------
82 -- text[] deletion from array containers will only delete string types as only strings can be keys:
83 SELECT '[1, "1", "2", 2]'::jsonb - array['1','2'];
84  ?column? 
85 ----------
86  [1, 2]
87 (1 row)
88
89 -- simple text[] deletion from an object container
90 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['a',' b'];
91      ?column?     
92 ------------------
93  {"b": 2, "c": 3}
94 (1 row)
95
96 -- simple text[] deletion from an object container should only match keys
97 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - ARRAY['1',' 2'];
98          ?column?         
99 --------------------------
100  {"a": 1, "b": 2, "c": 3}
101 (1 row)
102
103 -------------------------------------------------------------------------------
104 -- Tests for jsonb - jsonb
105 -------------------------------------------------------------------------------
106 -- jsonb deletion from an object should match on key/value
107 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
108      ?column?     
109 ------------------
110  {"a": 1, "c": 3}
111 (1 row)
112
113 -- jsonb deletion from an array should only match on key
114 SELECT '["a", "b", "c"]'::jsonb - '{"a": 4, "b": 2}'::jsonb;
115  ?column? 
116 ----------
117  ["c"]
118 (1 row)
119
120 -- jsonb deletion from nested objects should not be part matched
121 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"a": 4, "b": 2}'::jsonb;
122         ?column?         
123 -------------------------
124  {"c": 3, "d": {"a": 4}}
125 (1 row)
126
127 -- but a match of all nested values should narcg
128 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": 4}}'::jsonb - '{"d": {"a": 4}, "b": 2}'::jsonb;
129      ?column?     
130 ------------------
131  {"a": 4, "c": 3}
132 (1 row)
133
134 -- others
135 SELECT '{"a": 4, "b": 2, "c": 3, "d": {"a": false}}'::jsonb - '{"d": {"a": false}, "b": 2}'::jsonb;
136      ?column?     
137 ------------------
138  {"a": 4, "c": 3}
139 (1 row)
140
141 -------------------------------------------------------------------------------
142 -- Tests for jsonb || jsonb
143 -------------------------------------------------------------------------------
144 -- duplicates should automatically be removed by lower level logic
145 SELECT '{"a": 1, "b": 2, "c": 3}'::jsonb || '{"a": 4, "b": 2, "d": 4}'::jsonb;
146              ?column?             
147 ----------------------------------
148  {"a": 4, "b": 2, "c": 3, "d": 4}
149 (1 row)
150
151 -- concatentation of arrays
152 SELECT '["a", "b"]'::jsonb || '["c"]'::jsonb;
153     ?column?     
154 -----------------
155  ["a", "b", "c"]
156 (1 row)
157
158 -- concatentation of scalars and arrays should be wrapped into arrays
159 SELECT '["a", "b"]'::jsonb || '"c"'::jsonb;
160     ?column?     
161 -----------------
162  ["a", "b", "c"]
163 (1 row)
164
165 -- likewise concatentation of objects and arrays should be wrapped into arrays
166 SELECT '["a", "b"]'::jsonb || '{"a": 4, "b": 2}'::jsonb;
167            ?column?           
168 ------------------------------
169  ["a", "b", {"a": 4, "b": 2}]
170 (1 row)
171
172 -- and all concatentation should be in natural order supplied
173 SELECT '{"a": 4, "b": 2}'::jsonb || '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
174                 ?column?                
175 ----------------------------------------
176  [{"a": 4, "b": 2}, "a", "b", "c", "d"]
177 (1 row)
178
179 -- others
180 SELECT 'false'::jsonb || '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
181           ?column?           
182 -----------------------------
183  [false, "a", "b", "c", "d"]
184 (1 row)
185