Only skip pages marked as clean in the visibility map, if the last 32
[PostgreSQL.git] / contrib / btree_gist / btree_date.c
blobbbfded17ed82156fb55d7941cc619fbf4f1aaa6f
1 /*
2 * $PostgreSQL:$
3 */
4 #include "btree_gist.h"
5 #include "btree_utils_num.h"
6 #include "utils/date.h"
8 typedef struct
10 DateADT lower;
11 DateADT upper;
12 } dateKEY;
15 ** date ops
17 PG_FUNCTION_INFO_V1(gbt_date_compress);
18 PG_FUNCTION_INFO_V1(gbt_date_union);
19 PG_FUNCTION_INFO_V1(gbt_date_picksplit);
20 PG_FUNCTION_INFO_V1(gbt_date_consistent);
21 PG_FUNCTION_INFO_V1(gbt_date_penalty);
22 PG_FUNCTION_INFO_V1(gbt_date_same);
24 Datum gbt_date_compress(PG_FUNCTION_ARGS);
25 Datum gbt_date_union(PG_FUNCTION_ARGS);
26 Datum gbt_date_picksplit(PG_FUNCTION_ARGS);
27 Datum gbt_date_consistent(PG_FUNCTION_ARGS);
28 Datum gbt_date_penalty(PG_FUNCTION_ARGS);
29 Datum gbt_date_same(PG_FUNCTION_ARGS);
31 static bool
32 gbt_dategt(const void *a, const void *b)
34 return DatumGetBool(
35 DirectFunctionCall2(date_gt, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
39 static bool
40 gbt_datege(const void *a, const void *b)
42 return DatumGetBool(
43 DirectFunctionCall2(date_ge, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
47 static bool
48 gbt_dateeq(const void *a, const void *b)
50 return DatumGetBool(
51 DirectFunctionCall2(date_eq, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
55 static bool
56 gbt_datele(const void *a, const void *b)
58 return DatumGetBool(
59 DirectFunctionCall2(date_le, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
63 static bool
64 gbt_datelt(const void *a, const void *b)
66 return DatumGetBool(
67 DirectFunctionCall2(date_lt, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
73 static int
74 gbt_datekey_cmp(const void *a, const void *b)
76 if (gbt_dategt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
77 return 1;
78 else if (gbt_datelt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
79 return -1;
80 return 0;
84 static const gbtree_ninfo tinfo =
86 gbt_t_date,
87 sizeof(DateADT),
88 gbt_dategt,
89 gbt_datege,
90 gbt_dateeq,
91 gbt_datele,
92 gbt_datelt,
93 gbt_datekey_cmp
97 /**************************************************
98 * date ops
99 **************************************************/
103 Datum
104 gbt_date_compress(PG_FUNCTION_ARGS)
106 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
107 GISTENTRY *retval = NULL;
109 PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
114 Datum
115 gbt_date_consistent(PG_FUNCTION_ARGS)
117 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
118 DateADT query = PG_GETARG_DATEADT(1);
119 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
120 /* Oid subtype = PG_GETARG_OID(3); */
121 bool *recheck = (bool *) PG_GETARG_POINTER(4);
122 dateKEY *kkk = (dateKEY *) DatumGetPointer(entry->key);
123 GBT_NUMKEY_R key;
125 /* All cases served by this function are exact */
126 *recheck = false;
128 key.lower = (GBT_NUMKEY *) & kkk->lower;
129 key.upper = (GBT_NUMKEY *) & kkk->upper;
131 PG_RETURN_BOOL(
132 gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
137 Datum
138 gbt_date_union(PG_FUNCTION_ARGS)
140 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
141 void *out = palloc(sizeof(dateKEY));
143 *(int *) PG_GETARG_POINTER(1) = sizeof(dateKEY);
144 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
148 Datum
149 gbt_date_penalty(PG_FUNCTION_ARGS)
151 dateKEY *origentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
152 dateKEY *newentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
153 float *result = (float *) PG_GETARG_POINTER(2);
154 int32 diff,
155 res;
157 diff = DatumGetInt32(DirectFunctionCall2(
158 date_mi,
159 DateADTGetDatum(newentry->upper),
160 DateADTGetDatum(origentry->upper)));
162 res = Max(diff, 0);
164 diff = DatumGetInt32(DirectFunctionCall2(
165 date_mi,
166 DateADTGetDatum(origentry->lower),
167 DateADTGetDatum(newentry->lower)));
169 res += Max(diff, 0);
171 *result = 0.0;
173 if (res > 0)
175 diff = DatumGetInt32(DirectFunctionCall2(
176 date_mi,
177 DateADTGetDatum(origentry->upper),
178 DateADTGetDatum(origentry->lower)));
179 *result += FLT_MIN;
180 *result += (float) (res / ((double) (res + diff)));
181 *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
184 PG_RETURN_POINTER(result);
188 Datum
189 gbt_date_picksplit(PG_FUNCTION_ARGS)
191 PG_RETURN_POINTER(gbt_num_picksplit(
192 (GistEntryVector *) PG_GETARG_POINTER(0),
193 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
194 &tinfo
198 Datum
199 gbt_date_same(PG_FUNCTION_ARGS)
201 dateKEY *b1 = (dateKEY *) PG_GETARG_POINTER(0);
202 dateKEY *b2 = (dateKEY *) PG_GETARG_POINTER(1);
203 bool *result = (bool *) PG_GETARG_POINTER(2);
205 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
206 PG_RETURN_POINTER(result);