Debian Codegraph @

contrib/cube/expected/cube.out

raw
1 --
2 -- Test cube datatype
3 --
4 CREATE EXTENSION cube;
5 -- Check whether any of our opclasses fail amvalidate
6 SELECT amname, opcname
7 FROM pg_opclass opc LEFT JOIN pg_am am ON am.oid = opcmethod
8 WHERE opc.oid >= 16384 AND NOT amvalidate(opc.oid);
9 amname | opcname
10 --------+---------
11 (0 rows)
12
13 --
14 -- testing the input and output functions
15 --
16 -- Any number (a one-dimensional point)
17 SELECT '1'::cube AS cube;
18 cube
19 ------
20 (1)
21 (1 row)
22
23 SELECT '-1'::cube AS cube;
24 cube
25 ------
26 (-1)
27 (1 row)
28
29 SELECT '1.'::cube AS cube;
30 cube
31 ------
32 (1)
33 (1 row)
34
35 SELECT '-1.'::cube AS cube;
36 cube
37 ------
38 (-1)
39 (1 row)
40
41 SELECT '.1'::cube AS cube;
42 cube
43 -------
44 (0.1)
45 (1 row)
46
47 SELECT '-.1'::cube AS cube;
48 cube
49 --------
50 (-0.1)
51 (1 row)
52
53 SELECT '1.0'::cube AS cube;
54 cube
55 ------
56 (1)
57 (1 row)
58
59 SELECT '-1.0'::cube AS cube;
60 cube
61 ------
62 (-1)
63 (1 row)
64
65 SELECT 'infinity'::cube AS cube;
66 cube
67 ------------
68 (Infinity)
69 (1 row)
70
71 SELECT '-infinity'::cube AS cube;
72 cube
73 -------------
74 (-Infinity)
75 (1 row)
76
77 SELECT 'NaN'::cube AS cube;
78 cube
79 -------
80 (NaN)
81 (1 row)
82
83 SELECT '.1234567890123456'::cube AS cube;
84 cube
85 ----------------------
86 (0.1234567890123456)
87 (1 row)
88
89 SELECT '+.1234567890123456'::cube AS cube;
90 cube
91 ----------------------
92 (0.1234567890123456)
93 (1 row)
94
95 SELECT '-.1234567890123456'::cube AS cube;
96 cube
97 -----------------------
98 (-0.1234567890123456)
99 (1 row)
100
101 -- simple lists (points)
102 SELECT '()'::cube AS cube;
103 cube
104 ------
105 ()
106 (1 row)
107
108 SELECT '1,2'::cube AS cube;
109 cube
110 --------
111 (1, 2)
112 (1 row)
113
114 SELECT '(1,2)'::cube AS cube;
115 cube
116 --------
117 (1, 2)
118 (1 row)
119
120 SELECT '1,2,3,4,5'::cube AS cube;
121 cube
122 -----------------
123 (1, 2, 3, 4, 5)
124 (1 row)
125
126 SELECT '(1,2,3,4,5)'::cube AS cube;
127 cube
128 -----------------
129 (1, 2, 3, 4, 5)
130 (1 row)
131
132 -- double lists (cubes)
133 SELECT '(),()'::cube AS cube;
134 cube
135 ------
136 ()
137 (1 row)
138
139 SELECT '(0),(0)'::cube AS cube;
140 cube
141 ------
142 (0)
143 (1 row)
144
145 SELECT '(0),(1)'::cube AS cube;
146 cube
147 ---------
148 (0),(1)
149 (1 row)
150
151 SELECT '[(0),(0)]'::cube AS cube;
152 cube
153 ------
154 (0)
155 (1 row)
156
157 SELECT '[(0),(1)]'::cube AS cube;
158 cube
159 ---------
160 (0),(1)
161 (1 row)
162
163 SELECT '(0,0,0,0),(0,0,0,0)'::cube AS cube;
164 cube
165 --------------
166 (0, 0, 0, 0)
167 (1 row)
168
169 SELECT '(0,0,0,0),(1,0,0,0)'::cube AS cube;
170 cube
171 ---------------------------
172 (0, 0, 0, 0),(1, 0, 0, 0)
173 (1 row)
174
175 SELECT '[(0,0,0,0),(0,0,0,0)]'::cube AS cube;
176 cube
177 --------------
178 (0, 0, 0, 0)
179 (1 row)
180
181 SELECT '[(0,0,0,0),(1,0,0,0)]'::cube AS cube;
182 cube
183 ---------------------------
184 (0, 0, 0, 0),(1, 0, 0, 0)
185 (1 row)
186
187 -- invalid input: parse errors
188 SELECT ''::cube AS cube;
189 ERROR: invalid input syntax for cube
190 LINE 1: SELECT ''::cube AS cube;
191 ^
192 DETAIL: syntax error at end of input
193 SELECT 'ABC'::cube AS cube;
194 ERROR: invalid input syntax for cube
195 LINE 1: SELECT 'ABC'::cube AS cube;
196 ^
197 DETAIL: syntax error at or near "A"
198 SELECT '[]'::cube AS cube;
199 ERROR: invalid input syntax for cube
200 LINE 1: SELECT '[]'::cube AS cube;
201 ^
202 DETAIL: syntax error at or near "]"
203 SELECT '[()]'::cube AS cube;
204 ERROR: invalid input syntax for cube
205 LINE 1: SELECT '[()]'::cube AS cube;
206 ^
207 DETAIL: syntax error at or near "]"
208 SELECT '[(1)]'::cube AS cube;
209 ERROR: invalid input syntax for cube
210 LINE 1: SELECT '[(1)]'::cube AS cube;
211 ^
212 DETAIL: syntax error at or near "]"
213 SELECT '[(1),]'::cube AS cube;
214 ERROR: invalid input syntax for cube
215 LINE 1: SELECT '[(1),]'::cube AS cube;
216 ^
217 DETAIL: syntax error at or near "]"
218 SELECT '[(1),2]'::cube AS cube;
219 ERROR: invalid input syntax for cube
220 LINE 1: SELECT '[(1),2]'::cube AS cube;
221 ^
222 DETAIL: syntax error at or near "2"
223 SELECT '[(1),(2),(3)]'::cube AS cube;
224 ERROR: invalid input syntax for cube
225 LINE 1: SELECT '[(1),(2),(3)]'::cube AS cube;
226 ^
227 DETAIL: syntax error at or near ","
228 SELECT '1,'::cube AS cube;
229 ERROR: invalid input syntax for cube
230 LINE 1: SELECT '1,'::cube AS cube;
231 ^
232 DETAIL: syntax error at end of input
233 SELECT '1,2,'::cube AS cube;
234 ERROR: invalid input syntax for cube
235 LINE 1: SELECT '1,2,'::cube AS cube;
236 ^
237 DETAIL: syntax error at end of input
238 SELECT '1,,2'::cube AS cube;
239 ERROR: invalid input syntax for cube
240 LINE 1: SELECT '1,,2'::cube AS cube;
241 ^
242 DETAIL: syntax error at or near ","
243 SELECT '(1,)'::cube AS cube;
244 ERROR: invalid input syntax for cube
245 LINE 1: SELECT '(1,)'::cube AS cube;
246 ^
247 DETAIL: syntax error at or near ")"
248 SELECT '(1,2,)'::cube AS cube;
249 ERROR: invalid input syntax for cube
250 LINE 1: SELECT '(1,2,)'::cube AS cube;
251 ^
252 DETAIL: syntax error at or near ")"
253 SELECT '(1,,2)'::cube AS cube;
254 ERROR: invalid input syntax for cube
255 LINE 1: SELECT '(1,,2)'::cube AS cube;
256 ^
257 DETAIL: syntax error at or near ","
258 -- invalid input: semantic errors and trailing garbage
259 SELECT '[(1),(2)],'::cube AS cube; -- 0
260 ERROR: invalid input syntax for cube
261 LINE 1: SELECT '[(1),(2)],'::cube AS cube;
262 ^
263 DETAIL: syntax error at or near ","
264 SELECT '[(1,2,3),(2,3)]'::cube AS cube; -- 1
265 ERROR: invalid input syntax for cube
266 LINE 1: SELECT '[(1,2,3),(2,3)]'::cube AS cube;
267 ^
268 DETAIL: Different point dimensions in (1,2,3) and (2,3).
269 SELECT '[(1,2),(1,2,3)]'::cube AS cube; -- 1
270 ERROR: invalid input syntax for cube
271 LINE 1: SELECT '[(1,2),(1,2,3)]'::cube AS cube;
272 ^
273 DETAIL: Different point dimensions in (1,2) and (1,2,3).
274 SELECT '(1),(2),'::cube AS cube; -- 2
275 ERROR: invalid input syntax for cube
276 LINE 1: SELECT '(1),(2),'::cube AS cube;
277 ^
278 DETAIL: syntax error at or near ","
279 SELECT '(1,2,3),(2,3)'::cube AS cube; -- 3
280 ERROR: invalid input syntax for cube
281 LINE 1: SELECT '(1,2,3),(2,3)'::cube AS cube;
282 ^
283 DETAIL: Different point dimensions in (1,2,3) and (2,3).
284 SELECT '(1,2),(1,2,3)'::cube AS cube; -- 3
285 ERROR: invalid input syntax for cube
286 LINE 1: SELECT '(1,2),(1,2,3)'::cube AS cube;
287 ^
288 DETAIL: Different point dimensions in (1,2) and (1,2,3).
289 SELECT '(1,2,3)ab'::cube AS cube; -- 4
290 ERROR: invalid input syntax for cube
291 LINE 1: SELECT '(1,2,3)ab'::cube AS cube;
292 ^
293 DETAIL: syntax error at or near "a"
294 SELECT '(1,2,3)a'::cube AS cube; -- 5
295 ERROR: invalid input syntax for cube
296 LINE 1: SELECT '(1,2,3)a'::cube AS cube;
297 ^
298 DETAIL: syntax error at or near "a"
299 SELECT '(1,2)('::cube AS cube; -- 5
300 ERROR: invalid input syntax for cube
301 LINE 1: SELECT '(1,2)('::cube AS cube;
302 ^
303 DETAIL: syntax error at or near "("
304 SELECT '1,2ab'::cube AS cube; -- 6
305 ERROR: invalid input syntax for cube
306 LINE 1: SELECT '1,2ab'::cube AS cube;
307 ^
308 DETAIL: syntax error at or near "a"
309 SELECT '1 e7'::cube AS cube; -- 6
310 ERROR: invalid input syntax for cube
311 LINE 1: SELECT '1 e7'::cube AS cube;
312 ^
313 DETAIL: syntax error at or near "e"
314 SELECT '1,2a'::cube AS cube; -- 7
315 ERROR: invalid input syntax for cube
316 LINE 1: SELECT '1,2a'::cube AS cube;
317 ^
318 DETAIL: syntax error at or near "a"
319 SELECT '1..2'::cube AS cube; -- 7
320 ERROR: invalid input syntax for cube
321 LINE 1: SELECT '1..2'::cube AS cube;
322 ^
323 DETAIL: syntax error at or near ".2"
324 SELECT '-1e-700'::cube AS cube; -- out of range
325 ERROR: "-1e-700" is out of range for type double precision
326 LINE 1: SELECT '-1e-700'::cube AS cube;
327 ^
328 -- Also try it with non-error-throwing API
329 SELECT pg_input_is_valid('(1,2)', 'cube');
330 pg_input_is_valid
331 -------------------
332 t
333 (1 row)
334
335 SELECT pg_input_is_valid('[(1),]', 'cube');
336 pg_input_is_valid
337 -------------------
338 f
339 (1 row)
340
341 SELECT pg_input_is_valid('-1e-700', 'cube');
342 pg_input_is_valid
343 -------------------
344 f
345 (1 row)
346
347 SELECT * FROM pg_input_error_info('-1e-700', 'cube');
348 message | detail | hint | sql_error_code
349 -----------------------------------------------------+--------+------+----------------
350 "-1e-700" is out of range for type double precision | | | 22003
351 (1 row)
352
353 --
354 -- Testing building cubes from float8 values
355 --
356 SELECT cube(0::float8);
357 cube
358 ------
359 (0)
360 (1 row)
361
362 SELECT cube(1::float8);
363 cube
364 ------
365 (1)
366 (1 row)
367
368 SELECT cube(1,2);
369 cube
370 ---------
371 (1),(2)
372 (1 row)
373
374 SELECT cube(cube(1,2),3);
375 cube
376 ---------------
377 (1, 3),(2, 3)
378 (1 row)
379
380 SELECT cube(cube(1,2),3,4);
381 cube
382 ---------------
383 (1, 3),(2, 4)
384 (1 row)
385
386 SELECT cube(cube(cube(1,2),3,4),5);
387 cube
388 ---------------------
389 (1, 3, 5),(2, 4, 5)
390 (1 row)
391
392 SELECT cube(cube(cube(1,2),3,4),5,6);
393 cube
394 ---------------------
395 (1, 3, 5),(2, 4, 6)
396 (1 row)
397
398 --
399 -- Test that the text -> cube cast was installed.
400 --
401 SELECT '(0)'::text::cube;
402 cube
403 ------
404 (0)
405 (1 row)
406
407 --
408 -- Test the float[] -> cube cast
409 --
410 SELECT cube('{0,1,2}'::float[], '{3,4,5}'::float[]);
411 cube
412 ---------------------
413 (0, 1, 2),(3, 4, 5)
414 (1 row)
415
416 SELECT cube('{0,1,2}'::float[], '{3}'::float[]);
417 ERROR: UR and LL arrays must be of same length
418 SELECT cube(NULL::float[], '{3}'::float[]);
419 cube
420 ------
421
422 (1 row)
423
424 SELECT cube('{0,1,2}'::float[]);
425 cube
426 -----------
427 (0, 1, 2)
428 (1 row)
429
430 SELECT cube_subset(cube('(1,3,5),(6,7,8)'), ARRAY[3,2,1,1]);
431 cube_subset
432 ---------------------------
433 (5, 3, 1, 1),(8, 7, 6, 6)
434 (1 row)
435
436 SELECT cube_subset(cube('(1,3,5),(1,3,5)'), ARRAY[3,2,1,1]);
437 cube_subset
438 --------------
439 (5, 3, 1, 1)
440 (1 row)
441
442 SELECT cube_subset(cube('(1,3,5),(6,7,8)'), ARRAY[4,0]);
443 ERROR: Index out of bounds
444 SELECT cube_subset(cube('(6,7,8),(6,7,8)'), ARRAY[4,0]);
445 ERROR: Index out of bounds
446 -- test for limits: this should pass
447 SELECT cube_subset(cube('(6,7,8),(6,7,8)'), array(SELECT 1 as a FROM generate_series(1,100)));
448 cube_subset
449 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
450 (6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6)
451 (1 row)
452
453 -- and this should fail
454 SELECT cube_subset(cube('(6,7,8),(6,7,8)'), array(SELECT 1 as a FROM generate_series(1,101)));
455 ERROR: array is too long
456 DETAIL: A cube cannot have more than 100 dimensions.
457 --
458 -- Test point processing
459 --
460 SELECT cube('(1,2),(1,2)'); -- cube_in
461 cube
462 --------
463 (1, 2)
464 (1 row)
465
466 SELECT cube('{0,1,2}'::float[], '{0,1,2}'::float[]); -- cube_a_f8_f8
467 cube
468 -----------
469 (0, 1, 2)
470 (1 row)
471
472 SELECT cube('{5,6,7,8}'::float[]); -- cube_a_f8
473 cube
474 --------------
475 (5, 6, 7, 8)
476 (1 row)
477
478 SELECT cube(1.37); -- cube_f8
479 cube
480 --------
481 (1.37)
482 (1 row)
483
484 SELECT cube(1.37, 1.37); -- cube_f8_f8
485 cube
486 --------
487 (1.37)
488 (1 row)
489
490 SELECT cube(cube(1,1), 42); -- cube_c_f8
491 cube
492 ---------
493 (1, 42)
494 (1 row)
495
496 SELECT cube(cube(1,2), 42); -- cube_c_f8
497 cube
498 -----------------
499 (1, 42),(2, 42)
500 (1 row)
501
502 SELECT cube(cube(1,1), 42, 42); -- cube_c_f8_f8
503 cube
504 ---------
505 (1, 42)
506 (1 row)
507
508 SELECT cube(cube(1,1), 42, 24); -- cube_c_f8_f8
509 cube
510 -----------------
511 (1, 42),(1, 24)
512 (1 row)
513
514 SELECT cube(cube(1,2), 42, 42); -- cube_c_f8_f8
515 cube
516 -----------------
517 (1, 42),(2, 42)
518 (1 row)
519
520 SELECT cube(cube(1,2), 42, 24); -- cube_c_f8_f8
521 cube
522 -----------------
523 (1, 42),(2, 24)
524 (1 row)
525
526 --
527 -- Testing limit of CUBE_MAX_DIM dimensions check in cube_in.
528 --
529 -- create too big cube from literal
530 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;
531 ERROR: invalid input syntax for cube
532 LINE 1: 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...
533 ^
534 DETAIL: A cube cannot have more than 100 dimensions.
535 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;
536 ERROR: invalid input syntax for cube
537 LINE 1: 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...
538 ^
539 DETAIL: A cube cannot have more than 100 dimensions.
540 -- from an array
541 select cube(array(SELECT 0 as a FROM generate_series(1,101)));
542 ERROR: array is too long
543 DETAIL: A cube cannot have more than 100 dimensions.
544 select cube(array(SELECT 0 as a FROM generate_series(1,101)),array(SELECT 0 as a FROM generate_series(1,101)));
545 ERROR: can't extend cube
546 DETAIL: A cube cannot have more than 100 dimensions.
547 -- extend cube beyond limit
548 -- this should work
549 select cube(array(SELECT 0 as a FROM generate_series(1,100)));
550 cube
551 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
552 (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
553 (1 row)
554
555 select cube(array(SELECT 0 as a FROM generate_series(1,100)),array(SELECT 0 as a FROM generate_series(1,100)));
556 cube
557 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
558 (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
559 (1 row)
560
561 -- this should fail
562 select cube(cube(array(SELECT 0 as a FROM generate_series(1,100))), 0);
563 ERROR: can't extend cube
564 DETAIL: A cube cannot have more than 100 dimensions.
565 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);
566 ERROR: can't extend cube
567 DETAIL: A cube cannot have more than 100 dimensions.
568 --
569 -- testing the operators
570 --
571 -- equality/inequality:
572 --
573 SELECT '24, 33.20'::cube = '24, 33.20'::cube AS bool;
574 bool
575 ------
576 t
577 (1 row)
578
579 SELECT '24, 33.20'::cube != '24, 33.20'::cube AS bool;
580 bool
581 ------
582 f
583 (1 row)
584
585 SELECT '24, 33.20'::cube = '24, 33.21'::cube AS bool;
586 bool
587 ------
588 f
589 (1 row)
590
591 SELECT '24, 33.20'::cube != '24, 33.21'::cube AS bool;
592 bool
593 ------
594 t
595 (1 row)
596
597 SELECT '(2,0),(3,1)'::cube = '(2,0,0,0,0),(3,1,0,0,0)'::cube AS bool;
598 bool
599 ------
600 f
601 (1 row)
602
603 SELECT '(2,0),(3,1)'::cube = '(2,0,0,0,0),(3,1,0,0,1)'::cube AS bool;
604 bool
605 ------
606 f
607 (1 row)
608
609 -- "lower than" / "greater than"
610 -- (these operators are not useful for anything but ordering)
611 --
612 SELECT '1'::cube > '2'::cube AS bool;
613 bool
614 ------
615 f
616 (1 row)
617
618 SELECT '1'::cube < '2'::cube AS bool;
619 bool
620 ------
621 t
622 (1 row)
623
624 SELECT '1,1'::cube > '1,2'::cube AS bool;
625 bool
626 ------
627 f
628 (1 row)
629
630 SELECT '1,1'::cube < '1,2'::cube AS bool;
631 bool
632 ------
633 t
634 (1 row)
635
636 SELECT '(2,0),(3,1)'::cube > '(2,0,0,0,0),(3,1,0,0,1)'::cube AS bool;
637 bool
638 ------
639 f
640 (1 row)
641
642 SELECT '(2,0),(3,1)'::cube < '(2,0,0,0,0),(3,1,0,0,1)'::cube AS bool;
643 bool
644 ------
645 t
646 (1 row)
647
648 SELECT '(2,0),(3,1)'::cube > '(2,0,0,0,1),(3,1,0,0,0)'::cube AS bool;
649 bool
650 ------
651 f
652 (1 row)
653
654 SELECT '(2,0),(3,1)'::cube < '(2,0,0,0,1),(3,1,0,0,0)'::cube AS bool;
655 bool
656 ------
657 t
658 (1 row)
659
660 SELECT '(2,0),(3,1)'::cube > '(2,0,0,0,0),(3,1,0,0,0)'::cube AS bool;
661 bool
662 ------
663 f
664 (1 row)
665
666 SELECT '(2,0),(3,1)'::cube < '(2,0,0,0,0),(3,1,0,0,0)'::cube AS bool;
667 bool
668 ------
669 t
670 (1 row)
671
672 SELECT '(2,0,0,0,0),(3,1,0,0,1)'::cube > '(2,0),(3,1)'::cube AS bool;
673 bool
674 ------
675 t
676 (1 row)
677
678 SELECT '(2,0,0,0,0),(3,1,0,0,1)'::cube < '(2,0),(3,1)'::cube AS bool;
679 bool
680 ------
681 f
682 (1 row)
683
684 SELECT '(2,0,0,0,1),(3,1,0,0,0)'::cube > '(2,0),(3,1)'::cube AS bool;
685 bool
686 ------
687 t
688 (1 row)
689
690 SELECT '(2,0,0,0,1),(3,1,0,0,0)'::cube < '(2,0),(3,1)'::cube AS bool;
691 bool
692 ------
693 f
694 (1 row)
695
696 SELECT '(2,0,0,0,0),(3,1,0,0,0)'::cube > '(2,0),(3,1)'::cube AS bool;
697 bool
698 ------
699 t
700 (1 row)
701
702 SELECT '(2,0,0,0,0),(3,1,0,0,0)'::cube < '(2,0),(3,1)'::cube AS bool;
703 bool
704 ------
705 f
706 (1 row)
707
708 -- "overlap"
709 --
710 SELECT '1'::cube && '1'::cube AS bool;
711 bool
712 ------
713 t
714 (1 row)
715
716 SELECT '1'::cube && '2'::cube AS bool;
717 bool
718 ------
719 f
720 (1 row)
721
722 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '0'::cube AS bool;
723 bool
724 ------
725 t
726 (1 row)
727
728 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '1'::cube AS bool;
729 bool
730 ------
731 t
732 (1 row)
733
734 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '1,1,1'::cube AS bool;
735 bool
736 ------
737 t
738 (1 row)
739
740 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '[(1,1,1),(2,2,2)]'::cube AS bool;
741 bool
742 ------
743 t
744 (1 row)
745
746 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '[(1,1),(2,2)]'::cube AS bool;
747 bool
748 ------
749 t
750 (1 row)
751
752 SELECT '[(-1,-1,-1),(1,1,1)]'::cube && '[(2,1,1),(2,2,2)]'::cube AS bool;
753 bool
754 ------
755 f
756 (1 row)
757
758 -- "contained in" (the left operand is the cube entirely enclosed by
759 -- the right operand):
760 --
761 SELECT '0'::cube <@ '0'::cube AS bool;
762 bool
763 ------
764 t
765 (1 row)
766
767 SELECT '0,0,0'::cube <@ '0,0,0'::cube AS bool;
768 bool
769 ------
770 t
771 (1 row)
772
773 SELECT '0,0'::cube <@ '0,0,1'::cube AS bool;
774 bool
775 ------
776 t
777 (1 row)
778
779 SELECT '0,0,0'::cube <@ '0,0,1'::cube AS bool;
780 bool
781 ------
782 f
783 (1 row)
784
785 SELECT '1,0,0'::cube <@ '0,0,1'::cube AS bool;
786 bool
787 ------
788 f
789 (1 row)
790
791 SELECT '(1,0,0),(0,0,1)'::cube <@ '(1,0,0),(0,0,1)'::cube AS bool;
792 bool
793 ------
794 t
795 (1 row)
796
797 SELECT '(1,0,0),(0,0,1)'::cube <@ '(-1,-1,-1),(1,1,1)'::cube AS bool;
798 bool
799 ------
800 t
801 (1 row)
802
803 SELECT '(1,0,0),(0,0,1)'::cube <@ '(-1,-1,-1,-1),(1,1,1,1)'::cube AS bool;
804 bool
805 ------
806 t
807 (1 row)
808
809 SELECT '0'::cube <@ '(-1),(1)'::cube AS bool;
810 bool
811 ------
812 t
813 (1 row)
814
815 SELECT '1'::cube <@ '(-1),(1)'::cube AS bool;
816 bool
817 ------
818 t
819 (1 row)
820
821 SELECT '-1'::cube <@ '(-1),(1)'::cube AS bool;
822 bool
823 ------
824 t
825 (1 row)
826
827 SELECT '(-1),(1)'::cube <@ '(-1),(1)'::cube AS bool;
828 bool
829 ------
830 t
831 (1 row)
832
833 SELECT '(-1),(1)'::cube <@ '(-1,-1),(1,1)'::cube AS bool;
834 bool
835 ------
836 t
837 (1 row)
838
839 SELECT '(-2),(1)'::cube <@ '(-1),(1)'::cube AS bool;
840 bool
841 ------
842 f
843 (1 row)
844
845 SELECT '(-2),(1)'::cube <@ '(-1,-1),(1,1)'::cube AS bool;
846 bool
847 ------
848 f
849 (1 row)
850
851 -- "contains" (the left operand is the cube that entirely encloses the
852 -- right operand)
853 --
854 SELECT '0'::cube @> '0'::cube AS bool;
855 bool
856 ------
857 t
858 (1 row)
859
860 SELECT '0,0,0'::cube @> '0,0,0'::cube AS bool;
861 bool
862 ------
863 t
864 (1 row)
865
866 SELECT '0,0,1'::cube @> '0,0'::cube AS bool;
867 bool
868 ------
869 t
870 (1 row)
871
872 SELECT '0,0,1'::cube @> '0,0,0'::cube AS bool;
873 bool
874 ------
875 f
876 (1 row)
877
878 SELECT '0,0,1'::cube @> '1,0,0'::cube AS bool;
879 bool
880 ------
881 f
882 (1 row)
883
884 SELECT '(1,0,0),(0,0,1)'::cube @> '(1,0,0),(0,0,1)'::cube AS bool;
885 bool
886 ------
887 t
888 (1 row)
889
890 SELECT '(-1,-1,-1),(1,1,1)'::cube @> '(1,0,0),(0,0,1)'::cube AS bool;
891 bool
892 ------
893 t
894 (1 row)
895
896 SELECT '(-1,-1,-1,-1),(1,1,1,1)'::cube @> '(1,0,0),(0,0,1)'::cube AS bool;
897 bool
898 ------
899 t
900 (1 row)
901
902 SELECT '(-1),(1)'::cube @> '0'::cube AS bool;
903 bool
904 ------
905 t
906 (1 row)
907
908 SELECT '(-1),(1)'::cube @> '1'::cube AS bool;
909 bool
910 ------
911 t
912 (1 row)
913
914 SELECT '(-1),(1)'::cube @> '-1'::cube AS bool;
915 bool
916 ------
917 t
918 (1 row)
919
920 SELECT '(-1),(1)'::cube @> '(-1),(1)'::cube AS bool;
921 bool
922 ------
923 t
924 (1 row)
925
926 SELECT '(-1,-1),(1,1)'::cube @> '(-1),(1)'::cube AS bool;
927 bool
928 ------
929 t
930 (1 row)
931
932 SELECT '(-1),(1)'::cube @> '(-2),(1)'::cube AS bool;
933 bool
934 ------
935 f
936 (1 row)
937
938 SELECT '(-1,-1),(1,1)'::cube @> '(-2),(1)'::cube AS bool;
939 bool
940 ------
941 f
942 (1 row)
943
944 -- Test of distance function
945 --
946 SELECT cube_distance('(0)'::cube,'(2,2,2,2)'::cube);
947 cube_distance
948 ---------------
949 4
950 (1 row)
951
952 SELECT cube_distance('(0)'::cube,'(.3,.4)'::cube);
953 cube_distance
954 ---------------
955 0.5
956 (1 row)
957
958 SELECT cube_distance('(2,3,4)'::cube,'(2,3,4)'::cube);
959 cube_distance
960 ---------------
961 0
962 (1 row)
963
964 SELECT cube_distance('(42,42,42,42)'::cube,'(137,137,137,137)'::cube);
965 cube_distance
966 ---------------
967 190
968 (1 row)
969
970 SELECT cube_distance('(42,42,42)'::cube,'(137,137)'::cube);
971 cube_distance
972 --------------------
973 140.76221083799445
974 (1 row)
975
976 -- Test of cube function (text to cube)
977 --
978 SELECT cube('(1,1.2)'::text);
979 cube
980 ----------
981 (1, 1.2)
982 (1 row)
983
984 SELECT cube(NULL);
985 cube
986 ------
987
988 (1 row)
989
990 -- Test of cube_dim function (dimensions stored in cube)
991 --
992 SELECT cube_dim('(0)'::cube);
993 cube_dim
994 ----------
995 1
996 (1 row)
997
998 SELECT cube_dim('(0,0)'::cube);
999 cube_dim
1000 ----------
1001 2
1002 (1 row)
1003
1004 SELECT cube_dim('(0,0,0)'::cube);
1005 cube_dim
1006 ----------
1007 3
1008 (1 row)
1009
1010 SELECT cube_dim('(42,42,42),(42,42,42)'::cube);
1011 cube_dim
1012 ----------
1013 3
1014 (1 row)
1015
1016 SELECT cube_dim('(4,8,15,16,23),(4,8,15,16,23)'::cube);
1017 cube_dim
1018 ----------
1019 5
1020 (1 row)
1021
1022 -- Test of cube_ll_coord function (retrieves LL coordinate values)
1023 --
1024 SELECT cube_ll_coord('(-1,1),(2,-2)'::cube, 1);
1025 cube_ll_coord
1026 ---------------
1027 -1
1028 (1 row)
1029
1030 SELECT cube_ll_coord('(-1,1),(2,-2)'::cube, 2);
1031 cube_ll_coord
1032 ---------------
1033 -2
1034 (1 row)
1035
1036 SELECT cube_ll_coord('(-1,1),(2,-2)'::cube, 3);
1037 cube_ll_coord
1038 ---------------
1039 0
1040 (1 row)
1041
1042 SELECT cube_ll_coord('(1,2),(1,2)'::cube, 1);
1043 cube_ll_coord
1044 ---------------
1045 1
1046 (1 row)
1047
1048 SELECT cube_ll_coord('(1,2),(1,2)'::cube, 2);
1049 cube_ll_coord
1050 ---------------
1051 2
1052 (1 row)
1053
1054 SELECT cube_ll_coord('(1,2),(1,2)'::cube, 3);
1055 cube_ll_coord
1056 ---------------
1057 0
1058 (1 row)
1059
1060 SELECT cube_ll_coord('(42,137)'::cube, 1);
1061 cube_ll_coord
1062 ---------------
1063 42
1064 (1 row)
1065
1066 SELECT cube_ll_coord('(42,137)'::cube, 2);
1067 cube_ll_coord
1068 ---------------
1069 137
1070 (1 row)
1071
1072 SELECT cube_ll_coord('(42,137)'::cube, 3);
1073 cube_ll_coord
1074 ---------------
1075 0
1076 (1 row)
1077
1078 -- Test of cube_ur_coord function (retrieves UR coordinate values)
1079 --
1080 SELECT cube_ur_coord('(-1,1),(2,-2)'::cube, 1);
1081 cube_ur_coord
1082 ---------------
1083 2
1084 (1 row)
1085
1086 SELECT cube_ur_coord('(-1,1),(2,-2)'::cube, 2);
1087 cube_ur_coord
1088 ---------------
1089 1
1090 (1 row)
1091
1092 SELECT cube_ur_coord('(-1,1),(2,-2)'::cube, 3);
1093 cube_ur_coord
1094 ---------------
1095 0
1096 (1 row)
1097
1098 SELECT cube_ur_coord('(1,2),(1,2)'::cube, 1);
1099 cube_ur_coord
1100 ---------------
1101 1
1102 (1 row)
1103
1104 SELECT cube_ur_coord('(1,2),(1,2)'::cube, 2);
1105 cube_ur_coord
1106 ---------------
1107 2
1108 (1 row)
1109
1110 SELECT cube_ur_coord('(1,2),(1,2)'::cube, 3);
1111 cube_ur_coord
1112 ---------------
1113 0
1114 (1 row)
1115
1116 SELECT cube_ur_coord('(42,137)'::cube, 1);
1117 cube_ur_coord
1118 ---------------
1119 42
1120 (1 row)
1121
1122 SELECT cube_ur_coord('(42,137)'::cube, 2);
1123 cube_ur_coord
1124 ---------------
1125 137
1126 (1 row)
1127
1128 SELECT cube_ur_coord('(42,137)'::cube, 3);
1129 cube_ur_coord
1130 ---------------
1131 0
1132 (1 row)
1133
1134 -- Test of cube_is_point
1135 --
1136 SELECT cube_is_point('(0)'::cube);
1137 cube_is_point
1138 ---------------
1139 t
1140 (1 row)
1141
1142 SELECT cube_is_point('(0,1,2)'::cube);
1143 cube_is_point
1144 ---------------
1145 t
1146 (1 row)
1147
1148 SELECT cube_is_point('(0,1,2),(0,1,2)'::cube);
1149 cube_is_point
1150 ---------------
1151 t
1152 (1 row)
1153
1154 SELECT cube_is_point('(0,1,2),(-1,1,2)'::cube);
1155 cube_is_point
1156 ---------------
1157 f
1158 (1 row)
1159
1160 SELECT cube_is_point('(0,1,2),(0,-1,2)'::cube);
1161 cube_is_point
1162 ---------------
1163 f
1164 (1 row)
1165
1166 SELECT cube_is_point('(0,1,2),(0,1,-2)'::cube);
1167 cube_is_point
1168 ---------------
1169 f
1170 (1 row)
1171
1172 -- Test of cube_enlarge (enlarging and shrinking cubes)
1173 --
1174 SELECT cube_enlarge('(0)'::cube, 0, 0);
1175 cube_enlarge
1176 --------------
1177 (0)
1178 (1 row)
1179
1180 SELECT cube_enlarge('(0)'::cube, 0, 1);
1181 cube_enlarge
1182 --------------
1183 (0)
1184 (1 row)
1185
1186 SELECT cube_enlarge('(0)'::cube, 0, 2);
1187 cube_enlarge
1188 --------------
1189 (0)
1190 (1 row)
1191
1192 SELECT cube_enlarge('(2),(-2)'::cube, 0, 4);
1193 cube_enlarge
1194 --------------
1195 (-2),(2)
1196 (1 row)
1197
1198 SELECT cube_enlarge('(0)'::cube, 1, 0);
1199 cube_enlarge
1200 --------------
1201 (-1),(1)
1202 (1 row)
1203
1204 SELECT cube_enlarge('(0)'::cube, 1, 1);
1205 cube_enlarge
1206 --------------
1207 (-1),(1)
1208 (1 row)
1209
1210 SELECT cube_enlarge('(0)'::cube, 1, 2);
1211 cube_enlarge
1212 -----------------
1213 (-1, -1),(1, 1)
1214 (1 row)
1215
1216 SELECT cube_enlarge('(2),(-2)'::cube, 1, 4);
1217 cube_enlarge
1218 -------------------------------
1219 (-3, -1, -1, -1),(3, 1, 1, 1)
1220 (1 row)
1221
1222 SELECT cube_enlarge('(0)'::cube, -1, 0);
1223 cube_enlarge
1224 --------------
1225 (0)
1226 (1 row)
1227
1228 SELECT cube_enlarge('(0)'::cube, -1, 1);
1229 cube_enlarge
1230 --------------
1231 (0)
1232 (1 row)
1233
1234 SELECT cube_enlarge('(0)'::cube, -1, 2);
1235 cube_enlarge
1236 --------------
1237 (0)
1238 (1 row)
1239
1240 SELECT cube_enlarge('(2),(-2)'::cube, -1, 4);
1241 cube_enlarge
1242 --------------
1243 (-1),(1)
1244 (1 row)
1245
1246 SELECT cube_enlarge('(0,0,0)'::cube, 1, 0);
1247 cube_enlarge
1248 ------------------------
1249 (-1, -1, -1),(1, 1, 1)
1250 (1 row)
1251
1252 SELECT cube_enlarge('(0,0,0)'::cube, 1, 2);
1253 cube_enlarge
1254 ------------------------
1255 (-1, -1, -1),(1, 1, 1)
1256 (1 row)
1257
1258 SELECT cube_enlarge('(2,-2),(-3,7)'::cube, 1, 2);
1259 cube_enlarge
1260 -----------------
1261 (-4, -3),(3, 8)
1262 (1 row)
1263
1264 SELECT cube_enlarge('(2,-2),(-3,7)'::cube, 3, 2);
1265 cube_enlarge
1266 ------------------
1267 (-6, -5),(5, 10)
1268 (1 row)
1269
1270 SELECT cube_enlarge('(2,-2),(-3,7)'::cube, -1, 2);
1271 cube_enlarge
1272 -----------------
1273 (-2, -1),(1, 6)
1274 (1 row)
1275
1276 SELECT cube_enlarge('(2,-2),(-3,7)'::cube, -3, 2);
1277 cube_enlarge
1278 ---------------------
1279 (-0.5, 1),(-0.5, 4)
1280 (1 row)
1281
1282 SELECT cube_enlarge('(42,-23,-23),(42,23,23)'::cube, -23, 5);
1283 cube_enlarge
1284 --------------
1285 (42, 0, 0)
1286 (1 row)
1287
1288 SELECT cube_enlarge('(42,-23,-23),(42,23,23)'::cube, -24, 5);
1289 cube_enlarge
1290 --------------
1291 (42, 0, 0)
1292 (1 row)
1293
1294 -- Test of cube_union (MBR for two cubes)
1295 --
1296 SELECT cube_union('(1,2),(3,4)'::cube, '(5,6,7),(8,9,10)'::cube);
1297 cube_union
1298 ----------------------
1299 (1, 2, 0),(8, 9, 10)
1300 (1 row)
1301
1302 SELECT cube_union('(1,2)'::cube, '(4,2,0,0)'::cube);
1303 cube_union
1304 ---------------------------
1305 (1, 2, 0, 0),(4, 2, 0, 0)
1306 (1 row)
1307
1308 SELECT cube_union('(1,2),(1,2)'::cube, '(4,2),(4,2)'::cube);
1309 cube_union
1310 ---------------
1311 (1, 2),(4, 2)
1312 (1 row)
1313
1314 SELECT cube_union('(1,2),(1,2)'::cube, '(1,2),(1,2)'::cube);
1315 cube_union
1316 ------------
1317 (1, 2)
1318 (1 row)
1319
1320 SELECT cube_union('(1,2),(1,2)'::cube, '(1,2,0),(1,2,0)'::cube);
1321 cube_union
1322 ------------
1323 (1, 2, 0)
1324 (1 row)
1325
1326 -- Test of cube_inter
1327 --
1328 SELECT cube_inter('(1,2),(10,11)'::cube, '(3,4), (16,15)'::cube); -- intersects
1329 cube_inter
1330 -----------------
1331 (3, 4),(10, 11)
1332 (1 row)
1333
1334 SELECT cube_inter('(1,2),(10,11)'::cube, '(3,4), (6,5)'::cube); -- includes
1335 cube_inter
1336 ---------------
1337 (3, 4),(6, 5)
1338 (1 row)
1339
1340 SELECT cube_inter('(1,2),(10,11)'::cube, '(13,14), (16,15)'::cube); -- no intersection
1341 cube_inter
1342 -------------------
1343 (13, 14),(10, 11)
1344 (1 row)
1345
1346 SELECT cube_inter('(1,2),(10,11)'::cube, '(3,14), (16,15)'::cube); -- no intersection, but one dimension intersects
1347 cube_inter
1348 ------------------
1349 (3, 14),(10, 11)
1350 (1 row)
1351
1352 SELECT cube_inter('(1,2),(10,11)'::cube, '(10,11), (16,15)'::cube); -- point intersection
1353 cube_inter
1354 ------------
1355 (10, 11)
1356 (1 row)
1357
1358 SELECT cube_inter('(1,2,3)'::cube, '(1,2,3)'::cube); -- point args
1359 cube_inter
1360 ------------
1361 (1, 2, 3)
1362 (1 row)
1363
1364 SELECT cube_inter('(1,2,3)'::cube, '(5,6,3)'::cube); -- point args
1365 cube_inter
1366 ---------------------
1367 (5, 6, 3),(1, 2, 3)
1368 (1 row)
1369
1370 -- Test of cube_size
1371 --
1372 SELECT cube_size('(4,8),(15,16)'::cube);
1373 cube_size
1374 -----------
1375 88
1376 (1 row)
1377
1378 SELECT cube_size('(42,137)'::cube);
1379 cube_size
1380 -----------
1381 0
1382 (1 row)
1383
1384 -- Test of distances (euclidean distance may not be bit-exact)
1385 --
1386 SET extra_float_digits = 0;
1387 SELECT cube_distance('(1,1)'::cube, '(4,5)'::cube);
1388 cube_distance
1389 ---------------
1390 5
1391 (1 row)
1392
1393 SELECT '(1,1)'::cube <-> '(4,5)'::cube as d_e;
1394 d_e
1395 -----
1396 5
1397 (1 row)
1398
1399 RESET extra_float_digits;
1400 SELECT distance_chebyshev('(1,1)'::cube, '(4,5)'::cube);
1401 distance_chebyshev
1402 --------------------
1403 4
1404 (1 row)
1405
1406 SELECT '(1,1)'::cube <=> '(4,5)'::cube as d_c;
1407 d_c
1408 -----
1409 4
1410 (1 row)
1411
1412 SELECT distance_taxicab('(1,1)'::cube, '(4,5)'::cube);
1413 distance_taxicab
1414 ------------------
1415 7
1416 (1 row)
1417
1418 SELECT '(1,1)'::cube <#> '(4,5)'::cube as d_t;
1419 d_t
1420 -----
1421 7
1422 (1 row)
1423
1424 -- zero for overlapping
1425 SELECT cube_distance('(2,2),(10,10)'::cube, '(0,0),(5,5)'::cube);
1426 cube_distance
1427 ---------------
1428 0
1429 (1 row)
1430
1431 SELECT distance_chebyshev('(2,2),(10,10)'::cube, '(0,0),(5,5)'::cube);
1432 distance_chebyshev
1433 --------------------
1434 0
1435 (1 row)
1436
1437 SELECT distance_taxicab('(2,2),(10,10)'::cube, '(0,0),(5,5)'::cube);
1438 distance_taxicab
1439 ------------------
1440 0
1441 (1 row)
1442
1443 -- coordinate access
1444 SELECT cube(array[10,20,30], array[40,50,60])->1;
1445 ?column?
1446 ----------
1447 10
1448 (1 row)
1449
1450 SELECT cube(array[40,50,60], array[10,20,30])->1;
1451 ?column?
1452 ----------
1453 40
1454 (1 row)
1455
1456 SELECT cube(array[10,20,30], array[40,50,60])->6;
1457 ?column?
1458 ----------
1459 60
1460 (1 row)
1461
1462 SELECT cube(array[10,20,30], array[40,50,60])->0;
1463 ERROR: cube index 0 is out of bounds
1464 SELECT cube(array[10,20,30], array[40,50,60])->7;
1465 ERROR: cube index 7 is out of bounds
1466 SELECT cube(array[10,20,30], array[40,50,60])->-1;
1467 ERROR: cube index -1 is out of bounds
1468 SELECT cube(array[10,20,30], array[40,50,60])->-6;
1469 ERROR: cube index -6 is out of bounds
1470 SELECT cube(array[10,20,30])->3;
1471 ?column?
1472 ----------
1473 30
1474 (1 row)
1475
1476 SELECT cube(array[10,20,30])->6;
1477 ?column?
1478 ----------
1479 30
1480 (1 row)
1481
1482 SELECT cube(array[10,20,30])->-6;
1483 ERROR: cube index -6 is out of bounds
1484 -- "normalized" coordinate access
1485 SELECT cube(array[10,20,30], array[40,50,60])~>1;
1486 ?column?
1487 ----------
1488 10
1489 (1 row)
1490
1491 SELECT cube(array[40,50,60], array[10,20,30])~>1;
1492 ?column?
1493 ----------
1494 10
1495 (1 row)
1496
1497 SELECT cube(array[10,20,30], array[40,50,60])~>2;
1498 ?column?
1499 ----------
1500 40
1501 (1 row)
1502
1503 SELECT cube(array[40,50,60], array[10,20,30])~>2;
1504 ?column?
1505 ----------
1506 40
1507 (1 row)
1508
1509 SELECT cube(array[10,20,30], array[40,50,60])~>3;
1510 ?column?
1511 ----------
1512 20
1513 (1 row)
1514
1515 SELECT cube(array[40,50,60], array[10,20,30])~>3;
1516 ?column?
1517 ----------
1518 20
1519 (1 row)
1520
1521 SELECT cube(array[40,50,60], array[10,20,30])~>0;
1522 ERROR: zero cube index is not defined
1523 SELECT cube(array[40,50,60], array[10,20,30])~>4;
1524 ?column?
1525 ----------
1526 50
1527 (1 row)
1528
1529 SELECT cube(array[40,50,60], array[10,20,30])~>(-1);
1530 ?column?
1531 ----------
1532 -10
1533 (1 row)
1534
1535 -- Load some example data and build the index
1536 --
1537 CREATE TABLE test_cube (c cube);
1538 \copy test_cube from 'data/test_cube.data'
1539 CREATE INDEX test_cube_ix ON test_cube USING gist (c);
1540 SELECT * FROM test_cube WHERE c && '(3000,1000),(0,0)' ORDER BY c;
1541 c
1542 --------------------------
1543 (337, 455),(240, 359)
1544 (759, 187),(662, 163)
1545 (1444, 403),(1346, 344)
1546 (1594, 1043),(1517, 971)
1547 (2424, 160),(2424, 81)
1548 (5 rows)
1549
1550 -- Test sorting
1551 SELECT * FROM test_cube WHERE c && '(3000,1000),(0,0)' GROUP BY c ORDER BY c;
1552 c
1553 --------------------------
1554 (337, 455),(240, 359)
1555 (759, 187),(662, 163)
1556 (1444, 403),(1346, 344)
1557 (1594, 1043),(1517, 971)
1558 (2424, 160),(2424, 81)
1559 (5 rows)
1560
1561 -- Test index-only scans
1562 SET enable_bitmapscan = false;
1563 EXPLAIN (COSTS OFF)
1564 SELECT c FROM test_cube WHERE c <@ '(3000,1000),(0,0)' ORDER BY c;
1565 QUERY PLAN
1566 --------------------------------------------------------
1567 Sort
1568 Sort Key: c
1569 -> Index Only Scan using test_cube_ix on test_cube
1570 Index Cond: (c <@ '(3000, 1000),(0, 0)'::cube)
1571 (4 rows)
1572
1573 SELECT c FROM test_cube WHERE c <@ '(3000,1000),(0,0)' ORDER BY c;
1574 c
1575 -------------------------
1576 (337, 455),(240, 359)
1577 (759, 187),(662, 163)
1578 (1444, 403),(1346, 344)
1579 (2424, 160),(2424, 81)
1580 (4 rows)
1581
1582 RESET enable_bitmapscan;
1583 -- Test kNN
1584 INSERT INTO test_cube VALUES ('(1,1)'), ('(100000)'), ('(0, 100000)'); -- Some corner cases
1585 SET enable_seqscan = false;
1586 -- Test different metrics
1587 SET extra_float_digits = 0;
1588 SELECT *, c <-> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <-> '(100, 100),(500, 500)'::cube LIMIT 5;
1589 c | dist
1590 -------------------------+------------------
1591 (337, 455),(240, 359) | 0
1592 (1, 1) | 140.007142674936
1593 (759, 187),(662, 163) | 162
1594 (948, 1201),(907, 1156) | 772.000647668122
1595 (1444, 403),(1346, 344) | 846
1596 (5 rows)
1597
1598 RESET extra_float_digits;
1599 SELECT *, c <=> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <=> '(100, 100),(500, 500)'::cube LIMIT 5;
1600 c | dist
1601 -------------------------+------
1602 (337, 455),(240, 359) | 0
1603 (1, 1) | 99
1604 (759, 187),(662, 163) | 162
1605 (948, 1201),(907, 1156) | 656
1606 (1444, 403),(1346, 344) | 846
1607 (5 rows)
1608
1609 SELECT *, c <#> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <#> '(100, 100),(500, 500)'::cube LIMIT 5;
1610 c | dist
1611 -------------------------+------
1612 (337, 455),(240, 359) | 0
1613 (759, 187),(662, 163) | 162
1614 (1, 1) | 198
1615 (1444, 403),(1346, 344) | 846
1616 (369, 1457),(278, 1409) | 909
1617 (5 rows)
1618
1619 -- Test sorting by coordinates
1620 SELECT c~>1, c FROM test_cube ORDER BY c~>1 LIMIT 15; -- ascending by left bound
1621 ?column? | c
1622 ----------+---------------------------
1623 0 | (0, 100000)
1624 1 | (1, 1)
1625 3 | (54, 38679),(3, 38602)
1626 15 | (83, 10271),(15, 10265)
1627 64 | (122, 46832),(64, 46762)
1628 92 | (167, 17214),(92, 17184)
1629 107 | (161, 24465),(107, 24374)
1630 120 | (162, 26040),(120, 25963)
1631 138 | (154, 4019),(138, 3990)
1632 175 | (259, 1850),(175, 1820)
1633 179 | (207, 40886),(179, 40879)
1634 204 | (288, 49588),(204, 49571)
1635 226 | (270, 32616),(226, 32607)
1636 235 | (318, 31489),(235, 31404)
1637 240 | (337, 455),(240, 359)
1638 (15 rows)
1639
1640 SELECT c~>2, c FROM test_cube ORDER BY c~>2 LIMIT 15; -- ascending by right bound
1641 ?column? | c
1642 ----------+---------------------------
1643 0 | (0, 100000)
1644 1 | (1, 1)
1645 54 | (54, 38679),(3, 38602)
1646 83 | (83, 10271),(15, 10265)
1647 122 | (122, 46832),(64, 46762)
1648 154 | (154, 4019),(138, 3990)
1649 161 | (161, 24465),(107, 24374)
1650 162 | (162, 26040),(120, 25963)
1651 167 | (167, 17214),(92, 17184)
1652 207 | (207, 40886),(179, 40879)
1653 259 | (259, 1850),(175, 1820)
1654 270 | (270, 29508),(264, 29440)
1655 270 | (270, 32616),(226, 32607)
1656 288 | (288, 49588),(204, 49571)
1657 318 | (318, 31489),(235, 31404)
1658 (15 rows)
1659
1660 SELECT c~>3, c FROM test_cube ORDER BY c~>3 LIMIT 15; -- ascending by lower bound
1661 ?column? | c
1662 ----------+---------------------------
1663 0 | (100000)
1664 1 | (1, 1)
1665 6 | (30333, 50),(30273, 6)
1666 43 | (43301, 75),(43227, 43)
1667 51 | (19650, 142),(19630, 51)
1668 81 | (2424, 160),(2424, 81)
1669 108 | (3449, 171),(3354, 108)
1670 109 | (18037, 155),(17941, 109)
1671 114 | (28511, 208),(28479, 114)
1672 118 | (19946, 217),(19941, 118)
1673 139 | (16906, 191),(16816, 139)
1674 163 | (759, 187),(662, 163)
1675 181 | (22684, 266),(22656, 181)
1676 213 | (24423, 255),(24360, 213)
1677 222 | (45989, 249),(45910, 222)
1678 (15 rows)
1679
1680 SELECT c~>4, c FROM test_cube ORDER BY c~>4 LIMIT 15; -- ascending by upper bound
1681 ?column? | c
1682 ----------+---------------------------
1683 0 | (100000)
1684 1 | (1, 1)
1685 50 | (30333, 50),(30273, 6)
1686 75 | (43301, 75),(43227, 43)
1687 142 | (19650, 142),(19630, 51)
1688 155 | (18037, 155),(17941, 109)
1689 160 | (2424, 160),(2424, 81)
1690 171 | (3449, 171),(3354, 108)
1691 187 | (759, 187),(662, 163)
1692 191 | (16906, 191),(16816, 139)
1693 208 | (28511, 208),(28479, 114)
1694 217 | (19946, 217),(19941, 118)
1695 249 | (45989, 249),(45910, 222)
1696 255 | (24423, 255),(24360, 213)
1697 266 | (22684, 266),(22656, 181)
1698 (15 rows)
1699
1700 SELECT c~>(-1), c FROM test_cube ORDER BY c~>(-1) LIMIT 15; -- descending by left bound
1701 ?column? | c
1702 ----------+-------------------------------
1703 -100000 | (100000)
1704 -49951 | (50027, 49230),(49951, 49214)
1705 -49937 | (49980, 35004),(49937, 34963)
1706 -49927 | (49985, 6436),(49927, 6338)
1707 -49908 | (49999, 27218),(49908, 27176)
1708 -49905 | (49954, 1340),(49905, 1294)
1709 -49902 | (49944, 25163),(49902, 25153)
1710 -49898 | (49981, 34876),(49898, 34786)
1711 -49897 | (49957, 43390),(49897, 43384)
1712 -49848 | (49853, 18504),(49848, 18503)
1713 -49818 | (49902, 41752),(49818, 41746)
1714 -49810 | (49907, 30225),(49810, 30158)
1715 -49808 | (49843, 5175),(49808, 5145)
1716 -49805 | (49887, 24274),(49805, 24184)
1717 -49798 | (49847, 7128),(49798, 7067)
1718 (15 rows)
1719
1720 SELECT c~>(-2), c FROM test_cube ORDER BY c~>(-2) LIMIT 15; -- descending by right bound
1721 ?column? | c
1722 ----------+-------------------------------
1723 -100000 | (100000)
1724 -50027 | (50027, 49230),(49951, 49214)
1725 -49999 | (49999, 27218),(49908, 27176)
1726 -49985 | (49985, 6436),(49927, 6338)
1727 -49981 | (49981, 34876),(49898, 34786)
1728 -49980 | (49980, 35004),(49937, 34963)
1729 -49957 | (49957, 43390),(49897, 43384)
1730 -49954 | (49954, 1340),(49905, 1294)
1731 -49944 | (49944, 25163),(49902, 25153)
1732 -49907 | (49907, 30225),(49810, 30158)
1733 -49902 | (49902, 41752),(49818, 41746)
1734 -49887 | (49887, 24274),(49805, 24184)
1735 -49853 | (49853, 18504),(49848, 18503)
1736 -49847 | (49847, 7128),(49798, 7067)
1737 -49843 | (49843, 5175),(49808, 5145)
1738 (15 rows)
1739
1740 SELECT c~>(-3), c FROM test_cube ORDER BY c~>(-3) LIMIT 15; -- descending by lower bound
1741 ?column? | c
1742 ----------+-------------------------------
1743 -100000 | (0, 100000)
1744 -49992 | (30746, 50040),(30727, 49992)
1745 -49987 | (36311, 50073),(36258, 49987)
1746 -49934 | (3531, 49962),(3463, 49934)
1747 -49915 | (17954, 49975),(17865, 49915)
1748 -49914 | (2168, 50012),(2108, 49914)
1749 -49913 | (31287, 49923),(31236, 49913)
1750 -49885 | (21551, 49983),(21492, 49885)
1751 -49878 | (43925, 49912),(43888, 49878)
1752 -49849 | (19128, 49932),(19112, 49849)
1753 -49844 | (38266, 49852),(38233, 49844)
1754 -49836 | (14913, 49873),(14849, 49836)
1755 -49834 | (37595, 49849),(37581, 49834)
1756 -49830 | (46151, 49848),(46058, 49830)
1757 -49818 | (29261, 49910),(29247, 49818)
1758 (15 rows)
1759
1760 SELECT c~>(-4), c FROM test_cube ORDER BY c~>(-4) LIMIT 15; -- descending by upper bound
1761 ?column? | c
1762 ----------+-------------------------------
1763 -100000 | (0, 100000)
1764 -50073 | (36311, 50073),(36258, 49987)
1765 -50040 | (30746, 50040),(30727, 49992)
1766 -50012 | (2168, 50012),(2108, 49914)
1767 -49983 | (21551, 49983),(21492, 49885)
1768 -49975 | (17954, 49975),(17865, 49915)
1769 -49962 | (3531, 49962),(3463, 49934)
1770 -49932 | (19128, 49932),(19112, 49849)
1771 -49923 | (31287, 49923),(31236, 49913)
1772 -49912 | (43925, 49912),(43888, 49878)
1773 -49910 | (29261, 49910),(29247, 49818)
1774 -49873 | (14913, 49873),(14849, 49836)
1775 -49858 | (20007, 49858),(19921, 49778)
1776 -49852 | (38266, 49852),(38233, 49844)
1777 -49849 | (37595, 49849),(37581, 49834)
1778 (15 rows)
1779
1780 -- Same queries with sequential scan (should give the same results as above)
1781 RESET enable_seqscan;
1782 SET enable_indexscan = OFF;
1783 SET extra_float_digits = 0;
1784 SELECT *, c <-> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <-> '(100, 100),(500, 500)'::cube LIMIT 5;
1785 c | dist
1786 -------------------------+------------------
1787 (337, 455),(240, 359) | 0
1788 (1, 1) | 140.007142674936
1789 (759, 187),(662, 163) | 162
1790 (948, 1201),(907, 1156) | 772.000647668122
1791 (1444, 403),(1346, 344) | 846
1792 (5 rows)
1793
1794 RESET extra_float_digits;
1795 SELECT *, c <=> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <=> '(100, 100),(500, 500)'::cube LIMIT 5;
1796 c | dist
1797 -------------------------+------
1798 (337, 455),(240, 359) | 0
1799 (1, 1) | 99
1800 (759, 187),(662, 163) | 162
1801 (948, 1201),(907, 1156) | 656
1802 (1444, 403),(1346, 344) | 846
1803 (5 rows)
1804
1805 SELECT *, c <#> '(100, 100),(500, 500)'::cube as dist FROM test_cube ORDER BY c <#> '(100, 100),(500, 500)'::cube LIMIT 5;
1806 c | dist
1807 -------------------------+------
1808 (337, 455),(240, 359) | 0
1809 (759, 187),(662, 163) | 162
1810 (1, 1) | 198
1811 (1444, 403),(1346, 344) | 846
1812 (369, 1457),(278, 1409) | 909
1813 (5 rows)
1814
1815 SELECT c~>1, c FROM test_cube ORDER BY c~>1 LIMIT 15; -- ascending by left bound
1816 ?column? | c
1817 ----------+---------------------------
1818 0 | (0, 100000)
1819 1 | (1, 1)
1820 3 | (54, 38679),(3, 38602)
1821 15 | (83, 10271),(15, 10265)
1822 64 | (122, 46832),(64, 46762)
1823 92 | (167, 17214),(92, 17184)
1824 107 | (161, 24465),(107, 24374)
1825 120 | (162, 26040),(120, 25963)
1826 138 | (154, 4019),(138, 3990)
1827 175 | (259, 1850),(175, 1820)
1828 179 | (207, 40886),(179, 40879)
1829 204 | (288, 49588),(204, 49571)
1830 226 | (270, 32616),(226, 32607)
1831 235 | (318, 31489),(235, 31404)
1832 240 | (337, 455),(240, 359)
1833 (15 rows)
1834
1835 SELECT c~>2, c FROM test_cube ORDER BY c~>2 LIMIT 15; -- ascending by right bound
1836 ?column? | c
1837 ----------+---------------------------
1838 0 | (0, 100000)
1839 1 | (1, 1)
1840 54 | (54, 38679),(3, 38602)
1841 83 | (83, 10271),(15, 10265)
1842 122 | (122, 46832),(64, 46762)
1843 154 | (154, 4019),(138, 3990)
1844 161 | (161, 24465),(107, 24374)
1845 162 | (162, 26040),(120, 25963)
1846 167 | (167, 17214),(92, 17184)
1847 207 | (207, 40886),(179, 40879)
1848 259 | (259, 1850),(175, 1820)
1849 270 | (270, 29508),(264, 29440)
1850 270 | (270, 32616),(226, 32607)
1851 288 | (288, 49588),(204, 49571)
1852 318 | (318, 31489),(235, 31404)
1853 (15 rows)
1854
1855 SELECT c~>3, c FROM test_cube ORDER BY c~>3 LIMIT 15; -- ascending by lower bound
1856 ?column? | c
1857 ----------+---------------------------
1858 0 | (100000)
1859 1 | (1, 1)
1860 6 | (30333, 50),(30273, 6)
1861 43 | (43301, 75),(43227, 43)
1862 51 | (19650, 142),(19630, 51)
1863 81 | (2424, 160),(2424, 81)
1864 108 | (3449, 171),(3354, 108)
1865 109 | (18037, 155),(17941, 109)
1866 114 | (28511, 208),(28479, 114)
1867 118 | (19946, 217),(19941, 118)
1868 139 | (16906, 191),(16816, 139)
1869 163 | (759, 187),(662, 163)
1870 181 | (22684, 266),(22656, 181)
1871 213 | (24423, 255),(24360, 213)
1872 222 | (45989, 249),(45910, 222)
1873 (15 rows)
1874
1875 SELECT c~>4, c FROM test_cube ORDER BY c~>4 LIMIT 15; -- ascending by upper bound
1876 ?column? | c
1877 ----------+---------------------------
1878 0 | (100000)
1879 1 | (1, 1)
1880 50 | (30333, 50),(30273, 6)
1881 75 | (43301, 75),(43227, 43)
1882 142 | (19650, 142),(19630, 51)
1883 155 | (18037, 155),(17941, 109)
1884 160 | (2424, 160),(2424, 81)
1885 171 | (3449, 171),(3354, 108)
1886 187 | (759, 187),(662, 163)
1887 191 | (16906, 191),(16816, 139)
1888 208 | (28511, 208),(28479, 114)
1889 217 | (19946, 217),(19941, 118)
1890 249 | (45989, 249),(45910, 222)
1891 255 | (24423, 255),(24360, 213)
1892 266 | (22684, 266),(22656, 181)
1893 (15 rows)
1894
1895 SELECT c~>(-1), c FROM test_cube ORDER BY c~>(-1) LIMIT 15; -- descending by left bound
1896 ?column? | c
1897 ----------+-------------------------------
1898 -100000 | (100000)
1899 -49951 | (50027, 49230),(49951, 49214)
1900 -49937 | (49980, 35004),(49937, 34963)
1901 -49927 | (49985, 6436),(49927, 6338)
1902 -49908 | (49999, 27218),(49908, 27176)
1903 -49905 | (49954, 1340),(49905, 1294)
1904 -49902 | (49944, 25163),(49902, 25153)
1905 -49898 | (49981, 34876),(49898, 34786)
1906 -49897 | (49957, 43390),(49897, 43384)
1907 -49848 | (49853, 18504),(49848, 18503)
1908 -49818 | (49902, 41752),(49818, 41746)
1909 -49810 | (49907, 30225),(49810, 30158)
1910 -49808 | (49843, 5175),(49808, 5145)
1911 -49805 | (49887, 24274),(49805, 24184)
1912 -49798 | (49847, 7128),(49798, 7067)
1913 (15 rows)
1914
1915 SELECT c~>(-2), c FROM test_cube ORDER BY c~>(-2) LIMIT 15; -- descending by right bound
1916 ?column? | c
1917 ----------+-------------------------------
1918 -100000 | (100000)
1919 -50027 | (50027, 49230),(49951, 49214)
1920 -49999 | (49999, 27218),(49908, 27176)
1921 -49985 | (49985, 6436),(49927, 6338)
1922 -49981 | (49981, 34876),(49898, 34786)
1923 -49980 | (49980, 35004),(49937, 34963)
1924 -49957 | (49957, 43390),(49897, 43384)
1925 -49954 | (49954, 1340),(49905, 1294)
1926 -49944 | (49944, 25163),(49902, 25153)
1927 -49907 | (49907, 30225),(49810, 30158)
1928 -49902 | (49902, 41752),(49818, 41746)
1929 -49887 | (49887, 24274),(49805, 24184)
1930 -49853 | (49853, 18504),(49848, 18503)
1931 -49847 | (49847, 7128),(49798, 7067)
1932 -49843 | (49843, 5175),(49808, 5145)
1933 (15 rows)
1934
1935 SELECT c~>(-3), c FROM test_cube ORDER BY c~>(-3) LIMIT 15; -- descending by lower bound
1936 ?column? | c
1937 ----------+-------------------------------
1938 -100000 | (0, 100000)
1939 -49992 | (30746, 50040),(30727, 49992)
1940 -49987 | (36311, 50073),(36258, 49987)
1941 -49934 | (3531, 49962),(3463, 49934)
1942 -49915 | (17954, 49975),(17865, 49915)
1943 -49914 | (2168, 50012),(2108, 49914)
1944 -49913 | (31287, 49923),(31236, 49913)
1945 -49885 | (21551, 49983),(21492, 49885)
1946 -49878 | (43925, 49912),(43888, 49878)
1947 -49849 | (19128, 49932),(19112, 49849)
1948 -49844 | (38266, 49852),(38233, 49844)
1949 -49836 | (14913, 49873),(14849, 49836)
1950 -49834 | (37595, 49849),(37581, 49834)
1951 -49830 | (46151, 49848),(46058, 49830)
1952 -49818 | (29261, 49910),(29247, 49818)
1953 (15 rows)
1954
1955 SELECT c~>(-4), c FROM test_cube ORDER BY c~>(-4) LIMIT 15; -- descending by upper bound
1956 ?column? | c
1957 ----------+-------------------------------
1958 -100000 | (0, 100000)
1959 -50073 | (36311, 50073),(36258, 49987)
1960 -50040 | (30746, 50040),(30727, 49992)
1961 -50012 | (2168, 50012),(2108, 49914)
1962 -49983 | (21551, 49983),(21492, 49885)
1963 -49975 | (17954, 49975),(17865, 49915)
1964 -49962 | (3531, 49962),(3463, 49934)
1965 -49932 | (19128, 49932),(19112, 49849)
1966 -49923 | (31287, 49923),(31236, 49913)
1967 -49912 | (43925, 49912),(43888, 49878)
1968 -49910 | (29261, 49910),(29247, 49818)
1969 -49873 | (14913, 49873),(14849, 49836)
1970 -49858 | (20007, 49858),(19921, 49778)
1971 -49852 | (38266, 49852),(38233, 49844)
1972 -49849 | (37595, 49849),(37581, 49834)
1973 (15 rows)
1974
1975 RESET enable_indexscan;