PostgreSQL Source Code  git master
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/builtins.h"
10 
11 /* enums are really Oids, so we just use the same structure */
12 
13 typedef struct
14 {
17 } oidKEY;
18 
19 /*
20 ** enum ops
21 */
29 
30 
31 static bool
32 gbt_enumgt(const void *a, const void *b, FmgrInfo *flinfo)
33 {
35  ObjectIdGetDatum(*((const Oid *) a)),
36  ObjectIdGetDatum(*((const Oid *) b))));
37 }
38 static bool
39 gbt_enumge(const void *a, const void *b, FmgrInfo *flinfo)
40 {
42  ObjectIdGetDatum(*((const Oid *) a)),
43  ObjectIdGetDatum(*((const Oid *) b))));
44 }
45 static bool
46 gbt_enumeq(const void *a, const void *b, FmgrInfo *flinfo)
47 {
48  return (*((const Oid *) a) == *((const Oid *) b));
49 }
50 static bool
51 gbt_enumle(const void *a, const void *b, FmgrInfo *flinfo)
52 {
54  ObjectIdGetDatum(*((const Oid *) a)),
55  ObjectIdGetDatum(*((const Oid *) b))));
56 }
57 static bool
58 gbt_enumlt(const void *a, const void *b, FmgrInfo *flinfo)
59 {
61  ObjectIdGetDatum(*((const Oid *) a)),
62  ObjectIdGetDatum(*((const Oid *) b))));
63 }
64 
65 static int
66 gbt_enumkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
67 {
68  oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
69  oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
70 
71  if (ia->lower == ib->lower)
72  {
73  if (ia->upper == ib->upper)
74  return 0;
75 
78  ObjectIdGetDatum(ib->upper)));
79  }
80 
83  ObjectIdGetDatum(ib->lower)));
84 }
85 
86 static const gbtree_ninfo tinfo =
87 {
88  gbt_t_enum,
89  sizeof(Oid),
90  8, /* sizeof(gbtreekey8) */
91  gbt_enumgt,
92  gbt_enumge,
93  gbt_enumeq,
94  gbt_enumle,
95  gbt_enumlt,
97  NULL /* no KNN support at least for now */
98 };
99 
100 
101 /**************************************************
102  * Enum ops
103  **************************************************/
104 
105 
106 Datum
108 {
109  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
110 
112 }
113 
114 Datum
116 {
117  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
118 
120 }
121 
122 Datum
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, (void *) &query, &strategy,
141  GIST_LEAF(entry), &tinfo,
142  fcinfo->flinfo));
143 }
144 
145 Datum
147 {
149  void *out = palloc(sizeof(oidKEY));
150 
151  *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
152  PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo, fcinfo->flinfo));
153 }
154 
155 
156 Datum
158 {
159  oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
160  oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
161  float *result = (float *) PG_GETARG_POINTER(2);
162 
163  penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
164 
165  PG_RETURN_POINTER(result);
166 }
167 
168 Datum
170 {
173  &tinfo, fcinfo->flinfo));
174 }
175 
176 Datum
178 {
179  oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
180  oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
181  bool *result = (bool *) PG_GETARG_POINTER(2);
182 
183  *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
184  PG_RETURN_POINTER(result);
185 }
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:46
Datum gbt_enum_penalty(PG_FUNCTION_ARGS)
Definition: btree_enum.c:157
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:39
static bool gbt_enumlt(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_enum.c:58
Datum gbt_enum_picksplit(PG_FUNCTION_ARGS)
Definition: btree_enum.c:169
static int gbt_enumkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_enum.c:66
static const gbtree_ninfo tinfo
Definition: btree_enum.c:86
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:32
Datum gbt_enum_same(PG_FUNCTION_ARGS)
Definition: btree_enum.c:177
static bool gbt_enumle(const void *a, const void *b, FmgrInfo *flinfo)
Definition: btree_enum.c:51
@ gbt_t_enum
Definition: btree_gist.h:38
GISTENTRY * gbt_num_fetch(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_compress(GISTENTRY *entry, const gbtree_ninfo *tinfo)
void * gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo, FmgrInfo *flinfo)
GIST_SPLITVEC * gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, 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
Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1085
#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:170
int b
Definition: isn.c:70
int a
Definition: isn.c:69
void * palloc(Size size)
Definition: mcxt.c:1316
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
uint16 StrategyNumber
Definition: stratnum.h:22
Definition: fmgr.h:57
Datum key
Definition: gist.h:160
Oid upper
Definition: btree_enum.c:16
Oid lower
Definition: btree_enum.c:15