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 '{(,z)}'::textmultirange;
106 select '{(a,)}'::textmultirange;
112 select '{[,z]}'::textmultirange;
118 select '{[a,]}'::textmultirange;
124 select '{(,)}'::textmultirange;
130 select '{[ , ]}'::textmultirange;
136 select '{["",""]}'::textmultirange;
142 select '{[",",","]}'::textmultirange;
148 select '{["\\","\\"]}'::textmultirange;
154 select '{["""","\""]}'::textmultirange;
160 select '{(\\,a)}'::textmultirange;
166 select '{((,z)}'::textmultirange;
172 select '{([,z)}'::textmultirange;
178 select '{(!,()}'::textmultirange;
184 select '{(!,[)}'::textmultirange;
190 select '{[a,a]}'::textmultirange;
196 select '{[a,a],[a,b]}'::textmultirange;
202 select '{[a,b), [b,e]}'::textmultirange;
208 select '{[a,d), [b,f]}'::textmultirange;
214 select '{[a,a],[b,b]}'::textmultirange;
220 -- without canonicalization, we can't join these:
221 select '{[a,a], [b,b]}'::textmultirange;
227 -- with canonicalization, we can join these:
228 select '{[1,2], [3,4]}'::int4multirange;
234 select '{[a,a], [b,b], [c,c]}'::textmultirange;
236 ---------------------
240 select '{[a,d], [b,e]}'::textmultirange;
246 select '{[a,d), [d,e)}'::textmultirange;
252 -- these are allowed but normalize to empty:
253 select '{[a,a)}'::textmultirange;
259 select '{(a,a]}'::textmultirange;
265 select '{(a,a)}'::textmultirange;
272 -- test the constructor
274 select textmultirange();
280 select textmultirange(textrange('a', 'c'));
286 select textmultirange(textrange('a', 'c'), textrange('f', 'g'));
292 select textmultirange(textrange('a', 'c'), textrange('b', 'd'));
299 -- test casts, both a built-in range type and a user-defined one:
301 select 'empty'::int4range::int4multirange;
307 select int4range(1, 3)::int4multirange;
313 select int4range(1, null)::int4multirange;
319 select int4range(null, null)::int4multirange;
325 select 'empty'::textrange::textmultirange;
331 select textrange('a', 'c')::textmultirange;
337 select textrange('a', null)::textmultirange;
343 select textrange(null, null)::textmultirange;
350 -- test unnest(multirange) function
352 select unnest(int4multirange(int4range('5', '6'), int4range('1', '2')));
359 select unnest(textmultirange(textrange('a', 'b'), textrange('d', 'e')));
367 -- create some test data and test the operators
369 CREATE TABLE nummultirange_test (nmr NUMMULTIRANGE);
370 CREATE INDEX nummultirange_test_btree ON nummultirange_test(nmr);
371 INSERT INTO nummultirange_test VALUES('{}');
372 INSERT INTO nummultirange_test VALUES('{[,)}');
373 INSERT INTO nummultirange_test VALUES('{[3,]}');
374 INSERT INTO nummultirange_test VALUES('{[,), [3,]}');
375 INSERT INTO nummultirange_test VALUES('{[, 5)}');
376 INSERT INTO nummultirange_test VALUES(nummultirange());
377 INSERT INTO nummultirange_test VALUES(nummultirange(variadic '{}'::numrange[]));
378 INSERT INTO nummultirange_test VALUES(nummultirange(numrange(1.1, 2.2)));
379 INSERT INTO nummultirange_test VALUES('{empty}');
380 INSERT INTO nummultirange_test VALUES(nummultirange(numrange(1.7, 1.7, '[]'), numrange(1.7, 1.9)));
381 INSERT INTO nummultirange_test VALUES(nummultirange(numrange(1.7, 1.7, '[]'), numrange(1.9, 2.1)));
382 SELECT nmr, isempty(nmr), lower(nmr), upper(nmr) FROM nummultirange_test ORDER BY nmr;
383 nmr | isempty | lower | upper
384 -----------------------+---------+-------+-------
392 {[1.1,2.2)} | f | 1.1 | 2.2
393 {[1.7,1.7],[1.9,2.1)} | f | 1.7 | 2.1
394 {[1.7,1.9)} | f | 1.7 | 1.9
398 SELECT nmr, lower_inc(nmr), lower_inf(nmr), upper_inc(nmr), upper_inf(nmr) FROM nummultirange_test ORDER BY nmr;
399 nmr | lower_inc | lower_inf | upper_inc | upper_inf
400 -----------------------+-----------+-----------+-----------+-----------
405 {(,5)} | f | t | f | f
406 {(,)} | f | t | f | t
407 {(,)} | f | t | f | t
408 {[1.1,2.2)} | t | f | f | f
409 {[1.7,1.7],[1.9,2.1)} | t | f | f | f
410 {[1.7,1.9)} | t | f | f | f
411 {[3,)} | t | f | f | t
414 SELECT * FROM nummultirange_test WHERE nmr = '{}';
423 SELECT * FROM nummultirange_test WHERE nmr = '{(,5)}';
429 SELECT * FROM nummultirange_test WHERE nmr = '{[3,)}';
435 SELECT * FROM nummultirange_test WHERE nmr = '{[1.7,1.7]}';
440 SELECT * FROM nummultirange_test WHERE nmr = '{[1.7,1.7],[1.9,2.1)}';
442 -----------------------
443 {[1.7,1.7],[1.9,2.1)}
446 SELECT * FROM nummultirange_test WHERE nmr < '{}';
451 SELECT * FROM nummultirange_test WHERE nmr < '{[-1000.0, -1000.0]}';
463 SELECT * FROM nummultirange_test WHERE nmr < '{[0.0, 1.0]}';
475 SELECT * FROM nummultirange_test WHERE nmr < '{[1000.0, 1001.0]}';
477 -----------------------
488 {[1.7,1.7],[1.9,2.1)}
491 SELECT * FROM nummultirange_test WHERE nmr <= '{}';
500 SELECT * FROM nummultirange_test WHERE nmr <= '{[3,)}';
502 -----------------------
513 {[1.7,1.7],[1.9,2.1)}
516 SELECT * FROM nummultirange_test WHERE nmr >= '{}';
518 -----------------------
529 {[1.7,1.7],[1.9,2.1)}
532 SELECT * FROM nummultirange_test WHERE nmr >= '{[3,)}';
538 SELECT * FROM nummultirange_test WHERE nmr > '{}';
540 -----------------------
547 {[1.7,1.7],[1.9,2.1)}
550 SELECT * FROM nummultirange_test WHERE nmr > '{[-1000.0, -1000.0]}';
552 -----------------------
556 {[1.7,1.7],[1.9,2.1)}
559 SELECT * FROM nummultirange_test WHERE nmr > '{[0.0, 1.0]}';
561 -----------------------
565 {[1.7,1.7],[1.9,2.1)}
568 SELECT * FROM nummultirange_test WHERE nmr > '{[1000.0, 1001.0]}';
573 SELECT * FROM nummultirange_test WHERE nmr <> '{}';
575 -----------------------
582 {[1.7,1.7],[1.9,2.1)}
585 SELECT * FROM nummultirange_test WHERE nmr <> '{(,5)}';
587 -----------------------
597 {[1.7,1.7],[1.9,2.1)}
600 select nummultirange(numrange(2.0, 1.0));
601 ERROR: range lower bound must be less than or equal to range upper bound
602 select nummultirange(numrange(5.0, 6.0), numrange(1.0, 2.0));
604 -----------------------
605 {[1.0,2.0),[5.0,6.0)}
608 analyze nummultirange_test;
610 SELECT * FROM nummultirange_test WHERE range_overlaps_multirange(numrange(4.0, 4.2), nmr);
619 SELECT * FROM nummultirange_test WHERE numrange(4.0, 4.2) && nmr;
628 SELECT * FROM nummultirange_test WHERE multirange_overlaps_range(nmr, numrange(4.0, 4.2));
637 SELECT * FROM nummultirange_test WHERE nmr && numrange(4.0, 4.2);
646 SELECT * FROM nummultirange_test WHERE multirange_overlaps_multirange(nmr, nummultirange(numrange(4.0, 4.2), numrange(6.0, 7.0)));
655 SELECT * FROM nummultirange_test WHERE nmr && nummultirange(numrange(4.0, 4.2), numrange(6.0, 7.0));
664 SELECT * FROM nummultirange_test WHERE nmr && nummultirange(numrange(6.0, 7.0));
672 SELECT * FROM nummultirange_test WHERE nmr && nummultirange(numrange(6.0, 7.0), numrange(8.0, 9.0));
681 SELECT * FROM nummultirange_test WHERE multirange_contains_elem(nmr, 4.0);
690 SELECT * FROM nummultirange_test WHERE nmr @> 4.0;
699 SELECT * FROM nummultirange_test WHERE multirange_contains_range(nmr, numrange(4.0, 4.2));
708 SELECT * FROM nummultirange_test WHERE nmr @> numrange(4.0, 4.2);
717 SELECT * FROM nummultirange_test WHERE multirange_contains_multirange(nmr, '{[4.0,4.2), [6.0, 8.0)}');
725 SELECT * FROM nummultirange_test WHERE nmr @> '{[4.0,4.2), [6.0, 8.0)}'::nummultirange;
733 -- x is contained by mr
734 SELECT * FROM nummultirange_test WHERE elem_contained_by_multirange(4.0, nmr);
743 SELECT * FROM nummultirange_test WHERE 4.0 <@ nmr;
752 SELECT * FROM nummultirange_test WHERE range_contained_by_multirange(numrange(4.0, 4.2), nmr);
761 SELECT * FROM nummultirange_test WHERE numrange(4.0, 4.2) <@ nmr;
770 SELECT * FROM nummultirange_test WHERE multirange_contained_by_multirange('{[4.0,4.2), [6.0, 8.0)}', nmr);
778 SELECT * FROM nummultirange_test WHERE '{[4.0,4.2), [6.0, 8.0)}'::nummultirange <@ nmr;
787 SELECT 'empty'::numrange && nummultirange();
793 SELECT 'empty'::numrange && nummultirange(numrange(1,2));
799 SELECT nummultirange() && 'empty'::numrange;
805 SELECT nummultirange(numrange(1,2)) && 'empty'::numrange;
811 SELECT nummultirange() && nummultirange();
817 SELECT nummultirange() && nummultirange(numrange(1,2));
823 SELECT nummultirange(numrange(1,2)) && nummultirange();
829 SELECT nummultirange(numrange(3,4)) && nummultirange(numrange(1,2), numrange(7,8));
835 SELECT nummultirange(numrange(1,2), numrange(7,8)) && nummultirange(numrange(3,4));
841 SELECT nummultirange(numrange(3,4)) && nummultirange(numrange(1,2), numrange(3.5,8));
847 SELECT nummultirange(numrange(1,2), numrange(3.5,8)) && numrange(3,4);
853 SELECT nummultirange(numrange(1,2), numrange(3.5,8)) && nummultirange(numrange(3,4));
859 select '{(10,20),(30,40),(50,60)}'::nummultirange && '(42,92)'::numrange;
866 SELECT nummultirange() @> nummultirange();
872 SELECT nummultirange() @> 'empty'::numrange;
878 SELECT nummultirange(numrange(null,null)) @> numrange(1,2);
884 SELECT nummultirange(numrange(null,null)) @> numrange(null,2);
890 SELECT nummultirange(numrange(null,null)) @> numrange(2,null);
896 SELECT nummultirange(numrange(null,5)) @> numrange(null,3);
902 SELECT nummultirange(numrange(null,5)) @> numrange(null,8);
908 SELECT nummultirange(numrange(5,null)) @> numrange(8,null);
914 SELECT nummultirange(numrange(5,null)) @> numrange(3,null);
920 SELECT nummultirange(numrange(1,5)) @> numrange(8,9);
926 SELECT nummultirange(numrange(1,5)) @> numrange(3,9);
932 SELECT nummultirange(numrange(1,5)) @> numrange(1,4);
938 SELECT nummultirange(numrange(1,5)) @> numrange(1,5);
944 SELECT nummultirange(numrange(-4,-2), numrange(1,5)) @> numrange(1,5);
950 SELECT nummultirange(numrange(1,5), numrange(8,9)) @> numrange(1,5);
956 SELECT nummultirange(numrange(1,5), numrange(8,9)) @> numrange(6,7);
962 SELECT nummultirange(numrange(1,5), numrange(6,9)) @> numrange(6,7);
968 SELECT '{[1,5)}'::nummultirange @> '{[1,5)}';
974 SELECT '{[-4,-2), [1,5)}'::nummultirange @> '{[1,5)}';
980 SELECT '{[1,5), [8,9)}'::nummultirange @> '{[1,5)}';
986 SELECT '{[1,5), [8,9)}'::nummultirange @> '{[6,7)}';
992 SELECT '{[1,5), [6,9)}'::nummultirange @> '{[6,7)}';
998 select '{(10,20),(30,40),(50,60)}'::nummultirange @> '(52,56)'::numrange;
1004 SELECT numrange(null,null) @> nummultirange(numrange(1,2));
1010 SELECT numrange(null,null) @> nummultirange(numrange(null,2));
1016 SELECT numrange(null,null) @> nummultirange(numrange(2,null));
1022 SELECT numrange(null,5) @> nummultirange(numrange(null,3));
1028 SELECT numrange(null,5) @> nummultirange(numrange(null,8));
1034 SELECT numrange(5,null) @> nummultirange(numrange(8,null));
1040 SELECT numrange(5,null) @> nummultirange(numrange(3,null));
1046 SELECT numrange(1,5) @> nummultirange(numrange(8,9));
1052 SELECT numrange(1,5) @> nummultirange(numrange(3,9));
1058 SELECT numrange(1,5) @> nummultirange(numrange(1,4));
1064 SELECT numrange(1,5) @> nummultirange(numrange(1,5));
1070 SELECT numrange(1,9) @> nummultirange(numrange(-4,-2), numrange(1,5));
1076 SELECT numrange(1,9) @> nummultirange(numrange(1,5), numrange(8,9));
1082 SELECT numrange(1,9) @> nummultirange(numrange(1,5), numrange(6,9));
1088 SELECT numrange(1,9) @> nummultirange(numrange(1,5), numrange(6,10));
1094 SELECT '{[1,9)}' @> '{[1,5)}'::nummultirange;
1100 SELECT '{[1,9)}' @> '{[-4,-2), [1,5)}'::nummultirange;
1106 SELECT '{[1,9)}' @> '{[1,5), [8,9)}'::nummultirange;
1112 SELECT '{[1,9)}' @> '{[1,5), [6,9)}'::nummultirange;
1118 SELECT '{[1,9)}' @> '{[1,5), [6,10)}'::nummultirange;
1125 SELECT nummultirange() <@ nummultirange();
1131 SELECT 'empty'::numrange <@ nummultirange();
1137 SELECT numrange(1,2) <@ nummultirange(numrange(null,null));
1143 SELECT numrange(null,2) <@ nummultirange(numrange(null,null));
1149 SELECT numrange(2,null) <@ nummultirange(numrange(null,null));
1155 SELECT numrange(null,3) <@ nummultirange(numrange(null,5));
1161 SELECT numrange(null,8) <@ nummultirange(numrange(null,5));
1167 SELECT numrange(8,null) <@ nummultirange(numrange(5,null));
1173 SELECT numrange(3,null) <@ nummultirange(numrange(5,null));
1179 SELECT numrange(8,9) <@ nummultirange(numrange(1,5));
1185 SELECT numrange(3,9) <@ nummultirange(numrange(1,5));
1191 SELECT numrange(1,4) <@ nummultirange(numrange(1,5));
1197 SELECT numrange(1,5) <@ nummultirange(numrange(1,5));
1203 SELECT numrange(1,5) <@ nummultirange(numrange(-4,-2), numrange(1,5));
1209 SELECT numrange(1,5) <@ nummultirange(numrange(1,5), numrange(8,9));
1215 SELECT numrange(6,7) <@ nummultirange(numrange(1,5), numrange(8,9));
1221 SELECT numrange(6,7) <@ nummultirange(numrange(1,5), numrange(6,9));
1227 SELECT '{[1,5)}' <@ '{[1,5)}'::nummultirange;
1233 SELECT '{[1,5)}' <@ '{[-4,-2), [1,5)}'::nummultirange;
1239 SELECT '{[1,5)}' <@ '{[1,5), [8,9)}'::nummultirange;
1245 SELECT '{[6,7)}' <@ '{[1,5), [8,9)}'::nummultirange;
1251 SELECT '{[6,7)}' <@ '{[1,5), [6,9)}'::nummultirange;
1257 SELECT nummultirange(numrange(1,2)) <@ numrange(null,null);
1263 SELECT nummultirange(numrange(null,2)) <@ numrange(null,null);
1269 SELECT nummultirange(numrange(2,null)) <@ numrange(null,null);
1275 SELECT nummultirange(numrange(null,3)) <@ numrange(null,5);
1281 SELECT nummultirange(numrange(null,8)) <@ numrange(null,5);
1287 SELECT nummultirange(numrange(8,null)) <@ numrange(5,null);
1293 SELECT nummultirange(numrange(3,null)) <@ numrange(5,null);
1299 SELECT nummultirange(numrange(8,9)) <@ numrange(1,5);
1305 SELECT nummultirange(numrange(3,9)) <@ numrange(1,5);
1311 SELECT nummultirange(numrange(1,4)) <@ numrange(1,5);
1317 SELECT nummultirange(numrange(1,5)) <@ numrange(1,5);
1323 SELECT nummultirange(numrange(-4,-2), numrange(1,5)) <@ numrange(1,9);
1329 SELECT nummultirange(numrange(1,5), numrange(8,9)) <@ numrange(1,9);
1335 SELECT nummultirange(numrange(1,5), numrange(6,9)) <@ numrange(1,9);
1341 SELECT nummultirange(numrange(1,5), numrange(6,10)) <@ numrange(1,9);
1347 SELECT '{[1,5)}'::nummultirange <@ '{[1,9)}';
1353 SELECT '{[-4,-2), [1,5)}'::nummultirange <@ '{[1,9)}';
1359 SELECT '{[1,5), [8,9)}'::nummultirange <@ '{[1,9)}';
1365 SELECT '{[1,5), [6,9)}'::nummultirange <@ '{[1,9)}';
1371 SELECT '{[1,5), [6,10)}'::nummultirange <@ '{[1,9)}';
1378 SELECT 'empty'::numrange &< nummultirange();
1384 SELECT 'empty'::numrange &< nummultirange(numrange(1,2));
1390 SELECT nummultirange() &< 'empty'::numrange;
1396 SELECT nummultirange(numrange(1,2)) &< 'empty'::numrange;
1402 SELECT nummultirange() &< nummultirange();
1408 SELECT nummultirange(numrange(1,2)) &< nummultirange();
1414 SELECT nummultirange() &< nummultirange(numrange(1,2));
1420 SELECT numrange(6,7) &< nummultirange(numrange(3,4));
1426 SELECT numrange(1,2) &< nummultirange(numrange(3,4));
1432 SELECT numrange(1,4) &< nummultirange(numrange(3,4));
1438 SELECT numrange(1,6) &< nummultirange(numrange(3,4));
1444 SELECT numrange(3.5,6) &< nummultirange(numrange(3,4));
1450 SELECT nummultirange(numrange(6,7)) &< numrange(3,4);
1456 SELECT nummultirange(numrange(1,2)) &< numrange(3,4);
1462 SELECT nummultirange(numrange(1,4)) &< numrange(3,4);
1468 SELECT nummultirange(numrange(1,6)) &< numrange(3,4);
1474 SELECT nummultirange(numrange(3.5,6)) &< numrange(3,4);
1480 SELECT nummultirange(numrange(6,7)) &< nummultirange(numrange(3,4));
1486 SELECT nummultirange(numrange(1,2)) &< nummultirange(numrange(3,4));
1492 SELECT nummultirange(numrange(1,4)) &< nummultirange(numrange(3,4));
1498 SELECT nummultirange(numrange(1,6)) &< nummultirange(numrange(3,4));
1504 SELECT nummultirange(numrange(3.5,6)) &< nummultirange(numrange(3,4));
1511 SELECT nummultirange() &> 'empty'::numrange;
1517 SELECT nummultirange(numrange(1,2)) &> 'empty'::numrange;
1523 SELECT 'empty'::numrange &> nummultirange();
1529 SELECT 'empty'::numrange &> nummultirange(numrange(1,2));
1535 SELECT nummultirange() &> nummultirange();
1541 SELECT nummultirange() &> nummultirange(numrange(1,2));
1547 SELECT nummultirange(numrange(1,2)) &> nummultirange();
1553 SELECT nummultirange(numrange(3,4)) &> numrange(6,7);
1559 SELECT nummultirange(numrange(3,4)) &> numrange(1,2);
1565 SELECT nummultirange(numrange(3,4)) &> numrange(1,4);
1571 SELECT nummultirange(numrange(3,4)) &> numrange(1,6);
1577 SELECT nummultirange(numrange(3,4)) &> numrange(3.5,6);
1583 SELECT numrange(3,4) &> nummultirange(numrange(6,7));
1589 SELECT numrange(3,4) &> nummultirange(numrange(1,2));
1595 SELECT numrange(3,4) &> nummultirange(numrange(1,4));
1601 SELECT numrange(3,4) &> nummultirange(numrange(1,6));
1607 SELECT numrange(3,4) &> nummultirange(numrange(3.5,6));
1613 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(6,7));
1619 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(1,2));
1625 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(1,4));
1631 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(1,6));
1637 SELECT nummultirange(numrange(3,4)) &> nummultirange(numrange(3.5,6));
1644 SELECT 'empty'::numrange -|- nummultirange();
1650 SELECT 'empty'::numrange -|- nummultirange(numrange(1,2));
1656 SELECT nummultirange() -|- 'empty'::numrange;
1662 SELECT nummultirange(numrange(1,2)) -|- 'empty'::numrange;
1668 SELECT nummultirange() -|- nummultirange();
1674 SELECT nummultirange(numrange(1,2)) -|- nummultirange();
1680 SELECT nummultirange() -|- nummultirange(numrange(1,2));
1686 SELECT numrange(1,2) -|- nummultirange(numrange(2,4));
1692 SELECT numrange(1,2) -|- nummultirange(numrange(3,4));
1698 SELECT nummultirange(numrange(1,2)) -|- numrange(2,4);
1704 SELECT nummultirange(numrange(1,2)) -|- numrange(3,4);
1710 SELECT nummultirange(numrange(1,2)) -|- nummultirange(numrange(2,4));
1716 SELECT nummultirange(numrange(1,2)) -|- nummultirange(numrange(3,4));
1722 SELECT nummultirange(numrange(1,2), numrange(5,6)) -|- nummultirange(numrange(3,4));
1728 SELECT nummultirange(numrange(1,2), numrange(5,6)) -|- nummultirange(numrange(6,7));
1734 SELECT nummultirange(numrange(1,2), numrange(5,6)) -|- nummultirange(numrange(8,9));
1740 SELECT nummultirange(numrange(1,2)) -|- nummultirange(numrange(2,4), numrange(6,7));
1747 select 'empty'::numrange << nummultirange();
1753 select numrange(1,2) << nummultirange();
1759 select numrange(1,2) << nummultirange(numrange(3,4));
1765 select numrange(1,2) << nummultirange(numrange(0,4));
1771 select numrange(1,2) << nummultirange(numrange(0,4), numrange(7,8));
1777 select nummultirange() << 'empty'::numrange;
1783 select nummultirange() << numrange(1,2);
1789 select nummultirange(numrange(3,4)) << numrange(3,6);
1795 select nummultirange(numrange(0,2)) << numrange(3,6);
1801 select nummultirange(numrange(0,2), numrange(7,8)) << numrange(3,6);
1807 select nummultirange(numrange(-4,-2), numrange(0,2)) << numrange(3,6);
1813 select nummultirange() << nummultirange();
1819 select nummultirange() << nummultirange(numrange(1,2));
1825 select nummultirange(numrange(1,2)) << nummultirange();
1831 select nummultirange(numrange(1,2)) << nummultirange(numrange(1,2));
1837 select nummultirange(numrange(1,2)) << nummultirange(numrange(3,4));
1843 select nummultirange(numrange(1,2)) << nummultirange(numrange(3,4), numrange(7,8));
1849 select nummultirange(numrange(1,2), numrange(4,5)) << nummultirange(numrange(3,4), numrange(7,8));
1856 select nummultirange() >> 'empty'::numrange;
1862 select nummultirange() >> numrange(1,2);
1868 select nummultirange(numrange(3,4)) >> numrange(1,2);
1874 select nummultirange(numrange(0,4)) >> numrange(1,2);
1880 select nummultirange(numrange(0,4), numrange(7,8)) >> numrange(1,2);
1886 select 'empty'::numrange >> nummultirange();
1892 select numrange(1,2) >> nummultirange();
1898 select numrange(3,6) >> nummultirange(numrange(3,4));
1904 select numrange(3,6) >> nummultirange(numrange(0,2));
1910 select numrange(3,6) >> nummultirange(numrange(0,2), numrange(7,8));
1916 select numrange(3,6) >> nummultirange(numrange(-4,-2), numrange(0,2));
1922 select nummultirange() >> nummultirange();
1928 select nummultirange(numrange(1,2)) >> nummultirange();
1934 select nummultirange() >> nummultirange(numrange(1,2));
1940 select nummultirange(numrange(1,2)) >> nummultirange(numrange(1,2));
1946 select nummultirange(numrange(3,4)) >> nummultirange(numrange(1,2));
1952 select nummultirange(numrange(3,4), numrange(7,8)) >> nummultirange(numrange(1,2));
1958 select nummultirange(numrange(3,4), numrange(7,8)) >> nummultirange(numrange(1,2), numrange(4,5));
1965 SELECT nummultirange() + nummultirange();
1971 SELECT nummultirange() + nummultirange(numrange(1,2));
1977 SELECT nummultirange(numrange(1,2)) + nummultirange();
1983 SELECT nummultirange(numrange(1,2)) + nummultirange(numrange(1,2));
1989 SELECT nummultirange(numrange(1,2)) + nummultirange(numrange(2,4));
1995 SELECT nummultirange(numrange(1,2)) + nummultirange(numrange(3,4));
2001 SELECT nummultirange(numrange(1,2), numrange(4,5)) + nummultirange(numrange(2,4));
2007 SELECT nummultirange(numrange(1,2), numrange(4,5)) + nummultirange(numrange(3,4));
2013 SELECT nummultirange(numrange(1,2), numrange(4,5)) + nummultirange(numrange(0,9));
2020 SELECT range_merge(nummultirange());
2026 SELECT range_merge(nummultirange(numrange(1,2)));
2032 SELECT range_merge(nummultirange(numrange(1,2), numrange(7,8)));
2039 SELECT nummultirange() - nummultirange();
2045 SELECT nummultirange() - nummultirange(numrange(1,2));
2051 SELECT nummultirange(numrange(1,2)) - nummultirange();
2057 SELECT nummultirange(numrange(1,2), numrange(3,4)) - nummultirange();
2063 SELECT nummultirange(numrange(1,2)) - nummultirange(numrange(1,2));
2069 SELECT nummultirange(numrange(1,2)) - nummultirange(numrange(2,4));
2075 SELECT nummultirange(numrange(1,2)) - nummultirange(numrange(3,4));
2081 SELECT nummultirange(numrange(1,4)) - nummultirange(numrange(1,2));
2087 SELECT nummultirange(numrange(1,4)) - nummultirange(numrange(2,3));
2093 SELECT nummultirange(numrange(1,4)) - nummultirange(numrange(0,8));
2099 SELECT nummultirange(numrange(1,4)) - nummultirange(numrange(0,2));
2105 SELECT nummultirange(numrange(1,8)) - nummultirange(numrange(0,2), numrange(3,4));
2111 SELECT nummultirange(numrange(1,8)) - nummultirange(numrange(2,3), numrange(5,null));
2117 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(-2,0));
2123 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(2,4));
2129 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(3,5));
2135 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(0,9));
2141 SELECT nummultirange(numrange(1,3), numrange(4,5)) - nummultirange(numrange(2,9));
2147 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(8,9));
2153 SELECT nummultirange(numrange(1,2), numrange(4,5)) - nummultirange(numrange(-2,0), numrange(8,9));
2160 SELECT nummultirange() * nummultirange();
2166 SELECT nummultirange() * nummultirange(numrange(1,2));
2172 SELECT nummultirange(numrange(1,2)) * nummultirange();
2178 SELECT '{[1,3)}'::nummultirange * '{[1,5)}'::nummultirange;
2184 SELECT '{[1,3)}'::nummultirange * '{[0,5)}'::nummultirange;
2190 SELECT '{[1,3)}'::nummultirange * '{[0,2)}'::nummultirange;
2196 SELECT '{[1,3)}'::nummultirange * '{[2,5)}'::nummultirange;
2202 SELECT '{[1,4)}'::nummultirange * '{[2,3)}'::nummultirange;
2208 SELECT '{[1,4)}'::nummultirange * '{[0,2), [3,5)}'::nummultirange;
2214 SELECT '{[1,4), [7,10)}'::nummultirange * '{[0,8), [9,12)}'::nummultirange;
2216 ----------------------
2217 {[1,4),[7,8),[9,10)}
2220 SELECT '{[1,4), [7,10)}'::nummultirange * '{[9,12)}'::nummultirange;
2226 SELECT '{[1,4), [7,10)}'::nummultirange * '{[-5,-4), [5,6), [9,12)}'::nummultirange;
2232 SELECT '{[1,4), [7,10)}'::nummultirange * '{[0,2), [3,8), [9,12)}'::nummultirange;
2234 ----------------------------
2235 {[1,2),[3,4),[7,8),[9,10)}
2238 SELECT '{[1,4), [7,10)}'::nummultirange * '{[0,2), [3,8), [9,12)}'::nummultirange;
2240 ----------------------------
2241 {[1,2),[3,4),[7,8),[9,10)}
2245 create table test_multirange_gist(mr int4multirange);
2246 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;
2247 insert into test_multirange_gist select '{}'::int4multirange from generate_series(1,500) g;
2248 insert into test_multirange_gist select int4multirange(int4range(g, g+10000)) from generate_series(1,1000) g;
2249 insert into test_multirange_gist select int4multirange(int4range(NULL, g*10, '(]'), int4range(g*10, g*20, '(]')) from generate_series(1,100) g;
2250 insert into test_multirange_gist select int4multirange(int4range(g*10, g*20, '(]'), int4range(g*20, NULL, '(]')) from generate_series(1,100) g;
2251 create index test_mulrirange_gist_idx on test_multirange_gist using gist (mr);
2252 -- test statistics and selectivity estimation as well
2254 -- We don't check the accuracy of selectivity estimation, but at least check
2256 analyze test_multirange_gist;
2257 -- first, verify non-indexed results
2258 SET enable_seqscan = t;
2259 SET enable_indexscan = f;
2260 SET enable_bitmapscan = f;
2261 select count(*) from test_multirange_gist where mr = '{}'::int4multirange;
2267 select count(*) from test_multirange_gist where mr @> 'empty'::int4range;
2273 select count(*) from test_multirange_gist where mr && 'empty'::int4range;
2279 select count(*) from test_multirange_gist where mr <@ 'empty'::int4range;
2285 select count(*) from test_multirange_gist where mr << 'empty'::int4range;
2291 select count(*) from test_multirange_gist where mr >> 'empty'::int4range;
2297 select count(*) from test_multirange_gist where mr &< 'empty'::int4range;
2303 select count(*) from test_multirange_gist where mr &> 'empty'::int4range;
2309 select count(*) from test_multirange_gist where mr -|- 'empty'::int4range;
2315 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2321 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2327 select count(*) from test_multirange_gist where mr && '{}'::int4multirange;
2333 select count(*) from test_multirange_gist where mr <@ '{}'::int4multirange;
2339 select count(*) from test_multirange_gist where mr << '{}'::int4multirange;
2345 select count(*) from test_multirange_gist where mr >> '{}'::int4multirange;
2351 select count(*) from test_multirange_gist where mr &< '{}'::int4multirange;
2357 select count(*) from test_multirange_gist where mr &> '{}'::int4multirange;
2363 select count(*) from test_multirange_gist where mr -|- '{}'::int4multirange;
2369 select count(*) from test_multirange_gist where mr = int4multirange(int4range(10,20), int4range(30,40), int4range(50,60));
2375 select count(*) from test_multirange_gist where mr @> 10;
2381 select count(*) from test_multirange_gist where mr @> int4range(10,20);
2387 select count(*) from test_multirange_gist where mr && int4range(10,20);
2393 select count(*) from test_multirange_gist where mr <@ int4range(10,50);
2399 select count(*) from test_multirange_gist where mr << int4range(100,500);
2405 select count(*) from test_multirange_gist where mr >> int4range(100,500);
2411 select count(*) from test_multirange_gist where mr &< int4range(100,500);
2417 select count(*) from test_multirange_gist where mr &> int4range(100,500);
2423 select count(*) from test_multirange_gist where mr -|- int4range(100,500);
2429 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2435 select count(*) from test_multirange_gist where mr @> int4multirange(int4range(10,20), int4range(30,40));
2441 select count(*) from test_multirange_gist where mr && '{(10,20),(30,40),(50,60)}'::int4multirange;
2447 select count(*) from test_multirange_gist where mr <@ '{(10,30),(40,60),(70,90)}'::int4multirange;
2453 select count(*) from test_multirange_gist where mr << int4multirange(int4range(100,200), int4range(400,500));
2459 select count(*) from test_multirange_gist where mr >> int4multirange(int4range(100,200), int4range(400,500));
2465 select count(*) from test_multirange_gist where mr &< int4multirange(int4range(100,200), int4range(400,500));
2471 select count(*) from test_multirange_gist where mr &> int4multirange(int4range(100,200), int4range(400,500));
2477 select count(*) from test_multirange_gist where mr -|- int4multirange(int4range(100,200), int4range(400,500));
2483 -- now check same queries using index
2484 SET enable_seqscan = f;
2485 SET enable_indexscan = t;
2486 SET enable_bitmapscan = f;
2487 select count(*) from test_multirange_gist where mr = '{}'::int4multirange;
2493 select count(*) from test_multirange_gist where mr @> 'empty'::int4range;
2499 select count(*) from test_multirange_gist where mr && 'empty'::int4range;
2505 select count(*) from test_multirange_gist where mr <@ 'empty'::int4range;
2511 select count(*) from test_multirange_gist where mr << 'empty'::int4range;
2517 select count(*) from test_multirange_gist where mr >> 'empty'::int4range;
2523 select count(*) from test_multirange_gist where mr &< 'empty'::int4range;
2529 select count(*) from test_multirange_gist where mr &> 'empty'::int4range;
2535 select count(*) from test_multirange_gist where mr -|- 'empty'::int4range;
2541 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2547 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2553 select count(*) from test_multirange_gist where mr && '{}'::int4multirange;
2559 select count(*) from test_multirange_gist where mr <@ '{}'::int4multirange;
2565 select count(*) from test_multirange_gist where mr << '{}'::int4multirange;
2571 select count(*) from test_multirange_gist where mr >> '{}'::int4multirange;
2577 select count(*) from test_multirange_gist where mr &< '{}'::int4multirange;
2583 select count(*) from test_multirange_gist where mr &> '{}'::int4multirange;
2589 select count(*) from test_multirange_gist where mr -|- '{}'::int4multirange;
2595 select count(*) from test_multirange_gist where mr @> 'empty'::int4range;
2601 select count(*) from test_multirange_gist where mr = int4multirange(int4range(10,20), int4range(30,40), int4range(50,60));
2607 select count(*) from test_multirange_gist where mr @> 10;
2613 select count(*) from test_multirange_gist where mr @> int4range(10,20);
2619 select count(*) from test_multirange_gist where mr && int4range(10,20);
2625 select count(*) from test_multirange_gist where mr <@ int4range(10,50);
2631 select count(*) from test_multirange_gist where mr << int4range(100,500);
2637 select count(*) from test_multirange_gist where mr >> int4range(100,500);
2643 select count(*) from test_multirange_gist where mr &< int4range(100,500);
2649 select count(*) from test_multirange_gist where mr &> int4range(100,500);
2655 select count(*) from test_multirange_gist where mr -|- int4range(100,500);
2661 select count(*) from test_multirange_gist where mr @> '{}'::int4multirange;
2667 select count(*) from test_multirange_gist where mr @> int4multirange(int4range(10,20), int4range(30,40));
2673 select count(*) from test_multirange_gist where mr && '{(10,20),(30,40),(50,60)}'::int4multirange;
2679 select count(*) from test_multirange_gist where mr <@ '{(10,30),(40,60),(70,90)}'::int4multirange;
2685 select count(*) from test_multirange_gist where mr << int4multirange(int4range(100,200), int4range(400,500));
2691 select count(*) from test_multirange_gist where mr >> int4multirange(int4range(100,200), int4range(400,500));
2697 select count(*) from test_multirange_gist where mr &< int4multirange(int4range(100,200), int4range(400,500));
2703 select count(*) from test_multirange_gist where mr &> int4multirange(int4range(100,200), int4range(400,500));
2709 select count(*) from test_multirange_gist where mr -|- int4multirange(int4range(100,200), int4range(400,500));
2715 drop table test_multirange_gist;
2717 -- range_agg function
2719 create table reservations ( room_id integer not null, booked_during daterange );
2720 insert into reservations values
2721 -- 1: has a meets and a gap
2722 (1, daterange('2018-07-01', '2018-07-07')),
2723 (1, daterange('2018-07-07', '2018-07-14')),
2724 (1, daterange('2018-07-20', '2018-07-22')),
2725 -- 2: just a single row
2726 (2, daterange('2018-07-01', '2018-07-03')),
2727 -- 3: one null range
2729 -- 4: two null ranges
2732 -- 5: a null range and a non-null range
2734 (5, daterange('2018-07-01', '2018-07-03')),
2736 (6, daterange('2018-07-01', '2018-07-07')),
2737 (6, daterange('2018-07-05', '2018-07-10')),
2738 -- 7: two ranges that meet: no gap or overlap
2739 (7, daterange('2018-07-01', '2018-07-07')),
2740 (7, daterange('2018-07-07', '2018-07-14')),
2741 -- 8: an empty range
2742 (8, 'empty'::daterange)
2744 SELECT room_id, range_agg(booked_during)
2749 ---------+---------------------------------------------------
2750 1 | {[07-01-2018,07-14-2018),[07-20-2018,07-22-2018)}
2751 2 | {[07-01-2018,07-03-2018)}
2754 5 | {[07-01-2018,07-03-2018)}
2755 6 | {[07-01-2018,07-10-2018)}
2756 7 | {[07-01-2018,07-14-2018)}
2760 -- range_agg on a custom range type too
2763 ('[a,c]'::textrange),
2764 ('[b,b]'::textrange),
2765 ('[c,f]'::textrange),
2766 ('[g,h)'::textrange),
2767 ('[h,j)'::textrange)
2774 select range_intersect_agg(nmr) from nummultirange_test;
2776 ---------------------
2780 select range_intersect_agg(nmr) from nummultirange_test where false;
2782 ---------------------
2786 -- test with just one input:
2787 select range_intersect_agg(nmr) from (values ('{[1,2]}'::nummultirange)) t(nmr);
2789 ---------------------
2793 select range_intersect_agg(nmr) from nummultirange_test where nmr @> 4.0;
2795 ---------------------
2799 create table nummultirange_test2(nmr nummultirange);
2800 create index nummultirange_test2_hash_idx on nummultirange_test2 using hash (nmr);
2801 INSERT INTO nummultirange_test2 VALUES('{[, 5)}');
2802 INSERT INTO nummultirange_test2 VALUES(nummultirange(numrange(1.1, 2.2)));
2803 INSERT INTO nummultirange_test2 VALUES(nummultirange(numrange(1.1, 2.2)));
2804 INSERT INTO nummultirange_test2 VALUES(nummultirange(numrange(1.1, 2.2,'()')));
2805 INSERT INTO nummultirange_test2 VALUES('{}');
2806 select * from nummultirange_test2 where nmr = '{}';
2812 select * from nummultirange_test2 where nmr = nummultirange(numrange(1.1, 2.2));
2819 select * from nummultirange_test2 where nmr = nummultirange(numrange(1.1, 2.3));
2824 set enable_nestloop=t;
2825 set enable_hashjoin=f;
2826 set enable_mergejoin=f;
2827 select * from nummultirange_test natural join nummultirange_test2 order by nmr;
2839 set enable_nestloop=f;
2840 set enable_hashjoin=t;
2841 set enable_mergejoin=f;
2842 select * from nummultirange_test natural join nummultirange_test2 order by nmr;
2854 set enable_nestloop=f;
2855 set enable_hashjoin=f;
2856 set enable_mergejoin=t;
2857 select * from nummultirange_test natural join nummultirange_test2 order by nmr;
2869 set enable_nestloop to default;
2870 set enable_hashjoin to default;
2871 set enable_mergejoin to default;
2872 DROP TABLE nummultirange_test2;
2874 -- Test user-defined multirange of floats
2876 select '{[123.001, 5.e9)}'::float8multirange @> 888.882::float8;
2882 create table float8multirange_test(f8mr float8multirange, i int);
2883 insert into float8multirange_test values(float8multirange(float8range(-100.00007, '1.111113e9')), 42);
2884 select * from float8multirange_test;
2886 ---------------------------+----
2887 {[-100.00007,1111113000)} | 42
2890 drop table float8multirange_test;
2892 -- Test multirange types over domains
2894 create domain mydomain as int4;
2895 create type mydomainrange as range(subtype=mydomain);
2896 select '{[4,50)}'::mydomainmultirange @> 7::mydomain;
2902 drop domain mydomain cascade;
2903 NOTICE: drop cascades to type mydomainrange
2905 -- Test domains over multirange types
2907 create domain restrictedmultirange as int4multirange check (upper(value) < 10);
2908 select '{[4,5)}'::restrictedmultirange @> 7;
2914 select '{[4,50)}'::restrictedmultirange @> 7; -- should fail
2915 ERROR: value for domain restrictedmultirange violates check constraint "restrictedmultirange_check"
2916 drop domain restrictedmultirange;
2918 -- Check automatic naming of multiranges
2920 create type intr as range(subtype=int);
2921 select intr_multirange(intr(1,10));
2928 create type intmultirange as (x int, y int);
2929 create type intrange as range(subtype=int); -- should fail
2930 ERROR: type "intmultirange" already exists
2931 DETAIL: Failed while creating a multirange type for type "intrange".
2932 HINT: You can manually specify a multirange type name using the "multirange_type_name" attribute.
2933 drop type intmultirange;
2934 create type intr_multirange as (x int, y int);
2935 create type intr as range(subtype=int); -- should fail
2936 ERROR: type "intr_multirange" already exists
2937 DETAIL: Failed while creating a multirange type for type "intr".
2938 HINT: You can manually specify a multirange type name using the "multirange_type_name" attribute.
2939 drop type intr_multirange;
2941 -- Test multiple multirange types over the same subtype and manual naming of
2942 -- the multirange type.
2945 create type textrange1 as range(subtype=text, multirange_type_name=int, collation="C");
2946 ERROR: type "int4" already exists
2948 create type textrange1 as range(subtype=text, multirange_type_name=multirange_of_text, collation="C");
2949 -- should pass, because existing _textrange1 is automatically renamed
2950 create type textrange2 as range(subtype=text, multirange_type_name=_textrange1, collation="C");
2951 select multirange_of_text(textrange2('a','Z')); -- should fail
2952 ERROR: function multirange_of_text(textrange2) does not exist
2953 LINE 1: select multirange_of_text(textrange2('a','Z'));
2955 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
2956 select multirange_of_text(textrange1('a','Z')) @> 'b'::text;
2957 ERROR: range lower bound must be less than or equal to range upper bound
2958 select unnest(multirange_of_text(textrange1('a','b'), textrange1('d','e')));
2965 select _textrange1(textrange2('a','z')) @> 'b'::text;
2971 drop type textrange1;
2972 drop type textrange2;
2974 -- Test polymorphic type system
2976 create function anyarray_anymultirange_func(a anyarray, r anymultirange)
2977 returns anyelement as 'select $1[1] + lower($2);' language sql;
2978 select anyarray_anymultirange_func(ARRAY[1,2], int4multirange(int4range(10,20)));
2979 anyarray_anymultirange_func
2980 -----------------------------
2985 select anyarray_anymultirange_func(ARRAY[1,2], nummultirange(numrange(10,20)));
2986 ERROR: function anyarray_anymultirange_func(integer[], nummultirange) does not exist
2987 LINE 1: select anyarray_anymultirange_func(ARRAY[1,2], nummultirange...
2989 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
2990 drop function anyarray_anymultirange_func(anyarray, anymultirange);
2992 create function bogus_func(anyelement)
2993 returns anymultirange as 'select int4multirange(int4range(1,10))' language sql;
2994 ERROR: cannot determine result data type
2995 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.
2997 create function bogus_func(int)
2998 returns anymultirange as 'select int4multirange(int4range(1,10))' language sql;
2999 ERROR: cannot determine result data type
3000 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.
3001 create function range_add_bounds(anymultirange)
3002 returns anyelement as 'select lower($1) + upper($1)' language sql;
3003 select range_add_bounds(int4multirange(int4range(1, 17)));
3009 select range_add_bounds(nummultirange(numrange(1.0001, 123.123)));
3015 create function multirangetypes_sql(q anymultirange, b anyarray, out c anyelement)
3016 as $$ select upper($1) + $2[1] $$
3018 select multirangetypes_sql(int4multirange(int4range(1,10)), ARRAY[2,20]);
3020 ---------------------
3024 select multirangetypes_sql(nummultirange(numrange(1,10)), ARRAY[2,20]); -- match failure
3025 ERROR: function multirangetypes_sql(nummultirange, integer[]) does not exist
3026 LINE 1: select multirangetypes_sql(nummultirange(numrange(1,10)), AR...
3028 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
3029 create function anycompatiblearray_anycompatiblemultirange_func(a anycompatiblearray, mr anycompatiblemultirange)
3030 returns anycompatible as 'select $1[1] + lower($2);' language sql;
3031 select anycompatiblearray_anycompatiblemultirange_func(ARRAY[1,2], multirange(int4range(10,20)));
3032 anycompatiblearray_anycompatiblemultirange_func
3033 -------------------------------------------------
3037 select anycompatiblearray_anycompatiblemultirange_func(ARRAY[1,2], multirange(numrange(10,20)));
3038 anycompatiblearray_anycompatiblemultirange_func
3039 -------------------------------------------------
3044 select anycompatiblearray_anycompatiblemultirange_func(ARRAY[1.1,2], multirange(int4range(10,20)));
3045 ERROR: function anycompatiblearray_anycompatiblemultirange_func(numeric[], int4multirange) does not exist
3046 LINE 1: select anycompatiblearray_anycompatiblemultirange_func(ARRAY...
3048 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
3049 drop function anycompatiblearray_anycompatiblemultirange_func(anycompatiblearray, anycompatiblemultirange);
3050 create function anycompatiblerange_anycompatiblemultirange_func(r anycompatiblerange, mr anycompatiblemultirange)
3051 returns anycompatible as 'select lower($1) + lower($2);' language sql;
3052 select anycompatiblerange_anycompatiblemultirange_func(int4range(1,2), multirange(int4range(10,20)));
3053 anycompatiblerange_anycompatiblemultirange_func
3054 -------------------------------------------------
3059 select anycompatiblerange_anycompatiblemultirange_func(numrange(1,2), multirange(int4range(10,20)));
3060 ERROR: function anycompatiblerange_anycompatiblemultirange_func(numrange, int4multirange) does not exist
3061 LINE 1: select anycompatiblerange_anycompatiblemultirange_func(numra...
3063 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
3064 drop function anycompatiblerange_anycompatiblemultirange_func(anycompatiblerange, anycompatiblemultirange);
3066 create function bogus_func(anycompatible)
3067 returns anycompatiblerange as 'select int4range(1,10)' language sql;
3068 ERROR: cannot determine result data type
3069 DETAIL: A result of type anycompatiblerange requires at least one input of type anycompatiblerange or anycompatiblemultirange.
3071 -- Arrays of multiranges
3073 select ARRAY[nummultirange(numrange(1.1, 1.2)), nummultirange(numrange(12.3, 155.5))];
3075 ----------------------------------
3076 {"{[1.1,1.2)}","{[12.3,155.5)}"}
3079 create table i8mr_array (f1 int, f2 int8multirange[]);
3080 insert into i8mr_array values (42, array[int8multirange(int8range(1,10)), int8multirange(int8range(2,20))]);
3081 select * from i8mr_array;
3083 ----+-------------------------
3084 42 | {"{[1,10)}","{[2,20)}"}
3087 drop table i8mr_array;
3089 -- Multiranges of arrays
3091 select arraymultirange(arrayrange(ARRAY[1,2], ARRAY[2,1]));
3093 ---------------------
3097 select arraymultirange(arrayrange(ARRAY[2,1], ARRAY[1,2])); -- fail
3098 ERROR: range lower bound must be less than or equal to range upper bound
3099 select array[1,1] <@ arraymultirange(arrayrange(array[1,2], array[2,1]));
3105 select array[1,3] <@ arraymultirange(arrayrange(array[1,2], array[2,1]));
3112 -- Ranges of composites
3114 create type two_ints as (a int, b int);
3115 create type two_ints_range as range (subtype = two_ints);
3116 -- with force_parallel_mode on, this exercises tqueue.c's range remapping
3117 select *, row_to_json(upper(t)) as u from
3118 (values (two_ints_multirange(two_ints_range(row(1,2), row(3,4)))),
3119 (two_ints_multirange(two_ints_range(row(5,6), row(7,8))))) v(t);
3121 ---------------------+---------------
3122 {["(1,2)","(3,4)")} | {"a":3,"b":4}
3123 {["(5,6)","(7,8)")} | {"a":7,"b":8}
3126 drop type two_ints cascade;
3127 NOTICE: drop cascades to type two_ints_range
3129 -- Check behavior when subtype lacks a hash function
3131 set enable_sort = off; -- try to make it pick a hash setop implementation
3132 select '{(2,5)}'::cashmultirange except select '{(5,6)}'::cashmultirange;
3140 -- OUT/INOUT/TABLE functions
3142 -- infer anymultirange from anymultirange
3143 create function mr_outparam_succeed(i anymultirange, out r anymultirange, out t text)
3144 as $$ select $1, 'foo'::text $$ language sql;
3145 select * from mr_outparam_succeed(int4multirange(int4range(1,2)));
3151 -- infer anyarray from anymultirange
3152 create function mr_outparam_succeed2(i anymultirange, out r anyarray, out t text)
3153 as $$ select ARRAY[upper($1)], 'foo'::text $$ language sql;
3154 select * from mr_outparam_succeed2(int4multirange(int4range(1,2)));
3160 -- infer anyrange from anymultirange
3161 create function mr_outparam_succeed3(i anymultirange, out r anyrange, out t text)
3162 as $$ select range_merge($1), 'foo'::text $$ language sql;
3163 select * from mr_outparam_succeed3(int4multirange(int4range(1,2)));
3169 -- infer anymultirange from anyrange
3170 create function mr_outparam_succeed4(i anyrange, out r anymultirange, out t text)
3171 as $$ select multirange($1), 'foo'::text $$ language sql;
3172 select * from mr_outparam_succeed4(int4range(1,2));
3178 -- infer anyelement from anymultirange
3179 create function mr_inoutparam_succeed(out i anyelement, inout r anymultirange)
3180 as $$ select upper($1), $1 $$ language sql;
3181 select * from mr_inoutparam_succeed(int4multirange(int4range(1,2)));
3187 -- infer anyelement+anymultirange from anyelement+anymultirange
3188 create function mr_table_succeed(i anyelement, r anymultirange) returns table(i anyelement, r anymultirange)
3189 as $$ select $1, $2 $$ language sql;
3190 select * from mr_table_succeed(123, int4multirange(int4range(1,11)));
3196 -- use anymultirange in plpgsql
3197 create function mr_polymorphic(i anyrange) returns anymultirange
3198 as $$ begin return multirange($1); end; $$ language plpgsql;
3199 select mr_polymorphic(int4range(1, 4));
3206 create function mr_outparam_fail(i anyelement, out r anymultirange, out t text)
3207 as $$ select '[1,10]', 'foo' $$ language sql;
3208 ERROR: cannot determine result data type
3209 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.
3211 create function mr_inoutparam_fail(inout i anyelement, out r anymultirange)
3212 as $$ select $1, '[1,10]' $$ language sql;
3213 ERROR: cannot determine result data type
3214 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.
3216 create function mr_table_fail(i anyelement) returns table(i anyelement, r anymultirange)
3217 as $$ select $1, '[1,10]' $$ language sql;
3218 ERROR: cannot determine result data type
3219 DETAIL: A result of type anymultirange requires at least one input of type anyrange or anymultirange.