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 {
33  DateADTGetDatum(*((const DateADT *) a)),
34  DateADTGetDatum(*((const DateADT *) b))));
35 }
36 
37 static bool
38 gbt_datege(const void *a, const void *b, FmgrInfo *flinfo)
39 {
41  DateADTGetDatum(*((const DateADT *) a)),
42  DateADTGetDatum(*((const DateADT *) b))));
43 }
44 
45 static bool
46 gbt_dateeq(const void *a, const void *b, FmgrInfo *flinfo)
47 {
49  DateADTGetDatum(*((const DateADT *) a)),
50  DateADTGetDatum(*((const DateADT *) b)))
51  );
52 }
53 
54 static bool
55 gbt_datele(const void *a, const void *b, FmgrInfo *flinfo)
56 {
58  DateADTGetDatum(*((const DateADT *) a)),
59  DateADTGetDatum(*((const DateADT *) b))));
60 }
61 
62 static bool
63 gbt_datelt(const void *a, const void *b, FmgrInfo *flinfo)
64 {
66  DateADTGetDatum(*((const DateADT *) a)),
67  DateADTGetDatum(*((const DateADT *) b))));
68 }
69 
70 
71 
72 static int
73 gbt_datekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
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 }
89 
90 static float8
91 gdb_date_dist(const void *a, const void *b, FmgrInfo *flinfo)
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 }
100 
101 
102 static const gbtree_ninfo tinfo =
103 {
104  gbt_t_date,
105  sizeof(DateADT),
106  8, /* sizeof(gbtreekey8) */
107  gbt_dategt,
108  gbt_datege,
109  gbt_dateeq,
110  gbt_datele,
111  gbt_datelt,
114 };
115 
116 
118 Datum
120 {
121  /* we assume the difference can't overflow */
123  PG_GETARG_DATUM(0),
124  PG_GETARG_DATUM(1));
125 
126  PG_RETURN_INT32(abs(DatumGetInt32(diff)));
127 }
128 
129 
130 /**************************************************
131  * date ops
132  **************************************************/
133 
134 
135 
136 Datum
138 {
139  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140 
142 }
143 
144 Datum
146 {
147  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
148 
150 }
151 
152 Datum
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 }
174 
175 
176 Datum
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 }
192 
193 
194 Datum
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 }
203 
204 
205 Datum
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 }
240 
241 
242 Datum
244 {
247  &tinfo, fcinfo->flinfo));
248 }
249 
250 Datum
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 }
static bool gbt_datelt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:63
Datum gbt_date_fetch(PG_FUNCTION_ARGS)
Definition: btree_date.c:145
Datum gbt_date_consistent(PG_FUNCTION_ARGS)
Definition: btree_date.c:153
Datum gbt_date_penalty(PG_FUNCTION_ARGS)
Definition: btree_date.c:206
Datum gbt_date_union(PG_FUNCTION_ARGS)
Definition: btree_date.c:195
static bool gbt_datege(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:38
PG_FUNCTION_INFO_V1(gbt_date_compress)
static int gbt_datekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:73
static float8 gdb_date_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:91
Datum gbt_date_distance(PG_FUNCTION_ARGS)
Definition: btree_date.c:177
static const gbtree_ninfo tinfo
Definition: btree_date.c:102
static bool gbt_dategt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:30
Datum gbt_date_same(PG_FUNCTION_ARGS)
Definition: btree_date.c:251
Datum gbt_date_compress(PG_FUNCTION_ARGS)
Definition: btree_date.c:137
static bool gbt_datele(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:55
Datum date_dist(PG_FUNCTION_ARGS)
Definition: btree_date.c:119
static bool gbt_dateeq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_date.c:46
Datum gbt_date_picksplit(PG_FUNCTION_ARGS)
Definition: btree_date.c:243
@ gbt_t_date
Definition: btree_gist.h:27
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, 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)
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
char GBT_NUMKEY
signed int int32
Definition: c.h:494
#define Max(x, y)
Definition: c.h:998
double float8
Definition: c.h:630
Datum date_cmp(PG_FUNCTION_ARGS)
Definition: date.c:437
Datum date_le(PG_FUNCTION_ARGS)
Definition: date.c:410
Datum date_eq(PG_FUNCTION_ARGS)
Definition: date.c:383
Datum date_gt(PG_FUNCTION_ARGS)
Definition: date.c:419
Datum date_lt(PG_FUNCTION_ARGS)
Definition: date.c:401
Datum date_ge(PG_FUNCTION_ARGS)
Definition: date.c:428
Datum date_mi(PG_FUNCTION_ARGS)
Definition: date.c:487
static Datum DateADTGetDatum(DateADT X)
Definition: date.h:72
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:89
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIST_LEAF(entry)
Definition: gist.h:170
int b
Definition: isn.c:70
int a
Definition: isn.c:69
void * palloc(Size size)
Definition: mcxt.c:1316
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
Datum key
Definition: gist.h:160
DateADT upper
Definition: btree_date.c:14
DateADT lower
Definition: btree_date.c:13