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