PostgreSQL Source Code git master
Loading...
Searching...
No Matches
tsvector_op.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/htup_details.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "parser/parse_coerce.h"
#include "tsearch/ts_utils.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/regproc.h"
#include "utils/rel.h"
Include dependency graph for tsvector_op.c:

Go to the source code of this file.

Data Structures

struct  CHKVAL
 
struct  StatEntry
 
struct  TSVectorStat
 

Macros

#define STATENTRYHDRSZ   (offsetof(StatEntry, lexeme))
 
#define TSVECTORCMPFUNC(type, action, ret)
 
#define compareEntry(pa, a, pb, b)
 
#define TSPO_L_ONLY   0x01 /* emit positions appearing only in L */
 
#define TSPO_R_ONLY   0x02 /* emit positions appearing only in R */
 
#define TSPO_BOTH   0x04 /* emit positions appearing in both L&R */
 
#define compareStatWord(a, e, t)
 

Typedefs

typedef struct StatEntry StatEntry
 

Functions

static TSTernaryValue TS_execute_recurse (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
 
static bool TS_execute_locations_recurse (QueryItem *curitem, void *arg, TSExecuteCallback chkcond, List **locations)
 
static int tsvector_bsearch (const TSVectorData *tsv, char *lexeme, int lexeme_len)
 
static Datum tsvector_update_trigger (PG_FUNCTION_ARGS, bool config_column)
 
static int silly_cmp_tsvector (const TSVectorData *a, const TSVectorData *b)
 
 TSVECTORCMPFUNC (lt,<, BOOL)
 
 TSVECTORCMPFUNC (le,<=, BOOL)
 
 TSVECTORCMPFUNC (eq,==, BOOL)
 
 TSVECTORCMPFUNC (ge, >=, BOOL)
 
 TSVECTORCMPFUNC (gt, >, BOOL)
 
 TSVECTORCMPFUNC (ne, !=, BOOL)
 
 TSVECTORCMPFUNC (cmp,+, INT32)
 
Datum tsvector_strip (PG_FUNCTION_ARGS)
 
Datum tsvector_length (PG_FUNCTION_ARGS)
 
static int parse_weight (char cw)
 
Datum tsvector_setweight (PG_FUNCTION_ARGS)
 
Datum tsvector_setweight_by_filter (PG_FUNCTION_ARGS)
 
static int32 add_pos (TSVector src, WordEntry *srcptr, TSVector dest, WordEntry *destptr, int32 maxpos)
 
static int compare_int (const void *va, const void *vb)
 
static int compare_text_lexemes (const void *va, const void *vb)
 
static TSVector tsvector_delete_by_indices (TSVector tsv, int *indices_to_delete, int indices_count)
 
Datum tsvector_delete_str (PG_FUNCTION_ARGS)
 
Datum tsvector_delete_arr (PG_FUNCTION_ARGS)
 
Datum tsvector_unnest (PG_FUNCTION_ARGS)
 
Datum tsvector_to_array (PG_FUNCTION_ARGS)
 
Datum array_to_tsvector (PG_FUNCTION_ARGS)
 
Datum tsvector_filter (PG_FUNCTION_ARGS)
 
Datum tsvector_concat (PG_FUNCTION_ARGS)
 
int32 tsCompareString (char *a, int lena, char *b, int lenb, bool prefix)
 
static TSTernaryValue checkclass_str (CHKVAL *chkval, WordEntry *entry, QueryOperand *val, ExecPhraseData *data)
 
static TSTernaryValue checkcondition_str (void *checkval, QueryOperand *val, ExecPhraseData *data)
 
static TSTernaryValue TS_phrase_output (ExecPhraseData *data, ExecPhraseData *Ldata, ExecPhraseData *Rdata, int emit, int Loffset, int Roffset, int max_npos)
 
static TSTernaryValue TS_phrase_execute (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond, ExecPhraseData *data)
 
bool TS_execute (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
 
TSTernaryValue TS_execute_ternary (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
 
ListTS_execute_locations (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
 
bool tsquery_requires_match (QueryItem *curitem)
 
Datum ts_match_qv (PG_FUNCTION_ARGS)
 
Datum ts_match_vq (PG_FUNCTION_ARGS)
 
Datum ts_match_tt (PG_FUNCTION_ARGS)
 
Datum ts_match_tq (PG_FUNCTION_ARGS)
 
static int check_weight (TSVector txt, WordEntry *wptr, int8 weight)
 
static void insertStatEntry (MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 off)
 
static void chooseNextStatEntry (MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 low, uint32 high, uint32 offset)
 
static TSVectorStatts_accum (MemoryContext persistentContext, TSVectorStat *stat, Datum data)
 
static void ts_setup_firstcall (FunctionCallInfo fcinfo, FuncCallContext *funcctx, TSVectorStat *stat)
 
static StatEntrywalkStatEntryTree (TSVectorStat *stat)
 
static Datum ts_process_call (FuncCallContext *funcctx)
 
static TSVectorStatts_stat_sql (MemoryContext persistentContext, text *txt, text *ws)
 
Datum ts_stat1 (PG_FUNCTION_ARGS)
 
Datum ts_stat2 (PG_FUNCTION_ARGS)
 
Datum tsvector_update_trigger_byid (PG_FUNCTION_ARGS)
 
Datum tsvector_update_trigger_bycolumn (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ compareEntry

#define compareEntry (   pa,
  a,
  pb,
  b 
)
Value:
tsCompareString((pa) + (a)->pos, (a)->len, \
(pb) + (b)->pos, (b)->len, \
false)
int b
Definition isn.c:74
int a
Definition isn.c:73
const void size_t len
static int fb(int x)
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)

Definition at line 349 of file tsvector_op.c.

361{
362 uint16 *clen = &_POSVECPTR(dest, destptr)->npos;
363 int i;
365 startlen;
367 *dpos = POSDATAPTR(dest, destptr);
368
369 if (!destptr->haspos)
370 *clen = 0;
371
372 startlen = *clen;
373 for (i = 0;
374 i < slen && *clen < MAXNUMPOS &&
375 (*clen == 0 || WEP_GETPOS(dpos[*clen - 1]) != MAXENTRYPOS - 1);
376 i++)
377 {
380 (*clen)++;
381 }
382
383 if (*clen != startlen)
384 destptr->haspos = 1;
385 return *clen - startlen;
386}
387
388/*
389 * Perform binary search of given lexeme in TSVector.
390 * Returns lexeme position in TSVector's entry array or -1 if lexeme wasn't
391 * found.
392 */
393static int
394tsvector_bsearch(const TSVectorData *tsv, char *lexeme, int lexeme_len)
395{
396 const WordEntry *arrin = ARRPTR(tsv);
397 int StopLow = 0,
398 StopHigh = tsv->size,
400 cmp;
401
402 while (StopLow < StopHigh)
403 {
404 StopMiddle = (StopLow + StopHigh) / 2;
405
407 STRPTR(tsv) + arrin[StopMiddle].pos,
409 false);
410
411 if (cmp < 0)
413 else if (cmp > 0)
414 StopLow = StopMiddle + 1;
415 else /* found it */
416 return StopMiddle;
417 }
418
419 return -1;
420}
421
422/*
423 * qsort comparator functions
424 */
425
426static int
427compare_int(const void *va, const void *vb)
428{
429 int a = *((const int *) va);
430 int b = *((const int *) vb);
431
432 return pg_cmp_s32(a, b);
433}
434
435static int
436compare_text_lexemes(const void *va, const void *vb)
437{
438 Datum a = *((const Datum *) va);
439 Datum b = *((const Datum *) vb);
444
445 return tsCompareString(alex, alex_len, blex, blex_len, false);
446}
447
448/*
449 * Internal routine to delete lexemes from TSVector by array of offsets.
450 *
451 * int *indices_to_delete -- array of lexeme offsets to delete (modified here!)
452 * int indices_count -- size of that array
453 *
454 * Returns new TSVector without given lexemes along with their positions
455 * and weights.
456 */
457static TSVector
459 int indices_count)
460{
463 *arrout;
464 char *data = STRPTR(tsv),
465 *dataout;
466 int i, /* index in arrin */
467 j, /* index in arrout */
468 k, /* index in indices_to_delete */
469 curoff; /* index in dataout area */
470
471 /*
472 * Sort the filter array to simplify membership checks below. Also, get
473 * rid of any duplicate entries, so that we can assume that indices_count
474 * is exactly equal to the number of lexemes that will be removed.
475 */
476 if (indices_count > 1)
477 {
481 }
482
483 /*
484 * Here we overestimate tsout size, since we don't know how much space is
485 * used by the deleted lexeme(s). We will set exact size below.
486 */
488
489 /* This count must be correct because STRPTR(tsout) relies on it. */
490 tsout->size = tsv->size - indices_count;
491
492 /*
493 * Copy tsv to tsout, skipping lexemes listed in indices_to_delete.
494 */
497 curoff = 0;
498 for (i = j = k = 0; i < tsv->size; i++)
499 {
500 /*
501 * If current i is present in indices_to_delete, skip this lexeme.
502 * Since indices_to_delete is already sorted, we only need to check
503 * the current (k'th) entry.
504 */
505 if (k < indices_count && i == indices_to_delete[k])
506 {
507 k++;
508 continue;
509 }
510
511 /* Copy lexeme and its positions and weights */
512 memcpy(dataout + curoff, data + arrin[i].pos, arrin[i].len);
513 arrout[j].haspos = arrin[i].haspos;
514 arrout[j].len = arrin[i].len;
515 arrout[j].pos = curoff;
516 curoff += arrin[i].len;
517 if (arrin[i].haspos)
518 {
519 int len = POSDATALEN(tsv, arrin + i) * sizeof(WordEntryPos)
520 + sizeof(uint16);
521
524 STRPTR(tsv) + SHORTALIGN(arrin[i].pos + arrin[i].len),
525 len);
526 curoff += len;
527 }
528
529 j++;
530 }
531
532 /*
533 * k should now be exactly equal to indices_count. If it isn't then the
534 * caller provided us with indices outside of [0, tsv->size) range and
535 * estimation of tsout's size is wrong.
536 */
537 Assert(k == indices_count);
538
540 return tsout;
541}
542
543/*
544 * Delete given lexeme from tsvector.
545 * Implementation of user-level ts_delete(tsvector, text).
546 */
547Datum
549{
551 tsout;
553 char *lexeme = VARDATA_ANY(tlexeme);
556
557 if ((skip_index = tsvector_bsearch(tsin, lexeme, lexeme_len)) == -1)
559
561
565}
566
567/*
568 * Delete given array of lexemes from tsvector.
569 * Implementation of user-level ts_delete(tsvector, text[]).
570 */
571Datum
573{
575 tsout;
577 int i,
578 nlex,
582 bool *nulls;
583
585
586 /*
587 * In typical use case array of lexemes to delete is relatively small. So
588 * here we optimize things for that scenario: iterate through lexarr
589 * performing binary search of each lexeme from lexarr in tsvector.
590 */
591 skip_indices = palloc0(nlex * sizeof(int));
592 for (i = skip_count = 0; i < nlex; i++)
593 {
594 char *lex;
595 int lex_len,
596 lex_pos;
597
598 /* Ignore null array elements, they surely don't match */
599 if (nulls[i])
600 continue;
601
605
606 if (lex_pos >= 0)
608 }
609
611
615
617}
618
619/*
620 * Expand tsvector as table with following columns:
621 * lexeme: lexeme text
622 * positions: integer array of lexeme positions
623 * weights: char array of weights corresponding to positions
624 */
625Datum
627{
630
631 if (SRF_IS_FIRSTCALL())
632 {
633 MemoryContext oldcontext;
634 TupleDesc tupdesc;
635
637 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
638
639 tupdesc = CreateTemplateTupleDesc(3);
640 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "lexeme",
641 TEXTOID, -1, 0);
642 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "positions",
643 INT2ARRAYOID, -1, 0);
644 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "weights",
645 TEXTARRAYOID, -1, 0);
646 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
647 elog(ERROR, "return type must be a row type");
648 TupleDescFinalize(tupdesc);
649 funcctx->tuple_desc = tupdesc;
650
651 funcctx->user_fctx = PG_GETARG_TSVECTOR_COPY(0);
652
653 MemoryContextSwitchTo(oldcontext);
654 }
655
657 tsin = (TSVector) funcctx->user_fctx;
658
659 if (funcctx->call_cntr < tsin->size)
660 {
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
670
671 if (arrin[i].haspos)
672 {
675 Datum *weights;
676 char weight;
677
678 /*
679 * Internally tsvector stores position and weight in the same
680 * uint16 (2 bits for weight, 14 for position). Here we extract
681 * that in two separate arrays.
682 */
683 posv = _POSVECPTR(tsin, arrin + i);
684 positions = palloc(posv->npos * sizeof(Datum));
685 weights = palloc(posv->npos * sizeof(Datum));
686 for (j = 0; j < posv->npos; j++)
687 {
689 weight = 'D' - WEP_GETWEIGHT(posv->pos[j]);
691 1));
692 }
693
696 }
697 else
698 {
699 nulls[1] = nulls[2] = true;
700 }
701
702 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
704 }
705 else
706 {
708 }
709}
710
711/*
712 * Convert tsvector to array of lexemes.
713 */
714Datum
716{
719 Datum *elements;
720 int i;
721 ArrayType *array;
722
723 elements = palloc(tsin->size * sizeof(Datum));
724
725 for (i = 0; i < tsin->size; i++)
726 {
728 arrin[i].len));
729 }
730
731 array = construct_array_builtin(elements, tsin->size, TEXTOID);
732
733 pfree(elements);
735 PG_RETURN_POINTER(array);
736}
737
738/*
739 * Build tsvector from array of lexemes.
740 */
741Datum
743{
748 bool *nulls;
749 int nitems,
750 i,
751 tslen,
752 datalen = 0;
753 char *cur;
754
756
757 /*
758 * Reject nulls and zero length strings (maybe we should just ignore them,
759 * instead?)
760 */
761 for (i = 0; i < nitems; i++)
762 {
763 if (nulls[i])
766 errmsg("lexeme array may not contain nulls")));
767
771 errmsg("lexeme array may not contain empty strings")));
772 }
773
774 /* Sort and de-dup, because this is required for a valid tsvector. */
775 if (nitems > 1)
776 {
778 nitems = qunique(dlexemes, nitems, sizeof(Datum),
780 }
781
782 /* Calculate space needed for surviving lexemes. */
783 for (i = 0; i < nitems; i++)
784 datalen += VARSIZE(DatumGetPointer(dlexemes[i])) - VARHDRSZ;
785 tslen = CALCDATASIZE(nitems, datalen);
786
787 /* Allocate and fill tsvector. */
790 tsout->size = nitems;
791
793 cur = STRPTR(tsout);
794 for (i = 0; i < nitems; i++)
795 {
796 char *lex = VARDATA(DatumGetPointer(dlexemes[i]));
798
799 memcpy(cur, lex, lex_len);
800 arrout[i].haspos = 0;
801 arrout[i].len = lex_len;
802 arrout[i].pos = cur - STRPTR(tsout);
803 cur += lex_len;
804 }
805
806 PG_FREE_IF_COPY(v, 0);
808}
809
810/*
811 * ts_filter(): keep only lexemes with given weights in tsvector.
812 */
813Datum
815{
817 tsout;
820 *arrout;
821 char *datain = STRPTR(tsin),
822 *dataout;
824 bool *nulls;
825 int nweights;
826 int i,
827 j;
828 int cur_pos = 0;
829 char mask = 0;
830
832
833 for (i = 0; i < nweights; i++)
834 {
835 char char_weight;
836
837 if (nulls[i])
840 errmsg("weight array may not contain nulls")));
841
843 mask |= 1 << parse_weight(char_weight);
844 }
845
847 tsout->size = tsin->size;
850
851 for (i = j = 0; i < tsin->size; i++)
852 {
854 *posvout;
855 int npos = 0;
856 int k;
857
858 if (!arrin[i].haspos)
859 continue;
860
864
865 for (k = 0; k < posvin->npos; k++)
866 {
867 if (mask & (1 << WEP_GETWEIGHT(posvin->pos[k])))
868 posvout->pos[npos++] = posvin->pos[k];
869 }
870
871 /* if no satisfactory positions found, skip lexeme */
872 if (!npos)
873 continue;
874
875 arrout[j].haspos = true;
876 arrout[j].len = arrin[i].len;
877 arrout[j].pos = cur_pos;
878
880 posvout->npos = npos;
882 cur_pos += POSDATALEN(tsout, arrout + j) * sizeof(WordEntryPos) +
883 sizeof(uint16);
884 j++;
885 }
886
887 tsout->size = j;
888 if (dataout != STRPTR(tsout))
890
892
895}
896
897Datum
899{
902 TSVector out;
903 WordEntry *ptr;
905 *ptr2;
906 WordEntryPos *p;
907 int maxpos = 0,
908 i,
909 j,
910 i1,
911 i2,
912 dataoff,
915 char *data,
916 *data1,
917 *data2;
918
919 /* Get max position in in1; we'll need this to offset in2's positions */
920 ptr = ARRPTR(in1);
921 i = in1->size;
922 while (i--)
923 {
924 if ((j = POSDATALEN(in1, ptr)) != 0)
925 {
926 p = POSDATAPTR(in1, ptr);
927 while (j--)
928 {
929 if (WEP_GETPOS(*p) > maxpos)
930 maxpos = WEP_GETPOS(*p);
931 p++;
932 }
933 }
934 ptr++;
935 }
936
937 ptr1 = ARRPTR(in1);
938 ptr2 = ARRPTR(in2);
939 data1 = STRPTR(in1);
940 data2 = STRPTR(in2);
941 i1 = in1->size;
942 i2 = in2->size;
943
944 /*
945 * Conservative estimate of space needed. We might need all the data in
946 * both inputs, and conceivably add a pad byte before position data for
947 * each item where there was none before.
948 */
949 output_bytes = VARSIZE(in1) + VARSIZE(in2) + i1 + i2;
950
953
954 /*
955 * We must make out->size valid so that STRPTR(out) is sensible. We'll
956 * collapse out any unused space at the end.
957 */
958 out->size = in1->size + in2->size;
959
960 ptr = ARRPTR(out);
961 data = STRPTR(out);
962 dataoff = 0;
963 while (i1 && i2)
964 {
966
967 if (cmp < 0)
968 { /* in1 first */
969 ptr->haspos = ptr1->haspos;
970 ptr->len = ptr1->len;
971 memcpy(data + dataoff, data1 + ptr1->pos, ptr1->len);
972 ptr->pos = dataoff;
973 dataoff += ptr1->len;
974 if (ptr->haspos)
975 {
978 dataoff += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16);
979 }
980
981 ptr++;
982 ptr1++;
983 i1--;
984 }
985 else if (cmp > 0)
986 { /* in2 first */
987 ptr->haspos = ptr2->haspos;
988 ptr->len = ptr2->len;
989 memcpy(data + dataoff, data2 + ptr2->pos, ptr2->len);
990 ptr->pos = dataoff;
991 dataoff += ptr2->len;
992 if (ptr->haspos)
993 {
994 int addlen = add_pos(in2, ptr2, out, ptr, maxpos);
995
996 if (addlen == 0)
997 ptr->haspos = 0;
998 else
999 {
1001 dataoff += addlen * sizeof(WordEntryPos) + sizeof(uint16);
1002 }
1003 }
1004
1005 ptr++;
1006 ptr2++;
1007 i2--;
1008 }
1009 else
1010 {
1011 ptr->haspos = ptr1->haspos | ptr2->haspos;
1012 ptr->len = ptr1->len;
1013 memcpy(data + dataoff, data1 + ptr1->pos, ptr1->len);
1014 ptr->pos = dataoff;
1015 dataoff += ptr1->len;
1016 if (ptr->haspos)
1017 {
1018 if (ptr1->haspos)
1019 {
1021 memcpy(data + dataoff, _POSVECPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16));
1022 dataoff += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16);
1023 if (ptr2->haspos)
1024 dataoff += add_pos(in2, ptr2, out, ptr, maxpos) * sizeof(WordEntryPos);
1025 }
1026 else /* must have ptr2->haspos */
1027 {
1028 int addlen = add_pos(in2, ptr2, out, ptr, maxpos);
1029
1030 if (addlen == 0)
1031 ptr->haspos = 0;
1032 else
1033 {
1035 dataoff += addlen * sizeof(WordEntryPos) + sizeof(uint16);
1036 }
1037 }
1038 }
1039
1040 ptr++;
1041 ptr1++;
1042 ptr2++;
1043 i1--;
1044 i2--;
1045 }
1046 }
1047
1048 while (i1)
1049 {
1050 ptr->haspos = ptr1->haspos;
1051 ptr->len = ptr1->len;
1052 memcpy(data + dataoff, data1 + ptr1->pos, ptr1->len);
1053 ptr->pos = dataoff;
1054 dataoff += ptr1->len;
1055 if (ptr->haspos)
1056 {
1058 memcpy(data + dataoff, _POSVECPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16));
1059 dataoff += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16);
1060 }
1061
1062 ptr++;
1063 ptr1++;
1064 i1--;
1065 }
1066
1067 while (i2)
1068 {
1069 ptr->haspos = ptr2->haspos;
1070 ptr->len = ptr2->len;
1071 memcpy(data + dataoff, data2 + ptr2->pos, ptr2->len);
1072 ptr->pos = dataoff;
1073 dataoff += ptr2->len;
1074 if (ptr->haspos)
1075 {
1076 int addlen = add_pos(in2, ptr2, out, ptr, maxpos);
1077
1078 if (addlen == 0)
1079 ptr->haspos = 0;
1080 else
1081 {
1083 dataoff += addlen * sizeof(WordEntryPos) + sizeof(uint16);
1084 }
1085 }
1086
1087 ptr++;
1088 ptr2++;
1089 i2--;
1090 }
1091
1092 /*
1093 * Instead of checking each offset individually, we check for overflow of
1094 * pos fields once at the end.
1095 */
1096 if (dataoff > MAXSTRPOS)
1097 ereport(ERROR,
1099 errmsg("string is too long for tsvector (%d bytes, max %d bytes)", dataoff, MAXSTRPOS)));
1100
1101 /*
1102 * Adjust sizes (asserting that we didn't overrun the original estimates)
1103 * and collapse out any unused array entries.
1104 */
1105 output_size = ptr - ARRPTR(out);
1107 out->size = output_size;
1108 if (data != STRPTR(out))
1109 memmove(STRPTR(out), data, dataoff);
1111 Assert(output_bytes <= VARSIZE(out));
1113
1114 PG_FREE_IF_COPY(in1, 0);
1115 PG_FREE_IF_COPY(in2, 1);
1116 PG_RETURN_POINTER(out);
1117}
1118
1119/*
1120 * Compare two strings by tsvector rules.
1121 *
1122 * if prefix = true then it returns zero value iff b has prefix a
1123 */
1124int32
1125tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
1126{
1127 int cmp;
1128
1129 if (lena == 0)
1130 {
1131 if (prefix)
1132 cmp = 0; /* empty string is prefix of anything */
1133 else
1134 cmp = (lenb > 0) ? -1 : 0;
1135 }
1136 else if (lenb == 0)
1137 {
1138 cmp = (lena > 0) ? 1 : 0;
1139 }
1140 else
1141 {
1142 cmp = memcmp(a, b, Min((unsigned int) lena, (unsigned int) lenb));
1143
1144 if (prefix)
1145 {
1146 if (cmp == 0 && lena > lenb)
1147 cmp = 1; /* a is longer, so not a prefix of b */
1148 }
1149 else if (cmp == 0 && lena != lenb)
1150 {
1151 cmp = (lena < lenb) ? -1 : 1;
1152 }
1153 }
1154
1155 return cmp;
1156}
1157
1158/*
1159 * Check weight info or/and fill 'data' with the required positions
1160 */
1161static TSTernaryValue
1164{
1166
1167 Assert(data == NULL || data->npos == 0);
1168
1169 if (entry->haspos)
1170 {
1172
1173 /*
1174 * We can't use the _POSVECPTR macro here because the pointer to the
1175 * tsvector's lexeme storage is already contained in chkval->values.
1176 */
1178 (chkval->values + SHORTALIGN(entry->pos + entry->len));
1179
1180 if (val->weight && data)
1181 {
1184
1185 /*
1186 * Filter position information by weights
1187 */
1188 dptr = data->pos = palloc_array(WordEntryPos, posvec->npos);
1189 data->allocated = true;
1190
1191 /* Is there a position with a matching weight? */
1192 while (posvec_iter < posvec->pos + posvec->npos)
1193 {
1194 /* If true, append this position to the data->pos */
1195 if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1196 {
1198 dptr++;
1199 }
1200
1201 posvec_iter++;
1202 }
1203
1204 data->npos = dptr - data->pos;
1205
1206 if (data->npos > 0)
1207 result = TS_YES;
1208 else
1209 {
1210 pfree(data->pos);
1211 data->pos = NULL;
1212 data->allocated = false;
1213 }
1214 }
1215 else if (val->weight)
1216 {
1218
1219 /* Is there a position with a matching weight? */
1220 while (posvec_iter < posvec->pos + posvec->npos)
1221 {
1222 if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1223 {
1224 result = TS_YES;
1225 break; /* no need to go further */
1226 }
1227
1228 posvec_iter++;
1229 }
1230 }
1231 else if (data)
1232 {
1233 data->npos = posvec->npos;
1234 data->pos = posvec->pos;
1235 data->allocated = false;
1236 result = TS_YES;
1237 }
1238 else
1239 {
1240 /* simplest case: no weight check, positions not needed */
1241 result = TS_YES;
1242 }
1243 }
1244 else
1245 {
1246 /*
1247 * Position info is lacking, so if the caller requires it, we can only
1248 * say that maybe there is a match.
1249 *
1250 * Notice, however, that we *don't* check val->weight here.
1251 * Historically, stripped tsvectors are considered to match queries
1252 * whether or not the query has a weight restriction; that's a little
1253 * dubious but we'll preserve the behavior.
1254 */
1255 if (data)
1256 result = TS_MAYBE;
1257 else
1258 result = TS_YES;
1259 }
1260
1261 return result;
1262}
1263
1264/*
1265 * TS_execute callback for matching a tsquery operand to plain tsvector data
1266 */
1267static TSTernaryValue
1269{
1271 WordEntry *StopLow = chkval->arrb;
1272 WordEntry *StopHigh = chkval->arre;
1274 TSTernaryValue res = TS_NO;
1275
1276 /* Loop invariant: StopLow <= val < StopHigh */
1277 while (StopLow < StopHigh)
1278 {
1279 int difference;
1280
1281 StopMiddle = StopLow + (StopHigh - StopLow) / 2;
1282 difference = tsCompareString(chkval->operand + val->distance,
1283 val->length,
1284 chkval->values + StopMiddle->pos,
1285 StopMiddle->len,
1286 false);
1287
1288 if (difference == 0)
1289 {
1290 /* Check weight info & fill 'data' with positions */
1292 break;
1293 }
1294 else if (difference > 0)
1295 StopLow = StopMiddle + 1;
1296 else
1298 }
1299
1300 /*
1301 * If it's a prefix search, we should also consider lexemes that the
1302 * search term is a prefix of (which will necessarily immediately follow
1303 * the place we found in the above loop). But we can skip them if there
1304 * was a definite match on the exact term AND the caller doesn't need
1305 * position info.
1306 */
1307 if (val->prefix && (res != TS_YES || data))
1308 {
1310 int npos = 0,
1311 totalpos = 0;
1312
1313 /* adjust start position for corner case */
1314 if (StopLow >= StopHigh)
1316
1317 /* we don't try to re-use any data from the initial match */
1318 if (data)
1319 {
1320 if (data->allocated)
1321 pfree(data->pos);
1322 data->pos = NULL;
1323 data->allocated = false;
1324 data->npos = 0;
1325 }
1326 res = TS_NO;
1327
1328 while ((res != TS_YES || data) &&
1330 tsCompareString(chkval->operand + val->distance,
1331 val->length,
1332 chkval->values + StopMiddle->pos,
1333 StopMiddle->len,
1334 true) == 0)
1335 {
1337
1339
1340 if (subres != TS_NO)
1341 {
1342 if (data)
1343 {
1344 /*
1345 * We need to join position information
1346 */
1347 if (subres == TS_MAYBE)
1348 {
1349 /*
1350 * No position info for this match, so we must report
1351 * MAYBE overall.
1352 */
1353 res = TS_MAYBE;
1354 /* forget any previous positions */
1355 npos = 0;
1356 /* don't leak storage */
1357 if (allpos)
1358 pfree(allpos);
1359 break;
1360 }
1361
1362 while (npos + data->npos > totalpos)
1363 {
1364 if (totalpos == 0)
1365 {
1366 totalpos = 256;
1368 }
1369 else
1370 {
1371 totalpos *= 2;
1373 }
1374 }
1375
1376 memcpy(allpos + npos, data->pos, sizeof(WordEntryPos) * data->npos);
1377 npos += data->npos;
1378
1379 /* don't leak storage from individual matches */
1380 if (data->allocated)
1381 pfree(data->pos);
1382 data->pos = NULL;
1383 data->allocated = false;
1384 /* it's important to reset data->npos before next loop */
1385 data->npos = 0;
1386 }
1387 else
1388 {
1389 /* Don't need positions, just handle YES/MAYBE */
1390 if (subres == TS_YES || res == TS_NO)
1391 res = subres;
1392 }
1393 }
1394
1395 StopMiddle++;
1396 }
1397
1398 if (data && npos > 0)
1399 {
1400 /* Sort and make unique array of found positions */
1401 data->pos = allpos;
1402 qsort(data->pos, npos, sizeof(WordEntryPos), compareWordEntryPos);
1403 data->npos = qunique(data->pos, npos, sizeof(WordEntryPos),
1405 data->allocated = true;
1406 res = TS_YES;
1407 }
1408 }
1409
1410 return res;
1411}
1412
1413/*
1414 * Compute output position list for a tsquery operator in phrase mode.
1415 *
1416 * Merge the position lists in Ldata and Rdata as specified by "emit",
1417 * returning the result list into *data. The input position lists must be
1418 * sorted and unique, and the output will be as well.
1419 *
1420 * data: pointer to initially-all-zeroes output struct, or NULL
1421 * Ldata, Rdata: input position lists
1422 * emit: bitmask of TSPO_XXX flags
1423 * Loffset: offset to be added to Ldata positions before comparing/outputting
1424 * Roffset: offset to be added to Rdata positions before comparing/outputting
1425 * max_npos: maximum possible required size of output position array
1426 *
1427 * Loffset and Roffset should not be negative, else we risk trying to output
1428 * negative positions, which won't fit into WordEntryPos.
1429 *
1430 * The result is boolean (TS_YES or TS_NO), but for the caller's convenience
1431 * we return it as TSTernaryValue.
1432 *
1433 * Returns TS_YES if any positions were emitted to *data; or if data is NULL,
1434 * returns TS_YES if any positions would have been emitted.
1435 */
1436#define TSPO_L_ONLY 0x01 /* emit positions appearing only in L */
1437#define TSPO_R_ONLY 0x02 /* emit positions appearing only in R */
1438#define TSPO_BOTH 0x04 /* emit positions appearing in both L&R */
1439
1440static TSTernaryValue
1444 int emit,
1445 int Loffset,
1446 int Roffset,
1447 int max_npos)
1448{
1449 int Lindex,
1450 Rindex;
1451
1452 /* Loop until both inputs are exhausted */
1453 Lindex = Rindex = 0;
1454 while (Lindex < Ldata->npos || Rindex < Rdata->npos)
1455 {
1456 int Lpos,
1457 Rpos;
1458 int output_pos = 0;
1459
1460 /*
1461 * Fetch current values to compare. WEP_GETPOS() is needed because
1462 * ExecPhraseData->data can point to a tsvector's WordEntryPosVector.
1463 */
1464 if (Lindex < Ldata->npos)
1465 Lpos = WEP_GETPOS(Ldata->pos[Lindex]) + Loffset;
1466 else
1467 {
1468 /* L array exhausted, so we're done if R_ONLY isn't set */
1469 if (!(emit & TSPO_R_ONLY))
1470 break;
1471 Lpos = INT_MAX;
1472 }
1473 if (Rindex < Rdata->npos)
1474 Rpos = WEP_GETPOS(Rdata->pos[Rindex]) + Roffset;
1475 else
1476 {
1477 /* R array exhausted, so we're done if L_ONLY isn't set */
1478 if (!(emit & TSPO_L_ONLY))
1479 break;
1480 Rpos = INT_MAX;
1481 }
1482
1483 /* Merge-join the two input lists */
1484 if (Lpos < Rpos)
1485 {
1486 /* Lpos is not matched in Rdata, should we output it? */
1487 if (emit & TSPO_L_ONLY)
1488 output_pos = Lpos;
1489 Lindex++;
1490 }
1491 else if (Lpos == Rpos)
1492 {
1493 /* Lpos and Rpos match ... should we output it? */
1494 if (emit & TSPO_BOTH)
1495 output_pos = Rpos;
1496 Lindex++;
1497 Rindex++;
1498 }
1499 else /* Lpos > Rpos */
1500 {
1501 /* Rpos is not matched in Ldata, should we output it? */
1502 if (emit & TSPO_R_ONLY)
1503 output_pos = Rpos;
1504 Rindex++;
1505 }
1506
1507 if (output_pos > 0)
1508 {
1509 if (data)
1510 {
1511 /* Store position, first allocating output array if needed */
1512 if (data->pos == NULL)
1513 {
1514 data->pos = (WordEntryPos *)
1515 palloc(max_npos * sizeof(WordEntryPos));
1516 data->allocated = true;
1517 }
1518 data->pos[data->npos++] = output_pos;
1519 }
1520 else
1521 {
1522 /*
1523 * Exact positions not needed, so return TS_YES as soon as we
1524 * know there is at least one.
1525 */
1526 return TS_YES;
1527 }
1528 }
1529 }
1530
1531 if (data && data->npos > 0)
1532 {
1533 /* Let's assert we didn't overrun the array */
1534 Assert(data->npos <= max_npos);
1535 return TS_YES;
1536 }
1537 return TS_NO;
1538}
1539
1540/*
1541 * Execute tsquery at or below an OP_PHRASE operator.
1542 *
1543 * This handles tsquery execution at recursion levels where we need to care
1544 * about match locations.
1545 *
1546 * In addition to the same arguments used for TS_execute, the caller may pass
1547 * a preinitialized-to-zeroes ExecPhraseData struct, to be filled with lexeme
1548 * match position info on success. data == NULL if no position data need be
1549 * returned.
1550 * Note: the function assumes data != NULL for operators other than OP_PHRASE.
1551 * This is OK because an outside call always starts from an OP_PHRASE node,
1552 * and all internal recursion cases pass data != NULL.
1553 *
1554 * The detailed semantics of the match data, given that the function returned
1555 * TS_YES (successful match), are:
1556 *
1557 * npos > 0, negate = false:
1558 * query is matched at specified position(s) (and only those positions)
1559 * npos > 0, negate = true:
1560 * query is matched at all positions *except* specified position(s)
1561 * npos = 0, negate = true:
1562 * query is matched at all positions
1563 * npos = 0, negate = false:
1564 * disallowed (this should result in TS_NO or TS_MAYBE, as appropriate)
1565 *
1566 * Successful matches also return a "width" value which is the match width in
1567 * lexemes, less one. Hence, "width" is zero for simple one-lexeme matches,
1568 * and is the sum of the phrase operator distances for phrase matches. Note
1569 * that when width > 0, the listed positions represent the ends of matches not
1570 * the starts. (This unintuitive rule is needed to avoid possibly generating
1571 * negative positions, which wouldn't fit into the WordEntryPos arrays.)
1572 *
1573 * If the TSExecuteCallback function reports that an operand is present
1574 * but fails to provide position(s) for it, we will return TS_MAYBE when
1575 * it is possible but not certain that the query is matched.
1576 *
1577 * When the function returns TS_NO or TS_MAYBE, it must return npos = 0,
1578 * negate = false (which is the state initialized by the caller); but the
1579 * "width" output in such cases is undefined.
1580 */
1581static TSTernaryValue
1582TS_phrase_execute(QueryItem *curitem, void *arg, uint32 flags,
1585{
1587 Rdata;
1589 rmatch;
1590 int Loffset,
1591 Roffset,
1592 maxwidth;
1593
1594 /* since this function recurses, it could be driven to stack overflow */
1596
1597 /* ... and let's check for query cancel while we're at it */
1599
1600 if (curitem->type == QI_VAL)
1601 return chkcond(arg, (QueryOperand *) curitem, data);
1602
1603 switch (curitem->qoperator.oper)
1604 {
1605 case OP_NOT:
1606
1607 /*
1608 * We need not touch data->width, since a NOT operation does not
1609 * change the match width.
1610 */
1611 if (flags & TS_EXEC_SKIP_NOT)
1612 {
1613 /* with SKIP_NOT, report NOT as "match everywhere" */
1614 Assert(data->npos == 0 && !data->negate);
1615 data->negate = true;
1616 return TS_YES;
1617 }
1618 switch (TS_phrase_execute(curitem + 1, arg, flags, chkcond, data))
1619 {
1620 case TS_NO:
1621 /* change "match nowhere" to "match everywhere" */
1622 Assert(data->npos == 0 && !data->negate);
1623 data->negate = true;
1624 return TS_YES;
1625 case TS_YES:
1626 if (data->npos > 0)
1627 {
1628 /* we have some positions, invert negate flag */
1629 data->negate = !data->negate;
1630 return TS_YES;
1631 }
1632 else if (data->negate)
1633 {
1634 /* change "match everywhere" to "match nowhere" */
1635 data->negate = false;
1636 return TS_NO;
1637 }
1638 /* Should not get here if result was TS_YES */
1639 Assert(false);
1640 break;
1641 case TS_MAYBE:
1642 /* match positions are, and remain, uncertain */
1643 return TS_MAYBE;
1644 }
1645 break;
1646
1647 case OP_PHRASE:
1648 case OP_AND:
1649 memset(&Ldata, 0, sizeof(Ldata));
1650 memset(&Rdata, 0, sizeof(Rdata));
1651
1652 lmatch = TS_phrase_execute(curitem + curitem->qoperator.left,
1653 arg, flags, chkcond, &Ldata);
1654 if (lmatch == TS_NO)
1655 return TS_NO;
1656
1657 rmatch = TS_phrase_execute(curitem + 1,
1658 arg, flags, chkcond, &Rdata);
1659 if (rmatch == TS_NO)
1660 return TS_NO;
1661
1662 /*
1663 * If either operand has no position information, then we can't
1664 * return reliable position data, only a MAYBE result.
1665 */
1666 if (lmatch == TS_MAYBE || rmatch == TS_MAYBE)
1667 return TS_MAYBE;
1668
1669 if (curitem->qoperator.oper == OP_PHRASE)
1670 {
1671 /*
1672 * Compute Loffset and Roffset suitable for phrase match, and
1673 * compute overall width of whole phrase match.
1674 */
1675 Loffset = curitem->qoperator.distance + Rdata.width;
1676 Roffset = 0;
1677 if (data)
1678 data->width = curitem->qoperator.distance +
1679 Ldata.width + Rdata.width;
1680 }
1681 else
1682 {
1683 /*
1684 * For OP_AND, set output width and alignment like OP_OR (see
1685 * comment below)
1686 */
1687 maxwidth = Max(Ldata.width, Rdata.width);
1688 Loffset = maxwidth - Ldata.width;
1689 Roffset = maxwidth - Rdata.width;
1690 if (data)
1691 data->width = maxwidth;
1692 }
1693
1694 if (Ldata.negate && Rdata.negate)
1695 {
1696 /* !L & !R: treat as !(L | R) */
1700 Ldata.npos + Rdata.npos);
1701 if (data)
1702 data->negate = true;
1703 return TS_YES;
1704 }
1705 else if (Ldata.negate)
1706 {
1707 /* !L & R */
1708 return TS_phrase_output(data, &Ldata, &Rdata,
1711 Rdata.npos);
1712 }
1713 else if (Rdata.negate)
1714 {
1715 /* L & !R */
1716 return TS_phrase_output(data, &Ldata, &Rdata,
1719 Ldata.npos);
1720 }
1721 else
1722 {
1723 /* straight AND */
1724 return TS_phrase_output(data, &Ldata, &Rdata,
1725 TSPO_BOTH,
1727 Min(Ldata.npos, Rdata.npos));
1728 }
1729
1730 case OP_OR:
1731 memset(&Ldata, 0, sizeof(Ldata));
1732 memset(&Rdata, 0, sizeof(Rdata));
1733
1734 lmatch = TS_phrase_execute(curitem + curitem->qoperator.left,
1735 arg, flags, chkcond, &Ldata);
1736 rmatch = TS_phrase_execute(curitem + 1,
1737 arg, flags, chkcond, &Rdata);
1738
1739 if (lmatch == TS_NO && rmatch == TS_NO)
1740 return TS_NO;
1741
1742 /*
1743 * If either operand has no position information, then we can't
1744 * return reliable position data, only a MAYBE result.
1745 */
1746 if (lmatch == TS_MAYBE || rmatch == TS_MAYBE)
1747 return TS_MAYBE;
1748
1749 /*
1750 * Cope with undefined output width from failed submatch. (This
1751 * takes less code than trying to ensure that all failure returns
1752 * set data->width to zero.)
1753 */
1754 if (lmatch == TS_NO)
1755 Ldata.width = 0;
1756 if (rmatch == TS_NO)
1757 Rdata.width = 0;
1758
1759 /*
1760 * For OP_AND and OP_OR, report the width of the wider of the two
1761 * inputs, and align the narrower input's positions to the right
1762 * end of that width. This rule deals at least somewhat
1763 * reasonably with cases like "x <-> (y | z <-> q)".
1764 */
1765 maxwidth = Max(Ldata.width, Rdata.width);
1766 Loffset = maxwidth - Ldata.width;
1767 Roffset = maxwidth - Rdata.width;
1768 data->width = maxwidth;
1769
1770 if (Ldata.negate && Rdata.negate)
1771 {
1772 /* !L | !R: treat as !(L & R) */
1774 TSPO_BOTH,
1776 Min(Ldata.npos, Rdata.npos));
1777 data->negate = true;
1778 return TS_YES;
1779 }
1780 else if (Ldata.negate)
1781 {
1782 /* !L | R: treat as !(L & !R) */
1786 Ldata.npos);
1787 data->negate = true;
1788 return TS_YES;
1789 }
1790 else if (Rdata.negate)
1791 {
1792 /* L | !R: treat as !(!L & R) */
1796 Rdata.npos);
1797 data->negate = true;
1798 return TS_YES;
1799 }
1800 else
1801 {
1802 /* straight OR */
1803 return TS_phrase_output(data, &Ldata, &Rdata,
1806 Ldata.npos + Rdata.npos);
1807 }
1808
1809 default:
1810 elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1811 }
1812
1813 /* not reachable, but keep compiler quiet */
1814 return TS_NO;
1815}
1816
1817
1818/*
1819 * Evaluate tsquery boolean expression.
1820 *
1821 * curitem: current tsquery item (initially, the first one)
1822 * arg: opaque value to pass through to callback function
1823 * flags: bitmask of flag bits shown in ts_utils.h
1824 * chkcond: callback function to check whether a primitive value is present
1825 */
1826bool
1827TS_execute(QueryItem *curitem, void *arg, uint32 flags,
1829{
1830 /*
1831 * If we get TS_MAYBE from the recursion, return true. We could only see
1832 * that result if the caller passed TS_EXEC_PHRASE_NO_POS, so there's no
1833 * need to check again.
1834 */
1835 return TS_execute_recurse(curitem, arg, flags, chkcond) != TS_NO;
1836}
1837
1838/*
1839 * Evaluate tsquery boolean expression.
1840 *
1841 * This is the same as TS_execute except that TS_MAYBE is returned as-is.
1842 */
1844TS_execute_ternary(QueryItem *curitem, void *arg, uint32 flags,
1846{
1847 return TS_execute_recurse(curitem, arg, flags, chkcond);
1848}
1849
1850/*
1851 * TS_execute recursion for operators above any phrase operator. Here we do
1852 * not need to worry about lexeme positions. As soon as we hit an OP_PHRASE
1853 * operator, we pass it off to TS_phrase_execute which does worry.
1854 */
1855static TSTernaryValue
1856TS_execute_recurse(QueryItem *curitem, void *arg, uint32 flags,
1858{
1860
1861 /* since this function recurses, it could be driven to stack overflow */
1863
1864 /* ... and let's check for query cancel while we're at it */
1866
1867 if (curitem->type == QI_VAL)
1868 return chkcond(arg, (QueryOperand *) curitem,
1869 NULL /* don't need position info */ );
1870
1871 switch (curitem->qoperator.oper)
1872 {
1873 case OP_NOT:
1874 if (flags & TS_EXEC_SKIP_NOT)
1875 return TS_YES;
1876 switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1877 {
1878 case TS_NO:
1879 return TS_YES;
1880 case TS_YES:
1881 return TS_NO;
1882 case TS_MAYBE:
1883 return TS_MAYBE;
1884 }
1885 break;
1886
1887 case OP_AND:
1888 lmatch = TS_execute_recurse(curitem + curitem->qoperator.left, arg,
1889 flags, chkcond);
1890 if (lmatch == TS_NO)
1891 return TS_NO;
1892 switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1893 {
1894 case TS_NO:
1895 return TS_NO;
1896 case TS_YES:
1897 return lmatch;
1898 case TS_MAYBE:
1899 return TS_MAYBE;
1900 }
1901 break;
1902
1903 case OP_OR:
1904 lmatch = TS_execute_recurse(curitem + curitem->qoperator.left, arg,
1905 flags, chkcond);
1906 if (lmatch == TS_YES)
1907 return TS_YES;
1908 switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1909 {
1910 case TS_NO:
1911 return lmatch;
1912 case TS_YES:
1913 return TS_YES;
1914 case TS_MAYBE:
1915 return TS_MAYBE;
1916 }
1917 break;
1918
1919 case OP_PHRASE:
1920
1921 /*
1922 * If we get a MAYBE result, and the caller doesn't want that,
1923 * convert it to NO. It would be more consistent, perhaps, to
1924 * return the result of TS_phrase_execute() verbatim and then
1925 * convert MAYBE results at the top of the recursion. But
1926 * converting at the topmost phrase operator gives results that
1927 * are bug-compatible with the old implementation, so do it like
1928 * this for now.
1929 */
1930 switch (TS_phrase_execute(curitem, arg, flags, chkcond, NULL))
1931 {
1932 case TS_NO:
1933 return TS_NO;
1934 case TS_YES:
1935 return TS_YES;
1936 case TS_MAYBE:
1937 return (flags & TS_EXEC_PHRASE_NO_POS) ? TS_MAYBE : TS_NO;
1938 }
1939 break;
1940
1941 default:
1942 elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1943 }
1944
1945 /* not reachable, but keep compiler quiet */
1946 return TS_NO;
1947}
1948
1949/*
1950 * Evaluate tsquery and report locations of matching terms.
1951 *
1952 * This is like TS_execute except that it returns match locations not just
1953 * success/failure status. The callback function is required to provide
1954 * position data (we report failure if it doesn't).
1955 *
1956 * On successful match, the result is a List of ExecPhraseData structs, one
1957 * for each AND'ed term or phrase operator in the query. Each struct includes
1958 * a sorted array of lexeme positions matching that term. (Recall that for
1959 * phrase operators, the match includes width+1 lexemes, and the recorded
1960 * position is that of the rightmost lexeme.)
1961 *
1962 * OR subexpressions are handled by union'ing their match locations into a
1963 * single List element, which is valid since any of those locations contains
1964 * a match. However, when some of the OR'ed terms are phrase operators, we
1965 * report the maximum width of any of the OR'ed terms, making such cases
1966 * slightly imprecise in the conservative direction. (For example, if the
1967 * tsquery is "(A <-> B) | C", an occurrence of C in the data would be
1968 * reported as though it includes the lexeme to the left of C.)
1969 *
1970 * Locations of NOT subexpressions are not reported. (Obviously, there can
1971 * be no successful NOT matches at top level, or the match would have failed.
1972 * So this amounts to ignoring NOTs underneath ORs.)
1973 *
1974 * The result is NIL if no match, or if position data was not returned.
1975 *
1976 * Arguments are the same as for TS_execute, although flags is currently
1977 * vestigial since none of the defined bits are sensible here.
1978 */
1979List *
1980TS_execute_locations(QueryItem *curitem, void *arg,
1981 uint32 flags,
1983{
1984 List *result;
1985
1986 /* No flags supported, as yet */
1987 Assert(flags == TS_EXEC_EMPTY);
1989 return result;
1990 return NIL;
1991}
1992
1993/*
1994 * TS_execute_locations recursion for operators above any phrase operator.
1995 * OP_PHRASE subexpressions can be passed off to TS_phrase_execute.
1996 */
1997static bool
2000 List **locations)
2001{
2002 bool lmatch,
2003 rmatch;
2005 *rlocations;
2007
2008 /* since this function recurses, it could be driven to stack overflow */
2010
2011 /* ... and let's check for query cancel while we're at it */
2013
2014 /* Default locations result is empty */
2015 *locations = NIL;
2016
2017 if (curitem->type == QI_VAL)
2018 {
2020 if (chkcond(arg, (QueryOperand *) curitem, data) == TS_YES)
2021 {
2023 return true;
2024 }
2025 pfree(data);
2026 return false;
2027 }
2028
2029 switch (curitem->qoperator.oper)
2030 {
2031 case OP_NOT:
2032 if (!TS_execute_locations_recurse(curitem + 1, arg, chkcond,
2033 &llocations))
2034 return true; /* we don't pass back any locations */
2035 return false;
2036
2037 case OP_AND:
2038 if (!TS_execute_locations_recurse(curitem + curitem->qoperator.left,
2039 arg, chkcond,
2040 &llocations))
2041 return false;
2042 if (!TS_execute_locations_recurse(curitem + 1,
2043 arg, chkcond,
2044 &rlocations))
2045 return false;
2047 return true;
2048
2049 case OP_OR:
2051 arg, chkcond,
2052 &llocations);
2054 arg, chkcond,
2055 &rlocations);
2056 if (lmatch || rmatch)
2057 {
2058 /*
2059 * We generate an AND'able location struct from each
2060 * combination of sub-matches, following the disjunctive law
2061 * (A & B) | (C & D) = (A | C) & (A | D) & (B | C) & (B | D).
2062 *
2063 * However, if either input didn't produce locations (i.e., it
2064 * failed or was a NOT), we must just return the other list.
2065 */
2066 if (llocations == NIL)
2068 else if (rlocations == NIL)
2070 else
2071 {
2072 ListCell *ll;
2073
2074 foreach(ll, llocations)
2075 {
2077 ListCell *lr;
2078
2079 foreach(lr, rlocations)
2080 {
2082
2086 0, 0,
2087 ldata->npos + rdata->npos);
2088 /* Report the larger width, as explained above. */
2089 data->width = Max(ldata->width, rdata->width);
2091 }
2092 }
2093 }
2094
2095 return true;
2096 }
2097 return false;
2098
2099 case OP_PHRASE:
2100 /* We can hand this off to TS_phrase_execute */
2103 data) == TS_YES)
2104 {
2105 if (!data->negate)
2107 return true;
2108 }
2109 pfree(data);
2110 return false;
2111
2112 default:
2113 elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
2114 }
2115
2116 /* not reachable, but keep compiler quiet */
2117 return false;
2118}
2119
2120/*
2121 * Detect whether a tsquery boolean expression requires any positive matches
2122 * to values shown in the tsquery.
2123 *
2124 * This is needed to know whether a GIN index search requires full index scan.
2125 * For example, 'x & !y' requires a match of x, so it's sufficient to scan
2126 * entries for x; but 'x | !y' could match rows containing neither x nor y.
2127 */
2128bool
2130{
2131 /* since this function recurses, it could be driven to stack overflow */
2133
2134 if (curitem->type == QI_VAL)
2135 return true;
2136
2137 switch (curitem->qoperator.oper)
2138 {
2139 case OP_NOT:
2140
2141 /*
2142 * Assume there are no required matches underneath a NOT. For
2143 * some cases with nested NOTs, we could prove there's a required
2144 * match, but it seems unlikely to be worth the trouble.
2145 */
2146 return false;
2147
2148 case OP_PHRASE:
2149
2150 /*
2151 * Treat OP_PHRASE as OP_AND here
2152 */
2153 case OP_AND:
2154 /* If either side requires a match, we're good */
2155 if (tsquery_requires_match(curitem + curitem->qoperator.left))
2156 return true;
2157 else
2158 return tsquery_requires_match(curitem + 1);
2159
2160 case OP_OR:
2161 /* Both sides must require a match */
2162 if (tsquery_requires_match(curitem + curitem->qoperator.left))
2163 return tsquery_requires_match(curitem + 1);
2164 else
2165 return false;
2166
2167 default:
2168 elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
2169 }
2170
2171 /* not reachable, but keep compiler quiet */
2172 return false;
2173}
2174
2175/*
2176 * boolean operations
2177 */
2178Datum
2180{
2182 PG_GETARG_DATUM(1),
2183 PG_GETARG_DATUM(0)));
2184}
2185
2186Datum
2188{
2190 TSQuery query = PG_GETARG_TSQUERY(1);
2191 CHKVAL chkval;
2192 bool result;
2193
2194 /* empty query matches nothing */
2195 if (!query->size)
2196 {
2197 PG_FREE_IF_COPY(val, 0);
2198 PG_FREE_IF_COPY(query, 1);
2199 PG_RETURN_BOOL(false);
2200 }
2201
2202 chkval.arrb = ARRPTR(val);
2203 chkval.arre = chkval.arrb + val->size;
2204 chkval.values = STRPTR(val);
2205 chkval.operand = GETOPERAND(query);
2206 result = TS_execute(GETQUERY(query),
2207 &chkval,
2210
2211 PG_FREE_IF_COPY(val, 0);
2212 PG_FREE_IF_COPY(query, 1);
2214}
2215
2216Datum
2218{
2219 TSVector vector;
2220 TSQuery query;
2221 bool res;
2222
2224 PG_GETARG_DATUM(0)));
2226 PG_GETARG_DATUM(1)));
2227
2229 TSVectorGetDatum(vector),
2230 TSQueryGetDatum(query)));
2231
2232 pfree(vector);
2233 pfree(query);
2234
2235 PG_RETURN_BOOL(res);
2236}
2237
2238Datum
2240{
2241 TSVector vector;
2242 TSQuery query = PG_GETARG_TSQUERY(1);
2243 bool res;
2244
2246 PG_GETARG_DATUM(0)));
2247
2249 TSVectorGetDatum(vector),
2250 TSQueryGetDatum(query)));
2251
2252 pfree(vector);
2253 PG_FREE_IF_COPY(query, 1);
2254
2255 PG_RETURN_BOOL(res);
2256}
2257
2258/*
2259 * ts_stat statistic function support
2260 */
2261
2262
2263/*
2264 * Returns the number of positions in value 'wptr' within tsvector 'txt',
2265 * that have a weight equal to one of the weights in 'weight' bitmask.
2266 */
2267static int
2269{
2270 int len = POSDATALEN(txt, wptr);
2271 int num = 0;
2273
2274 while (len--)
2275 {
2276 if (weight & (1 << WEP_GETWEIGHT(*ptr)))
2277 num++;
2278 ptr++;
2279 }
2280 return num;
2281}
2282
2283#define compareStatWord(a,e,t) \
2284 tsCompareString((a)->lexeme, (a)->lenlexeme, \
2285 STRPTR(t) + (e)->pos, (e)->len, \
2286 false)
2287
2288static void
2290{
2291 WordEntry *we = ARRPTR(txt) + off;
2292 StatEntry *node = stat->root,
2293 *pnode = NULL;
2294 int n,
2295 res = 0;
2296 uint32 depth = 1;
2297
2298 if (stat->weight == 0)
2299 n = (we->haspos) ? POSDATALEN(txt, we) : 1;
2300 else
2301 n = (we->haspos) ? check_weight(txt, we, stat->weight) : 0;
2302
2303 if (n == 0)
2304 return; /* nothing to insert */
2305
2306 while (node)
2307 {
2308 res = compareStatWord(node, we, txt);
2309
2310 if (res == 0)
2311 {
2312 break;
2313 }
2314 else
2315 {
2316 pnode = node;
2317 node = (res < 0) ? node->left : node->right;
2318 }
2319 depth++;
2320 }
2321
2322 if (depth > stat->maxdepth)
2323 stat->maxdepth = depth;
2324
2325 if (node == NULL)
2326 {
2328 node->left = node->right = NULL;
2329 node->ndoc = 1;
2330 node->nentry = n;
2331 node->lenlexeme = we->len;
2332 memcpy(node->lexeme, STRPTR(txt) + we->pos, node->lenlexeme);
2333
2334 if (pnode == NULL)
2335 {
2336 stat->root = node;
2337 }
2338 else
2339 {
2340 if (res < 0)
2341 pnode->left = node;
2342 else
2343 pnode->right = node;
2344 }
2345 }
2346 else
2347 {
2348 node->ndoc++;
2349 node->nentry += n;
2350 }
2351}
2352
2353static void
2355 uint32 low, uint32 high, uint32 offset)
2356{
2357 uint32 pos;
2358 uint32 middle = (low + high) >> 1;
2359
2360 pos = (low + middle) >> 1;
2361 if (low != middle && pos >= offset && pos - offset < txt->size)
2362 insertStatEntry(persistentContext, stat, txt, pos - offset);
2363 pos = (high + middle + 1) >> 1;
2364 if (middle + 1 != high && pos >= offset && pos - offset < txt->size)
2365 insertStatEntry(persistentContext, stat, txt, pos - offset);
2366
2367 if (low != middle)
2369 if (high != middle + 1)
2370 chooseNextStatEntry(persistentContext, stat, txt, middle + 1, high, offset);
2371}
2372
2373/*
2374 * This is written like a custom aggregate function, because the
2375 * original plan was to do just that. Unfortunately, an aggregate function
2376 * can't return a set, so that plan was abandoned. If that limitation is
2377 * lifted in the future, ts_stat could be a real aggregate function so that
2378 * you could use it like this:
2379 *
2380 * SELECT ts_stat(vector_column) FROM vector_table;
2381 *
2382 * where vector_column is a tsvector-type column in vector_table.
2383 */
2384
2385static TSVectorStat *
2387{
2389 uint32 i,
2390 nbit = 0,
2391 offset;
2392
2393 if (stat == NULL)
2394 { /* Init in first */
2396 stat->maxdepth = 1;
2397 }
2398
2399 /* simple check of correctness */
2400 if (txt == NULL || txt->size == 0)
2401 {
2402 if (txt && txt != (TSVector) DatumGetPointer(data))
2403 pfree(txt);
2404 return stat;
2405 }
2406
2407 i = txt->size - 1;
2408 for (; i > 0; i >>= 1)
2409 nbit++;
2410
2411 nbit = 1 << nbit;
2412 offset = (nbit - txt->size) / 2;
2413
2414 insertStatEntry(persistentContext, stat, txt, (nbit >> 1) - offset);
2416
2417 return stat;
2418}
2419
2420static void
2423{
2424 TupleDesc tupdesc;
2425 MemoryContext oldcontext;
2426 StatEntry *node;
2427
2428 funcctx->user_fctx = stat;
2429
2430 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2431
2432 stat->stack = palloc0_array(StatEntry *, stat->maxdepth + 1);
2433 stat->stackpos = 0;
2434
2435 node = stat->root;
2436 /* find leftmost value */
2437 if (node == NULL)
2438 stat->stack[stat->stackpos] = NULL;
2439 else
2440 for (;;)
2441 {
2442 stat->stack[stat->stackpos] = node;
2443 if (node->left)
2444 {
2445 stat->stackpos++;
2446 node = node->left;
2447 }
2448 else
2449 break;
2450 }
2451 Assert(stat->stackpos <= stat->maxdepth);
2452
2453 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
2454 elog(ERROR, "return type must be a row type");
2455 funcctx->tuple_desc = tupdesc;
2456 funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
2457
2458 MemoryContextSwitchTo(oldcontext);
2459}
2460
2461static StatEntry *
2463{
2464 StatEntry *node = stat->stack[stat->stackpos];
2465
2466 if (node == NULL)
2467 return NULL;
2468
2469 if (node->ndoc != 0)
2470 {
2471 /* return entry itself: we already was at left sublink */
2472 return node;
2473 }
2474 else if (node->right && node->right != stat->stack[stat->stackpos + 1])
2475 {
2476 /* go on right sublink */
2477 stat->stackpos++;
2478 node = node->right;
2479
2480 /* find most-left value */
2481 for (;;)
2482 {
2483 stat->stack[stat->stackpos] = node;
2484 if (node->left)
2485 {
2486 stat->stackpos++;
2487 node = node->left;
2488 }
2489 else
2490 break;
2491 }
2492 Assert(stat->stackpos <= stat->maxdepth);
2493 }
2494 else
2495 {
2496 /* we already return all left subtree, itself and right subtree */
2497 if (stat->stackpos == 0)
2498 return NULL;
2499
2500 stat->stackpos--;
2501 return walkStatEntryTree(stat);
2502 }
2503
2504 return node;
2505}
2506
2507static Datum
2509{
2510 TSVectorStat *st;
2511 StatEntry *entry;
2512
2513 st = (TSVectorStat *) funcctx->user_fctx;
2514
2515 entry = walkStatEntryTree(st);
2516
2517 if (entry != NULL)
2518 {
2519 Datum result;
2520 char *values[3];
2521 char ndoc[16];
2522 char nentry[16];
2523 HeapTuple tuple;
2524
2525 values[0] = palloc(entry->lenlexeme + 1);
2526 memcpy(values[0], entry->lexeme, entry->lenlexeme);
2527 (values[0])[entry->lenlexeme] = '\0';
2528 sprintf(ndoc, "%d", entry->ndoc);
2529 values[1] = ndoc;
2530 sprintf(nentry, "%d", entry->nentry);
2531 values[2] = nentry;
2532
2533 tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
2534 result = HeapTupleGetDatum(tuple);
2535
2536 pfree(values[0]);
2537
2538 /* mark entry as already visited */
2539 entry->ndoc = 0;
2540
2541 return result;
2542 }
2543
2544 return (Datum) 0;
2545}
2546
2547static TSVectorStat *
2549{
2550 char *query = text_to_cstring(txt);
2552 bool isnull;
2553 Portal portal;
2555
2556 if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2557 /* internal error */
2558 elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2559
2560 if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2561 /* internal error */
2562 elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2563
2564 SPI_cursor_fetch(portal, true, 100);
2565
2566 if (SPI_tuptable == NULL ||
2567 SPI_tuptable->tupdesc->natts != 1 ||
2569 TSVECTOROID))
2570 ereport(ERROR,
2572 errmsg("ts_stat query must return one tsvector column")));
2573
2575 stat->maxdepth = 1;
2576
2577 if (ws)
2578 {
2579 char *buf;
2580 const char *end;
2581
2582 buf = VARDATA_ANY(ws);
2583 end = buf + VARSIZE_ANY_EXHDR(ws);
2584 while (buf < end)
2585 {
2586 int len = pg_mblen_range(buf, end);
2587
2588 if (len == 1)
2589 {
2590 switch (*buf)
2591 {
2592 case 'A':
2593 case 'a':
2594 stat->weight |= 1 << 3;
2595 break;
2596 case 'B':
2597 case 'b':
2598 stat->weight |= 1 << 2;
2599 break;
2600 case 'C':
2601 case 'c':
2602 stat->weight |= 1 << 1;
2603 break;
2604 case 'D':
2605 case 'd':
2606 stat->weight |= 1;
2607 break;
2608 default:
2609 stat->weight |= 0;
2610 }
2611 }
2612 buf += len;
2613 }
2614 }
2615
2616 while (SPI_processed > 0)
2617 {
2618 uint64 i;
2619
2620 for (i = 0; i < SPI_processed; i++)
2621 {
2623
2624 if (!isnull)
2626 }
2627
2629 SPI_cursor_fetch(portal, true, 100);
2630 }
2631
2633 SPI_cursor_close(portal);
2635 pfree(query);
2636
2637 return stat;
2638}
2639
2640Datum
2642{
2644 Datum result;
2645
2646 if (SRF_IS_FIRSTCALL())
2647 {
2650
2652 SPI_connect();
2653 stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, NULL);
2654 PG_FREE_IF_COPY(txt, 0);
2656 SPI_finish();
2657 }
2658
2660 if ((result = ts_process_call(funcctx)) != (Datum) 0)
2663}
2664
2665Datum
2667{
2669 Datum result;
2670
2671 if (SRF_IS_FIRSTCALL())
2672 {
2676
2678 SPI_connect();
2679 stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, ws);
2680 PG_FREE_IF_COPY(txt, 0);
2681 PG_FREE_IF_COPY(ws, 1);
2683 SPI_finish();
2684 }
2685
2687 if ((result = ts_process_call(funcctx)) != (Datum) 0)
2690}
2691
2692
2693/*
2694 * Triggers for automatic update of a tsvector column from text column(s)
2695 *
2696 * Trigger arguments are either
2697 * name of tsvector col, name of tsconfig to use, name(s) of text col(s)
2698 * name of tsvector col, name of regconfig col, name(s) of text col(s)
2699 * ie, tsconfig can either be specified by name, or indirectly as the
2700 * contents of a regconfig field in the row. If the name is used, it must
2701 * be explicitly schema-qualified.
2702 */
2703Datum
2705{
2706 return tsvector_update_trigger(fcinfo, false);
2707}
2708
2709Datum
2711{
2712 return tsvector_update_trigger(fcinfo, true);
2713}
2714
2715static Datum
2717{
2718 TriggerData *trigdata;
2720 Relation rel;
2723 i;
2724 ParsedText prs;
2725 Datum datum;
2726 bool isnull;
2727 text *txt;
2728 Oid cfgId;
2729 bool update_needed;
2730
2731 /* Check call context */
2732 if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
2733 elog(ERROR, "tsvector_update_trigger: not fired by trigger manager");
2734
2735 trigdata = (TriggerData *) fcinfo->context;
2736 if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
2737 elog(ERROR, "tsvector_update_trigger: must be fired for row");
2738 if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
2739 elog(ERROR, "tsvector_update_trigger: must be fired BEFORE event");
2740
2741 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
2742 {
2743 rettuple = trigdata->tg_trigtuple;
2744 update_needed = true;
2745 }
2746 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
2747 {
2748 rettuple = trigdata->tg_newtuple;
2749 update_needed = false; /* computed below */
2750 }
2751 else
2752 elog(ERROR, "tsvector_update_trigger: must be fired for INSERT or UPDATE");
2753
2754 trigger = trigdata->tg_trigger;
2755 rel = trigdata->tg_relation;
2756
2757 if (trigger->tgnargs < 3)
2758 elog(ERROR, "tsvector_update_trigger: arguments must be tsvector_field, ts_config, text_field1, ...)");
2759
2760 /* Find the target tsvector column */
2761 tsvector_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
2763 ereport(ERROR,
2765 errmsg("tsvector column \"%s\" does not exist",
2766 trigger->tgargs[0])));
2767 /* This will effectively reject system columns, so no separate test: */
2769 TSVECTOROID))
2770 ereport(ERROR,
2772 errmsg("column \"%s\" is not of tsvector type",
2773 trigger->tgargs[0])));
2774
2775 /* Find the configuration to use */
2776 if (config_column)
2777 {
2778 int config_attr_num;
2779
2780 config_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[1]);
2782 ereport(ERROR,
2784 errmsg("configuration column \"%s\" does not exist",
2785 trigger->tgargs[1])));
2787 REGCONFIGOID))
2788 ereport(ERROR,
2790 errmsg("column \"%s\" is not of regconfig type",
2791 trigger->tgargs[1])));
2792
2793 datum = SPI_getbinval(rettuple, rel->rd_att, config_attr_num, &isnull);
2794 if (isnull)
2795 ereport(ERROR,
2797 errmsg("configuration column \"%s\" must not be null",
2798 trigger->tgargs[1])));
2799 cfgId = DatumGetObjectId(datum);
2800 }
2801 else
2802 {
2803 List *names;
2804
2805 names = stringToQualifiedNameList(trigger->tgargs[1], NULL);
2806 /* require a schema so that results are not search path dependent */
2807 if (list_length(names) < 2)
2808 ereport(ERROR,
2810 errmsg("text search configuration name \"%s\" must be schema-qualified",
2811 trigger->tgargs[1])));
2812 cfgId = get_ts_config_oid(names, false);
2813 }
2814
2815 /* initialize parse state */
2816 prs.lenwords = 32;
2817 prs.curwords = 0;
2818 prs.pos = 0;
2820
2821 /* find all words in indexable column(s) */
2822 for (i = 2; i < trigger->tgnargs; i++)
2823 {
2824 int numattr;
2825
2826 numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
2828 ereport(ERROR,
2830 errmsg("column \"%s\" does not exist",
2831 trigger->tgargs[i])));
2833 ereport(ERROR,
2835 errmsg("column \"%s\" is not of a character type",
2836 trigger->tgargs[i])));
2837
2839 update_needed = true;
2840
2841 datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
2842 if (isnull)
2843 continue;
2844
2845 txt = DatumGetTextPP(datum);
2846
2848
2849 if (txt != (text *) DatumGetPointer(datum))
2850 pfree(txt);
2851 }
2852
2853 if (update_needed)
2854 {
2855 /* make tsvector value */
2856 datum = TSVectorGetDatum(make_tsvector(&prs));
2857 isnull = false;
2858
2859 /* and insert it into tuple */
2862 &datum, &isnull);
2863
2864 pfree(DatumGetPointer(datum));
2865 }
2866
2867 return PointerGetDatum(rettuple);
2868}
#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
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 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_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 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 * 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 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
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
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
#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)
static int check_weight(TSVector txt, WordEntry *wptr, int8 weight)
Datum tsvector_to_array(PG_FUNCTION_ARGS)
Datum ts_match_tq(PG_FUNCTION_ARGS)
Datum ts_stat1(PG_FUNCTION_ARGS)
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

