Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * contrib/btree_gist/btree_interval.c
3 : : */
4 : : #include "postgres.h"
5 : :
6 : : #include "btree_gist.h"
7 : : #include "btree_utils_num.h"
8 : : #include "utils/fmgrprotos.h"
9 : : #include "utils/rel.h"
10 : : #include "utils/sortsupport.h"
11 : : #include "utils/timestamp.h"
12 : :
13 : : typedef struct
14 : : {
15 : : Interval lower,
16 : : upper;
17 : : } intvKEY;
18 : :
19 : : /* GiST support functions */
7961 teodor@sigaev.ru 20 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_intv_compress);
4006 heikki.linnakangas@i 21 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_fetch);
7955 teodor@sigaev.ru 22 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_decompress);
7961 23 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_union);
24 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_picksplit);
25 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_consistent);
5492 tgl@sss.pgh.pa.us 26 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_distance);
7961 teodor@sigaev.ru 27 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_penalty);
28 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_same);
346 heikki.linnakangas@i 29 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_sortsupport);
30 : :
31 : :
32 : : static bool
3281 andrew@dunslane.net 33 : 2243 : gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
34 : : {
7868 bruce@momjian.us 35 : 2243 : return DatumGetBool(DirectFunctionCall2(interval_gt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
36 : : }
37 : :
38 : : static bool
3281 andrew@dunslane.net 39 : 522 : gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
40 : : {
7868 bruce@momjian.us 41 : 522 : return DatumGetBool(DirectFunctionCall2(interval_ge, IntervalPGetDatum(a), IntervalPGetDatum(b)));
42 : : }
43 : :
44 : : static bool
3281 andrew@dunslane.net 45 : 150 : gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
46 : : {
7868 bruce@momjian.us 47 : 150 : return DatumGetBool(DirectFunctionCall2(interval_eq, IntervalPGetDatum(a), IntervalPGetDatum(b)));
48 : : }
49 : :
50 : : static bool
3281 andrew@dunslane.net 51 : 623 : gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
52 : : {
7868 bruce@momjian.us 53 : 623 : return DatumGetBool(DirectFunctionCall2(interval_le, IntervalPGetDatum(a), IntervalPGetDatum(b)));
54 : : }
55 : :
56 : : static bool
3281 andrew@dunslane.net 57 : 2093 : gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
58 : : {
7868 bruce@momjian.us 59 : 2093 : return DatumGetBool(DirectFunctionCall2(interval_lt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
60 : : }
61 : :
62 : : static int
3281 andrew@dunslane.net 63 : 1197 : gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
64 : : {
5299 peter_e@gmx.net 65 : 1197 : intvKEY *ia = (intvKEY *) (((const Nsrt *) a)->t);
66 : 1197 : intvKEY *ib = (intvKEY *) (((const Nsrt *) b)->t);
67 : : int res;
68 : :
5947 teodor@sigaev.ru 69 : 1197 : res = DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->lower), IntervalPGetDatum(&ib->lower)));
70 [ - + ]: 1197 : if (res == 0)
5947 teodor@sigaev.ru 71 :UBC 0 : return DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->upper), IntervalPGetDatum(&ib->upper)));
72 : :
5947 teodor@sigaev.ru 73 :CBC 1197 : return res;
74 : : }
75 : :
76 : :
77 : : static double
7868 bruce@momjian.us 78 : 612 : intr2num(const Interval *i)
79 : : {
7542 tgl@sss.pgh.pa.us 80 : 612 : return INTERVAL_TO_SEC(i);
81 : : }
82 : :
83 : : static float8
3281 andrew@dunslane.net 84 : 306 : gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
85 : : {
1255 peter@eisentraut.org 86 : 306 : return fabs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
87 : : }
88 : :
89 : : /*
90 : : * INTERVALSIZE should be the actual size-on-disk of an Interval, as shown
91 : : * in pg_type. This might be less than sizeof(Interval) if the compiler
92 : : * insists on adding alignment padding at the end of the struct. (Note:
93 : : * this concern is obsolete with the current definition of Interval, but
94 : : * was real before a separate "day" field was added to it.)
95 : : */
96 : : #define INTERVALSIZE 16
97 : :
98 : : static const gbtree_ninfo tinfo =
99 : : {
100 : : gbt_t_intv,
101 : : sizeof(Interval),
102 : : 32, /* sizeof(gbtreekey32) */
103 : : gbt_intvgt,
104 : : gbt_intvge,
105 : : gbt_intveq,
106 : : gbt_intvle,
107 : : gbt_intvlt,
108 : : gbt_intvkey_cmp,
109 : : gbt_intv_dist
110 : : };
111 : :
112 : :
113 : : Interval *
5492 tgl@sss.pgh.pa.us 114 : 2234 : abs_interval(Interval *a)
115 : : {
116 : : static const Interval zero = {0, 0, 0};
117 : :
118 [ + + ]: 2234 : if (DatumGetBool(DirectFunctionCall2(interval_lt,
119 : : IntervalPGetDatum(a),
120 : : IntervalPGetDatum(&zero))))
121 : 1239 : a = DatumGetIntervalP(DirectFunctionCall1(interval_um,
122 : : IntervalPGetDatum(a)));
123 : :
124 : 2234 : return a;
125 : : }
126 : :
127 : 4 : PG_FUNCTION_INFO_V1(interval_dist);
128 : : Datum
129 : 606 : interval_dist(PG_FUNCTION_ARGS)
130 : : {
131 : 606 : Datum diff = DirectFunctionCall2(interval_mi,
132 : : PG_GETARG_DATUM(0),
133 : : PG_GETARG_DATUM(1));
134 : :
135 : 606 : PG_RETURN_INTERVAL_P(abs_interval(DatumGetIntervalP(diff)));
136 : : }
137 : :
138 : :
139 : : /**************************************************
140 : : * GiST support functions
141 : : **************************************************/
142 : :
143 : : Datum
7961 teodor@sigaev.ru 144 : 604 : gbt_intv_compress(PG_FUNCTION_ARGS)
145 : : {
7868 bruce@momjian.us 146 : 604 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
147 : 604 : GISTENTRY *retval = entry;
148 : :
149 [ + + ]: 604 : if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
150 : : {
151 : 600 : char *r = (char *) palloc(2 * INTERVALSIZE);
152 : :
100 michael@paquier.xyz 153 :GNC 600 : retval = palloc_object(GISTENTRY);
154 : :
7868 bruce@momjian.us 155 [ + - ]:CBC 600 : if (entry->leafkey)
156 : : {
157 : 600 : Interval *key = DatumGetIntervalP(entry->key);
158 : :
1132 peter@eisentraut.org 159 : 600 : memcpy(r, key, INTERVALSIZE);
160 : 600 : memcpy(r + INTERVALSIZE, key, INTERVALSIZE);
161 : : }
162 : : else
163 : : {
7868 bruce@momjian.us 164 :UBC 0 : intvKEY *key = (intvKEY *) DatumGetPointer(entry->key);
165 : :
166 : 0 : memcpy(r, &key->lower, INTERVALSIZE);
167 : 0 : memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
168 : : }
7868 bruce@momjian.us 169 :CBC 600 : gistentryinit(*retval, PointerGetDatum(r),
170 : : entry->rel, entry->page,
171 : : entry->offset, false);
172 : : }
173 : :
174 : 604 : PG_RETURN_POINTER(retval);
175 : : }
176 : :
177 : : Datum
4006 heikki.linnakangas@i 178 : 150 : gbt_intv_fetch(PG_FUNCTION_ARGS)
179 : : {
180 : 150 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
181 : :
182 : 150 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
183 : : }
184 : :
185 : : Datum
7955 teodor@sigaev.ru 186 : 4378 : gbt_intv_decompress(PG_FUNCTION_ARGS)
187 : : {
7868 bruce@momjian.us 188 : 4378 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
189 : 4378 : GISTENTRY *retval = entry;
190 : :
191 : : if (INTERVALSIZE != sizeof(Interval))
192 : : {
193 : : intvKEY *r = palloc_object(intvKEY);
194 : : char *key = DatumGetPointer(entry->key);
195 : :
196 : : retval = palloc_object(GISTENTRY);
197 : : memcpy(&r->lower, key, INTERVALSIZE);
198 : : memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
199 : :
200 : : gistentryinit(*retval, PointerGetDatum(r),
201 : : entry->rel, entry->page,
202 : : entry->offset, false);
203 : : }
204 : 4378 : PG_RETURN_POINTER(retval);
205 : : }
206 : :
207 : :
208 : : Datum
7961 teodor@sigaev.ru 209 : 1670 : gbt_intv_consistent(PG_FUNCTION_ARGS)
210 : : {
7868 bruce@momjian.us 211 : 1670 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
212 : 1670 : Interval *query = PG_GETARG_INTERVAL_P(1);
6544 tgl@sss.pgh.pa.us 213 : 1670 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
214 : : #ifdef NOT_USED
215 : : Oid subtype = PG_GETARG_OID(3);
216 : : #endif
217 : 1670 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
7868 bruce@momjian.us 218 : 1670 : intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
219 : : GBT_NUMKEY_R key;
220 : :
221 : : /* All cases served by this function are exact */
6544 tgl@sss.pgh.pa.us 222 : 1670 : *recheck = false;
223 : :
6121 bruce@momjian.us 224 : 1670 : key.lower = (GBT_NUMKEY *) &kkk->lower;
225 : 1670 : key.upper = (GBT_NUMKEY *) &kkk->upper;
226 : :
472 peter@eisentraut.org 227 : 1670 : PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
228 : : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
229 : : }
230 : :
231 : :
232 : : Datum
5492 tgl@sss.pgh.pa.us 233 : 308 : gbt_intv_distance(PG_FUNCTION_ARGS)
234 : : {
235 : 308 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
236 : 308 : Interval *query = PG_GETARG_INTERVAL_P(1);
237 : : #ifdef NOT_USED
238 : : Oid subtype = PG_GETARG_OID(3);
239 : : #endif
240 : 308 : intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
241 : : GBT_NUMKEY_R key;
242 : :
243 : 308 : key.lower = (GBT_NUMKEY *) &kkk->lower;
244 : 308 : key.upper = (GBT_NUMKEY *) &kkk->upper;
245 : :
472 peter@eisentraut.org 246 : 308 : PG_RETURN_FLOAT8(gbt_num_distance(&key, query, GIST_LEAF(entry),
247 : : &tinfo, fcinfo->flinfo));
248 : : }
249 : :
250 : :
251 : : Datum
7961 teodor@sigaev.ru 252 : 1 : gbt_intv_union(PG_FUNCTION_ARGS)
253 : : {
7868 bruce@momjian.us 254 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
255 : 1 : void *out = palloc(sizeof(intvKEY));
256 : :
257 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
472 peter@eisentraut.org 258 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
259 : : }
260 : :
261 : :
262 : : Datum
7961 teodor@sigaev.ru 263 :UBC 0 : gbt_intv_penalty(PG_FUNCTION_ARGS)
264 : : {
7868 bruce@momjian.us 265 : 0 : intvKEY *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
266 : 0 : intvKEY *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
267 : 0 : float *result = (float *) PG_GETARG_POINTER(2);
268 : : double iorg[2],
269 : : inew[2];
270 : :
271 : 0 : iorg[0] = intr2num(&origentry->lower);
272 : 0 : iorg[1] = intr2num(&origentry->upper);
273 : 0 : inew[0] = intr2num(&newentry->lower);
274 : 0 : inew[1] = intr2num(&newentry->upper);
275 : :
7456 276 [ # # # # : 0 : penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
# # ]
277 : :
7868 278 : 0 : PG_RETURN_POINTER(result);
279 : : }
280 : :
281 : : Datum
7961 teodor@sigaev.ru 282 :CBC 3 : gbt_intv_picksplit(PG_FUNCTION_ARGS)
283 : : {
2236 alvherre@alvh.no-ip. 284 : 3 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
285 : : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
286 : : &tinfo, fcinfo->flinfo));
287 : : }
288 : :
289 : : Datum
7961 teodor@sigaev.ru 290 :UBC 0 : gbt_intv_same(PG_FUNCTION_ARGS)
291 : : {
7868 bruce@momjian.us 292 : 0 : intvKEY *b1 = (intvKEY *) PG_GETARG_POINTER(0);
293 : 0 : intvKEY *b2 = (intvKEY *) PG_GETARG_POINTER(1);
294 : 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
295 : :
3281 andrew@dunslane.net 296 : 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
7868 bruce@momjian.us 297 : 0 : PG_RETURN_POINTER(result);
298 : : }
299 : :
300 : : static int
346 heikki.linnakangas@i 301 :CBC 5676 : gbt_intv_ssup_cmp(Datum x, Datum y, SortSupport ssup)
302 : : {
303 : 5676 : intvKEY *arg1 = (intvKEY *) DatumGetPointer(x);
304 : 5676 : intvKEY *arg2 = (intvKEY *) DatumGetPointer(y);
305 : :
306 : : /* for leaf items we expect lower == upper, so only compare lower */
307 : 5676 : return DatumGetInt32(DirectFunctionCall2(interval_cmp,
308 : : IntervalPGetDatum(&arg1->lower),
309 : : IntervalPGetDatum(&arg2->lower)));
310 : : }
311 : :
312 : : Datum
313 : 1 : gbt_intv_sortsupport(PG_FUNCTION_ARGS)
314 : : {
315 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
316 : :
317 : 1 : ssup->comparator = gbt_intv_ssup_cmp;
318 : 1 : ssup->ssup_extra = NULL;
319 : :
320 : 1 : PG_RETURN_VOID();
321 : : }
|