LCOV - code coverage report
Current view: top level - src/backend/utils/adt - tsvector.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 169 243 69.5 %
Date: 2025-04-24 12:15:10 Functions: 6 8 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * tsvector.c
       4             :  *    I/O functions for tsvector
       5             :  *
       6             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       7             :  *
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/utils/adt/tsvector.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : #include "postgres.h"
      16             : 
      17             : #include "common/int.h"
      18             : #include "libpq/pqformat.h"
      19             : #include "nodes/miscnodes.h"
      20             : #include "tsearch/ts_locale.h"
      21             : #include "tsearch/ts_utils.h"
      22             : #include "utils/fmgrprotos.h"
      23             : #include "utils/memutils.h"
      24             : #include "varatt.h"
      25             : 
      26             : typedef struct
      27             : {
      28             :     WordEntry   entry;          /* must be first, see compareentry */
      29             :     WordEntryPos *pos;
      30             :     int         poslen;         /* number of elements in pos */
      31             : } WordEntryIN;
      32             : 
      33             : 
      34             : /* Compare two WordEntryPos values for qsort */
      35             : int
      36        1106 : compareWordEntryPos(const void *a, const void *b)
      37             : {
      38        1106 :     int         apos = WEP_GETPOS(*(const WordEntryPos *) a);
      39        1106 :     int         bpos = WEP_GETPOS(*(const WordEntryPos *) b);
      40             : 
      41        1106 :     return pg_cmp_s32(apos, bpos);
      42             : }
      43             : 
      44             : /*
      45             :  * Removes duplicate pos entries. If there's two entries with same pos but
      46             :  * different weight, the higher weight is retained, so we can't use
      47             :  * qunique here.
      48             :  *
      49             :  * Returns new length.
      50             :  */
      51             : static int
      52       12336 : uniquePos(WordEntryPos *a, int l)
      53             : {
      54             :     WordEntryPos *ptr,
      55             :                *res;
      56             : 
      57       12336 :     if (l <= 1)
      58       11708 :         return l;
      59             : 
      60         628 :     qsort(a, l, sizeof(WordEntryPos), compareWordEntryPos);
      61             : 
      62         628 :     res = a;
      63         628 :     ptr = a + 1;
      64        1644 :     while (ptr - a < l)
      65             :     {
      66        1016 :         if (WEP_GETPOS(*ptr) != WEP_GETPOS(*res))
      67             :         {
      68         992 :             res++;
      69         992 :             *res = *ptr;
      70         992 :             if (res - a >= MAXNUMPOS - 1 ||
      71         992 :                 WEP_GETPOS(*res) == MAXENTRYPOS - 1)
      72             :                 break;
      73             :         }
      74          24 :         else if (WEP_GETWEIGHT(*ptr) > WEP_GETWEIGHT(*res))
      75           6 :             WEP_SETWEIGHT(*res, WEP_GETWEIGHT(*ptr));
      76        1016 :         ptr++;
      77             :     }
      78             : 
      79         628 :     return res + 1 - a;
      80             : }
      81             : 
      82             : /*
      83             :  * Compare two WordEntry structs for qsort_arg.  This can also be used on
      84             :  * WordEntryIN structs, since those have WordEntry as their first field.
      85             :  */
      86             : static int
      87     1129126 : compareentry(const void *va, const void *vb, void *arg)
      88             : {
      89     1129126 :     const WordEntry *a = (const WordEntry *) va;
      90     1129126 :     const WordEntry *b = (const WordEntry *) vb;
      91     1129126 :     char       *BufferStr = (char *) arg;
      92             : 
      93     2258252 :     return tsCompareString(&BufferStr[a->pos], a->len,
      94     1129126 :                            &BufferStr[b->pos], b->len,
      95             :                            false);
      96             : }
      97             : 
      98             : /*
      99             :  * Sort an array of WordEntryIN, remove duplicates.
     100             :  * *outbuflen receives the amount of space needed for strings and positions.
     101             :  */
     102             : static int
     103        4712 : uniqueentry(WordEntryIN *a, int l, char *buf, int *outbuflen)
     104             : {
     105             :     int         buflen;
     106             :     WordEntryIN *ptr,
     107             :                *res;
     108             : 
     109             :     Assert(l >= 1);
     110             : 
     111        4712 :     if (l > 1)
     112        4610 :         qsort_arg(a, l, sizeof(WordEntryIN), compareentry, buf);
     113             : 
     114        4712 :     buflen = 0;
     115        4712 :     res = a;
     116        4712 :     ptr = a + 1;
     117      238454 :     while (ptr - a < l)
     118             :     {
     119      233742 :         if (!(ptr->entry.len == res->entry.len &&
     120      232666 :               strncmp(&buf[ptr->entry.pos], &buf[res->entry.pos],
     121      232666 :                       res->entry.len) == 0))
     122             :         {
     123             :             /* done accumulating data into *res, count space needed */
     124      228216 :             buflen += res->entry.len;
     125      228216 :             if (res->entry.haspos)
     126             :             {
     127       11682 :                 res->poslen = uniquePos(res->pos, res->poslen);
     128       11682 :                 buflen = SHORTALIGN(buflen);
     129       11682 :                 buflen += res->poslen * sizeof(WordEntryPos) + sizeof(uint16);
     130             :             }
     131      228216 :             res++;
     132      228216 :             if (res != ptr)
     133       88572 :                 memcpy(res, ptr, sizeof(WordEntryIN));
     134             :         }
     135        5526 :         else if (ptr->entry.haspos)
     136             :         {
     137         318 :             if (res->entry.haspos)
     138             :             {
     139             :                 /* append ptr's positions to res's positions */
     140         312 :                 int         newlen = ptr->poslen + res->poslen;
     141             : 
     142         312 :                 res->pos = (WordEntryPos *)
     143         312 :                     repalloc(res->pos, newlen * sizeof(WordEntryPos));
     144         312 :                 memcpy(&res->pos[res->poslen], ptr->pos,
     145         312 :                        ptr->poslen * sizeof(WordEntryPos));
     146         312 :                 res->poslen = newlen;
     147         312 :                 pfree(ptr->pos);
     148             :             }
     149             :             else
     150             :             {
     151             :                 /* just give ptr's positions to pos */
     152           6 :                 res->entry.haspos = 1;
     153           6 :                 res->pos = ptr->pos;
     154           6 :                 res->poslen = ptr->poslen;
     155             :             }
     156             :         }
     157      233742 :         ptr++;
     158             :     }
     159             : 
     160             :     /* count space needed for last item */
     161        4712 :     buflen += res->entry.len;
     162        4712 :     if (res->entry.haspos)
     163             :     {
     164         654 :         res->poslen = uniquePos(res->pos, res->poslen);
     165         654 :         buflen = SHORTALIGN(buflen);
     166         654 :         buflen += res->poslen * sizeof(WordEntryPos) + sizeof(uint16);
     167             :     }
     168             : 
     169        4712 :     *outbuflen = buflen;
     170        4712 :     return res + 1 - a;
     171             : }
     172             : 
     173             : 
     174             : Datum
     175        4796 : tsvectorin(PG_FUNCTION_ARGS)
     176             : {
     177        4796 :     char       *buf = PG_GETARG_CSTRING(0);
     178        4796 :     Node       *escontext = fcinfo->context;
     179             :     TSVectorParseState state;
     180             :     WordEntryIN *arr;
     181             :     int         totallen;
     182             :     int         arrlen;         /* allocated size of arr */
     183             :     WordEntry  *inarr;
     184        4796 :     int         len = 0;
     185             :     TSVector    in;
     186             :     int         i;
     187             :     char       *token;
     188             :     int         toklen;
     189             :     WordEntryPos *pos;
     190             :     int         poslen;
     191             :     char       *strbuf;
     192             :     int         stroff;
     193             : 
     194             :     /*
     195             :      * Tokens are appended to tmpbuf, cur is a pointer to the end of used
     196             :      * space in tmpbuf.
     197             :      */
     198             :     char       *tmpbuf;
     199             :     char       *cur;
     200        4796 :     int         buflen = 256;   /* allocated size of tmpbuf */
     201             : 
     202        4796 :     state = init_tsvector_parser(buf, 0, escontext);
     203             : 
     204        4796 :     arrlen = 64;
     205        4796 :     arr = (WordEntryIN *) palloc(sizeof(WordEntryIN) * arrlen);
     206        4796 :     cur = tmpbuf = (char *) palloc(buflen);
     207             : 
     208      243250 :     while (gettoken_tsvector(state, &token, &toklen, &pos, &poslen, NULL))
     209             :     {
     210      238454 :         if (toklen >= MAXSTRLEN)
     211           0 :             ereturn(escontext, (Datum) 0,
     212             :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     213             :                      errmsg("word is too long (%ld bytes, max %ld bytes)",
     214             :                             (long) toklen,
     215             :                             (long) (MAXSTRLEN - 1))));
     216             : 
     217      238454 :         if (cur - tmpbuf > MAXSTRPOS)
     218           0 :             ereturn(escontext, (Datum) 0,
     219             :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     220             :                      errmsg("string is too long for tsvector (%ld bytes, max %ld bytes)",
     221             :                             (long) (cur - tmpbuf), (long) MAXSTRPOS)));
     222             : 
     223             :         /*
     224             :          * Enlarge buffers if needed
     225             :          */
     226      238454 :         if (len >= arrlen)
     227             :         {
     228        1740 :             arrlen *= 2;
     229             :             arr = (WordEntryIN *)
     230        1740 :                 repalloc(arr, sizeof(WordEntryIN) * arrlen);
     231             :         }
     232      238454 :         while ((cur - tmpbuf) + toklen >= buflen)
     233             :         {
     234           0 :             int         dist = cur - tmpbuf;
     235             : 
     236           0 :             buflen *= 2;
     237           0 :             tmpbuf = (char *) repalloc(tmpbuf, buflen);
     238           0 :             cur = tmpbuf + dist;
     239             :         }
     240      238454 :         arr[len].entry.len = toklen;
     241      238454 :         arr[len].entry.pos = cur - tmpbuf;
     242      238454 :         memcpy(cur, token, toklen);
     243      238454 :         cur += toklen;
     244             : 
     245      238454 :         if (poslen != 0)
     246             :         {
     247       12648 :             arr[len].entry.haspos = 1;
     248       12648 :             arr[len].pos = pos;
     249       12648 :             arr[len].poslen = poslen;
     250             :         }
     251             :         else
     252             :         {
     253      225806 :             arr[len].entry.haspos = 0;
     254      225806 :             arr[len].pos = NULL;
     255      225806 :             arr[len].poslen = 0;
     256             :         }
     257      238454 :         len++;
     258             :     }
     259             : 
     260        4790 :     close_tsvector_parser(state);
     261             : 
     262             :     /* Did gettoken_tsvector fail? */
     263        4790 :     if (SOFT_ERROR_OCCURRED(escontext))
     264          12 :         PG_RETURN_NULL();
     265             : 
     266        4778 :     if (len > 0)
     267        4712 :         len = uniqueentry(arr, len, tmpbuf, &buflen);
     268             :     else
     269          66 :         buflen = 0;
     270             : 
     271        4778 :     if (buflen > MAXSTRPOS)
     272           0 :         ereturn(escontext, (Datum) 0,
     273             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     274             :                  errmsg("string is too long for tsvector (%d bytes, max %d bytes)", buflen, MAXSTRPOS)));
     275             : 
     276        4778 :     totallen = CALCDATASIZE(len, buflen);
     277        4778 :     in = (TSVector) palloc0(totallen);
     278        4778 :     SET_VARSIZE(in, totallen);
     279        4778 :     in->size = len;
     280        4778 :     inarr = ARRPTR(in);
     281        4778 :     strbuf = STRPTR(in);
     282        4778 :     stroff = 0;
     283      237706 :     for (i = 0; i < len; i++)
     284             :     {
     285      232928 :         memcpy(strbuf + stroff, &tmpbuf[arr[i].entry.pos], arr[i].entry.len);
     286      232928 :         arr[i].entry.pos = stroff;
     287      232928 :         stroff += arr[i].entry.len;
     288      232928 :         if (arr[i].entry.haspos)
     289             :         {
     290             :             /* This should be unreachable because of MAXNUMPOS restrictions */
     291       12336 :             if (arr[i].poslen > 0xFFFF)
     292           0 :                 elog(ERROR, "positions array too long");
     293             : 
     294             :             /* Copy number of positions */
     295       12336 :             stroff = SHORTALIGN(stroff);
     296       12336 :             *(uint16 *) (strbuf + stroff) = (uint16) arr[i].poslen;
     297       12336 :             stroff += sizeof(uint16);
     298             : 
     299             :             /* Copy positions */
     300       12336 :             memcpy(strbuf + stroff, arr[i].pos, arr[i].poslen * sizeof(WordEntryPos));
     301       12336 :             stroff += arr[i].poslen * sizeof(WordEntryPos);
     302             : 
     303       12336 :             pfree(arr[i].pos);
     304             :         }
     305      232928 :         inarr[i] = arr[i].entry;
     306             :     }
     307             : 
     308             :     Assert((strbuf + stroff - (char *) in) == totallen);
     309             : 
     310        4778 :     PG_RETURN_TSVECTOR(in);
     311             : }
     312             : 
     313             : Datum
     314        7828 : tsvectorout(PG_FUNCTION_ARGS)
     315             : {
     316        7828 :     TSVector    out = PG_GETARG_TSVECTOR(0);
     317             :     char       *outbuf;
     318             :     int32       i,
     319        7828 :                 lenbuf = 0,
     320             :                 pp;
     321        7828 :     WordEntry  *ptr = ARRPTR(out);
     322             :     char       *curbegin,
     323             :                *curin,
     324             :                *curout;
     325             : 
     326        7828 :     lenbuf = out->size * 2 /* '' */ + out->size - 1 /* space */ + 2 /* \0 */ ;
     327      413828 :     for (i = 0; i < out->size; i++)
     328             :     {
     329      406000 :         lenbuf += ptr[i].len * 2 * pg_database_encoding_max_length() /* for escape */ ;
     330      406000 :         if (ptr[i].haspos)
     331       21308 :             lenbuf += 1 /* : */ + 7 /* int2 + , + weight */ * POSDATALEN(out, &(ptr[i]));
     332             :     }
     333             : 
     334        7828 :     curout = outbuf = (char *) palloc(lenbuf);
     335      413828 :     for (i = 0; i < out->size; i++)
     336             :     {
     337      406000 :         curbegin = curin = STRPTR(out) + ptr->pos;
     338      406000 :         if (i != 0)
     339      398412 :             *curout++ = ' ';
     340      406000 :         *curout++ = '\'';
     341     1224844 :         while (curin - curbegin < ptr->len)
     342             :         {
     343      818844 :             int         len = pg_mblen(curin);
     344             : 
     345      818844 :             if (t_iseq(curin, '\''))
     346          26 :                 *curout++ = '\'';
     347      818818 :             else if (t_iseq(curin, '\\'))
     348          90 :                 *curout++ = '\\';
     349             : 
     350     1637688 :             while (len--)
     351      818844 :                 *curout++ = *curin++;
     352             :         }
     353             : 
     354      406000 :         *curout++ = '\'';
     355      406000 :         if ((pp = POSDATALEN(out, ptr)) != 0)
     356             :         {
     357             :             WordEntryPos *wptr;
     358             : 
     359       21308 :             *curout++ = ':';
     360       21308 :             wptr = POSDATAPTR(out, ptr);
     361       43842 :             while (pp)
     362             :             {
     363       22534 :                 curout += sprintf(curout, "%d", WEP_GETPOS(*wptr));
     364       22534 :                 switch (WEP_GETWEIGHT(*wptr))
     365             :                 {
     366         122 :                     case 3:
     367         122 :                         *curout++ = 'A';
     368         122 :                         break;
     369          74 :                     case 2:
     370          74 :                         *curout++ = 'B';
     371          74 :                         break;
     372         236 :                     case 1:
     373         236 :                         *curout++ = 'C';
     374         236 :                         break;
     375       22102 :                     case 0:
     376             :                     default:
     377       22102 :                         break;
     378             :                 }
     379             : 
     380       22534 :                 if (pp > 1)
     381        1226 :                     *curout++ = ',';
     382       22534 :                 pp--;
     383       22534 :                 wptr++;
     384             :             }
     385             :         }
     386      406000 :         ptr++;
     387             :     }
     388             : 
     389        7828 :     *curout = '\0';
     390        7828 :     PG_FREE_IF_COPY(out, 0);
     391        7828 :     PG_RETURN_CSTRING(outbuf);
     392             : }
     393             : 
     394             : /*
     395             :  * Binary Input / Output functions. The binary format is as follows:
     396             :  *
     397             :  * uint32   number of lexemes
     398             :  *
     399             :  * for each lexeme:
     400             :  *      lexeme text in client encoding, null-terminated
     401             :  *      uint16  number of positions
     402             :  *      for each position:
     403             :  *          uint16 WordEntryPos
     404             :  */
     405             : 
     406             : Datum
     407           0 : tsvectorsend(PG_FUNCTION_ARGS)
     408             : {
     409           0 :     TSVector    vec = PG_GETARG_TSVECTOR(0);
     410             :     StringInfoData buf;
     411             :     int         i,
     412             :                 j;
     413           0 :     WordEntry  *weptr = ARRPTR(vec);
     414             : 
     415           0 :     pq_begintypsend(&buf);
     416             : 
     417           0 :     pq_sendint32(&buf, vec->size);
     418           0 :     for (i = 0; i < vec->size; i++)
     419             :     {
     420             :         uint16      npos;
     421             : 
     422             :         /*
     423             :          * the strings in the TSVector array are not null-terminated, so we
     424             :          * have to send the null-terminator separately
     425             :          */
     426           0 :         pq_sendtext(&buf, STRPTR(vec) + weptr->pos, weptr->len);
     427           0 :         pq_sendbyte(&buf, '\0');
     428             : 
     429           0 :         npos = POSDATALEN(vec, weptr);
     430           0 :         pq_sendint16(&buf, npos);
     431             : 
     432           0 :         if (npos > 0)
     433             :         {
     434           0 :             WordEntryPos *wepptr = POSDATAPTR(vec, weptr);
     435             : 
     436           0 :             for (j = 0; j < npos; j++)
     437           0 :                 pq_sendint16(&buf, wepptr[j]);
     438             :         }
     439           0 :         weptr++;
     440             :     }
     441             : 
     442           0 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
     443             : }
     444             : 
     445             : Datum
     446           0 : tsvectorrecv(PG_FUNCTION_ARGS)
     447             : {
     448           0 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
     449             :     TSVector    vec;
     450             :     int         i;
     451             :     int32       nentries;
     452             :     int         datalen;        /* number of bytes used in the variable size
     453             :                                  * area after fixed size TSVector header and
     454             :                                  * WordEntries */
     455             :     Size        hdrlen;
     456             :     Size        len;            /* allocated size of vec */
     457           0 :     bool        needSort = false;
     458             : 
     459           0 :     nentries = pq_getmsgint(buf, sizeof(int32));
     460           0 :     if (nentries < 0 || nentries > (MaxAllocSize / sizeof(WordEntry)))
     461           0 :         elog(ERROR, "invalid size of tsvector");
     462             : 
     463           0 :     hdrlen = DATAHDRSIZE + sizeof(WordEntry) * nentries;
     464             : 
     465           0 :     len = hdrlen * 2;           /* times two to make room for lexemes */
     466           0 :     vec = (TSVector) palloc0(len);
     467           0 :     vec->size = nentries;
     468             : 
     469           0 :     datalen = 0;
     470           0 :     for (i = 0; i < nentries; i++)
     471             :     {
     472             :         const char *lexeme;
     473             :         uint16      npos;
     474             :         size_t      lex_len;
     475             : 
     476           0 :         lexeme = pq_getmsgstring(buf);
     477           0 :         npos = (uint16) pq_getmsgint(buf, sizeof(uint16));
     478             : 
     479             :         /* sanity checks */
     480             : 
     481           0 :         lex_len = strlen(lexeme);
     482           0 :         if (lex_len > MAXSTRLEN)
     483           0 :             elog(ERROR, "invalid tsvector: lexeme too long");
     484             : 
     485           0 :         if (datalen > MAXSTRPOS)
     486           0 :             elog(ERROR, "invalid tsvector: maximum total lexeme length exceeded");
     487             : 
     488           0 :         if (npos > MAXNUMPOS)
     489           0 :             elog(ERROR, "unexpected number of tsvector positions");
     490             : 
     491             :         /*
     492             :          * Looks valid. Fill the WordEntry struct, and copy lexeme.
     493             :          *
     494             :          * But make sure the buffer is large enough first.
     495             :          */
     496           0 :         while (hdrlen + SHORTALIGN(datalen + lex_len) +
     497           0 :                sizeof(uint16) + npos * sizeof(WordEntryPos) >= len)
     498             :         {
     499           0 :             len *= 2;
     500           0 :             vec = (TSVector) repalloc(vec, len);
     501             :         }
     502             : 
     503           0 :         vec->entries[i].haspos = (npos > 0) ? 1 : 0;
     504           0 :         vec->entries[i].len = lex_len;
     505           0 :         vec->entries[i].pos = datalen;
     506             : 
     507           0 :         memcpy(STRPTR(vec) + datalen, lexeme, lex_len);
     508             : 
     509           0 :         datalen += lex_len;
     510             : 
     511           0 :         if (i > 0 && compareentry(&vec->entries[i],
     512           0 :                                   &vec->entries[i - 1],
     513           0 :                                   STRPTR(vec)) <= 0)
     514           0 :             needSort = true;
     515             : 
     516             :         /* Receive positions */
     517           0 :         if (npos > 0)
     518             :         {
     519             :             uint16      j;
     520             :             WordEntryPos *wepptr;
     521             : 
     522             :             /*
     523             :              * Pad to 2-byte alignment if necessary. Though we used palloc0
     524             :              * for the initial allocation, subsequent repalloc'd memory areas
     525             :              * are not initialized to zero.
     526             :              */
     527           0 :             if (datalen != SHORTALIGN(datalen))
     528             :             {
     529           0 :                 *(STRPTR(vec) + datalen) = '\0';
     530           0 :                 datalen = SHORTALIGN(datalen);
     531             :             }
     532             : 
     533           0 :             memcpy(STRPTR(vec) + datalen, &npos, sizeof(uint16));
     534             : 
     535           0 :             wepptr = POSDATAPTR(vec, &vec->entries[i]);
     536           0 :             for (j = 0; j < npos; j++)
     537             :             {
     538           0 :                 wepptr[j] = (WordEntryPos) pq_getmsgint(buf, sizeof(WordEntryPos));
     539           0 :                 if (j > 0 && WEP_GETPOS(wepptr[j]) <= WEP_GETPOS(wepptr[j - 1]))
     540           0 :                     elog(ERROR, "position information is misordered");
     541             :             }
     542             : 
     543           0 :             datalen += sizeof(uint16) + npos * sizeof(WordEntryPos);
     544             :         }
     545             :     }
     546             : 
     547           0 :     SET_VARSIZE(vec, hdrlen + datalen);
     548             : 
     549           0 :     if (needSort)
     550           0 :         qsort_arg(ARRPTR(vec), vec->size, sizeof(WordEntry),
     551           0 :                   compareentry, STRPTR(vec));
     552             : 
     553           0 :     PG_RETURN_TSVECTOR(vec);
     554             : }

Generated by: LCOV version 1.14