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-2019, 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;
53  char lexeme[FLEXIBLE_ARRAY_MEMBER];
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, 'i',
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, 'i',
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(
670  cstring_to_text_with_len(data + arrin[i].pos, arrin[i].len)
671  );
672 
673  if (arrin[i].haspos)
674  {
675  WordEntryPosVector *posv;
676  Datum *positions;
677  Datum *weights;
678  char weight;
679 
680  /*
681  * Internally tsvector stores position and weight in the same
682  * uint16 (2 bits for weight, 14 for position). Here we extract
683  * that in two separate arrays.
684  */
685  posv = _POSVECPTR(tsin, arrin + i);
686  positions = palloc(posv->npos * sizeof(Datum));
687  weights = palloc(posv->npos * sizeof(Datum));
688  for (j = 0; j < posv->npos; j++)
689  {
690  positions[j] = Int16GetDatum(WEP_GETPOS(posv->pos[j]));
691  weight = 'D' - WEP_GETWEIGHT(posv->pos[j]);
692  weights[j] = PointerGetDatum(
693  cstring_to_text_with_len(&weight, 1)
694  );
695  }
696 
697  values[1] = PointerGetDatum(
698  construct_array(positions, posv->npos, INT2OID, 2, true, 's'));
699  values[2] = PointerGetDatum(
700  construct_array(weights, posv->npos, TEXTOID, -1, false, 'i'));
701  }
702  else
703  {
704  nulls[1] = nulls[2] = true;
705  }
706 
707  tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
708  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
709  }
710  else
711  {
712  pfree(tsin);
713  SRF_RETURN_DONE(funcctx);
714  }
715 }
716 
717 /*
718  * Convert tsvector to array of lexemes.
719  */
720 Datum
722 {
723  TSVector tsin = PG_GETARG_TSVECTOR(0);
724  WordEntry *arrin = ARRPTR(tsin);
725  Datum *elements;
726  int i;
727  ArrayType *array;
728 
729  elements = palloc(tsin->size * sizeof(Datum));
730 
731  for (i = 0; i < tsin->size; i++)
732  {
733  elements[i] = PointerGetDatum(
734  cstring_to_text_with_len(STRPTR(tsin) + arrin[i].pos, arrin[i].len)
735  );
736  }
737 
738  array = construct_array(elements, tsin->size, TEXTOID, -1, false, 'i');
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, 'i', &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, 'c',
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 bool
1185  ExecPhraseData *data)
1186 {
1187  bool result = false;
1188 
1189  if (entry->haspos && (val->weight || data))
1190  {
1191  WordEntryPosVector *posvec;
1192 
1193  /*
1194  * We can't use the _POSVECPTR macro here because the pointer to the
1195  * tsvector's lexeme storage is already contained in chkval->values.
1196  */
1197  posvec = (WordEntryPosVector *)
1198  (chkval->values + SHORTALIGN(entry->pos + entry->len));
1199 
1200  if (val->weight && data)
1201  {
1202  WordEntryPos *posvec_iter = posvec->pos;
1203  WordEntryPos *dptr;
1204 
1205  /*
1206  * Filter position information by weights
1207  */
1208  dptr = data->pos = palloc(sizeof(WordEntryPos) * posvec->npos);
1209  data->allocated = true;
1210 
1211  /* Is there a position with a matching weight? */
1212  while (posvec_iter < posvec->pos + posvec->npos)
1213  {
1214  /* If true, append this position to the data->pos */
1215  if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1216  {
1217  *dptr = WEP_GETPOS(*posvec_iter);
1218  dptr++;
1219  }
1220 
1221  posvec_iter++;
1222  }
1223 
1224  data->npos = dptr - data->pos;
1225 
1226  if (data->npos > 0)
1227  result = true;
1228  }
1229  else if (val->weight)
1230  {
1231  WordEntryPos *posvec_iter = posvec->pos;
1232 
1233  /* Is there a position with a matching weight? */
1234  while (posvec_iter < posvec->pos + posvec->npos)
1235  {
1236  if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1237  {
1238  result = true;
1239  break; /* no need to go further */
1240  }
1241 
1242  posvec_iter++;
1243  }
1244  }
1245  else /* data != NULL */
1246  {
1247  data->npos = posvec->npos;
1248  data->pos = posvec->pos;
1249  data->allocated = false;
1250  result = true;
1251  }
1252  }
1253  else
1254  {
1255  result = true;
1256  }
1257 
1258  return result;
1259 }
1260 
1261 /*
1262  * is there value 'val' in array or not ?
1263  */
1264 static bool
1266 {
1267  CHKVAL *chkval = (CHKVAL *) checkval;
1268  WordEntry *StopLow = chkval->arrb;
1269  WordEntry *StopHigh = chkval->arre;
1270  WordEntry *StopMiddle = StopHigh;
1271  int difference = -1;
1272  bool res = false;
1273 
1274  /* Loop invariant: StopLow <= val < StopHigh */
1275  while (StopLow < StopHigh)
1276  {
1277  StopMiddle = StopLow + (StopHigh - StopLow) / 2;
1278  difference = tsCompareString(chkval->operand + val->distance,
1279  val->length,
1280  chkval->values + StopMiddle->pos,
1281  StopMiddle->len,
1282  false);
1283 
1284  if (difference == 0)
1285  {
1286  /* Check weight info & fill 'data' with positions */
1287  res = checkclass_str(chkval, StopMiddle, val, data);
1288  break;
1289  }
1290  else if (difference > 0)
1291  StopLow = StopMiddle + 1;
1292  else
1293  StopHigh = StopMiddle;
1294  }
1295 
1296  if ((!res || data) && val->prefix)
1297  {
1298  WordEntryPos *allpos = NULL;
1299  int npos = 0,
1300  totalpos = 0;
1301 
1302  /*
1303  * there was a failed exact search, so we should scan further to find
1304  * a prefix match. We also need to do so if caller needs position info
1305  */
1306  if (StopLow >= StopHigh)
1307  StopMiddle = StopHigh;
1308 
1309  while ((!res || data) && StopMiddle < chkval->arre &&
1310  tsCompareString(chkval->operand + val->distance,
1311  val->length,
1312  chkval->values + StopMiddle->pos,
1313  StopMiddle->len,
1314  true) == 0)
1315  {
1316  if (data)
1317  {
1318  /*
1319  * We need to join position information
1320  */
1321  res = checkclass_str(chkval, StopMiddle, val, data);
1322 
1323  if (res)
1324  {
1325  while (npos + data->npos >= totalpos)
1326  {
1327  if (totalpos == 0)
1328  {
1329  totalpos = 256;
1330  allpos = palloc(sizeof(WordEntryPos) * totalpos);
1331  }
1332  else
1333  {
1334  totalpos *= 2;
1335  allpos = repalloc(allpos, sizeof(WordEntryPos) * totalpos);
1336  }
1337  }
1338 
1339  memcpy(allpos + npos, data->pos, sizeof(WordEntryPos) * data->npos);
1340  npos += data->npos;
1341  }
1342  }
1343  else
1344  {
1345  res = checkclass_str(chkval, StopMiddle, val, NULL);
1346  }
1347 
1348  StopMiddle++;
1349  }
1350 
1351  if (res && data)
1352  {
1353  /* Sort and make unique array of found positions */
1354  data->pos = allpos;
1355  qsort(data->pos, npos, sizeof(WordEntryPos), compareWordEntryPos);
1356  data->npos = qunique(data->pos, npos, sizeof(WordEntryPos),
1358  data->allocated = true;
1359  }
1360  }
1361 
1362  return res;
1363 }
1364 
1365 /*
1366  * Compute output position list for a tsquery operator in phrase mode.
1367  *
1368  * Merge the position lists in Ldata and Rdata as specified by "emit",
1369  * returning the result list into *data. The input position lists must be
1370  * sorted and unique, and the output will be as well.
1371  *
1372  * data: pointer to initially-all-zeroes output struct, or NULL
1373  * Ldata, Rdata: input position lists
1374  * emit: bitmask of TSPO_XXX flags
1375  * Loffset: offset to be added to Ldata positions before comparing/outputting
1376  * Roffset: offset to be added to Rdata positions before comparing/outputting
1377  * max_npos: maximum possible required size of output position array
1378  *
1379  * Loffset and Roffset should not be negative, else we risk trying to output
1380  * negative positions, which won't fit into WordEntryPos.
1381  *
1382  * Returns true if any positions were emitted to *data; or if data is NULL,
1383  * returns true if any positions would have been emitted.
1384  */
1385 #define TSPO_L_ONLY 0x01 /* emit positions appearing only in L */
1386 #define TSPO_R_ONLY 0x02 /* emit positions appearing only in R */
1387 #define TSPO_BOTH 0x04 /* emit positions appearing in both L&R */
1388 
1389 static bool
1391  ExecPhraseData *Ldata,
1392  ExecPhraseData *Rdata,
1393  int emit,
1394  int Loffset,
1395  int Roffset,
1396  int max_npos)
1397 {
1398  int Lindex,
1399  Rindex;
1400 
1401  /* Loop until both inputs are exhausted */
1402  Lindex = Rindex = 0;
1403  while (Lindex < Ldata->npos || Rindex < Rdata->npos)
1404  {
1405  int Lpos,
1406  Rpos;
1407  int output_pos = 0;
1408 
1409  /*
1410  * Fetch current values to compare. WEP_GETPOS() is needed because
1411  * ExecPhraseData->data can point to a tsvector's WordEntryPosVector.
1412  */
1413  if (Lindex < Ldata->npos)
1414  Lpos = WEP_GETPOS(Ldata->pos[Lindex]) + Loffset;
1415  else
1416  {
1417  /* L array exhausted, so we're done if R_ONLY isn't set */
1418  if (!(emit & TSPO_R_ONLY))
1419  break;
1420  Lpos = INT_MAX;
1421  }
1422  if (Rindex < Rdata->npos)
1423  Rpos = WEP_GETPOS(Rdata->pos[Rindex]) + Roffset;
1424  else
1425  {
1426  /* R array exhausted, so we're done if L_ONLY isn't set */
1427  if (!(emit & TSPO_L_ONLY))
1428  break;
1429  Rpos = INT_MAX;
1430  }
1431 
1432  /* Merge-join the two input lists */
1433  if (Lpos < Rpos)
1434  {
1435  /* Lpos is not matched in Rdata, should we output it? */
1436  if (emit & TSPO_L_ONLY)
1437  output_pos = Lpos;
1438  Lindex++;
1439  }
1440  else if (Lpos == Rpos)
1441  {
1442  /* Lpos and Rpos match ... should we output it? */
1443  if (emit & TSPO_BOTH)
1444  output_pos = Rpos;
1445  Lindex++;
1446  Rindex++;
1447  }
1448  else /* Lpos > Rpos */
1449  {
1450  /* Rpos is not matched in Ldata, should we output it? */
1451  if (emit & TSPO_R_ONLY)
1452  output_pos = Rpos;
1453  Rindex++;
1454  }
1455 
1456  if (output_pos > 0)
1457  {
1458  if (data)
1459  {
1460  /* Store position, first allocating output array if needed */
1461  if (data->pos == NULL)
1462  {
1463  data->pos = (WordEntryPos *)
1464  palloc(max_npos * sizeof(WordEntryPos));
1465  data->allocated = true;
1466  }
1467  data->pos[data->npos++] = output_pos;
1468  }
1469  else
1470  {
1471  /*
1472  * Exact positions not needed, so return true as soon as we
1473  * know there is at least one.
1474  */
1475  return true;
1476  }
1477  }
1478  }
1479 
1480  if (data && data->npos > 0)
1481  {
1482  /* Let's assert we didn't overrun the array */
1483  Assert(data->npos <= max_npos);
1484  return true;
1485  }
1486  return false;
1487 }
1488 
1489 /*
1490  * Execute tsquery at or below an OP_PHRASE operator.
1491  *
1492  * This handles tsquery execution at recursion levels where we need to care
1493  * about match locations.
1494  *
1495  * In addition to the same arguments used for TS_execute, the caller may pass
1496  * a preinitialized-to-zeroes ExecPhraseData struct, to be filled with lexeme
1497  * match position info on success. data == NULL if no position data need be
1498  * returned. (In practice, outside callers pass NULL, and only the internal
1499  * recursion cases pass a data pointer.)
1500  * Note: the function assumes data != NULL for operators other than OP_PHRASE.
1501  * This is OK because an outside call always starts from an OP_PHRASE node.
1502  *
1503  * The detailed semantics of the match data, given that the function returned
1504  * "true" (successful match, or possible match), are:
1505  *
1506  * npos > 0, negate = false:
1507  * query is matched at specified position(s) (and only those positions)
1508  * npos > 0, negate = true:
1509  * query is matched at all positions *except* specified position(s)
1510  * npos = 0, negate = false:
1511  * query is possibly matched, matching position(s) are unknown
1512  * (this should only be returned when TS_EXEC_PHRASE_NO_POS flag is set)
1513  * npos = 0, negate = true:
1514  * query is matched at all positions
1515  *
1516  * Successful matches also return a "width" value which is the match width in
1517  * lexemes, less one. Hence, "width" is zero for simple one-lexeme matches,
1518  * and is the sum of the phrase operator distances for phrase matches. Note
1519  * that when width > 0, the listed positions represent the ends of matches not
1520  * the starts. (This unintuitive rule is needed to avoid possibly generating
1521  * negative positions, which wouldn't fit into the WordEntryPos arrays.)
1522  *
1523  * When the function returns "false" (no match), it must return npos = 0,
1524  * negate = false (which is the state initialized by the caller); but the
1525  * "width" output in such cases is undefined.
1526  */
1527 static bool
1528 TS_phrase_execute(QueryItem *curitem, void *arg, uint32 flags,
1529  TSExecuteCallback chkcond,
1530  ExecPhraseData *data)
1531 {
1532  ExecPhraseData Ldata,
1533  Rdata;
1534  bool lmatch,
1535  rmatch;
1536  int Loffset,
1537  Roffset,
1538  maxwidth;
1539 
1540  /* since this function recurses, it could be driven to stack overflow */
1542 
1543  if (curitem->type == QI_VAL)
1544  return chkcond(arg, (QueryOperand *) curitem, data);
1545 
1546  switch (curitem->qoperator.oper)
1547  {
1548  case OP_NOT:
1549 
1550  /*
1551  * Because a "true" result with no specific positions is taken as
1552  * uncertain, we need no special care here for !TS_EXEC_CALC_NOT.
1553  * If it's a false positive, the right things happen anyway.
1554  *
1555  * Also, we need not touch data->width, since a NOT operation does
1556  * not change the match width.
1557  */
1558  if (TS_phrase_execute(curitem + 1, arg, flags, chkcond, data))
1559  {
1560  if (data->npos > 0)
1561  {
1562  /* we have some positions, invert negate flag */
1563  data->negate = !data->negate;
1564  return true;
1565  }
1566  else if (data->negate)
1567  {
1568  /* change "match everywhere" to "match nowhere" */
1569  data->negate = false;
1570  return false;
1571  }
1572  /* match positions are, and remain, uncertain */
1573  return true;
1574  }
1575  else
1576  {
1577  /* change "match nowhere" to "match everywhere" */
1578  Assert(data->npos == 0 && !data->negate);
1579  data->negate = true;
1580  return true;
1581  }
1582 
1583  case OP_PHRASE:
1584  case OP_AND:
1585  memset(&Ldata, 0, sizeof(Ldata));
1586  memset(&Rdata, 0, sizeof(Rdata));
1587 
1588  if (!TS_phrase_execute(curitem + curitem->qoperator.left,
1589  arg, flags, chkcond, &Ldata))
1590  return false;
1591 
1592  if (!TS_phrase_execute(curitem + 1,
1593  arg, flags, chkcond, &Rdata))
1594  return false;
1595 
1596  /*
1597  * If either operand has no position information, then we can't
1598  * return position data, only a "possible match" result. "Possible
1599  * match" answers are only wanted when TS_EXEC_PHRASE_NO_POS flag
1600  * is set, otherwise return false.
1601  */
1602  if ((Ldata.npos == 0 && !Ldata.negate) ||
1603  (Rdata.npos == 0 && !Rdata.negate))
1604  return (flags & TS_EXEC_PHRASE_NO_POS) ? true : false;
1605 
1606  if (curitem->qoperator.oper == OP_PHRASE)
1607  {
1608  /*
1609  * Compute Loffset and Roffset suitable for phrase match, and
1610  * compute overall width of whole phrase match.
1611  */
1612  Loffset = curitem->qoperator.distance + Rdata.width;
1613  Roffset = 0;
1614  if (data)
1615  data->width = curitem->qoperator.distance +
1616  Ldata.width + Rdata.width;
1617  }
1618  else
1619  {
1620  /*
1621  * For OP_AND, set output width and alignment like OP_OR (see
1622  * comment below)
1623  */
1624  maxwidth = Max(Ldata.width, Rdata.width);
1625  Loffset = maxwidth - Ldata.width;
1626  Roffset = maxwidth - Rdata.width;
1627  if (data)
1628  data->width = maxwidth;
1629  }
1630 
1631  if (Ldata.negate && Rdata.negate)
1632  {
1633  /* !L & !R: treat as !(L | R) */
1634  (void) TS_phrase_output(data, &Ldata, &Rdata,
1636  Loffset, Roffset,
1637  Ldata.npos + Rdata.npos);
1638  if (data)
1639  data->negate = true;
1640  return true;
1641  }
1642  else if (Ldata.negate)
1643  {
1644  /* !L & R */
1645  return TS_phrase_output(data, &Ldata, &Rdata,
1646  TSPO_R_ONLY,
1647  Loffset, Roffset,
1648  Rdata.npos);
1649  }
1650  else if (Rdata.negate)
1651  {
1652  /* L & !R */
1653  return TS_phrase_output(data, &Ldata, &Rdata,
1654  TSPO_L_ONLY,
1655  Loffset, Roffset,
1656  Ldata.npos);
1657  }
1658  else
1659  {
1660  /* straight AND */
1661  return TS_phrase_output(data, &Ldata, &Rdata,
1662  TSPO_BOTH,
1663  Loffset, Roffset,
1664  Min(Ldata.npos, Rdata.npos));
1665  }
1666 
1667  case OP_OR:
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  rmatch = TS_phrase_execute(curitem + 1,
1674  arg, flags, chkcond, &Rdata);
1675 
1676  if (!lmatch && !rmatch)
1677  return false;
1678 
1679  /*
1680  * If a valid operand has no position information, then we can't
1681  * return position data, only a "possible match" result. "Possible
1682  * match" answers are only wanted when TS_EXEC_PHRASE_NO_POS flag
1683  * is set, otherwise return false.
1684  */
1685  if ((lmatch && Ldata.npos == 0 && !Ldata.negate) ||
1686  (rmatch && Rdata.npos == 0 && !Rdata.negate))
1687  return (flags & TS_EXEC_PHRASE_NO_POS) ? true : false;
1688 
1689  /*
1690  * Cope with undefined output width from failed submatch. (This
1691  * takes less code than trying to ensure that all failure returns
1692  * set data->width to zero.)
1693  */
1694  if (!lmatch)
1695  Ldata.width = 0;
1696  if (!rmatch)
1697  Rdata.width = 0;
1698 
1699  /*
1700  * For OP_AND and OP_OR, report the width of the wider of the two
1701  * inputs, and align the narrower input's positions to the right
1702  * end of that width. This rule deals at least somewhat
1703  * reasonably with cases like "x <-> (y | z <-> q)".
1704  */
1705  maxwidth = Max(Ldata.width, Rdata.width);
1706  Loffset = maxwidth - Ldata.width;
1707  Roffset = maxwidth - Rdata.width;
1708  data->width = maxwidth;
1709 
1710  if (Ldata.negate && Rdata.negate)
1711  {
1712  /* !L | !R: treat as !(L & R) */
1713  (void) TS_phrase_output(data, &Ldata, &Rdata,
1714  TSPO_BOTH,
1715  Loffset, Roffset,
1716  Min(Ldata.npos, Rdata.npos));
1717  data->negate = true;
1718  return true;
1719  }
1720  else if (Ldata.negate)
1721  {
1722  /* !L | R: treat as !(L & !R) */
1723  (void) TS_phrase_output(data, &Ldata, &Rdata,
1724  TSPO_L_ONLY,
1725  Loffset, Roffset,
1726  Ldata.npos);
1727  data->negate = true;
1728  return true;
1729  }
1730  else if (Rdata.negate)
1731  {
1732  /* L | !R: treat as !(!L & R) */
1733  (void) TS_phrase_output(data, &Ldata, &Rdata,
1734  TSPO_R_ONLY,
1735  Loffset, Roffset,
1736  Rdata.npos);
1737  data->negate = true;
1738  return true;
1739  }
1740  else
1741  {
1742  /* straight OR */
1743  return TS_phrase_output(data, &Ldata, &Rdata,
1745  Loffset, Roffset,
1746  Ldata.npos + Rdata.npos);
1747  }
1748 
1749  default:
1750  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1751  }
1752 
1753  /* not reachable, but keep compiler quiet */
1754  return false;
1755 }
1756 
1757 
1758 /*
1759  * Evaluate tsquery boolean expression.
1760  *
1761  * curitem: current tsquery item (initially, the first one)
1762  * arg: opaque value to pass through to callback function
1763  * flags: bitmask of flag bits shown in ts_utils.h
1764  * chkcond: callback function to check whether a primitive value is present
1765  *
1766  * The logic here deals only with operators above any phrase operator, for
1767  * which we do not need to worry about lexeme positions. As soon as we hit an
1768  * OP_PHRASE operator, we pass it off to TS_phrase_execute which does worry.
1769  */
1770 bool
1771 TS_execute(QueryItem *curitem, void *arg, uint32 flags,
1772  TSExecuteCallback chkcond)
1773 {
1774  /* since this function recurses, it could be driven to stack overflow */
1776 
1777  if (curitem->type == QI_VAL)
1778  return chkcond(arg, (QueryOperand *) curitem,
1779  NULL /* we don't need position info */ );
1780 
1781  switch (curitem->qoperator.oper)
1782  {
1783  case OP_NOT:
1784  if (flags & TS_EXEC_CALC_NOT)
1785  return !TS_execute(curitem + 1, arg, flags, chkcond);
1786  else
1787  return true;
1788 
1789  case OP_AND:
1790  if (TS_execute(curitem + curitem->qoperator.left, arg, flags, chkcond))
1791  return TS_execute(curitem + 1, arg, flags, chkcond);
1792  else
1793  return false;
1794 
1795  case OP_OR:
1796  if (TS_execute(curitem + curitem->qoperator.left, arg, flags, chkcond))
1797  return true;
1798  else
1799  return TS_execute(curitem + 1, arg, flags, chkcond);
1800 
1801  case OP_PHRASE:
1802  return TS_phrase_execute(curitem, arg, flags, chkcond, NULL);
1803 
1804  default:
1805  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1806  }
1807 
1808  /* not reachable, but keep compiler quiet */
1809  return false;
1810 }
1811 
1812 /*
1813  * Detect whether a tsquery boolean expression requires any positive matches
1814  * to values shown in the tsquery.
1815  *
1816  * This is needed to know whether a GIN index search requires full index scan.
1817  * For example, 'x & !y' requires a match of x, so it's sufficient to scan
1818  * entries for x; but 'x | !y' could match rows containing neither x nor y.
1819  */
1820 bool
1822 {
1823  /* since this function recurses, it could be driven to stack overflow */
1825 
1826  if (curitem->type == QI_VAL)
1827  return true;
1828 
1829  switch (curitem->qoperator.oper)
1830  {
1831  case OP_NOT:
1832 
1833  /*
1834  * Assume there are no required matches underneath a NOT. For
1835  * some cases with nested NOTs, we could prove there's a required
1836  * match, but it seems unlikely to be worth the trouble.
1837  */
1838  return false;
1839 
1840  case OP_PHRASE:
1841 
1842  /*
1843  * Treat OP_PHRASE as OP_AND here
1844  */
1845  case OP_AND:
1846  /* If either side requires a match, we're good */
1847  if (tsquery_requires_match(curitem + curitem->qoperator.left))
1848  return true;
1849  else
1850  return tsquery_requires_match(curitem + 1);
1851 
1852  case OP_OR:
1853  /* Both sides must require a match */
1854  if (tsquery_requires_match(curitem + curitem->qoperator.left))
1855  return tsquery_requires_match(curitem + 1);
1856  else
1857  return false;
1858 
1859  default:
1860  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1861  }
1862 
1863  /* not reachable, but keep compiler quiet */
1864  return false;
1865 }
1866 
1867 /*
1868  * boolean operations
1869  */
1870 Datum
1872 {
1874  PG_GETARG_DATUM(1),
1875  PG_GETARG_DATUM(0)));
1876 }
1877 
1878 Datum
1880 {
1882  TSQuery query = PG_GETARG_TSQUERY(1);
1883  CHKVAL chkval;
1884  bool result;
1885 
1886  /* empty query matches nothing */
1887  if (!query->size)
1888  {
1889  PG_FREE_IF_COPY(val, 0);
1890  PG_FREE_IF_COPY(query, 1);
1891  PG_RETURN_BOOL(false);
1892  }
1893 
1894  chkval.arrb = ARRPTR(val);
1895  chkval.arre = chkval.arrb + val->size;
1896  chkval.values = STRPTR(val);
1897  chkval.operand = GETOPERAND(query);
1898  result = TS_execute(GETQUERY(query),
1899  &chkval,
1902 
1903  PG_FREE_IF_COPY(val, 0);
1904  PG_FREE_IF_COPY(query, 1);
1905  PG_RETURN_BOOL(result);
1906 }
1907 
1908 Datum
1910 {
1911  TSVector vector;
1912  TSQuery query;
1913  bool res;
1914 
1916  PG_GETARG_DATUM(0)));
1918  PG_GETARG_DATUM(1)));
1919 
1921  TSVectorGetDatum(vector),
1922  TSQueryGetDatum(query)));
1923 
1924  pfree(vector);
1925  pfree(query);
1926 
1927  PG_RETURN_BOOL(res);
1928 }
1929 
1930 Datum
1932 {
1933  TSVector vector;
1934  TSQuery query = PG_GETARG_TSQUERY(1);
1935  bool res;
1936 
1938  PG_GETARG_DATUM(0)));
1939 
1941  TSVectorGetDatum(vector),
1942  TSQueryGetDatum(query)));
1943 
1944  pfree(vector);
1945  PG_FREE_IF_COPY(query, 1);
1946 
1947  PG_RETURN_BOOL(res);
1948 }
1949 
1950 /*
1951  * ts_stat statistic function support
1952  */
1953 
1954 
1955 /*
1956  * Returns the number of positions in value 'wptr' within tsvector 'txt',
1957  * that have a weight equal to one of the weights in 'weight' bitmask.
1958  */
1959 static int
1961 {
1962  int len = POSDATALEN(txt, wptr);
1963  int num = 0;
1964  WordEntryPos *ptr = POSDATAPTR(txt, wptr);
1965 
1966  while (len--)
1967  {
1968  if (weight & (1 << WEP_GETWEIGHT(*ptr)))
1969  num++;
1970  ptr++;
1971  }
1972  return num;
1973 }
1974 
1975 #define compareStatWord(a,e,t) \
1976  tsCompareString((a)->lexeme, (a)->lenlexeme, \
1977  STRPTR(t) + (e)->pos, (e)->len, \
1978  false)
1979 
1980 static void
1982 {
1983  WordEntry *we = ARRPTR(txt) + off;
1984  StatEntry *node = stat->root,
1985  *pnode = NULL;
1986  int n,
1987  res = 0;
1988  uint32 depth = 1;
1989 
1990  if (stat->weight == 0)
1991  n = (we->haspos) ? POSDATALEN(txt, we) : 1;
1992  else
1993  n = (we->haspos) ? check_weight(txt, we, stat->weight) : 0;
1994 
1995  if (n == 0)
1996  return; /* nothing to insert */
1997 
1998  while (node)
1999  {
2000  res = compareStatWord(node, we, txt);
2001 
2002  if (res == 0)
2003  {
2004  break;
2005  }
2006  else
2007  {
2008  pnode = node;
2009  node = (res < 0) ? node->left : node->right;
2010  }
2011  depth++;
2012  }
2013 
2014  if (depth > stat->maxdepth)
2015  stat->maxdepth = depth;
2016 
2017  if (node == NULL)
2018  {
2019  node = MemoryContextAlloc(persistentContext, STATENTRYHDRSZ + we->len);
2020  node->left = node->right = NULL;
2021  node->ndoc = 1;
2022  node->nentry = n;
2023  node->lenlexeme = we->len;
2024  memcpy(node->lexeme, STRPTR(txt) + we->pos, node->lenlexeme);
2025 
2026  if (pnode == NULL)
2027  {
2028  stat->root = node;
2029  }
2030  else
2031  {
2032  if (res < 0)
2033  pnode->left = node;
2034  else
2035  pnode->right = node;
2036  }
2037 
2038  }
2039  else
2040  {
2041  node->ndoc++;
2042  node->nentry += n;
2043  }
2044 }
2045 
2046 static void
2048  uint32 low, uint32 high, uint32 offset)
2049 {
2050  uint32 pos;
2051  uint32 middle = (low + high) >> 1;
2052 
2053  pos = (low + middle) >> 1;
2054  if (low != middle && pos >= offset && pos - offset < txt->size)
2055  insertStatEntry(persistentContext, stat, txt, pos - offset);
2056  pos = (high + middle + 1) >> 1;
2057  if (middle + 1 != high && pos >= offset && pos - offset < txt->size)
2058  insertStatEntry(persistentContext, stat, txt, pos - offset);
2059 
2060  if (low != middle)
2061  chooseNextStatEntry(persistentContext, stat, txt, low, middle, offset);
2062  if (high != middle + 1)
2063  chooseNextStatEntry(persistentContext, stat, txt, middle + 1, high, offset);
2064 }
2065 
2066 /*
2067  * This is written like a custom aggregate function, because the
2068  * original plan was to do just that. Unfortunately, an aggregate function
2069  * can't return a set, so that plan was abandoned. If that limitation is
2070  * lifted in the future, ts_stat could be a real aggregate function so that
2071  * you could use it like this:
2072  *
2073  * SELECT ts_stat(vector_column) FROM vector_table;
2074  *
2075  * where vector_column is a tsvector-type column in vector_table.
2076  */
2077 
2078 static TSVectorStat *
2079 ts_accum(MemoryContext persistentContext, TSVectorStat *stat, Datum data)
2080 {
2081  TSVector txt = DatumGetTSVector(data);
2082  uint32 i,
2083  nbit = 0,
2084  offset;
2085 
2086  if (stat == NULL)
2087  { /* Init in first */
2088  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2089  stat->maxdepth = 1;
2090  }
2091 
2092  /* simple check of correctness */
2093  if (txt == NULL || txt->size == 0)
2094  {
2095  if (txt && txt != (TSVector) DatumGetPointer(data))
2096  pfree(txt);
2097  return stat;
2098  }
2099 
2100  i = txt->size - 1;
2101  for (; i > 0; i >>= 1)
2102  nbit++;
2103 
2104  nbit = 1 << nbit;
2105  offset = (nbit - txt->size) / 2;
2106 
2107  insertStatEntry(persistentContext, stat, txt, (nbit >> 1) - offset);
2108  chooseNextStatEntry(persistentContext, stat, txt, 0, nbit, offset);
2109 
2110  return stat;
2111 }
2112 
2113 static void
2115  TSVectorStat *stat)
2116 {
2117  TupleDesc tupdesc;
2118  MemoryContext oldcontext;
2119  StatEntry *node;
2120 
2121  funcctx->user_fctx = (void *) stat;
2122 
2123  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2124 
2125  stat->stack = palloc0(sizeof(StatEntry *) * (stat->maxdepth + 1));
2126  stat->stackpos = 0;
2127 
2128  node = stat->root;
2129  /* find leftmost value */
2130  if (node == NULL)
2131  stat->stack[stat->stackpos] = NULL;
2132  else
2133  for (;;)
2134  {
2135  stat->stack[stat->stackpos] = node;
2136  if (node->left)
2137  {
2138  stat->stackpos++;
2139  node = node->left;
2140  }
2141  else
2142  break;
2143  }
2144  Assert(stat->stackpos <= stat->maxdepth);
2145 
2146  tupdesc = CreateTemplateTupleDesc(3);
2147  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "word",
2148  TEXTOID, -1, 0);
2149  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "ndoc",
2150  INT4OID, -1, 0);
2151  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "nentry",
2152  INT4OID, -1, 0);
2153  funcctx->tuple_desc = BlessTupleDesc(tupdesc);
2154  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
2155 
2156  MemoryContextSwitchTo(oldcontext);
2157 }
2158 
2159 static StatEntry *
2161 {
2162  StatEntry *node = stat->stack[stat->stackpos];
2163 
2164  if (node == NULL)
2165  return NULL;
2166 
2167  if (node->ndoc != 0)
2168  {
2169  /* return entry itself: we already was at left sublink */
2170  return node;
2171  }
2172  else if (node->right && node->right != stat->stack[stat->stackpos + 1])
2173  {
2174  /* go on right sublink */
2175  stat->stackpos++;
2176  node = node->right;
2177 
2178  /* find most-left value */
2179  for (;;)
2180  {
2181  stat->stack[stat->stackpos] = node;
2182  if (node->left)
2183  {
2184  stat->stackpos++;
2185  node = node->left;
2186  }
2187  else
2188  break;
2189  }
2190  Assert(stat->stackpos <= stat->maxdepth);
2191  }
2192  else
2193  {
2194  /* we already return all left subtree, itself and right subtree */
2195  if (stat->stackpos == 0)
2196  return NULL;
2197 
2198  stat->stackpos--;
2199  return walkStatEntryTree(stat);
2200  }
2201 
2202  return node;
2203 }
2204 
2205 static Datum
2207 {
2208  TSVectorStat *st;
2209  StatEntry *entry;
2210 
2211  st = (TSVectorStat *) funcctx->user_fctx;
2212 
2213  entry = walkStatEntryTree(st);
2214 
2215  if (entry != NULL)
2216  {
2217  Datum result;
2218  char *values[3];
2219  char ndoc[16];
2220  char nentry[16];
2221  HeapTuple tuple;
2222 
2223  values[0] = palloc(entry->lenlexeme + 1);
2224  memcpy(values[0], entry->lexeme, entry->lenlexeme);
2225  (values[0])[entry->lenlexeme] = '\0';
2226  sprintf(ndoc, "%d", entry->ndoc);
2227  values[1] = ndoc;
2228  sprintf(nentry, "%d", entry->nentry);
2229  values[2] = nentry;
2230 
2231  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
2232  result = HeapTupleGetDatum(tuple);
2233 
2234  pfree(values[0]);
2235 
2236  /* mark entry as already visited */
2237  entry->ndoc = 0;
2238 
2239  return result;
2240  }
2241 
2242  return (Datum) 0;
2243 }
2244 
2245 static TSVectorStat *
2246 ts_stat_sql(MemoryContext persistentContext, text *txt, text *ws)
2247 {
2248  char *query = text_to_cstring(txt);
2249  TSVectorStat *stat;
2250  bool isnull;
2251  Portal portal;
2252  SPIPlanPtr plan;
2253 
2254  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2255  /* internal error */
2256  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2257 
2258  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2259  /* internal error */
2260  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2261 
2262  SPI_cursor_fetch(portal, true, 100);
2263 
2264  if (SPI_tuptable == NULL ||
2265  SPI_tuptable->tupdesc->natts != 1 ||
2267  TSVECTOROID))
2268  ereport(ERROR,
2269  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2270  errmsg("ts_stat query must return one tsvector column")));
2271 
2272  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2273  stat->maxdepth = 1;
2274 
2275  if (ws)
2276  {
2277  char *buf;
2278 
2279  buf = VARDATA_ANY(ws);
2280  while (buf - VARDATA_ANY(ws) < VARSIZE_ANY_EXHDR(ws))
2281  {
2282  if (pg_mblen(buf) == 1)
2283  {
2284  switch (*buf)
2285  {
2286  case 'A':
2287  case 'a':
2288  stat->weight |= 1 << 3;
2289  break;
2290  case 'B':
2291  case 'b':
2292  stat->weight |= 1 << 2;
2293  break;
2294  case 'C':
2295  case 'c':
2296  stat->weight |= 1 << 1;
2297  break;
2298  case 'D':
2299  case 'd':
2300  stat->weight |= 1;
2301  break;
2302  default:
2303  stat->weight |= 0;
2304  }
2305  }
2306  buf += pg_mblen(buf);
2307  }
2308  }
2309 
2310  while (SPI_processed > 0)
2311  {
2312  uint64 i;
2313 
2314  for (i = 0; i < SPI_processed; i++)
2315  {
2316  Datum data = SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull);
2317 
2318  if (!isnull)
2319  stat = ts_accum(persistentContext, stat, data);
2320  }
2321 
2323  SPI_cursor_fetch(portal, true, 100);
2324  }
2325 
2327  SPI_cursor_close(portal);
2328  SPI_freeplan(plan);
2329  pfree(query);
2330 
2331  return stat;
2332 }
2333 
2334 Datum
2336 {
2337  FuncCallContext *funcctx;
2338  Datum result;
2339 
2340  if (SRF_IS_FIRSTCALL())
2341  {
2342  TSVectorStat *stat;
2343  text *txt = PG_GETARG_TEXT_PP(0);
2344 
2345  funcctx = SRF_FIRSTCALL_INIT();
2346  SPI_connect();
2347  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, NULL);
2348  PG_FREE_IF_COPY(txt, 0);
2349  ts_setup_firstcall(fcinfo, funcctx, stat);
2350  SPI_finish();
2351  }
2352 
2353  funcctx = SRF_PERCALL_SETUP();
2354  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2355  SRF_RETURN_NEXT(funcctx, result);
2356  SRF_RETURN_DONE(funcctx);
2357 }
2358 
2359 Datum
2361 {
2362  FuncCallContext *funcctx;
2363  Datum result;
2364 
2365  if (SRF_IS_FIRSTCALL())
2366  {
2367  TSVectorStat *stat;
2368  text *txt = PG_GETARG_TEXT_PP(0);
2369  text *ws = PG_GETARG_TEXT_PP(1);
2370 
2371  funcctx = SRF_FIRSTCALL_INIT();
2372  SPI_connect();
2373  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, ws);
2374  PG_FREE_IF_COPY(txt, 0);
2375  PG_FREE_IF_COPY(ws, 1);
2376  ts_setup_firstcall(fcinfo, funcctx, stat);
2377  SPI_finish();
2378  }
2379 
2380  funcctx = SRF_PERCALL_SETUP();
2381  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2382  SRF_RETURN_NEXT(funcctx, result);
2383  SRF_RETURN_DONE(funcctx);
2384 }
2385 
2386 
2387 /*
2388  * Triggers for automatic update of a tsvector column from text column(s)
2389  *
2390  * Trigger arguments are either
2391  * name of tsvector col, name of tsconfig to use, name(s) of text col(s)
2392  * name of tsvector col, name of regconfig col, name(s) of text col(s)
2393  * ie, tsconfig can either be specified by name, or indirectly as the
2394  * contents of a regconfig field in the row. If the name is used, it must
2395  * be explicitly schema-qualified.
2396  */
2397 Datum
2399 {
2400  return tsvector_update_trigger(fcinfo, false);
2401 }
2402 
2403 Datum
2405 {
2406  return tsvector_update_trigger(fcinfo, true);
2407 }
2408 
2409 static Datum
2411 {
2412  TriggerData *trigdata;
2413  Trigger *trigger;
2414  Relation rel;
2415  HeapTuple rettuple = NULL;
2416  int tsvector_attr_num,
2417  i;
2418  ParsedText prs;
2419  Datum datum;
2420  bool isnull;
2421  text *txt;
2422  Oid cfgId;
2423 
2424  /* Check call context */
2425  if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
2426  elog(ERROR, "tsvector_update_trigger: not fired by trigger manager");
2427 
2428  trigdata = (TriggerData *) fcinfo->context;
2429  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
2430  elog(ERROR, "tsvector_update_trigger: must be fired for row");
2431  if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
2432  elog(ERROR, "tsvector_update_trigger: must be fired BEFORE event");
2433 
2434  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
2435  rettuple = trigdata->tg_trigtuple;
2436  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
2437  rettuple = trigdata->tg_newtuple;
2438  else
2439  elog(ERROR, "tsvector_update_trigger: must be fired for INSERT or UPDATE");
2440 
2441  trigger = trigdata->tg_trigger;
2442  rel = trigdata->tg_relation;
2443 
2444  if (trigger->tgnargs < 3)
2445  elog(ERROR, "tsvector_update_trigger: arguments must be tsvector_field, ts_config, text_field1, ...)");
2446 
2447  /* Find the target tsvector column */
2448  tsvector_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
2449  if (tsvector_attr_num == SPI_ERROR_NOATTRIBUTE)
2450  ereport(ERROR,
2451  (errcode(ERRCODE_UNDEFINED_COLUMN),
2452  errmsg("tsvector column \"%s\" does not exist",
2453  trigger->tgargs[0])));
2454  /* This will effectively reject system columns, so no separate test: */
2455  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, tsvector_attr_num),
2456  TSVECTOROID))
2457  ereport(ERROR,
2458  (errcode(ERRCODE_DATATYPE_MISMATCH),
2459  errmsg("column \"%s\" is not of tsvector type",
2460  trigger->tgargs[0])));
2461 
2462  /* Find the configuration to use */
2463  if (config_column)
2464  {
2465  int config_attr_num;
2466 
2467  config_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[1]);
2468  if (config_attr_num == SPI_ERROR_NOATTRIBUTE)
2469  ereport(ERROR,
2470  (errcode(ERRCODE_UNDEFINED_COLUMN),
2471  errmsg("configuration column \"%s\" does not exist",
2472  trigger->tgargs[1])));
2473  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, config_attr_num),
2474  REGCONFIGOID))
2475  ereport(ERROR,
2476  (errcode(ERRCODE_DATATYPE_MISMATCH),
2477  errmsg("column \"%s\" is not of regconfig type",
2478  trigger->tgargs[1])));
2479 
2480  datum = SPI_getbinval(rettuple, rel->rd_att, config_attr_num, &isnull);
2481  if (isnull)
2482  ereport(ERROR,
2483  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2484  errmsg("configuration column \"%s\" must not be null",
2485  trigger->tgargs[1])));
2486  cfgId = DatumGetObjectId(datum);
2487  }
2488  else
2489  {
2490  List *names;
2491 
2492  names = stringToQualifiedNameList(trigger->tgargs[1]);
2493  /* require a schema so that results are not search path dependent */
2494  if (list_length(names) < 2)
2495  ereport(ERROR,
2496  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2497  errmsg("text search configuration name \"%s\" must be schema-qualified",
2498  trigger->tgargs[1])));
2499  cfgId = get_ts_config_oid(names, false);
2500  }
2501 
2502  /* initialize parse state */
2503  prs.lenwords = 32;
2504  prs.curwords = 0;
2505  prs.pos = 0;
2506  prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
2507 
2508  /* find all words in indexable column(s) */
2509  for (i = 2; i < trigger->tgnargs; i++)
2510  {
2511  int numattr;
2512 
2513  numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
2514  if (numattr == SPI_ERROR_NOATTRIBUTE)
2515  ereport(ERROR,
2516  (errcode(ERRCODE_UNDEFINED_COLUMN),
2517  errmsg("column \"%s\" does not exist",
2518  trigger->tgargs[i])));
2519  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, numattr), TEXTOID))
2520  ereport(ERROR,
2521  (errcode(ERRCODE_DATATYPE_MISMATCH),
2522  errmsg("column \"%s\" is not of a character type",
2523  trigger->tgargs[i])));
2524 
2525  datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
2526  if (isnull)
2527  continue;
2528 
2529  txt = DatumGetTextPP(datum);
2530 
2531  parsetext(cfgId, &prs, VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
2532 
2533  if (txt != (text *) DatumGetPointer(datum))
2534  pfree(txt);
2535  }
2536 
2537  /* make tsvector value */
2538  datum = TSVectorGetDatum(make_tsvector(&prs));
2539  isnull = false;
2540 
2541  /* and insert it into tuple */
2542  rettuple = heap_modify_tuple_by_cols(rettuple, rel->rd_att,
2543  1, &tsvector_attr_num,
2544  &datum, &isnull);
2545 
2546  pfree(DatumGetPointer(datum));
2547 
2548  return PointerGetDatum(rettuple);
2549 }
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:1771
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
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:2047
#define TSPO_R_ONLY
Definition: tsvector_op.c:1386
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:813
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:1871
bool allocated
Definition: ts_utils.h:152
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:282
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define VARHDRSZ
Definition: c.h:562
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
bool tsquery_requires_match(QueryItem *curitem)
Definition: tsvector_op.c:1821
#define DatumGetObjectId(X)
Definition: postgres.h:500
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
Datum tsvector_concat(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:920
#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:911
#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:344
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
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:608
Oid get_ts_config_oid(List *names, bool missing_ok)
Definition: namespace.c:2671
Datum tsvector_update_trigger_bycolumn(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:2404
#define QI_VAL
Definition: ts_type.h:134
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:2335
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
uint32 distance
Definition: ts_type.h:158
#define MAXSTRPOS
Definition: ts_type.h:50
#define TSPO_BOTH
Definition: tsvector_op.c:1387
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:286
#define GETQUERY(x)
Definition: _int.h:136
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:347
WordEntry * arrb
Definition: tsvector_op.c:38
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2116
#define GETOPERAND(x)
Definition: ltree.h:118
#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:1390
int32 * arrb
Definition: _int_bool.c:227
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:288
#define sprintf
Definition: port.h:194
#define true
Definition: c.h:313
#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:1265
unsigned short uint16
Definition: c.h:358
void pfree(void *pointer)
Definition: mcxt.c:1056
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:358
#define MAXNUMPOS
Definition: ts_type.h:86
Datum ts_match_vq(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:1879
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
#define memmove(d, s, c)
Definition: c.h:1267
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:183
static StatEntry * walkStatEntryTree(TSVectorStat *stat)
Definition: tsvector_op.c:2160
void check_stack_depth(void)
Definition: postgres.c:3302
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
static TSVectorStat * ts_stat_sql(MemoryContext persistentContext, text *txt, text *ws)
Definition: tsvector_op.c:2246
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:359
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 ereport(elevel, rest)
Definition: elog.h:141
#define TSQueryGetDatum(X)
Definition: ts_type.h:237
static void ts_setup_firstcall(FunctionCallInfo fcinfo, FuncCallContext *funcctx, TSVectorStat *stat)
Definition: tsvector_op.c:2114
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:1385
StatEntry ** stack
Definition: tsvector_op.c:64
signed char int8
Definition: c.h:345
#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:40
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:349
static void insertStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 off)
Definition: tsvector_op.c:1981
uintptr_t Datum
Definition: postgres.h:367
Datum difference(PG_FUNCTION_ARGS)
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
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:84
HeapTuple tg_newtuple
Definition: trigger.h:36
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
#define Max(x, y)
Definition: c.h:905
Datum tsvector_to_array(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:721
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define Assert(condition)
Definition: c.h:739
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:2206
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:1960
Datum ts_stat2(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:2360
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:1147
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
Datum tsvector_update_trigger_byid(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:2398
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:134
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:1595
char * text_to_cstring(const text *t)
Definition: varlena.c:204
char * values
Definition: tsvector_op.c:40
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1687
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:116
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:822
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:2410
#define STRPTR(x)
Definition: hstore.h:76
int32 size
Definition: ts_type.h:208
#define elog(elevel,...)
Definition: elog.h:228
int i
int16 tgnargs
Definition: reltrigger.h:37
void * arg
static bool TS_phrase_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond, ExecPhraseData *data)
Definition: tsvector_op.c:1528
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1539
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:556
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
static TSVectorStat * ts_accum(MemoryContext persistentContext, TSVectorStat *stat, Datum data)
Definition: tsvector_op.c:2079
#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:488
#define SHORTALIGN(LEN)
Definition: c.h:688
static bool checkclass_str(CHKVAL *chkval, WordEntry *entry, QueryOperand *val, ExecPhraseData *data)
Definition: tsvector_op.c:1184
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:128
#define compareStatWord(a, e, t)
Definition: tsvector_op.c:1975
#define MAXENTRYPOS
Definition: ts_type.h:85
Definition: pg_list.h:50
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:122
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:617
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:268
Datum ts_match_tt(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:1909
Datum ts_match_tq(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:1931
struct StatEntry * right
Definition: tsvector_op.c:51
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:306
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:284
int32 weight
Definition: tsvector_op.c:60
#define OP_NOT
Definition: ts_type.h:166