PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
btree_int2.c
Go to the documentation of this file.
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/sortsupport.h"
10
11typedef struct int16key
12{
16
17/* GiST support functions */
27
28
29static bool
30gbt_int2gt(const void *a, const void *b, FmgrInfo *flinfo)
31{
32 return (*((const int16 *) a) > *((const int16 *) b));
33}
34static bool
35gbt_int2ge(const void *a, const void *b, FmgrInfo *flinfo)
36{
37 return (*((const int16 *) a) >= *((const int16 *) b));
38}
39static bool
40gbt_int2eq(const void *a, const void *b, FmgrInfo *flinfo)
41{
42 return (*((const int16 *) a) == *((const int16 *) b));
43}
44static bool
45gbt_int2le(const void *a, const void *b, FmgrInfo *flinfo)
46{
47 return (*((const int16 *) a) <= *((const int16 *) b));
48}
49static bool
50gbt_int2lt(const void *a, const void *b, FmgrInfo *flinfo)
51{
52 return (*((const int16 *) a) < *((const int16 *) b));
53}
54
55static int
56gbt_int2key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57{
58 int16KEY *ia = (int16KEY *) (((const Nsrt *) a)->t);
59 int16KEY *ib = (int16KEY *) (((const Nsrt *) b)->t);
60
61 if (ia->lower == ib->lower)
62 {
63 if (ia->upper == ib->upper)
64 return 0;
65
66 return (ia->upper > ib->upper) ? 1 : -1;
67 }
68
69 return (ia->lower > ib->lower) ? 1 : -1;
70}
71
72static float8
73gbt_int2_dist(const void *a, const void *b, FmgrInfo *flinfo)
74{
75 return GET_FLOAT_DISTANCE(int16, a, b);
76}
77
78
79static const gbtree_ninfo tinfo =
80{
82 sizeof(int16),
83 4, /* sizeof(gbtreekey4) */
91};
92
93
97{
100 int16 r;
101 int16 ra;
102
103 if (pg_sub_s16_overflow(a, b, &r) ||
104 r == PG_INT16_MIN)
106 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
107 errmsg("smallint out of range")));
108
109 ra = abs(r);
110
111 PG_RETURN_INT16(ra);
112}
113
114
115/**************************************************
116 * GiST support functions
117 **************************************************/
118
119Datum
121{
122 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
123
125}
126
127Datum
129{
130 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
131
133}
134
135Datum
137{
138 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
139 int16 query = PG_GETARG_INT16(1);
141
142 /* Oid subtype = PG_GETARG_OID(3); */
143 bool *recheck = (bool *) PG_GETARG_POINTER(4);
144 int16KEY *kkk = (int16KEY *) DatumGetPointer(entry->key);
146
147 /* All cases served by this function are exact */
148 *recheck = false;
149
150 key.lower = (GBT_NUMKEY *) &kkk->lower;
151 key.upper = (GBT_NUMKEY *) &kkk->upper;
152
153 PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
154 GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
155}
156
157Datum
159{
160 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
161 int16 query = PG_GETARG_INT16(1);
162
163 /* Oid subtype = PG_GETARG_OID(3); */
164 int16KEY *kkk = (int16KEY *) DatumGetPointer(entry->key);
166
167 key.lower = (GBT_NUMKEY *) &kkk->lower;
168 key.upper = (GBT_NUMKEY *) &kkk->upper;
169
171 &tinfo, fcinfo->flinfo));
172}
173
174Datum
176{
178 void *out = palloc(sizeof(int16KEY));
179
180 *(int *) PG_GETARG_POINTER(1) = sizeof(int16KEY);
181 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
182}
183
184Datum
186{
187 int16KEY *origentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
189 float *result = (float *) PG_GETARG_POINTER(2);
190
191 penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
192
193 PG_RETURN_POINTER(result);
194}
195
196Datum
198{
201 &tinfo, fcinfo->flinfo));
202}
203
204Datum
206{
209 bool *result = (bool *) PG_GETARG_POINTER(2);
210
211 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
212 PG_RETURN_POINTER(result);
213}
214
215static int
217{
218 int16KEY *arg1 = (int16KEY *) DatumGetPointer(x);
219 int16KEY *arg2 = (int16KEY *) DatumGetPointer(y);
220
221 /* for leaf items we expect lower == upper, so only compare lower */
222 if (arg1->lower < arg2->lower)
223 return -1;
224 else if (arg1->lower > arg2->lower)
225 return 1;
226 else
227 return 0;
228}
229
230Datum
232{
234
237}
@ gbt_t_int2
Definition: btree_gist.h:17
static bool gbt_int2lt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int2.c:50
static bool gbt_int2le(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int2.c:45
Datum gbt_int2_fetch(PG_FUNCTION_ARGS)
Definition: btree_int2.c:128
Datum gbt_int2_same(PG_FUNCTION_ARGS)
Definition: btree_int2.c:205
static int gbt_int2key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int2.c:56
Datum gbt_int2_sortsupport(PG_FUNCTION_ARGS)
Definition: btree_int2.c:231
Datum gbt_int2_union(PG_FUNCTION_ARGS)
Definition: btree_int2.c:175
Datum gbt_int2_consistent(PG_FUNCTION_ARGS)
Definition: btree_int2.c:136
static bool gbt_int2ge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int2.c:35
static bool gbt_int2gt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int2.c:30
PG_FUNCTION_INFO_V1(gbt_int2_compress)
static bool gbt_int2eq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int2.c:40
Datum gbt_int2_compress(PG_FUNCTION_ARGS)
Definition: btree_int2.c:120
static const gbtree_ninfo tinfo
Definition: btree_int2.c:79
Datum gbt_int2_distance(PG_FUNCTION_ARGS)
Definition: btree_int2.c:158
static int gbt_int2_ssup_cmp(Datum x, Datum y, SortSupport ssup)
Definition: btree_int2.c:216
Datum gbt_int2_penalty(PG_FUNCTION_ARGS)
Definition: btree_int2.c:185
Datum gbt_int2_picksplit(PG_FUNCTION_ARGS)
Definition: btree_int2.c:197
Datum int2_dist(PG_FUNCTION_ARGS)
Definition: btree_int2.c:96
static float8 gbt_int2_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int2.c:73
struct int16key int16KEY
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
float8 gbt_num_distance(const GBT_NUMKEY_R *key, const void *query, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query, const StrategyNumber *strategy, bool is_leaf, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
bool gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GISTENTRY * gbt_num_fetch(GISTENTRY *entry, const gbtree_ninfo *tinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
#define penalty_num(result, olower, oupper, nlower, nupper)
#define GET_FLOAT_DISTANCE(t, arg1, arg2)
char GBT_NUMKEY
double float8
Definition: c.h:601
int16_t int16
Definition: c.h:497
#define PG_INT16_MIN
Definition: c.h:556
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_RETURN_INT16(x)
Definition: fmgr.h:356
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define GIST_LEAF(entry)
Definition: gist.h:171
static bool pg_sub_s16_overflow(int16 a, int16 b, int16 *result)
Definition: int.h:85
int y
Definition: isn.c:76
int b
Definition: isn.c:74
int x
Definition: isn.c:75
int a
Definition: isn.c:73
void * palloc(Size size)
Definition: mcxt.c:1945
uintptr_t Datum
Definition: postgres.h:69
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
Datum key
Definition: gist.h:161
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
int16 upper
Definition: btree_int2.c:14
int16 lower
Definition: btree_int2.c:13