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