Chapter 5. Temporal Types: Generic Operations (Part 1)

Table of Contents

Introduction
Input and Output
Constructors
Conversions
Accessors
Transformations

Introduction

We present next the functions and operators for temporal types. These functions and operators are polymorphic, that is, their arguments may be of several types, and the result type may depend on the type of the arguments. To express this, we use the following notation:

  • time represents any time type, that is, timestamptz, tstzspan, tstzset, or tstzspanset,

  • ttype represents any temporal type,

  • tdisc represents any temporal type with a discrete base type, that is, tbool, tint, or ttext,

  • tcont represents any temporal type with a continuous base type, that is, tfloat, tgeompoint, or tgeogpoint,

  • torder represents any temporal type whose base type has a total order defined, that is, tint, tfloat, or ttext,

  • talpha represents any temporal alphanumeric type, such as, tint or ttext,

  • tnumber represents any temporal number type, that is, tint or tfloat,

  • tpoint represents a temporal point type, that is, tgeompoint or tgeogpoint,

  • ttypeInst represents any temporal type with instant subtype,

  • ttypeDiscSeq represents any temporal type with sequence subtype and discrete interpolation,

  • ttypeContSeq represents any temporal type with sequence subtype and continuous interpolation,

  • ttypeSeqSet represents any temporal type with sequence set subtype,

  • base represents any base type of a temporal type, that is, boolean, integer, float, text, geometry, or geography,

  • values represents any set of values of a base type of a temporal type, for example, integer, intset, intspan, and intspanset for the base type integer

  • number represents any number base type, that is, integer or float,

  • numspan represents any number span type, that is, either intspan or floatspan,

  • geo represents the types geometry or geography,

  • geompoint represents the type geometry restricted to a point.

  • point represents the types geometry or geography restricted to a point.

  • type[] represents an array of type.

  • <type> in the name of a function represents the functions obtained by replacing <type> by a specific type. For example, tintDiscSeq or tfloatDiscSeq are represented by ttypeDiscSeq.

A common way to generalize the traditional operations to the temporal types is to apply the operation at each instant, which yields a temporal value as result. In that case, the operation is only defined on the intersection of the emporal extents of the operands; if the temporal extents are disjoint, then the result is null. For example, the temporal comparison operators, such as #<, test whether the values taken by their operands at each instant satisfy the condition and return a temporal Boolean. Examples of the various generalizations of the operators are given next.

-- Temporal comparison
SELECT tfloat '[2@2001-01-01, 2@2001-01-03)' #< tfloat '[1@2001-01-01, 3@2001-01-03)';
-- {[f@2001-01-01, f@2001-01-02], (t@2001-01-02, t@2001-01-03)}
SELECT tfloat '[1@2001-01-01, 3@2001-01-03)' #< tfloat '[3@2001-01-03, 1@2001-01-05)';
-- NULL

-- Temporal addition
SELECT tint '[1@2001-01-01, 1@2001-01-03)' + tint '[2@2001-01-02, 2@2001-01-05)';
-- [3@2001-01-02, 3@2001-01-03)

-- Temporal intersects
SELECT tIntersects(tgeompoint '[Point(0 1)@2001-01-01, Point(3 1)@2001-01-04)',
  geometry 'Polygon((1 0,1 2,2 2,2 0,1 0))');
-- {[f@2001-01-01, t@2001-01-02, t@2001-01-03], (f@2001-01-03, f@2001-01-04]}

-- Temporal distance
SELECT tgeompoint '[Point(0 0)@2001-01-01 08:00:00, Point(0 1)@2001-01-03 08:10:00)' <->
  tgeompoint '[Point(0 0)@2001-01-02 08:05:00, Point(1 1)@2001-01-05 08:15:00)';
-- [0.5@2001-01-02 08:05:00+00, 0.745184033794557@2001-01-03 08:10:00+00)

Another common requirement is to determine whether the operands ever or always satisfy a condition with respect to an operation. These can be obtained by applying the ever or always comparison operators. These operators are denoted by prefixing the traditional comparison operators with, respectively, ? (ever) and % (always). Examples of ever and always comparison operators are given next.

-- Does the operands ever intersect?
SELECT eIntersects(tgeompoint '[Point(0 1)@2001-01-01, Point(3 1)@2001-01-04)',
  geometry 'Polygon((1 0,1 2,2 2,2 0,1 0))') ?= true;
-- true

-- Does the operands always intersect?
SELECT aIntersects(tgeompoint '[Point(0 1)@2001-01-01, Point(3 1)@2001-01-04)',
  geometry 'Polygon((0 0,0 2,4 2,4 0,0 0))') %= true;
-- true

-- Is the left operand ever less than the right one ?
SELECT (tfloat '[1@2001-01-01, 3@2001-01-03)' #<
  tfloat '[3@2001-01-01, 1@2001-01-03)') ?= true;
-- true

-- Is the left operand always less than the right one ?
SELECT (tfloat '[1@2001-01-01, 3@2001-01-03)' #<
  tfloat '[2@2001-01-01, 4@2001-01-03)') %= true;
-- true

For example, the eIntersects function determines whether there is an instant at which the two arguments spatially intersect.

We describe next the functions and operators for temporal types. For conciseness, in the examples we mostly use sequences composed of two instants.