PostgreSQL Source Code  git master
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/builtins.h"
9 #include "utils/timestamp.h"
10 
11 typedef struct
12 {
15 } intvKEY;
16 
17 
18 /*
19 ** Interval ops
20 */
30 
31 
32 static bool
33 gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
34 {
36 }
37 
38 static bool
39 gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
40 {
42 }
43 
44 static bool
45 gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
46 {
48 }
49 
50 static bool
51 gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
52 {
54 }
55 
56 static bool
57 gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)
58 {
60 }
61 
62 static int
63 gbt_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 
77 static double
79 {
80  return INTERVAL_TO_SEC(i);
81 }
82 
83 static float8
84 gbt_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 
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,
110 };
111 
112 
113 Interval *
115 {
116  static Interval zero = {0, 0, 0};
117 
120  IntervalPGetDatum(&zero))))
122  IntervalPGetDatum(a)));
123 
124  return a;
125 }
126 
128 Datum
130 {
132  PG_GETARG_DATUM(0),
133  PG_GETARG_DATUM(1));
134 
136 }
137 
138 
139 /**************************************************
140  * interval ops
141  **************************************************/
142 
143 
144 Datum
146 {
147  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
148  GISTENTRY *retval = entry;
149 
150  if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
151  {
152  char *r = (char *) palloc(2 * INTERVALSIZE);
153 
154  retval = palloc(sizeof(GISTENTRY));
155 
156  if (entry->leafkey)
157  {
158  Interval *key = DatumGetIntervalP(entry->key);
159 
160  memcpy(r, key, INTERVALSIZE);
161  memcpy(r + INTERVALSIZE, key, INTERVALSIZE);
162  }
163  else
164  {
165  intvKEY *key = (intvKEY *) DatumGetPointer(entry->key);
166 
167  memcpy(r, &key->lower, INTERVALSIZE);
168  memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
169  }
170  gistentryinit(*retval, PointerGetDatum(r),
171  entry->rel, entry->page,
172  entry->offset, false);
173  }
174 
175  PG_RETURN_POINTER(retval);
176 }
177 
178 Datum
180 {
181  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
182 
184 }
185 
186 Datum
188 {
189  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
190  GISTENTRY *retval = entry;
191 
192  if (INTERVALSIZE != sizeof(Interval))
193  {
194  intvKEY *r = palloc(sizeof(intvKEY));
195  char *key = DatumGetPointer(entry->key);
196 
197  retval = palloc(sizeof(GISTENTRY));
198  memcpy(&r->lower, key, INTERVALSIZE);
199  memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
200 
201  gistentryinit(*retval, PointerGetDatum(r),
202  entry->rel, entry->page,
203  entry->offset, false);
204  }
205  PG_RETURN_POINTER(retval);
206 }
207 
208 
209 Datum
211 {
212  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
213  Interval *query = PG_GETARG_INTERVAL_P(1);
215 
216  /* Oid subtype = PG_GETARG_OID(3); */
217  bool *recheck = (bool *) PG_GETARG_POINTER(4);
218  intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->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, (void *) query, &strategy,
228  GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
229 }
230 
231 
232 Datum
234 {
235  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
236  Interval *query = PG_GETARG_INTERVAL_P(1);
237 
238  /* Oid subtype = PG_GETARG_OID(3); */
239  intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
241 
242  key.lower = (GBT_NUMKEY *) &kkk->lower;
243  key.upper = (GBT_NUMKEY *) &kkk->upper;
244 
245  PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) query, GIST_LEAF(entry),
246  &tinfo, fcinfo->flinfo));
247 }
248 
249 
250 Datum
252 {
254  void *out = palloc(sizeof(intvKEY));
255 
256  *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
257  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
258 }
259 
260 
261 Datum
263 {
264  intvKEY *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
265  intvKEY *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
266  float *result = (float *) PG_GETARG_POINTER(2);
267  double iorg[2],
268  inew[2];
269 
270  iorg[0] = intr2num(&origentry->lower);
271  iorg[1] = intr2num(&origentry->upper);
272  inew[0] = intr2num(&newentry->lower);
273  inew[1] = intr2num(&newentry->upper);
274 
275  penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
276 
277  PG_RETURN_POINTER(result);
278 }
279 
280 Datum
282 {
285  &tinfo, fcinfo->flinfo));
286 }
287 
288 Datum
290 {
291  intvKEY *b1 = (intvKEY *) PG_GETARG_POINTER(0);
292  intvKEY *b2 = (intvKEY *) PG_GETARG_POINTER(1);
293  bool *result = (bool *) PG_GETARG_POINTER(2);
294 
295  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
296  PG_RETURN_POINTER(result);
297 }
Datum interval_gt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2550
Datum interval_le(PG_FUNCTION_ARGS)
Definition: timestamp.c:2559
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3506
Datum interval_lt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2541
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2577
Datum interval_um(PG_FUNCTION_ARGS)
Definition: timestamp.c:3393
Datum interval_ge(PG_FUNCTION_ARGS)
Definition: timestamp.c:2568
Datum interval_eq(PG_FUNCTION_ARGS)
Definition: timestamp.c:2523
@ gbt_t_intv
Definition: btree_gist.h:28
Interval * abs_interval(Interval *a)
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)
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)
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
PG_FUNCTION_INFO_V1(gbt_intv_compress)
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
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)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, 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:630
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:170
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:244
int b
Definition: isn.c:70
int a
Definition: isn.c:69
int i
Definition: isn.c:73
void * palloc(Size size)
Definition: mcxt.c:1316
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
OffsetNumber offset
Definition: gist.h:163
Datum key
Definition: gist.h:160
Page page
Definition: gist.h:162
Relation rel
Definition: gist.h:161
bool leafkey
Definition: gist.h:164
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