◆ compareStatWord

#define compareStatWord (   a,
  e,
 
)
Value:
tsCompareString((a)->lexeme, (a)->lenlexeme, \
STRPTR(t) + (e)->pos, (e)->len, \
false)
e

Definition at line 2284 of file tsvector_op.c.

◆ STATENTRYHDRSZ

#define STATENTRYHDRSZ   (offsetof(StatEntry, lexeme))

Definition at line 56 of file tsvector_op.c.

◆ TSPO_BOTH

#define TSPO_BOTH   0x04 /* emit positions appearing in both L&R */

Definition at line 1439 of file tsvector_op.c.

◆ TSPO_L_ONLY

#define TSPO_L_ONLY   0x01 /* emit positions appearing only in L */

Definition at line 1437 of file tsvector_op.c.

◆ TSPO_R_ONLY

#define TSPO_R_ONLY   0x02 /* emit positions appearing only in R */

Definition at line 1438 of file tsvector_op.c.

◆ TSVECTORCMPFUNC

#define TSVECTORCMPFUNC (   type,
  action,
  ret 
)
Value:
{ \
int res = silly_cmp_tsvector(a, b); \
PG_RETURN_##ret( res action 0 ); \
} \
/* keep compiler quiet - no extra ; */ \
extern int no_such_variable
static int silly_cmp_tsvector(const TSVectorData *a, const TSVectorData *b)
Definition tsvector_op.c:86
const char * type

