The traditional comparison operators (=, <, and so on) require that the left and right operands be of the same base type. Excepted equality and inequality, the other comparison operators are not useful in the real world but allow B-tree indexes to be constructed on temporal types. These operators compare the bounding periods (see the section called “Comparisons”), then the bounding boxes (see the section called “Comparisons”) and if those are equal, then the comparison depends on the subtype. For instant values, they compare first the timestamps and if those are equal, compare the values. For sequence values, they compare the first N instants, where N is the minimum of the number of composing instants of both values. Finally, for sequence set values, they compare the first N sequence values, where N is the minimum of the number of composing sequences of both values. For a temporal geography value the bounding boxes are not compared: one point of the sphere written with the longitudes 180 and -180, or at a pole with two longitudes, is one value, which compares equal, orders and hashes alike, while its bounding boxes differ.
The equality and inequality operators consider the equivalent representation for different subtypes as shown next.
SELECT tint '1@2001-01-01' = tint '{1@2001-01-01}';
-- true
SELECT tfloat '1.5@2001-01-01' = tfloat '[1.5@2001-01-01]';
-- true
SELECT ttext 'AAA@2001-01-01' = ttext '{[AAA@2001-01-01]}';
-- true
SELECT tgeompoint '{Point(1 1)@2001-01-01, Point(2 2)@2001-01-02}' =
tgeompoint '{[Point(1 1)@2001-01-01], [Point(2 2)@2001-01-02]}';
-- true
SELECT tgeography '[Point(1 1)@2001-01-01, Linestring(1 1,2 2)@2001-01-02]' =
tgeography '{[Point(1 1)@2001-01-01, Linestring(1 1,2 2)@2001-01-02]}';
-- true
Traditional comparisons
ttype {=, <>, <, >, <=, >=} ttype → boolean
cmp(ttype,ttype) → integer
Function cmp returns -1, 0, or 1 depending on whether the first argument is, respectively, less than, equal to, or greater than the second one.
SELECT tint '[1@2001-01-01, 1@2001-01-04)' = tint '[2@2001-01-03, 2@2001-01-05)'; -- false SELECT tint '[1@2001-01-01, 1@2001-01-04)' <> tint '[2@2001-01-03, 2@2001-01-05)'; -- true SELECT tint '[1@2001-01-01, 1@2001-01-04)' < tint '[2@2001-01-03, 2@2001-01-05)'; -- true SELECT ttfloat '[1@2001-01-01, 1@2001-01-04)' > ttfloat '[2@2001-01-03, 2@2001-01-05)'; -- false SELECT tbigint '[1@2001-01-01, 1@2001-01-04)' <= tbigint '[2@2001-01-03, 2@2001-01-05)'; -- true SELECT tbigint '[1@2001-01-01, 1@2001-01-04)' >= tbigint '[2@2001-01-03, 2@2001-01-05)'; -- false SELECT cmp(tfloat '[1@2001-01-01, 1@2001-01-04)', '[2@2001-01-03, 2@2001-01-05)'); -- -1
Hash values
hash(ttype) → integer hashExtended(ttype,seed bigint) → bigint
Function hash returns a 32-bit hash value and function hashExtended a 64-bit hash value computed with the given seed. Equal values have equal hash values, which is what hash indexes, hash joins, and hash aggregation rely on; this holds across subtypes, so that an instant has the hash value of a sequence or sequence set holding only that instant.
SELECT hash(tint '1@2001-01-01') = hash(tint '{1@2001-01-01}');
-- true
SELECT hash(tfloat '1.5@2001-01-01') = hash(tfloat '{[1.5@2001-01-01]}');
-- true
SELECT hash(tgeompoint '{Point(1 1)@2001-01-01, Point(2 2)@2001-01-02}') =
hash(tgeompoint '{[Point(1 1)@2001-01-01], [Point(2 2)@2001-01-02]}');
-- true
SELECT hashExtended(tint '[1@2001-01-01, 2@2001-01-02]', 0) <>
hashExtended(tint '[1@2001-01-01, 2@2001-01-02]', 1);
-- true
A possible generalization of the traditional comparison operators (=, <>, <, <=, etc.) to temporal types consists in determining whether the comparison is ever or always true. In this case, the result is a Boolean value. MobilityDB provides operators to test whether the comparison of a temporal value and a value of the base type or two temporal values is ever or always true. These operators are denoted by prefixing the traditional comparison operators with, respectively, ? (ever) and % (always). Some examples are ?=, %<>, or ?<=. Ever and always equality and non-equality are available for all temporal types, while ever and always inequalities are only available for temporal types whose base type has a total order defined, that is, tint, tfloat, or ttext. The ever and always comparisons are inverse operators: for example, ?= is the inverse of %<>, and ?> is the inverse of %<=.
Ever and always comparisons
{base,ttype} {?=, ?<>, ?<, ?>, ?<=, ?>=} {base,ttype} → boolean
{base,ttype} {%=, %<>, %<, %>, %<=, %>=} {base,ttype} → boolean
The operators do not take into account whether the bounds are inclusive or not.
SELECT tint '[1@2001-01-01, 3@2001-01-04]' ?= 2; -- false SELECT tgeometry '[Point(0 0)@2001-01-01, Linestring(0 0,1 1)@2001-01-04]' ?= geometry 'Linestring(1 1,0 0)'; -- true SELECT tfloat '[1@2001-01-01, 1@2001-01-04)' %= 1; -- true SELECT tgeometry '[Linestring(0 0,1 1)@2001-01-01, Linestring(1 1,0 0)@2001-01-04)' %= geometry 'Linestring(0 0,1 1)'; -- true
SELECT tfloat '[1@2001-01-01, 3@2001-01-04)' ?<> 2; -- true SELECT tgeompoint '[Point(1 1)@2001-01-01, Point(1 1)@2001-01-04)' ?<> geometry 'Point(1 1)'; -- false SELECT tfloat '[1@2001-01-01, 3@2001-01-04)' %<> 2; -- false SELECT tgeogpoint '[Point(1 1)@2001-01-01, Point(1 1)@2001-01-04)' %<> geography 'Point(2 2)'; -- true
SELECT tint '[1@2001-01-01, 4@2001-01-04]' ?< 2; -- true SELECT tfloat '[1@2001-01-01, 4@2001-01-04)' %< 2; -- false
SELECT tint '[1@2001-01-03, 1@2001-01-05]' ?> 0; -- true SELECT tfloat '[1@2001-01-03, 1@2001-01-05)' %> 1; -- false
SELECT tint '[1@2001-01-01, 1@2001-01-05]' ?<= 2; -- true SELECT tfloat '[1@2001-01-01, 1@2001-01-05)' %<= 4; -- true
SELECT ttext '{[AAA@2001-01-01, AAA@2001-01-03),
[BBB@2001-01-04, BBB@2001-01-05)}' ?> 'AAA'::text;
-- true
SELECT ttext '{[AAA@2001-01-01, AAA@2001-01-03),
[BBB@2001-01-04, BBB@2001-01-05)}' %> 'AAA'::text;
-- false
Ever and always overlaps
{tstzspan,tbool,ttext,tjsonb} ?&& {tstzspan,tbool,ttext,tjsonb} → boolean
{tstzspan,tbool,ttext,tjsonb} %&& {tstzspan,tbool,ttext,tjsonb} → boolean
{tbox,tnumber} ?&& {tbox,tnumber} → boolean
{tbox,tnumber} %&& {tbox,tnumber} → boolean
{stbox,tgeo} ?&& {stbox,tgeo} → boolean
{stbox,tgeo} %&& {stbox,tgeo} → boolean
The bounding box operator && (the section called “Bounding Box Operators”) tests whether the bounding box of a temporal value overlaps a box, while ?&& tests whether the value itself lies in the box at some instant, and %&& whether it lies in the box at every instant at which it is defined. The box of a temporal Boolean, text, or JSONB value is a tstzspan, that of a temporal number a tbox, and that of a temporal geo an stbox; the borders of a spatiotemporal box belong to it, and a span, as the value span of a temporal box, contains its bounds as it states them. A value whose bounding box overlaps a box need not lie in it at any instant. In the examples below, a temporal point moves from (0,0) to (10,10) between 0:00 and 10:00, so its bounding box overlaps a box in the corner (8,0)–(10,2), which the point never enters, and a box over its path between 8:00 and 10:00, which the point crosses at 5:00. Likewise, a temporal text defined on 1 and 2 January and on 4 and 5 January has a bounding period that overlaps the morning of 3 January, at which the text is not defined, and a temporal float from 1 to 5 over four days lies in the box of the values 2.5 to 3.5 and never in the box of the values 4.5 to 5 over its first two days. For two temporal values, the operators test whether the bounding boxes of their values overlap at some instant, or at every instant, that both values share; for two temporal points or numbers, whether they are at the same position or have the same value, and for two temporal Booleans, texts, or JSONB values, whether they share an instant. Two values that share no instant answer NULL, as their ever equality does.
SELECT tgeompoint '[Point(0 0)@2001-01-01 00:00, Point(10 10)@2001-01-01 10:00]' ?&&
stbox 'STBOX XT(((8,0),(10,2)),[2001-01-01 00:00, 2001-01-01 10:00])';
-- false
SELECT tgeompoint '[Point(0 0)@2001-01-01 00:00, Point(10 10)@2001-01-01 10:00]' ?&&
stbox 'STBOX XT(((4,4),(6,6)),[2001-01-01 04:00, 2001-01-01 06:00])';
-- true
SELECT tgeompoint '[Point(0 0)@2001-01-01 00:00, Point(10 10)@2001-01-01 10:00]' ?&&
stbox 'STBOX XT(((4,4),(6,6)),[2001-01-01 08:00, 2001-01-01 10:00])';
-- false
SELECT tgeompoint '[Point(0 0)@2001-01-01 00:00, Point(10 10)@2001-01-01 10:00]' %&&
stbox 'STBOX X((0,0),(10,10))';
-- true
SELECT ttext '{[AA@2001-01-01, AA@2001-01-02], [BB@2001-01-04, BB@2001-01-05]}' &&
tstzspan '[2001-01-03, 2001-01-03 12:00]';
-- true
SELECT ttext '{[AA@2001-01-01, AA@2001-01-02], [BB@2001-01-04, BB@2001-01-05]}' ?&&
tstzspan '[2001-01-03, 2001-01-03 12:00]';
-- false
SELECT tfloat '[1@2001-01-01, 5@2001-01-05]' ?&&
tbox 'TBOXFLOAT XT([2.5, 3.5],[2001-01-01, 2001-01-05])';
-- true
SELECT tfloat '[1@2001-01-01, 5@2001-01-05]' ?&&
tbox 'TBOXFLOAT XT([4.5, 5],[2001-01-01, 2001-01-03])';
-- false
SELECT tfloat '[1@2001-01-01, 3@2001-01-03]' ?&& tfloat '[3@2001-01-01, 1@2001-01-03]';
-- true
SELECT tfloat '[1@2001-01-01, 3@2001-01-03]' %&& tfloat '[3@2001-01-01, 1@2001-01-03]';
-- false
SELECT tgeompoint '[Point(0 0)@2001-01-01, Point(2 2)@2001-01-03]' ?&&
tgeompoint '[Point(2 0)@2001-01-01, Point(0 2)@2001-01-03]';
-- true
Another possible generalization of the traditional comparison operators (=, <>, <, <=, etc.) to temporal types consists in determining whether the comparison is true or false at each instant. In this case, the result is a temporal Boolean. The temporal comparison operators are denoted by prefixing the traditional comparison operators with #. Some examples are #= or #<=. Temporal equality and non-equality are available for all temporal types, while temporal inequalities are only available for temporal types whose base type has a total order defined, that is, tint, tfloat, or ttext.
Temporal comparisons
{base,ttype} {#=, #<>, #<, #>, #<=, #>=} {base,ttype} → boolean
SELECT tfloat '[1@2001-01-01, 2@2001-01-04)' #= 3;
-- {[f@2001-01-01, f@2001-01-04)}
SELECT tfloat '[1@2001-01-01, 4@2001-01-04)' #= tfloat '[4@2001-01-02, 1@2001-01-05)';
-- {[f@2001-01-02, t@2001-01-03], (f@2001-01-03, f@2001-01-04)}
SELECT tgeompoint '[Point(0 0)@2001-01-01,
Point(2 2)@2001-01-03)' #= geometry 'Point(1 1)';
-- {[f@2001-01-01, t@2001-01-02], (f@2001-01-02, f@2001-01-03)}
SELECT tgeometry '[Point(0 0)@2001-01-01,
Linestring(1 1,2 2)@2001-01-03]' #= geometry 'Linestring(2 2,1 1)';
-- {[f@2001-01-01, t@2001-01-03]}
SELECT tfloat '[1@2001-01-01, 4@2001-01-04)' #<> 2;
-- {[t@2001-01-01, f@2001-01-02], (t@2001-01-02, 2001-01-04)}
SELECT tfloat '[1@2001-01-01, 4@2001-01-04)' #<> tfloat '[2@2001-01-02, 2@2001-01-05)';
-- {[f@2001-01-02], (t@2001-01-02, t@2001-01-04)}
SELECT tgeometry '[Point(0 0)@2001-01-01, Linestring(1 1,2 2)@2001-01-03]' #<>
geometry 'Linestring(2 2,1 1)';
-- {[t@2001-01-01, f@2001-01-03]}
SELECT tfloat '[1@2001-01-01, 4@2001-01-04)' #< 2;
-- {[t@2001-01-01, f@2001-01-02, f@2001-01-04)}
SELECT 1 #> tint '[1@2001-01-03, 1@2001-01-05)';
-- [f@2001-01-03, f@2001-01-05)
SELECT tfloat '[1@2001-01-01, 1@2001-01-05)' #<= tfloat '{2@2001-01-03, 3@2001-01-04}';
-- {t@2001-01-03, t@2001-01-04}
SELECT 'AAA'::text #> ttext '{[AAA@2001-01-01, AAA@2001-01-03),
[BBB@2001-01-04, BBB@2001-01-05)}';
-- {[f@2001-01-01, f@2001-01-03), [t@2001-01-04, t@2001-01-05)}