Line data Source code
1 : /*
2 : * contrib/btree_gist/btree_int8.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 int64key
13 : {
14 : int64 lower;
15 : int64 upper;
16 : } int64KEY;
17 :
18 : /* GiST support functions */
19 8 : PG_FUNCTION_INFO_V1(gbt_int8_compress);
20 8 : PG_FUNCTION_INFO_V1(gbt_int8_fetch);
21 8 : PG_FUNCTION_INFO_V1(gbt_int8_union);
22 8 : PG_FUNCTION_INFO_V1(gbt_int8_picksplit);
23 8 : PG_FUNCTION_INFO_V1(gbt_int8_consistent);
24 8 : PG_FUNCTION_INFO_V1(gbt_int8_distance);
25 8 : PG_FUNCTION_INFO_V1(gbt_int8_penalty);
26 8 : PG_FUNCTION_INFO_V1(gbt_int8_same);
27 8 : PG_FUNCTION_INFO_V1(gbt_int8_sortsupport);
28 :
29 :
30 : static bool
31 3276 : gbt_int8gt(const void *a, const void *b, FmgrInfo *flinfo)
32 : {
33 3276 : return (*((const int64 *) a) > *((const int64 *) b));
34 : }
35 : static bool
36 1112 : gbt_int8ge(const void *a, const void *b, FmgrInfo *flinfo)
37 : {
38 1112 : return (*((const int64 *) a) >= *((const int64 *) b));
39 : }
40 : static bool
41 548 : gbt_int8eq(const void *a, const void *b, FmgrInfo *flinfo)
42 : {
43 548 : return (*((const int64 *) a) == *((const int64 *) b));
44 : }
45 : static bool
46 1116 : gbt_int8le(const void *a, const void *b, FmgrInfo *flinfo)
47 : {
48 1116 : return (*((const int64 *) a) <= *((const int64 *) b));
49 : }
50 : static bool
51 2730 : gbt_int8lt(const void *a, const void *b, FmgrInfo *flinfo)
52 : {
53 2730 : return (*((const int64 *) a) < *((const int64 *) b));
54 : }
55 :
56 : static int
57 1092 : gbt_int8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
58 : {
59 1092 : int64KEY *ia = (int64KEY *) (((const Nsrt *) a)->t);
60 1092 : int64KEY *ib = (int64KEY *) (((const Nsrt *) b)->t);
61 :
62 1092 : 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 1092 : return (ia->lower > ib->lower) ? 1 : -1;
71 : }
72 :
73 : static float8
74 550 : gbt_int8_dist(const void *a, const void *b, FmgrInfo *flinfo)
75 : {
76 550 : return GET_FLOAT_DISTANCE(int64, a, b);
77 : }
78 :
79 :
80 : static const gbtree_ninfo tinfo =
81 : {
82 : gbt_t_int8,
83 : sizeof(int64),
84 : 16, /* sizeof(gbtreekey16) */
85 : gbt_int8gt,
86 : gbt_int8ge,
87 : gbt_int8eq,
88 : gbt_int8le,
89 : gbt_int8lt,
90 : gbt_int8key_cmp,
91 : gbt_int8_dist
92 : };
93 :
94 :
95 8 : PG_FUNCTION_INFO_V1(int8_dist);
96 : Datum
97 1100 : int8_dist(PG_FUNCTION_ARGS)
98 : {
99 1100 : int64 a = PG_GETARG_INT64(0);
100 1100 : int64 b = PG_GETARG_INT64(1);
101 : int64 r;
102 : int64 ra;
103 :
104 1100 : if (pg_sub_s64_overflow(a, b, &r) ||
105 1100 : r == PG_INT64_MIN)
106 0 : ereport(ERROR,
107 : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
108 : errmsg("bigint out of range")));
109 :
110 1100 : ra = i64abs(r);
111 :
112 1100 : PG_RETURN_INT64(ra);
113 : }
114 :
115 :
116 : /**************************************************
117 : * GiST support functions
118 : **************************************************/
119 :
120 : Datum
121 1098 : gbt_int8_compress(PG_FUNCTION_ARGS)
122 : {
123 1098 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
124 :
125 1098 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
126 : }
127 :
128 : Datum
129 548 : gbt_int8_fetch(PG_FUNCTION_ARGS)
130 : {
131 548 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
132 :
133 548 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
134 : }
135 :
136 : Datum
137 3852 : gbt_int8_consistent(PG_FUNCTION_ARGS)
138 : {
139 3852 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
140 3852 : int64 query = PG_GETARG_INT64(1);
141 3852 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142 :
143 : /* Oid subtype = PG_GETARG_OID(3); */
144 3852 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
145 3852 : int64KEY *kkk = (int64KEY *) DatumGetPointer(entry->key);
146 : GBT_NUMKEY_R key;
147 :
148 : /* All cases served by this function are exact */
149 3852 : *recheck = false;
150 :
151 3852 : key.lower = (GBT_NUMKEY *) &kkk->lower;
152 3852 : key.upper = (GBT_NUMKEY *) &kkk->upper;
153 :
154 3852 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
155 : GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
156 : }
157 :
158 : Datum
159 552 : gbt_int8_distance(PG_FUNCTION_ARGS)
160 : {
161 552 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
162 552 : int64 query = PG_GETARG_INT64(1);
163 :
164 : /* Oid subtype = PG_GETARG_OID(3); */
165 552 : int64KEY *kkk = (int64KEY *) DatumGetPointer(entry->key);
166 : GBT_NUMKEY_R key;
167 :
168 552 : key.lower = (GBT_NUMKEY *) &kkk->lower;
169 552 : key.upper = (GBT_NUMKEY *) &kkk->upper;
170 :
171 552 : PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
172 : &tinfo, fcinfo->flinfo));
173 : }
174 :
175 : Datum
176 2 : gbt_int8_union(PG_FUNCTION_ARGS)
177 : {
178 2 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
179 2 : void *out = palloc(sizeof(int64KEY));
180 :
181 2 : *(int *) PG_GETARG_POINTER(1) = sizeof(int64KEY);
182 2 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
183 : }
184 :
185 : Datum
186 0 : gbt_int8_penalty(PG_FUNCTION_ARGS)
187 : {
188 0 : int64KEY *origentry = (int64KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
189 0 : int64KEY *newentry = (int64KEY *) 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 : }
196 :
197 : Datum
198 2 : gbt_int8_picksplit(PG_FUNCTION_ARGS)
199 : {
200 2 : 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_int8_same(PG_FUNCTION_ARGS)
207 : {
208 0 : int64KEY *b1 = (int64KEY *) PG_GETARG_POINTER(0);
209 0 : int64KEY *b2 = (int64KEY *) 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 : }
215 :
216 : static int
217 10218 : gbt_int8_ssup_cmp(Datum x, Datum y, SortSupport ssup)
218 : {
219 10218 : int64KEY *arg1 = (int64KEY *) DatumGetPointer(x);
220 10218 : int64KEY *arg2 = (int64KEY *) DatumGetPointer(y);
221 :
222 : /* for leaf items we expect lower == upper, so only compare lower */
223 10218 : if (arg1->lower < arg2->lower)
224 5134 : return -1;
225 5084 : else if (arg1->lower > arg2->lower)
226 5084 : return 1;
227 : else
228 0 : return 0;
229 :
230 : }
231 :
232 : Datum
233 2 : gbt_int8_sortsupport(PG_FUNCTION_ARGS)
234 : {
235 2 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
236 :
237 2 : ssup->comparator = gbt_int8_ssup_cmp;
238 2 : PG_RETURN_VOID();
239 : }
|