PolarDB-for-PostgreSQL
303 строки · 7.3 Кб
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/builtins.h"
9#include "utils/timestamp.h"
10
11typedef struct
12{
13Interval lower,
14upper;
15} intvKEY;
16
17
18/*
19** Interval ops
20*/
21PG_FUNCTION_INFO_V1(gbt_intv_compress);
22PG_FUNCTION_INFO_V1(gbt_intv_fetch);
23PG_FUNCTION_INFO_V1(gbt_intv_decompress);
24PG_FUNCTION_INFO_V1(gbt_intv_union);
25PG_FUNCTION_INFO_V1(gbt_intv_picksplit);
26PG_FUNCTION_INFO_V1(gbt_intv_consistent);
27PG_FUNCTION_INFO_V1(gbt_intv_distance);
28PG_FUNCTION_INFO_V1(gbt_intv_penalty);
29PG_FUNCTION_INFO_V1(gbt_intv_same);
30
31
32static bool
33gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
34{
35return DatumGetBool(DirectFunctionCall2(interval_gt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
36}
37
38static bool
39gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
40{
41return DatumGetBool(DirectFunctionCall2(interval_ge, IntervalPGetDatum(a), IntervalPGetDatum(b)));
42}
43
44static bool
45gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
46{
47return DatumGetBool(DirectFunctionCall2(interval_eq, IntervalPGetDatum(a), IntervalPGetDatum(b)));
48}
49
50static bool
51gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
52{
53return DatumGetBool(DirectFunctionCall2(interval_le, IntervalPGetDatum(a), IntervalPGetDatum(b)));
54}
55
56static bool
57gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
58{
59return DatumGetBool(DirectFunctionCall2(interval_lt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
60}
61
62static int
63gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
64{
65intvKEY *ia = (intvKEY *) (((const Nsrt *) a)->t);
66intvKEY *ib = (intvKEY *) (((const Nsrt *) b)->t);
67int res;
68
69res = DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->lower), IntervalPGetDatum(&ib->lower)));
70if (res == 0)
71return DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->upper), IntervalPGetDatum(&ib->upper)));
72
73return res;
74}
75
76
77static double
78intr2num(const Interval *i)
79{
80return INTERVAL_TO_SEC(i);
81}
82
83static float8
84gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
85{
86return (float8) Abs(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
98static const gbtree_ninfo tinfo =
99{
100gbt_t_intv,
101sizeof(Interval),
10232, /* sizeof(gbtreekey32) */
103gbt_intvgt,
104gbt_intvge,
105gbt_intveq,
106gbt_intvle,
107gbt_intvlt,
108gbt_intvkey_cmp,
109gbt_intv_dist
110};
111
112
113Interval *
114abs_interval(Interval *a)
115{
116static Interval zero = {0, 0, 0};
117
118if (DatumGetBool(DirectFunctionCall2(interval_lt,
119IntervalPGetDatum(a),
120IntervalPGetDatum(&zero))))
121a = DatumGetIntervalP(DirectFunctionCall1(interval_um,
122IntervalPGetDatum(a)));
123
124return a;
125}
126
127PG_FUNCTION_INFO_V1(interval_dist);
128Datum
129interval_dist(PG_FUNCTION_ARGS)
130{
131Datum diff = DirectFunctionCall2(interval_mi,
132PG_GETARG_DATUM(0),
133PG_GETARG_DATUM(1));
134
135PG_RETURN_INTERVAL_P(abs_interval(DatumGetIntervalP(diff)));
136}
137
138
139/**************************************************
140* interval ops
141**************************************************/
142
143
144Datum
145gbt_intv_compress(PG_FUNCTION_ARGS)
146{
147GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
148GISTENTRY *retval = entry;
149
150if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
151{
152char *r = (char *) palloc(2 * INTERVALSIZE);
153
154retval = palloc(sizeof(GISTENTRY));
155
156if (entry->leafkey)
157{
158Interval *key = DatumGetIntervalP(entry->key);
159
160memcpy((void *) r, (void *) key, INTERVALSIZE);
161memcpy((void *) (r + INTERVALSIZE), (void *) key, INTERVALSIZE);
162}
163else
164{
165intvKEY *key = (intvKEY *) DatumGetPointer(entry->key);
166
167memcpy(r, &key->lower, INTERVALSIZE);
168memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
169}
170gistentryinit(*retval, PointerGetDatum(r),
171entry->rel, entry->page,
172entry->offset, false);
173}
174
175PG_RETURN_POINTER(retval);
176
177}
178
179Datum
180gbt_intv_fetch(PG_FUNCTION_ARGS)
181{
182GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
183
184PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
185}
186
187Datum
188gbt_intv_decompress(PG_FUNCTION_ARGS)
189{
190GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
191GISTENTRY *retval = entry;
192
193if (INTERVALSIZE != sizeof(Interval))
194{
195intvKEY *r = palloc(sizeof(intvKEY));
196char *key = DatumGetPointer(entry->key);
197
198retval = palloc(sizeof(GISTENTRY));
199memcpy(&r->lower, key, INTERVALSIZE);
200memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
201
202gistentryinit(*retval, PointerGetDatum(r),
203entry->rel, entry->page,
204entry->offset, false);
205}
206PG_RETURN_POINTER(retval);
207}
208
209
210Datum
211gbt_intv_consistent(PG_FUNCTION_ARGS)
212{
213GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
214Interval *query = PG_GETARG_INTERVAL_P(1);
215StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
216
217/* Oid subtype = PG_GETARG_OID(3); */
218bool *recheck = (bool *) PG_GETARG_POINTER(4);
219intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
220GBT_NUMKEY_R key;
221
222/* All cases served by this function are exact */
223*recheck = false;
224
225key.lower = (GBT_NUMKEY *) &kkk->lower;
226key.upper = (GBT_NUMKEY *) &kkk->upper;
227
228PG_RETURN_BOOL(
229gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
230);
231}
232
233
234Datum
235gbt_intv_distance(PG_FUNCTION_ARGS)
236{
237GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
238Interval *query = PG_GETARG_INTERVAL_P(1);
239
240/* Oid subtype = PG_GETARG_OID(3); */
241intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
242GBT_NUMKEY_R key;
243
244key.lower = (GBT_NUMKEY *) &kkk->lower;
245key.upper = (GBT_NUMKEY *) &kkk->upper;
246
247PG_RETURN_FLOAT8(
248gbt_num_distance(&key, (void *) query, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
249);
250}
251
252
253Datum
254gbt_intv_union(PG_FUNCTION_ARGS)
255{
256GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
257void *out = palloc(sizeof(intvKEY));
258
259*(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
260PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
261}
262
263
264Datum
265gbt_intv_penalty(PG_FUNCTION_ARGS)
266{
267intvKEY *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
268intvKEY *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
269float *result = (float *) PG_GETARG_POINTER(2);
270double iorg[2],
271inew[2];
272
273iorg[0] = intr2num(&origentry->lower);
274iorg[1] = intr2num(&origentry->upper);
275inew[0] = intr2num(&newentry->lower);
276inew[1] = intr2num(&newentry->upper);
277
278penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
279
280PG_RETURN_POINTER(result);
281
282}
283
284Datum
285gbt_intv_picksplit(PG_FUNCTION_ARGS)
286{
287PG_RETURN_POINTER(gbt_num_picksplit(
288(GistEntryVector *) PG_GETARG_POINTER(0),
289(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
290&tinfo, fcinfo->flinfo
291));
292}
293
294Datum
295gbt_intv_same(PG_FUNCTION_ARGS)
296{
297intvKEY *b1 = (intvKEY *) PG_GETARG_POINTER(0);
298intvKEY *b2 = (intvKEY *) PG_GETARG_POINTER(1);
299bool *result = (bool *) PG_GETARG_POINTER(2);
300
301*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
302PG_RETURN_POINTER(result);
303}
304