Debian Codegraph @

contrib/intarray/sql/_int.sql

raw
1 CREATE EXTENSION intarray;
2
3 -- Check whether any of our opclasses fail amvalidate
4 SELECT amname, opcname
5 FROM pg_opclass opc LEFT JOIN pg_am am ON am.oid = opcmethod
6 WHERE opc.oid >= 16384 AND NOT amvalidate(opc.oid);
7
8 SELECT intset(1234);
9 SELECT icount('{1234234,234234}');
10 SELECT sort('{1234234,-30,234234}');
11 SELECT sort('{1234234,-30,234234}','asc');
12 SELECT sort('{1234234,-30,234234}','desc');
13 SELECT sort_asc('{1234234,-30,234234}');
14 SELECT sort_desc('{1234234,-30,234234}');
15 SELECT uniq('{1234234,-30,-30,234234,-30}');
16 SELECT uniq(sort_asc('{1234234,-30,-30,234234,-30}'));
17 SELECT idx('{1234234,-30,-30,234234,-30}',-30);
18 SELECT subarray('{1234234,-30,-30,234234,-30}',2,3);
19 SELECT subarray('{1234234,-30,-30,234234,-30}',-1,1);
20 SELECT subarray('{1234234,-30,-30,234234,-30}',0,-1);
21
22 SELECT #'{1234234,234234}'::int[];
23 SELECT '{123,623,445}'::int[] + 1245;
24 SELECT '{123,623,445}'::int[] + 445;
25 SELECT '{123,623,445}'::int[] + '{1245,87,445}';
26 SELECT '{123,623,445}'::int[] - 623;
27 SELECT '{123,623,445}'::int[] - '{1623,623}';
28 SELECT '{123,623,445}'::int[] | 623;
29 SELECT '{123,623,445}'::int[] | 1623;
30 SELECT '{123,623,445}'::int[] | '{1623,623}';
31 SELECT '{123,623,445}'::int[] & '{1623,623}';
32 SELECT '{-1,3,1}'::int[] & '{1,2}';
33 SELECT '{1}'::int[] & '{2}'::int[];
34 SELECT array_dims('{1}'::int[] & '{2}'::int[]);
35 SELECT ('{1}'::int[] & '{2}'::int[]) = '{}'::int[];
36 SELECT ('{}'::int[] & '{}'::int[]) = '{}'::int[];
37
38
39 --test query_int
40 SELECT '1'::query_int;
41 SELECT ' 1'::query_int;
42 SELECT '1 '::query_int;
43 SELECT ' 1 '::query_int;
44 SELECT ' ! 1 '::query_int;
45 SELECT '!1'::query_int;
46 SELECT '1|2'::query_int;
47 SELECT '1|!2'::query_int;
48 SELECT '!1|2'::query_int;
49 SELECT '!1|!2'::query_int;
50 SELECT '!(!1|!2)'::query_int;
51 SELECT '!(!1|2)'::query_int;
52 SELECT '!(1|!2)'::query_int;
53 SELECT '!(1|2)'::query_int;
54 SELECT '1&2'::query_int;
55 SELECT '!1&2'::query_int;
56 SELECT '1&!2'::query_int;
57 SELECT '!1&!2'::query_int;
58 SELECT '(1&2)'::query_int;
59 SELECT '1&(2)'::query_int;
60 SELECT '!(1)&2'::query_int;
61 SELECT '!(1&2)'::query_int;
62 SELECT '1|2&3'::query_int;
63 SELECT '1|(2&3)'::query_int;
64 SELECT '(1|2)&3'::query_int;
65 SELECT '1|2&!3'::query_int;
66 SELECT '1|!2&3'::query_int;
67 SELECT '!1|2&3'::query_int;
68 SELECT '!1|(2&3)'::query_int;
69 SELECT '!(1|2)&3'::query_int;
70 SELECT '(!1|2)&3'::query_int;
71 SELECT '1|(2|(4|(5|6)))'::query_int;
72 SELECT '1|2|4|5|6'::query_int;
73 SELECT '1&(2&(4&(5&6)))'::query_int;
74 SELECT '1&2&4&5&6'::query_int;
75 SELECT '1&(2&(4&(5|6)))'::query_int;
76 SELECT '1&(2&(4&(5|!6)))'::query_int;
77
78 -- Test for overflow of the int16 "left" field in findoprnd().
79 -- This query uses a balanced binary tree to avoid using too much stack.
80 DO $$
81 DECLARE
82 e text := '1';
83 BEGIN
84 FOR i IN 1..15 LOOP
85 e := '(' || e || '&' || e || ')';
86 END LOOP;
87 PERFORM ('0|' || e)::query_int;
88 END;
89 $$;
90
91 -- test non-error-throwing input
92
93 SELECT str as "query_int",
94 pg_input_is_valid(str,'query_int') as ok,
95 errinfo.sql_error_code,
96 errinfo.message,
97 errinfo.detail,
98 errinfo.hint
99 FROM (VALUES ('1&(2&(4&(5|6)))'),
100 ('1#(2&(4&(5&6)))'),
101 ('foo'))
102 AS a(str),
103 LATERAL pg_input_error_info(a.str, 'query_int') as errinfo;
104
105
106
107 CREATE TABLE test__int( a int[] );
108 \copy test__int from 'data/test__int.data'
109 ANALYZE test__int;
110
111 SELECT count(*) from test__int WHERE a && '{23,50}';
112 SELECT count(*) from test__int WHERE a @@ '23|50';
113 SELECT count(*) from test__int WHERE a @> '{23,50}';
114 SELECT count(*) from test__int WHERE a @@ '23&50';
115 SELECT count(*) from test__int WHERE a @> '{20,23}';
116 SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
117 SELECT count(*) from test__int WHERE a = '{73,23,20}';
118 SELECT count(*) from test__int WHERE a @@ '50&68';
119 SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
120 SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
121 SELECT count(*) from test__int WHERE a @@ '20 | !21';
122 SELECT count(*) from test__int WHERE a @@ '!20 & !21';
123 SELECT count(*) from test__int WHERE a @@ '!2733 & (2738 | 254)';
124
125 SET enable_seqscan = off; -- not all of these would use index by default
126
127 CREATE INDEX text_idx on test__int using gist ( a gist__int_ops );
128
129 SELECT count(*) from test__int WHERE a && '{23,50}';
130 SELECT count(*) from test__int WHERE a @@ '23|50';
131 SELECT count(*) from test__int WHERE a @> '{23,50}';
132 SELECT count(*) from test__int WHERE a @@ '23&50';
133 SELECT count(*) from test__int WHERE a @> '{20,23}';
134 SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
135 SELECT count(*) from test__int WHERE a = '{73,23,20}';
136 SELECT count(*) from test__int WHERE a @@ '50&68';
137 SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
138 SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
139 SELECT count(*) from test__int WHERE a @@ '20 | !21';
140 SELECT count(*) from test__int WHERE a @@ '!20 & !21';
141 SELECT count(*) from test__int WHERE a @@ '!2733 & (2738 | 254)';
142
143 INSERT INTO test__int SELECT array(SELECT x FROM generate_series(1, 1001) x); -- should fail
144
145 DROP INDEX text_idx;
146 CREATE INDEX text_idx on test__int using gist (a gist__int_ops(numranges = 0));
147 CREATE INDEX text_idx on test__int using gist (a gist__int_ops(numranges = 253));
148 CREATE INDEX text_idx on test__int using gist (a gist__int_ops(numranges = 252));
149
150 SELECT count(*) from test__int WHERE a && '{23,50}';
151 SELECT count(*) from test__int WHERE a @@ '23|50';
152 SELECT count(*) from test__int WHERE a @> '{23,50}';
153 SELECT count(*) from test__int WHERE a @@ '23&50';
154 SELECT count(*) from test__int WHERE a @> '{20,23}';
155 SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
156 SELECT count(*) from test__int WHERE a = '{73,23,20}';
157 SELECT count(*) from test__int WHERE a @@ '50&68';
158 SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
159 SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
160 SELECT count(*) from test__int WHERE a @@ '20 | !21';
161 SELECT count(*) from test__int WHERE a @@ '!20 & !21';
162 SELECT count(*) from test__int WHERE a @@ '!2733 & (2738 | 254)';
163
164 DROP INDEX text_idx;
165 CREATE INDEX text_idx on test__int using gist (a gist__intbig_ops(siglen = 0));
166 CREATE INDEX text_idx on test__int using gist (a gist__intbig_ops(siglen = 2025));
167 CREATE INDEX text_idx on test__int using gist (a gist__intbig_ops(siglen = 2024));
168
169 SELECT count(*) from test__int WHERE a && '{23,50}';
170 SELECT count(*) from test__int WHERE a @@ '23|50';
171 SELECT count(*) from test__int WHERE a @> '{23,50}';
172 SELECT count(*) from test__int WHERE a @@ '23&50';
173 SELECT count(*) from test__int WHERE a @> '{20,23}';
174 SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
175 SELECT count(*) from test__int WHERE a = '{73,23,20}';
176 SELECT count(*) from test__int WHERE a @@ '50&68';
177 SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
178 SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
179 SELECT count(*) from test__int WHERE a @@ '20 | !21';
180 SELECT count(*) from test__int WHERE a @@ '!20 & !21';
181 SELECT count(*) from test__int WHERE a @@ '!2733 & (2738 | 254)';
182
183 DROP INDEX text_idx;
184 CREATE INDEX text_idx on test__int using gist ( a gist__intbig_ops );
185
186 SELECT count(*) from test__int WHERE a && '{23,50}';
187 SELECT count(*) from test__int WHERE a @@ '23|50';
188 SELECT count(*) from test__int WHERE a @> '{23,50}';
189 SELECT count(*) from test__int WHERE a @@ '23&50';
190 SELECT count(*) from test__int WHERE a @> '{20,23}';
191 SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
192 SELECT count(*) from test__int WHERE a = '{73,23,20}';
193 SELECT count(*) from test__int WHERE a @@ '50&68';
194 SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
195 SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
196 SELECT count(*) from test__int WHERE a @@ '20 | !21';
197 SELECT count(*) from test__int WHERE a @@ '!20 & !21';
198 SELECT count(*) from test__int WHERE a @@ '!2733 & (2738 | 254)';
199
200 DROP INDEX text_idx;
201 CREATE INDEX text_idx on test__int using gin ( a gin__int_ops );
202
203 SELECT count(*) from test__int WHERE a && '{23,50}';
204 SELECT count(*) from test__int WHERE a @@ '23|50';
205 SELECT count(*) from test__int WHERE a @> '{23,50}';
206 SELECT count(*) from test__int WHERE a @@ '23&50';
207 SELECT count(*) from test__int WHERE a @> '{20,23}';
208 SELECT count(*) from test__int WHERE a <@ '{73,23,20}';
209 SELECT count(*) from test__int WHERE a = '{73,23,20}';
210 SELECT count(*) from test__int WHERE a @@ '50&68';
211 SELECT count(*) from test__int WHERE a @> '{20,23}' or a @> '{50,68}';
212 SELECT count(*) from test__int WHERE a @@ '(20&23)|(50&68)';
213 SELECT count(*) from test__int WHERE a @@ '20 | !21';
214 SELECT count(*) from test__int WHERE a @@ '!20 & !21';
215 SELECT count(*) from test__int WHERE a @@ '!2733 & (2738 | 254)';
216
217 DROP INDEX text_idx;
218
219 -- Repeat the same queries with an extended data set. The data set is the
220 -- same that we used before, except that each element in the array is
221 -- repeated three times, offset by 1000 and 2000. For example, {1, 5}
222 -- becomes {1, 1001, 2001, 5, 1005, 2005}.
223 --
224 -- That has proven to be unreasonably effective at exercising codepaths in
225 -- core GiST code related to splitting parent pages, which is not covered by
226 -- other tests. This is a bit out-of-place as the point is to test core GiST
227 -- code rather than this extension, but there is no suitable GiST opclass in
228 -- core that would reach the same codepaths.
229 CREATE TABLE more__int AS SELECT
230 -- Leave alone NULLs, empty arrays and the one row that we use to test
231 -- equality; also skip INT_MAX
232 CASE WHEN a IS NULL OR a = '{}' OR a = '{73,23,20}' THEN a ELSE
233 (select array_agg(u) || array_agg(u + 1000) || array_agg(u + 2000)
234 from unnest(a) u where u < 2000000000)
235 END AS a, a as b
236 FROM test__int;
237 CREATE INDEX ON more__int using gist (a gist__int_ops(numranges = 252));
238
239 SELECT count(*) from more__int WHERE a && '{23,50}';
240 SELECT count(*) from more__int WHERE a @@ '23|50';
241 SELECT count(*) from more__int WHERE a @> '{23,50}';
242 SELECT count(*) from more__int WHERE a @@ '23&50';
243 SELECT count(*) from more__int WHERE a @> '{20,23}';
244 SELECT count(*) from more__int WHERE a <@ '{73,23,20}';
245 SELECT count(*) from more__int WHERE a = '{73,23,20}';
246 SELECT count(*) from more__int WHERE a @@ '50&68';
247 SELECT count(*) from more__int WHERE a @> '{20,23}' or a @> '{50,68}';
248 SELECT count(*) from more__int WHERE a @@ '(20&23)|(50&68)';
249 SELECT count(*) from more__int WHERE a @@ '20 | !21';
250 SELECT count(*) from more__int WHERE a @@ '!20 & !21';
251 SELECT count(*) from test__int WHERE a @@ '!2733 & (2738 | 254)';
252
253
254 RESET enable_seqscan;