PostgreSQL Source Code  git master
btree_date.c File Reference
#include "postgres.h"
#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/builtins.h"
#include "utils/date.h"
Include dependency graph for btree_date.c:

Go to the source code of this file.

Data Structures

struct  dateKEY
 

Functions

 PG_FUNCTION_INFO_V1 (gbt_date_compress)
 
 PG_FUNCTION_INFO_V1 (gbt_date_fetch)
 
 PG_FUNCTION_INFO_V1 (gbt_date_union)
 
 PG_FUNCTION_INFO_V1 (gbt_date_picksplit)
 
 PG_FUNCTION_INFO_V1 (gbt_date_consistent)
 
 PG_FUNCTION_INFO_V1 (gbt_date_distance)
 
 PG_FUNCTION_INFO_V1 (gbt_date_penalty)
 
 PG_FUNCTION_INFO_V1 (gbt_date_same)
 
static bool gbt_dategt (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_datege (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_dateeq (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_datele (const void *a, const void *b, FmgrInfo *flinfo)
 
static bool gbt_datelt (const void *a, const void *b, FmgrInfo *flinfo)
 
static int gbt_datekey_cmp (const void *a, const void *b, FmgrInfo *flinfo)
 
static float8 gdb_date_dist (const void *a, const void *b, FmgrInfo *flinfo)
 
 PG_FUNCTION_INFO_V1 (date_dist)
 
Datum date_dist (PG_FUNCTION_ARGS)
 
Datum gbt_date_compress (PG_FUNCTION_ARGS)
 
Datum gbt_date_fetch (PG_FUNCTION_ARGS)
 
Datum gbt_date_consistent (PG_FUNCTION_ARGS)
 
Datum gbt_date_distance (PG_FUNCTION_ARGS)
 
Datum gbt_date_union (PG_FUNCTION_ARGS)
 
Datum gbt_date_penalty (PG_FUNCTION_ARGS)
 
Datum gbt_date_picksplit (PG_FUNCTION_ARGS)
 
Datum gbt_date_same (PG_FUNCTION_ARGS)
 

Variables

static const gbtree_ninfo tinfo
 

Function Documentation

◆ date_dist()

Datum date_dist ( PG_FUNCTION_ARGS  )

Definition at line 119 of file btree_date.c.

References Abs, date_mi(), DatumGetInt32, DirectFunctionCall2, PG_GETARG_DATUM, and PG_RETURN_INT32.

120 {
121  /* we assume the difference can't overflow */
123  PG_GETARG_DATUM(0),
124  PG_GETARG_DATUM(1));
125 
127 }
#define DatumGetInt32(X)
Definition: postgres.h:472
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
#define Abs(x)
Definition: c.h:933
uintptr_t Datum
Definition: postgres.h:367
Datum date_mi(PG_FUNCTION_ARGS)
Definition: date.c:492
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ gbt_date_compress()

Datum gbt_date_compress ( PG_FUNCTION_ARGS  )

Definition at line 137 of file btree_date.c.

References gbt_num_compress(), PG_GETARG_POINTER, and PG_RETURN_POINTER.

138 {
139  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140 
142 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
static const gbtree_ninfo tinfo
Definition: btree_date.c:102

◆ gbt_date_consistent()

Datum gbt_date_consistent ( PG_FUNCTION_ARGS  )

Definition at line 153 of file btree_date.c.

References DatumGetPointer, gbt_num_consistent(), GIST_LEAF, sort-test::key, GISTENTRY::key, dateKEY::lower, GBT_NUMKEY_R::lower, PG_GETARG_DATEADT, PG_GETARG_POINTER, PG_GETARG_UINT16, PG_RETURN_BOOL, dateKEY::upper, and GBT_NUMKEY_R::upper.

154 {
155  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
156  DateADT query = PG_GETARG_DATEADT(1);
158 
159  /* Oid subtype = PG_GETARG_OID(3); */
160  bool *recheck = (bool *) PG_GETARG_POINTER(4);
161  dateKEY *kkk = (dateKEY *) DatumGetPointer(entry->key);
163 
164  /* All cases served by this function are exact */
165  *recheck = false;
166 
167  key.lower = (GBT_NUMKEY *) &kkk->lower;
168  key.upper = (GBT_NUMKEY *) &kkk->upper;
169 
170  PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query, &strategy,
171  GIST_LEAF(entry), &tinfo,
172  fcinfo->flinfo));
173 }
#define GIST_LEAF(entry)
Definition: gist.h:161
DateADT lower
Definition: btree_date.c:13
const GBT_NUMKEY * lower
int32 DateADT
Definition: date.h:23
uint16 StrategyNumber
Definition: stratnum.h:22
const GBT_NUMKEY * upper
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
Datum key
Definition: gist.h:151
char GBT_NUMKEY
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
DateADT upper
Definition: btree_date.c:14
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define DatumGetPointer(X)
Definition: postgres.h:549
static const gbtree_ninfo tinfo
Definition: btree_date.c:102

◆ gbt_date_distance()

Datum gbt_date_distance ( PG_FUNCTION_ARGS  )

Definition at line 177 of file btree_date.c.

References DatumGetPointer, gbt_num_distance(), GIST_LEAF, sort-test::key, GISTENTRY::key, dateKEY::lower, GBT_NUMKEY_R::lower, PG_GETARG_DATEADT, PG_GETARG_POINTER, PG_RETURN_FLOAT8, dateKEY::upper, and GBT_NUMKEY_R::upper.

178 {
179  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
180  DateADT query = PG_GETARG_DATEADT(1);
181 
182  /* Oid subtype = PG_GETARG_OID(3); */
183  dateKEY *kkk = (dateKEY *) DatumGetPointer(entry->key);
185 
186  key.lower = (GBT_NUMKEY *) &kkk->lower;
187  key.upper = (GBT_NUMKEY *) &kkk->upper;
188 
189  PG_RETURN_FLOAT8(gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry),
190  &tinfo, fcinfo->flinfo));
191 }
#define GIST_LEAF(entry)
Definition: gist.h:161
DateADT lower
Definition: btree_date.c:13
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)
int32 DateADT
Definition: date.h:23
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:365
const GBT_NUMKEY * upper
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
Datum key
Definition: gist.h:151
char GBT_NUMKEY
#define PG_GETARG_DATEADT(n)
Definition: date.h:61
DateADT upper
Definition: btree_date.c:14
#define DatumGetPointer(X)
Definition: postgres.h:549
static const gbtree_ninfo tinfo
Definition: btree_date.c:102

