Age Owner Branch data TLA 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 */
3281 andrew@dunslane.net 23 :CBC 4 : PG_FUNCTION_INFO_V1(gbt_enum_compress);
24 : 4 : PG_FUNCTION_INFO_V1(gbt_enum_fetch);
25 : 4 : PG_FUNCTION_INFO_V1(gbt_enum_union);
26 : 4 : PG_FUNCTION_INFO_V1(gbt_enum_picksplit);
27 : 4 : PG_FUNCTION_INFO_V1(gbt_enum_consistent);
28 : 4 : PG_FUNCTION_INFO_V1(gbt_enum_penalty);
29 : 4 : PG_FUNCTION_INFO_V1(gbt_enum_same);
346 heikki.linnakangas@i 30 : 4 : PG_FUNCTION_INFO_V1(gbt_enum_sortsupport);
31 : :
32 : :
33 : : static bool
3281 andrew@dunslane.net 34 : 1593 : gbt_enumgt(const void *a, const void *b, FmgrInfo *flinfo)
35 : : {
2236 alvherre@alvh.no-ip. 36 : 1593 : return DatumGetBool(CallerFInfoFunctionCall2(enum_gt, flinfo, InvalidOid,
37 : : ObjectIdGetDatum(*((const Oid *) a)),
38 : : ObjectIdGetDatum(*((const Oid *) b))));
39 : : }
40 : : static bool
3281 andrew@dunslane.net 41 : 536 : gbt_enumge(const void *a, const void *b, FmgrInfo *flinfo)
42 : : {
2236 alvherre@alvh.no-ip. 43 : 536 : return DatumGetBool(CallerFInfoFunctionCall2(enum_ge, flinfo, InvalidOid,
44 : : ObjectIdGetDatum(*((const Oid *) a)),
45 : : ObjectIdGetDatum(*((const Oid *) b))));
46 : : }
47 : : static bool
3281 andrew@dunslane.net 48 : 532 : gbt_enumeq(const void *a, const void *b, FmgrInfo *flinfo)
49 : : {
50 : 532 : return (*((const Oid *) a) == *((const Oid *) b));
51 : : }
52 : : static bool
53 : 540 : gbt_enumle(const void *a, const void *b, FmgrInfo *flinfo)
54 : : {
2236 alvherre@alvh.no-ip. 55 : 540 : return DatumGetBool(CallerFInfoFunctionCall2(enum_le, flinfo, InvalidOid,
56 : : ObjectIdGetDatum(*((const Oid *) a)),
57 : : ObjectIdGetDatum(*((const Oid *) b))));
58 : : }
59 : : static bool
3281 andrew@dunslane.net 60 : 1593 : gbt_enumlt(const void *a, const void *b, FmgrInfo *flinfo)
61 : : {
2236 alvherre@alvh.no-ip. 62 : 1593 : return DatumGetBool(CallerFInfoFunctionCall2(enum_lt, flinfo, InvalidOid,
63 : : ObjectIdGetDatum(*((const Oid *) a)),
64 : : ObjectIdGetDatum(*((const Oid *) b))));
65 : : }
66 : :
67 : : static int
3281 andrew@dunslane.net 68 : 531 : gbt_enumkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
69 : : {
70 : 531 : oidKEY *ia = (oidKEY *) (((const Nsrt *) a)->t);
71 : 531 : oidKEY *ib = (oidKEY *) (((const Nsrt *) b)->t);
72 : :
73 [ + + ]: 531 : if (ia->lower == ib->lower)
74 : : {
75 [ + - ]: 525 : if (ia->upper == ib->upper)
76 : 525 : return 0;
77 : :
2236 alvherre@alvh.no-ip. 78 :UBC 0 : return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, flinfo, InvalidOid,
79 : : ObjectIdGetDatum(ia->upper),
80 : : ObjectIdGetDatum(ib->upper)));
81 : : }
82 : :
2236 alvherre@alvh.no-ip. 83 :CBC 6 : 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
3281 andrew@dunslane.net 108 : 534 : gbt_enum_compress(PG_FUNCTION_ARGS)
109 : : {
110 : 534 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
111 : :
112 : 534 : PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
113 : : }
114 : :
115 : : Datum
3281 andrew@dunslane.net 116 :UBC 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
3281 andrew@dunslane.net 124 :CBC 2670 : gbt_enum_consistent(PG_FUNCTION_ARGS)
125 : : {
126 : 2670 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
127 : 2670 : Oid query = PG_GETARG_OID(1);
128 : 2670 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
129 : : #ifdef NOT_USED
130 : : Oid subtype = PG_GETARG_OID(3);
131 : : #endif
132 : 2670 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
133 : 2670 : oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
134 : : GBT_NUMKEY_R key;
135 : :
136 : : /* All cases served by this function are exact */
137 : 2670 : *recheck = false;
138 : :
139 : 2670 : key.lower = (GBT_NUMKEY *) &kkk->lower;
140 : 2670 : key.upper = (GBT_NUMKEY *) &kkk->upper;
141 : :
472 peter@eisentraut.org 142 : 2670 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
143 : : GIST_LEAF(entry), &tinfo,
144 : : fcinfo->flinfo));
145 : : }
146 : :
147 : : Datum
3281 andrew@dunslane.net 148 : 1 : gbt_enum_union(PG_FUNCTION_ARGS)
149 : : {
150 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
151 : 1 : void *out = palloc(sizeof(oidKEY));
152 : :
153 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
472 peter@eisentraut.org 154 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
155 : : }
156 : :
157 : : Datum
3281 andrew@dunslane.net 158 :UBC 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 : : }
168 : :
169 : : Datum
3281 andrew@dunslane.net 170 :CBC 1 : gbt_enum_picksplit(PG_FUNCTION_ARGS)
171 : : {
2236 alvherre@alvh.no-ip. 172 : 1 : 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
3281 andrew@dunslane.net 178 :UBC 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 : : }
187 : :
188 : : static int
346 heikki.linnakangas@i 189 :CBC 4928 : gbt_enum_ssup_cmp(Datum x, Datum y, SortSupport ssup)
190 : : {
191 : 4928 : oidKEY *arg1 = (oidKEY *) DatumGetPointer(x);
192 : 4928 : oidKEY *arg2 = (oidKEY *) DatumGetPointer(y);
193 : :
194 : : /* for leaf items we expect lower == upper, so only compare lower */
195 : 9856 : return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp,
196 : 4928 : ssup->ssup_extra,
197 : : InvalidOid,
198 : : ObjectIdGetDatum(arg1->lower),
199 : : ObjectIdGetDatum(arg2->lower)));
200 : : }
201 : :
202 : : Datum
203 : 1 : gbt_enum_sortsupport(PG_FUNCTION_ARGS)
204 : : {
205 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
206 : : FmgrInfo *flinfo;
207 : :
208 : 1 : 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 : 1 : flinfo = MemoryContextAlloc(ssup->ssup_cxt, sizeof(FmgrInfo));
218 : 1 : fmgr_info_cxt(F_ENUM_CMP, flinfo, ssup->ssup_cxt);
219 : 1 : ssup->ssup_extra = flinfo;
220 : :
221 : 1 : PG_RETURN_VOID();
222 : : }
|