Line data Source code
1 : /*
2 : * contrib/btree_gist/btree_ts.c
3 : */
4 : #include "postgres.h"
5 :
6 : #include <limits.h>
7 :
8 : #include "btree_gist.h"
9 : #include "btree_utils_num.h"
10 : #include "utils/fmgrprotos.h"
11 : #include "utils/timestamp.h"
12 : #include "utils/float.h"
13 : #include "utils/rel.h"
14 : #include "utils/sortsupport.h"
15 :
16 : typedef struct
17 : {
18 : Timestamp lower;
19 : Timestamp upper;
20 : } tsKEY;
21 :
22 : /* GiST support functions */
23 10 : PG_FUNCTION_INFO_V1(gbt_ts_compress);
24 8 : PG_FUNCTION_INFO_V1(gbt_tstz_compress);
25 12 : PG_FUNCTION_INFO_V1(gbt_ts_fetch);
26 12 : PG_FUNCTION_INFO_V1(gbt_ts_union);
27 12 : PG_FUNCTION_INFO_V1(gbt_ts_picksplit);
28 10 : PG_FUNCTION_INFO_V1(gbt_ts_consistent);
29 10 : PG_FUNCTION_INFO_V1(gbt_ts_distance);
30 8 : PG_FUNCTION_INFO_V1(gbt_tstz_consistent);
31 8 : PG_FUNCTION_INFO_V1(gbt_tstz_distance);
32 12 : PG_FUNCTION_INFO_V1(gbt_ts_penalty);
33 12 : PG_FUNCTION_INFO_V1(gbt_ts_same);
34 12 : PG_FUNCTION_INFO_V1(gbt_ts_sortsupport);
35 :
36 :
37 : /* define for comparison */
38 :
39 : static bool
40 25938 : gbt_tsgt(const void *a, const void *b, FmgrInfo *flinfo)
41 : {
42 25938 : const Timestamp *aa = (const Timestamp *) a;
43 25938 : const Timestamp *bb = (const Timestamp *) b;
44 :
45 25938 : return DatumGetBool(DirectFunctionCall2(timestamp_gt,
46 : TimestampGetDatum(*aa),
47 : TimestampGetDatum(*bb)));
48 : }
49 :
50 : static bool
51 4690 : gbt_tsge(const void *a, const void *b, FmgrInfo *flinfo)
52 : {
53 4690 : const Timestamp *aa = (const Timestamp *) a;
54 4690 : const Timestamp *bb = (const Timestamp *) b;
55 :
56 4690 : return DatumGetBool(DirectFunctionCall2(timestamp_ge,
57 : TimestampGetDatum(*aa),
58 : TimestampGetDatum(*bb)));
59 : }
60 :
61 : static bool
62 10370 : gbt_tseq(const void *a, const void *b, FmgrInfo *flinfo)
63 : {
64 10370 : const Timestamp *aa = (const Timestamp *) a;
65 10370 : const Timestamp *bb = (const Timestamp *) b;
66 :
67 10370 : return DatumGetBool(DirectFunctionCall2(timestamp_eq,
68 : TimestampGetDatum(*aa),
69 : TimestampGetDatum(*bb)));
70 : }
71 :
72 : static bool
73 3974 : gbt_tsle(const void *a, const void *b, FmgrInfo *flinfo)
74 : {
75 3974 : const Timestamp *aa = (const Timestamp *) a;
76 3974 : const Timestamp *bb = (const Timestamp *) b;
77 :
78 3974 : return DatumGetBool(DirectFunctionCall2(timestamp_le,
79 : TimestampGetDatum(*aa),
80 : TimestampGetDatum(*bb)));
81 : }
82 :
83 : static bool
84 24862 : gbt_tslt(const void *a, const void *b, FmgrInfo *flinfo)
85 : {
86 24862 : const Timestamp *aa = (const Timestamp *) a;
87 24862 : const Timestamp *bb = (const Timestamp *) b;
88 :
89 24862 : return DatumGetBool(DirectFunctionCall2(timestamp_lt,
90 : TimestampGetDatum(*aa),
91 : TimestampGetDatum(*bb)));
92 : }
93 :
94 : static int
95 9650 : gbt_tskey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
96 : {
97 9650 : tsKEY *ia = (tsKEY *) (((const Nsrt *) a)->t);
98 9650 : tsKEY *ib = (tsKEY *) (((const Nsrt *) b)->t);
99 : int res;
100 :
101 9650 : res = DatumGetInt32(DirectFunctionCall2(timestamp_cmp, TimestampGetDatum(ia->lower), TimestampGetDatum(ib->lower)));
102 9650 : if (res == 0)
103 7476 : return DatumGetInt32(DirectFunctionCall2(timestamp_cmp, TimestampGetDatum(ia->upper), TimestampGetDatum(ib->upper)));
104 :
105 2174 : return res;
106 : }
107 :
108 : static float8
109 1122 : gbt_ts_dist(const void *a, const void *b, FmgrInfo *flinfo)
110 : {
111 1122 : const Timestamp *aa = (const Timestamp *) a;
112 1122 : const Timestamp *bb = (const Timestamp *) b;
113 : Interval *i;
114 :
115 1122 : if (TIMESTAMP_NOT_FINITE(*aa) || TIMESTAMP_NOT_FINITE(*bb))
116 40 : return get_float8_infinity();
117 :
118 1082 : i = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
119 : TimestampGetDatum(*aa),
120 : TimestampGetDatum(*bb)));
121 1082 : return fabs(INTERVAL_TO_SEC(i));
122 : }
123 :
124 : static const gbtree_ninfo tinfo =
125 : {
126 : gbt_t_ts,
127 : sizeof(Timestamp),
128 : 16, /* sizeof(gbtreekey16) */
129 : gbt_tsgt,
130 : gbt_tsge,
131 : gbt_tseq,
132 : gbt_tsle,
133 : gbt_tslt,
134 : gbt_tskey_cmp,
135 : gbt_ts_dist
136 : };
137 :
138 :
139 8 : PG_FUNCTION_INFO_V1(ts_dist);
140 : Datum
141 1142 : ts_dist(PG_FUNCTION_ARGS)
142 : {
143 1142 : Timestamp a = PG_GETARG_TIMESTAMP(0);
144 1142 : Timestamp b = PG_GETARG_TIMESTAMP(1);
145 : Interval *r;
146 :
147 1142 : if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b))
148 : {
149 48 : Interval *p = palloc_object(Interval);
150 :
151 48 : p->day = INT_MAX;
152 48 : p->month = INT_MAX;
153 48 : p->time = PG_INT64_MAX;
154 48 : PG_RETURN_INTERVAL_P(p);
155 : }
156 : else
157 1094 : r = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
158 : PG_GETARG_DATUM(0),
159 : PG_GETARG_DATUM(1)));
160 1094 : PG_RETURN_INTERVAL_P(abs_interval(r));
161 : }
162 :
163 8 : PG_FUNCTION_INFO_V1(tstz_dist);
164 : Datum
165 1104 : tstz_dist(PG_FUNCTION_ARGS)
166 : {
167 1104 : TimestampTz a = PG_GETARG_TIMESTAMPTZ(0);
168 1104 : TimestampTz b = PG_GETARG_TIMESTAMPTZ(1);
169 : Interval *r;
170 :
171 1104 : if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b))
172 : {
173 36 : Interval *p = palloc_object(Interval);
174 :
175 36 : p->day = INT_MAX;
176 36 : p->month = INT_MAX;
177 36 : p->time = PG_INT64_MAX;
178 36 : PG_RETURN_INTERVAL_P(p);
179 : }
180 :
181 1068 : r = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
182 : PG_GETARG_DATUM(0),
183 : PG_GETARG_DATUM(1)));
184 1068 : PG_RETURN_INTERVAL_P(abs_interval(r));
185 : }
186 :
187 : /**************************************************
188 : * GiST support functions
189 : **************************************************/
190 :
191 : static inline Timestamp
192 13250 : tstz_to_ts_gmt(TimestampTz ts)
193 : {
194 : /* No timezone correction is needed, since GMT is offset 0 by definition */
195 13250 : return (Timestamp) ts;
196 : }
197 :
198 :
199 : Datum
200 5224 : gbt_ts_compress(PG_FUNCTION_ARGS)
201 : {
202 5224 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
203 :
204 5224 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
205 : }
206 :
207 : Datum
208 1102 : gbt_tstz_compress(PG_FUNCTION_ARGS)
209 : {
210 1102 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
211 : GISTENTRY *retval;
212 :
213 1102 : if (entry->leafkey)
214 : {
215 1098 : tsKEY *r = palloc_object(tsKEY);
216 1098 : TimestampTz ts = DatumGetTimestampTz(entry->key);
217 : Timestamp gmt;
218 :
219 1098 : gmt = tstz_to_ts_gmt(ts);
220 :
221 1098 : retval = palloc_object(GISTENTRY);
222 1098 : r->lower = r->upper = gmt;
223 1098 : gistentryinit(*retval, PointerGetDatum(r),
224 : entry->rel, entry->page,
225 : entry->offset, false);
226 : }
227 : else
228 4 : retval = entry;
229 :
230 1102 : PG_RETURN_POINTER(retval);
231 : }
232 :
233 : Datum
234 1118 : gbt_ts_fetch(PG_FUNCTION_ARGS)
235 : {
236 1118 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
237 :
238 1118 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
239 : }
240 :
241 : Datum
242 4408 : gbt_ts_consistent(PG_FUNCTION_ARGS)
243 : {
244 4408 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
245 4408 : Timestamp query = PG_GETARG_TIMESTAMP(1);
246 4408 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
247 : #ifdef NOT_USED
248 : Oid subtype = PG_GETARG_OID(3);
249 : #endif
250 4408 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
251 4408 : tsKEY *kkk = (tsKEY *) DatumGetPointer(entry->key);
252 : GBT_NUMKEY_R key;
253 :
254 : /* All cases served by this function are exact */
255 4408 : *recheck = false;
256 :
257 4408 : key.lower = (GBT_NUMKEY *) &kkk->lower;
258 4408 : key.upper = (GBT_NUMKEY *) &kkk->upper;
259 :
260 4408 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
261 : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
262 : }
263 :
264 : Datum
265 572 : gbt_ts_distance(PG_FUNCTION_ARGS)
266 : {
267 572 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
268 572 : Timestamp query = PG_GETARG_TIMESTAMP(1);
269 : #ifdef NOT_USED
270 : Oid subtype = PG_GETARG_OID(3);
271 : #endif
272 572 : tsKEY *kkk = (tsKEY *) DatumGetPointer(entry->key);
273 : GBT_NUMKEY_R key;
274 :
275 572 : key.lower = (GBT_NUMKEY *) &kkk->lower;
276 572 : key.upper = (GBT_NUMKEY *) &kkk->upper;
277 :
278 572 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
279 : &tinfo, fcinfo->flinfo));
280 : }
281 :
282 : Datum
283 11598 : gbt_tstz_consistent(PG_FUNCTION_ARGS)
284 : {
285 11598 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
286 11598 : TimestampTz query = PG_GETARG_TIMESTAMPTZ(1);
287 11598 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
288 : #ifdef NOT_USED
289 : Oid subtype = PG_GETARG_OID(3);
290 : #endif
291 11598 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
292 11598 : char *kkk = (char *) DatumGetPointer(entry->key);
293 : GBT_NUMKEY_R key;
294 : Timestamp qqq;
295 :
296 : /* All cases served by this function are exact */
297 11598 : *recheck = false;
298 :
299 11598 : key.lower = (GBT_NUMKEY *) &kkk[0];
300 11598 : key.upper = (GBT_NUMKEY *) &kkk[MAXALIGN(tinfo.size)];
301 11598 : qqq = tstz_to_ts_gmt(query);
302 :
303 11598 : PG_RETURN_BOOL(gbt_num_consistent(&key, &qqq, &strategy,
304 : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
305 : }
306 :
307 : Datum
308 554 : gbt_tstz_distance(PG_FUNCTION_ARGS)
309 : {
310 554 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
311 554 : TimestampTz query = PG_GETARG_TIMESTAMPTZ(1);
312 : #ifdef NOT_USED
313 : Oid subtype = PG_GETARG_OID(3);
314 : #endif
315 554 : char *kkk = (char *) DatumGetPointer(entry->key);
316 : GBT_NUMKEY_R key;
317 : Timestamp qqq;
318 :
319 554 : key.lower = (GBT_NUMKEY *) &kkk[0];
320 554 : key.upper = (GBT_NUMKEY *) &kkk[MAXALIGN(tinfo.size)];
321 554 : qqq = tstz_to_ts_gmt(query);
322 :
323 554 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &qqq, GIST_LEAF(entry),
324 : &tinfo, fcinfo->flinfo));
325 : }
326 :
327 :
328 : Datum
329 3716 : gbt_ts_union(PG_FUNCTION_ARGS)
330 : {
331 3716 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
332 3716 : void *out = palloc(sizeof(tsKEY));
333 :
334 3716 : *(int *) PG_GETARG_POINTER(1) = sizeof(tsKEY);
335 3716 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
336 : }
337 :
338 :
339 : #define penalty_check_max_float(val) \
340 : do { \
341 : if ( val > FLT_MAX ) \
342 : val = FLT_MAX; \
343 : if ( val < -FLT_MAX ) \
344 : val = -FLT_MAX; \
345 : } while (0)
346 :
347 :
348 : Datum
349 8270 : gbt_ts_penalty(PG_FUNCTION_ARGS)
350 : {
351 8270 : tsKEY *origentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
352 8270 : tsKEY *newentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
353 8270 : float *result = (float *) PG_GETARG_POINTER(2);
354 :
355 : double orgdbl[2],
356 : newdbl[2];
357 :
358 : /*
359 : * We are always using "double" timestamps here. Precision should be good
360 : * enough.
361 : */
362 8270 : orgdbl[0] = ((double) origentry->lower);
363 8270 : orgdbl[1] = ((double) origentry->upper);
364 8270 : newdbl[0] = ((double) newentry->lower);
365 8270 : newdbl[1] = ((double) newentry->upper);
366 :
367 8270 : penalty_check_max_float(orgdbl[0]);
368 8270 : penalty_check_max_float(orgdbl[1]);
369 8270 : penalty_check_max_float(newdbl[0]);
370 8270 : penalty_check_max_float(newdbl[1]);
371 :
372 8270 : penalty_num(result, orgdbl[0], orgdbl[1], newdbl[0], newdbl[1]);
373 :
374 8270 : PG_RETURN_POINTER(result);
375 : }
376 :
377 :
378 : Datum
379 42 : gbt_ts_picksplit(PG_FUNCTION_ARGS)
380 : {
381 42 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
382 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
383 : &tinfo, fcinfo->flinfo));
384 : }
385 :
386 : Datum
387 3670 : gbt_ts_same(PG_FUNCTION_ARGS)
388 : {
389 3670 : tsKEY *b1 = (tsKEY *) PG_GETARG_POINTER(0);
390 3670 : tsKEY *b2 = (tsKEY *) PG_GETARG_POINTER(1);
391 3670 : bool *result = (bool *) PG_GETARG_POINTER(2);
392 :
393 3670 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
394 3670 : PG_RETURN_POINTER(result);
395 : }
396 :
397 : static int
398 22230 : gbt_ts_ssup_cmp(Datum x, Datum y, SortSupport ssup)
399 : {
400 22230 : tsKEY *arg1 = (tsKEY *) DatumGetPointer(x);
401 22230 : tsKEY *arg2 = (tsKEY *) DatumGetPointer(y);
402 :
403 : /* for leaf items we expect lower == upper, so only compare lower */
404 22230 : return DatumGetInt32(DirectFunctionCall2(timestamp_cmp,
405 : TimestampGetDatum(arg1->lower),
406 : TimestampGetDatum(arg2->lower)));
407 : }
408 :
409 : Datum
410 6 : gbt_ts_sortsupport(PG_FUNCTION_ARGS)
411 : {
412 6 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
413 :
414 6 : ssup->comparator = gbt_ts_ssup_cmp;
415 6 : ssup->ssup_extra = NULL;
416 :
417 6 : PG_RETURN_VOID();
418 : }
|