-
Notifications
You must be signed in to change notification settings - Fork 14
/
temporal.sql.in
451 lines (362 loc) · 12.7 KB
/
temporal.sql.in
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
--
-- period.sql
-- SQL install script for the PERIOD data type.
--
-- Portions Copyright (c) 2007-2008, Jeff Davis <[email protected]>
--
-- Adjust this setting to control where the objects get created.
SET search_path = public;
CREATE TYPE period;
CREATE OR REPLACE FUNCTION period_in(cstring) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_in';
CREATE OR REPLACE FUNCTION period_out(period) RETURNS cstring LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_out';
CREATE TYPE period(
input = period_in,
output = period_out,
internallength = 16,
alignment = double
);
--
-- INTERVAL
--
CREATE OR REPLACE FUNCTION length(period) RETURNS INTERVAL LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','length_period';
CREATE OR REPLACE FUNCTION period_offset(period, timestamptz) RETURNS INTERVAL LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_offset_period_timestamptz';
CREATE OR REPLACE FUNCTION period_offset_sec(period, timestamptz) RETURNS BIGINT LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_offset_sec_period_timestamptz';
--
-- TIMESTAMPTZ
--
CREATE OR REPLACE FUNCTION first(period) RETURNS TIMESTAMPTZ LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','first_period';
CREATE OR REPLACE FUNCTION last(period) RETURNS TIMESTAMPTZ LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','last_period';
CREATE OR REPLACE FUNCTION prior(period) RETURNS TIMESTAMPTZ LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','prior_period';
CREATE OR REPLACE FUNCTION next(period) RETURNS TIMESTAMPTZ LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','next_period';
--
-- BOOLEAN
--
CREATE OR REPLACE FUNCTION contains(period,TIMESTAMPTZ) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','contains_period_timestamptz';
CREATE OR REPLACE FUNCTION contains(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','contains_period_period';
CREATE OR REPLACE FUNCTION contained_by(TIMESTAMPTZ,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','contained_by_timestamptz_period';
CREATE OR REPLACE FUNCTION contained_by(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','contained_by_period_period';
CREATE OR REPLACE FUNCTION adjacent(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','adjacent_period_period';
CREATE OR REPLACE FUNCTION overlaps(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','overlaps_period_period';
CREATE OR REPLACE FUNCTION overleft(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','overleft_period_period';
CREATE OR REPLACE FUNCTION overright(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','overright_period_period';
CREATE OR REPLACE FUNCTION is_empty(period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','is_empty_period';
CREATE OR REPLACE FUNCTION equals(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','equals_period_period';
CREATE OR REPLACE FUNCTION nequals(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','nequals_period_period';
CREATE OR REPLACE FUNCTION before(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','before_period_period';
CREATE OR REPLACE FUNCTION after(period,period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','after_period_period';
-- djg added these so we can cheat and use a period in ORDER BY
CREATE OR REPLACE FUNCTION lessthan(period, period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','lessthan_period_period';
CREATE OR REPLACE FUNCTION lessthanequals(period, period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','lessthanequals_period_period';
CREATE OR REPLACE FUNCTION greaterthan(period, period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','greaterthan_period_period';
CREATE OR REPLACE FUNCTION greaterthanequals(period, period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','greaterthanequals_period_period';
CREATE OR REPLACE FUNCTION lessthan(period, timestamptz) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','lessthan_period_timestamptz';
CREATE OR REPLACE FUNCTION greaterthan(period, timestamptz) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','greaterthan_period_timestamptz';
CREATE OR REPLACE FUNCTION lessthan(timestamptz, period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','lessthan_timestamptz_period';
CREATE OR REPLACE FUNCTION greaterthan(timestamptz, period) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','greaterthan_timestamptz_period';
--
-- period
--
CREATE OR REPLACE FUNCTION empty_period() RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','empty_period';
CREATE OR REPLACE FUNCTION period_intersect(period,period) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','intersect_period_period';
CREATE OR REPLACE FUNCTION period_union(period,period) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','union_period_period';
CREATE OR REPLACE FUNCTION period(TIMESTAMPTZ) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_timestamptz';
CREATE OR REPLACE FUNCTION period(TIMESTAMPTZ,TIMESTAMPTZ) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_co_timestamptz_timestamptz';
CREATE OR REPLACE FUNCTION period_oo(TIMESTAMPTZ,TIMESTAMPTZ) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_oo_timestamptz_timestamptz';
CREATE OR REPLACE FUNCTION period_oc(TIMESTAMPTZ,TIMESTAMPTZ) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_oc_timestamptz_timestamptz';
CREATE OR REPLACE FUNCTION period_co(TIMESTAMPTZ,TIMESTAMPTZ) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_co_timestamptz_timestamptz';
CREATE OR REPLACE FUNCTION period_cc(TIMESTAMPTZ,TIMESTAMPTZ) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','period_cc_timestamptz_timestamptz';
CREATE OR REPLACE FUNCTION minus(period,period) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME','minus_period_period';
--
-- GiST Support
--
CREATE OR REPLACE FUNCTION gist_period_consistent(internal, period, int4) RETURNS BOOLEAN LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME';
CREATE OR REPLACE FUNCTION gist_period_union(internal, internal) RETURNS period LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME';
CREATE OR REPLACE FUNCTION gist_period_compress(internal) RETURNS INTERNAL LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME';
CREATE OR REPLACE FUNCTION gist_period_decompress(internal) RETURNS INTERNAL LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME';
CREATE OR REPLACE FUNCTION gist_period_penalty(internal, internal, internal) RETURNS INTERNAL LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME';
CREATE OR REPLACE FUNCTION gist_period_picksplit(internal, internal) RETURNS INTERNAL LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME';
CREATE OR REPLACE FUNCTION gist_period_same(period, period, internal) RETURNS INTERNAL LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME';
-- btree Support
CREATE OR REPLACE FUNCTION btree_period_compare(period, period) RETURNS INTEGER LANGUAGE C IMMUTABLE STRICT
AS 'MODULE_PATHNAME';
--
-- operators
--
-- equals
CREATE OPERATOR = (
PROCEDURE = equals,
LEFTARG = period,
RIGHTARG = period,
NEGATOR = !=,
RESTRICT = eqsel
);
CREATE OPERATOR != (
PROCEDURE = nequals,
LEFTARG = period,
RIGHTARG = period,
NEGATOR = =,
RESTRICT = neqsel
);
-- minus
CREATE OPERATOR - (
PROCEDURE = minus,
LEFTARG = period,
RIGHTARG = period
);
-- plus
CREATE OPERATOR + (
PROCEDURE = period_union,
LEFTARG = period,
RIGHTARG = period
);
-- contains (period,period)
CREATE OPERATOR @> (
PROCEDURE = contains,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= <@,
RESTRICT = contsel
);
-- contains (period,TIMESTAMPTZ)
CREATE OPERATOR @> (
PROCEDURE = contains,
LEFTARG = period,
RIGHTARG = TIMESTAMPTZ,
COMMUTATOR= <@,
RESTRICT = contsel
);
-- contained_by (period,period)
CREATE OPERATOR <@ (
PROCEDURE = contained_by,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= @>,
RESTRICT = contsel
);
-- contained_by (TIMESTAMPTZ,period)
CREATE OPERATOR <@ (
PROCEDURE = contained_by,
LEFTARG = TIMESTAMPTZ,
RIGHTARG = period,
COMMUTATOR= @>,
RESTRICT = contsel
);
-- alias for contains (period,period)
CREATE OPERATOR ~ (
PROCEDURE = contains,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= @,
RESTRICT = contsel
);
-- alias for contains (period,TIMESTAMPTZ)
CREATE OPERATOR ~ (
PROCEDURE = contains,
LEFTARG = period,
RIGHTARG = TIMESTAMPTZ,
COMMUTATOR= @,
RESTRICT = contsel
);
-- alias for contained_by (period,period)
CREATE OPERATOR @ (
PROCEDURE = contained_by,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= ~,
RESTRICT = contsel
);
-- alias for contained_by (period,TIMESTAMPTZ)
CREATE OPERATOR @ (
PROCEDURE = contained_by,
LEFTARG = TIMESTAMPTZ,
RIGHTARG = period,
COMMUTATOR= ~,
RESTRICT = contsel
);
-- overlaps
CREATE OPERATOR && (
PROCEDURE = overlaps,
LEFTARG = period,
RIGHTARG = period,
RESTRICT = areasel,
COMMUTATOR= &&
);
-- strictly before
CREATE OPERATOR << (
PROCEDURE = before,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= >>,
RESTRICT = areasel
);
-- strictly after
CREATE OPERATOR >> (
PROCEDURE = after,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= <<,
RESTRICT = areasel
);
-- A.last <= B.last
CREATE OPERATOR &< (
PROCEDURE = overleft,
LEFTARG = period,
RIGHTARG = period,
RESTRICT = areasel
);
-- A.first >= B.first
CREATE OPERATOR &> (
PROCEDURE = overright,
LEFTARG = period,
RIGHTARG = period,
RESTRICT = areasel
);
-- A.first < B.first or (A.first = B.first and A.last < B.last)
CREATE OPERATOR < (
PROCEDURE = lessthan,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= >,
NEGATOR = >=,
RESTRICT = scalarltsel,
JOIN = scalarltjoinsel
);
CREATE OPERATOR <= (
PROCEDURE = lessthanequals,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= >=,
NEGATOR = >,
RESTRICT = scalarltsel,
JOIN = scalarltjoinsel
);
-- A.last < B
CREATE OPERATOR < (
PROCEDURE = lessthan,
LEFTARG = period,
RIGHTARG = timestamptz,
COMMUTATOR= >,
RESTRICT = scalarltsel,
JOIN = scalarltjoinsel
);
-- B < A.first
CREATE OPERATOR < (
PROCEDURE = lessthan,
LEFTARG = timestamptz,
RIGHTARG = period,
COMMUTATOR= >,
RESTRICT = scalarltsel,
JOIN = scalarltjoinsel
);
-- A.first > B.first or (A.first = B.first and A.last > B.last)
CREATE OPERATOR > (
PROCEDURE = greaterthan,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= <=,
RESTRICT = scalargtsel,
JOIN = scalargtjoinsel
);
CREATE OPERATOR >= (
PROCEDURE = greaterthanequals,
LEFTARG = period,
RIGHTARG = period,
COMMUTATOR= <=,
NEGATOR = <,
RESTRICT = scalargtsel,
JOIN = scalargtjoinsel
);
-- A.first > B
CREATE OPERATOR > (
PROCEDURE = greaterthan,
LEFTARG = period,
RIGHTARG = timestamptz,
COMMUTATOR= <,
RESTRICT = scalargtsel,
JOIN = scalargtjoinsel
);
-- B > A.last
CREATE OPERATOR > (
PROCEDURE = greaterthan,
LEFTARG = timestamptz,
RIGHTARG = period,
COMMUTATOR= <,
RESTRICT = scalargtsel,
JOIN = scalargtjoinsel
);
CREATE OPERATOR CLASS gist_period_ops
DEFAULT FOR TYPE period USING gist AS
OPERATOR 1 <<, -- strictly before
OPERATOR 2 &<, -- overlaps or left of
OPERATOR 3 &&, -- overlaps
OPERATOR 4 &>, -- overlaps or right of
OPERATOR 5 >>, -- strictly after
OPERATOR 6 =, -- equal
OPERATOR 7 @>, -- contains
OPERATOR 8 <@, -- contained by
OPERATOR 17 ~, -- alias for contains
OPERATOR 18 @, -- alias for contained by
OPERATOR 27 @>(period,TIMESTAMPTZ),
OPERATOR 28 <@(TIMESTAMPTZ,period),
FUNCTION 1 gist_period_consistent(internal, period, int4),
FUNCTION 2 gist_period_union(internal, internal),
FUNCTION 3 gist_period_compress(internal),
FUNCTION 4 gist_period_decompress(internal),
FUNCTION 5 gist_period_penalty(internal, internal, internal),
FUNCTION 6 gist_period_picksplit(internal, internal),
FUNCTION 7 gist_period_same(period, period, internal);
CREATE OPERATOR CLASS btree_period_ops
DEFAULT FOR TYPE period USING btree AS
OPERATOR 1 <,
OPERATOR 2 <=,
OPERATOR 3 =,
OPERATOR 4 >=,
OPERATOR 5 >,
FUNCTION 1 btree_period_compare(period, period);