PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
btree_interval.c File Reference
#include "postgres.h"
#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/builtins.h"
#include "utils/timestamp.h"
Include dependency graph for btree_interval.c:

Go to the source code of this file.

Data Structures

struct  intvKEY
 

Macros

#define INTERVALSIZE   16
 

Functions

 PG_FUNCTION_INFO_V1 (gbt_intv_compress)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_fetch)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_decompress)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_union)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_picksplit)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_consistent)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_distance)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_penalty)
 
 PG_FUNCTION_INFO_V1 (gbt_intv_same)
 
static bool gbt_intvgt (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_intvge (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_intveq (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_intvle (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 double intr2num (const Interval *i)
 
static float8 gbt_intv_dist (const void *a, const void *b, FmgrInfo *flinfo)
 
Intervalabs_interval (Interval *a)
 
 PG_FUNCTION_INFO_V1 (interval_dist)
 
Datum interval_dist (PG_FUNCTION_ARGS)
 
Datum gbt_intv_compress (PG_FUNCTION_ARGS)
 
Datum gbt_intv_fetch (PG_FUNCTION_ARGS)
 
Datum gbt_intv_decompress (PG_FUNCTION_ARGS)
 
Datum gbt_intv_consistent (PG_FUNCTION_ARGS)
 
Datum gbt_intv_distance (PG_FUNCTION_ARGS)
 
Datum gbt_intv_union (PG_FUNCTION_ARGS)
 
Datum gbt_intv_penalty (PG_FUNCTION_ARGS)
 
Datum gbt_intv_picksplit (PG_FUNCTION_ARGS)
 
Datum gbt_intv_same (PG_FUNCTION_ARGS)
 

Variables

static const gbtree_ninfo tinfo
 

Macro Definition Documentation

#define INTERVALSIZE   16

Definition at line 96 of file btree_interval.c.

Referenced by gbt_intv_compress(), and gbt_intv_decompress().

Function Documentation

Interval* abs_interval ( Interval a)

Definition at line 114 of file btree_interval.c.

References DatumGetBool, DatumGetIntervalP, DirectFunctionCall1, DirectFunctionCall2, interval_lt(), interval_um(), and IntervalPGetDatum.

Referenced by interval_dist(), time_dist(), ts_dist(), and tstz_dist().

115 {
116  static Interval zero = {0, 0, 0};
117 
120  IntervalPGetDatum(&zero))))
122  IntervalPGetDatum(a)));
123 
124  return a;
125 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
Datum interval_lt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2353
#define DatumGetBool(X)
Definition: postgres.h:399
Datum interval_um(PG_FUNCTION_ARGS)
Definition: timestamp.c:2982
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum gbt_intv_compress ( PG_FUNCTION_ARGS  )

Definition at line 145 of file btree_interval.c.

References DatumGetIntervalP, DatumGetPointer, FALSE, gistentryinit, INTERVALSIZE, GISTENTRY::key, GISTENTRY::leafkey, intvKEY::lower, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, GISTENTRY::rel, and intvKEY::upper.

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 }
Relation rel
Definition: gist.h:124
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Page page
Definition: gist.h:125
#define FALSE
Definition: c.h:221
Interval upper
Datum key
Definition: gist.h:123
bool leafkey
Definition: gist.h:127
Interval lower
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
#define DatumGetPointer(X)
Definition: postgres.h:555
void * palloc(Size size)
Definition: mcxt.c:849
#define INTERVALSIZE
OffsetNumber offset
Definition: gist.h:126
Datum gbt_intv_consistent ( PG_FUNCTION_ARGS  )

Definition at line 211 of file btree_interval.c.

