Line data Source code
1 : /*
2 : * contrib/btree_gist/btree_text.c
3 : */
4 : #include "postgres.h"
5 :
6 : #include "btree_gist.h"
7 : #include "btree_utils_var.h"
8 : #include "utils/builtins.h"
9 :
10 : /*
11 : ** Text ops
12 : */
13 8 : PG_FUNCTION_INFO_V1(gbt_text_compress);
14 4 : PG_FUNCTION_INFO_V1(gbt_bpchar_compress);
15 10 : PG_FUNCTION_INFO_V1(gbt_text_union);
16 10 : PG_FUNCTION_INFO_V1(gbt_text_picksplit);
17 8 : PG_FUNCTION_INFO_V1(gbt_text_consistent);
18 4 : PG_FUNCTION_INFO_V1(gbt_bpchar_consistent);
19 10 : PG_FUNCTION_INFO_V1(gbt_text_penalty);
20 10 : PG_FUNCTION_INFO_V1(gbt_text_same);
21 :
22 :
23 : /* define for comparison */
24 :
25 : static bool
26 2390 : gbt_textgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
27 : {
28 2390 : return DatumGetBool(DirectFunctionCall2Coll(text_gt,
29 : collation,
30 : PointerGetDatum(a),
31 : PointerGetDatum(b)));
32 : }
33 :
34 : static bool
35 2404 : gbt_textge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
36 : {
37 2404 : return DatumGetBool(DirectFunctionCall2Coll(text_ge,
38 : collation,
39 : PointerGetDatum(a),
40 : PointerGetDatum(b)));
41 : }
42 :
43 : static bool
44 602 : gbt_texteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
45 : {
46 602 : return DatumGetBool(DirectFunctionCall2Coll(texteq,
47 : collation,
48 : PointerGetDatum(a),
49 : PointerGetDatum(b)));
50 : }
51 :
52 : static bool
53 2142 : gbt_textle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
54 : {
55 2142 : return DatumGetBool(DirectFunctionCall2Coll(text_le,
56 : collation,
57 : PointerGetDatum(a),
58 : PointerGetDatum(b)));
59 : }
60 :
61 : static bool
62 2002 : gbt_textlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
63 : {
64 2002 : return DatumGetBool(DirectFunctionCall2Coll(text_lt,
65 : collation,
66 : PointerGetDatum(a),
67 : PointerGetDatum(b)));
68 : }
69 :
70 : static int32
71 124218 : gbt_textcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
72 : {
73 124218 : return DatumGetInt32(DirectFunctionCall2Coll(bttextcmp,
74 : collation,
75 : PointerGetDatum(a),
76 : PointerGetDatum(b)));
77 : }
78 :
79 : static gbtree_vinfo tinfo =
80 : {
81 : gbt_t_text,
82 : 0,
83 : false,
84 : gbt_textgt,
85 : gbt_textge,
86 : gbt_texteq,
87 : gbt_textle,
88 : gbt_textlt,
89 : gbt_textcmp,
90 : NULL
91 : };
92 :
93 : /* bpchar needs its own comparison rules */
94 :
95 : static bool
96 1192 : gbt_bpchargt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
97 : {
98 1192 : return DatumGetBool(DirectFunctionCall2Coll(bpchargt,
99 : collation,
100 : PointerGetDatum(a),
101 : PointerGetDatum(b)));
102 : }
103 :
104 : static bool
105 1210 : gbt_bpcharge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
106 : {
107 1210 : return DatumGetBool(DirectFunctionCall2Coll(bpcharge,
108 : collation,
109 : PointerGetDatum(a),
110 : PointerGetDatum(b)));
111 : }
112 :
113 : static bool
114 142 : gbt_bpchareq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
115 : {
116 142 : return DatumGetBool(DirectFunctionCall2Coll(bpchareq,
117 : collation,
118 : PointerGetDatum(a),
119 : PointerGetDatum(b)));
120 : }
121 :
122 : static bool
123 1068 : gbt_bpcharle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
124 : {
125 1068 : return DatumGetBool(DirectFunctionCall2Coll(bpcharle,
126 : collation,
127 : PointerGetDatum(a),
128 : PointerGetDatum(b)));
129 : }
130 :
131 : static bool
132 926 : gbt_bpcharlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
133 : {
134 926 : return DatumGetBool(DirectFunctionCall2Coll(bpcharlt,
135 : collation,
136 : PointerGetDatum(a),
137 : PointerGetDatum(b)));
138 : }
139 :
140 : static int32
141 142 : gbt_bpcharcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
142 : {
143 142 : return DatumGetInt32(DirectFunctionCall2Coll(bpcharcmp,
144 : collation,
145 : PointerGetDatum(a),
146 : PointerGetDatum(b)));
147 : }
148 :
149 : static gbtree_vinfo bptinfo =
150 : {
151 : gbt_t_bpchar,
152 : 0,
153 : false,
154 : gbt_bpchargt,
155 : gbt_bpcharge,
156 : gbt_bpchareq,
157 : gbt_bpcharle,
158 : gbt_bpcharlt,
159 : gbt_bpcharcmp,
160 : NULL
161 : };
162 :
163 :
164 : /**************************************************
165 : * Text ops
166 : **************************************************/
167 :
168 :
169 : Datum
170 6038 : gbt_text_compress(PG_FUNCTION_ARGS)
171 : {
172 6038 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
173 :
174 6038 : if (tinfo.eml == 0)
175 : {
176 8 : tinfo.eml = pg_database_encoding_max_length();
177 : }
178 :
179 6038 : PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
180 : }
181 :
182 : Datum
183 2020 : gbt_bpchar_compress(PG_FUNCTION_ARGS)
184 : {
185 : /* This should never have been distinct from gbt_text_compress */
186 2020 : return gbt_text_compress(fcinfo);
187 : }
188 :
189 :
190 :
191 : Datum
192 9682 : gbt_text_consistent(PG_FUNCTION_ARGS)
193 : {
194 9682 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
195 9682 : void *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
196 9682 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
197 :
198 : /* Oid subtype = PG_GETARG_OID(3); */
199 9682 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
200 : bool retval;
201 9682 : GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
202 9682 : GBT_VARKEY_R r = gbt_var_key_readable(key);
203 :
204 : /* All cases served by this function are exact */
205 9682 : *recheck = false;
206 :
207 9682 : if (tinfo.eml == 0)
208 : {
209 0 : tinfo.eml = pg_database_encoding_max_length();
210 : }
211 :
212 19364 : retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
213 9682 : GIST_LEAF(entry), &tinfo, fcinfo->flinfo);
214 :
215 9682 : PG_RETURN_BOOL(retval);
216 : }
217 :
218 :
219 : Datum
220 4666 : gbt_bpchar_consistent(PG_FUNCTION_ARGS)
221 : {
222 4666 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
223 4666 : void *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
224 4666 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
225 :
226 : /* Oid subtype = PG_GETARG_OID(3); */
227 4666 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
228 : bool retval;
229 4666 : GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
230 4666 : GBT_VARKEY_R r = gbt_var_key_readable(key);
231 :
232 : /* All cases served by this function are exact */
233 4666 : *recheck = false;
234 :
235 4666 : if (bptinfo.eml == 0)
236 : {
237 2 : bptinfo.eml = pg_database_encoding_max_length();
238 : }
239 :
240 9332 : retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
241 4666 : GIST_LEAF(entry), &bptinfo, fcinfo->flinfo);
242 4666 : PG_RETURN_BOOL(retval);
243 : }
244 :
245 :
246 : Datum
247 4984 : gbt_text_union(PG_FUNCTION_ARGS)
248 : {
249 4984 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
250 4984 : int32 *size = (int *) PG_GETARG_POINTER(1);
251 :
252 4984 : PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
253 : &tinfo, fcinfo->flinfo));
254 : }
255 :
256 :
257 : Datum
258 38 : gbt_text_picksplit(PG_FUNCTION_ARGS)
259 : {
260 38 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
261 38 : GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
262 :
263 38 : gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
264 : &tinfo, fcinfo->flinfo);
265 38 : PG_RETURN_POINTER(v);
266 : }
267 :
268 : Datum
269 4978 : gbt_text_same(PG_FUNCTION_ARGS)
270 : {
271 4978 : Datum d1 = PG_GETARG_DATUM(0);
272 4978 : Datum d2 = PG_GETARG_DATUM(1);
273 4978 : bool *result = (bool *) PG_GETARG_POINTER(2);
274 :
275 4978 : *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
276 4978 : PG_RETURN_POINTER(result);
277 : }
278 :
279 :
280 : Datum
281 19068 : gbt_text_penalty(PG_FUNCTION_ARGS)
282 : {
283 19068 : GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
284 19068 : GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
285 19068 : float *result = (float *) PG_GETARG_POINTER(2);
286 :
287 19068 : PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
288 : &tinfo, fcinfo->flinfo));
289 : }
|