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