PostgreSQL Source Code git master
Loading...
Searching...
No Matches
btree_interval.c
Go to the documentation of this file.
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
13typedef struct
14{
17} intvKEY;
18
19/* GiST support functions */
30
31
32static bool
33gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
34{
36}
37
38static bool
39gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
40{
42}
43
44static bool
45gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
46{
48}
49
50static bool
51gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
52{
54}
55
56static bool
57gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
58{
60}
61
62static int
63gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
64{
65 intvKEY *ia = (intvKEY *) (((const Nsrt *) a)->t);
66 intvKEY *ib = (intvKEY *) (((const Nsrt *) b)->t);
67 int res;
68
70 if (res == 0)
72
73 return res;
74}
75
76
77static double
79{
80 return INTERVAL_TO_SEC(i);
81}
82
83static float8
84gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
85{
86 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
98static const gbtree_ninfo tinfo =
99{
101 sizeof(Interval),
102 32, /* sizeof(gbtreekey32) */
110};
111
112
113Interval *
126
128Datum
137
138
139/**************************************************
140 * GiST support functions
141 **************************************************/
142
143Datum
145{
146 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
147 GISTENTRY *retval = entry;
148
149 if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
150 {
151 char *r = (char *) palloc(2 * INTERVALSIZE);
152
153 retval = palloc_object(GISTENTRY);
154
155 if (entry->leafkey)
156 {
157 Interval *key = DatumGetIntervalP(entry->key);
158
159 memcpy(r, key, INTERVALSIZE);
161 }
162 else
163 {
164 intvKEY *key = (intvKEY *) DatumGetPointer(entry->key);
165
166 memcpy(r, &key->lower, INTERVALSIZE);
167 memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
168 }
169 gistentryinit(*retval, PointerGetDatum(r),
170 entry->rel, entry->page,
171 entry->offset, false);
172 }
173
174 PG_RETURN_POINTER(retval);
175}
176
177Datum
184
185Datum
187{
188 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
189 GISTENTRY *retval = entry;
190
191 if (INTERVALSIZE != sizeof(Interval))
192 {
194 char *key = DatumGetPointer(entry->key);
195
196 retval = palloc_object(GISTENTRY);
197 memcpy(&r->lower, key, INTERVALSIZE);
199
200 gistentryinit(*retval, PointerGetDatum(r),
201 entry->rel, entry->page,
202 entry->offset, false);
203 }
204 PG_RETURN_POINTER(retval);
205}
206
207
208Datum
210{
211 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
212 Interval *query = PG_GETARG_INTERVAL_P(1);
214#ifdef NOT_USED
215 Oid subtype = PG_GETARG_OID(3);
216#endif
217 bool *recheck = (bool *) PG_GETARG_POINTER(4);
218 intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
219 GBT_NUMKEY_R key;
220
221 /* All cases served by this function are exact */
222 *recheck = false;
223
224 key.lower = (GBT_NUMKEY *) &kkk->lower;
225 key.upper = (GBT_NUMKEY *) &kkk->upper;
226
227 PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
228 GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
229}
230
231
232Datum
234{
235 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
236 Interval *query = PG_GETARG_INTERVAL_P(1);
237#ifdef NOT_USED
238 Oid subtype = PG_GETARG_OID(3);
239#endif
240 intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
241 GBT_NUMKEY_R key;
242
243 key.lower = (GBT_NUMKEY *) &kkk->lower;
244 key.upper = (GBT_NUMKEY *) &kkk->upper;
245
246 PG_RETURN_FLOAT8(gbt_num_distance(&key, query, GIST_LEAF(entry),
247 &tinfo, fcinfo->flinfo));
248}
249
250
251Datum
253{
255 void *out = palloc(sizeof(intvKEY));
256
257 *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
258 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
259}
260
261
262Datum
264{
267 float *result = (float *) PG_GETARG_POINTER(2);
268 double iorg[2],
269 inew[2];
270
271 iorg[0] = intr2num(&origentry->lower);
272 iorg[1] = intr2num(&origentry->upper);
273 inew[0] = intr2num(&newentry->lower);
274 inew[1] = intr2num(&newentry->upper);
275
276 penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
277
278 PG_RETURN_POINTER(result);
279}
280
281Datum
288
289Datum
291{
294 bool *result = (bool *) PG_GETARG_POINTER(2);
295
296 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
297 PG_RETURN_POINTER(result);
298}
299
300static int
302{
305
306 /* for leaf items we expect lower == upper, so only compare lower */
308 IntervalPGetDatum(&arg1->lower),
309 IntervalPGetDatum(&arg2->lower)));
310}
311
312Datum
Datum interval_gt(PG_FUNCTION_ARGS)
Definition timestamp.c:2591
Datum interval_le(PG_FUNCTION_ARGS)
Definition timestamp.c:2600
Datum interval_mi(PG_FUNCTION_ARGS)
Definition timestamp.c:3559
Datum interval_lt(PG_FUNCTION_ARGS)
Definition timestamp.c:2582
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition timestamp.c:2618
Datum interval_um(PG_FUNCTION_ARGS)
Definition timestamp.c:3446
Datum interval_ge(PG_FUNCTION_ARGS)
Definition timestamp.c:2609
Datum interval_eq(PG_FUNCTION_ARGS)
Definition timestamp.c:2564
@ gbt_t_intv
Definition btree_gist.h:28
Datum gbt_intv_compress(PG_FUNCTION_ARGS)
static bool gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
Datum gbt_intv_decompress(PG_FUNCTION_ARGS)
Interval * abs_interval(Interval *a)
Datum gbt_intv_distance(PG_FUNCTION_ARGS)
static bool gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
Datum gbt_intv_fetch(PG_FUNCTION_ARGS)
static int gbt_intv_ssup_cmp(Datum x, Datum y, SortSupport ssup)
Datum gbt_intv_union(PG_FUNCTION_ARGS)
static double intr2num(const Interval *i)
static float8 gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
Datum gbt_intv_penalty(PG_FUNCTION_ARGS)
Datum gbt_intv_consistent(PG_FUNCTION_ARGS)
static bool gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
static int gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
static const gbtree_ninfo tinfo
Datum gbt_intv_picksplit(PG_FUNCTION_ARGS)
Datum gbt_intv_same(PG_FUNCTION_ARGS)
Datum interval_dist(PG_FUNCTION_ARGS)
static bool gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
#define INTERVALSIZE
Datum gbt_intv_sortsupport(PG_FUNCTION_ARGS)
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define penalty_num(result, olower, oupper, nlower, nupper)
#define INTERVAL_TO_SEC(ivp)
char GBT_NUMKEY
double float8
Definition c.h:644
#define palloc_object(type)
Definition fe_memutils.h:74
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:686
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_GETARG_UINT16(n)
Definition fmgr.h:272
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define GIST_LEAF(entry)
Definition gist.h:171
#define gistentryinit(e, k, r, pg, o, l)
Definition gist.h:245
int y
Definition isn.c:76
int b
Definition isn.c:74
int x
Definition isn.c:75
int a
Definition isn.c:73
int i
Definition isn.c:77
void * palloc(Size size)
Definition mcxt.c:1387
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212
unsigned int Oid
static int fb(int x)
struct SortSupportData * SortSupport
Definition sortsupport.h:58
uint16 StrategyNumber
Definition stratnum.h:22
OffsetNumber offset
Definition gist.h:164
Datum key
Definition gist.h:161
Page page
Definition gist.h:163
Relation rel
Definition gist.h:162
bool leafkey
Definition gist.h:165
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Interval lower
Interval upper
static Datum IntervalPGetDatum(const Interval *X)
Definition timestamp.h:58
#define PG_GETARG_INTERVAL_P(n)
Definition timestamp.h:65
static Interval * DatumGetIntervalP(Datum X)
Definition timestamp.h:40
#define PG_RETURN_INTERVAL_P(x)
Definition timestamp.h:69