Age Owner Branch data TLA 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 */
7771 teodor@sigaev.ru 18 :CBC 9 : PG_FUNCTION_INFO_V1(gbt_int4_compress);
3816 heikki.linnakangas@i 19 : 9 : PG_FUNCTION_INFO_V1(gbt_int4_fetch);
7771 teodor@sigaev.ru 20 : 9 : PG_FUNCTION_INFO_V1(gbt_int4_union);
21 : 9 : PG_FUNCTION_INFO_V1(gbt_int4_picksplit);
22 : 9 : PG_FUNCTION_INFO_V1(gbt_int4_consistent);
5302 tgl@sss.pgh.pa.us 23 : 9 : PG_FUNCTION_INFO_V1(gbt_int4_distance);
7771 teodor@sigaev.ru 24 : 9 : PG_FUNCTION_INFO_V1(gbt_int4_penalty);
25 : 9 : PG_FUNCTION_INFO_V1(gbt_int4_same);
156 heikki.linnakangas@i 26 : 9 : PG_FUNCTION_INFO_V1(gbt_int4_sortsupport);
27 : :
28 : : static bool
3091 andrew@dunslane.net 29 : 2133 : gbt_int4gt(const void *a, const void *b, FmgrInfo *flinfo)
30 : : {
5109 peter_e@gmx.net 31 : 2133 : return (*((const int32 *) a) > *((const int32 *) b));
32 : : }
33 : : static bool
3091 andrew@dunslane.net 34 : 576 : gbt_int4ge(const void *a, const void *b, FmgrInfo *flinfo)
35 : : {
5109 peter_e@gmx.net 36 : 576 : return (*((const int32 *) a) >= *((const int32 *) b));
37 : : }
38 : : static bool
3091 andrew@dunslane.net 39 : 1397 : gbt_int4eq(const void *a, const void *b, FmgrInfo *flinfo)
40 : : {
5109 peter_e@gmx.net 41 : 1397 : return (*((const int32 *) a) == *((const int32 *) b));
42 : : }
43 : : static bool
3091 andrew@dunslane.net 44 : 568 : gbt_int4le(const void *a, const void *b, FmgrInfo *flinfo)
45 : : {
5109 peter_e@gmx.net 46 : 568 : return (*((const int32 *) a) <= *((const int32 *) b));
47 : : }
48 : : static bool
3091 andrew@dunslane.net 49 : 1860 : gbt_int4lt(const void *a, const void *b, FmgrInfo *flinfo)
50 : : {
5109 peter_e@gmx.net 51 : 1860 : return (*((const int32 *) a) < *((const int32 *) b));
52 : : }
53 : :
54 : : static int
3091 andrew@dunslane.net 55 : 1044 : gbt_int4key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
56 : : {
5109 peter_e@gmx.net 57 : 1044 : int32KEY *ia = (int32KEY *) (((const Nsrt *) a)->t);
58 : 1044 : int32KEY *ib = (int32KEY *) (((const Nsrt *) b)->t);
59 : :
5757 teodor@sigaev.ru 60 [ + + ]: 1044 : if (ia->lower == ib->lower)
61 : : {
62 [ + - ]: 14 : if (ia->upper == ib->upper)
63 : 14 : return 0;
64 : :
5757 teodor@sigaev.ru 65 [ # # ]:UBC 0 : return (ia->upper > ib->upper) ? 1 : -1;
66 : : }
67 : :
5757 teodor@sigaev.ru 68 [ - + ]:CBC 1030 : return (ia->lower > ib->lower) ? 1 : -1;
69 : : }
70 : :
71 : : static float8
3091 andrew@dunslane.net 72 : 274 : gbt_int4_dist(const void *a, const void *b, FmgrInfo *flinfo)
73 : : {
4821 peter_e@gmx.net 74 : 274 : 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 : :
5302 tgl@sss.pgh.pa.us 93 : 4 : PG_FUNCTION_INFO_V1(int4_dist);
94 : : Datum
95 : 549 : int4_dist(PG_FUNCTION_ARGS)
96 : : {
4821 peter_e@gmx.net 97 : 549 : int32 a = PG_GETARG_INT32(0);
98 : 549 : int32 b = PG_GETARG_INT32(1);
99 : : int32 r;
100 : : int32 ra;
101 : :
2825 andres@anarazel.de 102 [ + - ]: 549 : if (pg_sub_s32_overflow(a, b, &r) ||
103 [ - + ]: 549 : r == PG_INT32_MIN)
5302 tgl@sss.pgh.pa.us 104 [ # # ]:UBC 0 : ereport(ERROR,
105 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
106 : : errmsg("integer out of range")));
107 : :
1065 peter@eisentraut.org 108 :CBC 549 : ra = abs(r);
109 : :
5302 tgl@sss.pgh.pa.us 110 : 549 : PG_RETURN_INT32(ra);
111 : : }
112 : :
113 : :
114 : : /**************************************************
115 : : * GiST support functions
116 : : **************************************************/
117 : :
118 : : Datum
7771 teodor@sigaev.ru 119 : 1082 : gbt_int4_compress(PG_FUNCTION_ARGS)
120 : : {
7678 bruce@momjian.us 121 : 1082 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
122 : :
3817 heikki.linnakangas@i 123 : 1082 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
124 : : }
125 : :
126 : : Datum
3816 127 : 273 : gbt_int4_fetch(PG_FUNCTION_ARGS)
128 : : {
129 : 273 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
130 : :
131 : 273 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
132 : : }
133 : :
134 : : Datum
7771 teodor@sigaev.ru 135 : 3053 : gbt_int4_consistent(PG_FUNCTION_ARGS)
136 : : {
7678 bruce@momjian.us 137 : 3053 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
138 : 3053 : int32 query = PG_GETARG_INT32(1);
6354 tgl@sss.pgh.pa.us 139 : 3053 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
140 : :
141 : : /* Oid subtype = PG_GETARG_OID(3); */
142 : 3053 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
7678 bruce@momjian.us 143 : 3053 : int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key);
144 : : GBT_NUMKEY_R key;
145 : :
146 : : /* All cases served by this function are exact */
6354 tgl@sss.pgh.pa.us 147 : 3053 : *recheck = false;
148 : :
5931 bruce@momjian.us 149 : 3053 : key.lower = (GBT_NUMKEY *) &kkk->lower;
150 : 3053 : key.upper = (GBT_NUMKEY *) &kkk->upper;
151 : :
282 peter@eisentraut.org 152 : 3053 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
153 : : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
154 : : }
155 : :
156 : : Datum
5302 tgl@sss.pgh.pa.us 157 : 275 : gbt_int4_distance(PG_FUNCTION_ARGS)
158 : : {
159 : 275 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
160 : 275 : int32 query = PG_GETARG_INT32(1);
161 : :
162 : : /* Oid subtype = PG_GETARG_OID(3); */
163 : 275 : int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key);
164 : : GBT_NUMKEY_R key;
165 : :
166 : 275 : key.lower = (GBT_NUMKEY *) &kkk->lower;
167 : 275 : key.upper = (GBT_NUMKEY *) &kkk->upper;
168 : :
282 peter@eisentraut.org 169 : 275 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
170 : : &tinfo, fcinfo->flinfo));
171 : : }
172 : :
173 : : Datum
7771 teodor@sigaev.ru 174 : 1 : gbt_int4_union(PG_FUNCTION_ARGS)
175 : : {
7678 bruce@momjian.us 176 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
177 : 1 : void *out = palloc(sizeof(int32KEY));
178 : :
179 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(int32KEY);
282 peter@eisentraut.org 180 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
181 : : }
182 : :
183 : : Datum
7771 teodor@sigaev.ru 184 :UBC 0 : gbt_int4_penalty(PG_FUNCTION_ARGS)
185 : : {
7678 bruce@momjian.us 186 : 0 : int32KEY *origentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
7266 187 : 0 : int32KEY *newentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
188 : 0 : float *result = (float *) PG_GETARG_POINTER(2);
189 : :
190 [ # # # # : 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
# # ]
191 : :
7678 192 : 0 : PG_RETURN_POINTER(result);
193 : : }
194 : :
195 : : Datum
7771 teodor@sigaev.ru 196 :CBC 2 : gbt_int4_picksplit(PG_FUNCTION_ARGS)
197 : : {
2046 alvherre@alvh.no-ip. 198 : 2 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
199 : : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
200 : : &tinfo, fcinfo->flinfo));
201 : : }
202 : :
203 : : Datum
7771 teodor@sigaev.ru 204 :UBC 0 : gbt_int4_same(PG_FUNCTION_ARGS)
205 : : {
7678 bruce@momjian.us 206 : 0 : int32KEY *b1 = (int32KEY *) PG_GETARG_POINTER(0);
207 : 0 : int32KEY *b2 = (int32KEY *) PG_GETARG_POINTER(1);
208 : 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
209 : :
3091 andrew@dunslane.net 210 : 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
7678 bruce@momjian.us 211 : 0 : PG_RETURN_POINTER(result);
212 : : }
213 : :
214 : : static int
156 heikki.linnakangas@i 215 :CBC 5582 : gbt_int4_ssup_cmp(Datum a, Datum b, SortSupport ssup)
216 : : {
217 : 5582 : int32KEY *ia = (int32KEY *) DatumGetPointer(a);
218 : 5582 : int32KEY *ib = (int32KEY *) DatumGetPointer(b);
219 : :
220 : : /* for leaf items we expect lower == upper, so only compare lower */
221 [ + + ]: 5582 : if (ia->lower < ib->lower)
222 : 2864 : return -1;
223 [ + + ]: 2718 : else if (ia->lower > ib->lower)
224 : 2703 : return 1;
225 : : else
226 : 15 : return 0;
227 : : }
228 : :
229 : : Datum
230 : 8 : gbt_int4_sortsupport(PG_FUNCTION_ARGS)
231 : : {
232 : 8 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
233 : :
234 : 8 : ssup->comparator = gbt_int4_ssup_cmp;
235 : 8 : PG_RETURN_VOID();
236 : : }
|