◆ gbt_date_fetch()

Datum gbt_date_fetch ( PG_FUNCTION_ARGS  )

Definition at line 145 of file btree_date.c.

References gbt_num_fetch(), PG_GETARG_POINTER, and PG_RETURN_POINTER.

146 {
147  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
148 
150 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static const gbtree_ninfo tinfo
Definition: btree_date.c:102

◆ gbt_date_penalty()

Datum gbt_date_penalty ( PG_FUNCTION_ARGS  )

Definition at line 206 of file btree_date.c.

References date_mi(), DateADTGetDatum, DatumGetInt32, DatumGetPointer, DirectFunctionCall2, sort-test::key, dateKEY::lower, Max, PG_GETARG_POINTER, PG_RETURN_POINTER, and dateKEY::upper.

207 {
208  dateKEY *origentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
209  dateKEY *newentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
210  float *result = (float *) PG_GETARG_POINTER(2);
211  int32 diff,
212  res;
213 
215  DateADTGetDatum(newentry->upper),
216  DateADTGetDatum(origentry->upper)));
217 
218  res = Max(diff, 0);
219 
221  DateADTGetDatum(origentry->lower),
222  DateADTGetDatum(newentry->lower)));
223 
224  res += Max(diff, 0);
225 
226  *result = 0.0;
227 
228  if (res > 0)
229  {
231  DateADTGetDatum(origentry->upper),
232  DateADTGetDatum(origentry->lower)));
233  *result += FLT_MIN;
234  *result += (float) (res / ((double) (res + diff)));
235  *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
236  }
237 
238  PG_RETURN_POINTER(result);
239 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
DateADT lower
Definition: btree_date.c:13
#define DatumGetInt32(X)
Definition: postgres.h:472
#define DateADTGetDatum(X)
Definition: date.h:57
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
signed int int32
Definition: c.h:362
#define Max(x, y)
Definition: c.h:921
DateADT upper
Definition: btree_date.c:14
#define DatumGetPointer(X)
Definition: postgres.h:549
Datum date_mi(PG_FUNCTION_ARGS)
Definition: date.c:492
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ gbt_date_picksplit()

