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_internal.h>
45#include <meos_geo.h>
46#include "temporal/doublen.h"
47#include "temporal/meos_catalog.h"
48
49/* To avoid including pg_collation_d */
50#define DEFAULT_COLLATION_OID 100
51#define C_COLLATION_OID 950
52#define POSIX_COLLATION_OID 951
53
54#ifndef FMGR_H
55 /* To avoid including fmgr.h However this implies that the text values must
56 * be ALWAYS detoasted */
57 #define DatumGetTextP(X) ((text *) DatumGetPointer(X)) // ((text *) PG_DETOAST_DATUM(X))
58#endif /* FMGR_H */
59
63#define MEOS_EPSILON 1.0e-06
64#define MEOS_FP_EQ(A, B) (fabs((A)-(B)) <= MEOS_EPSILON)
65#define MEOS_FP_NE(A, B) (fabs((A)-(B)) > MEOS_EPSILON)
66#define MEOS_FP_LT(A, B) (((A) + MEOS_EPSILON) < (B))
67#define MEOS_FP_LE(A, B) (((A) - MEOS_EPSILON) <= (B))
68#define MEOS_FP_GT(A, B) (((A) - MEOS_EPSILON) > (B))
69#define MEOS_FP_GE(A, B) (((A) + MEOS_EPSILON) >= (B))
70
74#define DIST_EPSILON 1.0e-06
75
76#define UNUSED __attribute__((unused))
77
79#define DISCONTINUOUS true
80#define CONTINUOUS false
81
83#define ORDER true
84#define ORDER_NO false
85
87#define QUOTES true
88#define QUOTES_NO false
89
91#define SPACES true
92#define SPACES_NO false
93
95#define INVERT true
96#define INVERT_NO false
97
99#define REST_AT true
100#define REST_MINUS false
101
103#define BORDER_INC true
104#define BORDER_EXC false
105
107#define EVER true
108#define ALWAYS false
109
111#define TPOINT true
112#define TGEO false
113
115#define GET_MIN true
116#define GET_MAX false
117
119#define CROSSINGS true
120#define CROSSINGS_NO false
121
123#define BBOX_TEST true
124#define BBOX_TEST_NO false
125
127#define MERGE true
128#define MERGE_NO false
129
130#define NORMALIZE true
131#define NORMALIZE_NO false
132
134#define WITH_Z true
135#define NO_Z false
136
138#define VALUE_SEL true
139#define TIME_SEL false
140
142#define UPPER_EXC true
143#define TIME_SEL false
144
146#define UNARY_UNION true
147#define UNARY_UNION_NO false
148
150typedef enum
151{
154} SyncMode;
155
157typedef enum
158{
163
165typedef enum
166{
169 MINUS
171
172/* PostgreSQL removed pg_atoi in version 15 */
173#if POSTGRESQL_VERSION_NUMBER >= 150000
174 extern int32 pg_strtoint32(const char *s);
175#else
176 /* To avoid including <utils/builtins.h> */
177 extern int32 pg_atoi(const char *s, int size, int c);
178#endif /* POSTGRESQL_VERSION_NUMBER >= 150000 */
179
180/*****************************************************************************
181 * Additional operator strategy numbers used in the GiST and SP-GiST temporal
182 * opclasses with respect to those defined in the file stratnum.h
183 *****************************************************************************/
184
185#define RTOverBeforeStrategyNumber 28 /* for &<# */
186#define RTBeforeStrategyNumber 29 /* for <<# */
187#define RTAfterStrategyNumber 30 /* for #>> */
188#define RTOverAfterStrategyNumber 31 /* for #&> */
189#define RTOverFrontStrategyNumber 32 /* for &</ */
190#define RTFrontStrategyNumber 33 /* for <</ */
191#define RTBackStrategyNumber 34 /* for />> */
192#define RTOverBackStrategyNumber 35 /* for /&> */
193
194/*****************************************************************************
195 * Well-Known Binary (WKB)
196 *****************************************************************************/
197
198/* Data type size */
199#define MEOS_WKB_BYTE_SIZE 1
200#define MEOS_WKB_INT2_SIZE 2
201#define MEOS_WKB_INT4_SIZE 4
202#define MEOS_WKB_INT8_SIZE 8
203#define MEOS_WKB_DOUBLE_SIZE 8
204#define MEOS_WKB_DATE_SIZE 4
205#define MEOS_WKB_TIMESTAMP_SIZE 8
206
207/* Temporal subtype */
209{
213};
214
215/* Span bounds */
216#define MEOS_WKB_LOWER_INC 0x01
217#define MEOS_WKB_UPPER_INC 0x02
218
219/* Machine endianness */
220#define XDR 0 /* big endian */
221#define NDR 1 /* little endian */
222
223/* Variation flags
224 * The first byte of the variation flag depends on the type we are sending
225 * - Set types: xxxO where O states whether the set is ordered or not
226 * - Box types: xxTX where X and T state whether the corresponding dimensions
227 * are present
228 * - Temporal types: xxSS where SS correspond to the subtype
229 * and x are unused bits
230 */
231#define MEOS_WKB_ORDERED 0x01 // 1
232#define MEOS_WKB_XFLAG 0x01 // 1
233#define MEOS_WKB_TFLAG 0x02 // 2
234#define MEOS_WKB_INTERPFLAGS 0x0C // 4 + 8
235#define MEOS_WKB_ZFLAG 0x10 // 16
236#define MEOS_WKB_GEODETICFLAG 0x20 // 32
237#define MEOS_WKB_SRIDFLAG 0x40 // 64
238
239#define MEOS_WKB_GET_INTERP(flags) (((flags) & MEOS_WKB_INTERPFLAGS) >> 2)
240#define MEOS_WKB_SET_INTERP(flags, value) ((flags) = (((flags) & ~MEOS_WKB_INTERPFLAGS) | ((value & 0x0003) << 2)))
241
242// #define MEOS_WKB_GET_LINEAR(flags) ((bool) (((flags) & MEOS_WKB_LINEARFLAG)>>3))
243
244/*****************************************************************************
245 * Definitions for binning and tiling
246 *****************************************************************************/
247
248/*
249 * The default origin is Monday 2000-01-03. We don't use PG epoch since it
250 * starts on a Saturday. This makes time bins by a week more intuitive and
251 * aligns it with date_trunc.
252 */
253#define JAN_3_2000 (2 * USECS_PER_DAY)
254#define DEFAULT_TIME_ORIGIN (JAN_3_2000)
255#define DEFAULT_FLOATSPAN_ORIGIN (0.0)
256#define DEFAULT_INTSPAN_ORIGIN (0)
257#define DEFAULT_BIGINTSPAN_ORIGIN (0)
258
259/*****************************************************************************
260 * Additional struct definitions for temporal types
261 *****************************************************************************/
262
266typedef union bboxunion
267{
272
273/*****************************************************************************
274 * Miscellaneous
275 *****************************************************************************/
276
277/* Definition of output function */
278typedef char *(*outfunc)(Datum value, meosType type, int maxdd);
279
280/* Definition of qsort comparator for integers */
281typedef int (*qsort_comparator) (const void *a, const void *b);
282
283/* Definition of a variadic function type for temporal lifting */
284typedef Datum (*varfunc) (Datum, ...);
285
286/* Definition of a turning point function for a temporal and a base types */
289
290/* Definition of a turning point function for two temporal types */
293
294/*****************************************************************************
295 * fmgr macros temporal types
296 *****************************************************************************/
297
298/* doubleN */
299
300#define DatumGetDouble2P(X) ((double2 *) DatumGetPointer(X))
301#define Double2PGetDatum(X) PointerGetDatum(X)
302#define DatumGetDouble3P(X) ((double3 *) DatumGetPointer(X))
303#define Double3PGetDatum(X) PointerGetDatum(X)
304#define DatumGetDouble4P(X) ((double4 *) DatumGetPointer(X))
305#define Double4PGetDatum(X) PointerGetDatum(X)
306
307/* Temporal types */
308
309#if MEOS
310 #define DatumGetTemporalP(X) ((Temporal *) DatumGetPointer(X))
311#else
312 #define DatumGetTemporalP(X) ((Temporal *) PG_DETOAST_DATUM(X))
313#endif /* MEOS */
314
315#define PG_GETARG_TEMPORAL_P(X) ((Temporal *) PG_GETARG_VARLENA_P(X))
316#define PG_GETARG_TINSTANT_P(X) ((TInstant *) PG_GETARG_VARLENA_P(X))
317#define PG_GETARG_TSEQUENCE_P(X) ((TSequence *) PG_GETARG_VARLENA_P(X))
318#define PG_GETARG_TSEQUENCESET_P(X) ((TSequenceSet *) PG_GETARG_VARLENA_P(X))
319
320#define PG_RETURN_TEMPORAL_P(X) PG_RETURN_POINTER(X)
321#define PG_RETURN_TINSTANT_P(X) PG_RETURN_POINTER(X)
322#define PG_RETURN_TSEQUENCE_P(X) PG_RETURN_POINTER(X)
323#define PG_RETURN_TSEQUENCESET_P(X) PG_RETURN_POINTER(X)
324
325#define TemporalPGetDatum(X) PointerGetDatum(X)
326#define TInstantPGetDatum(X) PointerGetDatum(X)
327#define TSequencePGetDatum(X) PointerGetDatum(X)
328#define TSequenceSetPGetDatum(X) PointerGetDatum(X)
329
330#define DATUM_FREE(value, basetype) \
331 do { \
332 if (! basetype_byvalue(basetype)) \
333 pfree(DatumGetPointer(value)); \
334 } while (0)
335
336#define DATUM_FREE_IF_COPY(value, basetype, n) \
337 do { \
338 if (! basetype_byvalue(basetype) && DatumGetPointer(value) != PG_GETARG_POINTER(n)) \
339 pfree(DatumGetPointer(value)); \
340 } while (0)
341
348#define PG_FREE_IF_COPY_P(ptrsrc, ptrori) \
349 do { \
350 if ((Pointer) (ptrsrc) != (Pointer) (ptrori)) \
351 pfree(ptrsrc); \
352 } while (0)
353
354#define PG_DATUM_NEEDS_DETOAST(datum) \
355 (VARATT_IS_EXTENDED((datum)) || VARATT_IS_EXTERNAL((datum)) || \
356 VARATT_IS_COMPRESSED((datum)))
357
358/*****************************************************************************/
359
360/* Parameter tests */
361
362extern bool ensure_has_X(meosType type, int16 flags);
363extern bool ensure_has_Z(meosType type, int16 flags);
364extern bool ensure_has_T(meosType type, int16 flags);
365extern bool ensure_has_not_Z(meosType type, int16 flags);
366extern bool ensure_not_null(void *ptr);
367extern bool ensure_one_not_null(void *ptr1, void *ptr2);
368extern bool ensure_one_true(bool hasshift, bool haswidth);
369extern bool ensure_valid_interp(meosType temptype, interpType interp);
370extern bool ensure_continuous(const Temporal *temp);
371extern bool ensure_same_interp(const Temporal *temp1, const Temporal *temp2);
372extern bool ensure_same_continuous_interp(int16 flags1, int16 flags2);
373extern bool ensure_linear_interp(int16 flags);
374extern bool ensure_nonlinear_interp(int16 flags);
375extern bool ensure_common_dimension(int16 flags1, int16 flags2);
376extern bool ensure_temporal_isof_type(const Temporal *temp, meosType type);
377extern bool ensure_temporal_isof_basetype(const Temporal *temp,
378 meosType basetype);
379extern bool ensure_temporal_isof_subtype(const Temporal *temp,
380 tempSubtype type);
381extern bool ensure_same_temporal_type(const Temporal *temp1,
382 const Temporal *temp2);
383
384extern bool ensure_valid_tnumber_numspan(const Temporal *temp, const Span *s);
385extern bool ensure_valid_tnumber_numspanset(const Temporal *temp,
386 const SpanSet *ss);
387extern bool ensure_valid_tnumber_tbox(const Temporal *temp, const TBox *box);
388extern bool ensure_valid_temporal_set(const Temporal *temp, const Set *s);
389extern bool ensure_valid_temporal_temporal(const Temporal *temp1, const Temporal *temp2);
390extern bool ensure_valid_tnumber_tnumber(const Temporal *temp1, const Temporal *temp2);
391extern bool ensure_not_negative(int i);
392extern bool ensure_positive(int i);
393extern bool not_negative_datum(Datum size, meosType basetype);
394extern bool ensure_not_negative_datum(Datum size, meosType basetype);
395extern bool positive_datum(Datum size, meosType basetype);
396extern bool ensure_positive_datum(Datum size, meosType basetype);
397extern bool ensure_valid_day_duration(const Interval *duration);
398extern bool positive_duration(const Interval *duration);
399extern bool ensure_positive_duration(const Interval *duration);
400
401/* General functions */
402
403extern void *temporal_bbox_ptr(const Temporal *temp);
404
405extern bool intersection_temporal_temporal(const Temporal *temp1,
406const Temporal *temp2, SyncMode mode, Temporal **inter1, Temporal **inter2);
407
408/* Version functions */
409
410extern char *mobilitydb_version(void);
411extern char *mobilitydb_full_version(void);
412
413/* Transformations */
414
415extern datum_func2 round_fn(meosType basetype);
416
417/* Restriction functions */
418
419extern bool temporal_bbox_restrict_value(const Temporal *temp, Datum value);
420
421/*****************************************************************************/
422
423#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:151
@ SYNCHRONIZE_CROSS
Definition: temporal.h:153
@ SYNCHRONIZE_NOCROSS
Definition: temporal.h:152
MEOS_WKB_TSUBTYPE
Definition: temporal.h:209
@ MEOS_WKB_TSEQUENCESET
temporal sequence set subtype
Definition: temporal.h:212
@ MEOS_WKB_TSEQUENCE
temporal sequence subtype
Definition: temporal.h:211
@ MEOS_WKB_TINSTANT
temporal instant subtype
Definition: temporal.h:210
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:287
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:281
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:284
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:291
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
bool ensure_temporal_isof_subtype(const Temporal *temp, tempSubtype type)
Ensure that a temporal value is of a given subtype.
Definition: temporal.c:323
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:158
@ TSPATIALTYPE
Definition: temporal.h:161
@ TNUMBERTYPE
Definition: temporal.h:160
@ TEMPORALTYPE
Definition: temporal.h:159
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
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:166
@ UNION
Definition: temporal.h:167
@ INTER
Definition: temporal.h:168
@ MINUS
Definition: temporal.h:169
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.
Datum(* datum_func2)(Datum, Datum)
Definition: meos_internal.h:729
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:269
Span p
Span.
Definition: temporal.h:268
STBox g
Spatiotemporal box.
Definition: temporal.h:270
Structure to represent all types of bounding boxes.
Definition: temporal.h:267