Definition at line 145 of file tsvector_op.c.

148{ \
151 int res = silly_cmp_tsvector(a, b); \
154 PG_RETURN_##ret( res action 0 ); \
155} \
156/* keep compiler quiet - no extra ; */ \
157extern int no_such_variable

Typedef Documentation

◆ StatEntry

Function Documentation

◆ add_pos()

static int32 add_pos ( TSVector  src,
WordEntry srcptr,
TSVector  dest,
WordEntry destptr,
int32  maxpos 
)
static

Definition at line 359 of file tsvector_op.c.

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}

References _POSVECPTR, fb(), i, LIMITPOS, MAXENTRYPOS, MAXNUMPOS, POSDATALEN, POSDATAPTR, WEP_GETPOS, WEP_GETWEIGHT, WEP_SETPOS, and WEP_SETWEIGHT.

Referenced by tsvector_concat().

◆ array_to_tsvector()

Datum array_to_tsvector ( PG_FUNCTION_ARGS  )

Definition at line 743 of file tsvector_op.c.

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}

References ARRPTR, CALCDATASIZE, compare_text_lexemes(), cur, DatumGetPointer(), deconstruct_array_builtin(), ereport, errcode(), errmsg, ERROR, fb(), i, memcpy(), nitems, palloc0(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_RETURN_POINTER, qsort, qunique(), SET_VARSIZE(), STRPTR, VARDATA(), VARHDRSZ, and VARSIZE().

◆ check_weight()

static int check_weight ( TSVector  txt,
WordEntry wptr,
int8  weight 
)
static

Definition at line 2269 of file tsvector_op.c.

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}

