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