PostgreSQL Source Code git master
btree_int8.c
Go to the documentation of this file.
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/sortsupport.h"
10
11typedef struct int64key
12{
16
17/* GiST support functions */
27
28
29static bool
30gbt_int8gt(const void *a, const void *b, FmgrInfo *flinfo)
31{
32 return (*((const int64 *) a) > *((const int64 *) b));
33}
34static bool
35gbt_int8ge(const void *a, const void *b, FmgrInfo *flinfo)
36{
37 return (*((const int64 *) a) >= *((const int64 *) b));
38}
39static bool
40gbt_int8eq(const void *a, const void *b, FmgrInfo *flinfo)
41{
42 return (*((const int64 *) a) == *((const int64 *) b));
43}
44static bool
45gbt_int8le(const void *a, const void *b, FmgrInfo *flinfo)
46{
47 return (*((const int64 *) a) <= *((const int64 *) b));
48}
49static bool
50gbt_int8lt(const void *a, const void *b, FmgrInfo *flinfo)
51{
52 return (*((const int64 *) a) < *((const int64 *) b));
53}
54
55static int
56gbt_int8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
57{
58 int64KEY *ia = (int64KEY *) (((const Nsrt *) a)->t);
59 int64KEY *ib = (int64KEY *) (((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_int8_dist(const void *a, const void *b, FmgrInfo *flinfo)
74{
75 return GET_FLOAT_DISTANCE(int64, a, b);
76}
77
78
79static const gbtree_ninfo tinfo =
80{
82 sizeof(int64),
83 16, /* sizeof(gbtreekey16) */
91};
92
93
97{
100 int64 r;
101 int64 ra;
102
103 if (pg_sub_s64_overflow(a, b, &r) ||
104 r == PG_INT64_MIN)
106 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
107 errmsg("bigint out of range")));
108
109 ra = i64abs(r);
110
111 PG_RETURN_INT64(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 int64 query = PG_GETARG_INT64(1);
141
142 /* Oid subtype = PG_GETARG_OID(3); */
143 bool *recheck = (bool *) PG_GETARG_POINTER(4);
144 int64KEY *kkk = (int64KEY *) 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 int64 query = PG_GETARG_INT64(1);
162
163 /* Oid subtype = PG_GETARG_OID(3); */
164 int64KEY *kkk = (int64KEY *) 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(int64KEY));
179
180 *(int *) PG_GETARG_POINTER(1) = sizeof(int64KEY);
181 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
182}
183
184Datum
186{
187 int64KEY *origentry = (int64KEY *) 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 int64KEY *arg1 = (int64KEY *) DatumGetPointer(x);
219 int64KEY *arg2 = (int64KEY *) 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}
230
231Datum
233{
235
238}
@ gbt_t_int8
Definition: btree_gist.h:19
static bool gbt_int8lt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int8.c:50
Datum gbt_int8_picksplit(PG_FUNCTION_ARGS)
Definition: btree_int8.c:197
Datum gbt_int8_consistent(PG_FUNCTION_ARGS)
Definition: btree_int8.c:136
Datum gbt_int8_union(PG_FUNCTION_ARGS)
Definition: btree_int8.c:175
static float8 gbt_int8_dist(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int8.c:73
Datum gbt_int8_distance(PG_FUNCTION_ARGS)
Definition: btree_int8.c:158
static int gbt_int8_ssup_cmp(Datum x, Datum y, SortSupport ssup)
Definition: btree_int8.c:216
Datum gbt_int8_penalty(PG_FUNCTION_ARGS)
Definition: btree_int8.c:185
Datum gbt_int8_sortsupport(PG_FUNCTION_ARGS)
Definition: btree_int8.c:232
static bool gbt_int8eq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int8.c:40
static int gbt_int8key_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int8.c:56
static const gbtree_ninfo tinfo
Definition: btree_int8.c:79
struct int64key int64KEY
Datum gbt_int8_compress(PG_FUNCTION_ARGS)
Definition: btree_int8.c:120
Datum gbt_int8_fetch(PG_FUNCTION_ARGS)
Definition: btree_int8.c:128
static bool gbt_int8ge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int8.c:35
Datum gbt_int8_same(PG_FUNCTION_ARGS)
Definition: btree_int8.c:205
static bool gbt_int8gt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int8.c:30
PG_FUNCTION_INFO_V1(gbt_int8_compress)
static bool gbt_int8le(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_int8.c:45
Datum int8_dist(PG_FUNCTION_ARGS)
Definition: btree_int8.c:96
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
int64_t int64
Definition: c.h:499
double float8
Definition: c.h:601
#define PG_INT64_MIN
Definition: c.h:562
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_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#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 GIST_LEAF(entry)
Definition: gist.h:171
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:262
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:1939
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
int64 lower
Definition: btree_int8.c:13
int64 upper
Definition: btree_int8.c:14