Line data Source code
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/rel.h"
12 : #include "utils/sortsupport.h"
13 :
14 : /* enums are really Oids, so we just use the same structure */
15 :
16 : typedef struct
17 : {
18 : Oid lower;
19 : Oid upper;
20 : } oidKEY;
21 :
22 : /* GiST support functions */
23 0 : PG_FUNCTION_INFO_V1(gbt_enum_compress);
24 0 : PG_FUNCTION_INFO_V1(gbt_enum_fetch);
25 0 : PG_FUNCTION_INFO_V1(gbt_enum_union);
26 0 : PG_FUNCTION_INFO_V1(gbt_enum_picksplit);
27 0 : PG_FUNCTION_INFO_V1(gbt_enum_consistent);
28 0 : PG_FUNCTION_INFO_V1(gbt_enum_penalty);
29 0 : PG_FUNCTION_INFO_V1(gbt_enum_same);
30 0 : PG_FUNCTION_INFO_V1(gbt_enum_sortsupport);
31 :
32 :
33 : static bool
34 0 : gbt_enumgt(const void *a, const void *b, FmgrInfo *flinfo)
35 : {
36 0 : return DatumGetBool(CallerFInfoFunctionCall2(enum_gt, flinfo, InvalidOid,
37 0 : ObjectIdGetDatum(*((const Oid *) a)),
38 0 : ObjectIdGetDatum(*((const Oid *) b))));
39 : }
40 : static bool
41 0 : gbt_enumge(const void *a, const void *b, FmgrInfo *flinfo)
42 : {
43 0 : return DatumGetBool(CallerFInfoFunctionCall2(enum_ge, flinfo, InvalidOid,
44 0 : ObjectIdGetDatum(*((const Oid *) a)),
45 0 : ObjectIdGetDatum(*((const Oid *) b))));
46 : }
47 : static bool
48 0 : gbt_enumeq(const void *a, const void *b, FmgrInfo *flinfo)
49 : {
50 0 : return (*((const Oid *) a) == *((const Oid *) b));
51 : }
52 : static bool
53 0 : gbt_enumle(const void *a, const void *b, FmgrInfo *flinfo)
54 : {
55 0 : return DatumGetBool(CallerFInfoFunctionCall2(enum_le, flinfo, InvalidOid,
56 0 : ObjectIdGetDatum(*((const Oid *) a)),
57 0 : ObjectIdGetDatum(*((const Oid *) b))));
58 : }
59 : static bool
60 0 : gbt_enumlt(const void *a, const void *b, FmgrInfo *flinfo)
61 : {
62 0 : return DatumGetBool(CallerFInfoFunctionCall2(enum_lt, flinfo, InvalidOid,
63 0 : ObjectIdGetDatum(*((const Oid *) a)),
64 0 : ObjectIdGetDatum(*((const Oid *) b))));
65 : }
66 :
67 : static int
68 0 : gbt_enumkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
69 : {
70 0 : oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
71 0 : oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
72 :
73 0 : if (ia->lower == ib->lower)
74 : {
75 0 : if (ia->upper == ib->upper)
76 0 : return 0;
77 :
78 0 : return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, flinfo, InvalidOid,
79 0 : ObjectIdGetDatum(ia->upper),
80 0 : ObjectIdGetDatum(ib->upper)));
81 : }
82 :
83 0 : return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, flinfo, InvalidOid,
84 0 : ObjectIdGetDatum(ia->lower),
85 0 : ObjectIdGetDatum(ib->lower)));
86 0 : }
87 :
88 : static const gbtree_ninfo tinfo =
89 : {
90 : gbt_t_enum,
91 : sizeof(Oid),
92 : 8, /* sizeof(gbtreekey8) */
93 : gbt_enumgt,
94 : gbt_enumge,
95 : gbt_enumeq,
96 : gbt_enumle,
97 : gbt_enumlt,
98 : gbt_enumkey_cmp,
99 : NULL /* no KNN support at least for now */
100 : };
101 :
102 :
103 : /**************************************************
104 : * GiST support functions
105 : **************************************************/
106 :
107 : Datum
108 0 : gbt_enum_compress(PG_FUNCTION_ARGS)
109 : {
110 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
111 :
112 0 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
113 0 : }
114 :
115 : Datum
116 0 : gbt_enum_fetch(PG_FUNCTION_ARGS)
117 : {
118 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
119 :
120 0 : PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
121 0 : }
122 :
123 : Datum
124 0 : gbt_enum_consistent(PG_FUNCTION_ARGS)
125 : {
126 0 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
127 0 : Oid query = PG_GETARG_OID(1);
128 0 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
129 : #ifdef NOT_USED
130 : Oid subtype = PG_GETARG_OID(3);
131 : #endif
132 0 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
133 0 : oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
134 0 : GBT_NUMKEY_R key;
135 :
136 : /* All cases served by this function are exact */
137 0 : *recheck = false;
138 :
139 0 : key.lower = (GBT_NUMKEY *) &kkk->lower;
140 0 : key.upper = (GBT_NUMKEY *) &kkk->upper;
141 :
142 0 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
143 : GIST_LEAF(entry), &tinfo,
144 : fcinfo->flinfo));
145 0 : }
146 :
147 : Datum
148 0 : gbt_enum_union(PG_FUNCTION_ARGS)
149 : {
150 0 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
151 0 : void *out = palloc(sizeof(oidKEY));
152 :
153 0 : *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
154 0 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
155 0 : }
156 :
157 : Datum
158 0 : gbt_enum_penalty(PG_FUNCTION_ARGS)
159 : {
160 0 : oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
161 0 : oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
162 0 : float *result = (float *) PG_GETARG_POINTER(2);
163 :
164 0 : penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
165 :
166 0 : PG_RETURN_POINTER(result);
167 0 : }
168 :
169 : Datum
170 0 : gbt_enum_picksplit(PG_FUNCTION_ARGS)
171 : {
172 0 : PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
173 : (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
174 : &tinfo, fcinfo->flinfo));
175 : }
176 :
177 : Datum
178 0 : gbt_enum_same(PG_FUNCTION_ARGS)
179 : {
180 0 : oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
181 0 : oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
182 0 : bool *result = (bool *) PG_GETARG_POINTER(2);
183 :
184 0 : *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
185 0 : PG_RETURN_POINTER(result);
186 0 : }
187 :
188 : static int
189 0 : gbt_enum_ssup_cmp(Datum x, Datum y, SortSupport ssup)
190 : {
191 0 : oidKEY *arg1 = (oidKEY *) DatumGetPointer(x);
192 0 : oidKEY *arg2 = (oidKEY *) DatumGetPointer(y);
193 :
194 : /* for leaf items we expect lower == upper, so only compare lower */
195 0 : return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp,
196 0 : ssup->ssup_extra,
197 : InvalidOid,
198 0 : ObjectIdGetDatum(arg1->lower),
199 0 : ObjectIdGetDatum(arg2->lower)));
200 0 : }
201 :
202 : Datum
203 0 : gbt_enum_sortsupport(PG_FUNCTION_ARGS)
204 : {
205 0 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
206 0 : FmgrInfo *flinfo;
207 :
208 0 : ssup->comparator = gbt_enum_ssup_cmp;
209 :
210 : /*
211 : * Since gbt_enum_ssup_cmp() uses enum_cmp() like the rest of the
212 : * comparison functions, it also needs to pass flinfo when calling it. The
213 : * caller to a SortSupport comparison function doesn't provide an FmgrInfo
214 : * struct, so look it up now, save it in ssup_extra and use it in
215 : * gbt_enum_ssup_cmp() later.
216 : */
217 0 : flinfo = MemoryContextAlloc(ssup->ssup_cxt, sizeof(FmgrInfo));
218 0 : fmgr_info_cxt(F_ENUM_CMP, flinfo, ssup->ssup_cxt);
219 0 : ssup->ssup_extra = flinfo;
220 :
221 0 : PG_RETURN_VOID();
222 0 : }
|