LCOV - code coverage report
Current view: top level - src/backend/utils/adt - tsvector_op.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 877 1023 85.7 %
Date: 2019-06-19 16:07:09 Functions: 40 49 81.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13