References DatumGetPointer, gbt_num_consistent(), GIST_LEAF, GISTENTRY::key, intvKEY::lower, GBT_NUMKEY_R::lower, PG_GETARG_INTERVAL_P, PG_GETARG_POINTER, PG_GETARG_UINT16, PG_RETURN_BOOL, intvKEY::upper, and GBT_NUMKEY_R::upper.

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, fcinfo->flinfo)
230  );
231 }
#define GIST_LEAF(entry)
Definition: gist.h:133
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
const GBT_NUMKEY * lower
uint16 StrategyNumber
Definition: stratnum.h:22
const GBT_NUMKEY * upper
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Interval upper
Datum key
Definition: gist.h:123
char GBT_NUMKEY
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Interval lower
static const gbtree_ninfo tinfo
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
#define DatumGetPointer(X)
Definition: postgres.h:555
Datum gbt_intv_decompress ( PG_FUNCTION_ARGS  )

Definition at line 188 of file btree_interval.c.

References DatumGetPointer, FALSE, gistentryinit, INTERVALSIZE, GISTENTRY::key, intvKEY::lower, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, GISTENTRY::rel, and intvKEY::upper.

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 }
Relation rel
Definition: gist.h:124
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Page page
Definition: gist.h:125
#define FALSE
Definition: c.h:221
Interval upper
Datum key
Definition: gist.h:123
Interval lower
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
#define DatumGetPointer(X)
Definition: postgres.h:555
void * palloc(Size size)
Definition: mcxt.c:849
#define INTERVALSIZE
OffsetNumber offset
Definition: gist.h:126
static float8 gbt_intv_dist ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 84 of file btree_interval.c.

References Abs, and intr2num().

85 {
86  return (float8) Abs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
87 }
static double intr2num(const Interval *i)
#define Abs(x)
Definition: c.h:812
double float8
Definition: c.h:381
Datum gbt_intv_distance ( PG_FUNCTION_ARGS  )

Definition at line 235 of file btree_interval.c.

References DatumGetPointer, gbt_num_distance(), GIST_LEAF, GISTENTRY::key, intvKEY::lower, GBT_NUMKEY_R::lower, PG_GETARG_INTERVAL_P, PG_GETARG_POINTER, PG_RETURN_FLOAT8, intvKEY::upper, and GBT_NUMKEY_R::upper.

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, fcinfo->flinfo)
249  );
250 }
#define GIST_LEAF(entry)
Definition: gist.h:133
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
const GBT_NUMKEY * lower
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
const GBT_NUMKEY * upper
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Interval upper
Datum key
Definition: gist.h:123
char GBT_NUMKEY
Interval lower
static const gbtree_ninfo tinfo
#define DatumGetPointer(X)
Definition: postgres.h:555
Datum gbt_intv_fetch ( PG_FUNCTION_ARGS  )

Definition at line 180 of file btree_interval.c.

References gbt_num_fetch(), PG_GETARG_POINTER, and PG_RETURN_POINTER.

181 {
182  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
183 
185 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static const gbtree_ninfo tinfo
Datum gbt_intv_penalty ( PG_FUNCTION_ARGS  )

Definition at line 265 of file btree_interval.c.

References DatumGetPointer, intr2num(), intvKEY::lower, penalty_num, PG_GETARG_POINTER, PG_RETURN_POINTER, result, and intvKEY::upper.

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 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static double intr2num(const Interval *i)
#define penalty_num(result, olower, oupper, nlower, nupper)
Interval upper
Interval lower
#define DatumGetPointer(X)
Definition: postgres.h:555
Datum gbt_intv_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 285 of file btree_interval.c.

References gbt_num_picksplit(), PG_GETARG_POINTER, and PG_RETURN_POINTER.

286 {
290  &tinfo, fcinfo->flinfo
291  ));
292 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static const gbtree_ninfo tinfo
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Datum gbt_intv_same ( PG_FUNCTION_ARGS  )

Definition at line 295 of file btree_interval.c.

References gbt_num_same(), PG_GETARG_POINTER, PG_RETURN_POINTER, and result.

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, fcinfo->flinfo);
302  PG_RETURN_POINTER(result);
303 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static const gbtree_ninfo tinfo
Datum gbt_intv_union ( PG_FUNCTION_ARGS  )

Definition at line 254 of file btree_interval.c.

