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 */
7771 teodor@sigaev.ru 20 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_intv_compress);
3816 heikki.linnakangas@i 21 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_fetch);
7765 teodor@sigaev.ru 22 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_decompress);
7771 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);
5302 tgl@sss.pgh.pa.us 26 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_distance);
7771 teodor@sigaev.ru 27 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_penalty);
28 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_same);
156 heikki.linnakangas@i 29 : 4 : PG_FUNCTION_INFO_V1(gbt_intv_sortsupport);
30 : :
31 : :
32 : : static bool
3091 andrew@dunslane.net 33 : 2243 : gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
34 : : {
7678 bruce@momjian.us 35 : 2243 : return DatumGetBool(DirectFunctionCall2(interval_gt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
36 : : }
37 : :
38 : : static bool
3091 andrew@dunslane.net 39 : 522 : gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
40 : : {
7678 bruce@momjian.us 41 : 522 : return DatumGetBool(DirectFunctionCall2(interval_ge, IntervalPGetDatum(a), IntervalPGetDatum(b)));
42 : : }
43 : :
44 : : static bool
3091 andrew@dunslane.net 45 : 150 : gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
46 : : {
7678 bruce@momjian.us 47 : 150 : return DatumGetBool(DirectFunctionCall2(interval_eq, IntervalPGetDatum(a), IntervalPGetDatum(b)));
48 : : }
49 : :
50 : : static bool
3091 andrew@dunslane.net 51 : 623 : gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
52 : : {
7678 bruce@momjian.us 53 : 623 : return DatumGetBool(DirectFunctionCall2(interval_le, IntervalPGetDatum(a), IntervalPGetDatum(b)));
54 : : }
55 : :
56 : : static bool
3091 andrew@dunslane.net 57 : 2093 : gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
58 : : {
7678 bruce@momjian.us 59 : 2093 : return DatumGetBool(DirectFunctionCall2(interval_lt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
60 : : }
61 : :
62 : : static int
3091 andrew@dunslane.net 63 : 1197 : gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
64 : : {
5109 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 : :
5757 teodor@sigaev.ru 69 : 1197 : res = DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->lower), IntervalPGetDatum(&ib->lower)));
70 [ - + ]: 1197 : if (res == 0)
5757 teodor@sigaev.ru 71 :UBC 0 : return DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->upper), IntervalPGetDatum(&ib->upper)));
72 : :
5757 teodor@sigaev.ru 73 :CBC 1197 : return res;
74 : : }
75 : :
76 : :
77 : : static double
7678 bruce@momjian.us 78 : 612 : intr2num(const Interval *i)
79 : : {
7352 tgl@sss.pgh.pa.us 80 : 612 : return INTERVAL_TO_SEC(i);
81 : : }
82 : :
83 : : static float8
3091 andrew@dunslane.net 84 : 306 : gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
85 : : {
1065 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 *
5302 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
7771 teodor@sigaev.ru 144 : 604 : gbt_intv_compress(PG_FUNCTION_ARGS)
145 : : {
7678 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 : :
153 : 600 : retval = palloc(sizeof(GISTENTRY));
154 : :
155 [ + - ]: 600 : if (entry->leafkey)
156 : : {
157 : 600 : Interval *key = DatumGetIntervalP(entry->key);
158 : :
942 peter@eisentraut.org 159 : 600 : memcpy(r, key, INTERVALSIZE);
160 : 600 : memcpy(r + INTERVALSIZE, key, INTERVALSIZE);
161 : : }
162 : : else
163 : : {
7678 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 : : }
7678 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
3816 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
7765 teodor@sigaev.ru 186 : 4378 : gbt_intv_decompress(PG_FUNCTION_ARGS)
187 : : {
7678 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(sizeof(intvKEY));
194 : : char *key = DatumGetPointer(entry->key);
195 : :
196 : : retval = palloc(sizeof(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
7771 teodor@sigaev.ru 209 : 1670 : gbt_intv_consistent(PG_FUNCTION_ARGS)
210 : : {
7678 bruce@momjian.us 211 : 1670 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
212 : 1670 : Interval *query = PG_GETARG_INTERVAL_P(1);
6354 tgl@sss.pgh.pa.us 213 : 1670 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
214 : :
215 : : /* Oid subtype = PG_GETARG_OID(3); */
216 : 1670 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
7678 bruce@momjian.us 217 : 1670 : intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
218 : : GBT_NUMKEY_R key;
219 : :
220 : : /* All cases served by this function are exact */
6354 tgl@sss.pgh.pa.us 221 : 1670 : *recheck = false;
222 : :
5931 bruce@momjian.us 223 : 1670 : key.lower = (GBT_NUMKEY *) &kkk->lower;
224 : 1670 : key.upper = (GBT_NUMKEY *) &kkk->upper;
225 : :
282 peter@eisentraut.org 226 : 1670 : PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
227 : : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
228 : : }
229 : :
230 : :
231 : : Datum
5302 tgl@sss.pgh.pa.us 232 : 308 : gbt_intv_distance(PG_FUNCTION_ARGS)
233 : : {
234 : 308 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
235 : 308 : Interval *query = PG_GETARG_INTERVAL_P(1);
236 : :
237 : : /* Oid subtype = PG_GETARG_OID(3); */
238 : 308 : intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
239 : : GBT_NUMKEY_R key;
240 : :
241 : 308 : key.lower = (GBT_NUMKEY *) &kkk->lower;
242 : 308 : key.upper = (GBT_NUMKEY *) &kkk->upper;
243 : :
282 peter@eisentraut.org 244 : 308 : PG_RETURN_FLOAT8(gbt_num_distance(&key, query, GIST_LEAF(entry),
245 : : &tinfo, fcinfo->flinfo));
246 : : }
247 : :
248 : :
249 : : Datum
7771 teodor@sigaev.ru 250 : 1 : gbt_intv_union(PG_FUNCTION_ARGS)
251 : : {
7678 bruce@momjian.us 252 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
253 : 1 : void *out = palloc(sizeof(intvKEY));
254 : :
255 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
282 peter@eisentraut.org 256 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
257 : : }
258 : :
259 : :
260 : : Datum
7771 teodor@sigaev.ru 261 :UBC 0 : gbt_intv_penalty(PG_FUNCTION_ARGS)
262 : : {
7678 bruce@momjian.us 263 : 0 : intvKEY *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
264 : 0 : intvKEY *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
265 : 0 : float *result = (float *) PG_GETARG_POINTER(2);
266 : : double iorg[2],
267 : : inew[2];
268 : :
269 : 0 : iorg[0] = intr2num(&origentry->lower);
270 : 0 : iorg[1] = intr2num(&origentry->upper);
271 : 0 : inew[0] = intr2num(&newentry->lower);
272 : 0 : inew[1] = intr2num(&newentry->upper);
273 : :
7266 274 [ # # # # : 0 : penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
# # ]
275 : :
7678 276 : 0 : PG_RETURN_POINTER(result);
277 : : }
278 : :
279 : : Datum
7771 teodor@sigaev.ru 280 :CBC 3 : gbt_intv_picksplit(PG_FUNCTION_ARGS)
281 : : {
2046 alvherre@alvh.no-ip. 282 : 3 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
283 : : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
284 : : &tinfo, fcinfo->flinfo));
285 : : }
286 : :
287 : : Datum
7771 teodor@sigaev.ru 288 :UBC 0 : gbt_intv_same(PG_FUNCTION_ARGS)
289 : : {
7678 bruce@momjian.us 290 : 0 : intvKEY *b1 = (intvKEY *) PG_GETARG_POINTER(0);
291 : 0 : intvKEY *b2 = (intvKEY *) PG_GETARG_POINTER(1);
292 : 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
293 : :
3091 andrew@dunslane.net 294 : 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
7678 bruce@momjian.us 295 : 0 : PG_RETURN_POINTER(result);
296 : : }
297 : :
298 : : static int
156 heikki.linnakangas@i 299 :CBC 5676 : gbt_intv_ssup_cmp(Datum x, Datum y, SortSupport ssup)
300 : : {
301 : 5676 : intvKEY *arg1 = (intvKEY *) DatumGetPointer(x);
302 : 5676 : intvKEY *arg2 = (intvKEY *) DatumGetPointer(y);
303 : :
304 : : /* for leaf items we expect lower == upper, so only compare lower */
305 : 5676 : return DatumGetInt32(DirectFunctionCall2(interval_cmp,
306 : : IntervalPGetDatum(&arg1->lower),
307 : : IntervalPGetDatum(&arg2->lower)));
308 : : }
309 : :
310 : : Datum
311 : 1 : gbt_intv_sortsupport(PG_FUNCTION_ARGS)
312 : : {
313 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
314 : :
315 : 1 : ssup->comparator = gbt_intv_ssup_cmp;
316 : 1 : ssup->ssup_extra = NULL;
317 : :
318 : 1 : PG_RETURN_VOID();
319 : : }
|