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