PostgreSQL Source Code  git master
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

◆ INTERVALSIZE

#define INTERVALSIZE   16

Definition at line 96 of file btree_interval.c.

Function Documentation

◆ abs_interval()

Interval* abs_interval ( Interval a)

Definition at line 114 of file btree_interval.c.

115 {
116  static Interval zero = {0, 0, 0};
117 
120  IntervalPGetDatum(&zero))))
122  IntervalPGetDatum(a)));
123 
124  return a;
125 }
Datum interval_lt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2433
Datum interval_um(PG_FUNCTION_ARGS)
Definition: timestamp.c:3103
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:633
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
int a
Definition: isn.c:69
#define DatumGetBool(X)
Definition: postgres.h:437
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
#define IntervalPGetDatum(X)
Definition: timestamp.h:33

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

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

◆ gbt_intv_compress()

Datum gbt_intv_compress ( PG_FUNCTION_ARGS  )

Definition at line 145 of file btree_interval.c.

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 }
#define INTERVALSIZE
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:242
void * palloc(Size size)
Definition: mcxt.c:1068
#define DatumGetPointer(X)
Definition: postgres.h:593
#define PointerGetDatum(X)
Definition: postgres.h:600
OffsetNumber offset
Definition: gist.h:161
Datum key
Definition: gist.h:158
Page page
Definition: gist.h:160
Relation rel
Definition: gist.h:159
bool leafkey
Definition: gist.h:162

References DatumGetIntervalP, DatumGetPointer, gistentryinit, INTERVALSIZE, GISTENTRY::key, sort-test::key, GISTENTRY::leafkey, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, and GISTENTRY::rel.

◆ gbt_intv_consistent()

Datum gbt_intv_consistent ( PG_FUNCTION_ARGS  )

Definition at line 210 of file btree_interval.c.

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 }
static 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)
char GBT_NUMKEY
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:168
uint16 StrategyNumber
Definition: stratnum.h:22
Interval lower
Interval upper
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37

References DatumGetPointer, gbt_num_consistent(), GIST_LEAF, GISTENTRY::key, sort-test::key, intvKEY::lower, PG_GETARG_INTERVAL_P, PG_GETARG_POINTER, PG_GETARG_UINT16, PG_RETURN_BOOL, tinfo, and intvKEY::upper.

◆ gbt_intv_decompress()

Datum gbt_intv_decompress ( PG_FUNCTION_ARGS  )

Definition at line 187 of file btree_interval.c.

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 }

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

◆ gbt_intv_dist()

static float8 gbt_intv_dist ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 84 of file btree_interval.c.

85 {
86  return (float8) Abs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
87 }
static double intr2num(const Interval *i)
double float8
Definition: c.h:565
#define Abs(x)
Definition: c.h:992
int b
Definition: isn.c:70

References a, Abs, b, and intr2num().

◆ gbt_intv_distance()

Datum gbt_intv_distance ( PG_FUNCTION_ARGS  )

Definition at line 233 of file btree_interval.c.

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 }
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:367

References DatumGetPointer, gbt_num_distance(), GIST_LEAF, GISTENTRY::key, sort-test::key, intvKEY::lower, PG_GETARG_INTERVAL_P, PG_GETARG_POINTER, PG_RETURN_FLOAT8, tinfo, and intvKEY::upper.

◆ gbt_intv_fetch()

Datum gbt_intv_fetch ( PG_FUNCTION_ARGS  )

Definition at line 179 of file btree_interval.c.

180 {
181  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
182 
184 }
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)

References gbt_num_fetch(), PG_GETARG_POINTER, PG_RETURN_POINTER, and tinfo.

◆ gbt_intv_penalty()

Datum gbt_intv_penalty ( PG_FUNCTION_ARGS  )

Definition at line 262 of file btree_interval.c.

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 }
#define penalty_num(result, olower, oupper, nlower, nupper)

References DatumGetPointer, intr2num(), sort-test::key, intvKEY::lower, penalty_num, PG_GETARG_POINTER, PG_RETURN_POINTER, and intvKEY::upper.

◆ gbt_intv_picksplit()

Datum gbt_intv_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 281 of file btree_interval.c.

282 {
285  &tinfo, fcinfo->flinfo));
286 }
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)

References gbt_num_picksplit(), PG_GETARG_POINTER, PG_RETURN_POINTER, and tinfo.

◆ gbt_intv_same()

Datum gbt_intv_same ( PG_FUNCTION_ARGS  )

