PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 {
14  upper;
15 } intvKEY;
16 
17 
18 /*
19 ** Interval ops
20 */
30 
31 
32 static bool
33 gbt_intvgt(const void *a, const void *b)
34 {
36 }
37 
38 static bool
39 gbt_intvge(const void *a, const void *b)
40 {
42 }
43 
44 static bool
45 gbt_intveq(const void *a, const void *b)
46 {
48 }
49 
50 static bool
51 gbt_intvle(const void *a, const void *b)
52 {
54 }
55 
56 static bool
57 gbt_intvlt(const void *a, const void *b)
58 {
60 }
61 
62 static int
63 gbt_intvkey_cmp(const void *a, const void *b)
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)
85 {
86  return (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 
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((void *) r, (void *) key, INTERVALSIZE);
161  memcpy((void *) (r + INTERVALSIZE), (void *) 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 
179 Datum
181 {
182  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
183 
184  PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
185 }
186 
187 Datum
189 {
190  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
191  GISTENTRY *retval = entry;
192 
193  if (INTERVALSIZE != sizeof(Interval))
194  {
195  intvKEY *r = palloc(sizeof(intvKEY));
196  char *key = DatumGetPointer(entry->key);
197 
198  retval = palloc(sizeof(GISTENTRY));
199  memcpy(&r->lower, key, INTERVALSIZE);
200  memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
201 
202  gistentryinit(*retval, PointerGetDatum(r),
203  entry->rel, entry->page,
204  entry->offset, FALSE);
205  }
206  PG_RETURN_POINTER(retval);
207 }
208 
209 
210 Datum
212 {
213  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
214  Interval *query = PG_GETARG_INTERVAL_P(1);
216 
217  /* Oid subtype = PG_GETARG_OID(3); */
218  bool *recheck = (bool *) PG_GETARG_POINTER(4);
219  intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
220  GBT_NUMKEY_R key;
221 
222  /* All cases served by this function are exact */
223  *recheck = false;
224 
225  key.lower = (GBT_NUMKEY *) &kkk->lower;
226  key.upper = (GBT_NUMKEY *) &kkk->upper;
227 
229  gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
230  );
231 }
232 
233 
234 Datum
236 {
237  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
238  Interval *query = PG_GETARG_INTERVAL_P(1);
239 
240  /* Oid subtype = PG_GETARG_OID(3); */
241  intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
242  GBT_NUMKEY_R key;
243 
244  key.lower = (GBT_NUMKEY *) &kkk->lower;
245  key.upper = (GBT_NUMKEY *) &kkk->upper;
246 
248  gbt_num_distance(&key, (void *) query, GIST_LEAF(entry), &tinfo)
249  );
250 }
251 
252 
253 Datum
255 {
257  void *out = palloc(sizeof(intvKEY));
258 
259  *(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
260  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
261 }
262 
263 
264 Datum
266 {
267  intvKEY *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
268  intvKEY *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
269  float *result = (float *) PG_GETARG_POINTER(2);
270  double iorg[2],
271  inew[2];
272 
273  iorg[0] = intr2num(&origentry->lower);
274  iorg[1] = intr2num(&origentry->upper);
275  inew[0] = intr2num(&newentry->lower);
276  inew[1] = intr2num(&newentry->upper);
277 
278  penalty_num(result, iorg[0], iorg[1], inew[0], inew[1]);
279 
280  PG_RETURN_POINTER(result);
281 
282 }
283 
284 Datum
286 {
290  &tinfo
291  ));
292 }
293 
294 Datum
296 {
297  intvKEY *b1 = (intvKEY *) PG_GETARG_POINTER(0);
298  intvKEY *b2 = (intvKEY *) PG_GETARG_POINTER(1);
299  bool *result = (bool *) PG_GETARG_POINTER(2);
300 
301  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
302  PG_RETURN_POINTER(result);
303 }
#define GIST_LEAF(entry)
Definition: gist.h:133
Datum interval_dist(PG_FUNCTION_ARGS)
Relation rel
Definition: gist.h:124
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define DatumGetIntervalP(X)
Definition: timestamp.h:49
const GBT_NUMKEY * lower
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
#define DatumGetInt32(X)
Definition: postgres.h:480
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:43
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
Datum gbt_intv_consistent(PG_FUNCTION_ARGS)
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
Datum gbt_intv_decompress(PG_FUNCTION_ARGS)
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:310
uint16 StrategyNumber
Definition: stratnum.h:22
const GBT_NUMKEY * upper
PG_FUNCTION_INFO_V1(gbt_intv_compress)
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
static double intr2num(const Interval *i)
Datum gbt_intv_union(PG_FUNCTION_ARGS)
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2686
Datum gbt_intv_distance(PG_FUNCTION_ARGS)
static bool gbt_intvge(const void *a, const void *b)
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3390
Page page
Definition: gist.h:125
#define Abs(x)
Definition: c.h:807
static int gbt_intvkey_cmp(const void *a, const void *b)
double float8
Definition: c.h:377
#define IntervalPGetDatum(X)
Definition: timestamp.h:53
#define FALSE
Definition: c.h:218
#define penalty_num(result, olower, oupper, nlower, nupper)
Interval upper
Datum interval_lt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2650
static bool gbt_intvgt(const void *a, const void *b)
Datum gbt_intv_same(PG_FUNCTION_ARGS)
Datum key
Definition: gist.h:123
char GBT_NUMKEY
#define DatumGetBool(X)
Definition: postgres.h:401
Datum interval_ge(PG_FUNCTION_ARGS)
Definition: timestamp.c:2677
Datum interval_le(PG_FUNCTION_ARGS)
Definition: timestamp.c:2668
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo)
Datum gbt_intv_fetch(PG_FUNCTION_ARGS)
bool leafkey
Definition: gist.h:127
Datum interval_eq(PG_FUNCTION_ARGS)
Definition: timestamp.c:2632
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo)
Datum interval_um(PG_FUNCTION_ARGS)
Definition: timestamp.c:3298
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
static bool gbt_intvle(const void *a, const void *b)
Interval * abs_interval(Interval *a)
Interval lower
static const gbtree_ninfo tinfo
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo)
Datum gbt_intv_penalty(PG_FUNCTION_ARGS)
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:228
#define DatumGetPointer(X)
Definition: postgres.h:557
static bool gbt_intveq(const void *a, const void *b)
void * palloc(Size size)
Definition: mcxt.c:891
#define INTERVALSIZE
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo)
int i
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
Datum interval_gt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2659
static float8 gbt_intv_dist(const void *a, const void *b)
Datum gbt_intv_compress(PG_FUNCTION_ARGS)
static bool gbt_intvlt(const void *a, const void *b)
#define INTERVAL_TO_SEC(ivp)
OffsetNumber offset
Definition: gist.h:126
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo)
Datum gbt_intv_picksplit(PG_FUNCTION_ARGS)