References fb(), len, POSDATALEN, POSDATAPTR, and WEP_GETWEIGHT.

Referenced by insertStatEntry().

◆ checkclass_str()

static TSTernaryValue checkclass_str ( CHKVAL chkval,
WordEntry entry,
QueryOperand val,
ExecPhraseData data 
)
static

Definition at line 1163 of file tsvector_op.c.

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}

References Assert, data, fb(), WordEntry::haspos, WordEntry::len, palloc_array, pfree(), WordEntry::pos, WordEntryPosVector::pos, result, SHORTALIGN, TS_MAYBE, TS_NO, TS_YES, val, WEP_GETPOS, and WEP_GETWEIGHT.

Referenced by checkcondition_str().

◆ checkcondition_str()

static TSTernaryValue checkcondition_str ( void checkval,
QueryOperand val,
ExecPhraseData data 
)
static

Definition at line 1269 of file tsvector_op.c.

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}

References checkclass_str(), compareWordEntryPos(), data, difference(), fb(), memcpy(), palloc_array, pfree(), qsort, qunique(), repalloc_array, TS_MAYBE, TS_NO, TS_YES, tsCompareString(), and val.

Referenced by ts_match_vq().

◆ chooseNextStatEntry()

static void chooseNextStatEntry ( MemoryContext  persistentContext,
TSVectorStat stat,
TSVector  txt,
uint32  low,
uint32  high,
uint32  offset 
)
static

