Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * tsvector_op.c
4 : : * operations over tsvector
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : *
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/utils/adt/tsvector_op.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : : #include "postgres.h"
15 : :
16 : : #include <limits.h>
17 : :
18 : : #include "access/htup_details.h"
19 : : #include "catalog/namespace.h"
20 : : #include "catalog/pg_type.h"
21 : : #include "commands/trigger.h"
22 : : #include "common/int.h"
23 : : #include "executor/spi.h"
24 : : #include "funcapi.h"
25 : : #include "lib/qunique.h"
26 : : #include "mb/pg_wchar.h"
27 : : #include "miscadmin.h"
28 : : #include "parser/parse_coerce.h"
29 : : #include "tsearch/ts_utils.h"
30 : : #include "utils/array.h"
31 : : #include "utils/builtins.h"
32 : : #include "utils/regproc.h"
33 : : #include "utils/rel.h"
34 : :
35 : :
36 : : typedef struct
37 : : {
38 : : WordEntry *arrb;
39 : : WordEntry *arre;
40 : : char *values;
41 : : char *operand;
42 : : } CHKVAL;
43 : :
44 : :
45 : : typedef struct StatEntry
46 : : {
47 : : uint32 ndoc; /* zero indicates that we were already here
48 : : * while walking through the tree */
49 : : uint32 nentry;
50 : : struct StatEntry *left;
51 : : struct StatEntry *right;
52 : : uint32 lenlexeme;
53 : : char lexeme[FLEXIBLE_ARRAY_MEMBER];
54 : : } StatEntry;
55 : :
56 : : #define STATENTRYHDRSZ (offsetof(StatEntry, lexeme))
57 : :
58 : : typedef struct
59 : : {
60 : : int32 weight;
61 : :
62 : : uint32 maxdepth;
63 : :
64 : : StatEntry **stack;
65 : : uint32 stackpos;
66 : :
67 : : StatEntry *root;
68 : : } TSVectorStat;
69 : :
70 : :
71 : : static TSTernaryValue TS_execute_recurse(QueryItem *curitem, void *arg,
72 : : uint32 flags,
73 : : TSExecuteCallback chkcond);
74 : : static bool TS_execute_locations_recurse(QueryItem *curitem,
75 : : void *arg,
76 : : TSExecuteCallback chkcond,
77 : : List **locations);
78 : : static int tsvector_bsearch(const TSVectorData *tsv, char *lexeme, int lexeme_len);
79 : : static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column);
80 : :
81 : :
82 : : /*
83 : : * Order: haspos, len, word, for all positions (pos, weight)
84 : : */
85 : : static int
187 peter@eisentraut.org 86 :GNC 261 : silly_cmp_tsvector(const TSVectorData *a, const TSVectorData *b)
87 : : {
6832 tgl@sss.pgh.pa.us 88 [ - + ]:CBC 261 : if (VARSIZE(a) < VARSIZE(b))
6832 tgl@sss.pgh.pa.us 89 :UBC 0 : return -1;
6832 tgl@sss.pgh.pa.us 90 [ - + ]:CBC 261 : else if (VARSIZE(a) > VARSIZE(b))
6832 tgl@sss.pgh.pa.us 91 :UBC 0 : return 1;
6832 tgl@sss.pgh.pa.us 92 [ - + ]:CBC 261 : else if (a->size < b->size)
6832 tgl@sss.pgh.pa.us 93 :UBC 0 : return -1;
6832 tgl@sss.pgh.pa.us 94 [ - + ]:CBC 261 : else if (a->size > b->size)
6832 tgl@sss.pgh.pa.us 95 :UBC 0 : return 1;
96 : : else
97 : : {
187 peter@eisentraut.org 98 :GNC 261 : const WordEntry *aptr = ARRPTR(a);
99 : 261 : const WordEntry *bptr = ARRPTR(b);
6832 tgl@sss.pgh.pa.us 100 :CBC 261 : int i = 0;
101 : : int res;
102 : :
103 : :
104 [ + + ]: 296 : for (i = 0; i < a->size; i++)
105 : : {
106 [ - + ]: 263 : if (aptr->haspos != bptr->haspos)
107 : : {
6832 tgl@sss.pgh.pa.us 108 [ # # ]:UBC 0 : return (aptr->haspos > bptr->haspos) ? -1 : 1;
109 : : }
6172 bruce@momjian.us 110 [ + + ]:CBC 263 : else if ((res = tsCompareString(STRPTR(a) + aptr->pos, aptr->len, STRPTR(b) + bptr->pos, bptr->len, false)) != 0)
111 : : {
6832 tgl@sss.pgh.pa.us 112 :GBC 228 : return res;
113 : : }
6832 tgl@sss.pgh.pa.us 114 [ + + ]:CBC 35 : else if (aptr->haspos)
115 : : {
6832 tgl@sss.pgh.pa.us 116 :GBC 32 : WordEntryPos *ap = POSDATAPTR(a, aptr);
117 : 32 : WordEntryPos *bp = POSDATAPTR(b, bptr);
118 : : int j;
119 : :
120 [ + - + - : 32 : if (POSDATALEN(a, aptr) != POSDATALEN(b, bptr))
- + ]
6832 tgl@sss.pgh.pa.us 121 [ # # # # :UBC 0 : return (POSDATALEN(a, aptr) > POSDATALEN(b, bptr)) ? -1 : 1;
# # ]
122 : :
6832 tgl@sss.pgh.pa.us 123 [ + - + + ]:GBC 64 : for (j = 0; j < POSDATALEN(a, aptr); j++)
124 : : {
125 [ - + ]: 32 : if (WEP_GETPOS(*ap) != WEP_GETPOS(*bp))
126 : : {
6832 tgl@sss.pgh.pa.us 127 [ # # ]:UBC 0 : return (WEP_GETPOS(*ap) > WEP_GETPOS(*bp)) ? -1 : 1;
128 : : }
6832 tgl@sss.pgh.pa.us 129 [ - + ]:GBC 32 : else if (WEP_GETWEIGHT(*ap) != WEP_GETWEIGHT(*bp))
130 : : {
6832 tgl@sss.pgh.pa.us 131 [ # # ]:UBC 0 : return (WEP_GETWEIGHT(*ap) > WEP_GETWEIGHT(*bp)) ? -1 : 1;
132 : : }
6832 tgl@sss.pgh.pa.us 133 :GBC 32 : ap++, bp++;
134 : : }
135 : : }
136 : :
6832 tgl@sss.pgh.pa.us 137 :CBC 35 : aptr++;
138 : 35 : bptr++;
139 : : }
140 : : }
141 : :
142 : 33 : return 0;
143 : : }
144 : :
145 : : #define TSVECTORCMPFUNC( type, action, ret ) \
146 : : Datum \
147 : : tsvector_##type(PG_FUNCTION_ARGS) \
148 : : { \
149 : : TSVector a = PG_GETARG_TSVECTOR(0); \
150 : : TSVector b = PG_GETARG_TSVECTOR(1); \
151 : : int res = silly_cmp_tsvector(a, b); \
152 : : PG_FREE_IF_COPY(a,0); \
153 : : PG_FREE_IF_COPY(b,1); \
154 : : PG_RETURN_##ret( res action 0 ); \
155 : : } \
156 : : /* keep compiler quiet - no extra ; */ \
157 : : extern int no_such_variable
158 : :
6832 tgl@sss.pgh.pa.us 159 [ # # # # ]:UBC 0 : TSVECTORCMPFUNC(lt, <, BOOL);
160 [ # # # # ]: 0 : TSVECTORCMPFUNC(le, <=, BOOL);
6832 tgl@sss.pgh.pa.us 161 [ - + - + ]:CBC 1 : TSVECTORCMPFUNC(eq, ==, BOOL);
6832 tgl@sss.pgh.pa.us 162 [ # # # # ]:UBC 0 : TSVECTORCMPFUNC(ge, >=, BOOL);
163 [ # # # # ]: 0 : TSVECTORCMPFUNC(gt, >, BOOL);
164 [ # # # # ]: 0 : TSVECTORCMPFUNC(ne, !=, BOOL);
6832 tgl@sss.pgh.pa.us 165 [ - + - + ]:GBC 260 : TSVECTORCMPFUNC(cmp, +, INT32);
166 : :
167 : : Datum
6832 tgl@sss.pgh.pa.us 168 :CBC 73 : tsvector_strip(PG_FUNCTION_ARGS)
169 : : {
170 : 73 : TSVector in = PG_GETARG_TSVECTOR(0);
171 : : TSVector out;
172 : : int i,
173 : 73 : len = 0;
174 : 73 : WordEntry *arrin = ARRPTR(in),
175 : : *arrout;
176 : : char *cur;
177 : :
178 [ + + ]: 261 : for (i = 0; i < in->size; i++)
6815 teodor@sigaev.ru 179 : 188 : len += arrin[i].len;
180 : :
6832 tgl@sss.pgh.pa.us 181 : 73 : len = CALCDATASIZE(in->size, len);
182 : 73 : out = (TSVector) palloc0(len);
183 : 73 : SET_VARSIZE(out, len);
184 : 73 : out->size = in->size;
185 : 73 : arrout = ARRPTR(out);
186 : 73 : cur = STRPTR(out);
187 [ + + ]: 261 : for (i = 0; i < in->size; i++)
188 : : {
189 : 188 : memcpy(cur, STRPTR(in) + arrin[i].pos, arrin[i].len);
190 : 188 : arrout[i].haspos = 0;
191 : 188 : arrout[i].len = arrin[i].len;
192 : 188 : arrout[i].pos = cur - STRPTR(out);
6815 teodor@sigaev.ru 193 : 188 : cur += arrout[i].len;
194 : : }
195 : :
6832 tgl@sss.pgh.pa.us 196 [ - + ]: 73 : PG_FREE_IF_COPY(in, 0);
197 : 73 : PG_RETURN_POINTER(out);
198 : : }
199 : :
200 : : Datum
201 : 7 : tsvector_length(PG_FUNCTION_ARGS)
202 : : {
203 : 7 : TSVector in = PG_GETARG_TSVECTOR(0);
5062 peter_e@gmx.net 204 : 7 : int32 ret = in->size;
205 : :
6832 tgl@sss.pgh.pa.us 206 [ - + ]: 7 : PG_FREE_IF_COPY(in, 0);
207 : 7 : PG_RETURN_INT32(ret);
208 : : }
209 : :
210 : : Datum
211 : 10 : tsvector_setweight(PG_FUNCTION_ARGS)
212 : : {
213 : 10 : TSVector in = PG_GETARG_TSVECTOR(0);
214 : 10 : char cw = PG_GETARG_CHAR(1);
215 : : TSVector out;
216 : : int i,
217 : : j;
218 : : WordEntry *entry;
219 : : WordEntryPos *p;
220 : 10 : int w = 0;
221 : :
222 [ - - + - : 10 : switch (cw)
- ]
223 : : {
6832 tgl@sss.pgh.pa.us 224 :UBC 0 : case 'A':
225 : : case 'a':
226 : 0 : w = 3;
227 : 0 : break;
228 : 0 : case 'B':
229 : : case 'b':
230 : 0 : w = 2;
231 : 0 : break;
6832 tgl@sss.pgh.pa.us 232 :CBC 10 : case 'C':
233 : : case 'c':
234 : 10 : w = 1;
235 : 10 : break;
6832 tgl@sss.pgh.pa.us 236 :UBC 0 : case 'D':
237 : : case 'd':
238 : 0 : w = 0;
239 : 0 : break;
240 : 0 : default:
241 : : /* internal error */
6733 242 [ # # ]: 0 : elog(ERROR, "unrecognized weight: %d", cw);
243 : : }
244 : :
6832 tgl@sss.pgh.pa.us 245 :CBC 10 : out = (TSVector) palloc(VARSIZE(in));
246 : 10 : memcpy(out, in, VARSIZE(in));
247 : 10 : entry = ARRPTR(out);
248 : 10 : i = out->size;
249 [ + + ]: 50 : while (i--)
250 : : {
251 [ + - + - ]: 40 : if ((j = POSDATALEN(out, entry)) != 0)
252 : : {
253 : 40 : p = POSDATAPTR(out, entry);
254 [ + + ]: 140 : while (j--)
255 : : {
256 : 100 : WEP_SETWEIGHT(*p, w);
257 : 100 : p++;
258 : : }
259 : : }
260 : 40 : entry++;
261 : : }
262 : :
263 [ - + ]: 10 : PG_FREE_IF_COPY(in, 0);
264 : 10 : PG_RETURN_POINTER(out);
265 : : }
266 : :
267 : : /*
268 : : * setweight(tsin tsvector, char_weight "char", lexemes "text"[])
269 : : *
270 : : * Assign weight w to elements of tsin that are listed in lexemes.
271 : : */
272 : : Datum
3707 teodor@sigaev.ru 273 : 20 : tsvector_setweight_by_filter(PG_FUNCTION_ARGS)
274 : : {
275 : 20 : TSVector tsin = PG_GETARG_TSVECTOR(0);
276 : 20 : char char_weight = PG_GETARG_CHAR(1);
277 : 20 : ArrayType *lexemes = PG_GETARG_ARRAYTYPE_P(2);
278 : :
279 : : TSVector tsout;
280 : : int i,
281 : : j,
282 : : nlexemes,
283 : : weight;
284 : : WordEntry *entry;
285 : : Datum *dlexemes;
286 : : bool *nulls;
287 : :
288 [ - - + - : 20 : switch (char_weight)
- ]
289 : : {
3617 rhaas@postgresql.org 290 :UBC 0 : case 'A':
291 : : case 'a':
3707 teodor@sigaev.ru 292 : 0 : weight = 3;
293 : 0 : break;
3617 rhaas@postgresql.org 294 : 0 : case 'B':
295 : : case 'b':
3707 teodor@sigaev.ru 296 : 0 : weight = 2;
297 : 0 : break;
3617 rhaas@postgresql.org 298 :CBC 20 : case 'C':
299 : : case 'c':
3707 teodor@sigaev.ru 300 : 20 : weight = 1;
301 : 20 : break;
3617 rhaas@postgresql.org 302 :UBC 0 : case 'D':
303 : : case 'd':
3707 teodor@sigaev.ru 304 : 0 : weight = 0;
305 : 0 : break;
306 : 0 : default:
307 : : /* internal error */
308 [ # # ]: 0 : elog(ERROR, "unrecognized weight: %c", char_weight);
309 : : }
310 : :
3707 teodor@sigaev.ru 311 :CBC 20 : tsout = (TSVector) palloc(VARSIZE(tsin));
312 : 20 : memcpy(tsout, tsin, VARSIZE(tsin));
313 : 20 : entry = ARRPTR(tsout);
314 : :
1404 peter@eisentraut.org 315 : 20 : deconstruct_array_builtin(lexemes, TEXTOID, &dlexemes, &nulls, &nlexemes);
316 : :
317 : : /*
318 : : * Assuming that lexemes array is significantly shorter than tsvector we
319 : : * can iterate through lexemes performing binary search of each lexeme
320 : : * from lexemes in tsvector.
321 : : */
3707 teodor@sigaev.ru 322 [ + + ]: 60 : for (i = 0; i < nlexemes; i++)
323 : : {
324 : : char *lex;
325 : : int lex_len,
326 : : lex_pos;
327 : :
328 : : /* Ignore null array elements, they surely don't match */
329 [ + + ]: 40 : if (nulls[i])
1641 tgl@sss.pgh.pa.us 330 : 5 : continue;
331 : :
273 peter@eisentraut.org 332 :GNC 35 : lex = VARDATA(DatumGetPointer(dlexemes[i]));
333 : 35 : lex_len = VARSIZE(DatumGetPointer(dlexemes[i])) - VARHDRSZ;
3707 teodor@sigaev.ru 334 :CBC 35 : lex_pos = tsvector_bsearch(tsout, lex, lex_len);
335 : :
336 [ + + + + : 35 : if (lex_pos >= 0 && (j = POSDATALEN(tsout, entry + lex_pos)) != 0)
+ + ]
337 : : {
338 : 20 : WordEntryPos *p = POSDATAPTR(tsout, entry + lex_pos);
339 : :
340 [ + + ]: 65 : while (j--)
341 : : {
342 : 45 : WEP_SETWEIGHT(*p, weight);
343 : 45 : p++;
344 : : }
345 : : }
346 : : }
347 : :
348 [ - + ]: 20 : PG_FREE_IF_COPY(tsin, 0);
349 [ - + ]: 20 : PG_FREE_IF_COPY(lexemes, 2);
350 : :
351 : 20 : PG_RETURN_POINTER(tsout);
352 : : }
353 : :
354 : : #define compareEntry(pa, a, pb, b) \
355 : : tsCompareString((pa) + (a)->pos, (a)->len, \
356 : : (pb) + (b)->pos, (b)->len, \
357 : : false)
358 : :
359 : : /*
360 : : * Add positions from src to dest after offsetting them by maxpos.
361 : : * Return the number added (might be less than expected due to overflow)
362 : : */
363 : : static int32
6746 bruce@momjian.us 364 : 10 : add_pos(TSVector src, WordEntry *srcptr,
365 : : TSVector dest, WordEntry *destptr,
366 : : int32 maxpos)
367 : : {
6811 teodor@sigaev.ru 368 : 10 : uint16 *clen = &_POSVECPTR(dest, destptr)->npos;
369 : : int i;
6832 tgl@sss.pgh.pa.us 370 [ + - ]: 10 : uint16 slen = POSDATALEN(src, srcptr),
371 : : startlen;
372 : 10 : WordEntryPos *spos = POSDATAPTR(src, srcptr),
373 : 10 : *dpos = POSDATAPTR(dest, destptr);
374 : :
375 [ - + ]: 10 : if (!destptr->haspos)
6832 tgl@sss.pgh.pa.us 376 :UBC 0 : *clen = 0;
377 : :
6832 tgl@sss.pgh.pa.us 378 :CBC 10 : startlen = *clen;
6769 379 : 10 : for (i = 0;
380 [ + + + - ]: 20 : i < slen && *clen < MAXNUMPOS &&
6746 bruce@momjian.us 381 [ + + + - ]: 10 : (*clen == 0 || WEP_GETPOS(dpos[*clen - 1]) != MAXENTRYPOS - 1);
6769 tgl@sss.pgh.pa.us 382 : 10 : i++)
383 : : {
6832 384 : 10 : WEP_SETWEIGHT(dpos[*clen], WEP_GETWEIGHT(spos[i]));
385 : 10 : WEP_SETPOS(dpos[*clen], LIMITPOS(WEP_GETPOS(spos[i]) + maxpos));
386 : 10 : (*clen)++;
387 : : }
388 : :
389 [ + - ]: 10 : if (*clen != startlen)
390 : 10 : destptr->haspos = 1;
391 : 10 : return *clen - startlen;
392 : : }
393 : :
394 : : /*
395 : : * Perform binary search of given lexeme in TSVector.
396 : : * Returns lexeme position in TSVector's entry array or -1 if lexeme wasn't
397 : : * found.
398 : : */
399 : : static int
187 peter@eisentraut.org 400 :GNC 165 : tsvector_bsearch(const TSVectorData *tsv, char *lexeme, int lexeme_len)
401 : : {
402 : 165 : const WordEntry *arrin = ARRPTR(tsv);
3707 teodor@sigaev.ru 403 :CBC 165 : int StopLow = 0,
404 : 165 : StopHigh = tsv->size,
405 : : StopMiddle,
406 : : cmp;
407 : :
408 [ + + ]: 435 : while (StopLow < StopHigh)
409 : : {
3617 rhaas@postgresql.org 410 : 385 : StopMiddle = (StopLow + StopHigh) / 2;
411 : :
3707 teodor@sigaev.ru 412 : 385 : cmp = tsCompareString(lexeme, lexeme_len,
3617 rhaas@postgresql.org 413 : 385 : STRPTR(tsv) + arrin[StopMiddle].pos,
414 : 385 : arrin[StopMiddle].len,
415 : : false);
416 : :
3707 teodor@sigaev.ru 417 [ + + ]: 385 : if (cmp < 0)
418 : 180 : StopHigh = StopMiddle;
419 [ + + ]: 205 : else if (cmp > 0)
420 : 90 : StopLow = StopMiddle + 1;
421 : : else /* found it */
422 : 115 : return StopMiddle;
423 : : }
424 : :
425 : 50 : return -1;
426 : : }
427 : :
428 : : /*
429 : : * qsort comparator functions
430 : : */
431 : :
432 : : static int
3560 tgl@sss.pgh.pa.us 433 : 65 : compare_int(const void *va, const void *vb)
434 : : {
435 : 65 : int a = *((const int *) va);
436 : 65 : int b = *((const int *) vb);
437 : :
809 nathan@postgresql.or 438 : 65 : return pg_cmp_s32(a, b);
439 : : }
440 : :
441 : : static int
3560 tgl@sss.pgh.pa.us 442 : 85 : compare_text_lexemes(const void *va, const void *vb)
443 : : {
444 : 85 : Datum a = *((const Datum *) va);
445 : 85 : Datum b = *((const Datum *) vb);
273 peter@eisentraut.org 446 :GNC 85 : char *alex = VARDATA_ANY(DatumGetPointer(a));
447 : 85 : int alex_len = VARSIZE_ANY_EXHDR(DatumGetPointer(a));
448 : 85 : char *blex = VARDATA_ANY(DatumGetPointer(b));
449 : 85 : int blex_len = VARSIZE_ANY_EXHDR(DatumGetPointer(b));
450 : :
3560 tgl@sss.pgh.pa.us 451 :CBC 85 : return tsCompareString(alex, alex_len, blex, blex_len, false);
452 : : }
453 : :
454 : : /*
455 : : * Internal routine to delete lexemes from TSVector by array of offsets.
456 : : *
457 : : * int *indices_to_delete -- array of lexeme offsets to delete (modified here!)
458 : : * int indices_count -- size of that array
459 : : *
460 : : * Returns new TSVector without given lexemes along with their positions
461 : : * and weights.
462 : : */
463 : : static TSVector
3707 teodor@sigaev.ru 464 : 55 : tsvector_delete_by_indices(TSVector tsv, int *indices_to_delete,
465 : : int indices_count)
466 : : {
467 : : TSVector tsout;
468 : 55 : WordEntry *arrin = ARRPTR(tsv),
469 : : *arrout;
470 : 55 : char *data = STRPTR(tsv),
471 : : *dataout;
472 : : int i, /* index in arrin */
473 : : j, /* index in arrout */
474 : : k, /* index in indices_to_delete */
475 : : curoff; /* index in dataout area */
476 : :
477 : : /*
478 : : * Sort the filter array to simplify membership checks below. Also, get
479 : : * rid of any duplicate entries, so that we can assume that indices_count
480 : : * is exactly equal to the number of lexemes that will be removed.
481 : : */
482 [ + + ]: 55 : if (indices_count > 1)
483 : : {
3560 tgl@sss.pgh.pa.us 484 : 25 : qsort(indices_to_delete, indices_count, sizeof(int), compare_int);
2371 tmunro@postgresql.or 485 : 25 : indices_count = qunique(indices_to_delete, indices_count, sizeof(int),
486 : : compare_int);
487 : : }
488 : :
489 : : /*
490 : : * Here we overestimate tsout size, since we don't know how much space is
491 : : * used by the deleted lexeme(s). We will set exact size below.
492 : : */
3560 tgl@sss.pgh.pa.us 493 : 55 : tsout = (TSVector) palloc0(VARSIZE(tsv));
494 : :
495 : : /* This count must be correct because STRPTR(tsout) relies on it. */
496 : 55 : tsout->size = tsv->size - indices_count;
497 : :
498 : : /*
499 : : * Copy tsv to tsout, skipping lexemes listed in indices_to_delete.
500 : : */
501 : 55 : arrout = ARRPTR(tsout);
3707 teodor@sigaev.ru 502 : 55 : dataout = STRPTR(tsout);
3560 tgl@sss.pgh.pa.us 503 : 55 : curoff = 0;
3707 teodor@sigaev.ru 504 [ + + ]: 330 : for (i = j = k = 0; i < tsv->size; i++)
505 : : {
506 : : /*
507 : : * If current i is present in indices_to_delete, skip this lexeme.
508 : : * Since indices_to_delete is already sorted, we only need to check
509 : : * the current (k'th) entry.
510 : : */
3617 rhaas@postgresql.org 511 [ + + + + ]: 275 : if (k < indices_count && i == indices_to_delete[k])
512 : : {
3707 teodor@sigaev.ru 513 : 80 : k++;
514 : 80 : continue;
515 : : }
516 : :
517 : : /* Copy lexeme and its positions and weights */
518 : 195 : memcpy(dataout + curoff, data + arrin[i].pos, arrin[i].len);
519 : 195 : arrout[j].haspos = arrin[i].haspos;
520 : 195 : arrout[j].len = arrin[i].len;
521 : 195 : arrout[j].pos = curoff;
522 : 195 : curoff += arrin[i].len;
523 [ + + ]: 195 : if (arrin[i].haspos)
524 : : {
3560 tgl@sss.pgh.pa.us 525 [ + - ]: 130 : int len = POSDATALEN(tsv, arrin + i) * sizeof(WordEntryPos)
1082 526 : 130 : + sizeof(uint16);
527 : :
3707 teodor@sigaev.ru 528 : 130 : curoff = SHORTALIGN(curoff);
529 : 130 : memcpy(dataout + curoff,
530 : 130 : STRPTR(tsv) + SHORTALIGN(arrin[i].pos + arrin[i].len),
531 : : len);
532 : 130 : curoff += len;
533 : : }
534 : :
535 : 195 : j++;
536 : : }
537 : :
538 : : /*
539 : : * k should now be exactly equal to indices_count. If it isn't then the
540 : : * caller provided us with indices outside of [0, tsv->size) range and
541 : : * estimation of tsout's size is wrong.
542 : : */
543 [ - + ]: 55 : Assert(k == indices_count);
544 : :
545 : 55 : SET_VARSIZE(tsout, CALCDATASIZE(tsout->size, curoff));
546 : 55 : return tsout;
547 : : }
548 : :
549 : : /*
550 : : * Delete given lexeme from tsvector.
551 : : * Implementation of user-level ts_delete(tsvector, text).
552 : : */
553 : : Datum
554 : 30 : tsvector_delete_str(PG_FUNCTION_ARGS)
555 : : {
556 : 30 : TSVector tsin = PG_GETARG_TSVECTOR(0),
557 : : tsout;
3341 noah@leadboat.com 558 : 30 : text *tlexeme = PG_GETARG_TEXT_PP(1);
559 [ - + ]: 30 : char *lexeme = VARDATA_ANY(tlexeme);
3707 teodor@sigaev.ru 560 [ - + - - : 30 : int lexeme_len = VARSIZE_ANY_EXHDR(tlexeme),
- - - - -
+ ]
561 : : skip_index;
562 : :
563 [ + + ]: 30 : if ((skip_index = tsvector_bsearch(tsin, lexeme, lexeme_len)) == -1)
564 : 10 : PG_RETURN_POINTER(tsin);
565 : :
566 : 20 : tsout = tsvector_delete_by_indices(tsin, &skip_index, 1);
567 : :
568 [ - + ]: 20 : PG_FREE_IF_COPY(tsin, 0);
569 [ - + ]: 20 : PG_FREE_IF_COPY(tlexeme, 1);
570 : 20 : PG_RETURN_POINTER(tsout);
571 : : }
572 : :
573 : : /*
574 : : * Delete given array of lexemes from tsvector.
575 : : * Implementation of user-level ts_delete(tsvector, text[]).
576 : : */
577 : : Datum
578 : 35 : tsvector_delete_arr(PG_FUNCTION_ARGS)
579 : : {
580 : 35 : TSVector tsin = PG_GETARG_TSVECTOR(0),
581 : : tsout;
582 : 35 : ArrayType *lexemes = PG_GETARG_ARRAYTYPE_P(1);
583 : : int i,
584 : : nlex,
585 : : skip_count,
586 : : *skip_indices;
587 : : Datum *dlexemes;
588 : : bool *nulls;
589 : :
1404 peter@eisentraut.org 590 : 35 : deconstruct_array_builtin(lexemes, TEXTOID, &dlexemes, &nulls, &nlex);
591 : :
592 : : /*
593 : : * In typical use case array of lexemes to delete is relatively small. So
594 : : * here we optimize things for that scenario: iterate through lexarr
595 : : * performing binary search of each lexeme from lexarr in tsvector.
596 : : */
3707 teodor@sigaev.ru 597 : 35 : skip_indices = palloc0(nlex * sizeof(int));
598 [ + + ]: 140 : for (i = skip_count = 0; i < nlex; i++)
599 : : {
600 : : char *lex;
601 : : int lex_len,
602 : : lex_pos;
603 : :
604 : : /* Ignore null array elements, they surely don't match */
605 [ + + ]: 105 : if (nulls[i])
1641 tgl@sss.pgh.pa.us 606 : 5 : continue;
607 : :
273 peter@eisentraut.org 608 :GNC 100 : lex = VARDATA(DatumGetPointer(dlexemes[i]));
609 : 100 : lex_len = VARSIZE(DatumGetPointer(dlexemes[i])) - VARHDRSZ;
3707 teodor@sigaev.ru 610 :CBC 100 : lex_pos = tsvector_bsearch(tsin, lex, lex_len);
611 : :
612 [ + + ]: 100 : if (lex_pos >= 0)
613 : 65 : skip_indices[skip_count++] = lex_pos;
614 : : }
615 : :
616 : 35 : tsout = tsvector_delete_by_indices(tsin, skip_indices, skip_count);
617 : :
618 : 35 : pfree(skip_indices);
619 [ - + ]: 35 : PG_FREE_IF_COPY(tsin, 0);
620 [ - + ]: 35 : PG_FREE_IF_COPY(lexemes, 1);
621 : :
622 : 35 : PG_RETURN_POINTER(tsout);
623 : : }
624 : :
625 : : /*
626 : : * Expand tsvector as table with following columns:
627 : : * lexeme: lexeme text
628 : : * positions: integer array of lexeme positions
629 : : * weights: char array of weights corresponding to positions
630 : : */
631 : : Datum
632 : 120 : tsvector_unnest(PG_FUNCTION_ARGS)
633 : : {
634 : : FuncCallContext *funcctx;
635 : : TSVector tsin;
636 : :
637 [ + + ]: 120 : if (SRF_IS_FIRSTCALL())
638 : : {
639 : : MemoryContext oldcontext;
640 : : TupleDesc tupdesc;
641 : :
642 : 20 : funcctx = SRF_FIRSTCALL_INIT();
643 : 20 : oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
644 : :
2723 andres@anarazel.de 645 : 20 : tupdesc = CreateTemplateTupleDesc(3);
3707 teodor@sigaev.ru 646 : 20 : TupleDescInitEntry(tupdesc, (AttrNumber) 1, "lexeme",
647 : : TEXTOID, -1, 0);
648 : 20 : TupleDescInitEntry(tupdesc, (AttrNumber) 2, "positions",
649 : : INT2ARRAYOID, -1, 0);
650 : 20 : TupleDescInitEntry(tupdesc, (AttrNumber) 3, "weights",
651 : : TEXTARRAYOID, -1, 0);
1231 michael@paquier.xyz 652 [ - + ]: 20 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1231 michael@paquier.xyz 653 [ # # ]:UBC 0 : elog(ERROR, "return type must be a row type");
50 drowley@postgresql.o 654 :GNC 20 : TupleDescFinalize(tupdesc);
1231 michael@paquier.xyz 655 :CBC 20 : funcctx->tuple_desc = tupdesc;
656 : :
3707 teodor@sigaev.ru 657 : 20 : funcctx->user_fctx = PG_GETARG_TSVECTOR_COPY(0);
658 : :
659 : 20 : MemoryContextSwitchTo(oldcontext);
660 : : }
661 : :
662 : 120 : funcctx = SRF_PERCALL_SETUP();
663 : 120 : tsin = (TSVector) funcctx->user_fctx;
664 : :
665 [ + + ]: 120 : if (funcctx->call_cntr < tsin->size)
666 : : {
667 : 100 : WordEntry *arrin = ARRPTR(tsin);
668 : 100 : char *data = STRPTR(tsin);
669 : : HeapTuple tuple;
670 : : int j,
671 : 100 : i = funcctx->call_cntr;
672 : 100 : bool nulls[] = {false, false, false};
673 : : Datum values[3];
674 : :
2287 alvherre@alvh.no-ip. 675 : 100 : values[0] = PointerGetDatum(cstring_to_text_with_len(data + arrin[i].pos, arrin[i].len));
676 : :
3707 teodor@sigaev.ru 677 [ + + ]: 100 : if (arrin[i].haspos)
678 : : {
679 : : WordEntryPosVector *posv;
680 : : Datum *positions;
681 : : Datum *weights;
682 : : char weight;
683 : :
684 : : /*
685 : : * Internally tsvector stores position and weight in the same
686 : : * uint16 (2 bits for weight, 14 for position). Here we extract
687 : : * that in two separate arrays.
688 : : */
689 : 60 : posv = _POSVECPTR(tsin, arrin + i);
690 : 60 : positions = palloc(posv->npos * sizeof(Datum));
3617 rhaas@postgresql.org 691 : 60 : weights = palloc(posv->npos * sizeof(Datum));
3707 teodor@sigaev.ru 692 [ + + ]: 168 : for (j = 0; j < posv->npos; j++)
693 : : {
694 : 108 : positions[j] = Int16GetDatum(WEP_GETPOS(posv->pos[j]));
695 : 108 : weight = 'D' - WEP_GETWEIGHT(posv->pos[j]);
2287 alvherre@alvh.no-ip. 696 : 108 : weights[j] = PointerGetDatum(cstring_to_text_with_len(&weight,
697 : : 1));
698 : : }
699 : :
1404 peter@eisentraut.org 700 : 60 : values[1] = PointerGetDatum(construct_array_builtin(positions, posv->npos, INT2OID));
701 : 60 : values[2] = PointerGetDatum(construct_array_builtin(weights, posv->npos, TEXTOID));
702 : : }
703 : : else
704 : : {
3707 teodor@sigaev.ru 705 : 40 : nulls[1] = nulls[2] = true;
706 : : }
707 : :
708 : 100 : tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
709 : 100 : SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
710 : : }
711 : : else
712 : : {
713 : 20 : SRF_RETURN_DONE(funcctx);
714 : : }
715 : : }
716 : :
717 : : /*
718 : : * Convert tsvector to array of lexemes.
719 : : */
720 : : Datum
721 : 10 : tsvector_to_array(PG_FUNCTION_ARGS)
722 : : {
3617 rhaas@postgresql.org 723 : 10 : TSVector tsin = PG_GETARG_TSVECTOR(0);
724 : 10 : WordEntry *arrin = ARRPTR(tsin);
725 : : Datum *elements;
726 : : int i;
727 : : ArrayType *array;
728 : :
3707 teodor@sigaev.ru 729 : 10 : elements = palloc(tsin->size * sizeof(Datum));
730 : :
731 [ + + ]: 60 : for (i = 0; i < tsin->size; i++)
732 : : {
2287 alvherre@alvh.no-ip. 733 : 50 : elements[i] = PointerGetDatum(cstring_to_text_with_len(STRPTR(tsin) + arrin[i].pos,
2287 alvherre@alvh.no-ip. 734 :ECB (30) : arrin[i].len));
735 : : }
736 : :
1404 peter@eisentraut.org 737 :CBC 10 : array = construct_array_builtin(elements, tsin->size, TEXTOID);
738 : :
3707 teodor@sigaev.ru 739 : 10 : pfree(elements);
740 [ - + ]: 10 : PG_FREE_IF_COPY(tsin, 0);
741 : 10 : PG_RETURN_POINTER(array);
742 : : }
743 : :
744 : : /*
745 : : * Build tsvector from array of lexemes.
746 : : */
747 : : Datum
748 : 18 : array_to_tsvector(PG_FUNCTION_ARGS)
749 : : {
750 : 18 : ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
751 : : TSVector tsout;
752 : : Datum *dlexemes;
753 : : WordEntry *arrout;
754 : : bool *nulls;
755 : : int nitems,
756 : : i,
757 : : tslen,
758 : 18 : datalen = 0;
759 : : char *cur;
760 : :
1404 peter@eisentraut.org 761 : 18 : deconstruct_array_builtin(v, TEXTOID, &dlexemes, &nulls, &nitems);
762 : :
763 : : /*
764 : : * Reject nulls and zero length strings (maybe we should just ignore them,
765 : : * instead?)
766 : : */
3707 teodor@sigaev.ru 767 [ + + ]: 95 : for (i = 0; i < nitems; i++)
768 : : {
769 [ + + ]: 85 : if (nulls[i])
770 [ + - ]: 4 : ereport(ERROR,
771 : : (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
772 : : errmsg("lexeme array may not contain nulls")));
773 : :
273 peter@eisentraut.org 774 [ + + ]:GNC 81 : if (VARSIZE(DatumGetPointer(dlexemes[i])) - VARHDRSZ == 0)
1641 tgl@sss.pgh.pa.us 775 [ + - ]:CBC 4 : ereport(ERROR,
776 : : (errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
777 : : errmsg("lexeme array may not contain empty strings")));
778 : : }
779 : :
780 : : /* Sort and de-dup, because this is required for a valid tsvector. */
3560 781 [ + - ]: 10 : if (nitems > 1)
782 : : {
783 : 10 : qsort(dlexemes, nitems, sizeof(Datum), compare_text_lexemes);
2371 tmunro@postgresql.or 784 : 10 : nitems = qunique(dlexemes, nitems, sizeof(Datum),
785 : : compare_text_lexemes);
786 : : }
787 : :
788 : : /* Calculate space needed for surviving lexemes. */
3560 tgl@sss.pgh.pa.us 789 [ + + ]: 50 : for (i = 0; i < nitems; i++)
273 peter@eisentraut.org 790 :GNC 40 : datalen += VARSIZE(DatumGetPointer(dlexemes[i])) - VARHDRSZ;
3707 teodor@sigaev.ru 791 :CBC 10 : tslen = CALCDATASIZE(nitems, datalen);
792 : :
793 : : /* Allocate and fill tsvector. */
794 : 10 : tsout = (TSVector) palloc0(tslen);
795 : 10 : SET_VARSIZE(tsout, tslen);
796 : 10 : tsout->size = nitems;
797 : :
798 : 10 : arrout = ARRPTR(tsout);
799 : 10 : cur = STRPTR(tsout);
800 [ + + ]: 50 : for (i = 0; i < nitems; i++)
801 : : {
273 peter@eisentraut.org 802 :GNC 40 : char *lex = VARDATA(DatumGetPointer(dlexemes[i]));
803 : 40 : int lex_len = VARSIZE(DatumGetPointer(dlexemes[i])) - VARHDRSZ;
804 : :
3707 teodor@sigaev.ru 805 :CBC 40 : memcpy(cur, lex, lex_len);
806 : 40 : arrout[i].haspos = 0;
807 : 40 : arrout[i].len = lex_len;
808 : 40 : arrout[i].pos = cur - STRPTR(tsout);
809 : 40 : cur += lex_len;
810 : : }
811 : :
812 [ - + ]: 10 : PG_FREE_IF_COPY(v, 0);
813 : 10 : PG_RETURN_POINTER(tsout);
814 : : }
815 : :
816 : : /*
817 : : * ts_filter(): keep only lexemes with given weights in tsvector.
818 : : */
819 : : Datum
820 : 14 : tsvector_filter(PG_FUNCTION_ARGS)
821 : : {
822 : 14 : TSVector tsin = PG_GETARG_TSVECTOR(0),
823 : : tsout;
824 : 14 : ArrayType *weights = PG_GETARG_ARRAYTYPE_P(1);
825 : 14 : WordEntry *arrin = ARRPTR(tsin),
826 : : *arrout;
827 : 14 : char *datain = STRPTR(tsin),
828 : : *dataout;
829 : : Datum *dweights;
830 : : bool *nulls;
831 : : int nweights;
832 : : int i,
833 : : j;
3653 834 : 14 : int cur_pos = 0;
835 : 14 : char mask = 0;
836 : :
1404 peter@eisentraut.org 837 : 14 : deconstruct_array_builtin(weights, CHAROID, &dweights, &nulls, &nweights);
838 : :
3652 tgl@sss.pgh.pa.us 839 [ + + ]: 32 : for (i = 0; i < nweights; i++)
840 : : {
841 : : char char_weight;
842 : :
3707 teodor@sigaev.ru 843 [ + + ]: 22 : if (nulls[i])
844 [ + - ]: 4 : ereport(ERROR,
845 : : (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
846 : : errmsg("weight array may not contain nulls")));
847 : :
848 : 18 : char_weight = DatumGetChar(dweights[i]);
849 [ + + - - : 18 : switch (char_weight)
- ]
850 : : {
3617 rhaas@postgresql.org 851 : 14 : case 'A':
852 : : case 'a':
3707 teodor@sigaev.ru 853 : 14 : mask = mask | 8;
854 : 14 : break;
3617 rhaas@postgresql.org 855 : 4 : case 'B':
856 : : case 'b':
3707 teodor@sigaev.ru 857 : 4 : mask = mask | 4;
858 : 4 : break;
3617 rhaas@postgresql.org 859 :UBC 0 : case 'C':
860 : : case 'c':
3707 teodor@sigaev.ru 861 : 0 : mask = mask | 2;
862 : 0 : break;
3617 rhaas@postgresql.org 863 : 0 : case 'D':
864 : : case 'd':
3707 teodor@sigaev.ru 865 : 0 : mask = mask | 1;
866 : 0 : break;
867 : 0 : default:
3617 rhaas@postgresql.org 868 [ # # ]: 0 : ereport(ERROR,
869 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
870 : : errmsg("unrecognized weight: \"%c\"", char_weight)));
871 : : }
872 : : }
873 : :
3707 teodor@sigaev.ru 874 :CBC 10 : tsout = (TSVector) palloc0(VARSIZE(tsin));
875 : 10 : tsout->size = tsin->size;
876 : 10 : arrout = ARRPTR(tsout);
877 : 10 : dataout = STRPTR(tsout);
878 : :
879 [ + + ]: 90 : for (i = j = 0; i < tsin->size; i++)
880 : : {
881 : : WordEntryPosVector *posvin,
882 : : *posvout;
3617 rhaas@postgresql.org 883 : 80 : int npos = 0;
884 : : int k;
885 : :
3707 teodor@sigaev.ru 886 [ + + ]: 80 : if (!arrin[i].haspos)
887 : 25 : continue;
888 : :
3617 rhaas@postgresql.org 889 : 55 : posvin = _POSVECPTR(tsin, arrin + i);
3707 teodor@sigaev.ru 890 : 55 : posvout = (WordEntryPosVector *)
3617 rhaas@postgresql.org 891 : 55 : (dataout + SHORTALIGN(cur_pos + arrin[i].len));
892 : :
3707 teodor@sigaev.ru 893 [ + + ]: 110 : for (k = 0; k < posvin->npos; k++)
894 : : {
895 [ + + ]: 55 : if (mask & (1 << WEP_GETWEIGHT(posvin->pos[k])))
896 : 25 : posvout->pos[npos++] = posvin->pos[k];
897 : : }
898 : :
899 : : /* if no satisfactory positions found, skip lexeme */
3654 rhaas@postgresql.org 900 [ + + ]: 55 : if (!npos)
3707 teodor@sigaev.ru 901 : 30 : continue;
902 : :
903 : 25 : arrout[j].haspos = true;
904 : 25 : arrout[j].len = arrin[i].len;
905 : 25 : arrout[j].pos = cur_pos;
906 : :
907 : 25 : memcpy(dataout + cur_pos, datain + arrin[i].pos, arrin[i].len);
908 : 25 : posvout->npos = npos;
909 : 25 : cur_pos += SHORTALIGN(arrin[i].len);
3617 rhaas@postgresql.org 910 [ + - ]: 25 : cur_pos += POSDATALEN(tsout, arrout + j) * sizeof(WordEntryPos) +
911 : : sizeof(uint16);
3707 teodor@sigaev.ru 912 : 25 : j++;
913 : : }
914 : :
915 : 10 : tsout->size = j;
916 [ + - ]: 10 : if (dataout != STRPTR(tsout))
917 : 10 : memmove(STRPTR(tsout), dataout, cur_pos);
918 : :
919 : 10 : SET_VARSIZE(tsout, CALCDATASIZE(tsout->size, cur_pos));
920 : :
921 [ - + ]: 10 : PG_FREE_IF_COPY(tsin, 0);
922 : 10 : PG_RETURN_POINTER(tsout);
923 : : }
924 : :
925 : : Datum
6832 tgl@sss.pgh.pa.us 926 : 9 : tsvector_concat(PG_FUNCTION_ARGS)
927 : : {
928 : 9 : TSVector in1 = PG_GETARG_TSVECTOR(0);
929 : 9 : TSVector in2 = PG_GETARG_TSVECTOR(1);
930 : : TSVector out;
931 : : WordEntry *ptr;
932 : : WordEntry *ptr1,
933 : : *ptr2;
934 : : WordEntryPos *p;
935 : 9 : int maxpos = 0,
936 : : i,
937 : : j,
938 : : i1,
939 : : i2,
940 : : dataoff,
941 : : output_bytes,
942 : : output_size;
943 : : char *data,
944 : : *data1,
945 : : *data2;
946 : :
947 : : /* Get max position in in1; we'll need this to offset in2's positions */
948 : 9 : ptr = ARRPTR(in1);
949 : 9 : i = in1->size;
950 [ + + ]: 23 : while (i--)
951 : : {
952 [ + - + - ]: 14 : if ((j = POSDATALEN(in1, ptr)) != 0)
953 : : {
954 : 14 : p = POSDATAPTR(in1, ptr);
955 [ + + ]: 28 : while (j--)
956 : : {
957 [ + + ]: 14 : if (WEP_GETPOS(*p) > maxpos)
958 : 9 : maxpos = WEP_GETPOS(*p);
959 : 14 : p++;
960 : : }
961 : : }
962 : 14 : ptr++;
963 : : }
964 : :
965 : 9 : ptr1 = ARRPTR(in1);
966 : 9 : ptr2 = ARRPTR(in2);
967 : 9 : data1 = STRPTR(in1);
968 : 9 : data2 = STRPTR(in2);
969 : 9 : i1 = in1->size;
970 : 9 : i2 = in2->size;
971 : :
972 : : /*
973 : : * Conservative estimate of space needed. We might need all the data in
974 : : * both inputs, and conceivably add a pad byte before position data for
975 : : * each item where there was none before.
976 : : */
5366 977 : 9 : output_bytes = VARSIZE(in1) + VARSIZE(in2) + i1 + i2;
978 : :
979 : 9 : out = (TSVector) palloc0(output_bytes);
980 : 9 : SET_VARSIZE(out, output_bytes);
981 : :
982 : : /*
983 : : * We must make out->size valid so that STRPTR(out) is sensible. We'll
984 : : * collapse out any unused space at the end.
985 : : */
6832 986 : 9 : out->size = in1->size + in2->size;
987 : :
988 : 9 : ptr = ARRPTR(out);
6769 989 : 9 : data = STRPTR(out);
990 : 9 : dataoff = 0;
6832 991 [ + + + + ]: 23 : while (i1 && i2)
992 : : {
993 : 14 : int cmp = compareEntry(data1, ptr1, data2, ptr2);
994 : :
995 [ + + ]: 14 : if (cmp < 0)
996 : : { /* in1 first */
997 : 5 : ptr->haspos = ptr1->haspos;
998 : 5 : ptr->len = ptr1->len;
6769 999 : 5 : memcpy(data + dataoff, data1 + ptr1->pos, ptr1->len);
1000 : 5 : ptr->pos = dataoff;
1001 : 5 : dataoff += ptr1->len;
6832 1002 [ + - ]: 5 : if (ptr->haspos)
1003 : : {
6769 1004 : 5 : dataoff = SHORTALIGN(dataoff);
1005 [ + - ]: 5 : memcpy(data + dataoff, _POSVECPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16));
1006 [ + - ]: 5 : dataoff += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16);
1007 : : }
1008 : :
6832 1009 : 5 : ptr++;
1010 : 5 : ptr1++;
1011 : 5 : i1--;
1012 : : }
1013 [ + + ]: 9 : else if (cmp > 0)
1014 : : { /* in2 first */
1015 : 4 : ptr->haspos = ptr2->haspos;
1016 : 4 : ptr->len = ptr2->len;
6769 1017 : 4 : memcpy(data + dataoff, data2 + ptr2->pos, ptr2->len);
1018 : 4 : ptr->pos = dataoff;
1019 : 4 : dataoff += ptr2->len;
6832 1020 [ - + ]: 4 : if (ptr->haspos)
1021 : : {
6832 tgl@sss.pgh.pa.us 1022 :UBC 0 : int addlen = add_pos(in2, ptr2, out, ptr, maxpos);
1023 : :
1024 [ # # ]: 0 : if (addlen == 0)
1025 : 0 : ptr->haspos = 0;
1026 : : else
1027 : : {
6769 1028 : 0 : dataoff = SHORTALIGN(dataoff);
1029 : 0 : dataoff += addlen * sizeof(WordEntryPos) + sizeof(uint16);
1030 : : }
1031 : : }
1032 : :
6832 tgl@sss.pgh.pa.us 1033 :CBC 4 : ptr++;
1034 : 4 : ptr2++;
1035 : 4 : i2--;
1036 : : }
1037 : : else
1038 : : {
1039 : 5 : ptr->haspos = ptr1->haspos | ptr2->haspos;
1040 : 5 : ptr->len = ptr1->len;
6769 1041 : 5 : memcpy(data + dataoff, data1 + ptr1->pos, ptr1->len);
1042 : 5 : ptr->pos = dataoff;
1043 : 5 : dataoff += ptr1->len;
6832 1044 [ + - ]: 5 : if (ptr->haspos)
1045 : : {
1046 [ + - ]: 5 : if (ptr1->haspos)
1047 : : {
6769 1048 : 5 : dataoff = SHORTALIGN(dataoff);
1049 [ + - ]: 5 : memcpy(data + dataoff, _POSVECPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16));
1050 [ + - ]: 5 : dataoff += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16);
6832 1051 [ + - ]: 5 : if (ptr2->haspos)
6769 1052 : 5 : dataoff += add_pos(in2, ptr2, out, ptr, maxpos) * sizeof(WordEntryPos);
1053 : : }
1054 : : else /* must have ptr2->haspos */
1055 : : {
6832 tgl@sss.pgh.pa.us 1056 :UBC 0 : int addlen = add_pos(in2, ptr2, out, ptr, maxpos);
1057 : :
1058 [ # # ]: 0 : if (addlen == 0)
1059 : 0 : ptr->haspos = 0;
1060 : : else
1061 : : {
6769 1062 : 0 : dataoff = SHORTALIGN(dataoff);
1063 : 0 : dataoff += addlen * sizeof(WordEntryPos) + sizeof(uint16);
1064 : : }
1065 : : }
1066 : : }
1067 : :
6832 tgl@sss.pgh.pa.us 1068 :CBC 5 : ptr++;
1069 : 5 : ptr1++;
1070 : 5 : ptr2++;
1071 : 5 : i1--;
1072 : 5 : i2--;
1073 : : }
1074 : : }
1075 : :
1076 [ + + ]: 13 : while (i1)
1077 : : {
1078 : 4 : ptr->haspos = ptr1->haspos;
1079 : 4 : ptr->len = ptr1->len;
6769 1080 : 4 : memcpy(data + dataoff, data1 + ptr1->pos, ptr1->len);
1081 : 4 : ptr->pos = dataoff;
1082 : 4 : dataoff += ptr1->len;
6832 1083 [ + - ]: 4 : if (ptr->haspos)
1084 : : {
6769 1085 : 4 : dataoff = SHORTALIGN(dataoff);
1086 [ + - ]: 4 : memcpy(data + dataoff, _POSVECPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16));
1087 [ + - ]: 4 : dataoff += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16);
1088 : : }
1089 : :
6832 1090 : 4 : ptr++;
1091 : 4 : ptr1++;
1092 : 4 : i1--;
1093 : : }
1094 : :
1095 [ + + ]: 14 : while (i2)
1096 : : {
1097 : 5 : ptr->haspos = ptr2->haspos;
1098 : 5 : ptr->len = ptr2->len;
6769 1099 : 5 : memcpy(data + dataoff, data2 + ptr2->pos, ptr2->len);
1100 : 5 : ptr->pos = dataoff;
1101 : 5 : dataoff += ptr2->len;
6832 1102 [ + - ]: 5 : if (ptr->haspos)
1103 : : {
1104 : 5 : int addlen = add_pos(in2, ptr2, out, ptr, maxpos);
1105 : :
1106 [ - + ]: 5 : if (addlen == 0)
6832 tgl@sss.pgh.pa.us 1107 :UBC 0 : ptr->haspos = 0;
1108 : : else
1109 : : {
6769 tgl@sss.pgh.pa.us 1110 :CBC 5 : dataoff = SHORTALIGN(dataoff);
1111 : 5 : dataoff += addlen * sizeof(WordEntryPos) + sizeof(uint16);
1112 : : }
1113 : : }
1114 : :
6832 1115 : 5 : ptr++;
1116 : 5 : ptr2++;
1117 : 5 : i2--;
1118 : : }
1119 : :
1120 : : /*
1121 : : * Instead of checking each offset individually, we check for overflow of
1122 : : * pos fields once at the end.
1123 : : */
6769 1124 [ - + ]: 9 : if (dataoff > MAXSTRPOS)
6769 tgl@sss.pgh.pa.us 1125 [ # # ]:UBC 0 : ereport(ERROR,
1126 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1127 : : errmsg("string is too long for tsvector (%d bytes, max %d bytes)", dataoff, MAXSTRPOS)));
1128 : :
1129 : : /*
1130 : : * Adjust sizes (asserting that we didn't overrun the original estimates)
1131 : : * and collapse out any unused array entries.
1132 : : */
5366 tgl@sss.pgh.pa.us 1133 :CBC 9 : output_size = ptr - ARRPTR(out);
1134 [ - + ]: 9 : Assert(output_size <= out->size);
1135 : 9 : out->size = output_size;
6832 1136 [ + + ]: 9 : if (data != STRPTR(out))
6769 1137 : 5 : memmove(STRPTR(out), data, dataoff);
5366 1138 : 9 : output_bytes = CALCDATASIZE(out->size, dataoff);
1139 [ - + ]: 9 : Assert(output_bytes <= VARSIZE(out));
1140 : 9 : SET_VARSIZE(out, output_bytes);
1141 : :
6832 1142 [ - + ]: 9 : PG_FREE_IF_COPY(in1, 0);
1143 [ - + ]: 9 : PG_FREE_IF_COPY(in2, 1);
1144 : 9 : PG_RETURN_POINTER(out);
1145 : : }
1146 : :
1147 : : /*
1148 : : * Compare two strings by tsvector rules.
1149 : : *
1150 : : * if prefix = true then it returns zero value iff b has prefix a
1151 : : */
1152 : : int32
6563 1153 : 4199156 : tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
1154 : : {
1155 : : int cmp;
1156 : :
6172 bruce@momjian.us 1157 [ + + ]: 4199156 : if (lena == 0)
1158 : : {
1159 [ - + ]: 30 : if (prefix)
5595 tgl@sss.pgh.pa.us 1160 :UBC 0 : cmp = 0; /* empty string is prefix of anything */
1161 : : else
6172 bruce@momjian.us 1162 [ + - ]:CBC 30 : cmp = (lenb > 0) ? -1 : 0;
1163 : : }
1164 [ - + ]: 4199126 : else if (lenb == 0)
1165 : : {
6172 bruce@momjian.us 1166 :UBC 0 : cmp = (lena > 0) ? 1 : 0;
1167 : : }
1168 : : else
1169 : : {
1540 tgl@sss.pgh.pa.us 1170 :CBC 4199126 : cmp = memcmp(a, b, Min((unsigned int) lena, (unsigned int) lenb));
1171 : :
6172 bruce@momjian.us 1172 [ + + ]: 4199126 : if (prefix)
1173 : : {
1174 [ + + - + ]: 11021 : if (cmp == 0 && lena > lenb)
5595 tgl@sss.pgh.pa.us 1175 :UBC 0 : cmp = 1; /* a is longer, so not a prefix of b */
1176 : : }
5595 tgl@sss.pgh.pa.us 1177 [ + + + + ]:CBC 4188105 : else if (cmp == 0 && lena != lenb)
1178 : : {
6563 1179 [ + + ]: 21679 : cmp = (lena < lenb) ? -1 : 1;
1180 : : }
1181 : : }
1182 : :
1183 : 4199156 : return cmp;
1184 : : }
1185 : :
1186 : : /*
1187 : : * Check weight info or/and fill 'data' with the required positions
1188 : : */
1189 : : static TSTernaryValue
3680 teodor@sigaev.ru 1190 : 45576 : checkclass_str(CHKVAL *chkval, WordEntry *entry, QueryOperand *val,
1191 : : ExecPhraseData *data)
1192 : : {
2111 tgl@sss.pgh.pa.us 1193 : 45576 : TSTernaryValue result = TS_NO;
1194 : :
1195 [ + + - + ]: 45576 : Assert(data == NULL || data->npos == 0);
1196 : :
1197 [ + + ]: 45576 : if (entry->haspos)
1198 : : {
1199 : : WordEntryPosVector *posvec;
1200 : :
1201 : : /*
1202 : : * We can't use the _POSVECPTR macro here because the pointer to the
1203 : : * tsvector's lexeme storage is already contained in chkval->values.
1204 : : */
3680 teodor@sigaev.ru 1205 : 3160 : posvec = (WordEntryPosVector *)
1206 : 3160 : (chkval->values + SHORTALIGN(entry->pos + entry->len));
1207 : :
1208 [ + + + + ]: 3160 : if (val->weight && data)
1209 : 40 : {
3617 rhaas@postgresql.org 1210 : 40 : WordEntryPos *posvec_iter = posvec->pos;
1211 : : WordEntryPos *dptr;
1212 : :
1213 : : /*
1214 : : * Filter position information by weights
1215 : : */
146 michael@paquier.xyz 1216 :GNC 40 : dptr = data->pos = palloc_array(WordEntryPos, posvec->npos);
3680 teodor@sigaev.ru 1217 :CBC 40 : data->allocated = true;
1218 : :
1219 : : /* Is there a position with a matching weight? */
1220 [ + + ]: 80 : while (posvec_iter < posvec->pos + posvec->npos)
1221 : : {
1222 : : /* If true, append this position to the data->pos */
1223 [ + + ]: 40 : if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1224 : : {
1225 : 20 : *dptr = WEP_GETPOS(*posvec_iter);
1226 : 20 : dptr++;
1227 : : }
1228 : :
1229 : 40 : posvec_iter++;
1230 : : }
1231 : :
1232 : 40 : data->npos = dptr - data->pos;
1233 : :
1234 [ + + ]: 40 : if (data->npos > 0)
2111 tgl@sss.pgh.pa.us 1235 : 20 : result = TS_YES;
1236 : : else
1237 : : {
1238 : 20 : pfree(data->pos);
1239 : 20 : data->pos = NULL;
1240 : 20 : data->allocated = false;
1241 : : }
1242 : : }
3680 teodor@sigaev.ru 1243 [ + + ]: 3120 : else if (val->weight)
1244 : : {
3617 rhaas@postgresql.org 1245 : 332 : WordEntryPos *posvec_iter = posvec->pos;
1246 : :
1247 : : /* Is there a position with a matching weight? */
3680 teodor@sigaev.ru 1248 [ + + ]: 503 : while (posvec_iter < posvec->pos + posvec->npos)
1249 : : {
1250 [ + + ]: 372 : if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1251 : : {
2111 tgl@sss.pgh.pa.us 1252 : 201 : result = TS_YES;
3617 rhaas@postgresql.org 1253 : 201 : break; /* no need to go further */
1254 : : }
1255 : :
3680 teodor@sigaev.ru 1256 : 171 : posvec_iter++;
1257 : : }
1258 : : }
2111 tgl@sss.pgh.pa.us 1259 [ + + ]: 2788 : else if (data)
1260 : : {
3680 teodor@sigaev.ru 1261 : 1645 : data->npos = posvec->npos;
3617 rhaas@postgresql.org 1262 : 1645 : data->pos = posvec->pos;
3680 teodor@sigaev.ru 1263 : 1645 : data->allocated = false;
2111 tgl@sss.pgh.pa.us 1264 : 1645 : result = TS_YES;
1265 : : }
1266 : : else
1267 : : {
1268 : : /* simplest case: no weight check, positions not needed */
1269 : 1143 : result = TS_YES;
1270 : : }
1271 : : }
1272 : : else
1273 : : {
1274 : : /*
1275 : : * Position info is lacking, so if the caller requires it, we can only
1276 : : * say that maybe there is a match.
1277 : : *
1278 : : * Notice, however, that we *don't* check val->weight here.
1279 : : * Historically, stripped tsvectors are considered to match queries
1280 : : * whether or not the query has a weight restriction; that's a little
1281 : : * dubious but we'll preserve the behavior.
1282 : : */
1283 [ + + ]: 42416 : if (data)
1284 : 15385 : result = TS_MAYBE;
1285 : : else
1286 : 27031 : result = TS_YES;
1287 : : }
1288 : :
3680 teodor@sigaev.ru 1289 : 45576 : return result;
1290 : : }
1291 : :
1292 : : /*
1293 : : * TS_execute callback for matching a tsquery operand to plain tsvector data
1294 : : */
1295 : : static TSTernaryValue
1296 : 189554 : checkcondition_str(void *checkval, QueryOperand *val, ExecPhraseData *data)
1297 : : {
6746 bruce@momjian.us 1298 : 189554 : CHKVAL *chkval = (CHKVAL *) checkval;
6815 teodor@sigaev.ru 1299 : 189554 : WordEntry *StopLow = chkval->arrb;
1300 : 189554 : WordEntry *StopHigh = chkval->arre;
6563 tgl@sss.pgh.pa.us 1301 : 189554 : WordEntry *StopMiddle = StopHigh;
2111 1302 : 189554 : TSTernaryValue res = TS_NO;
1303 : :
1304 : : /* Loop invariant: StopLow <= val < StopHigh */
6832 1305 [ + + ]: 1191598 : while (StopLow < StopHigh)
1306 : : {
1307 : : int difference;
1308 : :
1309 : 1037564 : StopMiddle = StopLow + (StopHigh - StopLow) / 2;
3680 teodor@sigaev.ru 1310 : 1037564 : difference = tsCompareString(chkval->operand + val->distance,
1311 : 1037564 : val->length,
1312 : 1037564 : chkval->values + StopMiddle->pos,
1313 : 1037564 : StopMiddle->len,
1314 : : false);
1315 : :
6832 tgl@sss.pgh.pa.us 1316 [ + + ]: 1037564 : if (difference == 0)
1317 : : {
1318 : : /* Check weight info & fill 'data' with positions */
3680 teodor@sigaev.ru 1319 : 35520 : res = checkclass_str(chkval, StopMiddle, val, data);
6563 tgl@sss.pgh.pa.us 1320 : 35520 : break;
1321 : : }
1322 [ + + ]: 1002044 : else if (difference > 0)
6832 1323 : 565083 : StopLow = StopMiddle + 1;
1324 : : else
1325 : 436961 : StopHigh = StopMiddle;
1326 : : }
1327 : :
1328 : : /*
1329 : : * If it's a prefix search, we should also consider lexemes that the
1330 : : * search term is a prefix of (which will necessarily immediately follow
1331 : : * the place we found in the above loop). But we can skip them if there
1332 : : * was a definite match on the exact term AND the caller doesn't need
1333 : : * position info.
1334 : : */
2111 1335 [ + + + + : 189554 : if (val->prefix && (res != TS_YES || data))
+ - ]
1336 : : {
3617 rhaas@postgresql.org 1337 : 11040 : WordEntryPos *allpos = NULL;
1338 : 11040 : int npos = 0,
1339 : 11040 : totalpos = 0;
1340 : :
1341 : : /* adjust start position for corner case */
6172 bruce@momjian.us 1342 [ + + ]: 11040 : if (StopLow >= StopHigh)
6563 tgl@sss.pgh.pa.us 1343 : 11030 : StopMiddle = StopHigh;
1344 : :
1345 : : /* we don't try to re-use any data from the initial match */
2111 1346 [ + + ]: 11040 : if (data)
1347 : : {
1348 [ - + ]: 30 : if (data->allocated)
2111 tgl@sss.pgh.pa.us 1349 :UBC 0 : pfree(data->pos);
2111 tgl@sss.pgh.pa.us 1350 :CBC 30 : data->pos = NULL;
1351 : 30 : data->allocated = false;
1352 : 30 : data->npos = 0;
1353 : : }
1354 : 11040 : res = TS_NO;
1355 : :
1356 [ - + ]: 21011 : while ((res != TS_YES || data) &&
1357 [ + + + + : 31751 : StopMiddle < chkval->arre &&
+ + ]
3680 teodor@sigaev.ru 1358 : 10655 : tsCompareString(chkval->operand + val->distance,
1359 : 10655 : val->length,
1360 : 10655 : chkval->values + StopMiddle->pos,
1361 : 10655 : StopMiddle->len,
1362 : : true) == 0)
1363 : : {
1364 : : TSTernaryValue subres;
1365 : :
2111 tgl@sss.pgh.pa.us 1366 : 10056 : subres = checkclass_str(chkval, StopMiddle, val, data);
1367 : :
1368 [ + + ]: 10056 : if (subres != TS_NO)
1369 : : {
1370 [ + + ]: 10006 : if (data)
1371 : : {
1372 : : /*
1373 : : * We need to join position information
1374 : : */
1375 [ - + ]: 35 : if (subres == TS_MAYBE)
1376 : : {
1377 : : /*
1378 : : * No position info for this match, so we must report
1379 : : * MAYBE overall.
1380 : : */
2111 tgl@sss.pgh.pa.us 1381 :UBC 0 : res = TS_MAYBE;
1382 : : /* forget any previous positions */
1383 : 0 : npos = 0;
1384 : : /* don't leak storage */
1385 [ # # ]: 0 : if (allpos)
1386 : 0 : pfree(allpos);
1387 : 0 : break;
1388 : : }
1389 : :
2111 tgl@sss.pgh.pa.us 1390 [ + + ]:CBC 65 : while (npos + data->npos > totalpos)
1391 : : {
3680 teodor@sigaev.ru 1392 [ + - ]: 30 : if (totalpos == 0)
1393 : : {
1394 : 30 : totalpos = 256;
146 michael@paquier.xyz 1395 :GNC 30 : allpos = palloc_array(WordEntryPos, totalpos);
1396 : : }
1397 : : else
1398 : : {
3680 teodor@sigaev.ru 1399 :UBC 0 : totalpos *= 2;
146 michael@paquier.xyz 1400 :UNC 0 : allpos = repalloc_array(allpos, WordEntryPos, totalpos);
1401 : : }
1402 : : }
1403 : :
3680 teodor@sigaev.ru 1404 :CBC 35 : memcpy(allpos + npos, data->pos, sizeof(WordEntryPos) * data->npos);
1405 : 35 : npos += data->npos;
1406 : :
1407 : : /* don't leak storage from individual matches */
2111 tgl@sss.pgh.pa.us 1408 [ + + ]: 35 : if (data->allocated)
1409 : 20 : pfree(data->pos);
1410 : 35 : data->pos = NULL;
1411 : 35 : data->allocated = false;
1412 : : /* it's important to reset data->npos before next loop */
1413 : 35 : data->npos = 0;
1414 : : }
1415 : : else
1416 : : {
1417 : : /* Don't need positions, just handle YES/MAYBE */
1418 [ - + - - ]: 9971 : if (subres == TS_YES || res == TS_NO)
1419 : 9971 : res = subres;
1420 : : }
1421 : : }
1422 : :
6563 1423 : 10056 : StopMiddle++;
1424 : : }
1425 : :
2111 1426 [ + + + - ]: 11040 : if (data && npos > 0)
1427 : : {
1428 : : /* Sort and make unique array of found positions */
3680 teodor@sigaev.ru 1429 : 30 : data->pos = allpos;
2371 tmunro@postgresql.or 1430 : 30 : qsort(data->pos, npos, sizeof(WordEntryPos), compareWordEntryPos);
1431 : 30 : data->npos = qunique(data->pos, npos, sizeof(WordEntryPos),
1432 : : compareWordEntryPos);
3680 teodor@sigaev.ru 1433 : 30 : data->allocated = true;
2111 tgl@sss.pgh.pa.us 1434 : 30 : res = TS_YES;
1435 : : }
1436 : : }
1437 : :
6172 bruce@momjian.us 1438 : 189554 : return res;
1439 : : }
1440 : :
1441 : : /*
1442 : : * Compute output position list for a tsquery operator in phrase mode.
1443 : : *
1444 : : * Merge the position lists in Ldata and Rdata as specified by "emit",
1445 : : * returning the result list into *data. The input position lists must be
1446 : : * sorted and unique, and the output will be as well.
1447 : : *
1448 : : * data: pointer to initially-all-zeroes output struct, or NULL
1449 : : * Ldata, Rdata: input position lists
1450 : : * emit: bitmask of TSPO_XXX flags
1451 : : * Loffset: offset to be added to Ldata positions before comparing/outputting
1452 : : * Roffset: offset to be added to Rdata positions before comparing/outputting
1453 : : * max_npos: maximum possible required size of output position array
1454 : : *
1455 : : * Loffset and Roffset should not be negative, else we risk trying to output
1456 : : * negative positions, which won't fit into WordEntryPos.
1457 : : *
1458 : : * The result is boolean (TS_YES or TS_NO), but for the caller's convenience
1459 : : * we return it as TSTernaryValue.
1460 : : *
1461 : : * Returns TS_YES if any positions were emitted to *data; or if data is NULL,
1462 : : * returns TS_YES if any positions would have been emitted.
1463 : : */
1464 : : #define TSPO_L_ONLY 0x01 /* emit positions appearing only in L */
1465 : : #define TSPO_R_ONLY 0x02 /* emit positions appearing only in R */
1466 : : #define TSPO_BOTH 0x04 /* emit positions appearing in both L&R */
1467 : :
1468 : : static TSTernaryValue
3422 tgl@sss.pgh.pa.us 1469 : 20113 : TS_phrase_output(ExecPhraseData *data,
1470 : : ExecPhraseData *Ldata,
1471 : : ExecPhraseData *Rdata,
1472 : : int emit,
1473 : : int Loffset,
1474 : : int Roffset,
1475 : : int max_npos)
1476 : : {
1477 : : int Lindex,
1478 : : Rindex;
1479 : :
1480 : : /* Loop until both inputs are exhausted */
1481 : 20113 : Lindex = Rindex = 0;
1482 [ + + + + ]: 20905 : while (Lindex < Ldata->npos || Rindex < Rdata->npos)
1483 : : {
1484 : : int Lpos,
1485 : : Rpos;
1486 : 1748 : int output_pos = 0;
1487 : :
1488 : : /*
1489 : : * Fetch current values to compare. WEP_GETPOS() is needed because
1490 : : * ExecPhraseData->data can point to a tsvector's WordEntryPosVector.
1491 : : */
1492 [ + + ]: 1748 : if (Lindex < Ldata->npos)
1493 : 1288 : Lpos = WEP_GETPOS(Ldata->pos[Lindex]) + Loffset;
1494 : : else
1495 : : {
1496 : : /* L array exhausted, so we're done if R_ONLY isn't set */
1497 [ + + ]: 460 : if (!(emit & TSPO_R_ONLY))
1498 : 113 : break;
1499 : 347 : Lpos = INT_MAX;
1500 : : }
1501 [ + + ]: 1635 : if (Rindex < Rdata->npos)
1502 : 1445 : Rpos = WEP_GETPOS(Rdata->pos[Rindex]) + Roffset;
1503 : : else
1504 : : {
1505 : : /* R array exhausted, so we're done if L_ONLY isn't set */
1506 [ + + ]: 190 : if (!(emit & TSPO_L_ONLY))
1507 : 122 : break;
1508 : 68 : Rpos = INT_MAX;
1509 : : }
1510 : :
1511 : : /* Merge-join the two input lists */
1512 [ + + ]: 1513 : if (Lpos < Rpos)
1513 : : {
1514 : : /* Lpos is not matched in Rdata, should we output it? */
1515 [ + + ]: 365 : if (emit & TSPO_L_ONLY)
1516 : 116 : output_pos = Lpos;
1517 : 365 : Lindex++;
1518 : : }
1519 [ + + ]: 1148 : else if (Lpos == Rpos)
1520 : : {
1521 : : /* Lpos and Rpos match ... should we output it? */
1522 [ + + ]: 621 : if (emit & TSPO_BOTH)
1523 : 553 : output_pos = Rpos;
1524 : 621 : Lindex++;
1525 : 621 : Rindex++;
1526 : : }
1527 : : else /* Lpos > Rpos */
1528 : : {
1529 : : /* Rpos is not matched in Ldata, should we output it? */
1530 [ + + ]: 527 : if (emit & TSPO_R_ONLY)
1531 : 376 : output_pos = Rpos;
1532 : 527 : Rindex++;
1533 : : }
1534 : :
1535 [ + + ]: 1513 : if (output_pos > 0)
1536 : : {
1537 [ + + ]: 1045 : if (data)
1538 : : {
1539 : : /* Store position, first allocating output array if needed */
1540 [ + + ]: 324 : if (data->pos == NULL)
1541 : : {
1542 : 261 : data->pos = (WordEntryPos *)
1543 : 261 : palloc(max_npos * sizeof(WordEntryPos));
1544 : 261 : data->allocated = true;
1545 : : }
1546 : 324 : data->pos[data->npos++] = output_pos;
1547 : : }
1548 : : else
1549 : : {
1550 : : /*
1551 : : * Exact positions not needed, so return TS_YES as soon as we
1552 : : * know there is at least one.
1553 : : */
2199 1554 : 721 : return TS_YES;
1555 : : }
1556 : : }
1557 : : }
1558 : :
3422 1559 [ + + + + ]: 19392 : if (data && data->npos > 0)
1560 : : {
1561 : : /* Let's assert we didn't overrun the array */
1562 [ - + ]: 261 : Assert(data->npos <= max_npos);
2199 1563 : 261 : return TS_YES;
1564 : : }
1565 : 19131 : return TS_NO;
1566 : : }
1567 : :
1568 : : /*
1569 : : * Execute tsquery at or below an OP_PHRASE operator.
1570 : : *
1571 : : * This handles tsquery execution at recursion levels where we need to care
1572 : : * about match locations.
1573 : : *
1574 : : * In addition to the same arguments used for TS_execute, the caller may pass
1575 : : * a preinitialized-to-zeroes ExecPhraseData struct, to be filled with lexeme
1576 : : * match position info on success. data == NULL if no position data need be
1577 : : * returned.
1578 : : * Note: the function assumes data != NULL for operators other than OP_PHRASE.
1579 : : * This is OK because an outside call always starts from an OP_PHRASE node,
1580 : : * and all internal recursion cases pass data != NULL.
1581 : : *
1582 : : * The detailed semantics of the match data, given that the function returned
1583 : : * TS_YES (successful match), are:
1584 : : *
1585 : : * npos > 0, negate = false:
1586 : : * query is matched at specified position(s) (and only those positions)
1587 : : * npos > 0, negate = true:
1588 : : * query is matched at all positions *except* specified position(s)
1589 : : * npos = 0, negate = true:
1590 : : * query is matched at all positions
1591 : : * npos = 0, negate = false:
1592 : : * disallowed (this should result in TS_NO or TS_MAYBE, as appropriate)
1593 : : *
1594 : : * Successful matches also return a "width" value which is the match width in
1595 : : * lexemes, less one. Hence, "width" is zero for simple one-lexeme matches,
1596 : : * and is the sum of the phrase operator distances for phrase matches. Note
1597 : : * that when width > 0, the listed positions represent the ends of matches not
1598 : : * the starts. (This unintuitive rule is needed to avoid possibly generating
1599 : : * negative positions, which wouldn't fit into the WordEntryPos arrays.)
1600 : : *
1601 : : * If the TSExecuteCallback function reports that an operand is present
1602 : : * but fails to provide position(s) for it, we will return TS_MAYBE when
1603 : : * it is possible but not certain that the query is matched.
1604 : : *
1605 : : * When the function returns TS_NO or TS_MAYBE, it must return npos = 0,
1606 : : * negate = false (which is the state initialized by the caller); but the
1607 : : * "width" output in such cases is undefined.
1608 : : */
1609 : : static TSTernaryValue
3427 1610 : 466432 : TS_phrase_execute(QueryItem *curitem, void *arg, uint32 flags,
1611 : : TSExecuteCallback chkcond,
1612 : : ExecPhraseData *data)
1613 : : {
1614 : : ExecPhraseData Ldata,
1615 : : Rdata;
1616 : : TSTernaryValue lmatch,
1617 : : rmatch;
1618 : : int Loffset,
1619 : : Roffset,
1620 : : maxwidth;
1621 : :
1622 : : /* since this function recurses, it could be driven to stack overflow */
3680 teodor@sigaev.ru 1623 : 466432 : check_stack_depth();
1624 : :
1625 : : /* ... and let's check for query cancel while we're at it */
1261 tgl@sss.pgh.pa.us 1626 [ - + ]: 466432 : CHECK_FOR_INTERRUPTS();
1627 : :
3680 teodor@sigaev.ru 1628 [ + + ]: 466432 : if (curitem->type == QI_VAL)
2111 tgl@sss.pgh.pa.us 1629 : 229451 : return chkcond(arg, (QueryOperand *) curitem, data);
1630 : :
3422 1631 [ + + + - ]: 236981 : switch (curitem->qoperator.oper)
1632 : : {
1633 : 80315 : case OP_NOT:
1634 : :
1635 : : /*
1636 : : * We need not touch data->width, since a NOT operation does not
1637 : : * change the match width.
1638 : : */
2111 1639 [ - + ]: 80315 : if (flags & TS_EXEC_SKIP_NOT)
1640 : : {
1641 : : /* with SKIP_NOT, report NOT as "match everywhere" */
3422 tgl@sss.pgh.pa.us 1642 [ # # # # ]:UBC 0 : Assert(data->npos == 0 && !data->negate);
1643 : 0 : data->negate = true;
2199 1644 : 0 : return TS_YES;
1645 : : }
2199 tgl@sss.pgh.pa.us 1646 [ + + + - ]:CBC 80315 : switch (TS_phrase_execute(curitem + 1, arg, flags, chkcond, data))
1647 : : {
1648 : 70189 : case TS_NO:
1649 : : /* change "match nowhere" to "match everywhere" */
1650 [ + - - + ]: 70189 : Assert(data->npos == 0 && !data->negate);
1651 : 70189 : data->negate = true;
1652 : 70189 : return TS_YES;
1653 : 273 : case TS_YES:
1654 [ + + ]: 273 : if (data->npos > 0)
1655 : : {
1656 : : /* we have some positions, invert negate flag */
1657 : 268 : data->negate = !data->negate;
1658 : 268 : return TS_YES;
1659 : : }
1660 [ + - ]: 5 : else if (data->negate)
1661 : : {
1662 : : /* change "match everywhere" to "match nowhere" */
1663 : 5 : data->negate = false;
1664 : 5 : return TS_NO;
1665 : : }
1666 : : /* Should not get here if result was TS_YES */
2199 tgl@sss.pgh.pa.us 1667 :UBC 0 : Assert(false);
1668 : : break;
2199 tgl@sss.pgh.pa.us 1669 :CBC 9853 : case TS_MAYBE:
1670 : : /* match positions are, and remain, uncertain */
1671 : 9853 : return TS_MAYBE;
1672 : : }
2199 tgl@sss.pgh.pa.us 1673 :UBC 0 : break;
1674 : :
3422 tgl@sss.pgh.pa.us 1675 :CBC 156540 : case OP_PHRASE:
1676 : : case OP_AND:
1677 : 156540 : memset(&Ldata, 0, sizeof(Ldata));
1678 : 156540 : memset(&Rdata, 0, sizeof(Rdata));
1679 : :
2199 1680 : 156540 : lmatch = TS_phrase_execute(curitem + curitem->qoperator.left,
1681 : : arg, flags, chkcond, &Ldata);
1682 [ + + ]: 156540 : if (lmatch == TS_NO)
1683 : 83545 : return TS_NO;
1684 : :
1685 : 72995 : rmatch = TS_phrase_execute(curitem + 1,
1686 : : arg, flags, chkcond, &Rdata);
1687 [ + + ]: 72995 : if (rmatch == TS_NO)
1688 : 35853 : return TS_NO;
1689 : :
1690 : : /*
1691 : : * If either operand has no position information, then we can't
1692 : : * return reliable position data, only a MAYBE result.
1693 : : */
1694 [ + + + + ]: 37142 : if (lmatch == TS_MAYBE || rmatch == TS_MAYBE)
1695 : 17155 : return TS_MAYBE;
1696 : :
3422 1697 [ + + ]: 19987 : if (curitem->qoperator.oper == OP_PHRASE)
1698 : : {
1699 : : /*
1700 : : * Compute Loffset and Roffset suitable for phrase match, and
1701 : : * compute overall width of whole phrase match.
1702 : : */
1703 : 19982 : Loffset = curitem->qoperator.distance + Rdata.width;
1704 : 19982 : Roffset = 0;
1705 [ + + ]: 19982 : if (data)
1706 : 155 : data->width = curitem->qoperator.distance +
1707 : 155 : Ldata.width + Rdata.width;
1708 : : }
1709 : : else
1710 : : {
1711 : : /*
1712 : : * For OP_AND, set output width and alignment like OP_OR (see
1713 : : * comment below)
1714 : : */
1715 : 5 : maxwidth = Max(Ldata.width, Rdata.width);
1716 : 5 : Loffset = maxwidth - Ldata.width;
1717 : 5 : Roffset = maxwidth - Rdata.width;
1718 [ + - ]: 5 : if (data)
1719 : 5 : data->width = maxwidth;
1720 : : }
1721 : :
1722 [ + + + + ]: 19987 : if (Ldata.negate && Rdata.negate)
1723 : : {
1724 : : /* !L & !R: treat as !(L | R) */
1725 : 18959 : (void) TS_phrase_output(data, &Ldata, &Rdata,
1726 : : TSPO_BOTH | TSPO_L_ONLY | TSPO_R_ONLY,
1727 : : Loffset, Roffset,
1728 : 18959 : Ldata.npos + Rdata.npos);
1729 [ - + ]: 18959 : if (data)
3422 tgl@sss.pgh.pa.us 1730 :UBC 0 : data->negate = true;
2199 tgl@sss.pgh.pa.us 1731 :CBC 18959 : return TS_YES;
1732 : : }
3422 1733 [ + + ]: 1028 : else if (Ldata.negate)
1734 : : {
1735 : : /* !L & R */
1736 : 309 : return TS_phrase_output(data, &Ldata, &Rdata,
1737 : : TSPO_R_ONLY,
1738 : : Loffset, Roffset,
1739 : : Rdata.npos);
1740 : : }
1741 [ + + ]: 719 : else if (Rdata.negate)
1742 : : {
1743 : : /* L & !R */
1744 : 5 : return TS_phrase_output(data, &Ldata, &Rdata,
1745 : : TSPO_L_ONLY,
1746 : : Loffset, Roffset,
1747 : : Ldata.npos);
1748 : : }
1749 : : else
1750 : : {
1751 : : /* straight AND */
1752 : 714 : return TS_phrase_output(data, &Ldata, &Rdata,
1753 : : TSPO_BOTH,
1754 : : Loffset, Roffset,
1755 : 714 : Min(Ldata.npos, Rdata.npos));
1756 : : }
1757 : :
1758 : 126 : case OP_OR:
1759 : 126 : memset(&Ldata, 0, sizeof(Ldata));
1760 : 126 : memset(&Rdata, 0, sizeof(Rdata));
1761 : :
1762 : 126 : lmatch = TS_phrase_execute(curitem + curitem->qoperator.left,
1763 : : arg, flags, chkcond, &Ldata);
1764 : 126 : rmatch = TS_phrase_execute(curitem + 1,
1765 : : arg, flags, chkcond, &Rdata);
1766 : :
2199 1767 [ + + + + ]: 126 : if (lmatch == TS_NO && rmatch == TS_NO)
1768 : 10 : return TS_NO;
1769 : :
1770 : : /*
1771 : : * If either operand has no position information, then we can't
1772 : : * return reliable position data, only a MAYBE result.
1773 : : */
1774 [ + - - + ]: 116 : if (lmatch == TS_MAYBE || rmatch == TS_MAYBE)
2199 tgl@sss.pgh.pa.us 1775 :UBC 0 : return TS_MAYBE;
1776 : :
1777 : : /*
1778 : : * Cope with undefined output width from failed submatch. (This
1779 : : * takes less code than trying to ensure that all failure returns
1780 : : * set data->width to zero.)
1781 : : */
2199 tgl@sss.pgh.pa.us 1782 [ + + ]:CBC 116 : if (lmatch == TS_NO)
3422 1783 : 15 : Ldata.width = 0;
2199 1784 [ + + ]: 116 : if (rmatch == TS_NO)
3422 1785 : 68 : Rdata.width = 0;
1786 : :
1787 : : /*
1788 : : * For OP_AND and OP_OR, report the width of the wider of the two
1789 : : * inputs, and align the narrower input's positions to the right
1790 : : * end of that width. This rule deals at least somewhat
1791 : : * reasonably with cases like "x <-> (y | z <-> q)".
1792 : : */
1793 : 116 : maxwidth = Max(Ldata.width, Rdata.width);
1794 : 116 : Loffset = maxwidth - Ldata.width;
1795 : 116 : Roffset = maxwidth - Rdata.width;
1796 : 116 : data->width = maxwidth;
1797 : :
1798 [ + + + + ]: 116 : if (Ldata.negate && Rdata.negate)
1799 : : {
1800 : : /* !L | !R: treat as !(L & R) */
1801 : 5 : (void) TS_phrase_output(data, &Ldata, &Rdata,
1802 : : TSPO_BOTH,
1803 : : Loffset, Roffset,
1804 : 5 : Min(Ldata.npos, Rdata.npos));
1805 : 5 : data->negate = true;
2199 1806 : 5 : return TS_YES;
1807 : : }
3422 1808 [ + + ]: 111 : else if (Ldata.negate)
1809 : : {
1810 : : /* !L | R: treat as !(L & !R) */
1811 : 25 : (void) TS_phrase_output(data, &Ldata, &Rdata,
1812 : : TSPO_L_ONLY,
1813 : : Loffset, Roffset,
1814 : : Ldata.npos);
1815 : 25 : data->negate = true;
2199 1816 : 25 : return TS_YES;
1817 : : }
3422 1818 [ + + ]: 86 : else if (Rdata.negate)
1819 : : {
1820 : : /* L | !R: treat as !(!L & R) */
1821 : 5 : (void) TS_phrase_output(data, &Ldata, &Rdata,
1822 : : TSPO_R_ONLY,
1823 : : Loffset, Roffset,
1824 : : Rdata.npos);
1825 : 5 : data->negate = true;
2199 1826 : 5 : return TS_YES;
1827 : : }
1828 : : else
1829 : : {
1830 : : /* straight OR */
3422 1831 : 81 : return TS_phrase_output(data, &Ldata, &Rdata,
1832 : : TSPO_BOTH | TSPO_L_ONLY | TSPO_R_ONLY,
1833 : : Loffset, Roffset,
1834 : 81 : Ldata.npos + Rdata.npos);
1835 : : }
1836 : :
3422 tgl@sss.pgh.pa.us 1837 :UBC 0 : default:
1838 [ # # ]: 0 : elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1839 : : }
1840 : :
1841 : : /* not reachable, but keep compiler quiet */
2199 1842 : 0 : return TS_NO;
1843 : : }
1844 : :
1845 : :
1846 : : /*
1847 : : * Evaluate tsquery boolean expression.
1848 : : *
1849 : : * curitem: current tsquery item (initially, the first one)
1850 : : * arg: opaque value to pass through to callback function
1851 : : * flags: bitmask of flag bits shown in ts_utils.h
1852 : : * chkcond: callback function to check whether a primitive value is present
1853 : : */
1854 : : bool
3427 tgl@sss.pgh.pa.us 1855 :CBC 346369 : TS_execute(QueryItem *curitem, void *arg, uint32 flags,
1856 : : TSExecuteCallback chkcond)
1857 : : {
1858 : : /*
1859 : : * If we get TS_MAYBE from the recursion, return true. We could only see
1860 : : * that result if the caller passed TS_EXEC_PHRASE_NO_POS, so there's no
1861 : : * need to check again.
1862 : : */
2199 1863 : 346369 : return TS_execute_recurse(curitem, arg, flags, chkcond) != TS_NO;
1864 : : }
1865 : :
1866 : : /*
1867 : : * Evaluate tsquery boolean expression.
1868 : : *
1869 : : * This is the same as TS_execute except that TS_MAYBE is returned as-is.
1870 : : */
1871 : : TSTernaryValue
1904 1872 : 24628 : TS_execute_ternary(QueryItem *curitem, void *arg, uint32 flags,
1873 : : TSExecuteCallback chkcond)
1874 : : {
1875 : 24628 : return TS_execute_recurse(curitem, arg, flags, chkcond);
1876 : : }
1877 : :
1878 : : /*
1879 : : * TS_execute recursion for operators above any phrase operator. Here we do
1880 : : * not need to worry about lexeme positions. As soon as we hit an OP_PHRASE
1881 : : * operator, we pass it off to TS_phrase_execute which does worry.
1882 : : */
1883 : : static TSTernaryValue
2199 1884 : 703087 : TS_execute_recurse(QueryItem *curitem, void *arg, uint32 flags,
1885 : : TSExecuteCallback chkcond)
1886 : : {
1887 : : TSTernaryValue lmatch;
1888 : :
1889 : : /* since this function recurses, it could be driven to stack overflow */
6822 1890 : 703087 : check_stack_depth();
1891 : :
1892 : : /* ... and let's check for query cancel while we're at it */
2104 1893 [ - + ]: 703087 : CHECK_FOR_INTERRUPTS();
1894 : :
6815 teodor@sigaev.ru 1895 [ + + ]: 703087 : if (curitem->type == QI_VAL)
3427 tgl@sss.pgh.pa.us 1896 : 282741 : return chkcond(arg, (QueryOperand *) curitem,
1897 : : NULL /* don't need position info */ );
1898 : :
6137 peter_e@gmx.net 1899 [ + + + + : 420346 : switch (curitem->qoperator.oper)
- ]
1900 : : {
6815 teodor@sigaev.ru 1901 : 135503 : case OP_NOT:
2111 tgl@sss.pgh.pa.us 1902 [ - + ]: 135503 : if (flags & TS_EXEC_SKIP_NOT)
2199 tgl@sss.pgh.pa.us 1903 :UBC 0 : return TS_YES;
2199 tgl@sss.pgh.pa.us 1904 [ + + + - ]:CBC 135503 : switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1905 : : {
1906 : 127848 : case TS_NO:
1907 : 127848 : return TS_YES;
1908 : 3264 : case TS_YES:
1909 : 3264 : return TS_NO;
1910 : 4391 : case TS_MAYBE:
1911 : 4391 : return TS_MAYBE;
1912 : : }
2199 tgl@sss.pgh.pa.us 1913 :UBC 0 : break;
1914 : :
6815 teodor@sigaev.ru 1915 :CBC 55903 : case OP_AND:
2199 tgl@sss.pgh.pa.us 1916 : 55903 : lmatch = TS_execute_recurse(curitem + curitem->qoperator.left, arg,
1917 : : flags, chkcond);
1918 [ + + ]: 55903 : if (lmatch == TS_NO)
1919 : 44395 : return TS_NO;
1920 [ + + + - ]: 11508 : switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1921 : : {
1922 : 6750 : case TS_NO:
1923 : 6750 : return TS_NO;
1924 : 2236 : case TS_YES:
1925 : 2236 : return lmatch;
1926 : 2522 : case TS_MAYBE:
1927 : 2522 : return TS_MAYBE;
1928 : : }
2199 tgl@sss.pgh.pa.us 1929 :UBC 0 : break;
1930 : :
6815 teodor@sigaev.ru 1931 :CBC 72660 : case OP_OR:
2199 tgl@sss.pgh.pa.us 1932 : 72660 : lmatch = TS_execute_recurse(curitem + curitem->qoperator.left, arg,
1933 : : flags, chkcond);
1934 [ + + ]: 72660 : if (lmatch == TS_YES)
1935 : 16144 : return TS_YES;
1936 [ + + + - ]: 56516 : switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1937 : : {
1938 : 38322 : case TS_NO:
1939 : 38322 : return lmatch;
1940 : 4962 : case TS_YES:
1941 : 4962 : return TS_YES;
1942 : 13232 : case TS_MAYBE:
1943 : 13232 : return TS_MAYBE;
1944 : : }
2199 tgl@sss.pgh.pa.us 1945 :UBC 0 : break;
1946 : :
3680 teodor@sigaev.ru 1947 :CBC 156280 : case OP_PHRASE:
1948 : :
1949 : : /*
1950 : : * If we get a MAYBE result, and the caller doesn't want that,
1951 : : * convert it to NO. It would be more consistent, perhaps, to
1952 : : * return the result of TS_phrase_execute() verbatim and then
1953 : : * convert MAYBE results at the top of the recursion. But
1954 : : * converting at the topmost phrase operator gives results that
1955 : : * are bug-compatible with the old implementation, so do it like
1956 : : * this for now.
1957 : : */
2199 tgl@sss.pgh.pa.us 1958 [ + + + - ]: 156280 : switch (TS_phrase_execute(curitem, arg, flags, chkcond, NULL))
1959 : : {
1960 : 119527 : case TS_NO:
1961 : 119527 : return TS_NO;
1962 : 19603 : case TS_YES:
1963 : 19603 : return TS_YES;
1964 : 17150 : case TS_MAYBE:
1965 : 17150 : return (flags & TS_EXEC_PHRASE_NO_POS) ? TS_MAYBE : TS_NO;
1966 : : }
2199 tgl@sss.pgh.pa.us 1967 :UBC 0 : break;
1968 : :
6815 teodor@sigaev.ru 1969 : 0 : default:
6137 peter_e@gmx.net 1970 [ # # ]: 0 : elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1971 : : }
1972 : :
1973 : : /* not reachable, but keep compiler quiet */
2199 tgl@sss.pgh.pa.us 1974 : 0 : return TS_NO;
1975 : : }
1976 : :
1977 : : /*
1978 : : * Evaluate tsquery and report locations of matching terms.
1979 : : *
1980 : : * This is like TS_execute except that it returns match locations not just
1981 : : * success/failure status. The callback function is required to provide
1982 : : * position data (we report failure if it doesn't).
1983 : : *
1984 : : * On successful match, the result is a List of ExecPhraseData structs, one
1985 : : * for each AND'ed term or phrase operator in the query. Each struct includes
1986 : : * a sorted array of lexeme positions matching that term. (Recall that for
1987 : : * phrase operators, the match includes width+1 lexemes, and the recorded
1988 : : * position is that of the rightmost lexeme.)
1989 : : *
1990 : : * OR subexpressions are handled by union'ing their match locations into a
1991 : : * single List element, which is valid since any of those locations contains
1992 : : * a match. However, when some of the OR'ed terms are phrase operators, we
1993 : : * report the maximum width of any of the OR'ed terms, making such cases
1994 : : * slightly imprecise in the conservative direction. (For example, if the
1995 : : * tsquery is "(A <-> B) | C", an occurrence of C in the data would be
1996 : : * reported as though it includes the lexeme to the left of C.)
1997 : : *
1998 : : * Locations of NOT subexpressions are not reported. (Obviously, there can
1999 : : * be no successful NOT matches at top level, or the match would have failed.
2000 : : * So this amounts to ignoring NOTs underneath ORs.)
2001 : : *
2002 : : * The result is NIL if no match, or if position data was not returned.
2003 : : *
2004 : : * Arguments are the same as for TS_execute, although flags is currently
2005 : : * vestigial since none of the defined bits are sensible here.
2006 : : */
2007 : : List *
1202 tgl@sss.pgh.pa.us 2008 :CBC 281 : TS_execute_locations(QueryItem *curitem, void *arg,
2009 : : uint32 flags,
2010 : : TSExecuteCallback chkcond)
2011 : : {
2012 : : List *result;
2013 : :
2014 : : /* No flags supported, as yet */
2015 [ - + ]: 281 : Assert(flags == TS_EXEC_EMPTY);
2016 [ + + ]: 281 : if (TS_execute_locations_recurse(curitem, arg, chkcond, &result))
2017 : 106 : return result;
2018 : 175 : return NIL;
2019 : : }
2020 : :
2021 : : /*
2022 : : * TS_execute_locations recursion for operators above any phrase operator.
2023 : : * OP_PHRASE subexpressions can be passed off to TS_phrase_execute.
2024 : : */
2025 : : static bool
2026 : 827 : TS_execute_locations_recurse(QueryItem *curitem, void *arg,
2027 : : TSExecuteCallback chkcond,
2028 : : List **locations)
2029 : : {
2030 : : bool lmatch,
2031 : : rmatch;
2032 : : List *llocations,
2033 : : *rlocations;
2034 : : ExecPhraseData *data;
2035 : :
2036 : : /* since this function recurses, it could be driven to stack overflow */
2037 : 827 : check_stack_depth();
2038 : :
2039 : : /* ... and let's check for query cancel while we're at it */
2040 [ - + ]: 827 : CHECK_FOR_INTERRUPTS();
2041 : :
2042 : : /* Default locations result is empty */
2043 : 827 : *locations = NIL;
2044 : :
2045 [ + + ]: 827 : if (curitem->type == QI_VAL)
2046 : : {
2047 : 347 : data = palloc0_object(ExecPhraseData);
2048 [ + + ]: 347 : if (chkcond(arg, (QueryOperand *) curitem, data) == TS_YES)
2049 : : {
2050 : 172 : *locations = list_make1(data);
2051 : 172 : return true;
2052 : : }
2053 : 175 : pfree(data);
2054 : 175 : return false;
2055 : : }
2056 : :
2057 [ + + + + : 480 : switch (curitem->qoperator.oper)
- ]
2058 : : {
2059 : 10 : case OP_NOT:
2060 [ - + ]: 10 : if (!TS_execute_locations_recurse(curitem + 1, arg, chkcond,
2061 : : &llocations))
1202 tgl@sss.pgh.pa.us 2062 :UBC 0 : return true; /* we don't pass back any locations */
1202 tgl@sss.pgh.pa.us 2063 :CBC 10 : return false;
2064 : :
2065 : 400 : case OP_AND:
2066 [ + + ]: 400 : if (!TS_execute_locations_recurse(curitem + curitem->qoperator.left,
2067 : : arg, chkcond,
2068 : : &llocations))
2069 : 304 : return false;
2070 [ + + ]: 96 : if (!TS_execute_locations_recurse(curitem + 1,
2071 : : arg, chkcond,
2072 : : &rlocations))
2073 : 41 : return false;
2074 : 55 : *locations = list_concat(llocations, rlocations);
2075 : 55 : return true;
2076 : :
2077 : 20 : case OP_OR:
2078 : 20 : lmatch = TS_execute_locations_recurse(curitem + curitem->qoperator.left,
2079 : : arg, chkcond,
2080 : : &llocations);
2081 : 20 : rmatch = TS_execute_locations_recurse(curitem + 1,
2082 : : arg, chkcond,
2083 : : &rlocations);
2084 [ - + - - ]: 20 : if (lmatch || rmatch)
2085 : : {
2086 : : /*
2087 : : * We generate an AND'able location struct from each
2088 : : * combination of sub-matches, following the disjunctive law
2089 : : * (A & B) | (C & D) = (A | C) & (A | D) & (B | C) & (B | D).
2090 : : *
2091 : : * However, if either input didn't produce locations (i.e., it
2092 : : * failed or was a NOT), we must just return the other list.
2093 : : */
2094 [ - + ]: 20 : if (llocations == NIL)
1202 tgl@sss.pgh.pa.us 2095 :UBC 0 : *locations = rlocations;
1202 tgl@sss.pgh.pa.us 2096 [ + + ]:CBC 20 : else if (rlocations == NIL)
2097 : 10 : *locations = llocations;
2098 : : else
2099 : : {
2100 : : ListCell *ll;
2101 : :
2102 [ + - + + : 20 : foreach(ll, llocations)
+ + ]
2103 : : {
2104 : 10 : ExecPhraseData *ldata = (ExecPhraseData *) lfirst(ll);
2105 : : ListCell *lr;
2106 : :
2107 [ + - + + : 20 : foreach(lr, rlocations)
+ + ]
2108 : : {
2109 : 10 : ExecPhraseData *rdata = (ExecPhraseData *) lfirst(lr);
2110 : :
2111 : 10 : data = palloc0_object(ExecPhraseData);
2112 : 10 : (void) TS_phrase_output(data, ldata, rdata,
2113 : : TSPO_BOTH | TSPO_L_ONLY | TSPO_R_ONLY,
2114 : : 0, 0,
2115 : 10 : ldata->npos + rdata->npos);
2116 : : /* Report the larger width, as explained above. */
2117 : 10 : data->width = Max(ldata->width, rdata->width);
2118 : 10 : *locations = lappend(*locations, data);
2119 : : }
2120 : : }
2121 : : }
2122 : :
2123 : 20 : return true;
2124 : : }
1202 tgl@sss.pgh.pa.us 2125 :UBC 0 : return false;
2126 : :
1202 tgl@sss.pgh.pa.us 2127 :CBC 50 : case OP_PHRASE:
2128 : : /* We can hand this off to TS_phrase_execute */
2129 : 50 : data = palloc0_object(ExecPhraseData);
2130 [ + - ]: 50 : if (TS_phrase_execute(curitem, arg, TS_EXEC_EMPTY, chkcond,
2131 : : data) == TS_YES)
2132 : : {
2133 [ + - ]: 50 : if (!data->negate)
2134 : 50 : *locations = list_make1(data);
2135 : 50 : return true;
2136 : : }
1202 tgl@sss.pgh.pa.us 2137 :UBC 0 : pfree(data);
2138 : 0 : return false;
2139 : :
2140 : 0 : default:
2141 [ # # ]: 0 : elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
2142 : : }
2143 : :
2144 : : /* not reachable, but keep compiler quiet */
2145 : : return false;
2146 : : }
2147 : :
2148 : : /*
2149 : : * Detect whether a tsquery boolean expression requires any positive matches
2150 : : * to values shown in the tsquery.
2151 : : *
2152 : : * This is needed to know whether a GIN index search requires full index scan.
2153 : : * For example, 'x & !y' requires a match of x, so it's sufficient to scan
2154 : : * entries for x; but 'x | !y' could match rows containing neither x nor y.
2155 : : */
2156 : : bool
5595 tgl@sss.pgh.pa.us 2157 :CBC 631 : tsquery_requires_match(QueryItem *curitem)
2158 : : {
2159 : : /* since this function recurses, it could be driven to stack overflow */
2160 : 631 : check_stack_depth();
2161 : :
2162 [ + + ]: 631 : if (curitem->type == QI_VAL)
2163 : 301 : return true;
2164 : :
2165 [ + + + - ]: 330 : switch (curitem->qoperator.oper)
2166 : : {
2167 : 127 : case OP_NOT:
2168 : :
2169 : : /*
2170 : : * Assume there are no required matches underneath a NOT. For
2171 : : * some cases with nested NOTs, we could prove there's a required
2172 : : * match, but it seems unlikely to be worth the trouble.
2173 : : */
2174 : 127 : return false;
2175 : :
3680 teodor@sigaev.ru 2176 : 153 : case OP_PHRASE:
2177 : :
2178 : : /*
2179 : : * Treat OP_PHRASE as OP_AND here
2180 : : */
2181 : : case OP_AND:
2182 : : /* If either side requires a match, we're good */
5595 tgl@sss.pgh.pa.us 2183 [ + + ]: 153 : if (tsquery_requires_match(curitem + curitem->qoperator.left))
2184 : 117 : return true;
2185 : : else
2186 : 36 : return tsquery_requires_match(curitem + 1);
2187 : :
2188 : 50 : case OP_OR:
2189 : : /* Both sides must require a match */
2190 [ + - ]: 50 : if (tsquery_requires_match(curitem + curitem->qoperator.left))
2191 : 50 : return tsquery_requires_match(curitem + 1);
2192 : : else
5595 tgl@sss.pgh.pa.us 2193 :UBC 0 : return false;
2194 : :
2195 : 0 : default:
2196 [ # # ]: 0 : elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
2197 : : }
2198 : :
2199 : : /* not reachable, but keep compiler quiet */
2200 : : return false;
2201 : : }
2202 : :
2203 : : /*
2204 : : * boolean operations
2205 : : */
2206 : : Datum
6832 tgl@sss.pgh.pa.us 2207 :CBC 40 : ts_match_qv(PG_FUNCTION_ARGS)
2208 : : {
2209 : 40 : PG_RETURN_DATUM(DirectFunctionCall2(ts_match_vq,
2210 : : PG_GETARG_DATUM(1),
2211 : : PG_GETARG_DATUM(0)));
2212 : : }
2213 : :
2214 : : Datum
2215 : 146808 : ts_match_vq(PG_FUNCTION_ARGS)
2216 : : {
2217 : 146808 : TSVector val = PG_GETARG_TSVECTOR(0);
2218 : 146808 : TSQuery query = PG_GETARG_TSQUERY(1);
2219 : : CHKVAL chkval;
2220 : : bool result;
2221 : :
2222 : : /* empty query matches nothing */
3386 2223 [ - + ]: 146808 : if (!query->size)
2224 : : {
6832 tgl@sss.pgh.pa.us 2225 [ # # ]:UBC 0 : PG_FREE_IF_COPY(val, 0);
2226 [ # # ]: 0 : PG_FREE_IF_COPY(query, 1);
2227 : 0 : PG_RETURN_BOOL(false);
2228 : : }
2229 : :
6832 tgl@sss.pgh.pa.us 2230 :CBC 146808 : chkval.arrb = ARRPTR(val);
2231 : 146808 : chkval.arre = chkval.arrb + val->size;
2232 : 146808 : chkval.values = STRPTR(val);
2233 : 146808 : chkval.operand = GETOPERAND(query);
3427 2234 : 146808 : result = TS_execute(GETQUERY(query),
2235 : : &chkval,
2236 : : TS_EXEC_EMPTY,
2237 : : checkcondition_str);
2238 : :
6832 2239 [ + + ]: 146808 : PG_FREE_IF_COPY(val, 0);
2240 [ - + ]: 146808 : PG_FREE_IF_COPY(query, 1);
2241 : 146808 : PG_RETURN_BOOL(result);
2242 : : }
2243 : :
2244 : : Datum
6832 tgl@sss.pgh.pa.us 2245 :UBC 0 : ts_match_tt(PG_FUNCTION_ARGS)
2246 : : {
2247 : : TSVector vector;
2248 : : TSQuery query;
2249 : : bool res;
2250 : :
2251 : 0 : vector = DatumGetTSVector(DirectFunctionCall1(to_tsvector,
2252 : : PG_GETARG_DATUM(0)));
2253 : 0 : query = DatumGetTSQuery(DirectFunctionCall1(plainto_tsquery,
2254 : : PG_GETARG_DATUM(1)));
2255 : :
2256 : 0 : res = DatumGetBool(DirectFunctionCall2(ts_match_vq,
2257 : : TSVectorGetDatum(vector),
2258 : : TSQueryGetDatum(query)));
2259 : :
2260 : 0 : pfree(vector);
2261 : 0 : pfree(query);
2262 : :
2263 : 0 : PG_RETURN_BOOL(res);
2264 : : }
2265 : :
2266 : : Datum
2267 : 0 : ts_match_tq(PG_FUNCTION_ARGS)
2268 : : {
2269 : : TSVector vector;
2270 : 0 : TSQuery query = PG_GETARG_TSQUERY(1);
2271 : : bool res;
2272 : :
2273 : 0 : vector = DatumGetTSVector(DirectFunctionCall1(to_tsvector,
2274 : : PG_GETARG_DATUM(0)));
2275 : :
2276 : 0 : res = DatumGetBool(DirectFunctionCall2(ts_match_vq,
2277 : : TSVectorGetDatum(vector),
2278 : : TSQueryGetDatum(query)));
2279 : :
2280 : 0 : pfree(vector);
2281 [ # # ]: 0 : PG_FREE_IF_COPY(query, 1);
2282 : :
2283 : 0 : PG_RETURN_BOOL(res);
2284 : : }
2285 : :
2286 : : /*
2287 : : * ts_stat statistic function support
2288 : : */
2289 : :
2290 : :
2291 : : /*
2292 : : * Returns the number of positions in value 'wptr' within tsvector 'txt',
2293 : : * that have a weight equal to one of the weights in 'weight' bitmask.
2294 : : */
2295 : : static int
6746 bruce@momjian.us 2296 :CBC 5452 : check_weight(TSVector txt, WordEntry *wptr, int8 weight)
2297 : : {
6832 tgl@sss.pgh.pa.us 2298 [ + - ]: 5452 : int len = POSDATALEN(txt, wptr);
2299 : 5452 : int num = 0;
2300 : 5452 : WordEntryPos *ptr = POSDATAPTR(txt, wptr);
2301 : :
2302 [ + + ]: 11100 : while (len--)
2303 : : {
2304 [ + + ]: 5648 : if (weight & (1 << WEP_GETWEIGHT(*ptr)))
2305 : 8 : num++;
2306 : 5648 : ptr++;
2307 : : }
2308 : 5452 : return num;
2309 : : }
2310 : :
2311 : : #define compareStatWord(a,e,t) \
2312 : : tsCompareString((a)->lexeme, (a)->lenlexeme, \
2313 : : STRPTR(t) + (e)->pos, (e)->len, \
2314 : : false)
2315 : :
2316 : : static void
6378 teodor@sigaev.ru 2317 : 230416 : insertStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 off)
2318 : : {
6172 bruce@momjian.us 2319 : 230416 : WordEntry *we = ARRPTR(txt) + off;
2320 : 230416 : StatEntry *node = stat->root,
2321 : 230416 : *pnode = NULL;
2322 : : int n,
6376 teodor@sigaev.ru 2323 : 230416 : res = 0;
6172 bruce@momjian.us 2324 : 230416 : uint32 depth = 1;
2325 : :
2326 [ + + ]: 230416 : if (stat->weight == 0)
6378 teodor@sigaev.ru 2327 [ + + ]: 115208 : n = (we->haspos) ? POSDATALEN(txt, we) : 1;
2328 : : else
2329 [ + + ]: 115208 : n = (we->haspos) ? check_weight(txt, we, stat->weight) : 0;
2330 : :
6172 bruce@momjian.us 2331 [ + + ]: 230416 : if (n == 0)
2332 : 115204 : return; /* nothing to insert */
2333 : :
2334 [ + + ]: 1163592 : while (node)
2335 : : {
6378 teodor@sigaev.ru 2336 : 1159016 : res = compareStatWord(node, we, txt);
2337 : :
2338 [ + + ]: 1159016 : if (res == 0)
2339 : : {
2340 : 110636 : break;
2341 : : }
2342 : : else
2343 : : {
2344 : 1048380 : pnode = node;
6172 bruce@momjian.us 2345 [ + + ]: 1048380 : node = (res < 0) ? node->left : node->right;
2346 : : }
6378 teodor@sigaev.ru 2347 : 1048380 : depth++;
2348 : : }
2349 : :
2350 [ + + ]: 115212 : if (depth > stat->maxdepth)
2351 : 84 : stat->maxdepth = depth;
2352 : :
2353 [ + + ]: 115212 : if (node == NULL)
2354 : : {
6172 bruce@momjian.us 2355 : 4576 : node = MemoryContextAlloc(persistentContext, STATENTRYHDRSZ + we->len);
6378 teodor@sigaev.ru 2356 : 4576 : node->left = node->right = NULL;
2357 : 4576 : node->ndoc = 1;
2358 : 4576 : node->nentry = n;
2359 : 4576 : node->lenlexeme = we->len;
2360 : 4576 : memcpy(node->lexeme, STRPTR(txt) + we->pos, node->lenlexeme);
2361 : :
6172 bruce@momjian.us 2362 [ + + ]: 4576 : if (pnode == NULL)
2363 : : {
6378 teodor@sigaev.ru 2364 : 8 : stat->root = node;
2365 : : }
2366 : : else
2367 : : {
2368 [ + + ]: 4568 : if (res < 0)
2369 : 2254 : pnode->left = node;
2370 : : else
2371 : 2314 : pnode->right = node;
2372 : : }
2373 : : }
2374 : : else
2375 : : {
2376 : 110636 : node->ndoc++;
2377 : 110636 : node->nentry += n;
2378 : : }
2379 : : }
2380 : :
2381 : : static void
6172 bruce@momjian.us 2382 : 330256 : chooseNextStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt,
2383 : : uint32 low, uint32 high, uint32 offset)
2384 : : {
2385 : : uint32 pos;
2386 : 330256 : uint32 middle = (low + high) >> 1;
2387 : :
6378 teodor@sigaev.ru 2388 : 330256 : pos = (low + middle) >> 1;
2389 [ + + + + : 330256 : if (low != middle && pos >= offset && pos - offset < txt->size)
+ + ]
6172 bruce@momjian.us 2390 : 113552 : insertStatEntry(persistentContext, stat, txt, pos - offset);
6378 teodor@sigaev.ru 2391 : 330256 : pos = (high + middle + 1) >> 1;
2392 [ + + + + : 330256 : if (middle + 1 != high && pos >= offset && pos - offset < txt->size)
+ + ]
6172 bruce@momjian.us 2393 : 112856 : insertStatEntry(persistentContext, stat, txt, pos - offset);
2394 : :
6378 teodor@sigaev.ru 2395 [ + + ]: 330256 : if (low != middle)
2396 : 165128 : chooseNextStatEntry(persistentContext, stat, txt, low, middle, offset);
2397 [ + + ]: 330256 : if (high != middle + 1)
2398 : 161120 : chooseNextStatEntry(persistentContext, stat, txt, middle + 1, high, offset);
6832 tgl@sss.pgh.pa.us 2399 : 330256 : }
2400 : :
2401 : : /*
2402 : : * This is written like a custom aggregate function, because the
2403 : : * original plan was to do just that. Unfortunately, an aggregate function
2404 : : * can't return a set, so that plan was abandoned. If that limitation is
2405 : : * lifted in the future, ts_stat could be a real aggregate function so that
2406 : : * you could use it like this:
2407 : : *
2408 : : * SELECT ts_stat(vector_column) FROM vector_table;
2409 : : *
2410 : : * where vector_column is a tsvector-type column in vector_table.
2411 : : */
2412 : :
2413 : : static TSVectorStat *
6378 teodor@sigaev.ru 2414 : 4072 : ts_accum(MemoryContext persistentContext, TSVectorStat *stat, Datum data)
2415 : : {
6172 bruce@momjian.us 2416 : 4072 : TSVector txt = DatumGetTSVector(data);
2417 : : uint32 i,
2418 : 4072 : nbit = 0,
2419 : : offset;
2420 : :
6832 tgl@sss.pgh.pa.us 2421 [ - + ]: 4072 : if (stat == NULL)
2422 : : { /* Init in first */
6378 teodor@sigaev.ru 2423 :UBC 0 : stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2424 : 0 : stat->maxdepth = 1;
2425 : : }
2426 : :
2427 : : /* simple check of correctness */
6832 tgl@sss.pgh.pa.us 2428 [ + - + + ]:CBC 4072 : if (txt == NULL || txt->size == 0)
2429 : : {
6378 teodor@sigaev.ru 2430 [ + - + - ]: 64 : if (txt && txt != (TSVector) DatumGetPointer(data))
6832 tgl@sss.pgh.pa.us 2431 : 64 : pfree(txt);
2432 : 64 : return stat;
2433 : : }
2434 : :
6378 teodor@sigaev.ru 2435 : 4008 : i = txt->size - 1;
2436 [ + + ]: 28480 : for (; i > 0; i >>= 1)
2437 : 24472 : nbit++;
2438 : :
2439 : 4008 : nbit = 1 << nbit;
2440 : 4008 : offset = (nbit - txt->size) / 2;
2441 : :
6172 bruce@momjian.us 2442 : 4008 : insertStatEntry(persistentContext, stat, txt, (nbit >> 1) - offset);
6378 teodor@sigaev.ru 2443 : 4008 : chooseNextStatEntry(persistentContext, stat, txt, 0, nbit, offset);
2444 : :
2445 : 4008 : return stat;
2446 : : }
2447 : :
2448 : : static void
6832 tgl@sss.pgh.pa.us 2449 : 8 : ts_setup_firstcall(FunctionCallInfo fcinfo, FuncCallContext *funcctx,
2450 : : TSVectorStat *stat)
2451 : : {
2452 : : TupleDesc tupdesc;
2453 : : MemoryContext oldcontext;
2454 : : StatEntry *node;
2455 : :
523 peter@eisentraut.org 2456 : 8 : funcctx->user_fctx = stat;
2457 : :
6832 tgl@sss.pgh.pa.us 2458 : 8 : oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2459 : :
146 michael@paquier.xyz 2460 :GNC 8 : stat->stack = palloc0_array(StatEntry *, stat->maxdepth + 1);
6172 bruce@momjian.us 2461 :CBC 8 : stat->stackpos = 0;
2462 : :
6378 teodor@sigaev.ru 2463 : 8 : node = stat->root;
2464 : : /* find leftmost value */
6048 tgl@sss.pgh.pa.us 2465 [ + - ]: 8 : if (node == NULL)
6048 tgl@sss.pgh.pa.us 2466 :UBC 0 : stat->stack[stat->stackpos] = NULL;
2467 : : else
2468 : : for (;;)
2469 : : {
6048 tgl@sss.pgh.pa.us 2470 :CBC 32 : stat->stack[stat->stackpos] = node;
2471 [ + + ]: 32 : if (node->left)
2472 : : {
2473 : 24 : stat->stackpos++;
2474 : 24 : node = node->left;
2475 : : }
2476 : : else
2477 : 8 : break;
2478 : : }
2479 [ - + ]: 8 : Assert(stat->stackpos <= stat->maxdepth);
2480 : :
1231 michael@paquier.xyz 2481 [ - + ]: 8 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1231 michael@paquier.xyz 2482 [ # # ]:UBC 0 : elog(ERROR, "return type must be a row type");
1231 michael@paquier.xyz 2483 :CBC 8 : funcctx->tuple_desc = tupdesc;
6832 tgl@sss.pgh.pa.us 2484 : 8 : funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
2485 : :
2486 : 8 : MemoryContextSwitchTo(oldcontext);
2487 : 8 : }
2488 : :
2489 : : static StatEntry *
6172 bruce@momjian.us 2490 : 9152 : walkStatEntryTree(TSVectorStat *stat)
2491 : : {
2492 : 9152 : StatEntry *node = stat->stack[stat->stackpos];
2493 : :
2494 [ - + ]: 9152 : if (node == NULL)
6378 teodor@sigaev.ru 2495 :UBC 0 : return NULL;
2496 : :
6172 bruce@momjian.us 2497 [ + + ]:CBC 9152 : if (node->ndoc != 0)
2498 : : {
2499 : : /* return entry itself: we already was at left sublink */
6378 teodor@sigaev.ru 2500 : 2262 : return node;
2501 : : }
2502 [ + + + + ]: 6890 : else if (node->right && node->right != stat->stack[stat->stackpos + 1])
2503 : : {
2504 : : /* go on right sublink */
2505 : 2314 : stat->stackpos++;
2506 : 2314 : node = node->right;
2507 : :
2508 : : /* find most-left value */
2509 : : for (;;)
2510 : : {
2511 : 4544 : stat->stack[stat->stackpos] = node;
2512 [ + + ]: 4544 : if (node->left)
2513 : : {
2514 : 2230 : stat->stackpos++;
2515 : 2230 : node = node->left;
2516 : : }
2517 : : else
2518 : 2314 : break;
2519 : : }
6048 tgl@sss.pgh.pa.us 2520 [ - + ]: 2314 : Assert(stat->stackpos <= stat->maxdepth);
2521 : : }
2522 : : else
2523 : : {
2524 : : /* we already return all left subtree, itself and right subtree */
6378 teodor@sigaev.ru 2525 [ + + ]: 4576 : if (stat->stackpos == 0)
2526 : 8 : return NULL;
2527 : :
2528 : 4568 : stat->stackpos--;
2529 : 4568 : return walkStatEntryTree(stat);
2530 : : }
2531 : :
2532 : 2314 : return node;
2533 : : }
2534 : :
2535 : : static Datum
6832 tgl@sss.pgh.pa.us 2536 : 4584 : ts_process_call(FuncCallContext *funcctx)
2537 : : {
2538 : : TSVectorStat *st;
2539 : : StatEntry *entry;
2540 : :
6378 teodor@sigaev.ru 2541 : 4584 : st = (TSVectorStat *) funcctx->user_fctx;
2542 : :
2543 : 4584 : entry = walkStatEntryTree(st);
2544 : :
2545 [ + + ]: 4584 : if (entry != NULL)
2546 : : {
2547 : : Datum result;
2548 : : char *values[3];
2549 : : char ndoc[16];
2550 : : char nentry[16];
2551 : : HeapTuple tuple;
2552 : :
2553 : 4576 : values[0] = palloc(entry->lenlexeme + 1);
2554 : 4576 : memcpy(values[0], entry->lexeme, entry->lenlexeme);
2555 : 4576 : (values[0])[entry->lenlexeme] = '\0';
6832 tgl@sss.pgh.pa.us 2556 : 4576 : sprintf(ndoc, "%d", entry->ndoc);
2557 : 4576 : values[1] = ndoc;
2558 : 4576 : sprintf(nentry, "%d", entry->nentry);
2559 : 4576 : values[2] = nentry;
2560 : :
2561 : 4576 : tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
2562 : 4576 : result = HeapTupleGetDatum(tuple);
2563 : :
2564 : 4576 : pfree(values[0]);
2565 : :
2566 : : /* mark entry as already visited */
6378 teodor@sigaev.ru 2567 : 4576 : entry->ndoc = 0;
2568 : :
6832 tgl@sss.pgh.pa.us 2569 : 4576 : return result;
2570 : : }
2571 : :
2572 : 8 : return (Datum) 0;
2573 : : }
2574 : :
2575 : : static TSVectorStat *
6378 teodor@sigaev.ru 2576 : 8 : ts_stat_sql(MemoryContext persistentContext, text *txt, text *ws)
2577 : : {
6615 tgl@sss.pgh.pa.us 2578 : 8 : char *query = text_to_cstring(txt);
2579 : : TSVectorStat *stat;
2580 : : bool isnull;
2581 : : Portal portal;
2582 : : SPIPlanPtr plan;
2583 : :
6832 2584 [ - + ]: 8 : if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2585 : : /* internal error */
6832 tgl@sss.pgh.pa.us 2586 [ # # ]:UBC 0 : elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2587 : :
6768 tgl@sss.pgh.pa.us 2588 [ - + ]:CBC 8 : if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2589 : : /* internal error */
6832 tgl@sss.pgh.pa.us 2590 [ # # ]:UBC 0 : elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2591 : :
6832 tgl@sss.pgh.pa.us 2592 :CBC 8 : SPI_cursor_fetch(portal, true, 100);
2593 : :
6768 2594 [ + - ]: 8 : if (SPI_tuptable == NULL ||
2595 [ + - ]: 8 : SPI_tuptable->tupdesc->natts != 1 ||
3883 teodor@sigaev.ru 2596 [ - + ]: 8 : !IsBinaryCoercible(SPI_gettypeid(SPI_tuptable->tupdesc, 1),
2597 : : TSVECTOROID))
6832 tgl@sss.pgh.pa.us 2598 [ # # ]:UBC 0 : ereport(ERROR,
2599 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2600 : : errmsg("ts_stat query must return one tsvector column")));
2601 : :
6378 teodor@sigaev.ru 2602 :CBC 8 : stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2603 : 8 : stat->maxdepth = 1;
2604 : :
6832 tgl@sss.pgh.pa.us 2605 [ + + ]: 8 : if (ws)
2606 : : {
2607 : : char *buf;
2608 : : const char *end;
2609 : :
3341 noah@leadboat.com 2610 [ - + ]: 4 : buf = VARDATA_ANY(ws);
118 tmunro@postgresql.or 2611 [ - + - - : 4 : end = buf + VARSIZE_ANY_EXHDR(ws);
- - - - -
+ ]
2612 [ + + ]: 12 : while (buf < end)
2613 : : {
2614 : 8 : int len = pg_mblen_range(buf, end);
2615 : :
2616 [ + - ]: 8 : if (len == 1)
2617 : : {
6832 tgl@sss.pgh.pa.us 2618 [ + + - - : 8 : switch (*buf)
- ]
2619 : : {
2620 : 4 : case 'A':
2621 : : case 'a':
2622 : 4 : stat->weight |= 1 << 3;
2623 : 4 : break;
2624 : 4 : case 'B':
2625 : : case 'b':
2626 : 4 : stat->weight |= 1 << 2;
2627 : 4 : break;
6832 tgl@sss.pgh.pa.us 2628 :UBC 0 : case 'C':
2629 : : case 'c':
2630 : 0 : stat->weight |= 1 << 1;
2631 : 0 : break;
2632 : 0 : case 'D':
2633 : : case 'd':
2634 : 0 : stat->weight |= 1;
2635 : 0 : break;
2636 : 0 : default:
2637 : 0 : stat->weight |= 0;
2638 : : }
2639 : : }
118 tmunro@postgresql.or 2640 :CBC 8 : buf += len;
2641 : : }
2642 : : }
2643 : :
6832 tgl@sss.pgh.pa.us 2644 [ + + ]: 56 : while (SPI_processed > 0)
2645 : : {
2646 : : uint64 i;
2647 : :
2648 [ + + ]: 4120 : for (i = 0; i < SPI_processed; i++)
2649 : : {
2650 : 4072 : Datum data = SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull);
2651 : :
2652 [ + - ]: 4072 : if (!isnull)
6378 teodor@sigaev.ru 2653 : 4072 : stat = ts_accum(persistentContext, stat, data);
2654 : : }
2655 : :
6832 tgl@sss.pgh.pa.us 2656 : 48 : SPI_freetuptable(SPI_tuptable);
2657 : 48 : SPI_cursor_fetch(portal, true, 100);
2658 : : }
2659 : :
2660 : 8 : SPI_freetuptable(SPI_tuptable);
2661 : 8 : SPI_cursor_close(portal);
2662 : 8 : SPI_freeplan(plan);
2663 : 8 : pfree(query);
2664 : :
2665 : 8 : return stat;
2666 : : }
2667 : :
2668 : : Datum
2669 : 4576 : ts_stat1(PG_FUNCTION_ARGS)
2670 : : {
2671 : : FuncCallContext *funcctx;
2672 : : Datum result;
2673 : :
2674 [ + + ]: 4576 : if (SRF_IS_FIRSTCALL())
2675 : : {
2676 : : TSVectorStat *stat;
3341 noah@leadboat.com 2677 : 4 : text *txt = PG_GETARG_TEXT_PP(0);
2678 : :
6832 tgl@sss.pgh.pa.us 2679 : 4 : funcctx = SRF_FIRSTCALL_INIT();
2680 : 4 : SPI_connect();
6378 teodor@sigaev.ru 2681 : 4 : stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, NULL);
6832 tgl@sss.pgh.pa.us 2682 [ - + ]: 4 : PG_FREE_IF_COPY(txt, 0);
2683 : 4 : ts_setup_firstcall(fcinfo, funcctx, stat);
2684 : 4 : SPI_finish();
2685 : : }
2686 : :
2687 : 4576 : funcctx = SRF_PERCALL_SETUP();
2688 [ + + ]: 4576 : if ((result = ts_process_call(funcctx)) != (Datum) 0)
2689 : 4572 : SRF_RETURN_NEXT(funcctx, result);
2690 : 4 : SRF_RETURN_DONE(funcctx);
2691 : : }
2692 : :
2693 : : Datum
2694 : 8 : ts_stat2(PG_FUNCTION_ARGS)
2695 : : {
2696 : : FuncCallContext *funcctx;
2697 : : Datum result;
2698 : :
2699 [ + + ]: 8 : if (SRF_IS_FIRSTCALL())
2700 : : {
2701 : : TSVectorStat *stat;
3341 noah@leadboat.com 2702 : 4 : text *txt = PG_GETARG_TEXT_PP(0);
2703 : 4 : text *ws = PG_GETARG_TEXT_PP(1);
2704 : :
6832 tgl@sss.pgh.pa.us 2705 : 4 : funcctx = SRF_FIRSTCALL_INIT();
2706 : 4 : SPI_connect();
6378 teodor@sigaev.ru 2707 : 4 : stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, ws);
6832 tgl@sss.pgh.pa.us 2708 [ - + ]: 4 : PG_FREE_IF_COPY(txt, 0);
2709 [ - + ]: 4 : PG_FREE_IF_COPY(ws, 1);
2710 : 4 : ts_setup_firstcall(fcinfo, funcctx, stat);
2711 : 4 : SPI_finish();
2712 : : }
2713 : :
2714 : 8 : funcctx = SRF_PERCALL_SETUP();
2715 [ + + ]: 8 : if ((result = ts_process_call(funcctx)) != (Datum) 0)
2716 : 4 : SRF_RETURN_NEXT(funcctx, result);
2717 : 4 : SRF_RETURN_DONE(funcctx);
2718 : : }
2719 : :
2720 : :
2721 : : /*
2722 : : * Triggers for automatic update of a tsvector column from text column(s)
2723 : : *
2724 : : * Trigger arguments are either
2725 : : * name of tsvector col, name of tsconfig to use, name(s) of text col(s)
2726 : : * name of tsvector col, name of regconfig col, name(s) of text col(s)
2727 : : * ie, tsconfig can either be specified by name, or indirectly as the
2728 : : * contents of a regconfig field in the row. If the name is used, it must
2729 : : * be explicitly schema-qualified.
2730 : : */
2731 : : Datum
2732 : 12 : tsvector_update_trigger_byid(PG_FUNCTION_ARGS)
2733 : : {
2734 : 12 : return tsvector_update_trigger(fcinfo, false);
2735 : : }
2736 : :
2737 : : Datum
6832 tgl@sss.pgh.pa.us 2738 :UBC 0 : tsvector_update_trigger_bycolumn(PG_FUNCTION_ARGS)
2739 : : {
2740 : 0 : return tsvector_update_trigger(fcinfo, true);
2741 : : }
2742 : :
2743 : : static Datum
6832 tgl@sss.pgh.pa.us 2744 :CBC 12 : tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
2745 : : {
2746 : : TriggerData *trigdata;
2747 : : Trigger *trigger;
2748 : : Relation rel;
2749 : 12 : HeapTuple rettuple = NULL;
2750 : : int tsvector_attr_num,
2751 : : i;
2752 : : ParsedText prs;
2753 : : Datum datum;
2754 : : bool isnull;
2755 : : text *txt;
2756 : : Oid cfgId;
2757 : : bool update_needed;
2758 : :
2759 : : /* Check call context */
3240 2760 [ + - - + ]: 12 : if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
6832 tgl@sss.pgh.pa.us 2761 [ # # ]:UBC 0 : elog(ERROR, "tsvector_update_trigger: not fired by trigger manager");
2762 : :
6832 tgl@sss.pgh.pa.us 2763 :CBC 12 : trigdata = (TriggerData *) fcinfo->context;
5688 2764 [ - + ]: 12 : if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
5688 tgl@sss.pgh.pa.us 2765 [ # # ]:UBC 0 : elog(ERROR, "tsvector_update_trigger: must be fired for row");
5688 tgl@sss.pgh.pa.us 2766 [ - + ]:CBC 12 : if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
6832 tgl@sss.pgh.pa.us 2767 [ # # ]:UBC 0 : elog(ERROR, "tsvector_update_trigger: must be fired BEFORE event");
2768 : :
6832 tgl@sss.pgh.pa.us 2769 [ + + ]:CBC 12 : if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
2770 : : {
2771 : 8 : rettuple = trigdata->tg_trigtuple;
2248 peter@eisentraut.org 2772 : 8 : update_needed = true;
2773 : : }
6832 tgl@sss.pgh.pa.us 2774 [ + - ]: 4 : else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
2775 : : {
2776 : 4 : rettuple = trigdata->tg_newtuple;
2248 peter@eisentraut.org 2777 : 4 : update_needed = false; /* computed below */
2778 : : }
2779 : : else
6832 tgl@sss.pgh.pa.us 2780 [ # # ]:UBC 0 : elog(ERROR, "tsvector_update_trigger: must be fired for INSERT or UPDATE");
2781 : :
6832 tgl@sss.pgh.pa.us 2782 :CBC 12 : trigger = trigdata->tg_trigger;
2783 : 12 : rel = trigdata->tg_relation;
2784 : :
2785 [ - + ]: 12 : if (trigger->tgnargs < 3)
6832 tgl@sss.pgh.pa.us 2786 [ # # ]:UBC 0 : elog(ERROR, "tsvector_update_trigger: arguments must be tsvector_field, ts_config, text_field1, ...)");
2787 : :
2788 : : /* Find the target tsvector column */
6832 tgl@sss.pgh.pa.us 2789 :CBC 12 : tsvector_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
2790 [ - + ]: 12 : if (tsvector_attr_num == SPI_ERROR_NOATTRIBUTE)
6832 tgl@sss.pgh.pa.us 2791 [ # # ]:UBC 0 : ereport(ERROR,
2792 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
2793 : : errmsg("tsvector column \"%s\" does not exist",
2794 : : trigger->tgargs[0])));
2795 : : /* This will effectively reject system columns, so no separate test: */
3883 teodor@sigaev.ru 2796 [ - + ]:CBC 12 : if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, tsvector_attr_num),
2797 : : TSVECTOROID))
6832 tgl@sss.pgh.pa.us 2798 [ # # ]:UBC 0 : ereport(ERROR,
2799 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
2800 : : errmsg("column \"%s\" is not of tsvector type",
2801 : : trigger->tgargs[0])));
2802 : :
2803 : : /* Find the configuration to use */
6832 tgl@sss.pgh.pa.us 2804 [ - + ]:CBC 12 : if (config_column)
2805 : : {
2806 : : int config_attr_num;
2807 : :
6832 tgl@sss.pgh.pa.us 2808 :UBC 0 : config_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[1]);
2809 [ # # ]: 0 : if (config_attr_num == SPI_ERROR_NOATTRIBUTE)
2810 [ # # ]: 0 : ereport(ERROR,
2811 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
2812 : : errmsg("configuration column \"%s\" does not exist",
2813 : : trigger->tgargs[1])));
3883 teodor@sigaev.ru 2814 [ # # ]: 0 : if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, config_attr_num),
2815 : : REGCONFIGOID))
6832 tgl@sss.pgh.pa.us 2816 [ # # ]: 0 : ereport(ERROR,
2817 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
2818 : : errmsg("column \"%s\" is not of regconfig type",
2819 : : trigger->tgargs[1])));
2820 : :
2821 : 0 : datum = SPI_getbinval(rettuple, rel->rd_att, config_attr_num, &isnull);
2822 [ # # ]: 0 : if (isnull)
2823 [ # # ]: 0 : ereport(ERROR,
2824 : : (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2825 : : errmsg("configuration column \"%s\" must not be null",
2826 : : trigger->tgargs[1])));
2827 : 0 : cfgId = DatumGetObjectId(datum);
2828 : : }
2829 : : else
2830 : : {
2831 : : List *names;
2832 : :
1225 tgl@sss.pgh.pa.us 2833 :CBC 12 : names = stringToQualifiedNameList(trigger->tgargs[1], NULL);
2834 : : /* require a schema so that results are not search path dependent */
6832 2835 [ - + ]: 12 : if (list_length(names) < 2)
6832 tgl@sss.pgh.pa.us 2836 [ # # ]:UBC 0 : ereport(ERROR,
2837 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2838 : : errmsg("text search configuration name \"%s\" must be schema-qualified",
2839 : : trigger->tgargs[1])));
5752 rhaas@postgresql.org 2840 :CBC 12 : cfgId = get_ts_config_oid(names, false);
2841 : : }
2842 : :
2843 : : /* initialize parse state */
6832 tgl@sss.pgh.pa.us 2844 : 12 : prs.lenwords = 32;
2845 : 12 : prs.curwords = 0;
2846 : 12 : prs.pos = 0;
146 michael@paquier.xyz 2847 :GNC 12 : prs.words = palloc_array(ParsedWord, prs.lenwords);
2848 : :
2849 : : /* find all words in indexable column(s) */
6832 tgl@sss.pgh.pa.us 2850 [ + + ]:CBC 24 : for (i = 2; i < trigger->tgnargs; i++)
2851 : : {
2852 : : int numattr;
2853 : :
2854 : 12 : numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
2855 [ - + ]: 12 : if (numattr == SPI_ERROR_NOATTRIBUTE)
6832 tgl@sss.pgh.pa.us 2856 [ # # ]:UBC 0 : ereport(ERROR,
2857 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
2858 : : errmsg("column \"%s\" does not exist",
2859 : : trigger->tgargs[i])));
3883 teodor@sigaev.ru 2860 [ - + ]:CBC 12 : if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, numattr), TEXTOID))
6832 tgl@sss.pgh.pa.us 2861 [ # # ]:UBC 0 : ereport(ERROR,
2862 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
2863 : : errmsg("column \"%s\" is not of a character type",
2864 : : trigger->tgargs[i])));
2865 : :
2248 peter@eisentraut.org 2866 [ + + ]:CBC 12 : if (bms_is_member(numattr - FirstLowInvalidHeapAttributeNumber, trigdata->tg_updatedcols))
2867 : 4 : update_needed = true;
2868 : :
6832 tgl@sss.pgh.pa.us 2869 : 12 : datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
2870 [ + + ]: 12 : if (isnull)
2871 : 4 : continue;
2872 : :
3341 noah@leadboat.com 2873 : 8 : txt = DatumGetTextPP(datum);
2874 : :
2875 [ - + - - : 8 : parsetext(cfgId, &prs, VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
- - - - +
- + - ]
2876 : :
6832 tgl@sss.pgh.pa.us 2877 [ - + ]: 8 : if (txt != (text *) DatumGetPointer(datum))
6832 tgl@sss.pgh.pa.us 2878 :UBC 0 : pfree(txt);
2879 : : }
2880 : :
2248 peter@eisentraut.org 2881 [ + - ]:CBC 12 : if (update_needed)
2882 : : {
2883 : : /* make tsvector value */
2884 : 12 : datum = TSVectorGetDatum(make_tsvector(&prs));
2885 : 12 : isnull = false;
2886 : :
2887 : : /* and insert it into tuple */
2888 : 12 : rettuple = heap_modify_tuple_by_cols(rettuple, rel->rd_att,
2889 : : 1, &tsvector_attr_num,
2890 : : &datum, &isnull);
2891 : :
2892 : 12 : pfree(DatumGetPointer(datum));
2893 : : }
2894 : :
6832 tgl@sss.pgh.pa.us 2895 : 12 : return PointerGetDatum(rettuple);
2896 : : }
|