forked from timescale/timescaledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_time_utils.c
362 lines (308 loc) · 13.5 KB
/
test_time_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <catalog/pg_type.h>
#include <utils/date.h>
#include <fmgr.h>
#include <funcapi.h>
#include <time_utils.h>
#include <utils.h>
#include "test_utils.h"
/*
* Functions to show TimescaleDB-specific limits of timestamps and dates:
*/
/*
* TIMESTAMP WITH TIME ZONE
*/
TS_FUNCTION_INFO_V1(ts_timestamptz_pg_min);
Datum
ts_timestamptz_pg_min(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMPTZ(MIN_TIMESTAMP);
}
TS_FUNCTION_INFO_V1(ts_timestamptz_pg_end);
Datum
ts_timestamptz_pg_end(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMPTZ(END_TIMESTAMP);
}
TS_FUNCTION_INFO_V1(ts_timestamptz_min);
Datum
ts_timestamptz_min(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMPTZ(TS_TIMESTAMP_MIN);
}
TS_FUNCTION_INFO_V1(ts_timestamptz_end);
Datum
ts_timestamptz_end(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMPTZ(TS_TIMESTAMP_END);
}
TS_FUNCTION_INFO_V1(ts_timestamptz_internal_min);
Datum
ts_timestamptz_internal_min(PG_FUNCTION_ARGS)
{
PG_RETURN_INT64(TS_TIMESTAMP_INTERNAL_MIN);
}
TS_FUNCTION_INFO_V1(ts_timestamptz_internal_end);
Datum
ts_timestamptz_internal_end(PG_FUNCTION_ARGS)
{
PG_RETURN_INT64(TS_TIMESTAMP_INTERNAL_END);
}
/*
* TIMESTAMP
*/
TS_FUNCTION_INFO_V1(ts_timestamp_pg_min);
Datum
ts_timestamp_pg_min(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMP(MIN_TIMESTAMP);
}
TS_FUNCTION_INFO_V1(ts_timestamp_pg_end);
Datum
ts_timestamp_pg_end(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMP(END_TIMESTAMP);
}
TS_FUNCTION_INFO_V1(ts_timestamp_min);
Datum
ts_timestamp_min(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMP(TS_TIMESTAMP_MIN);
}
TS_FUNCTION_INFO_V1(ts_timestamp_end);
Datum
ts_timestamp_end(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMP(TS_TIMESTAMP_END);
}
TS_FUNCTION_INFO_V1(ts_timestamp_internal_min);
Datum
ts_timestamp_internal_min(PG_FUNCTION_ARGS)
{
PG_RETURN_INT64(TS_TIMESTAMP_INTERNAL_MIN);
}
TS_FUNCTION_INFO_V1(ts_timestamp_internal_end);
Datum
ts_timestamp_internal_end(PG_FUNCTION_ARGS)
{
PG_RETURN_INT64(TS_TIMESTAMP_INTERNAL_END);
}
/*
* DATE
*/
TS_FUNCTION_INFO_V1(ts_date_pg_min);
Datum
ts_date_pg_min(PG_FUNCTION_ARGS)
{
PG_RETURN_DATEADT(DATETIME_MIN_JULIAN - POSTGRES_EPOCH_JDATE);
}
TS_FUNCTION_INFO_V1(ts_date_pg_end);
Datum
ts_date_pg_end(PG_FUNCTION_ARGS)
{
PG_RETURN_DATEADT(DATE_END_JULIAN - POSTGRES_EPOCH_JDATE);
}
TS_FUNCTION_INFO_V1(ts_date_min);
Datum
ts_date_min(PG_FUNCTION_ARGS)
{
PG_RETURN_DATEADT(TS_DATE_MIN);
}
TS_FUNCTION_INFO_V1(ts_date_end);
Datum
ts_date_end(PG_FUNCTION_ARGS)
{
PG_RETURN_DATEADT(TS_DATE_END);
}
TS_FUNCTION_INFO_V1(ts_date_internal_min);
Datum
ts_date_internal_min(PG_FUNCTION_ARGS)
{
PG_RETURN_INT64(TS_DATE_INTERNAL_MIN);
}
TS_FUNCTION_INFO_V1(ts_date_internal_end);
Datum
ts_date_internal_end(PG_FUNCTION_ARGS)
{
PG_RETURN_INT64(TS_DATE_INTERNAL_END);
}
TS_FUNCTION_INFO_V1(ts_test_time_utils);
Datum
ts_test_time_utils(PG_FUNCTION_ARGS)
{
TestAssertInt64Eq(ts_time_get_min(INT8OID), PG_INT64_MIN);
TestAssertInt64Eq(ts_time_get_max(INT8OID), PG_INT64_MAX);
TestAssertInt64Eq(ts_time_get_end_or_max(INT8OID), PG_INT64_MAX);
TestEnsureError(ts_time_get_end(INT8OID));
TestEnsureError(ts_time_get_nobegin(INT8OID));
TestEnsureError(ts_time_get_noend(INT8OID));
TestAssertInt64Eq(DatumGetInt64(ts_time_datum_get_nobegin_or_min(INT8OID)), PG_INT64_MIN);
TestAssertInt64Eq(DatumGetInt64(ts_time_datum_get_min(INT8OID)), PG_INT64_MIN);
TestAssertInt64Eq(DatumGetInt64(ts_time_datum_get_max(INT8OID)), PG_INT64_MAX);
TestEnsureError(ts_time_datum_get_end(INT8OID));
TestEnsureError(ts_time_datum_get_nobegin(INT8OID));
TestEnsureError(ts_time_datum_get_noend(INT8OID));
TestAssertInt64Eq(ts_time_get_min(INT4OID), PG_INT32_MIN);
TestAssertInt64Eq(ts_time_get_max(INT4OID), PG_INT32_MAX);
TestAssertInt64Eq(ts_time_get_end_or_max(INT4OID), PG_INT32_MAX);
TestEnsureError(ts_time_get_end(INT4OID));
TestEnsureError(ts_time_get_nobegin(INT4OID));
TestEnsureError(ts_time_get_noend(INT4OID));
TestAssertInt64Eq(DatumGetInt32(ts_time_datum_get_nobegin_or_min(INT4OID)), PG_INT32_MIN);
TestAssertInt64Eq(DatumGetInt32(ts_time_datum_get_min(INT4OID)), PG_INT32_MIN);
TestAssertInt64Eq(DatumGetInt32(ts_time_datum_get_max(INT4OID)), PG_INT32_MAX);
TestEnsureError(ts_time_datum_get_end(INT4OID));
TestEnsureError(ts_time_datum_get_nobegin(INT4OID));
TestEnsureError(ts_time_datum_get_noend(INT4OID));
TestAssertInt64Eq(ts_time_get_min(INT2OID), PG_INT16_MIN);
TestAssertInt64Eq(ts_time_get_max(INT2OID), PG_INT16_MAX);
TestAssertInt64Eq(ts_time_get_end_or_max(INT2OID), PG_INT16_MAX);
TestEnsureError(ts_time_get_end(INT2OID));
TestEnsureError(ts_time_get_nobegin(INT2OID));
TestEnsureError(ts_time_get_noend(INT2OID));
TestAssertInt64Eq(DatumGetInt16(ts_time_datum_get_nobegin_or_min(INT2OID)), PG_INT16_MIN);
TestAssertInt64Eq(DatumGetInt16(ts_time_datum_get_min(INT2OID)), PG_INT16_MIN);
TestAssertInt64Eq(DatumGetInt16(ts_time_datum_get_max(INT2OID)), PG_INT16_MAX);
TestEnsureError(ts_time_datum_get_end(INT2OID));
TestEnsureError(ts_time_datum_get_nobegin(INT2OID));
TestEnsureError(ts_time_datum_get_noend(INT2OID));
TestAssertInt64Eq(ts_time_get_min(TIMESTAMPOID), TS_TIMESTAMP_INTERNAL_MIN);
TestAssertInt64Eq(ts_time_get_max(TIMESTAMPOID), TS_TIMESTAMP_INTERNAL_MAX);
TestAssertInt64Eq(ts_time_get_end(TIMESTAMPOID), TS_TIMESTAMP_INTERNAL_END);
TestAssertInt64Eq(ts_time_get_end_or_max(TIMESTAMPOID), TS_TIMESTAMP_INTERNAL_END);
TestAssertInt64Eq(ts_time_get_nobegin(TIMESTAMPOID), TS_TIME_NOBEGIN);
TestAssertInt64Eq(ts_time_get_noend(TIMESTAMPOID), TS_TIME_NOEND);
TestAssertInt64Eq(DatumGetTimestamp(ts_time_datum_get_nobegin_or_min(TIMESTAMPOID)),
DT_NOBEGIN);
TestAssertInt64Eq(DatumGetTimestamp(ts_time_datum_get_min(TIMESTAMPOID)), TS_TIMESTAMP_MIN);
TestAssertInt64Eq(DatumGetTimestamp(ts_time_datum_get_max(TIMESTAMPOID)), TS_TIMESTAMP_MAX);
TestAssertInt64Eq(DatumGetTimestamp(ts_time_datum_get_end(TIMESTAMPOID)), TS_TIMESTAMP_END);
TestAssertInt64Eq(DatumGetTimestamp(ts_time_datum_get_nobegin(TIMESTAMPOID)), DT_NOBEGIN);
TestAssertInt64Eq(DatumGetTimestamp(ts_time_datum_get_noend(TIMESTAMPOID)), DT_NOEND);
TestAssertInt64Eq(ts_time_get_min(TIMESTAMPTZOID), TS_TIMESTAMP_INTERNAL_MIN);
TestAssertInt64Eq(ts_time_get_max(TIMESTAMPTZOID), TS_TIMESTAMP_INTERNAL_MAX);
TestAssertInt64Eq(ts_time_get_end(TIMESTAMPTZOID), TS_TIMESTAMP_INTERNAL_END);
TestAssertInt64Eq(ts_time_get_end_or_max(TIMESTAMPTZOID), TS_TIMESTAMP_INTERNAL_END);
TestAssertInt64Eq(ts_time_get_nobegin(TIMESTAMPTZOID), TS_TIME_NOBEGIN);
TestAssertInt64Eq(ts_time_get_noend(TIMESTAMPTZOID), TS_TIME_NOEND);
TestAssertInt64Eq(DatumGetTimestampTz(ts_time_datum_get_nobegin_or_min(TIMESTAMPTZOID)),
DT_NOBEGIN);
TestAssertInt64Eq(DatumGetTimestampTz(ts_time_datum_get_min(TIMESTAMPTZOID)), TS_TIMESTAMP_MIN);
TestAssertInt64Eq(DatumGetTimestampTz(ts_time_datum_get_max(TIMESTAMPTZOID)), TS_TIMESTAMP_MAX);
TestAssertInt64Eq(DatumGetTimestampTz(ts_time_datum_get_end(TIMESTAMPTZOID)), TS_TIMESTAMP_END);
TestAssertInt64Eq(DatumGetTimestampTz(ts_time_datum_get_nobegin(TIMESTAMPTZOID)), DT_NOBEGIN);
TestAssertInt64Eq(DatumGetTimestampTz(ts_time_datum_get_noend(TIMESTAMPTZOID)), DT_NOEND);
TestAssertInt64Eq(ts_time_get_min(DATEOID), TS_DATE_INTERNAL_MIN);
TestAssertInt64Eq(ts_time_get_max(DATEOID), TS_DATE_INTERNAL_MAX);
TestAssertInt64Eq(ts_time_get_end(DATEOID), TS_DATE_INTERNAL_END);
TestAssertInt64Eq(ts_time_get_end_or_max(DATEOID), TS_DATE_INTERNAL_END);
TestAssertInt64Eq(ts_time_get_nobegin(DATEOID), TS_TIME_NOBEGIN);
TestAssertInt64Eq(ts_time_get_noend(DATEOID), TS_TIME_NOEND);
TestAssertInt64Eq(DatumGetDateADT(ts_time_datum_get_nobegin_or_min(DATEOID)), DATEVAL_NOBEGIN);
TestAssertInt64Eq(DatumGetDateADT(ts_time_datum_get_min(DATEOID)), TS_DATE_MIN);
TestAssertInt64Eq(DatumGetDateADT(ts_time_datum_get_max(DATEOID)), TS_DATE_MAX);
TestAssertInt64Eq(DatumGetDateADT(ts_time_datum_get_end(DATEOID)), TS_DATE_END);
TestAssertInt64Eq(DatumGetDateADT(ts_time_datum_get_nobegin(DATEOID)), DATEVAL_NOBEGIN);
TestAssertInt64Eq(DatumGetDateADT(ts_time_datum_get_noend(DATEOID)), DATEVAL_NOEND);
/* Test boundary routines with unsupported time type */
TestEnsureError(ts_time_get_min(NUMERICOID));
TestEnsureError(ts_time_get_max(NUMERICOID));
TestEnsureError(ts_time_get_end(NUMERICOID));
TestEnsureError(ts_time_get_nobegin(NUMERICOID));
TestEnsureError(ts_time_get_noend(NUMERICOID));
TestEnsureError(ts_time_datum_get_nobegin_or_min(NUMERICOID));
TestEnsureError(ts_time_datum_get_min(NUMERICOID));
TestEnsureError(ts_time_datum_get_max(NUMERICOID));
TestEnsureError(ts_time_datum_get_end(NUMERICOID));
TestEnsureError(ts_time_datum_get_nobegin(NUMERICOID));
TestEnsureError(ts_time_datum_get_noend(NUMERICOID));
/* Test conversion of min, end, nobegin, and noend between native and
* internal (Unix) time */
TestAssertInt64Eq(ts_time_value_to_internal(ts_time_datum_get_min(INT2OID), INT2OID),
ts_time_get_min(INT2OID));
TestAssertInt64Eq(ts_time_value_to_internal(ts_time_datum_get_min(INT4OID), INT4OID),
ts_time_get_min(INT4OID));
TestAssertInt64Eq(ts_time_value_to_internal(ts_time_datum_get_min(INT8OID), INT8OID),
ts_time_get_min(INT8OID));
TestAssertInt64Eq(ts_time_value_to_internal(ts_time_datum_get_min(DATEOID), DATEOID),
ts_time_get_min(DATEOID));
TestAssertInt64Eq(ts_time_value_to_internal(ts_time_datum_get_min(TIMESTAMPOID), TIMESTAMPOID),
ts_time_get_min(TIMESTAMPOID));
TestAssertInt64Eq(ts_time_value_to_internal(ts_time_datum_get_min(TIMESTAMPTZOID),
TIMESTAMPTZOID),
ts_time_get_min(TIMESTAMPTZOID));
/* Test saturating addition */
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_max(INT2OID), 1, INT2OID),
ts_time_get_max(INT2OID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_max(INT4OID), 1, INT4OID),
ts_time_get_max(INT4OID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_max(INT8OID), 1, INT8OID),
ts_time_get_max(INT8OID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_max(DATEOID), 1, DATEOID),
ts_time_get_noend(DATEOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_max(DATEOID), 1, DATEOID),
ts_time_get_noend(DATEOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_max(TIMESTAMPOID), 1, TIMESTAMPOID),
ts_time_get_noend(TIMESTAMPOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_max(TIMESTAMPTZOID), 1, TIMESTAMPOID),
ts_time_get_noend(TIMESTAMPOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_end(DATEOID) - 2, 1, DATEOID),
ts_time_get_max(DATEOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_end(TIMESTAMPOID) - 2, 1, TIMESTAMPOID),
ts_time_get_max(TIMESTAMPOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_end(TIMESTAMPTZOID) - 2, 1, TIMESTAMPOID),
ts_time_get_max(TIMESTAMPOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_min(INT2OID), -1, INT2OID),
ts_time_get_min(INT2OID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_min(INT4OID), -1, INT4OID),
ts_time_get_min(INT4OID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_min(INT8OID), -1, INT8OID),
ts_time_get_min(INT8OID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_min(DATEOID), -1, DATEOID),
ts_time_get_nobegin(DATEOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_min(DATEOID), -1, DATEOID),
ts_time_get_nobegin(DATEOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_min(TIMESTAMPOID), -1, TIMESTAMPOID),
ts_time_get_nobegin(TIMESTAMPOID));
TestAssertInt64Eq(ts_time_saturating_add(ts_time_get_min(TIMESTAMPTZOID), -1, TIMESTAMPOID),
ts_time_get_nobegin(TIMESTAMPOID));
/* Test saturating subtraction */
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_min(INT2OID), 1, INT2OID),
ts_time_get_min(INT2OID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_min(INT4OID), 1, INT4OID),
ts_time_get_min(INT4OID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_min(INT8OID), 1, INT8OID),
ts_time_get_min(INT8OID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_min(DATEOID), 1, DATEOID),
ts_time_get_nobegin(DATEOID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_min(TIMESTAMPOID), 1, TIMESTAMPOID),
ts_time_get_nobegin(TIMESTAMPOID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_min(TIMESTAMPTZOID), 1, TIMESTAMPTZOID),
ts_time_get_nobegin(TIMESTAMPTZOID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_min(DATEOID) + 1, 1, DATEOID),
ts_time_get_min(DATEOID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_min(TIMESTAMPOID) + 1, 1, TIMESTAMPOID),
ts_time_get_min(TIMESTAMPOID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_min(TIMESTAMPTZOID) + 1,
1,
TIMESTAMPTZOID),
ts_time_get_min(TIMESTAMPTZOID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_max(INT2OID), -1, INT2OID),
ts_time_get_max(INT2OID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_max(INT4OID), -1, INT4OID),
ts_time_get_max(INT4OID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_max(INT8OID), -1, INT8OID),
ts_time_get_max(INT8OID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_max(DATEOID), -1, DATEOID),
ts_time_get_noend(DATEOID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_max(TIMESTAMPOID), -1, TIMESTAMPOID),
ts_time_get_noend(TIMESTAMPOID));
TestAssertInt64Eq(ts_time_saturating_sub(ts_time_get_max(TIMESTAMPTZOID), -1, TIMESTAMPTZOID),
ts_time_get_noend(TIMESTAMPTZOID));
PG_RETURN_VOID();
}