Line data Source code
1 : /*
2 : * contrib/btree_gist/btree_cash.c
3 : */
4 : #include "postgres.h"
5 :
6 : #include "btree_gist.h"
7 : #include "btree_utils_num.h"
8 : #include "common/int.h"
9 : #include "utils/cash.h"
10 : #include "utils/rel.h"
11 : #include "utils/sortsupport.h"
12 :
13 : typedef struct
14 : {
15 : Cash lower;
16 : Cash upper;
17 : } cashKEY;
18 :
19 : /* GiST support functions */
20 0 : PG_FUNCTION_INFO_V1(gbt_cash_compress);
21 0 : PG_FUNCTION_INFO_V1(gbt_cash_fetch);
22 0 : PG_FUNCTION_INFO_V1(gbt_cash_union);
23 0 : PG_FUNCTION_INFO_V1(gbt_cash_picksplit);
24 0 : PG_FUNCTION_INFO_V1(gbt_cash_consistent);
25 0 : PG_FUNCTION_INFO_V1(gbt_cash_distance);
26 0 : PG_FUNCTION_INFO_V1(gbt_cash_penalty);
27 0 : PG_FUNCTION_INFO_V1(gbt_cash_same);
28 0 : PG_FUNCTION_INFO_V1(gbt_cash_sortsupport);
29 :
30 : static bool
31 0 : gbt_cashgt(const void *a, const void *b, FmgrInfo *flinfo)
32 : {
33 0 : return (*((const Cash *) a) > *((const Cash *) b));
34 : }
35 : static bool
36 0 : gbt_cashge(const void *a, const void *b, FmgrInfo *flinfo)
37 : {
38 0 : return (*((const Cash *) a) >= *((const Cash *) b));
39 : }
40 : static bool
41 0 : gbt_casheq(const void *a, const void *b, FmgrInfo *flinfo)
42 : {
43 0 : return (*((const Cash *) a) == *((const Cash *) b));
44 : }
45 : static bool
46 0 : gbt_cashle(const void *a, const void *b, FmgrInfo *flinfo)
47 : {
48 0 : return (*((const Cash *) a) <= *((const Cash *) b));
49 : }
50 : static bool
51 0 : gbt_cashlt(const void *a, const void *b, FmgrInfo *flinfo)
52 : {
53 0 : return (*((const Cash *) a) < *((const Cash *) b));
54 : }
55 :
56 : static int
57 0 : gbt_cashkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
58 : {
59 0 : cashKEY *ia = (cashKEY *) (((const Nsrt *) a)->t);
60 0 : cashKEY *ib = (cashKEY *) (((const Nsrt *) b)->t);
61 :
62 0 : if (ia->lower == ib->lower)
63 : {
64 0 : if (ia->upper == ib->upper)
65 0 : return 0;
66 :
67 0 : return (ia->upper > ib->upper) ? 1 : -1;
68 : }
69 :
70 0 : return (ia->lower > ib->lower) ? 1 : -1;
71 0 : }
72 :
73 : static float8
74 0 : gbt_cash_dist(const void *a, const void *b, FmgrInfo *flinfo)
75 : {
76 0 : return GET_FLOAT_DISTANCE(Cash, a, b);
77 : }
78 :
79 :
80 : static const gbtree_ninfo tinfo =
81 : {
82 : gbt_t_cash,
83 : sizeof(Cash),
84 : 16, /* sizeof(gbtreekey16) */
85 : gbt_cashgt,
86 : gbt_cashge,
87 : gbt_casheq,
88 : gbt_cashle,
89 : gbt_cashlt,
90 : gbt_cashkey_cmp,
91 : gbt_cash_dist
92 : };
93 :
94 :
95 0 : PG_FUNCTION_INFO_V1(cash_dist);
96 : Datum
97 0 : cash_dist(PG_FUNCTION_ARGS)
98 : {
99 0 : Cash a = PG_GETARG_CASH(0);
100 0 : Cash b = PG_GETARG_CASH(1);
101 0 : Cash r;
102 0 : Cash ra;
103 :
104 0 : if (pg_sub_s64_overflow(a, b, &r) ||
105 0 : r == PG_INT64_MIN)
106 0 : ereport(ERROR,
107 : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
108 : errmsg("money out of range")));
109 :
110 0 : ra = i64abs(r);
111 :
112 0 : PG_RETURN_CASH(ra);
113 0 : }
114 :
115 :
116 : /**************************************************
117 : * GiST support functions
118 : **************************************************/
119 :
120 : Datum
121 0 : gbt_cash_compress(PG_FUNCTION_ARGS)
122 : {
123 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 :
125 0 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
126 0 : }
127 :
128 : Datum
129 0 : gbt_cash_fetch(PG_FUNCTION_ARGS)
130 : {
131 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132 :
133 0 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
134 0 : }
135 :
136 : Datum
137 0 : gbt_cash_consistent(PG_FUNCTION_ARGS)
138 : {
139 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140 0 : Cash query = PG_GETARG_CASH(1);
141 0 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142 : #ifdef NOT_USED
143 : Oid subtype = PG_GETARG_OID(3);
144 : #endif
145 0 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
146 0 : cashKEY *kkk = (cashKEY *) DatumGetPointer(entry->key);
147 0 : GBT_NUMKEY_R key;
148 :
149 : /* All cases served by this function are exact */
150 0 : *recheck = false;
151 :
152 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
153 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
154 :
155 0 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
156 : GIST_LEAF(entry), &tinfo,
157 : fcinfo->flinfo));
158 0 : }
159 :
160 : Datum
161 0 : gbt_cash_distance(PG_FUNCTION_ARGS)
162 : {
163 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
164 0 : Cash query = PG_GETARG_CASH(1);
165 : #ifdef NOT_USED
166 : Oid subtype = PG_GETARG_OID(3);
167 : #endif
168 0 : cashKEY *kkk = (cashKEY *) DatumGetPointer(entry->key);
169 0 : GBT_NUMKEY_R key;
170 :
171 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
172 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
173 :
174 0 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
175 : &tinfo, fcinfo->flinfo));
176 0 : }
177 :
178 : Datum
179 0 : gbt_cash_union(PG_FUNCTION_ARGS)
180 : {
181 0 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
182 0 : void *out = palloc(sizeof(cashKEY));
183 :
184 0 : *(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
185 0 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
186 0 : }
187 :
188 : Datum
189 0 : gbt_cash_penalty(PG_FUNCTION_ARGS)
190 : {
191 0 : cashKEY *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
192 0 : cashKEY *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
193 0 : float *result = (float *) PG_GETARG_POINTER(2);
194 :
195 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
196 :
197 0 : PG_RETURN_POINTER(result);
198 0 : }
199 :
200 : Datum
201 0 : gbt_cash_picksplit(PG_FUNCTION_ARGS)
202 : {
203 0 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
204 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
205 : &tinfo, fcinfo->flinfo));
206 : }
207 :
208 : Datum
209 0 : gbt_cash_same(PG_FUNCTION_ARGS)
210 : {
211 0 : cashKEY *b1 = (cashKEY *) PG_GETARG_POINTER(0);
212 0 : cashKEY *b2 = (cashKEY *) PG_GETARG_POINTER(1);
213 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
214 :
215 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
216 0 : PG_RETURN_POINTER(result);
217 0 : }
218 :
219 : static int
220 0 : gbt_cash_ssup_cmp(Datum x, Datum y, SortSupport ssup)
221 : {
222 0 : cashKEY *arg1 = (cashKEY *) DatumGetPointer(x);
223 0 : cashKEY *arg2 = (cashKEY *) DatumGetPointer(y);
224 :
225 : /* for leaf items we expect lower == upper, so only compare lower */
226 0 : if (arg1->lower > arg2->lower)
227 0 : return 1;
228 0 : else if (arg1->lower < arg2->lower)
229 0 : return -1;
230 : else
231 0 : return 0;
232 0 : }
233 :
234 : Datum
235 0 : gbt_cash_sortsupport(PG_FUNCTION_ARGS)
236 : {
237 0 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
238 :
239 0 : ssup->comparator = gbt_cash_ssup_cmp;
240 0 : ssup->ssup_extra = NULL;
241 :
242 0 : PG_RETURN_VOID();
243 0 : }
|