Definition at line 2355 of file tsvector_op.c.

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}

References chooseNextStatEntry(), fb(), and insertStatEntry().

Referenced by chooseNextStatEntry(), and ts_accum().

◆ compare_int()

static int compare_int ( const void va,
const void vb 
)
static

Definition at line 428 of file tsvector_op.c.

429{
430 int a = *((const int *) va);
431 int b = *((const int *) vb);
432
433 return pg_cmp_s32(a, b);
434}

References a, b, fb(), and pg_cmp_s32().

Referenced by tsvector_delete_by_indices().

◆ compare_text_lexemes()

static int compare_text_lexemes ( const void va,
const void vb 
)
static

Definition at line 437 of file tsvector_op.c.

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}

References a, b, DatumGetPointer(), fb(), tsCompareString(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by array_to_tsvector().

◆ insertStatEntry()

static void insertStatEntry ( MemoryContext  persistentContext,
TSVectorStat stat,
TSVector  txt,
uint32  off 
)
static

Definition at line 2290 of file tsvector_op.c.

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}

References ARRPTR, check_weight(), compareStatWord, fb(), StatEntry::left, StatEntry::lenlexeme, StatEntry::lexeme, memcpy(), MemoryContextAlloc(), StatEntry::ndoc, StatEntry::nentry, POSDATALEN, StatEntry::right, STATENTRYHDRSZ, and STRPTR.

