Debian Codegraph @

contrib/btree_gist/btree_enum.c

raw
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
15 typedef struct
16 {
17 Oid lower;
18 Oid upper;
19 } oidKEY;
20
21 /* GiST support functions */
22 PG_FUNCTION_INFO_V1(gbt_enum_compress);
23 PG_FUNCTION_INFO_V1(gbt_enum_fetch);
24 PG_FUNCTION_INFO_V1(gbt_enum_union);
25 PG_FUNCTION_INFO_V1(gbt_enum_picksplit);
26 PG_FUNCTION_INFO_V1(gbt_enum_consistent);
27 PG_FUNCTION_INFO_V1(gbt_enum_penalty);
28 PG_FUNCTION_INFO_V1(gbt_enum_same);
29 PG_FUNCTION_INFO_V1(gbt_enum_sortsupport);
30
31
32 static bool
33 gbt_enumgt(const void *a, const void *b, FmgrInfo *flinfo)
34 {
35 return DatumGetBool(CallerFInfoFunctionCall2(enum_gt, flinfo, InvalidOid,
36 ObjectIdGetDatum(*((const Oid *) a)),
37 ObjectIdGetDatum(*((const Oid *) b))));
38 }
39 static bool
40 gbt_enumge(const void *a, const void *b, FmgrInfo *flinfo)
41 {
42 return DatumGetBool(CallerFInfoFunctionCall2(enum_ge, flinfo, InvalidOid,
43 ObjectIdGetDatum(*((const Oid *) a)),
44 ObjectIdGetDatum(*((const Oid *) b))));
45 }
46 static bool
47 gbt_enumeq(const void *a, const void *b, FmgrInfo *flinfo)
48 {
49 return (*((const Oid *) a) == *((const Oid *) b));
50 }
51 static bool
52 gbt_enumle(const void *a, const void *b, FmgrInfo *flinfo)
53 {
54 return DatumGetBool(CallerFInfoFunctionCall2(enum_le, flinfo, InvalidOid,
55 ObjectIdGetDatum(*((const Oid *) a)),
56 ObjectIdGetDatum(*((const Oid *) b))));
57 }
58 static bool
59 gbt_enumlt(const void *a, const void *b, FmgrInfo *flinfo)
60 {
61 return DatumGetBool(CallerFInfoFunctionCall2(enum_lt, flinfo, InvalidOid,
62 ObjectIdGetDatum(*((const Oid *) a)),
63 ObjectIdGetDatum(*((const Oid *) b))));
64 }
65
66 static int
67 gbt_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
77 return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, flinfo, InvalidOid,
78 ObjectIdGetDatum(ia->upper),
79 ObjectIdGetDatum(ib->upper)));
80 }
81
82 return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, flinfo, InvalidOid,
83 ObjectIdGetDatum(ia->lower),
84 ObjectIdGetDatum(ib->lower)));
85 }
86
87 static const gbtree_ninfo tinfo =
88 {
89 gbt_t_enum,
90 sizeof(Oid),
91 8, /* sizeof(gbtreekey8) */
92 gbt_enumgt,
93 gbt_enumge,
94 gbt_enumeq,
95 gbt_enumle,
96 gbt_enumlt,
97 gbt_enumkey_cmp,
98 NULL /* no KNN support at least for now */
99 };
100
101
102 /**************************************************
103 * GiST support functions
104 **************************************************/
105
106 Datum
107 gbt_enum_compress(PG_FUNCTION_ARGS)
108 {
109 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
110
111 PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
112 }
113
114 Datum
115 gbt_enum_fetch(PG_FUNCTION_ARGS)
116 {
117 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
118
119 PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
120 }
121
122 Datum
123 gbt_enum_consistent(PG_FUNCTION_ARGS)
124 {
125 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
126 Oid query = PG_GETARG_OID(1);
127 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
128
129 /* Oid subtype = PG_GETARG_OID(3); */
130 bool *recheck = (bool *) PG_GETARG_POINTER(4);
131 oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
132 GBT_NUMKEY_R 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
145 Datum
146 gbt_enum_union(PG_FUNCTION_ARGS)
147 {
148 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
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
155 Datum
156 gbt_enum_penalty(PG_FUNCTION_ARGS)
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
167 Datum
168 gbt_enum_picksplit(PG_FUNCTION_ARGS)
169 {
170 PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
171 (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
172 &tinfo, fcinfo->flinfo));
173 }
174
175 Datum
176 gbt_enum_same(PG_FUNCTION_ARGS)
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
186 static int
187 gbt_enum_ssup_cmp(Datum x, Datum y, SortSupport ssup)
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 */
193 return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp,
194 ssup->ssup_extra,
195 InvalidOid,
196 arg1->lower,
197 arg2->lower));
198 }
199
200 Datum
201 gbt_enum_sortsupport(PG_FUNCTION_ARGS)
202 {
203 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
204 FmgrInfo *flinfo;
205
206 ssup->comparator = gbt_enum_ssup_cmp;
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
219 PG_RETURN_VOID();
220 }