Values of set and span types can be converted to one another or converted to and from PostgreSQL range types using the function CAST
or using the ::
notation.
Convert a base value to a set, span, or span set value
base::{set,span,spanset}
set(base) → set
span(base) → span
spanset(base) → spanset
SELECT CAST(timestamptz '2001-01-01 08:00:00' AS tstzset); -- {2001-01-01 08:00:00} SELECT timestamptz '2001-01-01 08:00:00'::tstzspan; -- [2001-01-01 08:00:00, 2001-01-01 08:00:00] SELECT spanset(timestamptz '2001-01-01 08:00:00'); -- {[2001-01-01 08:00:00, 2001-01-01 08:00:00]}
Convert a set value to a span set value
set::spanset
spanset(set) → spanset
SELECT spanset(tstzset '{2001-01-01 08:00:00, 2001-01-01 08:15:00, 2001-01-01 08:25:00}'); /* {[2001-01-01 08:00:00, 2001-01-01 08:00:00], [2001-01-01 08:15:00, 2001-01-01 08:15:00], [2001-01-01 08:25:00, 2001-01-01 08:25:00]} */
Convert a span value to a span set value
span::spanset
spanset(span) → spanset
SELECT floatspan '[1.5,2.5]'::floatspanset; -- {[1.5, 2.5]} SELECT tstzspan '[2001-01-01 08:00:00, 2001-01-01 08:30:00)'::tstzspanset; -- {[2001-01-01 08:00:00, 2001-01-01 08:30:00)}
Convert a set or a span set into a span, ignoring the potential time gaps
{set,spanset}::span
span({set,spanset}) → span
SELECT span(dateset '{2001-01-01, 2001-01-03, 2001-01-05}'); -- [2001-01-01, 2001-01-06) SELECT span(tstzspanset '{[2001-01-01, 2001-01-02), [2001-01-03, 2001-01-04)}'); -- [2001-01-01, 2001-01-04)
Convert a span value to and from a PostgreSQL range value
span::range
range::span
range(span) → range
span(range) → span
Notice that PostgreSQL range values accept empty ranges and ranges with infinite values, which are not allowed as span values in MobilityDB
SELECT intspan '[10, 20)'::int4range; -- [10,20) SELECT tstzspan '[2001-01-01 08:00:00, 2001-01-01 08:30:00)'::tstzrange; -- ["2001-01-01 08:00:00","2001-01-01 08:30:00") SELECT int4range '[10, 20)'::intspan; -- [10,20) SELECT int4range 'empty'::intspan; -- ERROR: Range cannot be empty SELECT int4range '[10,)'::intspan; -- ERROR: Range bounds cannot be infinite SELECT tstzrange '[2001-01-01 08:00:00, 2001-01-01 08:30:00)'::tstzspan; -- [2001-01-01 08:00:00, 2001-01-01 08:30:00)
Convert a span set value to and from a PostgreSQL multirange value
spanset::multirange
multirange::spanset
multirange(spanset) → multirange
spanset(multirange) → spanset
SELECT intspanset '{[1,2],[4,5]}'::int4multirange; -- {[1,3),[4,6)} SELECT tstzspanset '{[2001-01-01,2001-01-02],[2001-01-04,2001-01-05]}'::tstzmultirange; -- {[2001-01-01,2001-01-02],[2001-01-04,2001-01-05]} SELECT int4multirange '{[1,2],[4,5]}'::intspanset; -- {[1, 3), [4, 6)} SELECT tstzmultirange '{[2001-01-01,2001-01-02],[2001-01-04,2001-01-05]}'::tstzspanset; -- {[2001-01-01, 2001-01-02], [2001-01-04, 2001-01-05]}