PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
btree_enum.c
Go to the documentation of this file.
1/*
2 * contrib/btree_gist/btree_enum.c
3 */
4#include "postgres.h"
5
6#include "btree_gist.h"
7#include "btree_utils_num.h"
8#include "fmgr.h"
9#include "utils/fmgrprotos.h"
10#include "utils/fmgroids.h"
11#include "utils/sortsupport.h"
12
13/* enums are really Oids, so we just use the same structure */
14
15typedef struct
16{
19} oidKEY;
20
21/* GiST support functions */
30
31
32static bool
33gbt_enumgt(const void *a, const void *b, FmgrInfo *flinfo)
34{
36 ObjectIdGetDatum(*((const Oid *) a)),
37 ObjectIdGetDatum(*((const Oid *) b))));
38}
39static bool
40gbt_enumge(const void *a, const void *b, FmgrInfo *flinfo)
41{
43 ObjectIdGetDatum(*((const Oid *) a)),
44 ObjectIdGetDatum(*((const Oid *) b))));
45}
46static bool
47gbt_enumeq(const void *a, const void *b, FmgrInfo *flinfo)
48{
49 return (*((const Oid *) a) == *((const Oid *) b));
50}
51static bool
52gbt_enumle(const void *a, const void *b, FmgrInfo *flinfo)
53{
55 ObjectIdGetDatum(*((const Oid *) a)),
56 ObjectIdGetDatum(*((const Oid *) b))));
57}
58static bool
59gbt_enumlt(const void *a, const void *b, FmgrInfo *flinfo)
60{
62 ObjectIdGetDatum(*((const Oid *) a)),
63 ObjectIdGetDatum(*((const Oid *) b))));
64}
65
66static int
67gbt_enumkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
68{
69 oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
70 oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
71
72 if (ia->lower == ib->lower)
73 {
74 if (ia->upper == ib->upper)
75 return 0;
76
80 }
81
85}
86
87static const gbtree_ninfo tinfo =
88{
90 sizeof(Oid),
91 8, /* sizeof(gbtreekey8) */
98 NULL /* no KNN support at least for now */
99};
100
101
102/**************************************************
103 * GiST support functions
104 **************************************************/
105
106Datum
108{
109 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
110
112}
113
114Datum
116{
117 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
118
120}
121
122Datum
124{
125 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
126 Oid query = PG_GETARG_OID(1);
128
129 /* Oid subtype = PG_GETARG_OID(3); */
130 bool *recheck = (bool *) PG_GETARG_POINTER(4);
131 oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
133
134 /* All cases served by this function are exact */
135 *recheck = false;
136
137 key.lower = (GBT_NUMKEY *) &kkk->lower;
138 key.upper = (GBT_NUMKEY *) &kkk->upper;
139
140 PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
141 GIST_LEAF(entry), &tinfo,
142 fcinfo->flinfo));
143}
144
145Datum
147{
149 void *out = palloc(sizeof(oidKEY));
150
151 *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
152 PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
153}
154
155Datum
157{
158 oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
159 oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
160 float *result = (float *) PG_GETARG_POINTER(2);
161
162 penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
163
164 PG_RETURN_POINTER(result);
165}
166
167Datum
169{
172 &tinfo, fcinfo->flinfo));
173}
174
175Datum
177{
178 oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
179 oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
180 bool *result = (bool *) PG_GETARG_POINTER(2);
181
182 *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
183 PG_RETURN_POINTER(result);
184}
185
186static int
188{
189 oidKEY *arg1 = (oidKEY *) DatumGetPointer(x);
190 oidKEY *arg2 = (oidKEY *) DatumGetPointer(y);
191
192 /* for leaf items we expect lower == upper, so only compare lower */
194 ssup->ssup_extra,
196 arg1->lower,
197 arg2->lower));
198}
199
200Datum
202{
204 FmgrInfo *flinfo;
205
207
208 /*
209 * Since gbt_enum_ssup_cmp() uses enum_cmp() like the rest of the
210 * comparison functions, it also needs to pass flinfo when calling it. The
211 * caller to a SortSupport comparison function doesn't provide an FmgrInfo
212 * struct, so look it up now, save it in ssup_extra and use it in
213 * gbt_enum_ssup_cmp() later.
214 */
215 flinfo = MemoryContextAlloc(ssup->ssup_cxt, sizeof(FmgrInfo));
216 fmgr_info_cxt(F_ENUM_CMP, flinfo, ssup->ssup_cxt);
217 ssup->ssup_extra = flinfo;
218
220}
Datum gbt_enum_union(PG_FUNCTION_ARGS)
Definition: btree_enum.c:146
static bool gbt_enumeq(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_enum.c:47
Datum gbt_enum_penalty(PG_FUNCTION_ARGS)
Definition: btree_enum.c:156
Datum gbt_enum_compress(PG_FUNCTION_ARGS)
Definition: btree_enum.c:107
Datum gbt_enum_fetch(PG_FUNCTION_ARGS)
Definition: btree_enum.c:115
static bool gbt_enumge(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_enum.c:40
static bool gbt_enumlt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_enum.c:59
Datum gbt_enum_sortsupport(PG_FUNCTION_ARGS)
Definition: btree_enum.c:201
Datum gbt_enum_picksplit(PG_FUNCTION_ARGS)
Definition: btree_enum.c:168
static int gbt_enumkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_enum.c:67
static const gbtree_ninfo tinfo
Definition: btree_enum.c:87
Datum gbt_enum_consistent(PG_FUNCTION_ARGS)
Definition: btree_enum.c:123
PG_FUNCTION_INFO_V1(gbt_enum_compress)
static bool gbt_enumgt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_enum.c:33
Datum gbt_enum_same(PG_FUNCTION_ARGS)
Definition: btree_enum.c:176
static int gbt_enum_ssup_cmp(Datum x, Datum y, SortSupport ssup)
Definition: btree_enum.c:187
static bool gbt_enumle(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_enum.c:52
@ gbt_t_enum
Definition: btree_gist.h:38
GISTENTRY * gbt_num_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
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)
char GBT_NUMKEY
Datum enum_gt(PG_FUNCTION_ARGS)
Definition: enum.c:351
Datum enum_lt(PG_FUNCTION_ARGS)
Definition: enum.c:306
Datum enum_cmp(PG_FUNCTION_ARGS)
Definition: enum.c:378
Datum enum_ge(PG_FUNCTION_ARGS)
Definition: enum.c:342
Datum enum_le(PG_FUNCTION_ARGS)
Definition: enum.c:315
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1085
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#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
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 * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1260
void * palloc(Size size)
Definition: mcxt.c:1945
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:207
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
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
void * ssup_extra
Definition: sortsupport.h:87
MemoryContext ssup_cxt
Definition: sortsupport.h:66
Oid upper
Definition: btree_enum.c:18
Oid lower
Definition: btree_enum.c:17