Age Owner Branch data TLA 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 */
7771 teodor@sigaev.ru 20 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_cash_compress);
3816 heikki.linnakangas@i 21 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_fetch);
7771 teodor@sigaev.ru 22 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_union);
23 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_picksplit);
24 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_consistent);
5302 tgl@sss.pgh.pa.us 25 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_distance);
7771 teodor@sigaev.ru 26 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_penalty);
27 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_same);
156 heikki.linnakangas@i 28 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_sortsupport);
29 : :
30 : : static bool
3091 andrew@dunslane.net 31 : 1626 : gbt_cashgt(const void *a, const void *b, FmgrInfo *flinfo)
32 : : {
5109 peter_e@gmx.net 33 : 1626 : return (*((const Cash *) a) > *((const Cash *) b));
34 : : }
35 : : static bool
3091 andrew@dunslane.net 36 : 565 : gbt_cashge(const void *a, const void *b, FmgrInfo *flinfo)
37 : : {
5109 peter_e@gmx.net 38 : 565 : return (*((const Cash *) a) >= *((const Cash *) b));
39 : : }
40 : : static bool
3091 andrew@dunslane.net 41 : 272 : gbt_casheq(const void *a, const void *b, FmgrInfo *flinfo)
42 : : {
5109 peter_e@gmx.net 43 : 272 : return (*((const Cash *) a) == *((const Cash *) b));
44 : : }
45 : : static bool
3091 andrew@dunslane.net 46 : 554 : gbt_cashle(const void *a, const void *b, FmgrInfo *flinfo)
47 : : {
5109 peter_e@gmx.net 48 : 554 : return (*((const Cash *) a) <= *((const Cash *) b));
49 : : }
50 : : static bool
3091 andrew@dunslane.net 51 : 1355 : gbt_cashlt(const void *a, const void *b, FmgrInfo *flinfo)
52 : : {
5109 peter_e@gmx.net 53 : 1355 : return (*((const Cash *) a) < *((const Cash *) b));
54 : : }
55 : :
56 : : static int
3091 andrew@dunslane.net 57 : 542 : gbt_cashkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
58 : : {
5109 peter_e@gmx.net 59 : 542 : cashKEY *ia = (cashKEY *) (((const Nsrt *) a)->t);
60 : 542 : cashKEY *ib = (cashKEY *) (((const Nsrt *) b)->t);
61 : :
5757 teodor@sigaev.ru 62 [ - + ]: 542 : if (ia->lower == ib->lower)
63 : : {
5757 teodor@sigaev.ru 64 [ # # ]:UBC 0 : if (ia->upper == ib->upper)
65 : 0 : return 0;
66 : :
67 [ # # ]: 0 : return (ia->upper > ib->upper) ? 1 : -1;
68 : : }
69 : :
5757 teodor@sigaev.ru 70 [ - + ]:CBC 542 : return (ia->lower > ib->lower) ? 1 : -1;
71 : : }
72 : :
73 : : static float8
3091 andrew@dunslane.net 74 : 273 : gbt_cash_dist(const void *a, const void *b, FmgrInfo *flinfo)
75 : : {
5302 tgl@sss.pgh.pa.us 76 : 273 : 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 : 4 : PG_FUNCTION_INFO_V1(cash_dist);
96 : : Datum
97 : 546 : cash_dist(PG_FUNCTION_ARGS)
98 : : {
99 : 546 : Cash a = PG_GETARG_CASH(0);
100 : 546 : Cash b = PG_GETARG_CASH(1);
101 : : Cash r;
102 : : Cash ra;
103 : :
2825 andres@anarazel.de 104 [ + - ]: 546 : if (pg_sub_s64_overflow(a, b, &r) ||
105 [ - + ]: 546 : r == PG_INT64_MIN)
5302 tgl@sss.pgh.pa.us 106 [ # # ]:UBC 0 : ereport(ERROR,
107 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
108 : : errmsg("money out of range")));
109 : :
1062 peter@eisentraut.org 110 :CBC 546 : ra = i64abs(r);
111 : :
5302 tgl@sss.pgh.pa.us 112 : 546 : PG_RETURN_CASH(ra);
113 : : }
114 : :
115 : :
116 : : /**************************************************
117 : : * GiST support functions
118 : : **************************************************/
119 : :
120 : : Datum
7771 teodor@sigaev.ru 121 : 545 : gbt_cash_compress(PG_FUNCTION_ARGS)
122 : : {
7678 bruce@momjian.us 123 : 545 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 : :
3817 heikki.linnakangas@i 125 : 545 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
126 : : }
127 : :
128 : : Datum
3816 129 : 272 : gbt_cash_fetch(PG_FUNCTION_ARGS)
130 : : {
131 : 272 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132 : :
133 : 272 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
134 : : }
135 : :
136 : : Datum
7771 teodor@sigaev.ru 137 : 1912 : gbt_cash_consistent(PG_FUNCTION_ARGS)
138 : : {
7678 bruce@momjian.us 139 : 1912 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
6347 tgl@sss.pgh.pa.us 140 : 1912 : Cash query = PG_GETARG_CASH(1);
6354 141 : 1912 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142 : :
143 : : /* Oid subtype = PG_GETARG_OID(3); */
144 : 1912 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
7678 bruce@momjian.us 145 : 1912 : cashKEY *kkk = (cashKEY *) DatumGetPointer(entry->key);
146 : : GBT_NUMKEY_R key;
147 : :
148 : : /* All cases served by this function are exact */
6354 tgl@sss.pgh.pa.us 149 : 1912 : *recheck = false;
150 : :
5931 bruce@momjian.us 151 : 1912 : key.lower = (GBT_NUMKEY *) &kkk->lower;
152 : 1912 : key.upper = (GBT_NUMKEY *) &kkk->upper;
153 : :
282 peter@eisentraut.org 154 : 1912 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
155 : : GIST_LEAF(entry), &tinfo,
156 : : fcinfo->flinfo));
157 : : }
158 : :
159 : : Datum
5302 tgl@sss.pgh.pa.us 160 : 274 : gbt_cash_distance(PG_FUNCTION_ARGS)
161 : : {
162 : 274 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
163 : 274 : Cash query = PG_GETARG_CASH(1);
164 : :
165 : : /* Oid subtype = PG_GETARG_OID(3); */
166 : 274 : cashKEY *kkk = (cashKEY *) DatumGetPointer(entry->key);
167 : : GBT_NUMKEY_R key;
168 : :
169 : 274 : key.lower = (GBT_NUMKEY *) &kkk->lower;
170 : 274 : key.upper = (GBT_NUMKEY *) &kkk->upper;
171 : :
282 peter@eisentraut.org 172 : 274 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
173 : : &tinfo, fcinfo->flinfo));
174 : : }
175 : :
176 : : Datum
7771 teodor@sigaev.ru 177 : 1 : gbt_cash_union(PG_FUNCTION_ARGS)
178 : : {
7678 bruce@momjian.us 179 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
180 : 1 : void *out = palloc(sizeof(cashKEY));
181 : :
182 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
282 peter@eisentraut.org 183 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
184 : : }
185 : :
186 : : Datum
7771 teodor@sigaev.ru 187 :UBC 0 : gbt_cash_penalty(PG_FUNCTION_ARGS)
188 : : {
7678 bruce@momjian.us 189 : 0 : cashKEY *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
7266 190 : 0 : cashKEY *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
191 : 0 : float *result = (float *) PG_GETARG_POINTER(2);
192 : :
193 [ # # # # : 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
# # ]
194 : :
7678 195 : 0 : PG_RETURN_POINTER(result);
196 : : }
197 : :
198 : : Datum
7771 teodor@sigaev.ru 199 :CBC 1 : gbt_cash_picksplit(PG_FUNCTION_ARGS)
200 : : {
2046 alvherre@alvh.no-ip. 201 : 1 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
202 : : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
203 : : &tinfo, fcinfo->flinfo));
204 : : }
205 : :
206 : : Datum
7771 teodor@sigaev.ru 207 :UBC 0 : gbt_cash_same(PG_FUNCTION_ARGS)
208 : : {
7678 bruce@momjian.us 209 : 0 : cashKEY *b1 = (cashKEY *) PG_GETARG_POINTER(0);
210 : 0 : cashKEY *b2 = (cashKEY *) PG_GETARG_POINTER(1);
211 : 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
212 : :
3091 andrew@dunslane.net 213 : 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
7678 bruce@momjian.us 214 : 0 : PG_RETURN_POINTER(result);
215 : : }
216 : :
217 : : static int
156 heikki.linnakangas@i 218 :CBC 5274 : gbt_cash_ssup_cmp(Datum x, Datum y, SortSupport ssup)
219 : : {
220 : 5274 : cashKEY *arg1 = (cashKEY *) DatumGetPointer(x);
221 : 5274 : cashKEY *arg2 = (cashKEY *) DatumGetPointer(y);
222 : :
223 : : /* for leaf items we expect lower == upper, so only compare lower */
224 [ + + ]: 5274 : if (arg1->lower > arg2->lower)
225 : 2926 : return 1;
226 [ + - ]: 2348 : else if (arg1->lower < arg2->lower)
227 : 2348 : return -1;
228 : : else
156 heikki.linnakangas@i 229 :UBC 0 : return 0;
230 : : }
231 : :
232 : : Datum
156 heikki.linnakangas@i 233 :CBC 1 : gbt_cash_sortsupport(PG_FUNCTION_ARGS)
234 : : {
235 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
236 : :
237 : 1 : ssup->comparator = gbt_cash_ssup_cmp;
238 : 1 : ssup->ssup_extra = NULL;
239 : :
240 : 1 : PG_RETURN_VOID();
241 : : }
|