2 * contrib/btree_gist/btree_int4.c
4 #include "btree_gist.h"
5 #include "btree_utils_num.h"
7 typedef struct int32key
16 PG_FUNCTION_INFO_V1(gbt_int4_compress);
17 PG_FUNCTION_INFO_V1(gbt_int4_union);
18 PG_FUNCTION_INFO_V1(gbt_int4_picksplit);
19 PG_FUNCTION_INFO_V1(gbt_int4_consistent);
20 PG_FUNCTION_INFO_V1(gbt_int4_distance);
21 PG_FUNCTION_INFO_V1(gbt_int4_penalty);
22 PG_FUNCTION_INFO_V1(gbt_int4_same);
24 Datum gbt_int4_compress(PG_FUNCTION_ARGS);
25 Datum gbt_int4_union(PG_FUNCTION_ARGS);
26 Datum gbt_int4_picksplit(PG_FUNCTION_ARGS);
27 Datum gbt_int4_consistent(PG_FUNCTION_ARGS);
28 Datum gbt_int4_distance(PG_FUNCTION_ARGS);
29 Datum gbt_int4_penalty(PG_FUNCTION_ARGS);
30 Datum gbt_int4_same(PG_FUNCTION_ARGS);
34 gbt_int4gt(const void *a, const void *b)
36 return (*((int32 *) a) > *((int32 *) b));
39 gbt_int4ge(const void *a, const void *b)
41 return (*((int32 *) a) >= *((int32 *) b));
44 gbt_int4eq(const void *a, const void *b)
46 return (*((int32 *) a) == *((int32 *) b));
49 gbt_int4le(const void *a, const void *b)
51 return (*((int32 *) a) <= *((int32 *) b));
54 gbt_int4lt(const void *a, const void *b)
56 return (*((int32 *) a) < *((int32 *) b));
60 gbt_int4key_cmp(const void *a, const void *b)
62 int32KEY *ia = (int32KEY *) (((Nsrt *) a)->t);
63 int32KEY *ib = (int32KEY *) (((Nsrt *) b)->t);
65 if (ia->lower == ib->lower)
67 if (ia->upper == ib->upper)
70 return (ia->upper > ib->upper) ? 1 : -1;
73 return (ia->lower > ib->lower) ? 1 : -1;
77 gbt_int4_dist(const void *a, const void *b)
79 return GET_FLOAT_DISTANCE(int4, a, b);
83 static const gbtree_ninfo tinfo =
97 PG_FUNCTION_INFO_V1(int4_dist);
98 Datum int4_dist(PG_FUNCTION_ARGS);
100 int4_dist(PG_FUNCTION_ARGS)
102 int4 a = PG_GETARG_INT32(0);
103 int4 b = PG_GETARG_INT32(1);
110 /* Overflow check. */
111 if (ra < 0 || (!SAMESIGN(a, b) && !SAMESIGN(r, a)))
113 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
114 errmsg("integer out of range")));
120 /**************************************************
122 **************************************************/
126 gbt_int4_compress(PG_FUNCTION_ARGS)
128 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
129 GISTENTRY *retval = NULL;
131 PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
136 gbt_int4_consistent(PG_FUNCTION_ARGS)
138 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
139 int32 query = PG_GETARG_INT32(1);
140 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
142 /* Oid subtype = PG_GETARG_OID(3); */
143 bool *recheck = (bool *) PG_GETARG_POINTER(4);
144 int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key);
147 /* All cases served by this function are exact */
150 key.lower = (GBT_NUMKEY *) &kkk->lower;
151 key.upper = (GBT_NUMKEY *) &kkk->upper;
154 gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
160 gbt_int4_distance(PG_FUNCTION_ARGS)
162 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
163 int32 query = PG_GETARG_INT32(1);
165 /* Oid subtype = PG_GETARG_OID(3); */
166 int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key);
169 key.lower = (GBT_NUMKEY *) &kkk->lower;
170 key.upper = (GBT_NUMKEY *) &kkk->upper;
173 gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo)
179 gbt_int4_union(PG_FUNCTION_ARGS)
181 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
182 void *out = palloc(sizeof(int32KEY));
184 *(int *) PG_GETARG_POINTER(1) = sizeof(int32KEY);
185 PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
190 gbt_int4_penalty(PG_FUNCTION_ARGS)
192 int32KEY *origentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
193 int32KEY *newentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
194 float *result = (float *) PG_GETARG_POINTER(2);
196 penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
198 PG_RETURN_POINTER(result);
202 gbt_int4_picksplit(PG_FUNCTION_ARGS)
204 PG_RETURN_POINTER(gbt_num_picksplit(
205 (GistEntryVector *) PG_GETARG_POINTER(0),
206 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
212 gbt_int4_same(PG_FUNCTION_ARGS)
214 int32KEY *b1 = (int32KEY *) PG_GETARG_POINTER(0);
215 int32KEY *b2 = (int32KEY *) PG_GETARG_POINTER(1);
216 bool *result = (bool *) PG_GETARG_POINTER(2);
218 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
219 PG_RETURN_POINTER(result);