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 */
7961 teodor@sigaev.ru 20 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_cash_compress);
4006 heikki.linnakangas@i 21 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_fetch);
7961 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);
5492 tgl@sss.pgh.pa.us 25 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_distance);
7961 teodor@sigaev.ru 26 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_penalty);
27 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_same);
346 heikki.linnakangas@i 28 : 4 : PG_FUNCTION_INFO_V1(gbt_cash_sortsupport);
29 : :
30 : : static bool
3281 andrew@dunslane.net 31 : 1626 : gbt_cashgt(const void *a, const void *b, FmgrInfo *flinfo)
32 : : {
5299 peter_e@gmx.net 33 : 1626 : return (*((const Cash *) a) > *((const Cash *) b));
34 : : }
35 : : static bool
3281 andrew@dunslane.net 36 : 565 : gbt_cashge(const void *a, const void *b, FmgrInfo *flinfo)
37 : : {
5299 peter_e@gmx.net 38 : 565 : return (*((const Cash *) a) >= *((const Cash *) b));
39 : : }
40 : : static bool
3281 andrew@dunslane.net 41 : 272 : gbt_casheq(const void *a, const void *b, FmgrInfo *flinfo)
42 : : {
5299 peter_e@gmx.net 43 : 272 : return (*((const Cash *) a) == *((const Cash *) b));
44 : : }
45 : : static bool
3281 andrew@dunslane.net 46 : 554 : gbt_cashle(const void *a, const void *b, FmgrInfo *flinfo)
47 : : {
5299 peter_e@gmx.net 48 : 554 : return (*((const Cash *) a) <= *((const Cash *) b));
49 : : }
50 : : static bool
3281 andrew@dunslane.net 51 : 1355 : gbt_cashlt(const void *a, const void *b, FmgrInfo *flinfo)
52 : : {
5299 peter_e@gmx.net 53 : 1355 : return (*((const Cash *) a) < *((const Cash *) b));
54 : : }
55 : :
56 : : static int
3281 andrew@dunslane.net 57 : 542 : gbt_cashkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
58 : : {
5299 peter_e@gmx.net 59 : 542 : cashKEY *ia = (cashKEY *) (((const Nsrt *) a)->t);
60 : 542 : cashKEY *ib = (cashKEY *) (((const Nsrt *) b)->t);
61 : :
5947 teodor@sigaev.ru 62 [ - + ]: 542 : if (ia->lower == ib->lower)
63 : : {
5947 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 : :
5947 teodor@sigaev.ru 70 [ - + ]:CBC 542 : return (ia->lower > ib->lower) ? 1 : -1;
71 : : }
72 : :
73 : : static float8
3281 andrew@dunslane.net 74 : 273 : gbt_cash_dist(const void *a, const void *b, FmgrInfo *flinfo)
75 : : {
5492 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 : :
3015 andres@anarazel.de 104 [ + - ]: 546 : if (pg_sub_s64_overflow(a, b, &r) ||
105 [ - + ]: 546 : r == PG_INT64_MIN)
5492 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 : :
1252 peter@eisentraut.org 110 :CBC 546 : ra = i64abs(r);
111 : :
5492 tgl@sss.pgh.pa.us 112 : 546 : PG_RETURN_CASH(ra);
113 : : }
114 : :
115 : :
116 : : /**************************************************
117 : : * GiST support functions
118 : : **************************************************/
119 : :
120 : : Datum
7961 teodor@sigaev.ru 121 : 545 : gbt_cash_compress(PG_FUNCTION_ARGS)
122 : : {
7868 bruce@momjian.us 123 : 545 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 : :
4007 heikki.linnakangas@i 125 : 545 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
126 : : }
127 : :
128 : : Datum
4006 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
7961 teodor@sigaev.ru 137 : 1912 : gbt_cash_consistent(PG_FUNCTION_ARGS)
138 : : {
7868 bruce@momjian.us 139 : 1912 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
6537 tgl@sss.pgh.pa.us 140 : 1912 : Cash query = PG_GETARG_CASH(1);
6544 141 : 1912 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142 : : #ifdef NOT_USED
143 : : Oid subtype = PG_GETARG_OID(3);
144 : : #endif
145 : 1912 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
7868 bruce@momjian.us 146 : 1912 : cashKEY *kkk = (cashKEY *) DatumGetPointer(entry->key);
147 : : GBT_NUMKEY_R key;
148 : :
149 : : /* All cases served by this function are exact */
6544 tgl@sss.pgh.pa.us 150 : 1912 : *recheck = false;
151 : :
6121 bruce@momjian.us 152 : 1912 : key.lower = (GBT_NUMKEY *) &kkk->lower;
153 : 1912 : key.upper = (GBT_NUMKEY *) &kkk->upper;
154 : :
472 peter@eisentraut.org 155 : 1912 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
156 : : GIST_LEAF(entry), &tinfo,
157 : : fcinfo->flinfo));
158 : : }
159 : :
160 : : Datum
5492 tgl@sss.pgh.pa.us 161 : 274 : gbt_cash_distance(PG_FUNCTION_ARGS)
162 : : {
163 : 274 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
164 : 274 : Cash query = PG_GETARG_CASH(1);
165 : : #ifdef NOT_USED
166 : : Oid subtype = PG_GETARG_OID(3);
167 : : #endif
168 : 274 : cashKEY *kkk = (cashKEY *) DatumGetPointer(entry->key);
169 : : GBT_NUMKEY_R key;
170 : :
171 : 274 : key.lower = (GBT_NUMKEY *) &kkk->lower;
172 : 274 : key.upper = (GBT_NUMKEY *) &kkk->upper;
173 : :
472 peter@eisentraut.org 174 : 274 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
175 : : &tinfo, fcinfo->flinfo));
176 : : }
177 : :
178 : : Datum
7961 teodor@sigaev.ru 179 : 1 : gbt_cash_union(PG_FUNCTION_ARGS)
180 : : {
7868 bruce@momjian.us 181 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
182 : 1 : void *out = palloc(sizeof(cashKEY));
183 : :
184 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
472 peter@eisentraut.org 185 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
186 : : }
187 : :
188 : : Datum
7961 teodor@sigaev.ru 189 :UBC 0 : gbt_cash_penalty(PG_FUNCTION_ARGS)
190 : : {
7868 bruce@momjian.us 191 : 0 : cashKEY *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
7456 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 : :
7868 197 : 0 : PG_RETURN_POINTER(result);
198 : : }
199 : :
200 : : Datum
7961 teodor@sigaev.ru 201 :CBC 1 : gbt_cash_picksplit(PG_FUNCTION_ARGS)
202 : : {
2236 alvherre@alvh.no-ip. 203 : 1 : 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
7961 teodor@sigaev.ru 209 :UBC 0 : gbt_cash_same(PG_FUNCTION_ARGS)
210 : : {
7868 bruce@momjian.us 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 : :
3281 andrew@dunslane.net 215 : 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
7868 bruce@momjian.us 216 : 0 : PG_RETURN_POINTER(result);
217 : : }
218 : :
219 : : static int
346 heikki.linnakangas@i 220 :CBC 5274 : gbt_cash_ssup_cmp(Datum x, Datum y, SortSupport ssup)
221 : : {
222 : 5274 : cashKEY *arg1 = (cashKEY *) DatumGetPointer(x);
223 : 5274 : cashKEY *arg2 = (cashKEY *) DatumGetPointer(y);
224 : :
225 : : /* for leaf items we expect lower == upper, so only compare lower */
226 [ + + ]: 5274 : if (arg1->lower > arg2->lower)
227 : 2926 : return 1;
228 [ + - ]: 2348 : else if (arg1->lower < arg2->lower)
229 : 2348 : return -1;
230 : : else
346 heikki.linnakangas@i 231 :UBC 0 : return 0;
232 : : }
233 : :
234 : : Datum
346 heikki.linnakangas@i 235 :CBC 1 : gbt_cash_sortsupport(PG_FUNCTION_ARGS)
236 : : {
237 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
238 : :
239 : 1 : ssup->comparator = gbt_cash_ssup_cmp;
240 : 1 : ssup->ssup_extra = NULL;
241 : :
242 : 1 : PG_RETURN_VOID();
243 : : }
|