Datum gbt_date_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 243 of file btree_date.c.

References gbt_num_picksplit(), PG_GETARG_POINTER, and PG_RETURN_POINTER.

244 {
247  &tinfo, fcinfo->flinfo));
248 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
static const gbtree_ninfo tinfo
Definition: btree_date.c:102

◆ gbt_date_same()

Datum gbt_date_same ( PG_FUNCTION_ARGS  )

Definition at line 251 of file btree_date.c.

References gbt_num_same(), PG_GETARG_POINTER, and PG_RETURN_POINTER.

252 {
253  dateKEY *b1 = (dateKEY *) PG_GETARG_POINTER(0);
254  dateKEY *b2 = (dateKEY *) PG_GETARG_POINTER(1);
255  bool *result = (bool *) PG_GETARG_POINTER(2);
256 
257  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
258  PG_RETURN_POINTER(result);
259 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static const gbtree_ninfo tinfo
Definition: btree_date.c:102

◆ gbt_date_union()

Datum gbt_date_union ( PG_FUNCTION_ARGS  )

Definition at line 195 of file btree_date.c.

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

196 {
198  void *out = palloc(sizeof(dateKEY));
199 
200  *(int *) PG_GETARG_POINTER(1) = sizeof(dateKEY);
201  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
202 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static const gbtree_ninfo tinfo
Definition: btree_date.c:102
void * palloc(Size size)
Definition: mcxt.c:949
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)

◆ gbt_dateeq()

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

Definition at line 46 of file btree_date.c.

References date_eq(), DateADTGetDatum, DatumGetBool, and DirectFunctionCall2.

47 {
49  DateADTGetDatum(*((const DateADT *) a)),
50  DateADTGetDatum(*((const DateADT *) b)))
51  );
52 }
Datum date_eq(PG_FUNCTION_ARGS)
Definition: date.c:375
int32 DateADT
Definition: date.h:23
#define DateADTGetDatum(X)
Definition: date.h:57
#define DatumGetBool(X)
Definition: postgres.h:393
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ gbt_datege()

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

Definition at line 38 of file btree_date.c.

References date_ge(), DateADTGetDatum, DatumGetBool, and DirectFunctionCall2.

39 {
41  DateADTGetDatum(*((const DateADT *) a)),
42  DateADTGetDatum(*((const DateADT *) b))));
43 }
int32 DateADT
Definition: date.h:23
#define DateADTGetDatum(X)
Definition: date.h:57
#define DatumGetBool(X)
Definition: postgres.h:393
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626
Datum date_ge(PG_FUNCTION_ARGS)
Definition: date.c:420

◆ gbt_dategt()

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

Definition at line 30 of file btree_date.c.

References date_gt(), DateADTGetDatum, DatumGetBool, and DirectFunctionCall2.

31 {
33  DateADTGetDatum(*((const DateADT *) a)),
34  DateADTGetDatum(*((const DateADT *) b))));
35 }
int32 DateADT
Definition: date.h:23
#define DateADTGetDatum(X)
Definition: date.h:57
#define DatumGetBool(X)
Definition: postgres.h:393
Datum date_gt(PG_FUNCTION_ARGS)
Definition: date.c:411
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ gbt_datekey_cmp()

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

Definition at line 73 of file btree_date.c.

References date_cmp(), DateADTGetDatum, DatumGetInt32, DirectFunctionCall2, dateKEY::lower, and dateKEY::upper.

