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 */
3091 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);
156 heikki.linnakangas@i 30 : 4 : PG_FUNCTION_INFO_V1(gbt_enum_sortsupport);
31 : :
32 : :
33 : : static bool
3091 andrew@dunslane.net 34 : 1593 : gbt_enumgt(const void *a, const void *b, FmgrInfo *flinfo)
35 : : {
2046 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
3091 andrew@dunslane.net 41 : 536 : gbt_enumge(const void *a, const void *b, FmgrInfo *flinfo)
42 : : {
2046 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
3091 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 : : {
2046 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
3091 andrew@dunslane.net 60 : 1593 : gbt_enumlt(const void *a, const void *b, FmgrInfo *flinfo)
61 : : {
2046 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
3091 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 : :
2046 alvherre@alvh.no-ip. 78 :UBC 0 : return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, flinfo, InvalidOid,
79 : : ObjectIdGetDatum(ia->upper),
80 : : ObjectIdGetDatum(ib->upper)));
81 : : }
82 : :
2046 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
3091 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
3091 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
3091 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 : :
130 : : /* Oid subtype = PG_GETARG_OID(3); */
131 : 2670 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
132 : 2670 : oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
133 : : GBT_NUMKEY_R key;
134 : :
135 : : /* All cases served by this function are exact */
136 : 2670 : *recheck = false;
137 : :
138 : 2670 : key.lower = (GBT_NUMKEY *) &kkk->lower;
139 : 2670 : key.upper = (GBT_NUMKEY *) &kkk->upper;
140 : :
282 peter@eisentraut.org 141 : 2670 : PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
142 : : GIST_LEAF(entry), &tinfo,
143 : : fcinfo->flinfo));
144 : : }
145 : :
146 : : Datum
3091 andrew@dunslane.net 147 : 1 : gbt_enum_union(PG_FUNCTION_ARGS)
148 : : {
149 : 1 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
150 : 1 : void *out = palloc(sizeof(oidKEY));
151 : :
152 : 1 : *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
282 peter@eisentraut.org 153 : 1 : PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
154 : : }
155 : :
156 : : Datum
3091 andrew@dunslane.net 157 :UBC 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
3091 andrew@dunslane.net 169 :CBC 1 : gbt_enum_picksplit(PG_FUNCTION_ARGS)
170 : : {
2046 alvherre@alvh.no-ip. 171 : 1 : 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
3091 andrew@dunslane.net 177 :UBC 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
156 heikki.linnakangas@i 188 :CBC 4928 : gbt_enum_ssup_cmp(Datum x, Datum y, SortSupport ssup)
189 : : {
190 : 4928 : oidKEY *arg1 = (oidKEY *) DatumGetPointer(x);
191 : 4928 : oidKEY *arg2 = (oidKEY *) DatumGetPointer(y);
192 : :
193 : : /* for leaf items we expect lower == upper, so only compare lower */
194 : 9856 : return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp,
195 : 4928 : ssup->ssup_extra,
196 : : InvalidOid,
197 : : ObjectIdGetDatum(arg1->lower),
198 : : ObjectIdGetDatum(arg2->lower)));
199 : : }
200 : :
201 : : Datum
202 : 1 : gbt_enum_sortsupport(PG_FUNCTION_ARGS)
203 : : {
204 : 1 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
205 : : FmgrInfo *flinfo;
206 : :
207 : 1 : 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 : 1 : flinfo = MemoryContextAlloc(ssup->ssup_cxt, sizeof(FmgrInfo));
217 : 1 : fmgr_info_cxt(F_ENUM_CMP, flinfo, ssup->ssup_cxt);
218 : 1 : ssup->ssup_extra = flinfo;
219 : :
220 : 1 : PG_RETURN_VOID();
221 : : }
|