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