74 {
75  dateKEY *ia = (dateKEY *) (((const Nsrt *) a)->t);
76  dateKEY *ib = (dateKEY *) (((const Nsrt *) b)->t);
77  int res;
78 
81  DateADTGetDatum(ib->lower)));
82  if (res == 0)
85  DateADTGetDatum(ib->upper)));
86 
87  return res;
88 }
DateADT lower
Definition: btree_date.c:13
#define DatumGetInt32(X)
Definition: postgres.h:472
#define DateADTGetDatum(X)
Definition: date.h:57
Datum date_cmp(PG_FUNCTION_ARGS)
Definition: date.c:429
DateADT upper
Definition: btree_date.c:14
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ gbt_datele()

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

Definition at line 55 of file btree_date.c.

References date_le(), DateADTGetDatum, DatumGetBool, and DirectFunctionCall2.

56 {
58  DateADTGetDatum(*((const DateADT *) a)),
59  DateADTGetDatum(*((const DateADT *) b))));
60 }
Datum date_le(PG_FUNCTION_ARGS)
Definition: date.c:402
int32 DateADT
Definition: date.h:23
#define DateADTGetDatum(X)
Definition: date.h:57
#define DatumGetBool(X)
Definition: postgres.h:393
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ gbt_datelt()

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

Definition at line 63 of file btree_date.c.

References date_lt(), DateADTGetDatum, DatumGetBool, and DirectFunctionCall2.

64 {
66  DateADTGetDatum(*((const DateADT *) a)),
67  DateADTGetDatum(*((const DateADT *) b))));
68 }
Datum date_lt(PG_FUNCTION_ARGS)
Definition: date.c:393
int32 DateADT
Definition: date.h:23
#define DateADTGetDatum(X)
Definition: date.h:57
#define DatumGetBool(X)
Definition: postgres.h:393
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ gdb_date_dist()

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

Definition at line 91 of file btree_date.c.

References Abs, date_mi(), DateADTGetDatum, DatumGetInt32, and DirectFunctionCall2.

92 {
93  /* we assume the difference can't overflow */
95  DateADTGetDatum(*((const DateADT *) a)),
96  DateADTGetDatum(*((const DateADT *) b)));
97 
98  return (float8) Abs(DatumGetInt32(diff));
99 }
#define DatumGetInt32(X)
Definition: postgres.h:472
int32 DateADT
Definition: date.h:23
#define DateADTGetDatum(X)
Definition: date.h:57
#define Abs(x)
Definition: c.h:933
double float8
Definition: c.h:498
uintptr_t Datum
Definition: postgres.h:367
Datum date_mi(PG_FUNCTION_ARGS)
Definition: date.c:492
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ PG_FUNCTION_INFO_V1() [1/9]

PG_FUNCTION_INFO_V1 ( gbt_date_compress  )

◆ PG_FUNCTION_INFO_V1() [2/9]

PG_FUNCTION_INFO_V1 ( gbt_date_fetch  )

◆ PG_FUNCTION_INFO_V1() [3/9]

PG_FUNCTION_INFO_V1 ( gbt_date_union  )

◆ PG_FUNCTION_INFO_V1() [4/9]

PG_FUNCTION_INFO_V1 ( gbt_date_picksplit  )

◆ PG_FUNCTION_INFO_V1() [5/9]

PG_FUNCTION_INFO_V1 ( gbt_date_consistent  )

◆ PG_FUNCTION_INFO_V1() [6/9]

PG_FUNCTION_INFO_V1 ( gbt_date_distance  )

◆ PG_FUNCTION_INFO_V1() [7/9]

PG_FUNCTION_INFO_V1 ( gbt_date_penalty  )

◆ PG_FUNCTION_INFO_V1() [8/9]

PG_FUNCTION_INFO_V1 ( gbt_date_same  )

◆ PG_FUNCTION_INFO_V1() [9/9]

PG_FUNCTION_INFO_V1 ( date_dist  )

Variable Documentation

◆ tinfo

const gbtree_ninfo tinfo
static
Initial value:
=
{
sizeof(DateADT),
8,
}
static bool gbt_datege(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:38
static int gbt_datekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:73
int32 DateADT
Definition: date.h:23
static bool gbt_datele(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:55
static float8 gdb_date_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:91
static bool gbt_dategt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:30
static bool gbt_dateeq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:46
static bool gbt_datelt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:63

Definition at line 102 of file btree_date.c.