Line data Source code
1 : /*
2 : * contrib/btree_gist/btree_int2.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/rel.h"
10 : #include "utils/sortsupport.h"
11 :
12 : typedef struct int16key
13 : {
14 : int16 lower;
15 : int16 upper;
16 : } int16KEY;
17 :
18 : /* GiST support functions */
19 0 : PG_FUNCTION_INFO_V1(gbt_int2_compress);
20 0 : PG_FUNCTION_INFO_V1(gbt_int2_fetch);
21 0 : PG_FUNCTION_INFO_V1(gbt_int2_union);
22 0 : PG_FUNCTION_INFO_V1(gbt_int2_picksplit);
23 0 : PG_FUNCTION_INFO_V1(gbt_int2_consistent);
24 0 : PG_FUNCTION_INFO_V1(gbt_int2_distance);
25 0 : PG_FUNCTION_INFO_V1(gbt_int2_penalty);
26 0 : PG_FUNCTION_INFO_V1(gbt_int2_same);
27 0 : PG_FUNCTION_INFO_V1(gbt_int2_sortsupport);
28 :
29 :
30 : static bool
31 0 : gbt_int2gt(const void *a, const void *b, FmgrInfo *flinfo)
32 : {
33 0 : return (*((const int16 *) a) > *((const int16 *) b));
34 : }
35 : static bool
36 0 : gbt_int2ge(const void *a, const void *b, FmgrInfo *flinfo)
37 : {
38 0 : return (*((const int16 *) a) >= *((const int16 *) b));
39 : }
40 : static bool
41 0 : gbt_int2eq(const void *a, const void *b, FmgrInfo *flinfo)
42 : {
43 0 : return (*((const int16 *) a) == *((const int16 *) b));
44 : }
45 : static bool
46 0 : gbt_int2le(const void *a, const void *b, FmgrInfo *flinfo)
47 : {
48 0 : return (*((const int16 *) a) <= *((const int16 *) b));
49 : }
50 : static bool
51 0 : gbt_int2lt(const void *a, const void *b, FmgrInfo *flinfo)
52 : {
53 0 : return (*((const int16 *) a) < *((const int16 *) b));
54 : }
55 :
56 : static int
57 0 : gbt_int2key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
58 : {
59 0 : int16KEY *ia = (int16KEY *) (((const Nsrt *) a)->t);
60 0 : int16KEY *ib = (int16KEY *) (((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_int2_dist(const void *a, const void *b, FmgrInfo *flinfo)
75 : {
76 0 : return GET_FLOAT_DISTANCE(int16, a, b);
77 : }
78 :
79 :
80 : static const gbtree_ninfo tinfo =
81 : {
82 : gbt_t_int2,
83 : sizeof(int16),
84 : 4, /* sizeof(gbtreekey4) */
85 : gbt_int2gt,
86 : gbt_int2ge,
87 : gbt_int2eq,
88 : gbt_int2le,
89 : gbt_int2lt,
90 : gbt_int2key_cmp,
91 : gbt_int2_dist
92 : };
93 :
94 :
95 0 : PG_FUNCTION_INFO_V1(int2_dist);
96 : Datum
97 0 : int2_dist(PG_FUNCTION_ARGS)
98 : {
99 0 : int16 a = PG_GETARG_INT16(0);
100 0 : int16 b = PG_GETARG_INT16(1);
101 0 : int16 r;
102 0 : int16 ra;
103 :
104 0 : if (pg_sub_s16_overflow(a, b, &r) ||
105 0 : r == PG_INT16_MIN)
106 0 : ereport(ERROR,
107 : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
108 : errmsg("smallint out of range")));
109 :
110 0 : ra = abs(r);
111 :
112 0 : PG_RETURN_INT16(ra);
113 0 : }
114 :
115 :
116 : /**************************************************
117 : * GiST support functions
118 : **************************************************/
119 :
120 : Datum
121 0 : gbt_int2_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_int2_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_int2_consistent(PG_FUNCTION_ARGS)
138 : {
139 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140 0 : int16 query = PG_GETARG_INT16(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 : int16KEY *kkk = (int16KEY *) 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, fcinfo->flinfo));
157 0 : }
158 :
159 : Datum
160 0 : gbt_int2_distance(PG_FUNCTION_ARGS)
161 : {
162 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
163 0 : int16 query = PG_GETARG_INT16(1);
164 : #ifdef NOT_USED
165 : Oid subtype = PG_GETARG_OID(3);
166 : #endif
167 0 : int16KEY *kkk = (int16KEY *) DatumGetPointer(entry->key);
168 0 : GBT_NUMKEY_R key;
169 :
170 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
171 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
172 :
173 0 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
174 : &tinfo, fcinfo->flinfo));
175 0 : }
176 :
177 : Datum
178 0 : gbt_int2_union(PG_FUNCTION_ARGS)
179 : {
180 0 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
181 0 : void *out = palloc(sizeof(int16KEY));
182 :
183 0 : *(int *) PG_GETARG_POINTER(1) = sizeof(int16KEY);
184 0 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
185 0 : }
186 :
187 : Datum
188 0 : gbt_int2_penalty(PG_FUNCTION_ARGS)
189 : {
190 0 : int16KEY *origentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
191 0 : int16KEY *newentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
192 0 : float *result = (float *) PG_GETARG_POINTER(2);
193 :
194 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
195 :
196 0 : PG_RETURN_POINTER(result);
197 0 : }
198 :
199 : Datum
200 0 : gbt_int2_picksplit(PG_FUNCTION_ARGS)
201 : {
202 0 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
203 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
204 : &tinfo, fcinfo->flinfo));
205 : }
206 :
207 : Datum
208 0 : gbt_int2_same(PG_FUNCTION_ARGS)
209 : {
210 0 : int16KEY *b1 = (int16KEY *) PG_GETARG_POINTER(0);
211 0 : int16KEY *b2 = (int16KEY *) PG_GETARG_POINTER(1);
212 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
213 :
214 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
215 0 : PG_RETURN_POINTER(result);
216 0 : }
217 :
218 : static int
219 0 : gbt_int2_ssup_cmp(Datum x, Datum y, SortSupport ssup)
220 : {
221 0 : int16KEY *arg1 = (int16KEY *) DatumGetPointer(x);
222 0 : int16KEY *arg2 = (int16KEY *) DatumGetPointer(y);
223 :
224 : /* for leaf items we expect lower == upper, so only compare lower */
225 0 : if (arg1->lower < arg2->lower)
226 0 : return -1;
227 0 : else if (arg1->lower > arg2->lower)
228 0 : return 1;
229 : else
230 0 : return 0;
231 0 : }
232 :
233 : Datum
234 0 : gbt_int2_sortsupport(PG_FUNCTION_ARGS)
235 : {
236 0 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
237 :
238 0 : ssup->comparator = gbt_int2_ssup_cmp;
239 0 : PG_RETURN_VOID();
240 0 : }
|