Age Owner TLA 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 : */
6890 teodor 13 CBC 4 : PG_FUNCTION_INFO_V1(gbt_text_compress);
14 2 : PG_FUNCTION_INFO_V1(gbt_bpchar_compress);
15 5 : PG_FUNCTION_INFO_V1(gbt_text_union);
16 5 : PG_FUNCTION_INFO_V1(gbt_text_picksplit);
17 4 : PG_FUNCTION_INFO_V1(gbt_text_consistent);
18 2 : PG_FUNCTION_INFO_V1(gbt_bpchar_consistent);
19 5 : PG_FUNCTION_INFO_V1(gbt_text_penalty);
20 5 : PG_FUNCTION_INFO_V1(gbt_text_same);
21 :
22 :
23 : /* define for comparison */
24 :
25 : static bool
2210 andrew 26 1195 : gbt_textgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
27 : {
4380 tgl 28 1195 : return DatumGetBool(DirectFunctionCall2Coll(text_gt,
29 : collation,
30 : PointerGetDatum(a),
31 : PointerGetDatum(b)));
32 : }
33 :
34 : static bool
2210 andrew 35 1202 : gbt_textge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
36 : {
4380 tgl 37 1202 : return DatumGetBool(DirectFunctionCall2Coll(text_ge,
38 : collation,
39 : PointerGetDatum(a),
40 : PointerGetDatum(b)));
41 : }
42 :
43 : static bool
2210 andrew 44 291 : gbt_texteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
45 : {
4380 tgl 46 291 : return DatumGetBool(DirectFunctionCall2Coll(texteq,
47 : collation,
48 : PointerGetDatum(a),
49 : PointerGetDatum(b)));
50 : }
51 :
52 : static bool
2210 andrew 53 1061 : gbt_textle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
54 : {
4380 tgl 55 1061 : return DatumGetBool(DirectFunctionCall2Coll(text_le,
56 : collation,
57 : PointerGetDatum(a),
58 : PointerGetDatum(b)));
59 : }
60 :
61 : static bool
2210 andrew 62 991 : gbt_textlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
63 : {
4380 tgl 64 991 : return DatumGetBool(DirectFunctionCall2Coll(text_lt,
65 : collation,
66 : PointerGetDatum(a),
67 : PointerGetDatum(b)));
68 : }
69 :
70 : static int32
2210 andrew 71 61673 : gbt_textcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
72 : {
4380 tgl 73 61673 : 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
456 96 595 : gbt_bpchargt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
97 : {
98 595 : return DatumGetBool(DirectFunctionCall2Coll(bpchargt,
99 : collation,
100 : PointerGetDatum(a),
101 : PointerGetDatum(b)));
102 : }
103 :
104 : static bool
105 603 : gbt_bpcharge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
106 : {
107 603 : return DatumGetBool(DirectFunctionCall2Coll(bpcharge,
108 : collation,
109 : PointerGetDatum(a),
110 : PointerGetDatum(b)));
111 : }
112 :
113 : static bool
114 71 : gbt_bpchareq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
115 : {
116 71 : return DatumGetBool(DirectFunctionCall2Coll(bpchareq,
117 : collation,
118 : PointerGetDatum(a),
119 : PointerGetDatum(b)));
120 : }
121 :
122 : static bool
123 535 : gbt_bpcharle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
124 : {
125 535 : return DatumGetBool(DirectFunctionCall2Coll(bpcharle,
126 : collation,
127 : PointerGetDatum(a),
128 : PointerGetDatum(b)));
129 : }
130 :
131 : static bool
132 464 : gbt_bpcharlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
133 : {
134 464 : return DatumGetBool(DirectFunctionCall2Coll(bpcharlt,
135 : collation,
136 : PointerGetDatum(a),
137 : PointerGetDatum(b)));
138 : }
139 :
140 : static int32
141 71 : gbt_bpcharcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
142 : {
143 71 : 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
6797 bruce 170 3019 : gbt_text_compress(PG_FUNCTION_ARGS)
171 : {
172 3019 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
173 :
6385 174 3019 : if (tinfo.eml == 0)
175 : {
6491 teodor 176 4 : tinfo.eml = pg_database_encoding_max_length();
177 : }
178 :
6797 bruce 179 3019 : PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
180 : }
181 :
182 : Datum
183 1010 : gbt_bpchar_compress(PG_FUNCTION_ARGS)
184 : {
185 : /* This should never have been distinct from gbt_text_compress */
456 tgl 186 1010 : return gbt_text_compress(fcinfo);
187 : }
188 :
189 :
190 :
191 : Datum
6890 teodor 192 4811 : gbt_text_consistent(PG_FUNCTION_ARGS)
193 : {
6797 bruce 194 4811 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
195 4811 : void *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
196 4811 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
197 :
198 : /* Oid subtype = PG_GETARG_OID(3); */
5473 tgl 199 4811 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
200 : bool retval;
201 4811 : GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
6797 bruce 202 4811 : GBT_VARKEY_R r = gbt_var_key_readable(key);
203 :
204 : /* All cases served by this function are exact */
5473 tgl 205 4811 : *recheck = false;
206 :
6385 bruce 207 4811 : if (tinfo.eml == 0)
208 : {
6491 teodor 209 UBC 0 : tinfo.eml = pg_database_encoding_max_length();
210 : }
211 :
4370 tgl 212 CBC 9622 : retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
2210 andrew 213 4811 : GIST_LEAF(entry), &tinfo, fcinfo->flinfo);
214 :
6797 bruce 215 4811 : PG_RETURN_BOOL(retval);
216 : }
217 :
218 :
219 : Datum
6890 teodor 220 2332 : gbt_bpchar_consistent(PG_FUNCTION_ARGS)
221 : {
6797 bruce 222 2332 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
2029 tgl 223 2332 : void *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
6797 bruce 224 2332 : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
225 :
226 : /* Oid subtype = PG_GETARG_OID(3); */
5473 tgl 227 2332 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
228 : bool retval;
229 2332 : GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
6797 bruce 230 2332 : GBT_VARKEY_R r = gbt_var_key_readable(key);
231 :
232 : /* All cases served by this function are exact */
5473 tgl 233 2332 : *recheck = false;
234 :
456 235 2332 : if (bptinfo.eml == 0)
236 : {
237 1 : bptinfo.eml = pg_database_encoding_max_length();
238 : }
239 :
240 4664 : retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
241 2332 : GIST_LEAF(entry), &bptinfo, fcinfo->flinfo);
6797 bruce 242 2332 : PG_RETURN_BOOL(retval);
243 : }
244 :
245 :
246 : Datum
6890 teodor 247 2492 : gbt_text_union(PG_FUNCTION_ARGS)
248 : {
6797 bruce 249 2492 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
250 2492 : int32 *size = (int *) PG_GETARG_POINTER(1);
251 :
4370 tgl 252 2492 : PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
253 : &tinfo, fcinfo->flinfo));
254 : }
255 :
256 :
257 : Datum
6890 teodor 258 19 : gbt_text_picksplit(PG_FUNCTION_ARGS)
259 : {
6797 bruce 260 19 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
261 19 : GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
262 :
4370 tgl 263 19 : gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
264 : &tinfo, fcinfo->flinfo);
6797 bruce 265 19 : PG_RETURN_POINTER(v);
266 : }
267 :
268 : Datum
6890 teodor 269 2489 : gbt_text_same(PG_FUNCTION_ARGS)
270 : {
6797 bruce 271 2489 : Datum d1 = PG_GETARG_DATUM(0);
272 2489 : Datum d2 = PG_GETARG_DATUM(1);
273 2489 : bool *result = (bool *) PG_GETARG_POINTER(2);
274 :
2210 andrew 275 2489 : *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
4370 tgl 276 2489 : PG_RETURN_POINTER(result);
277 : }
278 :
279 :
280 : Datum
6890 teodor 281 9492 : gbt_text_penalty(PG_FUNCTION_ARGS)
282 : {
6797 bruce 283 9492 : GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
284 9492 : GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
6541 neilc 285 9492 : float *result = (float *) PG_GETARG_POINTER(2);
286 :
4370 tgl 287 9492 : PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
288 : &tinfo, fcinfo->flinfo));
289 : }
|