Referenced by chooseNextStatEntry(), and ts_accum().

◆ parse_weight()

static int parse_weight ( char  cw)
static

Definition at line 211 of file tsvector_op.c.

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}

References ereport, errcode(), errmsg, ERROR, and fb().

Referenced by tsvector_filter(), tsvector_setweight(), and tsvector_setweight_by_filter().

◆ silly_cmp_tsvector()

static int silly_cmp_tsvector ( const TSVectorData a,
const TSVectorData b 
)
static

Definition at line 86 of file tsvector_op.c.

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}

References a, ARRPTR, b, fb(), i, j, POSDATALEN, POSDATAPTR, STRPTR, tsCompareString(), VARSIZE(), WEP_GETPOS, and WEP_GETWEIGHT.

◆ ts_accum()

static TSVectorStat * ts_accum ( MemoryContext  persistentContext,
TSVectorStat stat,
Datum  data 
)
static

Definition at line 2387 of file tsvector_op.c.

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}

References chooseNextStatEntry(), data, DatumGetPointer(), DatumGetTSVector(), fb(), i, insertStatEntry(), MemoryContextAllocZero(), pfree(), and stat.

Referenced by ts_stat_sql().

◆ TS_execute()

bool TS_execute ( QueryItem curitem,
void arg,
uint32  flags,
TSExecuteCallback  chkcond 
)

