Debian Codegraph @

contrib/ltree/lquery_op.c

raw
1 /*
2 * op function for ltree and lquery
3 * Teodor Sigaev <[email protected]>
4 * contrib/ltree/lquery_op.c
5 */
6 #include "postgres.h"
7
8 #include <ctype.h>
9
10 #include "catalog/pg_collation.h"
11 #include "ltree.h"
12 #include "miscadmin.h"
13 #include "utils/array.h"
14 #include "utils/formatting.h"
15
16 PG_FUNCTION_INFO_V1(ltq_regex);
17 PG_FUNCTION_INFO_V1(ltq_rregex);
18
19 PG_FUNCTION_INFO_V1(lt_q_regex);
20 PG_FUNCTION_INFO_V1(lt_q_rregex);
21
22 #define NEXTVAL(x) ( (lquery*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) )
23
24 static char *
25 getlexeme(char *start, char *end, int *len)
26 {
27 char *ptr;
28
29 while (start < end && t_iseq(start, '_'))
30 start += pg_mblen_range(start, end);
31
32 ptr = start;
33 if (ptr >= end)
34 return NULL;
35
36 while (ptr < end && !t_iseq(ptr, '_'))
37 ptr += pg_mblen_range(ptr, end);
38
39 *len = ptr - start;
40 return start;
41 }
42
43 bool
44 compare_subnode(ltree_level *t, char *qn, int len, bool prefix, bool ci)
45 {
46 char *endt = t->name + t->len;
47 char *endq = qn + len;
48 char *tn;
49 int lent,
50 lenq;
51 bool isok;
52
53 while ((qn = getlexeme(qn, endq, &lenq)) != NULL)
54 {
55 tn = t->name;
56 isok = false;
57 while ((tn = getlexeme(tn, endt, &lent)) != NULL)
58 {
59 if (ltree_label_match(qn, lenq, tn, lent, prefix, ci))
60 {
61 isok = true;
62 break;
63 }
64 tn += lent;
65 }
66
67 if (!isok)
68 return false;
69 qn += lenq;
70 }
71
72 return true;
73 }
74
75 /*
76 * Check if the label matches the predicate string. If 'prefix' is true, then
77 * the predicate string is treated as a prefix. If 'ci' is true, then the
78 * predicate string is case-insensitive (and locale-aware).
79 */
80 bool
81 ltree_label_match(const char *pred, size_t pred_len, const char *label,
82 size_t label_len, bool prefix, bool ci)
83 {
84 static pg_locale_t locale = NULL;
85 char *fpred; /* casefolded predicate */
86 size_t fpred_len = pred_len;
87 char *flabel; /* casefolded label */
88 size_t flabel_len = label_len;
89 size_t len;
90 bool res;
91
92 /* fast path for binary match or binary prefix match */
93 if ((pred_len == label_len || (prefix && pred_len < label_len)) &&
94 strncmp(pred, label, pred_len) == 0)
95 return true;
96 else if (!ci)
97 return false;
98
99 if (!locale)
100 locale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID);
101
102 if (locale->ctype_is_c)
103 {
104 if (pred_len > label_len || (!prefix && pred_len != label_len))
105 return false;
106
107 for (int i = 0; i < pred_len; i++)
108 {
109 if (pg_ascii_tolower(pred[i]) != pg_ascii_tolower(label[i]))
110 return false;
111 }
112
113 return true;
114 }
115
116 /*
117 * Slow path for case-insensitive comparison: case fold and then compare.
118 * This path is necessary even if pred_len > label_len, because the byte
119 * lengths may change after casefolding.
120 */
121
122 fpred = palloc(fpred_len + 1);
123 len = pg_strfold(fpred, fpred_len + 1, pred, pred_len, locale);
124 if (len > fpred_len)
125 {
126 /* grow buffer if needed and retry */
127 fpred_len = len;
128 fpred = repalloc(fpred, fpred_len + 1);
129 len = pg_strfold(fpred, fpred_len + 1, pred, pred_len, locale);
130 }
131 Assert(len <= fpred_len);
132 fpred_len = len;
133
134 flabel = palloc(flabel_len + 1);
135 len = pg_strfold(flabel, flabel_len + 1, label, label_len, locale);
136 if (len > flabel_len)
137 {
138 /* grow buffer if needed and retry */
139 flabel_len = len;
140 flabel = repalloc(flabel, flabel_len + 1);
141 len = pg_strfold(flabel, flabel_len + 1, label, label_len, locale);
142 }
143 Assert(len <= flabel_len);
144 flabel_len = len;
145
146 if ((fpred_len == flabel_len || (prefix && fpred_len < flabel_len)) &&
147 strncmp(fpred, flabel, fpred_len) == 0)
148 res = true;
149 else
150 res = false;
151
152 pfree(fpred);
153 pfree(flabel);
154
155 return res;
156 }
157
158 /*
159 * See if an lquery_level matches an ltree_level
160 *
161 * This accounts for all flags including LQL_NOT, but does not
162 * consider repetition counts.
163 */
164 static bool
165 checkLevel(lquery_level *curq, ltree_level *curt)
166 {
167 lquery_variant *curvar = LQL_FIRST(curq);
168 bool success;
169
170 success = (curq->flag & LQL_NOT) ? false : true;
171
172 /* numvar == 0 means '*' which matches anything */
173 if (curq->numvar == 0)
174 return success;
175
176 for (int i = 0; i < curq->numvar; i++)
177 {
178 bool prefix = (curvar->flag & LVAR_ANYEND);
179 bool ci = (curvar->flag & LVAR_INCASE);
180
181 if (curvar->flag & LVAR_SUBLEXEME)
182 {
183 if (compare_subnode(curt, curvar->name, curvar->len, prefix, ci))
184 return success;
185 }
186 else if (ltree_label_match(curvar->name, curvar->len, curt->name,
187 curt->len, prefix, ci))
188 return success;
189
190 curvar = LVAR_NEXT(curvar);
191 }
192 return !success;
193 }
194
195 /*
196 * Try to match an lquery (of qlen items) to an ltree (of tlen items)
197 */
198 static bool
199 checkCond(lquery_level *curq, int qlen,
200 ltree_level *curt, int tlen)
201 {
202 /* Since this function recurses, it could be driven to stack overflow */
203 check_stack_depth();
204
205 /* Pathological patterns could take awhile, too */
206 CHECK_FOR_INTERRUPTS();
207
208 /* Loop while we have query items to consider */
209 while (qlen > 0)
210 {
211 int low,
212 high;
213 lquery_level *nextq;
214
215 /*
216 * Get min and max repetition counts for this query item, dealing with
217 * the backwards-compatibility hack that the low/high fields aren't
218 * meaningful for non-'*' items unless LQL_COUNT is set.
219 */
220 if ((curq->flag & LQL_COUNT) || curq->numvar == 0)
221 low = curq->low, high = curq->high;
222 else
223 low = high = 1;
224
225 /*
226 * We may limit "high" to the remaining text length; this avoids
227 * separate tests below.
228 */
229 if (high > tlen)
230 high = tlen;
231
232 /* Fail if a match of required number of items is impossible */
233 if (high < low)
234 return false;
235
236 /*
237 * Recursively check the rest of the pattern against each possible
238 * start point following some of this item's match(es).
239 */
240 nextq = LQL_NEXT(curq);
241 qlen--;
242
243 for (int matchcnt = 0; matchcnt < high; matchcnt++)
244 {
245 /*
246 * If we've consumed an acceptable number of matches of this item,
247 * and the rest of the pattern matches beginning here, we're good.
248 */
249 if (matchcnt >= low && checkCond(nextq, qlen, curt, tlen))
250 return true;
251
252 /*
253 * Otherwise, try to match one more text item to this query item.
254 */
255 if (!checkLevel(curq, curt))
256 return false;
257
258 curt = LEVEL_NEXT(curt);
259 tlen--;
260 }
261
262 /*
263 * Once we've consumed "high" matches, we can succeed only if the rest
264 * of the pattern matches beginning here. Loop around (if you prefer,
265 * think of this as tail recursion).
266 */
267 curq = nextq;
268 }
269
270 /*
271 * Once we're out of query items, we match only if there's no remaining
272 * text either.
273 */
274 return (tlen == 0);
275 }
276
277 Datum
278 ltq_regex(PG_FUNCTION_ARGS)
279 {
280 ltree *tree = PG_GETARG_LTREE_P(0);
281 lquery *query = PG_GETARG_LQUERY_P(1);
282 bool res;
283
284 res = checkCond(LQUERY_FIRST(query), query->numlevel,
285 LTREE_FIRST(tree), tree->numlevel);
286
287 PG_FREE_IF_COPY(tree, 0);
288 PG_FREE_IF_COPY(query, 1);
289 PG_RETURN_BOOL(res);
290 }
291
292 Datum
293 ltq_rregex(PG_FUNCTION_ARGS)
294 {
295 PG_RETURN_DATUM(DirectFunctionCall2(ltq_regex,
296 PG_GETARG_DATUM(1),
297 PG_GETARG_DATUM(0)
298 ));
299 }
300
301 Datum
302 lt_q_regex(PG_FUNCTION_ARGS)
303 {
304 ltree *tree = PG_GETARG_LTREE_P(0);
305 ArrayType *_query = PG_GETARG_ARRAYTYPE_P(1);
306 lquery *query = (lquery *) ARR_DATA_PTR(_query);
307 bool res = false;
308 int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
309
310 if (ARR_NDIM(_query) > 1)
311 ereport(ERROR,
312 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
313 errmsg("array must be one-dimensional")));
314 if (array_contains_nulls(_query))
315 ereport(ERROR,
316 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
317 errmsg("array must not contain nulls")));
318
319 while (num > 0)
320 {
321 if (DatumGetBool(DirectFunctionCall2(ltq_regex,
322 PointerGetDatum(tree), PointerGetDatum(query))))
323 {
324
325 res = true;
326 break;
327 }
328 num--;
329 query = NEXTVAL(query);
330 }
331
332 PG_FREE_IF_COPY(tree, 0);
333 PG_FREE_IF_COPY(_query, 1);
334 PG_RETURN_BOOL(res);
335 }
336
337 Datum
338 lt_q_rregex(PG_FUNCTION_ARGS)
339 {
340 PG_RETURN_DATUM(DirectFunctionCall2(lt_q_regex,
341 PG_GETARG_DATUM(1),
342 PG_GETARG_DATUM(0)
343 ));
344 }