2 * contrib/btree_gist/btree_date.c
6 #include "btree_gist.h"
7 #include "btree_utils_num.h"
8 #include "utils/builtins.h"
9 #include "utils/date.h"
20 PG_FUNCTION_INFO_V1(gbt_date_compress);
21 PG_FUNCTION_INFO_V1(gbt_date_fetch);
22 PG_FUNCTION_INFO_V1(gbt_date_union);
23 PG_FUNCTION_INFO_V1(gbt_date_picksplit);
24 PG_FUNCTION_INFO_V1(gbt_date_consistent);
25 PG_FUNCTION_INFO_V1(gbt_date_distance);
26 PG_FUNCTION_INFO_V1(gbt_date_penalty);
27 PG_FUNCTION_INFO_V1(gbt_date_same);
30 gbt_dategt(const void *a, const void *b, FmgrInfo *flinfo)
33 DirectFunctionCall2(date_gt, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
38 gbt_datege(const void *a, const void *b, FmgrInfo *flinfo)
41 DirectFunctionCall2(date_ge, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
46 gbt_dateeq(const void *a, const void *b, FmgrInfo *flinfo)
49 DirectFunctionCall2(date_eq, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
54 gbt_datele(const void *a, const void *b, FmgrInfo *flinfo)
57 DirectFunctionCall2(date_le, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
62 gbt_datelt(const void *a, const void *b, FmgrInfo *flinfo)
65 DirectFunctionCall2(date_lt, DateADTGetDatum(*((const DateADT *) a)), DateADTGetDatum(*((const DateADT *) b)))
72 gbt_datekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
74 dateKEY *ia = (dateKEY *) (((const Nsrt *) a)->t);
75 dateKEY *ib = (dateKEY *) (((const Nsrt *) b)->t);
78 res = DatumGetInt32(DirectFunctionCall2(date_cmp, DateADTGetDatum(ia->lower), DateADTGetDatum(ib->lower)));
80 return DatumGetInt32(DirectFunctionCall2(date_cmp, DateADTGetDatum(ia->upper), DateADTGetDatum(ib->upper)));
86 gdb_date_dist(const void *a, const void *b, FmgrInfo *flinfo)
88 /* we assume the difference can't overflow */
89 Datum diff = DirectFunctionCall2(date_mi,
90 DateADTGetDatum(*((const DateADT *) a)),
91 DateADTGetDatum(*((const DateADT *) b)));
93 return (float8) Abs(DatumGetInt32(diff));
97 static const gbtree_ninfo tinfo =
101 8, /* sizeof(gbtreekey8) */
112 PG_FUNCTION_INFO_V1(date_dist);
114 date_dist(PG_FUNCTION_ARGS)
116 /* we assume the difference can't overflow */
117 Datum diff = DirectFunctionCall2(date_mi,
121 PG_RETURN_INT32(Abs(DatumGetInt32(diff)));
125 /**************************************************
127 **************************************************/
132 gbt_date_compress(PG_FUNCTION_ARGS)
134 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
136 PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
140 gbt_date_fetch(PG_FUNCTION_ARGS)
142 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
144 PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
148 gbt_date_consistent(PG_FUNCTION_ARGS)
150 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
151 DateADT query = PG_GETARG_DATEADT(1);
152 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
154 /* Oid subtype = PG_GETARG_OID(3); */
155 bool *recheck = (bool *) PG_GETARG_POINTER(4);
156 dateKEY *kkk = (dateKEY *) DatumGetPointer(entry->key);
159 /* All cases served by this function are exact */
162 key.lower = (GBT_NUMKEY *) &kkk->lower;
163 key.upper = (GBT_NUMKEY *) &kkk->upper;
166 gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
172 gbt_date_distance(PG_FUNCTION_ARGS)
174 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
175 DateADT query = PG_GETARG_DATEADT(1);
177 /* Oid subtype = PG_GETARG_OID(3); */
178 dateKEY *kkk = (dateKEY *) DatumGetPointer(entry->key);
181 key.lower = (GBT_NUMKEY *) &kkk->lower;
182 key.upper = (GBT_NUMKEY *) &kkk->upper;
185 gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo, fcinfo->flinfo)
191 gbt_date_union(PG_FUNCTION_ARGS)
193 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
194 void *out = palloc(sizeof(dateKEY));
196 *(int *) PG_GETARG_POINTER(1) = sizeof(dateKEY);
197 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
202 gbt_date_penalty(PG_FUNCTION_ARGS)
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);
210 diff = DatumGetInt32(DirectFunctionCall2(
212 DateADTGetDatum(newentry->upper),
213 DateADTGetDatum(origentry->upper)));
217 diff = DatumGetInt32(DirectFunctionCall2(
219 DateADTGetDatum(origentry->lower),
220 DateADTGetDatum(newentry->lower)));
228 diff = DatumGetInt32(DirectFunctionCall2(
230 DateADTGetDatum(origentry->upper),
231 DateADTGetDatum(origentry->lower)));
233 *result += (float) (res / ((double) (res + diff)));
234 *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
237 PG_RETURN_POINTER(result);
242 gbt_date_picksplit(PG_FUNCTION_ARGS)
244 PG_RETURN_POINTER(gbt_num_picksplit(
245 (GistEntryVector *) PG_GETARG_POINTER(0),
246 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
247 &tinfo, fcinfo->flinfo
252 gbt_date_same(PG_FUNCTION_ARGS)
254 dateKEY *b1 = (dateKEY *) PG_GETARG_POINTER(0);
255 dateKEY *b2 = (dateKEY *) PG_GETARG_POINTER(1);
256 bool *result = (bool *) PG_GETARG_POINTER(2);
258 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
259 PG_RETURN_POINTER(result);