Definition at line 1828 of file tsvector_op.c.

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}

References arg, fb(), TS_execute_recurse(), and TS_NO.

Referenced by Cover(), gtsvector_consistent(), hlCover(), and ts_match_vq().

◆ TS_execute_locations()

List * TS_execute_locations ( QueryItem curitem,
void arg,
uint32  flags,
TSExecuteCallback  chkcond 
)

Definition at line 1981 of file tsvector_op.c.

1984{
1985 List *result;
1986
1987 /* No flags supported, as yet */
1988 Assert(flags == TS_EXEC_EMPTY);
1990 return result;
1991 return NIL;
1992}

References arg, Assert, fb(), NIL, result, TS_EXEC_EMPTY, and TS_execute_locations_recurse().

Referenced by prsd_headline().

◆ TS_execute_locations_recurse()

static bool TS_execute_locations_recurse ( QueryItem curitem,
void arg,
TSExecuteCallback  chkcond,
List **  locations 
)
static

Definition at line 1999 of file tsvector_op.c.

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}

References arg, CHECK_FOR_INTERRUPTS, check_stack_depth(), data, elog, ERROR, fb(), lappend(), QueryOperator::left, lfirst, list_concat(), list_make1, Max, NIL, OP_AND, OP_NOT, OP_OR, OP_PHRASE, QueryOperator::oper, palloc0_object, pfree(), QI_VAL, QueryItem::qoperator, TS_EXEC_EMPTY, TS_execute_locations_recurse(), TS_phrase_execute(), TS_phrase_output(), TS_YES, TSPO_BOTH, TSPO_L_ONLY, TSPO_R_ONLY, and QueryItem::type.

Referenced by TS_execute_locations(), and TS_execute_locations_recurse().

◆ TS_execute_recurse()

static TSTernaryValue TS_execute_recurse ( QueryItem curitem,
void arg,
uint32  flags,
TSExecuteCallback  chkcond 
)
static

Definition at line 1857 of file tsvector_op.c.

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}

References arg, CHECK_FOR_INTERRUPTS, check_stack_depth(), elog, ERROR, fb(), QueryOperator::left, OP_AND, OP_NOT, OP_OR, OP_PHRASE, QueryOperator::oper, QI_VAL, QueryItem::qoperator, TS_EXEC_PHRASE_NO_POS, TS_EXEC_SKIP_NOT, TS_execute_recurse(), TS_MAYBE, TS_NO, TS_phrase_execute(), TS_YES, and QueryItem::type.

Referenced by TS_execute(), TS_execute_recurse(), and TS_execute_ternary().

◆ TS_execute_ternary()

TSTernaryValue TS_execute_ternary ( QueryItem curitem,
void arg,
uint32  flags,
TSExecuteCallback  chkcond 
)

Definition at line 1845 of file tsvector_op.c.

1847{
1848 return TS_execute_recurse(curitem, arg, flags, chkcond);
1849}

References arg, fb(), and TS_execute_recurse().

Referenced by gin_tsquery_consistent(), and gin_tsquery_triconsistent().

◆ ts_match_qv()

Datum ts_match_qv ( PG_FUNCTION_ARGS  )

◆ ts_match_tq()

Datum ts_match_tq ( PG_FUNCTION_ARGS  )

◆ ts_match_tt()

◆ ts_match_vq()

Datum ts_match_vq ( PG_FUNCTION_ARGS  )

Definition at line 2188 of file tsvector_op.c.

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}

References ARRPTR, checkcondition_str(), fb(), GETOPERAND, GETQUERY, PG_FREE_IF_COPY, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, PG_RETURN_BOOL, result, TSQueryData::size, STRPTR, TS_EXEC_EMPTY, TS_execute(), and val.

Referenced by ts_match_qv(), ts_match_tq(), and ts_match_tt().

◆ TS_phrase_execute()

static TSTernaryValue TS_phrase_execute ( QueryItem curitem,
void arg,
uint32  flags,
TSExecuteCallback  chkcond,
ExecPhraseData data 
)
static

Definition at line 1583 of file tsvector_op.c.

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}

References arg, Assert, CHECK_FOR_INTERRUPTS, check_stack_depth(), data, QueryOperator::distance, elog, ERROR, fb(), QueryOperator::left, Max, Min, OP_AND, OP_NOT, OP_OR, OP_PHRASE, QueryOperator::oper, QI_VAL, QueryItem::qoperator, TS_EXEC_SKIP_NOT, TS_MAYBE, TS_NO, TS_phrase_execute(), TS_phrase_output(), TS_YES, TSPO_BOTH, TSPO_L_ONLY, TSPO_R_ONLY, and QueryItem::type.

