Debian Codegraph @

contrib/cube/sql/cube.sql

raw
1 --
2 -- Test cube datatype
3 --
4
5 CREATE EXTENSION cube;
6
7 -- Check whether any of our opclasses fail amvalidate
8 SELECT amname, opcname
9 FROM pg_opclass opc LEFT JOIN pg_am am ON am.oid = opcmethod
10 WHERE opc.oid >= 16384 AND NOT amvalidate(opc.oid);
11
12 --
13 -- testing the input and output functions
14 --
15
16 -- Any number (a one-dimensional point)
17 SELECT '1'::cube AS cube;
18 SELECT '-1'::cube AS cube;
19 SELECT '1.'::cube AS cube;
20 SELECT '-1.'::cube AS cube;
21 SELECT '.1'::cube AS cube;
22 SELECT '-.1'::cube AS cube;
23 SELECT '1.0'::cube AS cube;
24 SELECT '-1.0'::cube AS cube;
25 SELECT 'infinity'::cube AS cube;
26 SELECT '-infinity'::cube AS cube;
27 SELECT 'NaN'::cube AS cube;
28 SELECT '.1234567890123456'::cube AS cube;
29 SELECT '+.1234567890123456'::cube AS cube;
30 SELECT '-.1234567890123456'::cube AS cube;
31
32 -- simple lists (points)
33 SELECT '()'::cube AS cube;
34 SELECT '1,2'::cube AS cube;
35 SELECT '(1,2)'::cube AS cube;
36 SELECT '1,2,3,4,5'::cube AS cube;
37 SELECT '(1,2,3,4,5)'::cube AS cube;
38
39 -- double lists (cubes)
40 SELECT '(),()'::cube AS cube;
41 SELECT '(0),(0)'::cube AS cube;
42 SELECT '(0),(1)'::cube AS cube;
43 SELECT '[(0),(0)]'::cube AS cube;
44 SELECT '[(0),(1)]'::cube AS cube;
45 SELECT '(0,0,0,0),(0,0,0,0)'::cube AS cube;
46 SELECT '(0,0,0,0),(1,0,0,0)'::cube AS cube;
47 SELECT '[(0,0,0,0),(0,0,0,0)]'::cube AS cube;
48 SELECT '[(0,0,0,0),(1,0,0,0)]'::cube AS cube;
49
50 -- invalid input: parse errors
51 SELECT ''::cube AS cube;
52 SELECT 'ABC'::cube AS cube;
53 SELECT '[]'::cube AS cube;
54 SELECT '[()]'::cube AS cube;
55 SELECT '[(1)]'::cube AS cube;
56 SELECT '[(1),]'::cube AS cube;
57 SELECT '[(1),2]'::cube AS cube;
58 SELECT '[(1),(2),(3)]'::cube AS cube;
59 SELECT '1,'::cube AS cube;
60 SELECT '1,2,'::cube AS cube;
61 SELECT '1,,2'::cube AS cube;
62 SELECT '(1,)'::cube AS cube;
63 SELECT '(1,2,)'::cube AS cube;
64 SELECT '(1,,2)'::cube AS cube;
65
66 -- invalid input: semantic errors and trailing garbage
67 SELECT '[(1),(2)],'::cube AS cube; -- 0
68 SELECT '[(1,2,3),(2,3)]'::cube AS cube; -- 1
69 SELECT '[(1,2),(1,2,3)]'::cube AS cube; -- 1
70 SELECT '(1),(2),'::cube AS cube; -- 2
71 SELECT '(1,2,3),(2,3)'::cube AS cube; -- 3
72 SELECT '(1,2),(1,2,3)'::cube AS cube; -- 3
73 SELECT '(1,2,3)ab'::cube AS cube; -- 4
74 SELECT '(1,2,3)a'::cube AS cube; -- 5
75 SELECT '(1,2)('::cube AS cube; -- 5
76 SELECT '1,2ab'::cube AS cube; -- 6
77 SELECT '1 e7'::cube AS cube; -- 6
78 SELECT '1,2a'::cube AS cube; -- 7
79 SELECT '1..2'::cube AS cube; -- 7
80 SELECT '-1e-700'::cube AS cube; -- out of range
81
82 -- Also try it with non-error-throwing API
83 SELECT pg_input_is_valid('(1,2)', 'cube');
84 SELECT pg_input_is_valid('[(1),]', 'cube');
85 SELECT pg_input_is_valid('-1e-700', 'cube');
86 SELECT * FROM pg_input_error_info('-1e-700', 'cube');
87
88 --
89 -- Testing building cubes from float8 values
90 --
91
92 SELECT cube(0::float8);
93 SELECT cube(1::float8);
94 SELECT cube(1,2);
95 SELECT cube(cube(1,2),3);
96 SELECT cube(cube(1,2),3,4);
97 SELECT cube(cube(cube(1,2),3,4),5);
98 SELECT cube(cube(cube(1,2),3,4),5,6);
99
100 --
101 -- Test that the text -> cube cast was installed.
102 --
103
104 SELECT '(0)'::text::cube;
105
106 --
107 -- Test the float[] -> cube cast
108 --
109 SELECT cube('{0,1,2}'::float[], '{3,4,5}'::float[]);
110 SELECT cube('{0,1,2}'::float[], '{3}'::float[]);
111 SELECT cube(NULL::float[], '{3}'::float[]);
112 SELECT cube('{0,1,2}'::float[]);
113 SELECT cube_subset(cube('(1,3,5),(6,7,8)'), ARRAY[3,2,1,1]);
114 SELECT cube_subset(cube('(1,3,5),(1,3,5)'), ARRAY[3,2,1,1]);
115 SELECT cube_subset(cube('(1,3,5),(6,7,8)'), ARRAY[4,0]);
116 SELECT cube_subset(cube('(6,7,8),(6,7,8)'), ARRAY[4,0]);
117 -- test for limits: this should pass
118 SELECT cube_subset(cube('(6,7,8),(6,7,8)'), array(SELECT 1 as a FROM generate_series(1,100)));
119 -- and this should fail
120 SELECT cube_subset(cube('(6,7,8),(6,7,8)'), array(SELECT 1 as a FROM generate_series(1,101)));
121
122
123
124 --
125 -- Test point processing
126 --
127 SELECT cube('(1,2),(1,2)'); -- cube_in
128 SELECT cube('{0,1,2}'::float[], '{0,1,2}'::float[]); -- cube_a_f8_f8
129 SELECT cube('{5,6,7,8}'::float[]); -- cube_a_f8
130 SELECT cube(1.37); -- cube_f8
131 SELECT cube(1.37, 1.37); -- cube_f8_f8
132 SELECT cube(cube(1,1), 42); -- cube_c_f8
133 SELECT cube(cube(1,2), 42); -- cube_c_f8
134 SELECT cube(cube(1,1), 42, 42); -- cube_c_f8_f8
135 SELECT cube(cube(1,1), 42, 24); -- cube_c_f8_f8
136 SELECT cube(cube(1,2), 42, 42); -- cube_c_f8_f8
137 SELECT cube(cube(1,2), 42, 24); -- cube_c_f8_f8
138
139 --
140 -- Testing limit of CUBE_MAX_DIM dimensions check in cube_in.
141 --
142 -- create too big cube from literal
143 select '(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'::cube;
144 select '(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'::cube;
145 -- from an array
146 select cube(array(SELECT 0 as a FROM generate_series(1,101)));
147 select cube(array(SELECT 0 as a FROM generate_series(1,101)),array(SELECT 0 as a FROM generate_series(1,101)));
148
149 -- extend cube beyond limit
150 -- this should work
151 select cube(array(SELECT 0 as a FROM generate_series(1,100)));
152 select cube(array(SELECT 0 as a FROM generate_series(1,100)),array(SELECT 0 as a FROM generate_series(1,100)));
153 -- this should fail
154 select cube(cube(array(SELECT 0 as a FROM generate_series(1,100))), 0);
155 select cube(cube(array(SELECT 0 as a FROM generate_series(1,100)),array(SELECT 0 as a FROM generate_series(1,100))), 0, 0);
156
157
158 --
159 -- testing the operators
160 --
161
162 -- equality/inequality:
163 --
164 SELECT '24, 33.20'::cube = '24, 33.20'::cube AS bool;
165 SELECT '24, 33.20'::cube != '24, 33.20'::cube AS bool;
166 SELECT '24, 33.20'::cube = '24, 33.21'::cube AS bool;
167 SELECT '24, 33.20'::cube != '24, 33.21'::cube AS bool;
168 SELECT '(2,0),(3,1)'::cube = '(2,0,0,0,0),(3,1,0,0,0)'::cube AS bool;
169 SELECT '(2,0),(3,1)'::cube = '(2,0,0,0,0),(3,1,0,0,1)'::cube AS bool;
170
171 -- "lower than" / "greater than"
172 -- (these operators are not useful for anything but ordering)
173 --
174 SELECT '1'::cube > '2'::cube AS bool;
175 SELECT '1'::cube < '2'::cube AS bool;
176 SELECT '1,1'::cube > '1,2'::cube AS bool;
177 SELECT '1,1'::cube < '1,2'::cube AS bool;
178
179 SELECT '(2,0),(3,1)'::cube > '(2,0,0,0,0),(3,1,0,0,1)'::cube AS bool;
180 SELECT '(2,0),(3,1)'::cube < '(2,0,0,0,0),(3,1,0,0,1)'::cube AS bool;
181 SELECT '(2,0),(3,1)'::cube > '(2,0,0,0,1),(3,1,0,0,0)'::cube AS bool;
182 SELECT '(2,0),(3,1)'::cube < '(2,0,0,0,1),(3,1,0,0,0)'::cube AS bool;
183 SELECT '(2,0),(3,1)'::cube > '(2,0,0,0,0),(3,1,0,0,0)'::cube AS bool;
184 SELECT '(2,0),(3,1)'::cube < '(2,0,0,0,0),(3,1,0,0,0)'::cube AS bool;
185 SELECT '(2,0,0,0,0),(3,1,0,0,1)'::cube > '(2,0),(3,1)'::cube AS bool;
186 SELECT '(2,0,0,0,0),(3,1,0,0,1)'::cube < '(2,0),(3,1)'::cube AS bool;
187 SELECT '(2,0,0,0,1),(3,1,0,0,0)'::cube > '(2,0),(3,1)'::cube AS bool;
188 SELECT '(2,0,0,0,1),(3,1,0,0,0)'::cube < '(2,0),(3,1)'::cube AS bool;
189 SELECT '(2,0,0,0,0),(3,1,0,0,0)'::cube > '(2,0),(3,1)'::cube AS bool;
190 SELECT '(2,0,0,0,0),(3,1,0,0,0)'::cube < '(2,0),(3,1)'::cube AS bool;
191
192
193 -- "overlap"
194 --
195 SELECT '1'::cube && '1'::cube AS bool;
196 SELECT '1'::cube && '2'::cube AS bool;
197
198 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '0'::cube AS bool;
199 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '1'::cube AS bool;
200 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '1,1,1'::cube AS bool;
201 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '[(1,1,1),(2,2,2)]'::cube AS bool;
202 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '[(1,1),(2,2)]'::cube AS bool;
203 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '[(2,1,1),(2,2,2)]'::cube AS bool;
204
205
206 -- "contained in" (the left operand is the cube entirely enclosed by
207 -- the right operand):
208 --
209 SELECT '0'::cube <@ '0'::cube AS bool;
210 SELECT '0,0,0'::cube <@ '0,0,0'::cube AS bool;
211 SELECT '0,0'::cube <@ '0,0,1'::cube AS bool;
212 SELECT '0,0,0'::cube <@ '0,0,1'::cube AS bool;
213 SELECT '1,0,0'::cube <@ '0,0,1'::cube AS bool;
214 SELECT '(1,0,0),(0,0,1)'::cube <@ '(1,0,0),(0,0,1)'::cube AS bool;
215 SELECT '(1,0,0),(0,0,1)'::cube <@ '(-1,-1,-1),(1,1,1)'::cube AS bool;
216 SELECT '(1,0,0),(0,0,1)'::cube <@ '(-1,-1,-1,-1),(1,1,1,1)'::cube AS bool;
217 SELECT '0'::cube <@ '(-1),(1)'::cube AS bool;
218 SELECT '1'::cube <@ '(-1),(1)'::cube AS bool;
219 SELECT '-1'::cube <@ '(-1),(1)'::cube AS bool;
220 SELECT '(-1),(1)'::cube <@ '(-1),(1)'::cube AS bool;
221 SELECT '(-1),(1)'::cube <@ '(-1,-1),(1,1)'::cube AS bool;
222 SELECT '(-2),(1)'::cube <@ '(-1),(1)'::cube AS bool;
223 SELECT '(-2),(1)'::cube <@ '(-1,-1),(1,1)'::cube AS bool;
224
225
226 -- "contains" (the left operand is the cube that entirely encloses the
227 -- right operand)
228 --
229 SELECT '0'::cube @> '0'::cube AS bool;
230 SELECT '0,0,0'::cube @> '0,0,0'::cube AS bool;
231 SELECT '0,0,1'::cube @> '0,0'::cube AS bool;
232 SELECT '0,0,1'::cube @> '0,0,0'::cube AS bool;
233 SELECT '0,0,1'::cube @> '1,0,0'::cube AS bool;
234 SELECT '(1,0,0),(0,0,1)'::cube @> '(1,0,0),(0,0,1)'::cube AS bool;
235 SELECT '(-1,-1,-1),(1,1,1)'::cube @> '(1,0,0),(0,0,1)'::cube AS bool;
236 SELECT '(-1,-1,-1,-1),(1,1,1,1)'::cube @> '(1,0,0),(0,0,1)'::cube AS bool;
237 SELECT '(-1),(1)'::cube @> '0'::cube AS bool;
238 SELECT '(-1),(1)'::cube @> '1'::cube AS bool;
239 SELECT '(-1),(1)'::cube @> '-1'::cube AS bool;
240 SELECT '(-1),(1)'::cube @> '(-1),(1)'::cube AS bool;
241 SELECT '(-1,-1),(1,1)'::cube @> '(-1),(1)'::cube AS bool;
242 SELECT '(-1),(1)'::cube @> '(-2),(1)'::cube AS bool;
243 SELECT '(-1,-1),(1,1)'::cube @> '(-2),(1)'::cube AS bool;
244
245 -- Test of distance function
246 --
247 SELECT cube_distance('(0)'::cube,'(2,2,2,2)'::cube);
248 SELECT cube_distance('(0)'::cube,'(.3,.4)'::cube);
249 SELECT cube_distance('(2,3,4)'::cube,'(2,3,4)'::cube);
250 SELECT cube_distance('(42,42,42,42)'::cube,'(137,137,137,137)'::cube);
251 SELECT cube_distance('(42,42,42)'::cube,'(137,137)'::cube);
252
253 -- Test of cube function (text to cube)
254 --
255 SELECT cube('(1,1.2)'::text);
256 SELECT cube(NULL);
257
258 -- Test of cube_dim function (dimensions stored in cube)
259 --
260 SELECT cube_dim('(0)'::cube);
261 SELECT cube_dim('(0,0)'::cube);
262 SELECT cube_dim('(0,0,0)'::cube);
263 SELECT cube_dim('(42,42,42),(42,42,42)'::cube);
264 SELECT cube_dim('(4,8,15,16,23),(4,8,15,16,23)'::cube);
265
266 -- Test of cube_ll_coord function (retrieves LL coordinate values)
267 --
268 SELECT cube_ll_coord('(-1,1),(2,-2)'::cube, 1);
269 SELECT cube_ll_coord('(-1,1),(2,-2)'::cube, 2);
270 SELECT cube_ll_coord('(-1,1),(2,-2)'::cube, 3);
271 SELECT cube_ll_coord('(1,2),(1,2)'::cube, 1);
272 SELECT cube_ll_coord('(1,2),(1,2)'::cube, 2);
273 SELECT cube_ll_coord('(1,2),(1,2)'::cube, 3);
274 SELECT cube_ll_coord('(42,137)'::cube, 1);
275 SELECT cube_ll_coord('(42,137)'::cube, 2);
276 SELECT cube_ll_coord('(42,137)'::cube, 3);
277
278 -- Test of cube_ur_coord function (retrieves UR coordinate values)
279 --
280 SELECT cube_ur_coord('(-1,1),(2,-2)'::cube, 1);
281 SELECT cube_ur_coord('(-1,1),(2,-2)'::cube, 2);
282 SELECT cube_ur_coord('(-1,1),(2,-2)'::cube, 3);
283 SELECT cube_ur_coord('(1,2),(1,2)'::cube, 1);
284 SELECT cube_ur_coord('(1,2),(1,2)'::cube, 2);
285 SELECT cube_ur_coord('(1,2),(1,2)'::cube, 3);
286 SELECT cube_ur_coord('(42,137)'::cube, 1);
287 SELECT cube_ur_coord('(42,137)'::cube, 2);
288 SELECT cube_ur_coord('(42,137)'::cube, 3);
289
290 -- Test of cube_is_point
291 --
292 SELECT cube_is_point('(0)'::cube);
293 SELECT cube_is_point('(0,1,2)'::cube);
294 SELECT cube_is_point('(0,1,2),(0,1,2)'::cube);
295 SELECT cube_is_point('(0,1,2),(-1,1,2)'::cube);
296 SELECT cube_is_point('(0,1,2),(0,-1,2)'::cube);
297 SELECT cube_is_point('(0,1,2),(0,1,-2)'::cube);
298
299 -- Test of cube_enlarge (enlarging and shrinking cubes)
300 --
301 SELECT cube_enlarge('(0)'::cube, 0, 0);
302 SELECT cube_enlarge('(0)'::cube, 0, 1);
303 SELECT cube_enlarge('(0)'::cube, 0, 2);
304 SELECT cube_enlarge('(2),(-2)'::cube, 0, 4);
305 SELECT cube_enlarge('(0)'::cube, 1, 0);
306 SELECT cube_enlarge('(0)'::cube, 1, 1);
307 SELECT cube_enlarge('(0)'::cube, 1, 2);
308 SELECT cube_enlarge('(2),(-2)'::cube, 1, 4);
309 SELECT cube_enlarge('(0)'::cube, -1, 0);
310 SELECT cube_enlarge('(0)'::cube, -1, 1);
311 SELECT cube_enlarge('(0)'::cube, -1, 2);
312 SELECT cube_enlarge('(2),(-2)'::cube, -1, 4);
313 SELECT cube_enlarge('(0,0,0)'::cube, 1, 0);
314 SELECT cube_enlarge('(0,0,0)'::cube, 1, 2);
315 SELECT cube_enlarge('(2,-2),(-3,7)'::cube, 1, 2);
316 SELECT cube_enlarge('(2,-2),(-3,7)'::cube, 3, 2);
317 SELECT cube_enlarge('(2,-2),(-3,7)'::cube, -1, 2);
318 SELECT cube_enlarge('(2,-2),(-3,7)'::cube, -3, 2);
319 SELECT cube_enlarge('(42,-23,-23),(42,23,23)'::cube, -23, 5);
320 SELECT cube_enlarge('(42,-23,-23),(42,23,23)'::cube, -24, 5);
321
322 -- Test of cube_union (MBR for two cubes)
323 --
324 SELECT cube_union('(1,2),(3,4)'::cube, '(5,6,7),(8,9,10)'::cube);
325 SELECT cube_union('(1,2)'::cube, '(4,2,0,0)'::cube);
326 SELECT cube_union('(1,2),(1,2)'::cube, '(4,2),(4,2)'::cube);
327 SELECT cube_union('(1,2),(1,2)'::cube, '(1,2),(1,2)'::cube);
328 SELECT cube_union('(1,2),(1,2)'::cube, '(1,2,0),(1,2,0)'::cube);
329
330 -- Test of cube_inter
331 --
332 SELECT cube_inter('(1,2),(10,11)'::cube, '(3,4), (16,15)'::cube); -- intersects
333 SELECT cube_inter('(1,2),(10,11)'::cube, '(3,4), (6,5)'::cube); -- includes
334 SELECT cube_inter('(1,2),(10,11)'::cube, '(13,14), (16,15)'::cube); -- no intersection
335 SELECT cube_inter('(1,2),(10,11)'::cube, '(3,14), (16,15)'::cube); -- no intersection, but one dimension intersects
336 SELECT cube_inter('(1,2),(10,11)'::cube, '(10,11), (16,15)'::cube); -- point intersection
337 SELECT cube_inter('(1,2,3)'::cube, '(1,2,3)'::cube); -- point args
338 SELECT cube_inter('(1,2,3)'::cube, '(5,6,3)'::cube); -- point args
339
340 -- Test of cube_size
341 --
342 SELECT cube_size('(4,8),(15,16)'::cube);
343 SELECT cube_size('(42,137)'::cube);
344
345 -- Test of distances (euclidean distance may not be bit-exact)
346 --
347 SET extra_float_digits = 0;
348 SELECT cube_distance('(1,1)'::cube, '(4,5)'::cube);
349 SELECT '(1,1)'::cube <-> '(4,5)'::cube as d_e;
350 RESET extra_float_digits;
351 SELECT distance_chebyshev('(1,1)'::cube, '(4,5)'::cube);
352 SELECT '(1,1)'::cube <=> '(4,5)'::cube as d_c;
353 SELECT distance_taxicab('(1,1)'::cube, '(4,5)'::cube);
354 SELECT '(1,1)'::cube <#> '(4,5)'::cube as d_t;
355 -- zero for overlapping
356 SELECT cube_distance('(2,2),(10,10)'::cube, '(0,0),(5,5)'::cube);
357 SELECT distance_chebyshev('(2,2),(10,10)'::cube, '(0,0),(5,5)'::cube);
358 SELECT distance_taxicab('(2,2),(10,10)'::cube, '(0,0),(5,5)'::cube);
359 -- coordinate access
360 SELECT cube(array[10,20,30], array[40,50,60])->1;
361 SELECT cube(array[40,50,60], array[10,20,30])->1;
362 SELECT cube(array[10,20,30], array[40,50,60])->6;
363 SELECT cube(array[10,20,30], array[40,50,60])->0;
364 SELECT cube(array[10,20,30], array[40,50,60])->7;
365 SELECT cube(array[10,20,30], array[40,50,60])->-1;
366 SELECT cube(array[10,20,30], array[40,50,60])->-6;
367 SELECT cube(array[10,20,30])->3;
368 SELECT cube(array[10,20,30])->6;
369 SELECT cube(array[10,20,30])->-6;
370 -- "normalized" coordinate access
371 SELECT cube(array[10,20,30], array[40,50,60])~>1;
372 SELECT cube(array[40,50,60], array[10,20,30])~>1;
373 SELECT cube(array[10,20,30], array[40,50,60])~>2;
374 SELECT cube(array[40,50,60], array[10,20,30])~>2;
375 SELECT cube(array[10,20,30], array[40,50,60])~>3;
376 SELECT cube(array[40,50,60], array[10,20,30])~>3;
377
378 SELECT cube(array[40,50,60], array[10,20,30])~>0;
379 SELECT cube(array[40,50,60], array[10,20,30])~>4;
380 SELECT cube(array[40,50,60], array[10,20,30])~>(-1);
381
382 -- Load some example data and build the index
383 --
384 CREATE TABLE test_cube (c cube);
385
386 \copy test_cube from 'data/test_cube.data'
387
388 CREATE INDEX test_cube_ix ON test_cube USING gist (c);
389 SELECT * FROM test_cube WHERE c && '(3000,1000),(0,0)' ORDER BY c;
390
391 -- Test sorting
392 SELECT * FROM test_cube WHERE c && '(3000,1000),(0,0)' GROUP BY c ORDER BY c;
393
394 -- Test index-only scans
395 SET enable_bitmapscan = false;
396 EXPLAIN (COSTS OFF)
397 SELECT c FROM test_cube WHERE c <@ '(3000,1000),(0,0)' ORDER BY c;
398 SELECT c FROM test_cube WHERE c <@ '(3000,1000),(0,0)' ORDER BY c;
399 RESET enable_bitmapscan;
400
401 -- Test kNN
402 INSERT INTO test_cube VALUES ('(1,1)'), ('(100000)'), ('(0, 100000)'); -- Some corner cases
403 SET enable_seqscan = false;
404
405 -- Test different metrics
406 SET extra_float_digits = 0;
407 SELECT *, c <-> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <-> '(100, 100),(500, 500)'::cube LIMIT 5;
408 RESET extra_float_digits;
409 SELECT *, c <=> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <=> '(100, 100),(500, 500)'::cube LIMIT 5;
410 SELECT *, c <#> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <#> '(100, 100),(500, 500)'::cube LIMIT 5;
411
412 -- Test sorting by coordinates
413 SELECT c~>1, c FROM test_cube ORDER BY c~>1 LIMIT 15; -- ascending by left bound
414 SELECT c~>2, c FROM test_cube ORDER BY c~>2 LIMIT 15; -- ascending by right bound
415 SELECT c~>3, c FROM test_cube ORDER BY c~>3 LIMIT 15; -- ascending by lower bound
416 SELECT c~>4, c FROM test_cube ORDER BY c~>4 LIMIT 15; -- ascending by upper bound
417 SELECT c~>(-1), c FROM test_cube ORDER BY c~>(-1) LIMIT 15; -- descending by left bound
418 SELECT c~>(-2), c FROM test_cube ORDER BY c~>(-2) LIMIT 15; -- descending by right bound
419 SELECT c~>(-3), c FROM test_cube ORDER BY c~>(-3) LIMIT 15; -- descending by lower bound
420 SELECT c~>(-4), c FROM test_cube ORDER BY c~>(-4) LIMIT 15; -- descending by upper bound
421
422 -- Same queries with sequential scan (should give the same results as above)
423 RESET enable_seqscan;
424 SET enable_indexscan = OFF;
425 SET extra_float_digits = 0;
426 SELECT *, c <-> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <-> '(100, 100),(500, 500)'::cube LIMIT 5;
427 RESET extra_float_digits;
428 SELECT *, c <=> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <=> '(100, 100),(500, 500)'::cube LIMIT 5;
429 SELECT *, c <#> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <#> '(100, 100),(500, 500)'::cube LIMIT 5;
430 SELECT c~>1, c FROM test_cube ORDER BY c~>1 LIMIT 15; -- ascending by left bound
431 SELECT c~>2, c FROM test_cube ORDER BY c~>2 LIMIT 15; -- ascending by right bound
432 SELECT c~>3, c FROM test_cube ORDER BY c~>3 LIMIT 15; -- ascending by lower bound
433 SELECT c~>4, c FROM test_cube ORDER BY c~>4 LIMIT 15; -- ascending by upper bound
434 SELECT c~>(-1), c FROM test_cube ORDER BY c~>(-1) LIMIT 15; -- descending by left bound
435 SELECT c~>(-2), c FROM test_cube ORDER BY c~>(-2) LIMIT 15; -- descending by right bound
436 SELECT c~>(-3), c FROM test_cube ORDER BY c~>(-3) LIMIT 15; -- descending by lower bound
437 SELECT c~>(-4), c FROM test_cube ORDER BY c~>(-4) LIMIT 15; -- descending by upper bound
438 RESET enable_indexscan;