MobilityDB 1.3
Loading...
Searching...
No Matches
temporal.h
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * This MobilityDB code is provided under The PostgreSQL License.
4 * Copyright (c) 2016-2025, Université libre de Bruxelles and MobilityDB
5 * contributors
6 *
7 * MobilityDB includes portions of PostGIS version 3 source code released
8 * under the GNU General Public License (GPLv2 or later).
9 * Copyright (c) 2001-2025, PostGIS contributors
10 *
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose, without fee, and without a written
13 * agreement is hereby granted, provided that the above copyright notice and
14 * this paragraph and the following two paragraphs appear in all copies.
15 *
16 * IN NO EVENT SHALL UNIVERSITE LIBRE DE BRUXELLES BE LIABLE TO ANY PARTY FOR
17 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
18 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
19 * EVEN IF UNIVERSITE LIBRE DE BRUXELLES HAS BEEN ADVISED OF THE POSSIBILITY
20 * OF SUCH DAMAGE.
21 *
22 * UNIVERSITE LIBRE DE BRUXELLES SPECIFICALLY DISCLAIMS ANY WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON
25 * AN "AS IS" BASIS, AND UNIVERSITE LIBRE DE BRUXELLES HAS NO OBLIGATIONS TO
26 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
27 *
28 *****************************************************************************/
29
34#ifndef __TEMPORAL_H__
35#define __TEMPORAL_H__
36
37/* PostgreSQL */
38#include <postgres.h>
39#if POSTGRESQL_VERSION_NUMBER >= 160000
40 #include "varatt.h"
41#endif
42/* MEOS */
43#include <meos.h>
44#include <meos_geo.h>
45#include "temporal/doublen.h"
46#include "temporal/meos_catalog.h"
47
48/* To avoid including pg_collation_d */
49#define DEFAULT_COLLATION_OID 100
50#define C_COLLATION_OID 950
51#define POSIX_COLLATION_OID 951
52
53#ifndef FMGR_H
54 /* To avoid including fmgr.h However this implies that the text values must
55 * be ALWAYS detoasted */
56 #define DatumGetTextP(X) ((text *) DatumGetPointer(X)) // ((text *) PG_DETOAST_DATUM(X))
57#endif /* FMGR_H */
58
62#define MEOS_EPSILON 1.0e-06
63#define MEOS_FP_EQ(A, B) (fabs((A)-(B)) <= MEOS_EPSILON)
64#define MEOS_FP_NE(A, B) (fabs((A)-(B)) > MEOS_EPSILON)
65#define MEOS_FP_LT(A, B) (((A) + MEOS_EPSILON) < (B))
66#define MEOS_FP_LE(A, B) (((A) - MEOS_EPSILON) <= (B))
67#define MEOS_FP_GT(A, B) (((A) - MEOS_EPSILON) > (B))
68#define MEOS_FP_GE(A, B) (((A) + MEOS_EPSILON) >= (B))
69
73#define DIST_EPSILON 1.0e-06
74
75#define UNUSED __attribute__((unused))
76
78#define DISCONTINUOUS true
79#define CONTINUOUS false
80
82#define ORDER true
83#define ORDER_NO false
84
86#define QUOTES true
87#define QUOTES_NO false
88
90#define SPACES true
91#define SPACES_NO false
92
94#define INVERT true
95#define INVERT_NO false
96
98#define REST_AT true
99#define REST_MINUS false
100
102#define BORDER_INC true
103#define BORDER_EXC false
104
106#define EVER true
107#define ALWAYS false
108
110#define TPOINT true
111#define TGEO false
112
114#define GET_MIN true
115#define GET_MAX false
116
118#define CROSSINGS true
119#define CROSSINGS_NO false
120
122#define BBOX_TEST true
123#define BBOX_TEST_NO false
124
126#define MERGE true
127#define MERGE_NO false
128
129#define NORMALIZE true
130#define NORMALIZE_NO false
131
133#define WITH_Z true
134#define NO_Z false
135
137#define VALUE_SEL true
138#define TIME_SEL false
139
141#define UPPER_EXC true
142#define TIME_SEL false
143
145#define UNARY_UNION true
146#define UNARY_UNION_NO false
147
149typedef enum
150{
153} SyncMode;
154
156typedef enum
157{
162
164typedef enum
165{
168 MINUS
170
171/* PostgreSQL removed pg_atoi in version 15 */
172#if POSTGRESQL_VERSION_NUMBER >= 150000
173 extern int32 pg_strtoint32(const char *s);
174#else
175 /* To avoid including <utils/builtins.h> */
176 extern int32 pg_atoi(const char *s, int size, int c);
177#endif /* POSTGRESQL_VERSION_NUMBER >= 150000 */
178
179/*****************************************************************************
180 * Additional operator strategy numbers used in the GiST and SP-GiST temporal
181 * opclasses with respect to those defined in the file stratnum.h
182 *****************************************************************************/
183
184#define RTOverBeforeStrategyNumber 28 /* for &<# */
185#define RTBeforeStrategyNumber 29 /* for <<# */
186#define RTAfterStrategyNumber 30 /* for #>> */
187#define RTOverAfterStrategyNumber 31 /* for #&> */
188#define RTOverFrontStrategyNumber 32 /* for &</ */
189#define RTFrontStrategyNumber 33 /* for <</ */
190#define RTBackStrategyNumber 34 /* for />> */
191#define RTOverBackStrategyNumber 35 /* for /&> */
192
193/*****************************************************************************
194 * Well-Known Binary (WKB)
195 *****************************************************************************/
196
197/* Data type size */
198#define MEOS_WKB_BYTE_SIZE 1
199#define MEOS_WKB_INT2_SIZE 2
200#define MEOS_WKB_INT4_SIZE 4
201#define MEOS_WKB_INT8_SIZE 8
202#define MEOS_WKB_DOUBLE_SIZE 8
203#define MEOS_WKB_DATE_SIZE 4
204#define MEOS_WKB_TIMESTAMP_SIZE 8
205
206/* Temporal subtype */
208{
212};
213
214/* Span bounds */
215#define MEOS_WKB_LOWER_INC 0x01
216#define MEOS_WKB_UPPER_INC 0x02
217
218/* Machine endianness */
219#define XDR 0 /* big endian */
220#define NDR 1 /* little endian */
221
222/* Variation flags
223 * The first byte of the variation flag depends on the type we are sending
224 * - Set types: xxxO where O states whether the set is ordered or not
225 * - Box types: xxTX where X and T state whether the corresponding dimensions
226 * are present
227 * - Temporal types: xxSS where SS correspond to the subtype
228 * and x are unused bits
229 */
230#define MEOS_WKB_ORDERED 0x01 // 1
231#define MEOS_WKB_XFLAG 0x01 // 1
232#define MEOS_WKB_TFLAG 0x02 // 2
233#define MEOS_WKB_INTERPFLAGS 0x0C // 4 + 8
234#define MEOS_WKB_ZFLAG 0x10 // 16
235#define MEOS_WKB_GEODETICFLAG 0x20 // 32
236#define MEOS_WKB_SRIDFLAG 0x40 // 64
237
238#define MEOS_WKB_GET_INTERP(flags) (((flags) & MEOS_WKB_INTERPFLAGS) >> 2)
239#define MEOS_WKB_SET_INTERP(flags, value) ((flags) = (((flags) & ~MEOS_WKB_INTERPFLAGS) | ((value & 0x0003) << 2)))
240
241// #define MEOS_WKB_GET_LINEAR(flags) ((bool) (((flags) & MEOS_WKB_LINEARFLAG)>>3))
242
243/*****************************************************************************
244 * Definitions for binning and tiling
245 *****************************************************************************/
246
247/*
248 * The default origin is Monday 2000-01-03. We don't use PG epoch since it
249 * starts on a Saturday. This makes time bins by a week more intuitive and
250 * aligns it with date_trunc.
251 */
252#define JAN_3_2000 (2 * USECS_PER_DAY)
253#define DEFAULT_TIME_ORIGIN (JAN_3_2000)
254#define DEFAULT_FLOATSPAN_ORIGIN (0.0)
255#define DEFAULT_INTSPAN_ORIGIN (0)
256#define DEFAULT_BIGINTSPAN_ORIGIN (0)
257
258/*****************************************************************************
259 * Additional struct definitions for temporal types
260 *****************************************************************************/
261
265typedef union bboxunion
266{
271
272/*****************************************************************************
273 * Miscellaneous
274 *****************************************************************************/
275
276/* Definition of output function */
277typedef char *(*outfunc)(Datum value, meosType type, int maxdd);
278
279/* Definition of qsort comparator for integers */
280typedef int (*qsort_comparator) (const void *a, const void *b);
281
282/* Definition of a variadic function type for temporal lifting */
283typedef Datum (*varfunc) (Datum, ...);
284
285/* Definition of a turning point function for a temporal and a base types */
288
289/* Definition of a turning point function for two temporal types */
292
293/* Definition of a function with one to three Datum arguments and returning
294 * a Datum */
298
299/*****************************************************************************
300 * fmgr macros temporal types
301 *****************************************************************************/
302
303/* doubleN */
304
305#define DatumGetDouble2P(X) ((double2 *) DatumGetPointer(X))
306#define Double2PGetDatum(X) PointerGetDatum(X)
307#define DatumGetDouble3P(X) ((double3 *) DatumGetPointer(X))
308#define Double3PGetDatum(X) PointerGetDatum(X)
309#define DatumGetDouble4P(X) ((double4 *) DatumGetPointer(X))
310#define Double4PGetDatum(X) PointerGetDatum(X)
311
312/* Temporal types */
313
314#if MEOS
315 #define DatumGetTemporalP(X) ((Temporal *) DatumGetPointer(X))
316#else
317 #define DatumGetTemporalP(X) ((Temporal *) PG_DETOAST_DATUM(X))
318#endif /* MEOS */
319
320#define PG_GETARG_TEMPORAL_P(X) ((Temporal *) PG_GETARG_VARLENA_P(X))
321#define PG_GETARG_TINSTANT_P(X) ((TInstant *) PG_GETARG_VARLENA_P(X))
322#define PG_GETARG_TSEQUENCE_P(X) ((TSequence *) PG_GETARG_VARLENA_P(X))
323#define PG_GETARG_TSEQUENCESET_P(X) ((TSequenceSet *) PG_GETARG_VARLENA_P(X))
324
325#define PG_RETURN_TEMPORAL_P(X) PG_RETURN_POINTER(X)
326#define PG_RETURN_TINSTANT_P(X) PG_RETURN_POINTER(X)
327#define PG_RETURN_TSEQUENCE_P(X) PG_RETURN_POINTER(X)
328#define PG_RETURN_TSEQUENCESET_P(X) PG_RETURN_POINTER(X)
329
330#define TemporalPGetDatum(X) PointerGetDatum(X)
331#define TInstantPGetDatum(X) PointerGetDatum(X)
332#define TSequencePGetDatum(X) PointerGetDatum(X)
333#define TSequenceSetPGetDatum(X) PointerGetDatum(X)
334
335#define DATUM_FREE(value, basetype) \
336 do { \
337 if (! basetype_byvalue(basetype)) \
338 pfree(DatumGetPointer(value)); \
339 } while (0)
340
341#define DATUM_FREE_IF_COPY(value, basetype, n) \
342 do { \
343 if (! basetype_byvalue(basetype) && DatumGetPointer(value) != PG_GETARG_POINTER(n)) \
344 pfree(DatumGetPointer(value)); \
345 } while (0)
346
353#define PG_FREE_IF_COPY_P(ptrsrc, ptrori) \
354 do { \
355 if ((Pointer) (ptrsrc) != (Pointer) (ptrori)) \
356 pfree(ptrsrc); \
357 } while (0)
358
359#define PG_DATUM_NEEDS_DETOAST(datum) \
360 (VARATT_IS_EXTENDED((datum)) || VARATT_IS_EXTERNAL((datum)) || \
361 VARATT_IS_COMPRESSED((datum)))
362
363/*****************************************************************************/
364
365/* Parameter tests */
366
367extern bool ensure_has_X(meosType type, int16 flags);
368extern bool ensure_has_Z(meosType type, int16 flags);
369extern bool ensure_has_T(meosType type, int16 flags);
370extern bool ensure_has_not_Z(meosType type, int16 flags);
371extern bool ensure_not_null(void *ptr);
372extern bool ensure_one_not_null(void *ptr1, void *ptr2);
373extern bool ensure_one_true(bool hasshift, bool haswidth);
374extern bool ensure_valid_interp(meosType temptype, interpType interp);
375extern bool ensure_continuous(const Temporal *temp);
376extern bool ensure_same_interp(const Temporal *temp1, const Temporal *temp2);
377extern bool ensure_same_continuous_interp(int16 flags1, int16 flags2);
378extern bool ensure_linear_interp(int16 flags);
379extern bool ensure_nonlinear_interp(int16 flags);
380extern bool ensure_common_dimension(int16 flags1, int16 flags2);
381extern bool ensure_temporal_isof_type(const Temporal *temp, meosType type);
382extern bool ensure_temporal_isof_basetype(const Temporal *temp,
383 meosType basetype);
384extern bool ensure_temporal_isof_subtype(const Temporal *temp,
385 tempSubtype type);
386extern bool ensure_same_temporal_type(const Temporal *temp1,
387 const Temporal *temp2);
388
389extern bool ensure_valid_tnumber_numspan(const Temporal *temp, const Span *s);
390extern bool ensure_valid_tnumber_numspanset(const Temporal *temp,
391 const SpanSet *ss);
392extern bool ensure_valid_tnumber_tbox(const Temporal *temp, const TBox *box);
393extern bool ensure_valid_temporal_set(const Temporal *temp, const Set *s);
394extern bool ensure_valid_temporal_temporal(const Temporal *temp1, const Temporal *temp2);
395extern bool ensure_valid_tnumber_tnumber(const Temporal *temp1, const Temporal *temp2);
396extern bool ensure_not_negative(int i);
397extern bool ensure_positive(int i);
398extern bool not_negative_datum(Datum size, meosType basetype);
399extern bool ensure_not_negative_datum(Datum size, meosType basetype);
400extern bool positive_datum(Datum size, meosType basetype);
401extern bool ensure_positive_datum(Datum size, meosType basetype);
402extern bool ensure_valid_day_duration(const Interval *duration);
403extern bool positive_duration(const Interval *duration);
404extern bool ensure_positive_duration(const Interval *duration);
405
406/* General functions */
407
408extern void *temporal_bbox_ptr(const Temporal *temp);
409
410extern bool intersection_temporal_temporal(const Temporal *temp1,
411const Temporal *temp2, SyncMode mode, Temporal **inter1, Temporal **inter2);
412
413/* Version functions */
414
415extern char *mobilitydb_version(void);
416extern char *mobilitydb_full_version(void);
417
418/* Transformations */
419
420extern datum_func2 round_fn(meosType basetype);
421
422/* Restriction functions */
423
424extern bool temporal_bbox_restrict_value(const Temporal *temp, Datum value);
425
426/*****************************************************************************/
427
428#endif
char * mobilitydb_full_version(void)
Return the versions of the MobilityDB extension and its dependencies.
Definition: temporal.c:772
char * mobilitydb_version(void)
Return the version of the MobilityDB extension.
Definition: temporal.c:762
meosType
Enumeration that defines the built-in and temporal types used in MobilityDB.
Definition: meos_catalog.h:55
bool ensure_valid_tnumber_tnumber(const Temporal *temp1, const Temporal *temp2)
Ensure that two temporal numbers have the same span type.
Definition: temporal.c:430
SyncMode
Enumeration for the intersection/synchronization functions.
Definition: temporal.h:150
@ SYNCHRONIZE_CROSS
Definition: temporal.h:152
@ SYNCHRONIZE_NOCROSS
Definition: temporal.h:151
MEOS_WKB_TSUBTYPE
Definition: temporal.h:208
@ MEOS_WKB_TSEQUENCESET
temporal sequence set subtype
Definition: temporal.h:211
@ MEOS_WKB_TSEQUENCE
temporal sequence subtype
Definition: temporal.h:210
@ MEOS_WKB_TINSTANT
temporal instant subtype
Definition: temporal.h:209
bool positive_duration(const Interval *duration)
Return true if an interval is a positive duration.
Definition: temporal.c:611
int(* tpfunc_base)(Datum, Datum, Datum, TimestampTz, TimestampTz, TimestampTz *, TimestampTz *)
Definition: temporal.h:286
bool ensure_valid_tnumber_numspan(const Temporal *temp, const Span *s)
Ensure that a temporal number and a span have the same span type.
Definition: temporal.c:339
void * temporal_bbox_ptr(const Temporal *temp)
Return a pointer to the precomputed bounding box of a temporal value.
Definition: temporal.c:650
bool ensure_not_null(void *ptr)
Ensure that the pointer is not null.
Definition: temporal.c:136
int(* qsort_comparator)(const void *a, const void *b)
Definition: temporal.h:280
bool ensure_temporal_isof_type(const Temporal *temp, meosType type)
Ensure that a temporal value is of a temporal type.
Definition: temporal.c:294
bool ensure_one_true(bool hasshift, bool haswidth)
Ensure that at least one of the values is true.
Definition: temporal.c:161
bool ensure_temporal_isof_basetype(const Temporal *temp, meosType basetype)
Ensure that a temporal value has a given base type.
Definition: temporal.c:280
bool ensure_valid_temporal_temporal(const Temporal *temp1, const Temporal *temp2)
Ensure that a temporal number and a temporal box have the same span type.
Definition: temporal.c:412
bool ensure_positive_duration(const Interval *duration)
Ensure that an interval is a positive duration.
Definition: temporal.c:626
bool not_negative_datum(Datum size, meosType basetype)
Return true if a number is not negative.
Definition: temporal.c:475
bool ensure_same_continuous_interp(int16 flags1, int16 flags2)
Ensure that two temporal values have the same continuous interpolation.
Definition: temporal.c:220
bool ensure_positive_datum(Datum size, meosType basetype)
Ensure that a number is strictly positive.
Definition: temporal.c:537
bool ensure_has_Z(meosType type, int16 flags)
Ensure that a MEOS type has Z dimension.
Definition: temporal.c:97
Datum(* varfunc)(Datum,...)
Definition: temporal.h:283
bool intersection_temporal_temporal(const Temporal *temp1, const Temporal *temp2, SyncMode mode, Temporal **inter1, Temporal **inter2)
Return the temporal intersection of two temporal values.
Definition: temporal.c:668
bool ensure_valid_day_duration(const Interval *duration)
Ensure that a day interval for binning is valid.
Definition: temporal.c:572
datum_func2 round_fn(meosType basetype)
Return the function for rounding a base type.
Definition: temporal.c:1179
int(* tpfunc_temp)(Datum, Datum, Datum, Datum, Datum, TimestampTz, TimestampTz, TimestampTz *, TimestampTz *)
Definition: temporal.h:290
bool ensure_continuous(const Temporal *temp)
Ensure that the subtype of temporal type is a sequence (set)
Definition: temporal.c:190
bool ensure_valid_temporal_set(const Temporal *temp, const Set *s)
Ensure that a temporal number and a set have the same span type.
Definition: temporal.c:395
bool ensure_not_negative_datum(Datum size, meosType basetype)
Ensure that a number is not negative.
Definition: temporal.c:492
bool ensure_same_temporal_type(const Temporal *temp1, const Temporal *temp2)
Ensure that two temporal values have the same temporal type.
Definition: temporal.c:309
bool ensure_linear_interp(int16 flags)
Ensure that a temporal value has linear interpolation.
Definition: temporal.c:237
bool temporal_bbox_restrict_value(const Temporal *temp, Datum value)
Return true if the bounding box of a temporal value contains a base value.
Definition: temporal_restrict.c:74
bool ensure_nonlinear_interp(int16 flags)
Ensure that a temporal value does not have linear interpolation.
Definition: temporal.c:250
int32 pg_atoi(const char *s, int size, int c)
bool ensure_has_T(meosType type, int16 flags)
Ensure that a MEOS type has Z dimension.
Definition: temporal.c:123
Datum(* datum_func2)(Datum, Datum)
Definition: temporal.h:296
bool ensure_temporal_isof_subtype(const Temporal *temp, tempSubtype type)
Ensure that a temporal value is of a given subtype.
Definition: temporal.c:323
Datum(* datum_func3)(Datum, Datum, Datum)
Definition: temporal.h:297
bool ensure_valid_interp(meosType temptype, interpType interp)
Ensure that an interpolation is valid.
Definition: temporal.c:175
bool ensure_has_not_Z(meosType type, int16 flags)
Ensure that a MEOS type has not Z dimension.
Definition: temporal.c:110
bool positive_datum(Datum size, meosType basetype)
Return true if a number is strictly positive.
Definition: temporal.c:514
TemporalFamily
Enumeration for the families of temporal types.
Definition: temporal.h:157
@ TSPATIALTYPE
Definition: temporal.h:160
@ TNUMBERTYPE
Definition: temporal.h:159
@ TEMPORALTYPE
Definition: temporal.h:158
bool ensure_one_not_null(void *ptr1, void *ptr2)
Ensure that at least one of the pointers is not null.
Definition: temporal.c:148
bool ensure_common_dimension(int16 flags1, int16 flags2)
Ensure that two temporal values have at least one common dimension.
Definition: temporal.c:263
Datum(* datum_func1)(Datum)
Definition: temporal.h:295
bool ensure_same_interp(const Temporal *temp1, const Temporal *temp2)
Ensure that two temporal values have the same interpolation.
Definition: temporal.c:205
bool ensure_not_negative(int i)
Ensure that a number is positive or zero.
Definition: temporal.c:449
bool ensure_positive(int i)
Ensure that a number is positive.
Definition: temporal.c:462
SetOper
Enumeration for the set operations of span and temporal types.
Definition: temporal.h:165
@ UNION
Definition: temporal.h:166
@ INTER
Definition: temporal.h:167
@ MINUS
Definition: temporal.h:168
bool ensure_valid_tnumber_numspanset(const Temporal *temp, const SpanSet *ss)
Ensure that a temporal number and a span set have the same span type.
Definition: temporal.c:356
bool ensure_valid_tnumber_tbox(const Temporal *temp, const TBox *box)
Ensure that a temporal number and a temporal box have the same span type.
Definition: temporal.c:375
bool ensure_has_X(meosType type, int16 flags)
Ensure that a MEOS type has X dimension.
Definition: temporal.c:84
tempSubtype
Enumeration that defines the temporal subtypes used in MEOS.
Definition: meos.h:150
interpType
Enumeration that defines the interpolation types used in MEOS.
Definition: meos.h:161
External API of the Mobility Engine Open Source (MEOS) library.
int32 pg_strtoint32(const char *s)
Return an input string converted to a signed 32 bit integer.
Definition: numutils.c:95
int64 TimestampTz
Definition: postgres_ext_defs.in.h:22
signed short int16
Definition: postgres_ext_defs.in.h:10
uintptr_t Datum
Definition: postgres_ext_defs.in.h:7
signed int int32
Definition: postgres_ext_defs.in.h:11
Definition: postgres_ext_defs.in.h:27
Structure to represent spatiotemporal boxes.
Definition: meos.h:134
Structure to represent sets of values.
Definition: meos.h:80
Structure to represent span sets.
Definition: meos.h:108
Structure to represent spans (a.k.a.
Definition: meos.h:94
Structure to represent temporal boxes.
Definition: meos.h:124
Structure to represent the common structure of temporal values of any temporal subtype.
Definition: meos.h:173
TBox b
Temporal box.
Definition: temporal.h:268
Span p
Span.
Definition: temporal.h:267
STBox g
Spatiotemporal box.
Definition: temporal.h:269
Structure to represent all types of bounding boxes.
Definition: temporal.h:266