Definition at line 289 of file btree_interval.c.

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 }
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)

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

◆ gbt_intv_union()

Datum gbt_intv_union ( PG_FUNCTION_ARGS  )

Definition at line 251 of file btree_interval.c.

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 }
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)

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

◆ gbt_intveq()

static bool gbt_intveq ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 45 of file btree_interval.c.

46 {
48 }
Datum interval_eq(PG_FUNCTION_ARGS)
Definition: timestamp.c:2415

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

◆ gbt_intvge()

static bool gbt_intvge ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 39 of file btree_interval.c.

40 {
42 }
Datum interval_ge(PG_FUNCTION_ARGS)
Definition: timestamp.c:2460

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

◆ gbt_intvgt()

static bool gbt_intvgt ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 33 of file btree_interval.c.

34 {
36 }
Datum interval_gt(PG_FUNCTION_ARGS)
Definition: timestamp.c:2442

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

◆ gbt_intvkey_cmp()

static int gbt_intvkey_cmp ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 63 of file btree_interval.c.

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 }
Datum interval_cmp(PG_FUNCTION_ARGS)
Definition: timestamp.c:2469
#define DatumGetInt32(X)
Definition: postgres.h:516

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

◆ gbt_intvle()

static bool gbt_intvle ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

Definition at line 51 of file btree_interval.c.

52 {
54 }
Datum interval_le(PG_FUNCTION_ARGS)
Definition: timestamp.c:2451

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

◆ gbt_intvlt()

static bool gbt_intvlt ( const void *  a,
const void *  b,
FmgrInfo flinfo 
)
static

◆ interval_dist()

Datum interval_dist ( PG_FUNCTION_ARGS  )

Definition at line 129 of file btree_interval.c.

130 {
132  PG_GETARG_DATUM(0),
133  PG_GETARG_DATUM(1));
134 
136 }
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3195
Interval * abs_interval(Interval *a)
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41

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

◆ intr2num()

static double intr2num ( const Interval i)
static

Definition at line 78 of file btree_interval.c.

79 {
80  return INTERVAL_TO_SEC(i);
81 }
#define INTERVAL_TO_SEC(ivp)
int i
Definition: isn.c:73

References i, and INTERVAL_TO_SEC.

Referenced by gbt_intv_dist(), and gbt_intv_penalty().

◆ PG_FUNCTION_INFO_V1() [1/10]

PG_FUNCTION_INFO_V1 ( gbt_intv_compress  )

◆ PG_FUNCTION_INFO_V1() [2/10]

PG_FUNCTION_INFO_V1 ( gbt_intv_consistent  )

◆ PG_FUNCTION_INFO_V1() [3/10]

PG_FUNCTION_INFO_V1 ( gbt_intv_decompress  )

◆ PG_FUNCTION_INFO_V1() [4/10]

PG_FUNCTION_INFO_V1 ( gbt_intv_distance  )

◆ PG_FUNCTION_INFO_V1() [5/10]

PG_FUNCTION_INFO_V1 ( gbt_intv_fetch  )

◆ PG_FUNCTION_INFO_V1() [6/10]

PG_FUNCTION_INFO_V1 ( gbt_intv_penalty  )

◆ PG_FUNCTION_INFO_V1() [7/10]

PG_FUNCTION_INFO_V1 ( gbt_intv_picksplit  )

◆ PG_FUNCTION_INFO_V1() [8/10]

PG_FUNCTION_INFO_V1 ( gbt_intv_same  )

◆ PG_FUNCTION_INFO_V1() [9/10]

PG_FUNCTION_INFO_V1 ( gbt_intv_union  )

◆ PG_FUNCTION_INFO_V1() [10/10]

PG_FUNCTION_INFO_V1 ( interval_dist  )

Variable Documentation

◆ tinfo

const gbtree_ninfo tinfo
static
Initial value:
=
{
sizeof(Interval),
32,
}
@ gbt_t_intv
Definition: btree_gist.h:28
static bool gbt_intvge(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intvle(const void *a, const void *b, FmgrInfo *flinfo)
static float8 gbt_intv_dist(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)
static int gbt_intvkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
static bool gbt_intveq(const void *a, const void *b, FmgrInfo *flinfo)

Definition at line 98 of file btree_interval.c.

Referenced by gbt_intv_consistent(), gbt_intv_distance(), gbt_intv_fetch(), gbt_intv_picksplit(), gbt_intv_same(), and gbt_intv_union().