MobilityDB  1.0
tbox.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * This MobilityDB code is provided under The PostgreSQL License.
4  *
5  * Copyright (c) 2016-2021, Université libre de Bruxelles and MobilityDB
6  * contributors
7  *
8  * MobilityDB includes portions of PostGIS version 3 source code released
9  * under the GNU General Public License (GPLv2 or later).
10  * Copyright (c) 2001-2021, PostGIS contributors
11  *
12  * Permission to use, copy, modify, and distribute this software and its
13  * documentation for any purpose, without fee, and without a written
14  * agreement is hereby granted, provided that the above copyright notice and
15  * this paragraph and the following two paragraphs appear in all copies.
16  *
17  * IN NO EVENT SHALL UNIVERSITE LIBRE DE BRUXELLES BE LIABLE TO ANY PARTY FOR
18  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
19  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
20  * EVEN IF UNIVERSITE LIBRE DE BRUXELLES HAS BEEN ADVISED OF THE POSSIBILITY
21  * OF SUCH DAMAGE.
22  *
23  * UNIVERSITE LIBRE DE BRUXELLES SPECIFICALLY DISCLAIMS ANY WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON
26  * AN "AS IS" BASIS, AND UNIVERSITE LIBRE DE BRUXELLES HAS NO OBLIGATIONS TO
27  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
28  *
29  *****************************************************************************/
30 
36 #ifndef __TBOX_H__
37 #define __TBOX_H__
38 
39 #include <postgres.h>
40 #include <catalog/pg_type.h>
41 #include <utils/rangetypes.h>
42 #if POSTGRESQL_VERSION_NUMBER < 110000
43 #include <utils/timestamp.h>
44 #endif
45 
46 #include "timetypes.h"
47 
48 /*****************************************************************************/
49 
53 typedef struct
54 {
55  double xmin;
56  double xmax;
57  TimestampTz tmin;
58  TimestampTz tmax;
59  int16 flags;
60 } TBOX;
61 
62 /* fmgr macros temporal types */
63 
64 #define DatumGetTboxP(X) ((TBOX *) DatumGetPointer(X))
65 #define TboxPGetDatum(X) PointerGetDatum(X)
66 #define PG_GETARG_TBOX_P(n) DatumGetTboxP(PG_GETARG_DATUM(n))
67 #define PG_RETURN_TBOX_P(x) return TboxPGetDatum(x)
68 
69 /* Miscellaneous functions */
70 
71 extern TBOX *tbox_make(bool hasx, bool hast, double xmin, double xmax,
72  TimestampTz tmin, TimestampTz tmax);
73 extern void tbox_set(TBOX *box, bool hasx, bool hast, double xmin,
74  double xmax, TimestampTz tmin, TimestampTz tmax);
75 extern TBOX *tbox_copy(const TBOX *box);
76 extern void tbox_expand(TBOX *box1, const TBOX *box2);
77 extern void tbox_shift_tscale(TBOX *box, const Interval *start,
78  const Interval *duration);
79 
80 /* Parameter tests */
81 
82 extern void ensure_has_X_tbox(const TBOX *box);
83 extern void ensure_has_T_tbox(const TBOX *box);
84 extern void ensure_same_dimensionality_tbox(const TBOX *box1, const TBOX *box2);
85 
86 /* Input/output functions */
87 
88 extern Datum tbox_in(PG_FUNCTION_ARGS);
89 extern Datum tbox_out(PG_FUNCTION_ARGS);
90 
91 /* Constructor functions */
92 
93 extern Datum tbox_constructor(PG_FUNCTION_ARGS);
94 extern Datum tbox_constructor_t(PG_FUNCTION_ARGS);
95 
96 /* Casting */
97 
98 extern Datum tbox_to_floatrange(PG_FUNCTION_ARGS);
99 extern Datum tbox_to_period(PG_FUNCTION_ARGS);
100 
101 /* Transform a type to a TBOX */
102 
103 extern Datum int_to_tbox(PG_FUNCTION_ARGS);
104 extern Datum float_to_tbox(PG_FUNCTION_ARGS);
105 extern Datum number_to_tbox(PG_FUNCTION_ARGS);
106 extern Datum range_to_tbox(PG_FUNCTION_ARGS);
107 extern Datum timestamp_to_tbox(PG_FUNCTION_ARGS);
108 extern Datum period_to_tbox(PG_FUNCTION_ARGS);
109 extern Datum timestampset_to_tbox(PG_FUNCTION_ARGS);
110 extern Datum periodset_to_tbox(PG_FUNCTION_ARGS);
111 extern Datum int_timestamp_to_tbox(PG_FUNCTION_ARGS);
112 extern Datum float_timestamp_to_tbox(PG_FUNCTION_ARGS);
113 extern Datum int_period_to_tbox(PG_FUNCTION_ARGS);
114 extern Datum float_period_to_tbox(PG_FUNCTION_ARGS);
115 extern Datum range_timestamp_to_tbox(PG_FUNCTION_ARGS);
116 extern Datum range_period_to_tbox(PG_FUNCTION_ARGS);
117 
118 extern void number_to_box(TBOX *box, Datum value, Oid basetypid);
119 extern void range_to_tbox_internal(TBOX *box, const RangeType *r);
120 extern void int_to_tbox_internal(TBOX *box, int i);
121 extern void float_to_tbox_internal(TBOX *box, double d);
122 extern void timestamp_to_tbox_internal(TBOX *box, TimestampTz t);
123 extern void timestampset_to_tbox_internal(TBOX *box, const TimestampSet *ts);
124 extern void period_to_tbox_internal(TBOX *box, const Period *p);
125 extern void periodset_to_tbox_internal(TBOX *box, const PeriodSet *ps);
126 
127 /* Accessor functions */
128 
129 extern Datum tbox_xmin(PG_FUNCTION_ARGS);
130 extern Datum tbox_xmax(PG_FUNCTION_ARGS);
131 extern Datum tbox_tmin(PG_FUNCTION_ARGS);
132 extern Datum tbox_tmax(PG_FUNCTION_ARGS);
133 
134 /* Casting */
135 
136 extern Datum tbox_expand_value(PG_FUNCTION_ARGS);
137 extern Datum tbox_expand_temporal(PG_FUNCTION_ARGS);
138 extern Datum tbox_set_precision(PG_FUNCTION_ARGS);
139 
140 /* Topological functions */
141 
142 extern Datum contains_tbox_tbox(PG_FUNCTION_ARGS);
143 extern Datum contained_tbox_tbox(PG_FUNCTION_ARGS);
144 extern Datum overlaps_tbox_tbox(PG_FUNCTION_ARGS);
145 extern Datum same_tbox_tbox(PG_FUNCTION_ARGS);
146 extern Datum adjacent_tbox_tbox(PG_FUNCTION_ARGS);
147 
148 extern bool contains_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
149 extern bool contained_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
150 extern bool overlaps_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
151 extern bool same_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
152 extern bool adjacent_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
153 
154 /* Position functions */
155 
156 extern Datum left_tbox_tbox(PG_FUNCTION_ARGS);
157 extern Datum overleft_tbox_tbox(PG_FUNCTION_ARGS);
158 extern Datum right_tbox_tbox(PG_FUNCTION_ARGS);
159 extern Datum overright_tbox_tbox(PG_FUNCTION_ARGS);
160 extern Datum before_tbox_tbox(PG_FUNCTION_ARGS);
161 extern Datum overbefore_tbox_tbox(PG_FUNCTION_ARGS);
162 extern Datum after_tbox_tbox(PG_FUNCTION_ARGS);
163 extern Datum overafter_tbox_tbox(PG_FUNCTION_ARGS);
164 
165 extern bool left_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
166 extern bool overleft_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
167 extern bool right_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
168 extern bool overright_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
169 extern bool before_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
170 extern bool overbefore_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
171 extern bool after_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
172 extern bool overafter_tbox_tbox_internal(const TBOX *box1, const TBOX *box2);
173 
174 /* Set functions */
175 
176 extern Datum tbox_union(PG_FUNCTION_ARGS);
177 extern Datum tbox_intersection(PG_FUNCTION_ARGS);
178 
179 extern TBOX *tbox_union_internal(const TBOX *box1, const TBOX *box2);
180 extern TBOX *tbox_minus_internal(const TBOX *box1, const TBOX *box2);
181 extern TBOX *tbox_intersection_internal(const TBOX *box1, const TBOX *box2);
182 
183 /* Comparison functions */
184 
185 extern Datum tbox_cmp(PG_FUNCTION_ARGS);
186 extern Datum tbox_lt(PG_FUNCTION_ARGS);
187 extern Datum tbox_le(PG_FUNCTION_ARGS);
188 extern Datum tbox_gt(PG_FUNCTION_ARGS);
189 extern Datum tbox_ge(PG_FUNCTION_ARGS);
190 extern Datum tbox_eq(PG_FUNCTION_ARGS);
191 extern Datum tbox_ne(PG_FUNCTION_ARGS);
192 
193 extern int tbox_cmp_internal(const TBOX *box1, const TBOX *box2);
194 extern bool tbox_eq_internal(const TBOX *box1, const TBOX *box2);
195 
196 /*****************************************************************************/
197 
198 #endif
TBOX * tbox_union_internal(const TBOX *box1, const TBOX *box2)
Returns the union of the temporal boxes (internal function)
Definition: tbox.c:1204
void range_to_tbox_internal(TBOX *box, const RangeType *r)
Transform the range to a temporal box (internal function)
Definition: tbox.c:401
Structure to represent periods.
Definition: timetypes.h:52
void ensure_has_X_tbox(const TBOX *box)
Ensure that the temporal box has X values.
Definition: tbox.c:157
bool before_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box is strictly before the second one (internal function) ...
Definition: tbox.c:1104
Datum before_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box is strictly before the second one.
Definition: tbox.c:1116
Datum tbox_intersection(PG_FUNCTION_ARGS)
Returns the intersection of the temporal boxes.
Definition: tbox.c:1279
Datum tbox_to_floatrange(PG_FUNCTION_ARGS)
Cast the temporal box value as a float range value.
Definition: tbox.c:644
Datum int_timestamp_to_tbox(PG_FUNCTION_ARGS)
Transform the integer and the timestamp to a temporal box.
Definition: tbox.c:540
Datum adjacent_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the temporal boxes are adjacent.
Definition: tbox.c:992
void tbox_set(TBOX *box, bool hasx, bool hast, double xmin, double xmax, TimestampTz tmin, TimestampTz tmax)
Set the temporal box from the argument values.
Definition: tbox.c:75
TBOX * tbox_make(bool hasx, bool hast, double xmin, double xmax, TimestampTz tmin, TimestampTz tmax)
Constructs a newly allocated temporal box.
Definition: tbox.c:62
Datum tbox_gt(PG_FUNCTION_ARGS)
Returns true if the first temporal box value is greater than the second one.
Definition: tbox.c:1461
bool right_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box is strictly to the right of the second one (internal function)...
Definition: tbox.c:1056
bool tbox_eq_internal(const TBOX *box1, const TBOX *box2)
Returns true if the two temporal boxes are equal (internal function)
Definition: tbox.c:1476
Datum float_to_tbox(PG_FUNCTION_ARGS)
Transform the float to a temporal box.
Definition: tbox.c:375
Datum tbox_eq(PG_FUNCTION_ARGS)
Returns true if the two temporal boxes are equal.
Definition: tbox.c:1493
Datum float_period_to_tbox(PG_FUNCTION_ARGS)
Transform the float and the period to a temporal box.
Definition: tbox.c:580
Datum timestampset_to_tbox(PG_FUNCTION_ARGS)
Transform the period set to a temporal box.
Definition: tbox.c:472
Datum same_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the temporal boxes are equal on the common dimensions.
Definition: tbox.c:955
int16 flags
flags
Definition: tbox.h:59
bool after_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box is strictly after the second one (internal function) ...
Definition: tbox.c:1152
Datum overafter_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box does not extend before the second one.
Definition: tbox.c:1188
Datum overlaps_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the temporal boxes overlap.
Definition: tbox.c:927
Datum tbox_expand_value(PG_FUNCTION_ARGS)
Expand the value dimension of the temporal box with the double value.
Definition: tbox.c:771
bool contained_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box is contained by the second one (internal function) ...
Definition: tbox.c:889
void ensure_has_T_tbox(const TBOX *box)
Ensure that the temporal box has T values.
Definition: tbox.c:168
Datum overleft_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box does not extend to the right of the second one...
Definition: tbox.c:1044
bool overleft_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box does not extend to the right of the second one (internal funct...
Definition: tbox.c:1032
bool overright_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box does not extend to the left of the second one (internal functi...
Definition: tbox.c:1080
Datum number_to_tbox(PG_FUNCTION_ARGS)
void timestamp_to_tbox_internal(TBOX *box, TimestampTz t)
Transform the timestamp to a temporal box (internal function)
Definition: tbox.c:434
Datum tbox_lt(PG_FUNCTION_ARGS)
Returns true if the first temporal box value is less than the second one.
Definition: tbox.c:1420
Datum tbox_tmax(PG_FUNCTION_ARGS)
Returns the maximum timestamp of the temporal box value.
Definition: tbox.c:740
void timestampset_to_tbox_internal(TBOX *box, const TimestampSet *ts)
Transform the period set to a temporal box (internal function)
Definition: tbox.c:458
Datum overright_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box does not extend to the left of the second one.
Definition: tbox.c:1092
Datum tbox_xmax(PG_FUNCTION_ARGS)
Returns the maximum X value of the temporal box value.
Definition: tbox.c:714
void float_to_tbox_internal(TBOX *box, double d)
Transform the float to a temporal box (internal function)
Definition: tbox.c:363
void number_to_box(TBOX *box, Datum value, Oid basetypid)
Transform the value to a temporal box (internal function only)
Definition: tbox.c:324
void periodset_to_tbox_internal(TBOX *box, const PeriodSet *ps)
Transform the period set to a temporal box (internal function)
Definition: tbox.c:510
Datum tbox_tmin(PG_FUNCTION_ARGS)
Returns the minimum timestamp of the temporal box value.
Definition: tbox.c:727
Datum timestamp_to_tbox(PG_FUNCTION_ARGS)
Transform the timestamp to a temporal box.
Definition: tbox.c:446
bool overafter_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box does not extend before the second one (internal function) ...
Definition: tbox.c:1176
Structure to represent period sets.
Definition: timetypes.h:84
Datum tbox_le(PG_FUNCTION_ARGS)
Returns true if the first temporal box value is less than or equal to the second one.
Definition: tbox.c:1434
Datum period_to_tbox(PG_FUNCTION_ARGS)
Transform the period to a temporal box.
Definition: tbox.c:498
void ensure_same_dimensionality_tbox(const TBOX *box1, const TBOX *box2)
Ensure that the temporal boxes have the same dimensionality.
Definition: tbox.c:179
TimestampTz tmax
maximum timestamp
Definition: tbox.h:58
Datum float_timestamp_to_tbox(PG_FUNCTION_ARGS)
Transform the float and the timestamp to a temporal box.
Definition: tbox.c:553
bool overbefore_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box does not extend after the second one (internal function) ...
Definition: tbox.c:1128
void period_to_tbox_internal(TBOX *box, const Period *p)
Transform the period to a temporal box (internal function)
Definition: tbox.c:485
double xmin
minimum number value
Definition: tbox.h:55
Structure to represent temporal boxes.
Definition: tbox.h:53
Datum tbox_in(PG_FUNCTION_ARGS)
Input function for temporal boxes.
Definition: tbox.c:204
bool same_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the temporal boxes are equal on the common dimensions (internal function) ...
Definition: tbox.c:939
Datum tbox_set_precision(PG_FUNCTION_ARGS)
Set the precision of the value dimension of the temporal box to the number of decimal places...
Definition: tbox.c:812
TBOX * tbox_copy(const TBOX *box)
Returns a copy of the temporal box value.
Definition: tbox.c:111
Functions for time types based on TimestampTz, that is, TimestampSet, Period, and PeriodSet...
Structure to represent timestamp sets.
Definition: timetypes.h:73
bool adjacent_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the temporal boxes are adjacent (internal function)
Definition: tbox.c:967
TBOX * tbox_intersection_internal(const TBOX *box1, const TBOX *box2)
Returns the intersection of the temporal boxes (internal function)
Definition: tbox.c:1247
Datum tbox_expand_temporal(PG_FUNCTION_ARGS)
Expand the time dimension of the temporal box with the interval value.
Definition: tbox.c:799
Datum after_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box is strictly after the second one.
Definition: tbox.c:1164
bool contains_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box contains the second one (internal function) ...
Definition: tbox.c:861
Datum range_to_tbox(PG_FUNCTION_ARGS)
Transform the range to a temporal box.
Definition: tbox.c:414
bool overlaps_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the temporal boxes overlap (internal function)
Definition: tbox.c:911
Datum range_timestamp_to_tbox(PG_FUNCTION_ARGS)
Transform the range and the timestamp to a temporal box.
Definition: tbox.c:593
Datum tbox_constructor(PG_FUNCTION_ARGS)
Construct a temporal box value from the arguments.
Definition: tbox.c:276
void tbox_expand(TBOX *box1, const TBOX *box2)
Expand the first temporal box value with the second one.
Definition: tbox.c:122
Datum tbox_ne(PG_FUNCTION_ARGS)
Returns true if the two temporal boxes are different.
Definition: tbox.c:1505
Datum tbox_xmin(PG_FUNCTION_ARGS)
Returns the minimum X value of the temporal box value.
Definition: tbox.c:701
Datum tbox_ge(PG_FUNCTION_ARGS)
Returns true if the first temporal box value is greater than or equal to the second one...
Definition: tbox.c:1448
Datum tbox_to_period(PG_FUNCTION_ARGS)
Cast the temporal box value as a period value.
Definition: tbox.c:659
void tbox_shift_tscale(TBOX *box, const Interval *start, const Interval *duration)
Shift and/or scale the time span of the temporal box by the interval.
Definition: tbox.c:134
Datum tbox_cmp(PG_FUNCTION_ARGS)
Returns -1, 0, or 1 depending on whether the first temporal box value is less than, equal, or greater than the second one.
Definition: tbox.c:1407
int tbox_cmp_internal(const TBOX *box1, const TBOX *box2)
Returns -1, 0, or 1 depending on whether the first temporal box value is less than, equal to, or greater than the second one (internal function).
Definition: tbox.c:1360
Datum right_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box is strictly to the right of the second one.
Definition: tbox.c:1068
double xmax
maximum number value
Definition: tbox.h:56
Datum int_period_to_tbox(PG_FUNCTION_ARGS)
Transform the integer and the period to a temporal box.
Definition: tbox.c:566
Datum tbox_out(PG_FUNCTION_ARGS)
Output function for temporal boxes.
Definition: tbox.c:260
Datum overbefore_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box does not extend after the second one.
Definition: tbox.c:1140
Datum left_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box is strictly to the left of the second one. ...
Definition: tbox.c:1020
Datum contained_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box is contained by the second one.
Definition: tbox.c:899
TimestampTz tmin
minimum timestamp
Definition: tbox.h:57
Datum int_to_tbox(PG_FUNCTION_ARGS)
Transform the integer to a temporal box.
Definition: tbox.c:351
bool left_tbox_tbox_internal(const TBOX *box1, const TBOX *box2)
Returns true if the first temporal box is strictly to the left of the second one (internal function) ...
Definition: tbox.c:1008
Datum tbox_union(PG_FUNCTION_ARGS)
Returns the union of the temporal boxes.
Definition: tbox.c:1234
Datum range_period_to_tbox(PG_FUNCTION_ARGS)
Transform the range and the period to a temporal box.
Definition: tbox.c:617
Datum contains_tbox_tbox(PG_FUNCTION_ARGS)
Returns true if the first temporal box contains the second one.
Definition: tbox.c:877
Datum tbox_constructor_t(PG_FUNCTION_ARGS)
Construct a temporal box value from the timestamps.
Definition: tbox.c:306
Datum periodset_to_tbox(PG_FUNCTION_ARGS)
Transform the period set to a temporal box.
Definition: tbox.c:524
void int_to_tbox_internal(TBOX *box, int i)
Transform the integer to a temporal box (internal function)
Definition: tbox.c:339
TBOX * tbox_minus_internal(const TBOX *box1, const TBOX *box2)