Ever and Always Relationships

Operands of different dimensions read the dimensions both of them have, see the section called “Operands of Different Dimensions”.

Each relationship of this section has two forms. The scalar form compares two values, a geometry or a temporal geometry on each side, and returns whether the relationship holds at some instant (prefix e, ever) or at every instant (prefix a, always) of the time they share. The array form, whose name ends in Pairs, compares every element of an array of temporal geometries with every element of another and returns, one row per pair, the one-based indexes i and j of the pairs for which the scalar form returns true. A pair sharing no instant, for which the scalar form returns null, is not returned. The array form states in a single call the spatial join of two sets of moving objects, which the scalar form states by comparing every pair of their cross product: the spatiotemporal box of each element settles the pairs whose boxes alone decide the relationship, and the relationship is computed exactly on the others. The indexes are positions in the arrays, and an array_agg of the identifiers in the order of the array of values maps them back to the objects. The array form is defined for temporal points and temporal geometries, planar and geodetic, where the scalar form is, except touches, contains and covers, whose array forms take temporal geometries. In the array form of contains and covers, the element of the first array is the one that contains or covers the element of the second.

The following example returns, for each pair of ships, whether they ever come within a distance of 2 of each other, first with the scalar form on two of them, then with the array form on all of them, mapping the indexes back to the identifiers.

SELECT eDwithin(tgeompoint '[Point(0 0)@2001-01-01, Point(0 10)@2001-01-02]',
  tgeompoint '[Point(1 0)@2001-01-01, Point(1 10)@2001-01-02]', 2);
-- true
WITH ships(id, trip) AS (VALUES
  ('A', tgeompoint '[Point(0 0)@2001-01-01, Point(0 10)@2001-01-02]'),
  ('B', tgeompoint '[Point(50 50)@2001-01-01, Point(50 60)@2001-01-02]'),
  ('C', tgeompoint '[Point(1 0)@2001-01-01, Point(1 10)@2001-01-02]')),
fleet AS (SELECT array_agg(id ORDER BY id) AS ids,
  array_agg(trip ORDER BY id) AS trips FROM ships)
SELECT ids[i] AS ship1, ids[j] AS ship2
FROM fleet, eDwithinPairs(trips, trips, 2)
WHERE i < j;
-- A | C