1 -- Tests for multirange data types.
5 -- negative tests; should fail
6 select ''::textmultirange;
7 ERROR: malformed multirange literal: ""
8 LINE 1: select ''::textmultirange;
10 DETAIL: Missing left brace.
11 select '{,}'::textmultirange;
12 ERROR: malformed multirange literal: "{,}"
13 LINE 1: select '{,}'::textmultirange;
15 DETAIL: Expected range start.
16 select '{(,)}.'::textmultirange;
17 ERROR: malformed multirange literal: "{(,)}."
18 LINE 1: select '{(,)}.'::textmultirange;
20 DETAIL: Junk after closing right brace.
21 select '{[a,c),}'::textmultirange;
22 ERROR: malformed multirange literal: "{[a,c),}"
23 LINE 1: select '{[a,c),}'::textmultirange;
25 DETAIL: Expected range start.
26 select '{,[a,c)}'::textmultirange;
27 ERROR: malformed multirange literal: "{,[a,c)}"
28 LINE 1: select '{,[a,c)}'::textmultirange;
30 DETAIL: Expected range start.
31 select '{-[a,z)}'::textmultirange;
32 ERROR: malformed multirange literal: "{-[a,z)}"
33 LINE 1: select '{-[a,z)}'::textmultirange;
35 DETAIL: Expected range start.
36 select '{[a,z) - }'::textmultirange;
37 ERROR: malformed multirange literal: "{[a,z) - }"
38 LINE 1: select '{[a,z) - }'::textmultirange;
40 DETAIL: Expected comma or end of multirange.
41 select '{(",a)}'::textmultirange;
42 ERROR: malformed multirange literal: "{(",a)}"
43 LINE 1: select '{(",a)}'::textmultirange;
45 DETAIL: Unexpected end of input.
46 select '{(,,a)}'::textmultirange;
47 ERROR: malformed range literal: "(,,a)"
48 LINE 1: select '{(,,a)}'::textmultirange;
50 DETAIL: Too many commas.
51 select '{(),a)}'::textmultirange;
52 ERROR: malformed range literal: "()"
53 LINE 1: select '{(),a)}'::textmultirange;
55 DETAIL: Missing comma after lower bound.
56 select '{(a,))}'::textmultirange;
57 ERROR: malformed multirange literal: "{(a,))}"
58 LINE 1: select '{(a,))}'::textmultirange;
60 DETAIL: Expected comma or end of multirange.
61 select '{(],a)}'::textmultirange;
62 ERROR: malformed range literal: "(]"
63 LINE 1: select '{(],a)}'::textmultirange;
65 DETAIL: Missing comma after lower bound.
66 select '{(a,])}'::textmultirange;
67 ERROR: malformed multirange literal: "{(a,])}"
68 LINE 1: select '{(a,])}'::textmultirange;
70 DETAIL: Expected comma or end of multirange.
71 select '{[z,a]}'::textmultirange;
72 ERROR: range lower bound must be less than or equal to range upper bound
73 LINE 1: select '{[z,a]}'::textmultirange;
76 select '{}'::textmultirange;
82 select ' {} '::textmultirange;
88 select ' { empty, empty } '::textmultirange;
94 select ' {( " a " " a ", " z " " z " ) }'::textmultirange;
96 ----------------------------
100 select textrange('\\\\', repeat('a', 200))::textmultirange;
102 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
103 {["\\\\\\\\",aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)}
106 select '{(,z)}'::textmultirange;
112 select '{(a,)}'::textmultirange;
118 select '{[,z]}'::textmultirange;
124 select '{[a,]}'::textmultirange;
130 select '{(,)}'::textmultirange;
136 select '{[ , ]}'::textmultirange;
142 select '{["",""]}'::textmultirange;
148 select '{[",",","]}'::textmultirange;
154 select '{["\\","\\"]}'::textmultirange;
160 select '{["""","\""]}'::textmultirange;
166 select '{(\\,a)}'::textmultirange;
172 select '{((,z)}'::textmultirange;
178 select '{([,z)}'::textmultirange;
184 select '{(!,()}'::textmultirange;
190 select '{(!,[)}'::textmultirange;
196 select '{[a,a]}'::textmultirange;
202 select '{[a,a],[a,b]}'::textmultirange;
208 select '{[a,b), [b,e]}'::textmultirange;
214 select '{[a,d), [b,f]}'::textmultirange;
220 select '{[a,a],[b,b]}'::textmultirange;
226 -- without canonicalization, we can't join these:
227 select '{[a,a], [b,b]}'::textmultirange;
233 -- with canonicalization, we can join these:
234 select '{[1,2], [3,4]}'::int4multirange;
240 select '{[a,a], [b,b], [c,c]}'::textmultirange;
242 ---------------------
246 select '{[a,d], [b,e]}'::textmultirange;
252 select '{[a,d), [d,e)}'::textmultirange;
258 -- these are allowed but normalize to empty:
259 select '{[a,a)}'::textmultirange;
265 select '{(a,a]}'::textmultirange;
271 select '{(a,a)}'::textmultirange;
277 -- Also try it with non-error-throwing API
278 select pg_input_is_valid('{[1,2], [4,5]}', 'int4multirange');
284 select pg_input_is_valid('{[1,2], [4,5]', 'int4multirange');
290 select * from pg_input_error_info('{[1,2], [4,5]', 'int4multirange');
291 message | detail | hint | sql_error_code
292 -----------------------------------------------+--------------------------+------+----------------
293 malformed multirange literal: "{[1,2], [4,5]" | Unexpected end of input. | | 22P02
296 select pg_input_is_valid('{[1,2], [4,zed]}', 'int4multirange');
302 select * from pg_input_error_info('{[1,2], [4,zed]}', 'int4multirange');
303 message | detail | hint | sql_error_code
304 ----------------------------------------------+--------+------+----------------
305 invalid input syntax for type integer: "zed" | | | 22P02
309 -- test the constructor
311 select textmultirange();
317 select textmultirange(textrange('a', 'c'));
323 select textmultirange(textrange('a', 'c'), textrange('f', 'g'));
329 select textmultirange(textrange('\\\\', repeat('a', 200)), textrange('c', 'd'));
331 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
332 {["\\\\\\\\",aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),[c,d)}
336 -- test casts, both a built-in range type and a user-defined one:
338 select 'empty'::int4range::int4multirange;
344 select int4range(1, 3)::int4multirange;
350 select int4range(1, null)::int4multirange;
356 select int4range(null, null)::int4multirange;
362 select 'empty'::textrange::textmultirange;
368 select textrange('a', 'c')::textmultirange;
374 select textrange('a', null)::textmultirange;
380 select textrange(null, null)::textmultirange;
387 -- test unnest(multirange) function
389 select unnest(int4multirange(int4range('5', '6'), int4range('1', '2')));
396 select unnest(textmultirange(textrange('a', 'b'), textrange('d', 'e')));
403 select unnest(textmultirange(textrange('\\\\', repeat('a', 200)), textrange('c', 'd')));
405 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
406 ["\\\\\\\\",aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
411 -- create some test data and test the operators
413 CREATE TABLE nummultirange_test (nmr NUMMULTIRANGE);
414 CREATE INDEX nummultirange_test_btree ON nummultirange_test(nmr);
415 INSERT INTO nummultirange_test VALUES('{}');
416 INSERT INTO nummultirange_test VALUES('{[,)}');
417 INSERT INTO nummultirange_test VALUES('{[3,]}');
418 INSERT INTO nummultirange_test VALUES('{[,), [3,]}');
419 INSERT INTO nummultirange_test VALUES('{[, 5)}');
420 INSERT INTO nummultirange_test VALUES(nummultirange());
421 INSERT INTO nummultirange_test VALUES(nummultirange(variadic '{}'::numrange[]));
422 INSERT INTO nummultirange_test VALUES(nummultirange(numrange(1.1, 2.2)));
423 INSERT INTO nummultirange_test VALUES('{empty}');
424 INSERT INTO nummultirange_test VALUES(nummultirange(numrange(1.7, 1.7, '[]'), numrange(1.7, 1.9)));
425 INSERT INTO nummultirange_test VALUES(nummultirange(numrange(1.7, 1.7, '[]'), numrange(1.9, 2.1)));
426 SELECT nmr, isempty(nmr), lower(nmr), upper(nmr) FROM nummultirange_test ORDER BY nmr;
427 nmr | isempty | lower | upper
428 -----------------------+---------+-------+-------
436 {[1.1,2.2)} | f | 1.1 | 2.2
437 {[1.7,1.7],[1.9,2.1)} | f | 1.7 | 2.1
438 {[1.7,1.9)} | f | 1.7 | 1.9
442 SELECT nmr, lower_inc(nmr), lower_inf(nmr), upper_inc(nmr), upper_inf(nmr) FROM nummultirange_test ORDER BY nmr;
443 nmr | lower_inc | lower_inf | upper_inc | upper_inf
444 -----------------------+-----------+-----------+-----------+-----------
449 {(,5)} | f | t | f | f
450 {(,)} | f | t | f | t
451 {(,)} | f | t | f | t
452 {[1.1,2.2)} | t | f | f | f
453 {[1.7,1.7],[1.9,2.1)} | t | f | f | f
454 {[1.7,1.9)} | t | f | f | f
455 {[3,)} | t | f | f | t
458 SELECT * FROM nummultirange_test WHERE nmr = '{}';
467 SELECT * FROM nummultirange_test WHERE nmr = '{(,5)}';
473 SELECT * FROM nummultirange_test WHERE nmr = '{[3,)}';
479 SELECT * FROM nummultirange_test WHERE nmr = '{[1.7,1.7]}';
484 SELECT * FROM nummultirange_test WHERE nmr = '{[1.7,1.7],[1.9,2.1)}';
486 -----------------------
487 {[1.7,1.7],[1.9,2.1)}
490 SELECT * FROM nummultirange_test WHERE nmr < '{}';
495 SELECT * FROM nummultirange_test WHERE nmr < '{[-1000.0, -1000.0]}';
507 SELECT * FROM nummultirange_test WHERE nmr < '{[0.0, 1.0]}';
519 SELECT * FROM nummultirange_test WHERE nmr < '{[1000.0, 1001.0]}';
521 -----------------------
532 {[1.7,1.7],[1.9,2.1)}
535 SELECT * FROM nummultirange_test WHERE nmr <= '{}';
544 SELECT * FROM nummultirange_test WHERE nmr <= '{[3,)}';
546 -----------------------
557 {[1.7,1.7],[1.9,2.1)}
560 SELECT * FROM nummultirange_test WHERE nmr >= '{}';
562 -----------------------
573 {[1.7,1.7],[1.9,2.1)}
576 SELECT * FROM nummultirange_test WHERE nmr >= '{[3,)}';
582 SELECT * FROM nummultirange_test WHERE nmr > '{}';
584 -----------------------
591 {[1.7,1.7],[1.9,2.1)}
594 SELECT * FROM nummultirange_test WHERE nmr > '{[-1000.0, -1000.0]}';
596 -----------------------
600 {[1.7,1.7],[1.9,2.1)}
603 SELECT * FROM nummultirange_test WHERE nmr > '{[0.0, 1.0]}';
605 -----------------------
609 {[1.7,1.7],[1.9,2.1)}
612 SELECT * FROM nummultirange_test WHERE nmr > '{[1000.0, 1001.0]}';
617 SELECT * FROM nummultirange_test WHERE nmr <> '{}';
619 -----------------------
626 {[1.7,1.7],[1.9,2.1)}
629 SELECT * FROM nummultirange_test WHERE nmr <> '{(,5)}';
631 -----------------------
641 {[1.7,1.7],[1.9,2.1)}
644 select nummultirange(numrange(2.0, 1.0));
645 ERROR: range lower bound must be less than or equal to range upper bound
646 select nummultirange(numrange(5.0, 6.0), numrange(1.0, 2.0));
648 -----------------------
649 {[1.0,2.0),[5.0,6.0)}
652 analyze nummultirange_test;
654 SELECT * FROM nummultirange_test WHERE range_overlaps_multirange(numrange(4.0, 4.2), nmr);
663 SELECT * FROM nummultirange_test WHERE numrange(4.0, 4.2) && nmr;
672 SELECT * FROM nummultirange_test WHERE multirange_overlaps_range(nmr, numrange(4.0, 4.2));
681 SELECT * FROM nummultirange_test WHERE nmr && numrange(4.0, 4.2);
690 SELECT * FROM nummultirange_test WHERE multirange_overlaps_multirange(nmr, nummultirange(numrange(4.0, 4.2), numrange(6.0, 7.0)));
699 SELECT * FROM nummultirange_test WHERE nmr && nummultirange(numrange(4.0, 4.2), numrange(6.0, 7.0));
708 SELECT * FROM nummultirange_test WHERE nmr && nummultirange(numrange(6.0, 7.0));
716 SELECT * FROM nummultirange_test WHERE nmr && nummultirange(numrange(6.0, 7.0), numrange(8.0, 9.0));
725 SELECT * FROM nummultirange_test WHERE multirange_contains_elem(nmr, 4.0);
734 SELECT * FROM nummultirange_test WHERE nmr @> 4.0;
743 SELECT * FROM nummultirange_test WHERE multirange_contains_range(nmr, numrange(4.0, 4.2));
752 SELECT * FROM nummultirange_test WHERE nmr @> numrange(4.0, 4.2);
761 SELECT * FROM nummultirange_test WHERE multirange_contains_multirange(nmr, '{[4.0,4.2), [6.0, 8.0)}');
769 SELECT * FROM nummultirange_test WHERE nmr @> '{[4.0,4.2), [6.0, 8.0)}'::nummultirange;
777 -- x is contained by mr
778 SELECT * FROM nummultirange_test WHERE elem_contained_by_multirange(4.0, nmr);
787 SELECT * FROM nummultirange_test WHERE 4.0 <@ nmr;
796 SELECT * FROM nummultirange_test WHERE range_contained_by_multirange(numrange(4.0, 4.2), nmr);
805 SELECT * FROM nummultirange_test WHERE numrange(4.0, 4.2) <@ nmr;
814 SELECT * FROM nummultirange_test WHERE multirange_contained_by_multirange('{[4.0,4.2), [6.0, 8.0)}', nmr);
822 SELECT * FROM nummultirange_test WHERE '{[4.0,4.2), [6.0, 8.0)}'::nummultirange <@ nmr;
831 SELECT 'empty'::numrange && nummultirange();
837 SELECT 'empty'::numrange && nummultirange(numrange(1,2));
843 SELECT nummultirange() && 'empty'::numrange;
849 SELECT nummultirange(numrange(1,2)) && 'empty'::numrange;
855 SELECT nummultirange() && nummultirange();
861 SELECT nummultirange() && nummultirange(numrange(1,2));
867 SELECT nummultirange(numrange(1,2)) && nummultirange();
873 SELECT nummultirange(numrange(3,4)) && nummultirange(numrange(1,2), numrange(7,8));
879 SELECT nummultirange(numrange(1,2), numrange(7,8)) && nummultirange(numrange(3,4));
885 SELECT nummultirange(numrange(3,4)) && nummultirange(numrange(1,2), numrange(3.5,8));
891 SELECT nummultirange(numrange(1,2), numrange(3.5,8)) && numrange(3,4);
897 SELECT nummultirange(numrange(1,2), numrange(3.5,8)) && nummultirange(numrange(3,4));
903 select '{(10,20),(30,40),(50,60)}'::nummultirange && '(42,92)'::numrange;
910 SELECT nummultirange() @> nummultirange();
916 SELECT nummultirange() @> 'empty'::numrange;
922 SELECT nummultirange(numrange(null,null)) @> numrange(1,2);
928 SELECT nummultirange(numrange(null,null)) @> numrange(null,2);
934 SELECT nummultirange(numrange(null,null)) @> numrange(2,null);
940 SELECT nummultirange(numrange(null,5)) @> numrange(null,3);
946 SELECT nummultirange(numrange(null,5)) @> numrange(null,8);
952 SELECT nummultirange(numrange(5,null)) @> numrange(8,null);
958 SELECT nummultirange(numrange(5,null)) @> numrange(3,null);
964 SELECT nummultirange(numrange(1,5)) @> numrange(8,9);
970 SELECT nummultirange(numrange(1,5)) @> numrange(3,9);
976 SELECT nummultirange(numrange(1,5)) @> numrange(1,4);
982 SELECT nummultirange(numrange(1,5)) @> numrange(1,5);
988 SELECT nummultirange(numrange(-4,-2), numrange(1,5)) @> numrange(1,5);
994 SELECT nummultirange(numrange(1,5), numrange(8,9)) @> numrange(1,5);
1000 SELECT nummultirange(numrange(1,5), numrange(8,9)) @> numrange(6,7);
1006 SELECT nummultirange(numrange(1,5), numrange(6,9)) @> numrange(6,7);
1012 SELECT '{[1,5)}'::nummultirange @> '{[1,5)}';
1018 SELECT '{[-4,-2), [1,5)}'::nummultirange @> '{[1,5)}';
1024 SELECT '{[1,5), [8,9)}'::nummultirange @> '{[1,5)}';
1030 SELECT '{[1,5), [8,9)}'::nummultirange @> '{[6,7)}';
1036 SELECT '{[1,5), [6,9)}'::nummultirange @> '{[6,7)}';
1042 select '{(10,20),(30,40),(50,60)}'::nummultirange @> '(52,56)'::numrange;
1048 SELECT numrange(null,null) @> nummultirange(numrange(1,2));
1054 SELECT numrange(null,null) @> nummultirange(numrange(null,2));
1060 SELECT numrange(null,null) @> nummultirange(numrange(2,null));
1066 SELECT numrange(null,5) @> nummultirange(numrange(null,3));
1072 SELECT numrange(null,5) @> nummultirange(numrange(null,8));
1078 SELECT numrange(5,null) @> nummultirange(numrange(8,null));
1084 SELECT numrange(5,null) @> nummultirange(numrange(3,null));
1090 SELECT numrange(1,5) @> nummultirange(numrange(8,9));
1096 SELECT numrange(1,5) @> nummultirange(numrange(3,9));
1102 SELECT numrange(1,5) @> nummultirange(numrange(1,4));
1108 SELECT numrange(1,5) @> nummultirange(numrange(1,5));
1114 SELECT numrange(1,9) @> nummultirange(numrange(-4,-2), numrange(1,5));
1120 SELECT numrange(1,9) @> nummultirange(numrange(1,5), numrange(8,9));
1126 SELECT numrange(1,9) @> nummultirange(numrange(1,5), numrange(6,9));
1132 SELECT numrange(1,9) @> nummultirange(numrange(1,5), numrange(6,10));
1138 SELECT '{[1,9)}' @> '{[1,5)}'::nummultirange;
1144 SELECT '{[1,9)}' @> '{[-4,-2), [1,5)}'::nummultirange;
1150 SELECT '{[1,9)}' @> '{[1,5), [8,9)}'::nummultirange;
1156 SELECT '{[1,9)}' @> '{[1,5), [6,9)}'::nummultirange;
1162 SELECT '{[1,9)}' @> '{[1,5), [6,10)}'::nummultirange;
1169 SELECT nummultirange() <@ nummultirange();
1175 SELECT 'empty'::numrange <@ nummultirange();
1181 SELECT numrange(1,2) <@ nummultirange(numrange(null,null));
1187 SELECT numrange(null,2) <@ nummultirange(numrange(null,null));
1193 SELECT numrange(2,null) <@ nummultirange(numrange(null,null));
1199 SELECT numrange(null,3) <@ nummultirange(numrange(null,5));
1205 SELECT numrange(null,8) <@ nummultirange(numrange(null,5));
1211 SELECT numrange(8,null) <@ nummultirange(numrange(5,null));
1217 SELECT numrange(3,null) <@ nummultirange(numrange(5,null));
1223 SELECT numrange(8,9) <@ nummultirange(numrange(1,5));
1229 SELECT numrange(3,9) <@ nummultirange(numrange(1,5));
1235 SELECT numrange(1,4) <@ nummultirange(numrange(1,5));
1241 SELECT numrange(1,5) <@ nummultirange(numrange(1,5));
1247 SELECT numrange(1,5) <@ nummultirange(numrange(-4,-2), numrange(1,5));
1253 SELECT numrange(1,5) <@ nummultirange(numrange(1,5), numrange(8,9));
1259 SELECT numrange(6,7) <@ nummultirange(numrange(1,5), numrange(8,9));
1265 SELECT numrange(6,7) <@ nummultirange(numrange(1,5), numrange(6,9));
1271 SELECT '{[1,5)}' <@ '{[1,5)}'::nummultirange;
1277 SELECT '{[1,5)}' <@ '{[-4,-2), [1,5)}'::nummultirange;
1283 SELECT '{[1,5)}' <@ '{[1,5), [8,9)}'::nummultirange;
1289 SELECT '{[6,7)}' <@ '{[1,5), [8,9)}'::nummultirange;
1295 SELECT '{[6,7)}' <@ '{[1,5), [6,9)}'::nummultirange;
1301 SELECT nummultirange(numrange(1,2)) <@ numrange(null,null);
1307 SELECT nummultirange(numrange(null,2)) <@ numrange(null,null);
1313 SELECT nummultirange(numrange(2,null)) <@ numrange(null,null);
1319 SELECT nummultirange(numrange(null,3)) <@ numrange(null,5);
1325 SELECT nummultirange(numrange(null,8)) <@ numrange(null,5);
1331 SELECT nummultirange(numrange(8,null)) <@ numrange(5,null);
1337 SELECT nummultirange(numrange(3,null)) <@ numrange(5,null);
1343 SELECT nummultirange(numrange(8,9)) <@ numrange(1,5);
1349 SELECT nummultirange(numrange(3,9)) <@ numrange(1,5);
1355 SELECT nummultirange(numrange(1,4)) <@ numrange(1,5);
1361 SELECT nummultirange(numrange(1,5)) <@ numrange(1,5);
1367 SELECT nummultirange(numrange(-4,-2), numrange(1,5)) <@ numrange(1,9);
1373 SELECT nummultirange(numrange(1,5), numrange(8,9)) <@ numrange(1,9);
1379 SELECT nummultirange(numrange(1,5), numrange(6,9)) <@ numrange(1,9);
1385 SELECT nummultirange(numrange(1,5), numrange(6,10)) <@ numrange(1,9);
1391 SELECT '{[1,5)}'::nummultirange <@ '{[1,9)}';
1397 SELECT '{[-4,-2), [1,5)}'::nummultirange <@ '{[1,9)}';
1403 SELECT '{[1,5), [8,9)}'::nummultirange <@ '{[1,9)}';
1409 SELECT '{[1,5), [6,9)}'::nummultirange <@ '{[1,9)}';
1415 SELECT '{[1,5), [6,10)}'::nummultirange <@ '{[1,9)}';
1422 SELECT 'empty'::numrange &< nummultirange();
1428 SELECT 'empty'::numrange &< nummultirange(numrange(1,2));
1434 SELECT nummultirange() &< 'empty'::numrange;
1440 SELECT nummultirange(numrange(1,2)) &< 'empty'::numrange;
1446 SELECT nummultirange() &< nummultirange();
1452 SELECT nummultirange(numrange(1,2)) &< nummultirange();
1458 SELECT nummultirange() &< nummultirange(numrange(1,2));
1464 SELECT numrange(6,7) &< nummultirange(numrange(3,4));
1470 SELECT numrange(1,2) &< nummultirange(numrange(3,4));
1476 SELECT numrange(1,4) &< nummultirange(numrange(3,4));
1482 SELECT numrange(1,6) &< nummultirange(numrange(3,4));
1488 SELECT numrange(3.5,6) &< nummultirange(numrange(3,4));
1494 SELECT nummultirange(numrange(6,7)) &< numrange(3,4);
1500 SELECT nummultirange(numrange(1,2)) &< numrange(3,4);
1506 SELECT nummultirange(numrange(1,4)) &< numrange(3,4);
1512 SELECT nummultirange(numrange(1,6)) &< numrange(3,4);
1518 SELECT nummultirange(numrange(3.5,6)) &< numrange(3,4);
1524 SELECT nummultirange(numrange(6,7)) &< nummultirange(numrange(3,4));
1530 SELECT nummultirange(numrange(1,2)) &< nummultirange(numrange(3,4));
1536 SELECT nummultirange(numrange(1,4)) &< nummultirange(numrange(3,4));
1542 SELECT nummultirange(numrange(1,6)) &< nummultirange(numrange(3,4));
1548 SELECT nummultirange(numrange(3.5,6)) &< nummultirange(numrange(3,4));
1555 SELECT nummultirange() &> 'empty'::numrange;
1561 SELECT nummultirange(numrange(1,2)) &> 'empty'::numrange;
1567 SELECT 'empty'::numrange &> nummultirange();
1573 SELECT 'empty'::numrange &> nummultirange(numrange(1,2));
1579 SELECT nummultirange() &> nummultirange();
1585 SELECT nummultirange() &> nummultirange(numrange(1,2));
1591 SELECT nummultirange(numrange(1,2)) &> nummultirange();
1597 SELECT nummultirange(numrange(3,4)) &> numrange(6,7);
1603 SELECT nummultirange(numrange(3,4)) &> numrange(1,2);
1609 SELECT nummultirange(numrange(3,4)) &> numrange(1,4);
1615 SELECT nummultirange(numrange(3,4)) &> numrange(1,6);
1621 SELECT nummultirange(numrange(3,4)) &> numrange(3.5,6);
1627 SELECT numrange(3,4) &> nummultirange(numrange(6,7));
1633 SELECT numrange(3,4) &> nummultirange(numrange(1,2));
1639 SELECT numrange(3,4) &> nummultirange(numrange(1,4));
1645 SELECT numrange(3,4) &> nummultirange(numrange(1,6));
1651 SELECT numrange(3,4) &> nummultirange(numrange(3.5,6));
1657 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(6,7));
1663 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(1,2));
1669 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(1,4));
1675 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(1,6));
1681 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(3.5,6));
1688 SELECT 'empty'::numrange -|- nummultirange();
1694 SELECT 'empty'::numrange -|- nummultirange(numrange(1,2));
1700 SELECT nummultirange() -|- 'empty'::numrange;
1706 SELECT nummultirange(numrange(1,2)) -|- 'empty'::numrange;
1712 SELECT nummultirange() -|- nummultirange();
1718 SELECT nummultirange(numrange(1,2)) -|- nummultirange();
1724 SELECT nummultirange() -|- nummultirange(numrange(1,2));
1730 SELECT numrange(1,2) -|- nummultirange(numrange(2,4));
1736 SELECT numrange(1,2) -|- nummultirange(numrange(3,4));
1742 SELECT nummultirange(numrange(1,2)) -|- numrange(2,4);
1748 SELECT nummultirange(numrange(1,2)) -|- numrange(3,4);
1754 SELECT nummultirange(numrange(1,2)) -|- nummultirange(numrange(2,4));
1760 SELECT nummultirange(numrange(1,2)) -|- nummultirange(numrange(3,4));
1766 SELECT nummultirange(numrange(1,2), numrange(5,6)) -|- nummultirange(numrange(3,4));
1772 SELECT nummultirange(numrange(1,2), numrange(5,6)) -|- nummultirange(numrange(6,7));
1778 SELECT nummultirange(numrange(1,2), numrange(5,6)) -|- nummultirange(numrange(8,9));
1784 SELECT nummultirange(numrange(1,2)) -|- nummultirange(numrange(2,4), numrange(6,7));
1791 select 'empty'::numrange << nummultirange();
1797 select numrange(1,2) << nummultirange();
1803 select numrange(1,2) << nummultirange(numrange(3,4));
1809 select numrange(1,2) << nummultirange(numrange(0,4));
1815 select numrange(1,2) << nummultirange(numrange(0,4), numrange(7,8));
1821 select nummultirange() << 'empty'::numrange;
1827 select nummultirange() << numrange(1,2);
1833 select nummultirange(numrange(3,4)) << numrange(3,6);
1839 select nummultirange(numrange(0,2)) << numrange(3,6);
1845 select nummultirange(numrange(0,2), numrange(7,8)) << numrange(3,6);
1851 select nummultirange(numrange(-4,-2), numrange(0,2)) << numrange(3,6);
1857 select nummultirange() << nummultirange();
1863 select nummultirange() << nummultirange(numrange(1,2));
1869 select nummultirange(numrange(1,2)) << nummultirange();
1875 select nummultirange(numrange(1,2)) << nummultirange(numrange(1,2));
1881 select nummultirange(numrange(1,2)) << nummultirange(numrange(3,4));
1887 select nummultirange(numrange(1,2)) << nummultirange(numrange(3,4), numrange(7,8));
1893 select nummultirange(numrange(1,2), numrange(4,5)) << nummultirange(numrange(3,4), numrange(7,8));
1900 select nummultirange() >> 'empty'::numrange;
1906 select nummultirange() >> numrange(1,2);
1912 select nummultirange(numrange(3,4)) >> numrange(1,2);
1918 select nummultirange(numrange(0,4)) >> numrange(1,2);
1924 select nummultirange(numrange(0,4), numrange(7,8)) >> numrange(1,2);
1930 select 'empty'::numrange >> nummultirange();
1936 select numrange(1,2) >> nummultirange();
1942 select numrange(3,6) >> nummultirange(numrange(3,4));
1948 select numrange(3,6) >> nummultirange(numrange(0,2));
1954 select numrange(3,6) >> nummultirange(numrange(0,2), numrange(7,8));
1960 select numrange(3,6) >> nummultirange(numrange(-4,-2), numrange(0,2));
1966 select nummultirange() >> nummultirange();
1972 select nummultirange(numrange(1,2)) >> nummultirange();
1978 select nummultirange() >> nummultirange(numrange(1,2));
1984 select nummultirange(numrange(1,2)) >> nummultirange(numrange(1,2));
1990 select nummultirange(numrange(3,4)) >> nummultirange(numrange(1,2));
1996 select nummultirange(numrange(3,4), numrange(7,8)) >> nummultirange(numrange(1,2));
2002 select nummultirange(numrange(3,4), numrange(7,8)) >> nummultirange(numrange(1,2), numrange(4,5));
2009 SELECT nummultirange() + nummultirange();
2015 SELECT nummultirange() + nummultirange(numrange(1,2));
2021 SELECT nummultirange(numrange(1,2)) + nummultirange();
2027 SELECT nummultirange(numrange(1,2)) + nummultirange(numrange(1,2));
2033 SELECT nummultirange(numrange(1,2)) + nummultirange(numrange(2,4));
2039 SELECT nummultirange(numrange(1,2)) + nummultirange(numrange(3,4));
2045 SELECT nummultirange(numrange(1,2), numrange(4,5)) + nummultirange(numrange(2,4));
2051 SELECT nummultirange(numrange(1,2), numrange(4,5)) + nummultirange(numrange(3,4));
2057 SELECT nummultirange(numrange(1,2), numrange(4,5)) + nummultirange(numrange(0,9));
2064 SELECT range_merge(nummultirange());
2070 SELECT range_merge(nummultirange(numrange(1,2)));
2076 SELECT range_merge(nummultirange(numrange(1,2), numrange(7,8)));
2083 SELECT nummultirange() - nummultirange();
2089 SELECT nummultirange() - nummultirange(numrange(1,2));
2095 SELECT nummultirange(numrange(1,2)) - nummultirange();
2101 SELECT nummultirange(numrange(1,2), numrange(3,4)) - nummultirange();
2107 SELECT nummultirange(numrange(1,2)) - nummultirange(numrange(1,2));
2113 SELECT nummultirange(numrange(1,2)) - nummultirange(numrange(2,4));
2119 SELECT nummultirange(numrange(1,2)) - nummultirange(numrange(3,4));
2125 SELECT nummultirange(numrange(1,4)) - nummultirange(numrange(1,2));
2131 SELECT nummultirange(numrange(1,4)) - nummultirange(numrange(2,3));
2137 SELECT nummultirange(numrange(1,4)) - nummultirange(numrange(0,8));
2143 SELECT nummultirange(numrange(1,4)) - nummultirange(numrange(0,2));
2149 SELECT nummultirange(numrange(1,8)) - nummultirange(numrange(0,2), numrange(3,4));
2155 SELECT nummultirange(numrange(1,8)) - nummultirange(numrange(2,3), numrange(5,null));
2161 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(-2,0));
2167 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(2,4));
2173 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(3,5));
2179 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(0,9));
2185 SELECT nummultirange(numrange(1,3), numrange(4,5)) - nummultirange(numrange(2,9));
2191 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(8,9));
2197 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(-2,0), numrange(8,9));
2204 SELECT nummultirange() * nummultirange();
2210 SELECT nummultirange() * nummultirange(numrange(1,2));
2216 SELECT nummultirange(numrange(1,2)) * nummultirange();
2222 SELECT '{[1,3)}'::nummultirange * '{[1,5)}'::nummultirange;
2228 SELECT '{[1,3)}'::nummultirange * '{[0,5)}'::nummultirange;
2234 SELECT '{[1,3)}'::nummultirange * '{[0,2)}'::nummultirange;
2240 SELECT '{[1,3)}'::nummultirange * '{[2,5)}'::nummultirange;
2246 SELECT '{[1,4)}'::nummultirange * '{[2,3)}'::nummultirange;
2252 SELECT '{[1,4)}'::nummultirange * '{[0,2), [3,5)}'::nummultirange;
2258 SELECT '{[1,4), [7,10)}'::nummultirange * '{[0,8), [9,12)}'::nummultirange;
2260 ----------------------
2261 {[1,4),[7,8),[9,10)}
2264 SELECT '{[1,4), [7,10)}'::nummultirange * '{[9,12)}'::nummultirange;
2270 SELECT '{[1,4), [7,10)}'::nummultirange * '{[-5,-4), [5,6), [9,12)}'::nummultirange;
2276 SELECT '{[1,4), [7,10)}'::nummultirange * '{[0,2), [3,8), [9,12)}'::nummultirange;
2278 ----------------------------
2279 {[1,2),[3,4),[7,8),[9,10)}
2282 SELECT '{[1,4), [7,10)}'::nummultirange * '{[0,2), [3,8), [9,12)}'::nummultirange;
2284 ----------------------------
2285 {[1,2),[3,4),[7,8),[9,10)}
2289 create table test_multirange_gist(mr int4multirange);
2290 insert into test_multirange_gist select int4multirange(int4range(g, g+10),int4range(g+20, g+30),int4range(g+40, g+50)) from generate_series(1,2000) g;
2291 insert into test_multirange_gist select '{}'::int4multirange from generate_series(1,500) g;
2292 insert into test_multirange_gist select int4multirange(int4range(g, g+10000)) from generate_series(1,1000) g;
2293 insert into test_multirange_gist select int4multirange(int4range(NULL, g*10, '(]'), int4range(g*10, g*20, '(]')) from generate_series(1,100) g;
2294 insert into test_multirange_gist select int4multirange(int4range(g*10, g*20, '(]'), int4range(g*20, NULL, '(]')) from generate_series(1,100) g;
2295 create index test_mulrirange_gist_idx on test_multirange_gist using gist (mr);
2296 -- test statistics and selectivity estimation as well
2298 -- We don't check the accuracy of selectivity estimation, but at least check
2300 analyze test_multirange_gist;
2301 -- first, verify non-indexed results
2302 SET enable_seqscan = t;
2303 SET enable_indexscan = f;
2304 SET enable_bitmapscan = f;
2305 select count(*) from test_multirange_gist where mr = '{}'::int4multirange;
2311 select count(*) from test_multirange_gist where mr @> 'empty'::int4range;
2317 select count(*) from test_multirange_gist where mr && 'empty'::int4range;
2323 select count(*) from test_multirange_gist where mr <@ 'empty'::int4range;
2329 select count(*) from test_multirange_gist where mr << 'empty'::int4range;
2335 select count(*) from test_multirange_gist where mr >> 'empty'::int4range;
2341 select count(*) from test_multirange_gist where mr &< 'empty'::int4range;
2347 select count(*) from test_multirange_gist where mr &> 'empty'::int4range;
2353 select count(*) from test_multirange_gist where mr -|- 'empty'::int4range;
2359 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2365 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2371 select count(*) from test_multirange_gist where mr && '{}'::int4multirange;
2377 select count(*) from test_multirange_gist where mr <@ '{}'::int4multirange;
2383 select count(*) from test_multirange_gist where mr << '{}'::int4multirange;
2389 select count(*) from test_multirange_gist where mr >> '{}'::int4multirange;
2395 select count(*) from test_multirange_gist where mr &< '{}'::int4multirange;
2401 select count(*) from test_multirange_gist where mr &> '{}'::int4multirange;
2407 select count(*) from test_multirange_gist where mr -|- '{}'::int4multirange;
2413 select count(*) from test_multirange_gist where mr = int4multirange(int4range(10,20), int4range(30,40), int4range(50,60));
2419 select count(*) from test_multirange_gist where mr @> 10;
2425 select count(*) from test_multirange_gist where mr @> int4range(10,20);
2431 select count(*) from test_multirange_gist where mr && int4range(10,20);
2437 select count(*) from test_multirange_gist where mr <@ int4range(10,50);
2443 select count(*) from test_multirange_gist where mr << int4range(100,500);
2449 select count(*) from test_multirange_gist where mr >> int4range(100,500);
2455 select count(*) from test_multirange_gist where mr &< int4range(100,500);
2461 select count(*) from test_multirange_gist where mr &> int4range(100,500);
2467 select count(*) from test_multirange_gist where mr -|- int4range(100,500);
2473 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2479 select count(*) from test_multirange_gist where mr @> int4multirange(int4range(10,20), int4range(30,40));
2485 select count(*) from test_multirange_gist where mr && '{(10,20),(30,40),(50,60)}'::int4multirange;
2491 select count(*) from test_multirange_gist where mr <@ '{(10,30),(40,60),(70,90)}'::int4multirange;
2497 select count(*) from test_multirange_gist where mr << int4multirange(int4range(100,200), int4range(400,500));
2503 select count(*) from test_multirange_gist where mr >> int4multirange(int4range(100,200), int4range(400,500));
2509 select count(*) from test_multirange_gist where mr &< int4multirange(int4range(100,200), int4range(400,500));
2515 select count(*) from test_multirange_gist where mr &> int4multirange(int4range(100,200), int4range(400,500));
2521 select count(*) from test_multirange_gist where mr -|- int4multirange(int4range(100,200), int4range(400,500));
2527 -- now check same queries using index
2528 SET enable_seqscan = f;
2529 SET enable_indexscan = t;
2530 SET enable_bitmapscan = f;
2531 select count(*) from test_multirange_gist where mr = '{}'::int4multirange;
2537 select count(*) from test_multirange_gist where mr @> 'empty'::int4range;
2543 select count(*) from test_multirange_gist where mr && 'empty'::int4range;
2549 select count(*) from test_multirange_gist where mr <@ 'empty'::int4range;
2555 select count(*) from test_multirange_gist where mr << 'empty'::int4range;
2561 select count(*) from test_multirange_gist where mr >> 'empty'::int4range;
2567 select count(*) from test_multirange_gist where mr &< 'empty'::int4range;
2573 select count(*) from test_multirange_gist where mr &> 'empty'::int4range;
2579 select count(*) from test_multirange_gist where mr -|- 'empty'::int4range;
2585 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2591 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2597 select count(*) from test_multirange_gist where mr && '{}'::int4multirange;
2603 select count(*) from test_multirange_gist where mr <@ '{}'::int4multirange;
2609 select count(*) from test_multirange_gist where mr << '{}'::int4multirange;
2615 select count(*) from test_multirange_gist where mr >> '{}'::int4multirange;
2621 select count(*) from test_multirange_gist where mr &< '{}'::int4multirange;
2627 select count(*) from test_multirange_gist where mr &> '{}'::int4multirange;
2633 select count(*) from test_multirange_gist where mr -|- '{}'::int4multirange;
2639 select count(*) from test_multirange_gist where mr @> 'empty'::int4range;
2645 select count(*) from test_multirange_gist where mr = int4multirange(int4range(10,20), int4range(30,40), int4range(50,60));
2651 select count(*) from test_multirange_gist where mr @> 10;
2657 select count(*) from test_multirange_gist where mr @> int4range(10,20);
2663 select count(*) from test_multirange_gist where mr && int4range(10,20);
2669 select count(*) from test_multirange_gist where mr <@ int4range(10,50);
2675 select count(*) from test_multirange_gist where mr << int4range(100,500);
2681 select count(*) from test_multirange_gist where mr >> int4range(100,500);
2687 select count(*) from test_multirange_gist where mr &< int4range(100,500);
2693 select count(*) from test_multirange_gist where mr &> int4range(100,500);
2699 select count(*) from test_multirange_gist where mr -|- int4range(100,500);
2705 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2711 select count(*) from test_multirange_gist where mr @> int4multirange(int4range(10,20), int4range(30,40));
2717 select count(*) from test_multirange_gist where mr && '{(10,20),(30,40),(50,60)}'::int4multirange;
2723 select count(*) from test_multirange_gist where mr <@ '{(10,30),(40,60),(70,90)}'::int4multirange;
2729 select count(*) from test_multirange_gist where mr << int4multirange(int4range(100,200), int4range(400,500));
2735 select count(*) from test_multirange_gist where mr >> int4multirange(int4range(100,200), int4range(400,500));
2741 select count(*) from test_multirange_gist where mr &< int4multirange(int4range(100,200), int4range(400,500));
2747 select count(*) from test_multirange_gist where mr &> int4multirange(int4range(100,200), int4range(400,500));
2753 select count(*) from test_multirange_gist where mr -|- int4multirange(int4range(100,200), int4range(400,500));
2759 drop table test_multirange_gist;
2761 -- range_agg function
2763 create table reservations ( room_id integer not null, booked_during daterange );
2764 insert into reservations values
2765 -- 1: has a meets and a gap
2766 (1, daterange('2018-07-01', '2018-07-07')),
2767 (1, daterange('2018-07-07', '2018-07-14')),
2768 (1, daterange('2018-07-20', '2018-07-22')),
2769 -- 2: just a single row
2770 (2, daterange('2018-07-01', '2018-07-03')),
2771 -- 3: one null range
2773 -- 4: two null ranges
2776 -- 5: a null range and a non-null range
2778 (5, daterange('2018-07-01', '2018-07-03')),
2780 (6, daterange('2018-07-01', '2018-07-07')),
2781 (6, daterange('2018-07-05', '2018-07-10')),
2782 -- 7: two ranges that meet: no gap or overlap
2783 (7, daterange('2018-07-01', '2018-07-07')),
2784 (7, daterange('2018-07-07', '2018-07-14')),
2785 -- 8: an empty range
2786 (8, 'empty'::daterange)
2788 SELECT room_id, range_agg(booked_during)
2793 ---------+---------------------------------------------------
2794 1 | {[07-01-2018,07-14-2018),[07-20-2018,07-22-2018)}
2795 2 | {[07-01-2018,07-03-2018)}
2798 5 | {[07-01-2018,07-03-2018)}
2799 6 | {[07-01-2018,07-10-2018)}
2800 7 | {[07-01-2018,07-14-2018)}
2804 -- range_agg on a custom range type too
2807 ('[a,c]'::textrange),
2808 ('[b,b]'::textrange),
2809 ('[c,f]'::textrange),
2810 ('[g,h)'::textrange),
2811 ('[h,j)'::textrange)
2818 -- range_agg with multirange inputs
2819 select range_agg(nmr) from nummultirange_test;
2825 select range_agg(nmr) from nummultirange_test where false;
2831 select range_agg(null::nummultirange) from nummultirange_test;
2837 select range_agg(nmr) from (values ('{}'::nummultirange)) t(nmr);
2843 select range_agg(nmr) from (values ('{}'::nummultirange), ('{}'::nummultirange)) t(nmr);
2849 select range_agg(nmr) from (values ('{[1,2]}'::nummultirange)) t(nmr);
2855 select range_agg(nmr) from (values ('{[1,2], [5,6]}'::nummultirange)) t(nmr);
2861 select range_agg(nmr) from (values ('{[1,2], [2,3]}'::nummultirange)) t(nmr);
2867 select range_agg(nmr) from (values ('{[1,2]}'::nummultirange), ('{[5,6]}'::nummultirange)) t(nmr);
2873 select range_agg(nmr) from (values ('{[1,2]}'::nummultirange), ('{[2,3]}'::nummultirange)) t(nmr);
2880 -- range_intersect_agg function
2882 select range_intersect_agg(nmr) from nummultirange_test;
2884 ---------------------
2888 select range_intersect_agg(nmr) from nummultirange_test where false;
2890 ---------------------
2894 select range_intersect_agg(null::nummultirange) from nummultirange_test;
2896 ---------------------
2900 select range_intersect_agg(nmr) from (values ('{[1,3]}'::nummultirange), ('{[6,12]}'::nummultirange)) t(nmr);
2902 ---------------------
2906 select range_intersect_agg(nmr) from (values ('{[1,6]}'::nummultirange), ('{[3,12]}'::nummultirange)) t(nmr);
2908 ---------------------
2912 select range_intersect_agg(nmr) from (values ('{[1,6], [10,12]}'::nummultirange), ('{[4,14]}'::nummultirange)) t(nmr);
2914 ---------------------
2918 -- test with just one input:
2919 select range_intersect_agg(nmr) from (values ('{}'::nummultirange)) t(nmr);
2921 ---------------------
2925 select range_intersect_agg(nmr) from (values ('{[1,2]}'::nummultirange)) t(nmr);
2927 ---------------------
2931 select range_intersect_agg(nmr) from (values ('{[1,6], [10,12]}'::nummultirange)) t(nmr);
2933 ---------------------
2937 select range_intersect_agg(nmr) from nummultirange_test where nmr @> 4.0;
2939 ---------------------
2943 create table nummultirange_test2(nmr nummultirange);
2944 create index nummultirange_test2_hash_idx on nummultirange_test2 using hash (nmr);
2945 INSERT INTO nummultirange_test2 VALUES('{[, 5)}');
2946 INSERT INTO nummultirange_test2 VALUES(nummultirange(numrange(1.1, 2.2)));
2947 INSERT INTO nummultirange_test2 VALUES(nummultirange(numrange(1.1, 2.2)));
2948 INSERT INTO nummultirange_test2 VALUES(nummultirange(numrange(1.1, 2.2,'()')));
2949 INSERT INTO nummultirange_test2 VALUES('{}');
2950 select * from nummultirange_test2 where nmr = '{}';
2956 select * from nummultirange_test2 where nmr = nummultirange(numrange(1.1, 2.2));
2963 select * from nummultirange_test2 where nmr = nummultirange(numrange(1.1, 2.3));
2968 set enable_nestloop=t;
2969 set enable_hashjoin=f;
2970 set enable_mergejoin=f;
2971 select * from nummultirange_test natural join nummultirange_test2 order by nmr;
2983 set enable_nestloop=f;
2984 set enable_hashjoin=t;
2985 set enable_mergejoin=f;
2986 select * from nummultirange_test natural join nummultirange_test2 order by nmr;
2998 set enable_nestloop=f;
2999 set enable_hashjoin=f;
3000 set enable_mergejoin=t;
3001 select * from nummultirange_test natural join nummultirange_test2 order by nmr;
3013 set enable_nestloop to default;
3014 set enable_hashjoin to default;
3015 set enable_mergejoin to default;
3016 DROP TABLE nummultirange_test2;
3018 -- Test user-defined multirange of floats
3020 select '{[123.001, 5.e9)}'::float8multirange @> 888.882::float8;
3026 create table float8multirange_test(f8mr float8multirange, i int);
3027 insert into float8multirange_test values(float8multirange(float8range(-100.00007, '1.111113e9')), 42);
3028 select * from float8multirange_test;
3030 ---------------------------+----
3031 {[-100.00007,1111113000)} | 42
3034 drop table float8multirange_test;
3036 -- Test multirange types over domains
3038 create domain mydomain as int4;
3039 create type mydomainrange as range(subtype=mydomain);
3040 select '{[4,50)}'::mydomainmultirange @> 7::mydomain;
3046 drop domain mydomain cascade;
3047 NOTICE: drop cascades to type mydomainrange
3049 -- Test domains over multirange types
3051 create domain restrictedmultirange as int4multirange check (upper(value) < 10);
3052 select '{[4,5)}'::restrictedmultirange @> 7;
3058 select '{[4,50)}'::restrictedmultirange @> 7; -- should fail
3059 ERROR: value for domain restrictedmultirange violates check constraint "restrictedmultirange_check"
3060 drop domain restrictedmultirange;
3062 -- Check automatic naming of multiranges
3064 create type intr as range(subtype=int);
3065 select intr_multirange(intr(1,10));
3072 create type intmultirange as (x int, y int);
3073 create type intrange as range(subtype=int); -- should fail
3074 ERROR: type "intmultirange" already exists
3075 DETAIL: Failed while creating a multirange type for type "intrange".
3076 HINT: You can manually specify a multirange type name using the "multirange_type_name" attribute.
3077 drop type intmultirange;
3078 create type intr_multirange as (x int, y int);
3079 create type intr as range(subtype=int); -- should fail
3080 ERROR: type "intr_multirange" already exists
3081 DETAIL: Failed while creating a multirange type for type "intr".
3082 HINT: You can manually specify a multirange type name using the "multirange_type_name" attribute.
3083 drop type intr_multirange;
3085 -- Test multiple multirange types over the same subtype and manual naming of
3086 -- the multirange type.
3089 create type textrange1 as range(subtype=text, multirange_type_name=int, collation="C");
3090 ERROR: type "int4" already exists
3092 create type textrange1 as range(subtype=text, multirange_type_name=multirange_of_text, collation="C");
3093 -- should pass, because existing _textrange1 is automatically renamed
3094 create type textrange2 as range(subtype=text, multirange_type_name=_textrange1, collation="C");
3095 select multirange_of_text(textrange2('a','Z')); -- should fail
3096 ERROR: function multirange_of_text(textrange2) does not exist
3097 LINE 1: select multirange_of_text(textrange2('a','Z'));
3099 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
3100 select multirange_of_text(textrange1('a','Z')) @> 'b'::text;
3101 ERROR: range lower bound must be less than or equal to range upper bound
3102 select unnest(multirange_of_text(textrange1('a','b'), textrange1('d','e')));
3109 select _textrange1(textrange2('a','z')) @> 'b'::text;
3115 drop type textrange1;
3116 drop type textrange2;
3118 -- Multiranges don't have their own ownership or permissions.
3120 create type textrange1 as range(subtype=text, multirange_type_name=multitextrange1, collation="C");
3121 create role regress_multirange_owner;
3122 alter type multitextrange1 owner to regress_multirange_owner; -- fail
3123 ERROR: cannot alter multirange type multitextrange1
3124 HINT: You can alter type textrange1, which will alter the multirange type as well.
3125 alter type textrange1 owner to regress_multirange_owner;
3126 set role regress_multirange_owner;
3127 revoke usage on type multitextrange1 from public; -- fail
3128 ERROR: cannot set privileges of multirange types
3129 HINT: Set the privileges of the range type instead.
3130 revoke usage on type textrange1 from public;
3133 Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description
3134 --------+-----------------+-----------------+------+----------+--------------------------+-----------------------------------------------------+-------------
3135 public | multitextrange1 | multitextrange1 | var | | regress_multirange_owner | |
3136 public | textrange1 | textrange1 | var | | regress_multirange_owner | regress_multirange_owner=U/regress_multirange_owner |
3139 create temp table test1(f1 multitextrange1[]);
3140 revoke usage on type textrange1 from regress_multirange_owner;
3141 create temp table test2(f1 multitextrange1[]); -- fail
3142 ERROR: permission denied for type multitextrange1
3144 drop type textrange1;
3146 drop role regress_multirange_owner;
3148 -- Test polymorphic type system
3150 create function anyarray_anymultirange_func(a anyarray, r anymultirange)
3151 returns anyelement as 'select $1[1] + lower($2);' language sql;
3152 select anyarray_anymultirange_func(ARRAY[1,2], int4multirange(int4range(10,20)));
3153 anyarray_anymultirange_func
3154 -----------------------------
3159 select anyarray_anymultirange_func(ARRAY[1,2], nummultirange(numrange(10,20)));
3160 ERROR: function anyarray_anymultirange_func(integer[], nummultirange) does not exist
3161 LINE 1: select anyarray_anymultirange_func(ARRAY[1,2], nummultirange...
3163 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
3164 drop function anyarray_anymultirange_func(anyarray, anymultirange);
3166 create function bogus_func(anyelement)
3167 returns anymultirange as 'select int4multirange(int4range(1,10))' language sql;
3168 ERROR: cannot determine result data type
3169 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.
3171 create function bogus_func(int)
3172 returns anymultirange as 'select int4multirange(int4range(1,10))' language sql;
3173 ERROR: cannot determine result data type
3174 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.
3175 create function range_add_bounds(anymultirange)
3176 returns anyelement as 'select lower($1) + upper($1)' language sql;
3177 select range_add_bounds(int4multirange(int4range(1, 17)));
3183 select range_add_bounds(nummultirange(numrange(1.0001, 123.123)));
3189 create function multirangetypes_sql(q anymultirange, b anyarray, out c anyelement)
3190 as $$ select upper($1) + $2[1] $$
3192 select multirangetypes_sql(int4multirange(int4range(1,10)), ARRAY[2,20]);
3194 ---------------------
3198 select multirangetypes_sql(nummultirange(numrange(1,10)), ARRAY[2,20]); -- match failure
3199 ERROR: function multirangetypes_sql(nummultirange, integer[]) does not exist
3200 LINE 1: select multirangetypes_sql(nummultirange(numrange(1,10)), AR...
3202 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
3203 create function anycompatiblearray_anycompatiblemultirange_func(a anycompatiblearray, mr anycompatiblemultirange)
3204 returns anycompatible as 'select $1[1] + lower($2);' language sql;
3205 select anycompatiblearray_anycompatiblemultirange_func(ARRAY[1,2], multirange(int4range(10,20)));
3206 anycompatiblearray_anycompatiblemultirange_func
3207 -------------------------------------------------
3211 select anycompatiblearray_anycompatiblemultirange_func(ARRAY[1,2], multirange(numrange(10,20)));
3212 anycompatiblearray_anycompatiblemultirange_func
3213 -------------------------------------------------
3218 select anycompatiblearray_anycompatiblemultirange_func(ARRAY[1.1,2], multirange(int4range(10,20)));
3219 ERROR: function anycompatiblearray_anycompatiblemultirange_func(numeric[], int4multirange) does not exist
3220 LINE 1: select anycompatiblearray_anycompatiblemultirange_func(ARRAY...
3222 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
3223 drop function anycompatiblearray_anycompatiblemultirange_func(anycompatiblearray, anycompatiblemultirange);
3224 create function anycompatiblerange_anycompatiblemultirange_func(r anycompatiblerange, mr anycompatiblemultirange)
3225 returns anycompatible as 'select lower($1) + lower($2);' language sql;
3226 select anycompatiblerange_anycompatiblemultirange_func(int4range(1,2), multirange(int4range(10,20)));
3227 anycompatiblerange_anycompatiblemultirange_func
3228 -------------------------------------------------
3233 select anycompatiblerange_anycompatiblemultirange_func(numrange(1,2), multirange(int4range(10,20)));
3234 ERROR: function anycompatiblerange_anycompatiblemultirange_func(numrange, int4multirange) does not exist
3235 LINE 1: select anycompatiblerange_anycompatiblemultirange_func(numra...
3237 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
3238 drop function anycompatiblerange_anycompatiblemultirange_func(anycompatiblerange, anycompatiblemultirange);
3240 create function bogus_func(anycompatible)
3241 returns anycompatiblerange as 'select int4range(1,10)' language sql;
3242 ERROR: cannot determine result data type
3243 DETAIL: A result of type anycompatiblerange requires at least one input of type anycompatiblerange or anycompatiblemultirange.
3245 -- Arrays of multiranges
3247 select ARRAY[nummultirange(numrange(1.1, 1.2)), nummultirange(numrange(12.3, 155.5))];
3249 ----------------------------------
3250 {"{[1.1,1.2)}","{[12.3,155.5)}"}
3253 create table i8mr_array (f1 int, f2 int8multirange[]);
3254 insert into i8mr_array values (42, array[int8multirange(int8range(1,10)), int8multirange(int8range(2,20))]);
3255 select * from i8mr_array;
3257 ----+-------------------------
3258 42 | {"{[1,10)}","{[2,20)}"}
3261 drop table i8mr_array;
3263 -- Multiranges of arrays
3265 select arraymultirange(arrayrange(ARRAY[1,2], ARRAY[2,1]));
3267 ---------------------
3271 select arraymultirange(arrayrange(ARRAY[2,1], ARRAY[1,2])); -- fail
3272 ERROR: range lower bound must be less than or equal to range upper bound
3273 select array[1,1] <@ arraymultirange(arrayrange(array[1,2], array[2,1]));
3279 select array[1,3] <@ arraymultirange(arrayrange(array[1,2], array[2,1]));
3286 -- Ranges of composites
3288 create type two_ints as (a int, b int);
3289 create type two_ints_range as range (subtype = two_ints);
3290 -- with debug_parallel_query on, this exercises tqueue.c's range remapping
3291 select *, row_to_json(upper(t)) as u from
3292 (values (two_ints_multirange(two_ints_range(row(1,2), row(3,4)))),
3293 (two_ints_multirange(two_ints_range(row(5,6), row(7,8))))) v(t);
3295 ---------------------+---------------
3296 {["(1,2)","(3,4)")} | {"a":3,"b":4}
3297 {["(5,6)","(7,8)")} | {"a":7,"b":8}
3300 drop type two_ints cascade;
3301 NOTICE: drop cascades to type two_ints_range
3303 -- Check behavior when subtype lacks a hash function
3305 set enable_sort = off; -- try to make it pick a hash setop implementation
3306 select '{(01,10)}'::varbitmultirange except select '{(10,11)}'::varbitmultirange;
3314 -- OUT/INOUT/TABLE functions
3316 -- infer anymultirange from anymultirange
3317 create function mr_outparam_succeed(i anymultirange, out r anymultirange, out t text)
3318 as $$ select $1, 'foo'::text $$ language sql;
3319 select * from mr_outparam_succeed(int4multirange(int4range(1,2)));
3325 -- infer anyarray from anymultirange
3326 create function mr_outparam_succeed2(i anymultirange, out r anyarray, out t text)
3327 as $$ select ARRAY[upper($1)], 'foo'::text $$ language sql;
3328 select * from mr_outparam_succeed2(int4multirange(int4range(1,2)));
3334 -- infer anyrange from anymultirange
3335 create function mr_outparam_succeed3(i anymultirange, out r anyrange, out t text)
3336 as $$ select range_merge($1), 'foo'::text $$ language sql;
3337 select * from mr_outparam_succeed3(int4multirange(int4range(1,2)));
3343 -- infer anymultirange from anyrange
3344 create function mr_outparam_succeed4(i anyrange, out r anymultirange, out t text)
3345 as $$ select multirange($1), 'foo'::text $$ language sql;
3346 select * from mr_outparam_succeed4(int4range(1,2));
3352 -- infer anyelement from anymultirange
3353 create function mr_inoutparam_succeed(out i anyelement, inout r anymultirange)
3354 as $$ select upper($1), $1 $$ language sql;
3355 select * from mr_inoutparam_succeed(int4multirange(int4range(1,2)));
3361 -- infer anyelement+anymultirange from anyelement+anymultirange
3362 create function mr_table_succeed(i anyelement, r anymultirange) returns table(i anyelement, r anymultirange)
3363 as $$ select $1, $2 $$ language sql;
3364 select * from mr_table_succeed(123, int4multirange(int4range(1,11)));
3370 -- use anymultirange in plpgsql
3371 create function mr_polymorphic(i anyrange) returns anymultirange
3372 as $$ begin return multirange($1); end; $$ language plpgsql;
3373 select mr_polymorphic(int4range(1, 4));
3380 create function mr_outparam_fail(i anyelement, out r anymultirange, out t text)
3381 as $$ select '[1,10]', 'foo' $$ language sql;
3382 ERROR: cannot determine result data type
3383 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.
3385 create function mr_inoutparam_fail(inout i anyelement, out r anymultirange)
3386 as $$ select $1, '[1,10]' $$ language sql;
3387 ERROR: cannot determine result data type
3388 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.
3390 create function mr_table_fail(i anyelement) returns table(i anyelement, r anymultirange)
3391 as $$ select $1, '[1,10]' $$ language sql;
3392 ERROR: cannot determine result data type
3393 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.