References gbt_num_union(), palloc(), PG_GETARG_POINTER, and PG_RETURN_POINTER.

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, fcinfo->flinfo));
261 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static const gbtree_ninfo tinfo
void * palloc(Size size)
Definition: mcxt.c:849
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
static bool gbt_intveq ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 45 of file btree_interval.c.

References DatumGetBool, DirectFunctionCall2, interval_eq(), and IntervalPGetDatum.

46 {
48 }
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define DatumGetBool(X)
Definition: postgres.h:399
Datum interval_eq(PG_FUNCTION_ARGS)
Definition: timestamp.c:2335
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static bool gbt_intvge ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 39 of file btree_interval.c.

References DatumGetBool, DirectFunctionCall2, interval_ge(), and IntervalPGetDatum.

40 {
42 }
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define DatumGetBool(X)
Definition: postgres.h:399
Datum interval_ge(PG_FUNCTION_ARGS)
Definition: timestamp.c:2380
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static bool gbt_intvgt ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 33 of file btree_interval.c.

References DatumGetBool, DirectFunctionCall2, interval_gt(), and IntervalPGetDatum.

34 {
36 }
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define DatumGetBool(X)
Definition: postgres.h:399
Datum interval_gt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2362
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static int gbt_intvkey_cmp ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 63 of file btree_interval.c.

References DatumGetInt32, DirectFunctionCall2, interval_cmp(), IntervalPGetDatum, intvKEY::lower, and intvKEY::upper.

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 }
#define DatumGetInt32(X)
Definition: postgres.h:478
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2389
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
Interval upper
Interval lower
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static bool gbt_intvle ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 51 of file btree_interval.c.

References DatumGetBool, DirectFunctionCall2, interval_le(), and IntervalPGetDatum.

52 {
54 }
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define DatumGetBool(X)
Definition: postgres.h:399
Datum interval_le(PG_FUNCTION_ARGS)
Definition: timestamp.c:2371
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static bool gbt_intvlt ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 57 of file btree_interval.c.

References DatumGetBool, DirectFunctionCall2, interval_lt(), and IntervalPGetDatum.

58 {
60 }
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
Datum interval_lt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2353
#define DatumGetBool(X)
Definition: postgres.h:399
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum interval_dist ( PG_FUNCTION_ARGS  )

Definition at line 129 of file btree_interval.c.

References abs_interval(), DatumGetIntervalP, DirectFunctionCall2, interval_mi(), PG_GETARG_DATUM, and PG_RETURN_INTERVAL_P.

130 {
132  PG_GETARG_DATUM(0),
133  PG_GETARG_DATUM(1));
134 
136 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3074
uintptr_t Datum
Definition: postgres.h:372
Interval * abs_interval(Interval *a)
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static double intr2num ( const Interval i)
static

Definition at line 78 of file btree_interval.c.

References INTERVAL_TO_SEC.

Referenced by gbt_intv_dist(), and gbt_intv_penalty().

79 {
80  return INTERVAL_TO_SEC(i);
81 }
#define INTERVAL_TO_SEC(ivp)
PG_FUNCTION_INFO_V1 ( gbt_intv_compress  )
PG_FUNCTION_INFO_V1 ( gbt_intv_fetch  )
PG_FUNCTION_INFO_V1 ( gbt_intv_decompress  )
PG_FUNCTION_INFO_V1 ( gbt_intv_union  )
PG_FUNCTION_INFO_V1 ( gbt_intv_picksplit  )
PG_FUNCTION_INFO_V1 ( gbt_intv_consistent  )
PG_FUNCTION_INFO_V1 ( gbt_intv_distance  )
PG_FUNCTION_INFO_V1 ( gbt_intv_penalty  )
PG_FUNCTION_INFO_V1 ( gbt_intv_same  )
PG_FUNCTION_INFO_V1 ( interval_dist  )

Variable Documentation

const gbtree_ninfo tinfo
static
Initial value:
=
{
sizeof(Interval),
32,
}
static bool gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
static int gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
static float8 gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intvgt(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intvlt(const void *a, const void *b, FmgrInfo *flinfo)

Definition at line 98 of file btree_interval.c.