PostgreSQL Source Code  git master
btree_date.c
Go to the documentation of this file.
1 /*
2  * contrib/btree_gist/btree_date.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/date.h"
10 
11 typedef struct
12 {
15 } dateKEY;
16 
17 /*
18 ** date ops
19 */
28 
29 static bool
30 gbt_dategt(const void *a, const void *b, FmgrInfo *flinfo)
31 {
32  return DatumGetBool(
34  );
35 }
36 
37 static bool
38 gbt_datege(const void *a, const void *b, FmgrInfo *flinfo)
39 {
40  return DatumGetBool(
42  );
43 }
44 
45 static bool
46 gbt_dateeq(const void *a, const void *b, FmgrInfo *flinfo)
47 {
48  return DatumGetBool(
50  );
51 }
52 
53 static bool
54 gbt_datele(const void *a, const void *b, FmgrInfo *flinfo)
55 {
56  return DatumGetBool(
58  );
59 }
60 
61 static bool
62 gbt_datelt(const void *a, const void *b, FmgrInfo *flinfo)
63 {
64  return DatumGetBool(
66  );
67 }
68 
69 
70 
71 static int
72 gbt_datekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
73 {
74  dateKEY *ia = (dateKEY *) (((const Nsrt *) a)->t);
75  dateKEY *ib = (dateKEY *) (((const Nsrt *) b)->t);
76  int res;
77 
79  if (res == 0)
81 
82  return res;
83 }
84 
85 static float8
86 gdb_date_dist(const void *a, const void *b, FmgrInfo *flinfo)
87 {
88  /* we assume the difference can't overflow */
90  DateADTGetDatum(*((const DateADT *) a)),
91  DateADTGetDatum(*((const DateADT *) b)));
92 
93  return (float8) Abs(DatumGetInt32(diff));
94 }
95 
96 
97 static const gbtree_ninfo tinfo =
98 {
99  gbt_t_date,
100  sizeof(DateADT),
101  8, /* sizeof(gbtreekey8) */
102  gbt_dategt,
103  gbt_datege,
104  gbt_dateeq,
105  gbt_datele,
106  gbt_datelt,
109 };
110 
111 
113 Datum
115 {
116  /* we assume the difference can't overflow */
118  PG_GETARG_DATUM(0),
119  PG_GETARG_DATUM(1));
120 
122 }
123 
124 
125 /**************************************************
126  * date ops
127  **************************************************/
128 
129 
130 
131 Datum
133 {
134  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
135 
136  PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
137 }
138 
139 Datum
141 {
142  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
143 
144  PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
145 }
146 
147 Datum
149 {
150  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
151  DateADT query = PG_GETARG_DATEADT(1);
153 
154  /* Oid subtype = PG_GETARG_OID(3); */
155  bool *recheck = (bool *) PG_GETARG_POINTER(4);
156  dateKEY *kkk = (dateKEY *) DatumGetPointer(entry->key);
157  GBT_NUMKEY_R key;
158 
159  /* All cases served by this function are exact */
160  *recheck = false;
161 
162  key.lower = (GBT_NUMKEY *) &kkk->lower;
163  key.upper = (GBT_NUMKEY *) &kkk->upper;
164 
166  gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
167  );
168 }
169 
170 
171 Datum
173 {
174  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
175  DateADT query = PG_GETARG_DATEADT(1);
176 
177  /* Oid subtype = PG_GETARG_OID(3); */
178  dateKEY *kkk = (dateKEY *) DatumGetPointer(entry->key);
179  GBT_NUMKEY_R key;
180 
181  key.lower = (GBT_NUMKEY *) &kkk->lower;
182  key.upper = (GBT_NUMKEY *) &kkk->upper;
183 
185  gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
186  );
187 }
188 
189 
190 Datum
192 {
194  void *out = palloc(sizeof(dateKEY));
195 
196  *(int *) PG_GETARG_POINTER(1) = sizeof(dateKEY);
197  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
198 }
199 
200 
201 Datum
203 {
204  dateKEY *origentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
205  dateKEY *newentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
206  float *result = (float *) PG_GETARG_POINTER(2);
207  int32 diff,
208  res;
209 
211  date_mi,
212  DateADTGetDatum(newentry->upper),
213  DateADTGetDatum(origentry->upper)));
214 
215  res = Max(diff, 0);
216 
218  date_mi,
219  DateADTGetDatum(origentry->lower),
220  DateADTGetDatum(newentry->lower)));
221 
222  res += Max(diff, 0);
223 
224  *result = 0.0;
225 
226  if (res > 0)
227  {
229  date_mi,
230  DateADTGetDatum(origentry->upper),
231  DateADTGetDatum(origentry->lower)));
232  *result += FLT_MIN;
233  *result += (float) (res / ((double) (res + diff)));
234  *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
235  }
236 
237  PG_RETURN_POINTER(result);
238 }
239 
240 
241 Datum
243 {
247  &tinfo, fcinfo->flinfo
248  ));
249 }
250 
251 Datum
253 {
254  dateKEY *b1 = (dateKEY *) PG_GETARG_POINTER(0);
255  dateKEY *b2 = (dateKEY *) PG_GETARG_POINTER(1);
256  bool *result = (bool *) PG_GETARG_POINTER(2);
257 
258  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
259  PG_RETURN_POINTER(result);
260 }
#define GIST_LEAF(entry)
Definition: gist.h:133
Datum gbt_date_picksplit(PG_FUNCTION_ARGS)
Definition: btree_date.c:242
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Datum date_eq(PG_FUNCTION_ARGS)
Definition: date.c:389
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
static bool gbt_datege(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:38
Definition: fmgr.h:56
Datum gbt_date_fetch(PG_FUNCTION_ARGS)
Definition: btree_date.c:140
static int gbt_datekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:72
DateADT lower
Definition: btree_date.c:13
Datum date_le(PG_FUNCTION_ARGS)
Definition: date.c:416
Datum date_lt(PG_FUNCTION_ARGS)
Definition: date.c:407
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 DatumGetInt32(X)
Definition: postgres.h:478
int32 DateADT
Definition: date.h:22
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
Datum gbt_date_consistent(PG_FUNCTION_ARGS)
Definition: btree_date.c:148
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
Datum gbt_date_compress(PG_FUNCTION_ARGS)
Definition: btree_date.c:132
uint16 StrategyNumber
Definition: stratnum.h:22
#define DateADTGetDatum(X)
Definition: date.h:56
static bool gbt_datele(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:54
const GBT_NUMKEY * upper
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static float8 gdb_date_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:86
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 gbt_date_union(PG_FUNCTION_ARGS)
Definition: btree_date.c:191
Datum date_dist(PG_FUNCTION_ARGS)
Definition: btree_date.c:114
signed int int32
Definition: c.h:294
#define Abs(x)
Definition: c.h:818
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
double float8
Definition: c.h:439
Datum gbt_date_distance(PG_FUNCTION_ARGS)
Definition: btree_date.c:172
static bool gbt_dategt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:30
Datum key
Definition: gist.h:123
char GBT_NUMKEY
#define DatumGetBool(X)
Definition: postgres.h:399
Datum date_gt(PG_FUNCTION_ARGS)
Definition: date.c:425
#define PG_GETARG_DATEADT(n)
Definition: date.h:60
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
static bool gbt_dateeq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:46
#define Max(x, y)
Definition: c.h:806
Datum date_cmp(PG_FUNCTION_ARGS)
Definition: date.c:443
DateADT upper
Definition: btree_date.c:14
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
#define DatumGetPointer(X)
Definition: postgres.h:555
static const gbtree_ninfo tinfo
Definition: btree_date.c:97
Datum gbt_date_penalty(PG_FUNCTION_ARGS)
Definition: btree_date.c:202
void * palloc(Size size)
Definition: mcxt.c:835
PG_FUNCTION_INFO_V1(gbt_date_compress)
static bool gbt_datelt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:62
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
Datum date_mi(PG_FUNCTION_ARGS)
Definition: date.c:506
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
Datum gbt_date_same(PG_FUNCTION_ARGS)
Definition: btree_date.c:252
Datum date_ge(PG_FUNCTION_ARGS)
Definition: date.c:434
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)