Referenced by TS_execute_locations_recurse(), TS_execute_recurse(), and TS_phrase_execute().

◆ TS_phrase_output()

static TSTernaryValue TS_phrase_output ( ExecPhraseData data,
ExecPhraseData Ldata,
ExecPhraseData Rdata,
int  emit,
int  Loffset,
int  Roffset,
int  max_npos 
)
static

Definition at line 1442 of file tsvector_op.c.

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}

References Assert, data, fb(), palloc(), TS_NO, TS_YES, TSPO_BOTH, TSPO_L_ONLY, TSPO_R_ONLY, and WEP_GETPOS.

Referenced by TS_execute_locations_recurse(), and TS_phrase_execute().

◆ ts_process_call()

static Datum ts_process_call ( FuncCallContext funcctx)
static

Definition at line 2509 of file tsvector_op.c.

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}

References BuildTupleFromCStrings(), fb(), HeapTupleGetDatum(), StatEntry::lenlexeme, StatEntry::lexeme, memcpy(), StatEntry::ndoc, StatEntry::nentry, palloc(), pfree(), result, sprintf, values, and walkStatEntryTree().

Referenced by ts_stat1(), and ts_stat2().

◆ ts_setup_firstcall()

static void ts_setup_firstcall ( FunctionCallInfo  fcinfo,
FuncCallContext funcctx,
TSVectorStat stat 
)
static

Definition at line 2422 of file tsvector_op.c.

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}

References Assert, elog, ERROR, fb(), get_call_result_type(), StatEntry::left, MemoryContextSwitchTo(), palloc0_array, stat, TupleDescGetAttInMetadata(), and TYPEFUNC_COMPOSITE.

Referenced by ts_stat1(), and ts_stat2().

◆ ts_stat1()

◆ ts_stat2()

◆ ts_stat_sql()

static TSVectorStat * ts_stat_sql ( MemoryContext  persistentContext,
text txt,
text ws 
)
static

Definition at line 2549 of file tsvector_op.c.

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}

References buf, data, elog, ereport, errcode(), errmsg, ERROR, fb(), i, IsBinaryCoercible(), len, MemoryContextAllocZero(), TupleDescData::natts, pfree(), pg_mblen_range(), plan, SPI_cursor_close(), SPI_cursor_fetch(), SPI_cursor_open(), SPI_freeplan(), SPI_freetuptable(), SPI_getbinval(), SPI_gettypeid(), SPI_prepare(), SPI_processed, SPI_tuptable, stat, text_to_cstring(), ts_accum(), SPITupleTable::tupdesc, SPITupleTable::vals, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by ts_stat1(), and ts_stat2().

◆ tsCompareString()

int32 tsCompareString ( char a,
int  lena,
char b,
int  lenb,
bool  prefix 
)

Definition at line 1126 of file tsvector_op.c.

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}

References a, b, cmp(), fb(), and Min.

Referenced by checkcondition_str(), compare_text_lexemes(), compareentry(), compareQueryOperand(), compareWORD(), gin_cmp_prefix(), gin_cmp_tslexeme(), hlfinditem(), QTNodeCompare(), silly_cmp_tsvector(), and tsvector_bsearch().

◆ tsquery_requires_match()

bool tsquery_requires_match ( QueryItem curitem)

Definition at line 2130 of file tsvector_op.c.

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}

References check_stack_depth(), elog, ERROR, QueryOperator::left, OP_AND, OP_NOT, OP_OR, OP_PHRASE, QueryOperator::oper, QI_VAL, QueryItem::qoperator, tsquery_requires_match(), and QueryItem::type.

Referenced by gin_extract_tsquery(), and tsquery_requires_match().

◆ tsvector_bsearch()

static int tsvector_bsearch ( const TSVectorData tsv,
char lexeme,
int  lexeme_len 
)
static

Definition at line 395 of file tsvector_op.c.

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}

References ARRPTR, cmp(), fb(), len, StatEntry::lexeme, STRPTR, and tsCompareString().

Referenced by tsvector_delete_arr(), tsvector_delete_str(), and tsvector_setweight_by_filter().

◆ tsvector_concat()

Datum tsvector_concat ( PG_FUNCTION_ARGS  )

Definition at line 899 of file tsvector_op.c.

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}

References _POSVECPTR, add_pos(), ARRPTR, Assert, CALCDATASIZE, cmp(), compareEntry, data, ereport, errcode(), errmsg, ERROR, fb(), WordEntry::haspos, i, j, WordEntry::len, MAXSTRPOS, memcpy(), palloc0(), PG_FREE_IF_COPY, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, WordEntry::pos, POSDATALEN, POSDATAPTR, SET_VARSIZE(), SHORTALIGN, TSVectorData::size, STRPTR, VARSIZE(), and WEP_GETPOS.

◆ tsvector_delete_arr()

Datum tsvector_delete_arr ( PG_FUNCTION_ARGS  )

Definition at line 573 of file tsvector_op.c.

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}

References DatumGetPointer(), deconstruct_array_builtin(), fb(), i, palloc0(), pfree(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, tsvector_bsearch(), tsvector_delete_by_indices(), VARDATA(), VARHDRSZ, and VARSIZE().

◆ tsvector_delete_by_indices()

static TSVector tsvector_delete_by_indices ( TSVector  tsv,
int indices_to_delete,
int  indices_count 
)
static

Definition at line 459 of file tsvector_op.c.

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}

References ARRPTR, Assert, CALCDATASIZE, compare_int(), data, fb(), i, j, len, memcpy(), palloc0(), POSDATALEN, qsort, qunique(), SET_VARSIZE(), SHORTALIGN, STRPTR, and VARSIZE().

Referenced by tsvector_delete_arr(), and tsvector_delete_str().

◆ tsvector_delete_str()

◆ tsvector_filter()

Datum tsvector_filter ( PG_FUNCTION_ARGS  )

Definition at line 815 of file tsvector_op.c.

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}

References _POSVECPTR, ARRPTR, CALCDATASIZE, DatumGetChar(), deconstruct_array_builtin(), ereport, errcode(), errmsg, ERROR, fb(), i, j, len, memcpy(), palloc0(), parse_weight(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, POSDATALEN, SET_VARSIZE(), SHORTALIGN, STRPTR, VARSIZE(), and WEP_GETWEIGHT.

◆ tsvector_length()

Datum tsvector_length ( PG_FUNCTION_ARGS  )

Definition at line 201 of file tsvector_op.c.

202{
204 int32 ret = in->size;
205
206 PG_FREE_IF_COPY(in, 0);
207 PG_RETURN_INT32(ret);
208}
#define PG_RETURN_INT32(x)
Definition fmgr.h:355

References PG_FREE_IF_COPY, PG_GETARG_TSVECTOR, PG_RETURN_INT32, and TSVectorData::size.

◆ tsvector_setweight()

Datum tsvector_setweight ( PG_FUNCTION_ARGS  )

Definition at line 250 of file tsvector_op.c.

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}
#define PG_GETARG_CHAR(n)
Definition fmgr.h:273

References ARRPTR, fb(), i, j, memcpy(), palloc(), parse_weight(), PG_FREE_IF_COPY, PG_GETARG_CHAR, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, POSDATALEN, POSDATAPTR, TSVectorData::size, VARSIZE(), and WEP_SETWEIGHT.

◆ tsvector_setweight_by_filter()

Datum tsvector_setweight_by_filter ( PG_FUNCTION_ARGS  )

Definition at line 289 of file tsvector_op.c.

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}

References ARRPTR, DatumGetPointer(), deconstruct_array_builtin(), fb(), i, j, memcpy(), palloc(), parse_weight(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_CHAR, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, POSDATALEN, POSDATAPTR, tsvector_bsearch(), VARDATA(), VARHDRSZ, VARSIZE(), and WEP_SETWEIGHT.

◆ tsvector_strip()

Datum tsvector_strip ( PG_FUNCTION_ARGS  )

Definition at line 168 of file tsvector_op.c.

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}

References ARRPTR, CALCDATASIZE, cur, fb(), i, len, memcpy(), palloc0(), PG_FREE_IF_COPY, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, SET_VARSIZE(), TSVectorData::size, and STRPTR.

◆ tsvector_to_array()

Datum tsvector_to_array ( PG_FUNCTION_ARGS  )

Definition at line 716 of file tsvector_op.c.

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}

References ARRPTR, construct_array_builtin(), cstring_to_text_with_len(), fb(), i, len, palloc(), pfree(), PG_FREE_IF_COPY, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, PointerGetDatum, and STRPTR.

◆ tsvector_unnest()

Datum tsvector_unnest ( PG_FUNCTION_ARGS  )

Definition at line 627 of file tsvector_op.c.

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}

References _POSVECPTR, ARRPTR, construct_array_builtin(), CreateTemplateTupleDesc(), cstring_to_text_with_len(), data, elog, ERROR, fb(), get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), i, Int16GetDatum(), j, len, MemoryContextSwitchTo(), palloc(), PG_GETARG_TSVECTOR_COPY, PointerGetDatum, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, STRPTR, TupleDescFinalize(), TupleDescInitEntry(), TYPEFUNC_COMPOSITE, values, WEP_GETPOS, and WEP_GETWEIGHT.

◆ tsvector_update_trigger()

static Datum tsvector_update_trigger ( PG_FUNCTION_ARGS  ,
bool  config_column 
)
static

Definition at line 2717 of file tsvector_op.c.

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}

References bms_is_member(), CALLED_AS_TRIGGER, ParsedText::curwords, DatumGetObjectId(), DatumGetPointer(), DatumGetTextPP, elog, ereport, errcode(), errmsg, ERROR, fb(), FirstLowInvalidHeapAttributeNumber, get_ts_config_oid(), heap_modify_tuple_by_cols(), i, IsBinaryCoercible(), ParsedText::lenwords, list_length(), make_tsvector(), numattr, palloc_array, parsetext(), pfree(), PointerGetDatum, ParsedText::pos, RelationData::rd_att, SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), SPI_getbinval(), SPI_gettypeid(), stringToQualifiedNameList(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigger, TriggerData::tg_trigtuple, TriggerData::tg_updatedcols, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, TRIGGER_FIRED_FOR_ROW, TSVectorGetDatum(), VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and ParsedText::words.

Referenced by tsvector_update_trigger_bycolumn(), and tsvector_update_trigger_byid().

◆ tsvector_update_trigger_bycolumn()

Datum tsvector_update_trigger_bycolumn ( PG_FUNCTION_ARGS  )

Definition at line 2711 of file tsvector_op.c.

2712{
2713 return tsvector_update_trigger(fcinfo, true);
2714}

References tsvector_update_trigger().

◆ tsvector_update_trigger_byid()

Datum tsvector_update_trigger_byid ( PG_FUNCTION_ARGS  )

Definition at line 2705 of file tsvector_op.c.

2706{
2707 return tsvector_update_trigger(fcinfo, false);
2708}

References tsvector_update_trigger().

◆ TSVECTORCMPFUNC() [1/7]

TSVECTORCMPFUNC ( cmp  ,
,
INT32   
)

◆ TSVECTORCMPFUNC() [2/7]

TSVECTORCMPFUNC ( eq  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [3/7]

TSVECTORCMPFUNC ( ge  ,
>=  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [4/7]

TSVECTORCMPFUNC ( gt  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [5/7]

TSVECTORCMPFUNC ( le  ,
<=  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [6/7]

TSVECTORCMPFUNC ( lt  )

◆ TSVECTORCMPFUNC() [7/7]

TSVECTORCMPFUNC ( ne  ,
,
BOOL   
)

◆ walkStatEntryTree()

static StatEntry * walkStatEntryTree ( TSVectorStat stat)
static

Definition at line 2463 of file tsvector_op.c.

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}

References Assert, fb(), StatEntry::left, StatEntry::ndoc, StatEntry::right, and walkStatEntryTree().

Referenced by ts_process_call(), and walkStatEntryTree().