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

Go to the source code of this file.

Data Structures

struct  CHKVAL
 
struct  StatEntry
 
struct  TSVectorStat
 

Macros

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

Typedefs

typedef struct StatEntry StatEntry
 

Functions

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

Macro Definition Documentation

◆ compareEntry

#define compareEntry (   pa,
  a,
  pb,
  b 
)
Value:
tsCompareString((pa) + (a)->pos, (a)->len, \
(pb) + (b)->pos, (b)->len, \
false)
int b
Definition: isn.c:70
int a
Definition: isn.c:69
const void size_t len
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1148

Definition at line 350 of file tsvector_op.c.

◆ compareStatWord

#define compareStatWord (   a,
  e,
 
)
Value:
tsCompareString((a)->lexeme, (a)->lenlexeme, \
STRPTR(t) + (e)->pos, (e)->len, \
false)
#define STRPTR(x)
Definition: hstore.h:76
e
Definition: preproc-init.c:82

Definition at line 2135 of file tsvector_op.c.

◆ STATENTRYHDRSZ

#define STATENTRYHDRSZ   (offsetof(StatEntry, lexeme))

Definition at line 56 of file tsvector_op.c.

◆ TSPO_BOTH

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

Definition at line 1461 of file tsvector_op.c.

◆ TSPO_L_ONLY

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

Definition at line 1459 of file tsvector_op.c.

◆ TSPO_R_ONLY

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

Definition at line 1460 of file tsvector_op.c.

◆ TSVECTORCMPFUNC

#define TSVECTORCMPFUNC (   type,
  action,
  ret 
)
Value:
Datum \
tsvector_##type(PG_FUNCTION_ARGS) \
{ \
TSVector a = PG_GETARG_TSVECTOR(0); \
TSVector b = PG_GETARG_TSVECTOR(1); \
PG_FREE_IF_COPY(a,0); \
PG_FREE_IF_COPY(b,1); \
PG_RETURN_##ret( res action 0 ); \
} \
/* keep compiler quiet - no extra ; */ \
extern int no_such_variable
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:135
static int silly_cmp_tsvector(const TSVector a, const TSVector b)
Definition: tsvector_op.c:82

Definition at line 141 of file tsvector_op.c.

Typedef Documentation

◆ StatEntry

typedef struct StatEntry StatEntry

Function Documentation

◆ add_pos()

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

Definition at line 360 of file tsvector_op.c.

363 {
364  uint16 *clen = &_POSVECPTR(dest, destptr)->npos;
365  int i;
366  uint16 slen = POSDATALEN(src, srcptr),
367  startlen;
368  WordEntryPos *spos = POSDATAPTR(src, srcptr),
369  *dpos = POSDATAPTR(dest, destptr);
370 
371  if (!destptr->haspos)
372  *clen = 0;
373 
374  startlen = *clen;
375  for (i = 0;
376  i < slen && *clen < MAXNUMPOS &&
377  (*clen == 0 || WEP_GETPOS(dpos[*clen - 1]) != MAXENTRYPOS - 1);
378  i++)
379  {
380  WEP_SETWEIGHT(dpos[*clen], WEP_GETWEIGHT(spos[i]));
381  WEP_SETPOS(dpos[*clen], LIMITPOS(WEP_GETPOS(spos[i]) + maxpos));
382  (*clen)++;
383  }
384 
385  if (*clen != startlen)
386  destptr->haspos = 1;
387  return *clen - startlen;
388 }
unsigned short uint16
Definition: c.h:441
int i
Definition: isn.c:73
uint32 haspos
Definition: ts_type.h:44
#define WEP_GETPOS(x)
Definition: ts_type.h:80
#define _POSVECPTR(x, e)
Definition: ts_type.h:109
#define MAXENTRYPOS
Definition: ts_type.h:85
#define WEP_SETPOS(x, v)
Definition: ts_type.h:83
#define POSDATALEN(x, e)
Definition: ts_type.h:110
uint16 WordEntryPos
Definition: ts_type.h:63
#define MAXNUMPOS
Definition: ts_type.h:86
#define WEP_SETWEIGHT(x, v)
Definition: ts_type.h:82
#define LIMITPOS(x)
Definition: ts_type.h:87
#define POSDATAPTR(x, e)
Definition: ts_type.h:111
#define WEP_GETWEIGHT(x)
Definition: ts_type.h:79

References _POSVECPTR, generate_unaccent_rules::dest, WordEntry::haspos, i, LIMITPOS, MAXENTRYPOS, MAXNUMPOS, POSDATALEN, POSDATAPTR, WEP_GETPOS, WEP_GETWEIGHT, WEP_SETPOS, and WEP_SETWEIGHT.

Referenced by tsvector_concat().

◆ array_to_tsvector()

Datum array_to_tsvector ( PG_FUNCTION_ARGS  )

Definition at line 743 of file tsvector_op.c.

744 {
746  TSVector tsout;
747  Datum *dlexemes;
748  WordEntry *arrout;
749  bool *nulls;
750  int nitems,
751  i,
752  tslen,
753  datalen = 0;
754  char *cur;
755 
756  deconstruct_array_builtin(v, TEXTOID, &dlexemes, &nulls, &nitems);
757 
758  /*
759  * Reject nulls and zero length strings (maybe we should just ignore them,
760  * instead?)
761  */
762  for (i = 0; i < nitems; i++)
763  {
764  if (nulls[i])
765  ereport(ERROR,
766  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
767  errmsg("lexeme array may not contain nulls")));
768 
769  if (VARSIZE(dlexemes[i]) - VARHDRSZ == 0)
770  ereport(ERROR,
771  (errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
772  errmsg("lexeme array may not contain empty strings")));
773  }
774 
775  /* Sort and de-dup, because this is required for a valid tsvector. */
776  if (nitems > 1)
777  {
778  qsort(dlexemes, nitems, sizeof(Datum), compare_text_lexemes);
779  nitems = qunique(dlexemes, nitems, sizeof(Datum),
781  }
782 
783  /* Calculate space needed for surviving lexemes. */
784  for (i = 0; i < nitems; i++)
785  datalen += VARSIZE(dlexemes[i]) - VARHDRSZ;
786  tslen = CALCDATASIZE(nitems, datalen);
787 
788  /* Allocate and fill tsvector. */
789  tsout = (TSVector) palloc0(tslen);
790  SET_VARSIZE(tsout, tslen);
791  tsout->size = nitems;
792 
793  arrout = ARRPTR(tsout);
794  cur = STRPTR(tsout);
795  for (i = 0; i < nitems; i++)
796  {
797  char *lex = VARDATA(dlexemes[i]);
798  int lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
799 
800  memcpy(cur, lex, lex_len);
801  arrout[i].haspos = 0;
802  arrout[i].len = lex_len;
803  arrout[i].pos = cur - STRPTR(tsout);
804  cur += lex_len;
805  }
806 
807  PG_FREE_IF_COPY(v, 0);
808  PG_RETURN_POINTER(tsout);
809 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3642
#define VARHDRSZ
Definition: c.h:628
#define ARRPTR(x)
Definition: cube.c:25
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
void * palloc0(Size size)
Definition: mcxt.c:1230
#define qsort(a, b, c, d)
Definition: port.h:445
uintptr_t Datum
Definition: postgres.h:412
#define VARDATA(PTR)
Definition: postgres.h:316
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:343
#define VARSIZE(PTR)
Definition: postgres.h:317
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
int32 size
Definition: ts_type.h:93
uint32 pos
Definition: ts_type.h:46
uint32 len
Definition: ts_type.h:45
TSVectorData * TSVector
Definition: ts_type.h:98
static int compare_text_lexemes(const void *va, const void *vb)
Definition: tsvector_op.c:440

References ARRPTR, CALCDATASIZE, compare_text_lexemes(), cur, deconstruct_array_builtin(), ereport, errcode(), errmsg(), ERROR, WordEntry::haspos, i, WordEntry::len, palloc0(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_RETURN_POINTER, WordEntry::pos, qsort, qunique(), SET_VARSIZE, TSVectorData::size, STRPTR, VARDATA, VARHDRSZ, and VARSIZE.

◆ check_weight()

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

Definition at line 2120 of file tsvector_op.c.

2121 {
2122  int len = POSDATALEN(txt, wptr);
2123  int num = 0;
2124  WordEntryPos *ptr = POSDATAPTR(txt, wptr);
2125 
2126  while (len--)
2127  {
2128  if (weight & (1 << WEP_GETWEIGHT(*ptr)))
2129  num++;
2130  ptr++;
2131  }
2132  return num;
2133 }

References len, POSDATALEN, POSDATAPTR, and WEP_GETWEIGHT.

Referenced by insertStatEntry().

◆ checkclass_str()

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

Definition at line 1185 of file tsvector_op.c.

1187 {
1188  TSTernaryValue result = TS_NO;
1189 
1190  Assert(data == NULL || data->npos == 0);
1191 
1192  if (entry->haspos)
1193  {
1194  WordEntryPosVector *posvec;
1195 
1196  /*
1197  * We can't use the _POSVECPTR macro here because the pointer to the
1198  * tsvector's lexeme storage is already contained in chkval->values.
1199  */
1200  posvec = (WordEntryPosVector *)
1201  (chkval->values + SHORTALIGN(entry->pos + entry->len));
1202 
1203  if (val->weight && data)
1204  {
1205  WordEntryPos *posvec_iter = posvec->pos;
1206  WordEntryPos *dptr;
1207 
1208  /*
1209  * Filter position information by weights
1210  */
1211  dptr = data->pos = palloc(sizeof(WordEntryPos) * posvec->npos);
1212  data->allocated = true;
1213 
1214  /* Is there a position with a matching weight? */
1215  while (posvec_iter < posvec->pos + posvec->npos)
1216  {
1217  /* If true, append this position to the data->pos */
1218  if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1219  {
1220  *dptr = WEP_GETPOS(*posvec_iter);
1221  dptr++;
1222  }
1223 
1224  posvec_iter++;
1225  }
1226 
1227  data->npos = dptr - data->pos;
1228 
1229  if (data->npos > 0)
1230  result = TS_YES;
1231  else
1232  {
1233  pfree(data->pos);
1234  data->pos = NULL;
1235  data->allocated = false;
1236  }
1237  }
1238  else if (val->weight)
1239  {
1240  WordEntryPos *posvec_iter = posvec->pos;
1241 
1242  /* Is there a position with a matching weight? */
1243  while (posvec_iter < posvec->pos + posvec->npos)
1244  {
1245  if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1246  {
1247  result = TS_YES;
1248  break; /* no need to go further */
1249  }
1250 
1251  posvec_iter++;
1252  }
1253  }
1254  else if (data)
1255  {
1256  data->npos = posvec->npos;
1257  data->pos = posvec->pos;
1258  data->allocated = false;
1259  result = TS_YES;
1260  }
1261  else
1262  {
1263  /* simplest case: no weight check, positions not needed */
1264  result = TS_YES;
1265  }
1266  }
1267  else
1268  {
1269  /*
1270  * Position info is lacking, so if the caller requires it, we can only
1271  * say that maybe there is a match.
1272  *
1273  * Notice, however, that we *don't* check val->weight here.
1274  * Historically, stripped tsvectors are considered to match queries
1275  * whether or not the query has a weight restriction; that's a little
1276  * dubious but we'll preserve the behavior.
1277  */
1278  if (data)
1279  result = TS_MAYBE;
1280  else
1281  result = TS_YES;
1282  }
1283 
1284  return result;
1285 }
#define SHORTALIGN(LEN)
Definition: c.h:743
long val
Definition: informix.c:664
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1306
void * palloc(Size size)
Definition: mcxt.c:1199
const void * data
char * values
Definition: tsvector_op.c:40
WordEntryPos pos[FLEXIBLE_ARRAY_MEMBER]
Definition: ts_type.h:68
TSTernaryValue
Definition: ts_utils.h:129
@ TS_MAYBE
Definition: ts_utils.h:132
@ TS_NO
Definition: ts_utils.h:130
@ TS_YES
Definition: ts_utils.h:131

References Assert(), data, WordEntry::haspos, WordEntry::len, WordEntryPosVector::npos, palloc(), pfree(), WordEntry::pos, WordEntryPosVector::pos, SHORTALIGN, TS_MAYBE, TS_NO, TS_YES, val, CHKVAL::values, WEP_GETPOS, and WEP_GETWEIGHT.

Referenced by checkcondition_str().

◆ checkcondition_str()

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

Definition at line 1291 of file tsvector_op.c.

1292 {
1293  CHKVAL *chkval = (CHKVAL *) checkval;
1294  WordEntry *StopLow = chkval->arrb;
1295  WordEntry *StopHigh = chkval->arre;
1296  WordEntry *StopMiddle = StopHigh;
1298 
1299  /* Loop invariant: StopLow <= val < StopHigh */
1300  while (StopLow < StopHigh)
1301  {
1302  int difference;
1303 
1304  StopMiddle = StopLow + (StopHigh - StopLow) / 2;
1305  difference = tsCompareString(chkval->operand + val->distance,
1306  val->length,
1307  chkval->values + StopMiddle->pos,
1308  StopMiddle->len,
1309  false);
1310 
1311  if (difference == 0)
1312  {
1313  /* Check weight info & fill 'data' with positions */
1314  res = checkclass_str(chkval, StopMiddle, val, data);
1315  break;
1316  }
1317  else if (difference > 0)
1318  StopLow = StopMiddle + 1;
1319  else
1320  StopHigh = StopMiddle;
1321  }
1322 
1323  /*
1324  * If it's a prefix search, we should also consider lexemes that the
1325  * search term is a prefix of (which will necessarily immediately follow
1326  * the place we found in the above loop). But we can skip them if there
1327  * was a definite match on the exact term AND the caller doesn't need
1328  * position info.
1329  */
1330  if (val->prefix && (res != TS_YES || data))
1331  {
1332  WordEntryPos *allpos = NULL;
1333  int npos = 0,
1334  totalpos = 0;
1335 
1336  /* adjust start position for corner case */
1337  if (StopLow >= StopHigh)
1338  StopMiddle = StopHigh;
1339 
1340  /* we don't try to re-use any data from the initial match */
1341  if (data)
1342  {
1343  if (data->allocated)
1344  pfree(data->pos);
1345  data->pos = NULL;
1346  data->allocated = false;
1347  data->npos = 0;
1348  }
1349  res = TS_NO;
1350 
1351  while ((res != TS_YES || data) &&
1352  StopMiddle < chkval->arre &&
1353  tsCompareString(chkval->operand + val->distance,
1354  val->length,
1355  chkval->values + StopMiddle->pos,
1356  StopMiddle->len,
1357  true) == 0)
1358  {
1359  TSTernaryValue subres;
1360 
1361  subres = checkclass_str(chkval, StopMiddle, val, data);
1362 
1363  if (subres != TS_NO)
1364  {
1365  if (data)
1366  {
1367  /*
1368  * We need to join position information
1369  */
1370  if (subres == TS_MAYBE)
1371  {
1372  /*
1373  * No position info for this match, so we must report
1374  * MAYBE overall.
1375  */
1376  res = TS_MAYBE;
1377  /* forget any previous positions */
1378  npos = 0;
1379  /* don't leak storage */
1380  if (allpos)
1381  pfree(allpos);
1382  break;
1383  }
1384 
1385  while (npos + data->npos > totalpos)
1386  {
1387  if (totalpos == 0)
1388  {
1389  totalpos = 256;
1390  allpos = palloc(sizeof(WordEntryPos) * totalpos);
1391  }
1392  else
1393  {
1394  totalpos *= 2;
1395  allpos = repalloc(allpos, sizeof(WordEntryPos) * totalpos);
1396  }
1397  }
1398 
1399  memcpy(allpos + npos, data->pos, sizeof(WordEntryPos) * data->npos);
1400  npos += data->npos;
1401 
1402  /* don't leak storage from individual matches */
1403  if (data->allocated)
1404  pfree(data->pos);
1405  data->pos = NULL;
1406  data->allocated = false;
1407  /* it's important to reset data->npos before next loop */
1408  data->npos = 0;
1409  }
1410  else
1411  {
1412  /* Don't need positions, just handle YES/MAYBE */
1413  if (subres == TS_YES || res == TS_NO)
1414  res = subres;
1415  }
1416  }
1417 
1418  StopMiddle++;
1419  }
1420 
1421  if (data && npos > 0)
1422  {
1423  /* Sort and make unique array of found positions */
1424  data->pos = allpos;
1425  qsort(data->pos, npos, sizeof(WordEntryPos), compareWordEntryPos);
1426  data->npos = qunique(data->pos, npos, sizeof(WordEntryPos),
1428  data->allocated = true;
1429  res = TS_YES;
1430  }
1431  }
1432 
1433  return res;
1434 }
Datum difference(PG_FUNCTION_ARGS)
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1321
int32 * arrb
Definition: _int_bool.c:226
char * operand
Definition: ltxtquery_op.c:52
int32 * arre
Definition: _int_bool.c:227
int compareWordEntryPos(const void *a, const void *b)
Definition: tsvector.c:33
static TSTernaryValue checkclass_str(CHKVAL *chkval, WordEntry *entry, QueryOperand *val, ExecPhraseData *data)
Definition: tsvector_op.c:1185

References CHKVAL::arrb, CHKVAL::arre, checkclass_str(), compareWordEntryPos(), data, difference(), WordEntry::len, CHKVAL::operand, palloc(), pfree(), WordEntry::pos, qsort, qunique(), repalloc(), res, TS_MAYBE, TS_NO, TS_YES, tsCompareString(), val, and CHKVAL::values.

Referenced by ts_match_vq().

◆ chooseNextStatEntry()

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

Definition at line 2206 of file tsvector_op.c.

2208 {
2209  uint32 pos;
2210  uint32 middle = (low + high) >> 1;
2211 
2212  pos = (low + middle) >> 1;
2213  if (low != middle && pos >= offset && pos - offset < txt->size)
2214  insertStatEntry(persistentContext, stat, txt, pos - offset);
2215  pos = (high + middle + 1) >> 1;
2216  if (middle + 1 != high && pos >= offset && pos - offset < txt->size)
2217  insertStatEntry(persistentContext, stat, txt, pos - offset);
2218 
2219  if (low != middle)
2220  chooseNextStatEntry(persistentContext, stat, txt, low, middle, offset);
2221  if (high != middle + 1)
2222  chooseNextStatEntry(persistentContext, stat, txt, middle + 1, high, offset);
2223 }
unsigned int uint32
Definition: c.h:442
static void chooseNextStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 low, uint32 high, uint32 offset)
Definition: tsvector_op.c:2206
static void insertStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 off)
Definition: tsvector_op.c:2141

References insertStatEntry().

Referenced by ts_accum().

◆ compare_int()

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

Definition at line 429 of file tsvector_op.c.

430 {
431  int a = *((const int *) va);
432  int b = *((const int *) vb);
433 
434  if (a == b)
435  return 0;
436  return (a > b) ? 1 : -1;
437 }

References a, and b.

Referenced by tsvector_delete_by_indices().

◆ compare_text_lexemes()

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

Definition at line 440 of file tsvector_op.c.

441 {
442  Datum a = *((const Datum *) va);
443  Datum b = *((const Datum *) vb);
444  char *alex = VARDATA_ANY(a);
445  int alex_len = VARSIZE_ANY_EXHDR(a);
446  char *blex = VARDATA_ANY(b);
447  int blex_len = VARSIZE_ANY_EXHDR(b);
448 
449  return tsCompareString(alex, alex_len, blex, blex_len, false);
450 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:362
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:355

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

Referenced by array_to_tsvector().

◆ insertStatEntry()

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

Definition at line 2141 of file tsvector_op.c.

2142 {
2143  WordEntry *we = ARRPTR(txt) + off;
2144  StatEntry *node = stat->root,
2145  *pnode = NULL;
2146  int n,
2147  res = 0;
2148  uint32 depth = 1;
2149 
2150  if (stat->weight == 0)
2151  n = (we->haspos) ? POSDATALEN(txt, we) : 1;
2152  else
2153  n = (we->haspos) ? check_weight(txt, we, stat->weight) : 0;
2154 
2155  if (n == 0)
2156  return; /* nothing to insert */
2157 
2158  while (node)
2159  {
2160  res = compareStatWord(node, we, txt);
2161 
2162  if (res == 0)
2163  {
2164  break;
2165  }
2166  else
2167  {
2168  pnode = node;
2169  node = (res < 0) ? node->left : node->right;
2170  }
2171  depth++;
2172  }
2173 
2174  if (depth > stat->maxdepth)
2175  stat->maxdepth = depth;
2176 
2177  if (node == NULL)
2178  {
2179  node = MemoryContextAlloc(persistentContext, STATENTRYHDRSZ + we->len);
2180  node->left = node->right = NULL;
2181  node->ndoc = 1;
2182  node->nentry = n;
2183  node->lenlexeme = we->len;
2184  memcpy(node->lexeme, STRPTR(txt) + we->pos, node->lenlexeme);
2185 
2186  if (pnode == NULL)
2187  {
2188  stat->root = node;
2189  }
2190  else
2191  {
2192  if (res < 0)
2193  pnode->left = node;
2194  else
2195  pnode->right = node;
2196  }
2197  }
2198  else
2199  {
2200  node->ndoc++;
2201  node->nentry += n;
2202  }
2203 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:994
uint32 nentry
Definition: tsvector_op.c:49
struct StatEntry * left
Definition: tsvector_op.c:50
char lexeme[FLEXIBLE_ARRAY_MEMBER]
Definition: tsvector_op.c:53
uint32 lenlexeme
Definition: tsvector_op.c:52
uint32 ndoc
Definition: tsvector_op.c:47
struct StatEntry * right
Definition: tsvector_op.c:51
#define STATENTRYHDRSZ
Definition: tsvector_op.c:56
static int check_weight(TSVector txt, WordEntry *wptr, int8 weight)
Definition: tsvector_op.c:2120
#define compareStatWord(a, e, t)
Definition: tsvector_op.c:2135

References ARRPTR, check_weight(), compareStatWord, WordEntry::haspos, StatEntry::left, WordEntry::len, StatEntry::lenlexeme, StatEntry::lexeme, MemoryContextAlloc(), StatEntry::ndoc, StatEntry::nentry, WordEntry::pos, POSDATALEN, res, StatEntry::right, STATENTRYHDRSZ, and STRPTR.

Referenced by chooseNextStatEntry(), and ts_accum().

◆ silly_cmp_tsvector()

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

Definition at line 82 of file tsvector_op.c.

83 {
84  if (VARSIZE(a) < VARSIZE(b))
85  return -1;
86  else if (VARSIZE(a) > VARSIZE(b))
87  return 1;
88  else if (a->size < b->size)
89  return -1;
90  else if (a->size > b->size)
91  return 1;
92  else
93  {
94  WordEntry *aptr = ARRPTR(a);
95  WordEntry *bptr = ARRPTR(b);
96  int i = 0;
97  int res;
98 
99 
100  for (i = 0; i < a->size; i++)
101  {
102  if (aptr->haspos != bptr->haspos)
103  {
104  return (aptr->haspos > bptr->haspos) ? -1 : 1;
105  }
106  else if ((res = tsCompareString(STRPTR(a) + aptr->pos, aptr->len, STRPTR(b) + bptr->pos, bptr->len, false)) != 0)
107  {
108  return res;
109  }
110  else if (aptr->haspos)
111  {
112  WordEntryPos *ap = POSDATAPTR(a, aptr);
113  WordEntryPos *bp = POSDATAPTR(b, bptr);
114  int j;
115 
116  if (POSDATALEN(a, aptr) != POSDATALEN(b, bptr))
117  return (POSDATALEN(a, aptr) > POSDATALEN(b, bptr)) ? -1 : 1;
118 
119  for (j = 0; j < POSDATALEN(a, aptr); j++)
120  {
121  if (WEP_GETPOS(*ap) != WEP_GETPOS(*bp))
122  {
123  return (WEP_GETPOS(*ap) > WEP_GETPOS(*bp)) ? -1 : 1;
124  }
125  else if (WEP_GETWEIGHT(*ap) != WEP_GETWEIGHT(*bp))
126  {
127  return (WEP_GETWEIGHT(*ap) > WEP_GETWEIGHT(*bp)) ? -1 : 1;
128  }
129  ap++, bp++;
130  }
131  }
132 
133  aptr++;
134  bptr++;
135  }
136  }
137 
138  return 0;
139 }
int j
Definition: isn.c:74

References a, ARRPTR, b, WordEntry::haspos, i, j, WordEntry::len, WordEntry::pos, POSDATALEN, POSDATAPTR, res, STRPTR, tsCompareString(), VARSIZE, WEP_GETPOS, and WEP_GETWEIGHT.

◆ ts_accum()

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

Definition at line 2238 of file tsvector_op.c.

2239 {
2241  uint32 i,
2242  nbit = 0,
2243  offset;
2244 
2245  if (stat == NULL)
2246  { /* Init in first */
2247  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2248  stat->maxdepth = 1;
2249  }
2250 
2251  /* simple check of correctness */
2252  if (txt == NULL || txt->size == 0)
2253  {
2254  if (txt && txt != (TSVector) DatumGetPointer(data))
2255  pfree(txt);
2256  return stat;
2257  }
2258 
2259  i = txt->size - 1;
2260  for (; i > 0; i >>= 1)
2261  nbit++;
2262 
2263  nbit = 1 << nbit;
2264  offset = (nbit - txt->size) / 2;
2265 
2266  insertStatEntry(persistentContext, stat, txt, (nbit >> 1) - offset);
2267  chooseNextStatEntry(persistentContext, stat, txt, 0, nbit, offset);
2268 
2269  return stat;
2270 }
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1037
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:660
static TSVector DatumGetTSVector(Datum X)
Definition: ts_type.h:118
#define stat
Definition: win32_port.h:286

References chooseNextStatEntry(), data, DatumGetPointer(), DatumGetTSVector(), i, insertStatEntry(), MemoryContextAllocZero(), pfree(), TSVectorData::size, and stat.

Referenced by ts_stat_sql().

◆ TS_execute()

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

Definition at line 1850 of file tsvector_op.c.

1852 {
1853  /*
1854  * If we get TS_MAYBE from the recursion, return true. We could only see
1855  * that result if the caller passed TS_EXEC_PHRASE_NO_POS, so there's no
1856  * need to check again.
1857  */
1858  return TS_execute_recurse(curitem, arg, flags, chkcond) != TS_NO;
1859 }
void * arg
static TSTernaryValue TS_execute_recurse(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1879

References arg, TS_execute_recurse(), and TS_NO.

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

◆ TS_execute_recurse()

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

Definition at line 1879 of file tsvector_op.c.

1881 {
1882  TSTernaryValue lmatch;
1883 
1884  /* since this function recurses, it could be driven to stack overflow */
1886 
1887  /* ... and let's check for query cancel while we're at it */
1889 
1890  if (curitem->type == QI_VAL)
1891  return chkcond(arg, (QueryOperand *) curitem,
1892  NULL /* don't need position info */ );
1893 
1894  switch (curitem->qoperator.oper)
1895  {
1896  case OP_NOT:
1897  if (flags & TS_EXEC_SKIP_NOT)
1898  return TS_YES;
1899  switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1900  {
1901  case TS_NO:
1902  return TS_YES;
1903  case TS_YES:
1904  return TS_NO;
1905  case TS_MAYBE:
1906  return TS_MAYBE;
1907  }
1908  break;
1909 
1910  case OP_AND:
1911  lmatch = TS_execute_recurse(curitem + curitem->qoperator.left, arg,
1912  flags, chkcond);
1913  if (lmatch == TS_NO)
1914  return TS_NO;
1915  switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1916  {
1917  case TS_NO:
1918  return TS_NO;
1919  case TS_YES:
1920  return lmatch;
1921  case TS_MAYBE:
1922  return TS_MAYBE;
1923  }
1924  break;
1925 
1926  case OP_OR:
1927  lmatch = TS_execute_recurse(curitem + curitem->qoperator.left, arg,
1928  flags, chkcond);
1929  if (lmatch == TS_YES)
1930  return TS_YES;
1931  switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1932  {
1933  case TS_NO:
1934  return lmatch;
1935  case TS_YES:
1936  return TS_YES;
1937  case TS_MAYBE:
1938  return TS_MAYBE;
1939  }
1940  break;
1941 
1942  case OP_PHRASE:
1943 
1944  /*
1945  * If we get a MAYBE result, and the caller doesn't want that,
1946  * convert it to NO. It would be more consistent, perhaps, to
1947  * return the result of TS_phrase_execute() verbatim and then
1948  * convert MAYBE results at the top of the recursion. But
1949  * converting at the topmost phrase operator gives results that
1950  * are bug-compatible with the old implementation, so do it like
1951  * this for now.
1952  */
1953  switch (TS_phrase_execute(curitem, arg, flags, chkcond, NULL))
1954  {
1955  case TS_NO:
1956  return TS_NO;
1957  case TS_YES:
1958  return TS_YES;
1959  case TS_MAYBE:
1960  return (flags & TS_EXEC_PHRASE_NO_POS) ? TS_MAYBE : TS_NO;
1961  }
1962  break;
1963 
1964  default:
1965  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1966  }
1967 
1968  /* not reachable, but keep compiler quiet */
1969  return TS_NO;
1970 }
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
void check_stack_depth(void)
Definition: postgres.c:3440
uint32 left
Definition: ts_type.h:197
#define QI_VAL
Definition: ts_type.h:149
#define OP_AND
Definition: ts_type.h:180
#define OP_PHRASE
Definition: ts_type.h:182
#define OP_OR
Definition: ts_type.h:181
#define OP_NOT
Definition: ts_type.h:179
#define TS_EXEC_PHRASE_NO_POS
Definition: ts_utils.h:198
#define TS_EXEC_SKIP_NOT
Definition: ts_utils.h:191
static TSTernaryValue TS_phrase_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond, ExecPhraseData *data)
Definition: tsvector_op.c:1605
QueryOperator qoperator
Definition: ts_type.h:209
QueryItemType type
Definition: ts_type.h:208

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

Referenced by TS_execute(), and TS_execute_ternary().

◆ TS_execute_ternary()

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

Definition at line 1867 of file tsvector_op.c.

1869 {
1870  return TS_execute_recurse(curitem, arg, flags, chkcond);
1871 }

References arg, and TS_execute_recurse().

Referenced by gin_tsquery_consistent(), and gin_tsquery_triconsistent().

◆ ts_match_qv()

Datum ts_match_qv ( PG_FUNCTION_ARGS  )

Definition at line 2031 of file tsvector_op.c.

2032 {
2034  PG_GETARG_DATUM(1),
2035  PG_GETARG_DATUM(0)));
2036 }
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
Datum ts_match_vq(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:2039

References DirectFunctionCall2, PG_GETARG_DATUM, PG_RETURN_DATUM, and ts_match_vq().

◆ ts_match_tq()

Datum ts_match_tq ( PG_FUNCTION_ARGS  )

Definition at line 2091 of file tsvector_op.c.

2092 {
2093  TSVector vector;
2094  TSQuery query = PG_GETARG_TSQUERY(1);
2095  bool res;
2096 
2098  PG_GETARG_DATUM(0)));
2099 
2101  TSVectorGetDatum(vector),
2102  TSQueryGetDatum(query)));
2103 
2104  pfree(vector);
2105  PG_FREE_IF_COPY(query, 1);
2106 
2108 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static bool DatumGetBool(Datum X)
Definition: postgres.h:438
Datum to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:269
static Datum TSVectorGetDatum(const TSVectorData *X)
Definition: ts_type.h:130
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:266
static Datum TSQueryGetDatum(const TSQueryData *X)
Definition: ts_type.h:261

References DatumGetBool(), DatumGetTSVector(), DirectFunctionCall1, DirectFunctionCall2, pfree(), PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_GETARG_TSQUERY, PG_RETURN_BOOL, res, to_tsvector(), ts_match_vq(), TSQueryGetDatum(), and TSVectorGetDatum().

◆ ts_match_tt()

Datum ts_match_tt ( PG_FUNCTION_ARGS  )

Definition at line 2069 of file tsvector_op.c.

2070 {
2071  TSVector vector;
2072  TSQuery query;
2073  bool res;
2074 
2076  PG_GETARG_DATUM(0)));
2078  PG_GETARG_DATUM(1)));
2079 
2081  TSVectorGetDatum(vector),
2082  TSQueryGetDatum(query)));
2083 
2084  pfree(vector);
2085  pfree(query);
2086 
2088 }
Datum plainto_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:639
static TSQuery DatumGetTSQuery(Datum X)
Definition: ts_type.h:249

References DatumGetBool(), DatumGetTSQuery(), DatumGetTSVector(), DirectFunctionCall1, DirectFunctionCall2, pfree(), PG_GETARG_DATUM, PG_RETURN_BOOL, plainto_tsquery(), res, to_tsvector(), ts_match_vq(), TSQueryGetDatum(), and TSVectorGetDatum().

◆ ts_match_vq()

Datum ts_match_vq ( PG_FUNCTION_ARGS  )

Definition at line 2039 of file tsvector_op.c.

2040 {
2042  TSQuery query = PG_GETARG_TSQUERY(1);
2043  CHKVAL chkval;
2044  bool result;
2045 
2046  /* empty query matches nothing */
2047  if (!query->size)
2048  {
2049  PG_FREE_IF_COPY(val, 0);
2050  PG_FREE_IF_COPY(query, 1);
2051  PG_RETURN_BOOL(false);
2052  }
2053 
2054  chkval.arrb = ARRPTR(val);
2055  chkval.arre = chkval.arrb + val->size;
2056  chkval.values = STRPTR(val);
2057  chkval.operand = GETOPERAND(query);
2058  result = TS_execute(GETQUERY(query),
2059  &chkval,
2060  TS_EXEC_EMPTY,
2062 
2063  PG_FREE_IF_COPY(val, 0);
2064  PG_FREE_IF_COPY(query, 1);
2065  PG_RETURN_BOOL(result);
2066 }
#define GETQUERY(x)
Definition: _int.h:157
#define GETOPERAND(x)
Definition: ltree.h:164
int32 size
Definition: ts_type.h:221
#define TS_EXEC_EMPTY
Definition: ts_utils.h:184
static TSTernaryValue checkcondition_str(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsvector_op.c:1291
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1850

References CHKVAL::arrb, CHKVAL::arre, ARRPTR, checkcondition_str(), GETOPERAND, GETQUERY, CHKVAL::operand, PG_FREE_IF_COPY, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, PG_RETURN_BOOL, TSQueryData::size, STRPTR, TS_EXEC_EMPTY, TS_execute(), val, and CHKVAL::values.

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

◆ TS_phrase_execute()

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

Definition at line 1605 of file tsvector_op.c.

1608 {
1609  ExecPhraseData Ldata,
1610  Rdata;
1611  TSTernaryValue lmatch,
1612  rmatch;
1613  int Loffset,
1614  Roffset,
1615  maxwidth;
1616 
1617  /* since this function recurses, it could be driven to stack overflow */
1619 
1620  /* ... and let's check for query cancel while we're at it */
1622 
1623  if (curitem->type == QI_VAL)
1624  return chkcond(arg, (QueryOperand *) curitem, data);
1625 
1626  switch (curitem->qoperator.oper)
1627  {
1628  case OP_NOT:
1629 
1630  /*
1631  * We need not touch data->width, since a NOT operation does not
1632  * change the match width.
1633  */
1634  if (flags & TS_EXEC_SKIP_NOT)
1635  {
1636  /* with SKIP_NOT, report NOT as "match everywhere" */
1637  Assert(data->npos == 0 && !data->negate);
1638  data->negate = true;
1639  return TS_YES;
1640  }
1641  switch (TS_phrase_execute(curitem + 1, arg, flags, chkcond, data))
1642  {
1643  case TS_NO:
1644  /* change "match nowhere" to "match everywhere" */
1645  Assert(data->npos == 0 && !data->negate);
1646  data->negate = true;
1647  return TS_YES;
1648  case TS_YES:
1649  if (data->npos > 0)
1650  {
1651  /* we have some positions, invert negate flag */
1652  data->negate = !data->negate;
1653  return TS_YES;
1654  }
1655  else if (data->negate)
1656  {
1657  /* change "match everywhere" to "match nowhere" */
1658  data->negate = false;
1659  return TS_NO;
1660  }
1661  /* Should not get here if result was TS_YES */
1662  Assert(false);
1663  break;
1664  case TS_MAYBE:
1665  /* match positions are, and remain, uncertain */
1666  return TS_MAYBE;
1667  }
1668  break;
1669 
1670  case OP_PHRASE:
1671  case OP_AND:
1672  memset(&Ldata, 0, sizeof(Ldata));
1673  memset(&Rdata, 0, sizeof(Rdata));
1674 
1675  lmatch = TS_phrase_execute(curitem + curitem->qoperator.left,
1676  arg, flags, chkcond, &Ldata);
1677  if (lmatch == TS_NO)
1678  return TS_NO;
1679 
1680  rmatch = TS_phrase_execute(curitem + 1,
1681  arg, flags, chkcond, &Rdata);
1682  if (rmatch == TS_NO)
1683  return TS_NO;
1684 
1685  /*
1686  * If either operand has no position information, then we can't
1687  * return reliable position data, only a MAYBE result.
1688  */
1689  if (lmatch == TS_MAYBE || rmatch == TS_MAYBE)
1690  return TS_MAYBE;
1691 
1692  if (curitem->qoperator.oper == OP_PHRASE)
1693  {
1694  /*
1695  * Compute Loffset and Roffset suitable for phrase match, and
1696  * compute overall width of whole phrase match.
1697  */
1698  Loffset = curitem->qoperator.distance + Rdata.width;
1699  Roffset = 0;
1700  if (data)
1701  data->width = curitem->qoperator.distance +
1702  Ldata.width + Rdata.width;
1703  }
1704  else
1705  {
1706  /*
1707  * For OP_AND, set output width and alignment like OP_OR (see
1708  * comment below)
1709  */
1710  maxwidth = Max(Ldata.width, Rdata.width);
1711  Loffset = maxwidth - Ldata.width;
1712  Roffset = maxwidth - Rdata.width;
1713  if (data)
1714  data->width = maxwidth;
1715  }
1716 
1717  if (Ldata.negate && Rdata.negate)
1718  {
1719  /* !L & !R: treat as !(L | R) */
1720  (void) TS_phrase_output(data, &Ldata, &Rdata,
1722  Loffset, Roffset,
1723  Ldata.npos + Rdata.npos);
1724  if (data)
1725  data->negate = true;
1726  return TS_YES;
1727  }
1728  else if (Ldata.negate)
1729  {
1730  /* !L & R */
1731  return TS_phrase_output(data, &Ldata, &Rdata,
1732  TSPO_R_ONLY,
1733  Loffset, Roffset,
1734  Rdata.npos);
1735  }
1736  else if (Rdata.negate)
1737  {
1738  /* L & !R */
1739  return TS_phrase_output(data, &Ldata, &Rdata,
1740  TSPO_L_ONLY,
1741  Loffset, Roffset,
1742  Ldata.npos);
1743  }
1744  else
1745  {
1746  /* straight AND */
1747  return TS_phrase_output(data, &Ldata, &Rdata,
1748  TSPO_BOTH,
1749  Loffset, Roffset,
1750  Min(Ldata.npos, Rdata.npos));
1751  }
1752 
1753  case OP_OR:
1754  memset(&Ldata, 0, sizeof(Ldata));
1755  memset(&Rdata, 0, sizeof(Rdata));
1756 
1757  lmatch = TS_phrase_execute(curitem + curitem->qoperator.left,
1758  arg, flags, chkcond, &Ldata);
1759  rmatch = TS_phrase_execute(curitem + 1,
1760  arg, flags, chkcond, &Rdata);
1761 
1762  if (lmatch == TS_NO && rmatch == TS_NO)
1763  return TS_NO;
1764 
1765  /*
1766  * If either operand has no position information, then we can't
1767  * return reliable position data, only a MAYBE result.
1768  */
1769  if (lmatch == TS_MAYBE || rmatch == TS_MAYBE)
1770  return TS_MAYBE;
1771 
1772  /*
1773  * Cope with undefined output width from failed submatch. (This
1774  * takes less code than trying to ensure that all failure returns
1775  * set data->width to zero.)
1776  */
1777  if (lmatch == TS_NO)
1778  Ldata.width = 0;
1779  if (rmatch == TS_NO)
1780  Rdata.width = 0;
1781 
1782  /*
1783  * For OP_AND and OP_OR, report the width of the wider of the two
1784  * inputs, and align the narrower input's positions to the right
1785  * end of that width. This rule deals at least somewhat
1786  * reasonably with cases like "x <-> (y | z <-> q)".
1787  */
1788  maxwidth = Max(Ldata.width, Rdata.width);
1789  Loffset = maxwidth - Ldata.width;
1790  Roffset = maxwidth - Rdata.width;
1791  data->width = maxwidth;
1792 
1793  if (Ldata.negate && Rdata.negate)
1794  {
1795  /* !L | !R: treat as !(L & R) */
1796  (void) TS_phrase_output(data, &Ldata, &Rdata,
1797  TSPO_BOTH,
1798  Loffset, Roffset,
1799  Min(Ldata.npos, Rdata.npos));
1800  data->negate = true;
1801  return TS_YES;
1802  }
1803  else if (Ldata.negate)
1804  {
1805  /* !L | R: treat as !(L & !R) */
1806  (void) TS_phrase_output(data, &Ldata, &Rdata,
1807  TSPO_L_ONLY,
1808  Loffset, Roffset,
1809  Ldata.npos);
1810  data->negate = true;
1811  return TS_YES;
1812  }
1813  else if (Rdata.negate)
1814  {
1815  /* L | !R: treat as !(!L & R) */
1816  (void) TS_phrase_output(data, &Ldata, &Rdata,
1817  TSPO_R_ONLY,
1818  Loffset, Roffset,
1819  Rdata.npos);
1820  data->negate = true;
1821  return TS_YES;
1822  }
1823  else
1824  {
1825  /* straight OR */
1826  return TS_phrase_output(data, &Ldata, &Rdata,
1828  Loffset, Roffset,
1829  Ldata.npos + Rdata.npos);
1830  }
1831 
1832  default:
1833  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1834  }
1835 
1836  /* not reachable, but keep compiler quiet */
1837  return TS_NO;
1838 }
#define Min(x, y)
Definition: c.h:937
#define Max(x, y)
Definition: c.h:931
int16 distance
Definition: ts_type.h:196
#define TSPO_BOTH
Definition: tsvector_op.c:1461
#define TSPO_R_ONLY
Definition: tsvector_op.c:1460
static TSTernaryValue TS_phrase_output(ExecPhraseData *data, ExecPhraseData *Ldata, ExecPhraseData *Rdata, int emit, int Loffset, int Roffset, int max_npos)
Definition: tsvector_op.c:1464
#define TSPO_L_ONLY
Definition: tsvector_op.c:1459

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

Referenced by TS_execute_recurse().

◆ TS_phrase_output()

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

Definition at line 1464 of file tsvector_op.c.

1471 {
1472  int Lindex,
1473  Rindex;
1474 
1475  /* Loop until both inputs are exhausted */
1476  Lindex = Rindex = 0;
1477  while (Lindex < Ldata->npos || Rindex < Rdata->npos)
1478  {
1479  int Lpos,
1480  Rpos;
1481  int output_pos = 0;
1482 
1483  /*
1484  * Fetch current values to compare. WEP_GETPOS() is needed because
1485  * ExecPhraseData->data can point to a tsvector's WordEntryPosVector.
1486  */
1487  if (Lindex < Ldata->npos)
1488  Lpos = WEP_GETPOS(Ldata->pos[Lindex]) + Loffset;
1489  else
1490  {
1491  /* L array exhausted, so we're done if R_ONLY isn't set */
1492  if (!(emit & TSPO_R_ONLY))
1493  break;
1494  Lpos = INT_MAX;
1495  }
1496  if (Rindex < Rdata->npos)
1497  Rpos = WEP_GETPOS(Rdata->pos[Rindex]) + Roffset;
1498  else
1499  {
1500  /* R array exhausted, so we're done if L_ONLY isn't set */
1501  if (!(emit & TSPO_L_ONLY))
1502  break;
1503  Rpos = INT_MAX;
1504  }
1505 
1506  /* Merge-join the two input lists */
1507  if (Lpos < Rpos)
1508  {
1509  /* Lpos is not matched in Rdata, should we output it? */
1510  if (emit & TSPO_L_ONLY)
1511  output_pos = Lpos;
1512  Lindex++;
1513  }
1514  else if (Lpos == Rpos)
1515  {
1516  /* Lpos and Rpos match ... should we output it? */
1517  if (emit & TSPO_BOTH)
1518  output_pos = Rpos;
1519  Lindex++;
1520  Rindex++;
1521  }
1522  else /* Lpos > Rpos */
1523  {
1524  /* Rpos is not matched in Ldata, should we output it? */
1525  if (emit & TSPO_R_ONLY)
1526  output_pos = Rpos;
1527  Rindex++;
1528  }
1529 
1530  if (output_pos > 0)
1531  {
1532  if (data)
1533  {
1534  /* Store position, first allocating output array if needed */
1535  if (data->pos == NULL)
1536  {
1537  data->pos = (WordEntryPos *)
1538  palloc(max_npos * sizeof(WordEntryPos));
1539  data->allocated = true;
1540  }
1541  data->pos[data->npos++] = output_pos;
1542  }
1543  else
1544  {
1545  /*
1546  * Exact positions not needed, so return TS_YES as soon as we
1547  * know there is at least one.
1548  */
1549  return TS_YES;
1550  }
1551  }
1552  }
1553 
1554  if (data && data->npos > 0)
1555  {
1556  /* Let's assert we didn't overrun the array */
1557  Assert(data->npos <= max_npos);
1558  return TS_YES;
1559  }
1560  return TS_NO;
1561 }
WordEntryPos * pos
Definition: ts_utils.h:162

References Assert(), data, palloc(), ExecPhraseData::pos, TS_NO, TS_YES, TSPO_BOTH, TSPO_L_ONLY, TSPO_R_ONLY, and WEP_GETPOS.

Referenced by TS_phrase_execute().

◆ ts_process_call()

static Datum ts_process_call ( FuncCallContext funcctx)
static

Definition at line 2365 of file tsvector_op.c.

2366 {
2367  TSVectorStat *st;
2368  StatEntry *entry;
2369 
2370  st = (TSVectorStat *) funcctx->user_fctx;
2371 
2372  entry = walkStatEntryTree(st);
2373 
2374  if (entry != NULL)
2375  {
2376  Datum result;
2377  char *values[3];
2378  char ndoc[16];
2379  char nentry[16];
2380  HeapTuple tuple;
2381 
2382  values[0] = palloc(entry->lenlexeme + 1);
2383  memcpy(values[0], entry->lexeme, entry->lenlexeme);
2384  (values[0])[entry->lenlexeme] = '\0';
2385  sprintf(ndoc, "%d", entry->ndoc);
2386  values[1] = ndoc;
2387  sprintf(nentry, "%d", entry->nentry);
2388  values[2] = nentry;
2389 
2390  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
2391  result = HeapTupleGetDatum(tuple);
2392 
2393  pfree(values[0]);
2394 
2395  /* mark entry as already visited */
2396  entry->ndoc = 0;
2397 
2398  return result;
2399  }
2400 
2401  return (Datum) 0;
2402 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2135
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define sprintf
Definition: port.h:240
void * user_fctx
Definition: funcapi.h:82
AttInMetadata * attinmeta
Definition: funcapi.h:91
static StatEntry * walkStatEntryTree(TSVectorStat *stat)
Definition: tsvector_op.c:2319

References FuncCallContext::attinmeta, BuildTupleFromCStrings(), HeapTupleGetDatum(), StatEntry::lenlexeme, StatEntry::lexeme, StatEntry::ndoc, StatEntry::nentry, palloc(), pfree(), sprintf, FuncCallContext::user_fctx, values, and walkStatEntryTree().

Referenced by ts_stat1(), and ts_stat2().

◆ ts_setup_firstcall()

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

Definition at line 2273 of file tsvector_op.c.

2275 {
2276  TupleDesc tupdesc;
2277  MemoryContext oldcontext;
2278  StatEntry *node;
2279 
2280  funcctx->user_fctx = (void *) stat;
2281 
2282  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2283 
2284  stat->stack = palloc0(sizeof(StatEntry *) * (stat->maxdepth + 1));
2285  stat->stackpos = 0;
2286 
2287  node = stat->root;
2288  /* find leftmost value */
2289  if (node == NULL)
2290  stat->stack[stat->stackpos] = NULL;
2291  else
2292  for (;;)
2293  {
2294  stat->stack[stat->stackpos] = node;
2295  if (node->left)
2296  {
2297  stat->stackpos++;
2298  node = node->left;
2299  }
2300  else
2301  break;
2302  }
2303  Assert(stat->stackpos <= stat->maxdepth);
2304 
2305  tupdesc = CreateTemplateTupleDesc(3);
2306  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "word",
2307  TEXTOID, -1, 0);
2308  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "ndoc",
2309  INT4OID, -1, 0);
2310  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "nentry",
2311  INT4OID, -1, 0);
2312  funcctx->tuple_desc = BlessTupleDesc(tupdesc);
2313  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
2314 
2315  MemoryContextSwitchTo(oldcontext);
2316 }
int16 AttrNumber
Definition: attnum.h:21
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2071
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2086
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
TupleDesc tuple_desc
Definition: funcapi.h:112
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583

References Assert(), FuncCallContext::attinmeta, BlessTupleDesc(), CreateTemplateTupleDesc(), StatEntry::left, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc0(), FuncCallContext::tuple_desc, TupleDescGetAttInMetadata(), TupleDescInitEntry(), and FuncCallContext::user_fctx.

Referenced by ts_stat1(), and ts_stat2().

◆ ts_stat1()

Datum ts_stat1 ( PG_FUNCTION_ARGS  )

Definition at line 2494 of file tsvector_op.c.

2495 {
2496  FuncCallContext *funcctx;
2497  Datum result;
2498 
2499  if (SRF_IS_FIRSTCALL())
2500  {
2501  TSVectorStat *stat;
2502  text *txt = PG_GETARG_TEXT_PP(0);
2503 
2504  funcctx = SRF_FIRSTCALL_INIT();
2505  SPI_connect();
2506  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, NULL);
2507  PG_FREE_IF_COPY(txt, 0);
2508  ts_setup_firstcall(fcinfo, funcctx, stat);
2509  SPI_finish();
2510  }
2511 
2512  funcctx = SRF_PERCALL_SETUP();
2513  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2514  SRF_RETURN_NEXT(funcctx, result);
2515  SRF_RETURN_DONE(funcctx);
2516 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:303
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:307
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:305
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:327
int SPI_connect(void)
Definition: spi.c:95
int SPI_finish(void)
Definition: spi.c:183
Definition: c.h:623
static Datum ts_process_call(FuncCallContext *funcctx)
Definition: tsvector_op.c:2365
static void ts_setup_firstcall(FunctionCallInfo fcinfo, FuncCallContext *funcctx, TSVectorStat *stat)
Definition: tsvector_op.c:2273
static TSVectorStat * ts_stat_sql(MemoryContext persistentContext, text *txt, text *ws)
Definition: tsvector_op.c:2405

References FuncCallContext::multi_call_memory_ctx, PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, SPI_connect(), SPI_finish(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, stat, ts_process_call(), ts_setup_firstcall(), and ts_stat_sql().

◆ ts_stat2()

Datum ts_stat2 ( PG_FUNCTION_ARGS  )

Definition at line 2519 of file tsvector_op.c.

2520 {
2521  FuncCallContext *funcctx;
2522  Datum result;
2523 
2524  if (SRF_IS_FIRSTCALL())
2525  {
2526  TSVectorStat *stat;
2527  text *txt = PG_GETARG_TEXT_PP(0);
2528  text *ws = PG_GETARG_TEXT_PP(1);
2529 
2530  funcctx = SRF_FIRSTCALL_INIT();
2531  SPI_connect();
2532  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, ws);
2533  PG_FREE_IF_COPY(txt, 0);
2534  PG_FREE_IF_COPY(ws, 1);
2535  ts_setup_firstcall(fcinfo, funcctx, stat);
2536  SPI_finish();
2537  }
2538 
2539  funcctx = SRF_PERCALL_SETUP();
2540  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2541  SRF_RETURN_NEXT(funcctx, result);
2542  SRF_RETURN_DONE(funcctx);
2543 }

References FuncCallContext::multi_call_memory_ctx, PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, SPI_connect(), SPI_finish(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, stat, ts_process_call(), ts_setup_firstcall(), and ts_stat_sql().

◆ ts_stat_sql()

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

Definition at line 2405 of file tsvector_op.c.

2406 {
2407  char *query = text_to_cstring(txt);
2408  TSVectorStat *stat;
2409  bool isnull;
2410  Portal portal;
2411  SPIPlanPtr plan;
2412 
2413  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2414  /* internal error */
2415  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2416 
2417  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2418  /* internal error */
2419  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2420 
2421  SPI_cursor_fetch(portal, true, 100);
2422 
2423  if (SPI_tuptable == NULL ||
2424  SPI_tuptable->tupdesc->natts != 1 ||
2426  TSVECTOROID))
2427  ereport(ERROR,
2428  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2429  errmsg("ts_stat query must return one tsvector column")));
2430 
2431  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2432  stat->maxdepth = 1;
2433 
2434  if (ws)
2435  {
2436  char *buf;
2437 
2438  buf = VARDATA_ANY(ws);
2439  while (buf - VARDATA_ANY(ws) < VARSIZE_ANY_EXHDR(ws))
2440  {
2441  if (pg_mblen(buf) == 1)
2442  {
2443  switch (*buf)
2444  {
2445  case 'A':
2446  case 'a':
2447  stat->weight |= 1 << 3;
2448  break;
2449  case 'B':
2450  case 'b':
2451  stat->weight |= 1 << 2;
2452  break;
2453  case 'C':
2454  case 'c':
2455  stat->weight |= 1 << 1;
2456  break;
2457  case 'D':
2458  case 'd':
2459  stat->weight |= 1;
2460  break;
2461  default:
2462  stat->weight |= 0;
2463  }
2464  }
2465  buf += pg_mblen(buf);
2466  }
2467  }
2468 
2469  while (SPI_processed > 0)
2470  {
2471  uint64 i;
2472 
2473  for (i = 0; i < SPI_processed; i++)
2474  {
2476 
2477  if (!isnull)
2478  stat = ts_accum(persistentContext, stat, data);
2479  }
2480 
2482  SPI_cursor_fetch(portal, true, 100);
2483  }
2484 
2486  SPI_cursor_close(portal);
2487  SPI_freeplan(plan);
2488  pfree(query);
2489 
2490  return stat;
2491 }
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
bool IsBinaryCoercible(Oid srctype, Oid targettype)
static char * buf
Definition: pg_test_fsync.c:67
uint64 SPI_processed
Definition: spi.c:45
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1306
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:1023
SPITupleTable * SPI_tuptable
Definition: spi.c:46
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1804
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1384
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1443
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:858
void SPI_cursor_close(Portal portal)
Definition: spi.c:1860
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1250
TupleDesc tupdesc
Definition: spi.h:25
HeapTuple * vals
Definition: spi.h:26
static TSVectorStat * ts_accum(MemoryContext persistentContext, TSVectorStat *stat, Datum data)
Definition: tsvector_op.c:2238
char * text_to_cstring(const text *t)
Definition: varlena.c:222

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

Referenced by ts_stat1(), and ts_stat2().

◆ tsCompareString()

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

Definition at line 1148 of file tsvector_op.c.

1149 {
1150  int cmp;
1151 
1152  if (lena == 0)
1153  {
1154  if (prefix)
1155  cmp = 0; /* empty string is prefix of anything */
1156  else
1157  cmp = (lenb > 0) ? -1 : 0;
1158  }
1159  else if (lenb == 0)
1160  {
1161  cmp = (lena > 0) ? 1 : 0;
1162  }
1163  else
1164  {
1165  cmp = memcmp(a, b, Min((unsigned int) lena, (unsigned int) lenb));
1166 
1167  if (prefix)
1168  {
1169  if (cmp == 0 && lena > lenb)
1170  cmp = 1; /* a is longer, so not a prefix of b */
1171  }
1172  else if (cmp == 0 && lena != lenb)
1173  {
1174  cmp = (lena < lenb) ? -1 : 1;
1175  }
1176  }
1177 
1178  return cmp;
1179 }
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:747

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

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

◆ tsquery_requires_match()

bool tsquery_requires_match ( QueryItem curitem)

Definition at line 1981 of file tsvector_op.c.

1982 {
1983  /* since this function recurses, it could be driven to stack overflow */
1985 
1986  if (curitem->type == QI_VAL)
1987  return true;
1988 
1989  switch (curitem->qoperator.oper)
1990  {
1991  case OP_NOT:
1992 
1993  /*
1994  * Assume there are no required matches underneath a NOT. For
1995  * some cases with nested NOTs, we could prove there's a required
1996  * match, but it seems unlikely to be worth the trouble.
1997  */
1998  return false;
1999 
2000  case OP_PHRASE:
2001 
2002  /*
2003  * Treat OP_PHRASE as OP_AND here
2004  */
2005  case OP_AND:
2006  /* If either side requires a match, we're good */
2007  if (tsquery_requires_match(curitem + curitem->qoperator.left))
2008  return true;
2009  else
2010  return tsquery_requires_match(curitem + 1);
2011 
2012  case OP_OR:
2013  /* Both sides must require a match */
2014  if (tsquery_requires_match(curitem + curitem->qoperator.left))
2015  return tsquery_requires_match(curitem + 1);
2016  else
2017  return false;
2018 
2019  default:
2020  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
2021  }
2022 
2023  /* not reachable, but keep compiler quiet */
2024  return false;
2025 }
bool tsquery_requires_match(QueryItem *curitem)
Definition: tsvector_op.c:1981

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

Referenced by gin_extract_tsquery().

◆ tsvector_bsearch()

static int tsvector_bsearch ( const TSVector  tsv,
char *  lexeme,
int  lexeme_len 
)
static

Definition at line 396 of file tsvector_op.c.

397 {
398  WordEntry *arrin = ARRPTR(tsv);
399  int StopLow = 0,
400  StopHigh = tsv->size,
401  StopMiddle,
402  cmp;
403 
404  while (StopLow < StopHigh)
405  {
406  StopMiddle = (StopLow + StopHigh) / 2;
407 
408  cmp = tsCompareString(lexeme, lexeme_len,
409  STRPTR(tsv) + arrin[StopMiddle].pos,
410  arrin[StopMiddle].len,
411  false);
412 
413  if (cmp < 0)
414  StopHigh = StopMiddle;
415  else if (cmp > 0)
416  StopLow = StopMiddle + 1;
417  else /* found it */
418  return StopMiddle;
419  }
420 
421  return -1;
422 }

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

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

◆ tsvector_concat()

Datum tsvector_concat ( PG_FUNCTION_ARGS  )

Definition at line 921 of file tsvector_op.c.

922 {
923  TSVector in1 = PG_GETARG_TSVECTOR(0);
924  TSVector in2 = PG_GETARG_TSVECTOR(1);
925  TSVector out;
926  WordEntry *ptr;
927  WordEntry *ptr1,
928  *ptr2;
929  WordEntryPos *p;
930  int maxpos = 0,
931  i,
932  j,
933  i1,
934  i2,
935  dataoff,
936  output_bytes,
937  output_size;
938  char *data,
939  *data1,
940  *data2;
941 
942  /* Get max position in in1; we'll need this to offset in2's positions */
943  ptr = ARRPTR(in1);
944  i = in1->size;
945  while (i--)
946  {
947  if ((j = POSDATALEN(in1, ptr)) != 0)
948  {
949  p = POSDATAPTR(in1, ptr);
950  while (j--)
951  {
952  if (WEP_GETPOS(*p) > maxpos)
953  maxpos = WEP_GETPOS(*p);
954  p++;
955  }
956  }
957  ptr++;
958  }
959 
960  ptr1 = ARRPTR(in1);
961  ptr2 = ARRPTR(in2);
962  data1 = STRPTR(in1);
963  data2 = STRPTR(in2);
964  i1 = in1->size;
965  i2 = in2->size;
966 
967  /*
968  * Conservative estimate of space needed. We might need all the data in
969  * both inputs, and conceivably add a pad byte before position data for
970  * each item where there was none before.
971  */
972  output_bytes = VARSIZE(in1) + VARSIZE(in2) + i1 + i2;
973 
974  out = (TSVector) palloc0(output_bytes);
975  SET_VARSIZE(out, output_bytes);
976 
977  /*
978  * We must make out->size valid so that STRPTR(out) is sensible. We'll
979  * collapse out any unused space at the end.
980  */
981  out->size = in1->size + in2->size;
982 
983  ptr = ARRPTR(out);
984  data = STRPTR(out);
985  dataoff = 0;
986  while (i1 && i2)
987  {
988  int cmp = compareEntry(data1, ptr1, data2, ptr2);
989 
990  if (cmp < 0)
991  { /* in1 first */
992  ptr->haspos = ptr1->haspos;
993  ptr->len = ptr1->len;
994  memcpy(data + dataoff, data1 + ptr1->pos, ptr1->len);
995  ptr->pos = dataoff;
996  dataoff += ptr1->len;
997  if (ptr->haspos)
998  {
999  dataoff = SHORTALIGN(dataoff);
1000  memcpy(data + dataoff, _POSVECPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16));
1001  dataoff += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16);
1002  }
1003 
1004  ptr++;
1005  ptr1++;
1006  i1--;
1007  }
1008  else if (cmp > 0)
1009  { /* in2 first */
1010  ptr->haspos = ptr2->haspos;
1011  ptr->len = ptr2->len;
1012  memcpy(data + dataoff, data2 + ptr2->pos, ptr2->len);
1013  ptr->pos = dataoff;
1014  dataoff += ptr2->len;
1015  if (ptr->haspos)
1016  {
1017  int addlen = add_pos(in2, ptr2, out, ptr, maxpos);
1018 
1019  if (addlen == 0)
1020  ptr->haspos = 0;
1021  else
1022  {
1023  dataoff = SHORTALIGN(dataoff);
1024  dataoff += addlen * sizeof(WordEntryPos) + sizeof(uint16);
1025  }
1026  }
1027 
1028  ptr++;
1029  ptr2++;
1030  i2--;
1031  }
1032  else
1033  {
1034  ptr->haspos = ptr1->haspos | ptr2->haspos;
1035  ptr->len = ptr1->len;
1036  memcpy(data + dataoff, data1 + ptr1->pos, ptr1->len);
1037  ptr->pos = dataoff;
1038  dataoff += ptr1->len;
1039  if (ptr->haspos)
1040  {
1041  if (ptr1->haspos)
1042  {
1043  dataoff = SHORTALIGN(dataoff);
1044  memcpy(data + dataoff, _POSVECPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16));
1045  dataoff += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16);
1046  if (ptr2->haspos)
1047  dataoff += add_pos(in2, ptr2, out, ptr, maxpos) * sizeof(WordEntryPos);
1048  }
1049  else /* must have ptr2->haspos */
1050  {
1051  int addlen = add_pos(in2, ptr2, out, ptr, maxpos);
1052 
1053  if (addlen == 0)
1054  ptr->haspos = 0;
1055  else
1056  {
1057  dataoff = SHORTALIGN(dataoff);
1058  dataoff += addlen * sizeof(WordEntryPos) + sizeof(uint16);
1059  }
1060  }
1061  }
1062 
1063  ptr++;
1064  ptr1++;
1065  ptr2++;
1066  i1--;
1067  i2--;
1068  }
1069  }
1070 
1071  while (i1)
1072  {
1073  ptr->haspos = ptr1->haspos;
1074  ptr->len = ptr1->len;
1075  memcpy(data + dataoff, data1 + ptr1->pos, ptr1->len);
1076  ptr->pos = dataoff;
1077  dataoff += ptr1->len;
1078  if (ptr->haspos)
1079  {
1080  dataoff = SHORTALIGN(dataoff);
1081  memcpy(data + dataoff, _POSVECPTR(in1, ptr1), POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16));
1082  dataoff += POSDATALEN(in1, ptr1) * sizeof(WordEntryPos) + sizeof(uint16);
1083  }
1084 
1085  ptr++;
1086  ptr1++;
1087  i1--;
1088  }
1089 
1090  while (i2)
1091  {
1092  ptr->haspos = ptr2->haspos;
1093  ptr->len = ptr2->len;
1094  memcpy(data + dataoff, data2 + ptr2->pos, ptr2->len);
1095  ptr->pos = dataoff;
1096  dataoff += ptr2->len;
1097  if (ptr->haspos)
1098  {
1099  int addlen = add_pos(in2, ptr2, out, ptr, maxpos);
1100 
1101  if (addlen == 0)
1102  ptr->haspos = 0;
1103  else
1104  {
1105  dataoff = SHORTALIGN(dataoff);
1106  dataoff += addlen * sizeof(WordEntryPos) + sizeof(uint16);
1107  }
1108  }
1109 
1110  ptr++;
1111  ptr2++;
1112  i2--;
1113  }
1114 
1115  /*
1116  * Instead of checking each offset individually, we check for overflow of
1117  * pos fields once at the end.
1118  */
1119  if (dataoff > MAXSTRPOS)
1120  ereport(ERROR,
1121  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1122  errmsg("string is too long for tsvector (%d bytes, max %d bytes)", dataoff, MAXSTRPOS)));
1123 
1124  /*
1125  * Adjust sizes (asserting that we didn't overrun the original estimates)
1126  * and collapse out any unused array entries.
1127  */
1128  output_size = ptr - ARRPTR(out);
1129  Assert(output_size <= out->size);
1130  out->size = output_size;
1131  if (data != STRPTR(out))
1132  memmove(STRPTR(out), data, dataoff);
1133  output_bytes = CALCDATASIZE(out->size, dataoff);
1134  Assert(output_bytes <= VARSIZE(out));
1135  SET_VARSIZE(out, output_bytes);
1136 
1137  PG_FREE_IF_COPY(in1, 0);
1138  PG_FREE_IF_COPY(in2, 1);
1139  PG_RETURN_POINTER(out);
1140 }
#define MAXSTRPOS
Definition: ts_type.h:50
static int32 add_pos(TSVector src, WordEntry *srcptr, TSVector dest, WordEntry *destptr, int32 maxpos)
Definition: tsvector_op.c:360
#define compareEntry(pa, a, pb, b)
Definition: tsvector_op.c:350

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

◆ tsvector_delete_arr()

Datum tsvector_delete_arr ( PG_FUNCTION_ARGS  )

Definition at line 576 of file tsvector_op.c.

577 {
578  TSVector tsin = PG_GETARG_TSVECTOR(0),
579  tsout;
580  ArrayType *lexemes = PG_GETARG_ARRAYTYPE_P(1);
581  int i,
582  nlex,
583  skip_count,
584  *skip_indices;
585  Datum *dlexemes;
586  bool *nulls;
587 
588  deconstruct_array_builtin(lexemes, TEXTOID, &dlexemes, &nulls, &nlex);
589 
590  /*
591  * In typical use case array of lexemes to delete is relatively small. So
592  * here we optimize things for that scenario: iterate through lexarr
593  * performing binary search of each lexeme from lexarr in tsvector.
594  */
595  skip_indices = palloc0(nlex * sizeof(int));
596  for (i = skip_count = 0; i < nlex; i++)
597  {
598  char *lex;
599  int lex_len,
600  lex_pos;
601 
602  /* Ignore null array elements, they surely don't match */
603  if (nulls[i])
604  continue;
605 
606  lex = VARDATA(dlexemes[i]);
607  lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
608  lex_pos = tsvector_bsearch(tsin, lex, lex_len);
609 
610  if (lex_pos >= 0)
611  skip_indices[skip_count++] = lex_pos;
612  }
613 
614  tsout = tsvector_delete_by_indices(tsin, skip_indices, skip_count);
615 
616  pfree(skip_indices);
617  PG_FREE_IF_COPY(tsin, 0);
618  PG_FREE_IF_COPY(lexemes, 1);
619 
620  PG_RETURN_POINTER(tsout);
621 }
static int tsvector_bsearch(const TSVector tsv, char *lexeme, int lexeme_len)
Definition: tsvector_op.c:396
static TSVector tsvector_delete_by_indices(TSVector tsv, int *indices_to_delete, int indices_count)
Definition: tsvector_op.c:462

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

◆ tsvector_delete_by_indices()

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

Definition at line 462 of file tsvector_op.c.

464 {
465  TSVector tsout;
466  WordEntry *arrin = ARRPTR(tsv),
467  *arrout;
468  char *data = STRPTR(tsv),
469  *dataout;
470  int i, /* index in arrin */
471  j, /* index in arrout */
472  k, /* index in indices_to_delete */
473  curoff; /* index in dataout area */
474 
475  /*
476  * Sort the filter array to simplify membership checks below. Also, get
477  * rid of any duplicate entries, so that we can assume that indices_count
478  * is exactly equal to the number of lexemes that will be removed.
479  */
480  if (indices_count > 1)
481  {
482  qsort(indices_to_delete, indices_count, sizeof(int), compare_int);
483  indices_count = qunique(indices_to_delete, indices_count, sizeof(int),
484  compare_int);
485  }
486 
487  /*
488  * Here we overestimate tsout size, since we don't know how much space is
489  * used by the deleted lexeme(s). We will set exact size below.
490  */
491  tsout = (TSVector) palloc0(VARSIZE(tsv));
492 
493  /* This count must be correct because STRPTR(tsout) relies on it. */
494  tsout->size = tsv->size - indices_count;
495 
496  /*
497  * Copy tsv to tsout, skipping lexemes listed in indices_to_delete.
498  */
499  arrout = ARRPTR(tsout);
500  dataout = STRPTR(tsout);
501  curoff = 0;
502  for (i = j = k = 0; i < tsv->size; i++)
503  {
504  /*
505  * If current i is present in indices_to_delete, skip this lexeme.
506  * Since indices_to_delete is already sorted, we only need to check
507  * the current (k'th) entry.
508  */
509  if (k < indices_count && i == indices_to_delete[k])
510  {
511  k++;
512  continue;
513  }
514 
515  /* Copy lexeme and its positions and weights */
516  memcpy(dataout + curoff, data + arrin[i].pos, arrin[i].len);
517  arrout[j].haspos = arrin[i].haspos;
518  arrout[j].len = arrin[i].len;
519  arrout[j].pos = curoff;
520  curoff += arrin[i].len;
521  if (arrin[i].haspos)
522  {
523  int len = POSDATALEN(tsv, arrin + i) * sizeof(WordEntryPos)
524  + sizeof(uint16);
525 
526  curoff = SHORTALIGN(curoff);
527  memcpy(dataout + curoff,
528  STRPTR(tsv) + SHORTALIGN(arrin[i].pos + arrin[i].len),
529  len);
530  curoff += len;
531  }
532 
533  j++;
534  }
535 
536  /*
537  * k should now be exactly equal to indices_count. If it isn't then the
538  * caller provided us with indices outside of [0, tsv->size) range and
539  * estimation of tsout's size is wrong.
540  */
541  Assert(k == indices_count);
542 
543  SET_VARSIZE(tsout, CALCDATASIZE(tsout->size, curoff));
544  return tsout;
545 }
static int compare_int(const void *va, const void *vb)
Definition: tsvector_op.c:429

References ARRPTR, Assert(), CALCDATASIZE, compare_int(), data, WordEntry::haspos, i, j, WordEntry::len, len, palloc0(), POSDATALEN, qsort, qunique(), SET_VARSIZE, SHORTALIGN, TSVectorData::size, STRPTR, and VARSIZE.

Referenced by tsvector_delete_arr(), and tsvector_delete_str().

◆ tsvector_delete_str()

Datum tsvector_delete_str ( PG_FUNCTION_ARGS  )

Definition at line 552 of file tsvector_op.c.

553 {
554  TSVector tsin = PG_GETARG_TSVECTOR(0),
555  tsout;
556  text *tlexeme = PG_GETARG_TEXT_PP(1);
557  char *lexeme = VARDATA_ANY(tlexeme);
558  int lexeme_len = VARSIZE_ANY_EXHDR(tlexeme),
559  skip_index;
560 
561  if ((skip_index = tsvector_bsearch(tsin, lexeme, lexeme_len)) == -1)
562  PG_RETURN_POINTER(tsin);
563 
564  tsout = tsvector_delete_by_indices(tsin, &skip_index, 1);
565 
566  PG_FREE_IF_COPY(tsin, 0);
567  PG_FREE_IF_COPY(tlexeme, 1);
568  PG_RETURN_POINTER(tsout);
569 }

References StatEntry::lexeme, PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, tsvector_bsearch(), tsvector_delete_by_indices(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ tsvector_filter()

Datum tsvector_filter ( PG_FUNCTION_ARGS  )

Definition at line 815 of file tsvector_op.c.

816 {
817  TSVector tsin = PG_GETARG_TSVECTOR(0),
818  tsout;
820  WordEntry *arrin = ARRPTR(tsin),
821  *arrout;
822  char *datain = STRPTR(tsin),
823  *dataout;
824  Datum *dweights;
825  bool *nulls;
826  int nweights;
827  int i,
828  j;
829  int cur_pos = 0;
830  char mask = 0;
831 
832  deconstruct_array_builtin(weights, CHAROID, &dweights, &nulls, &nweights);
833 
834  for (i = 0; i < nweights; i++)
835  {
836  char char_weight;
837 
838  if (nulls[i])
839  ereport(ERROR,
840  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
841  errmsg("weight array may not contain nulls")));
842 
843  char_weight = DatumGetChar(dweights[i]);
844  switch (char_weight)
845  {
846  case 'A':
847  case 'a':
848  mask = mask | 8;
849  break;
850  case 'B':
851  case 'b':
852  mask = mask | 4;
853  break;
854  case 'C':
855  case 'c':
856  mask = mask | 2;
857  break;
858  case 'D':
859  case 'd':
860  mask = mask | 1;
861  break;
862  default:
863  ereport(ERROR,
864  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
865  errmsg("unrecognized weight: \"%c\"", char_weight)));
866  }
867  }
868 
869  tsout = (TSVector) palloc0(VARSIZE(tsin));
870  tsout->size = tsin->size;
871  arrout = ARRPTR(tsout);
872  dataout = STRPTR(tsout);
873 
874  for (i = j = 0; i < tsin->size; i++)
875  {
876  WordEntryPosVector *posvin,
877  *posvout;
878  int npos = 0;
879  int k;
880 
881  if (!arrin[i].haspos)
882  continue;
883 
884  posvin = _POSVECPTR(tsin, arrin + i);
885  posvout = (WordEntryPosVector *)
886  (dataout + SHORTALIGN(cur_pos + arrin[i].len));
887 
888  for (k = 0; k < posvin->npos; k++)
889  {
890  if (mask & (1 << WEP_GETWEIGHT(posvin->pos[k])))
891  posvout->pos[npos++] = posvin->pos[k];
892  }
893 
894  /* if no satisfactory positions found, skip lexeme */
895  if (!npos)
896  continue;
897 
898  arrout[j].haspos = true;
899  arrout[j].len = arrin[i].len;
900  arrout[j].pos = cur_pos;
901 
902  memcpy(dataout + cur_pos, datain + arrin[i].pos, arrin[i].len);
903  posvout->npos = npos;
904  cur_pos += SHORTALIGN(arrin[i].len);
905  cur_pos += POSDATALEN(tsout, arrout + j) * sizeof(WordEntryPos) +
906  sizeof(uint16);
907  j++;
908  }
909 
910  tsout->size = j;
911  if (dataout != STRPTR(tsout))
912  memmove(STRPTR(tsout), dataout, cur_pos);
913 
914  SET_VARSIZE(tsout, CALCDATASIZE(tsout->size, cur_pos));
915 
916  PG_FREE_IF_COPY(tsin, 0);
917  PG_RETURN_POINTER(tsout);
918 }
static char DatumGetChar(Datum X)
Definition: postgres.h:460
static const float weights[]
Definition: tsrank.c:24

References _POSVECPTR, ARRPTR, CALCDATASIZE, DatumGetChar(), deconstruct_array_builtin(), ereport, errcode(), errmsg(), ERROR, i, j, WordEntry::len, len, WordEntryPosVector::npos, palloc0(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, WordEntryPosVector::pos, POSDATALEN, SET_VARSIZE, SHORTALIGN, TSVectorData::size, STRPTR, VARSIZE, weights, and WEP_GETWEIGHT.

◆ tsvector_length()

Datum tsvector_length ( PG_FUNCTION_ARGS  )

Definition at line 197 of file tsvector_op.c.

198 {
200  int32 ret = in->size;
201 
202  PG_FREE_IF_COPY(in, 0);
203  PG_RETURN_INT32(ret);
204 }
signed int int32
Definition: c.h:430
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354

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

◆ tsvector_setweight()

Datum tsvector_setweight ( PG_FUNCTION_ARGS  )

Definition at line 207 of file tsvector_op.c.

208 {
210  char cw = PG_GETARG_CHAR(1);
211  TSVector out;
212  int i,
213  j;
214  WordEntry *entry;
215  WordEntryPos *p;
216  int w = 0;
217 
218  switch (cw)
219  {
220  case 'A':
221  case 'a':
222  w = 3;
223  break;
224  case 'B':
225  case 'b':
226  w = 2;
227  break;
228  case 'C':
229  case 'c':
230  w = 1;
231  break;
232  case 'D':
233  case 'd':
234  w = 0;
235  break;
236  default:
237  /* internal error */
238  elog(ERROR, "unrecognized weight: %d", cw);
239  }
240 
241  out = (TSVector) palloc(VARSIZE(in));
242  memcpy(out, in, VARSIZE(in));
243  entry = ARRPTR(out);
244  i = out->size;
245  while (i--)
246  {
247  if ((j = POSDATALEN(out, entry)) != 0)
248  {
249  p = POSDATAPTR(out, entry);
250  while (j--)
251  {
252  WEP_SETWEIGHT(*p, w);
253  p++;
254  }
255  }
256  entry++;
257  }
258 
259  PG_FREE_IF_COPY(in, 0);
260  PG_RETURN_POINTER(out);
261 }
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:273

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

◆ tsvector_setweight_by_filter()

Datum tsvector_setweight_by_filter ( PG_FUNCTION_ARGS  )

Definition at line 269 of file tsvector_op.c.

270 {
271  TSVector tsin = PG_GETARG_TSVECTOR(0);
272  char char_weight = PG_GETARG_CHAR(1);
273  ArrayType *lexemes = PG_GETARG_ARRAYTYPE_P(2);
274 
275  TSVector tsout;
276  int i,
277  j,
278  nlexemes,
279  weight;
280  WordEntry *entry;
281  Datum *dlexemes;
282  bool *nulls;
283 
284  switch (char_weight)
285  {
286  case 'A':
287  case 'a':
288  weight = 3;
289  break;
290  case 'B':
291  case 'b':
292  weight = 2;
293  break;
294  case 'C':
295  case 'c':
296  weight = 1;
297  break;
298  case 'D':
299  case 'd':
300  weight = 0;
301  break;
302  default:
303  /* internal error */
304  elog(ERROR, "unrecognized weight: %c", char_weight);
305  }
306 
307  tsout = (TSVector) palloc(VARSIZE(tsin));
308  memcpy(tsout, tsin, VARSIZE(tsin));
309  entry = ARRPTR(tsout);
310 
311  deconstruct_array_builtin(lexemes, TEXTOID, &dlexemes, &nulls, &nlexemes);
312 
313  /*
314  * Assuming that lexemes array is significantly shorter than tsvector we
315  * can iterate through lexemes performing binary search of each lexeme
316  * from lexemes in tsvector.
317  */
318  for (i = 0; i < nlexemes; i++)
319  {
320  char *lex;
321  int lex_len,
322  lex_pos;
323 
324  /* Ignore null array elements, they surely don't match */
325  if (nulls[i])
326  continue;
327 
328  lex = VARDATA(dlexemes[i]);
329  lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
330  lex_pos = tsvector_bsearch(tsout, lex, lex_len);
331 
332  if (lex_pos >= 0 && (j = POSDATALEN(tsout, entry + lex_pos)) != 0)
333  {
334  WordEntryPos *p = POSDATAPTR(tsout, entry + lex_pos);
335 
336  while (j--)
337  {
338  WEP_SETWEIGHT(*p, weight);
339  p++;
340  }
341  }
342  }
343 
344  PG_FREE_IF_COPY(tsin, 0);
345  PG_FREE_IF_COPY(lexemes, 2);
346 
347  PG_RETURN_POINTER(tsout);
348 }

References ARRPTR, deconstruct_array_builtin(), elog(), ERROR, i, j, palloc(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_CHAR, PG_GETARG_TSVECTOR, PG_RETURN_POINTER, POSDATALEN, POSDATAPTR, tsvector_bsearch(), VARDATA, VARHDRSZ, VARSIZE, and WEP_SETWEIGHT.

◆ tsvector_strip()

Datum tsvector_strip ( PG_FUNCTION_ARGS  )

Definition at line 164 of file tsvector_op.c.

165 {
167  TSVector out;
168  int i,
169  len = 0;
170  WordEntry *arrin = ARRPTR(in),
171  *arrout;
172  char *cur;
173 
174  for (i = 0; i < in->size; i++)
175  len += arrin[i].len;
176 
177  len = CALCDATASIZE(in->size, len);
178  out = (TSVector) palloc0(len);
179  SET_VARSIZE(out, len);
180  out->size = in->size;
181  arrout = ARRPTR(out);
182  cur = STRPTR(out);
183  for (i = 0; i < in->size; i++)
184  {
185  memcpy(cur, STRPTR(in) + arrin[i].pos, arrin[i].len);
186  arrout[i].haspos = 0;
187  arrout[i].len = arrin[i].len;
188  arrout[i].pos = cur - STRPTR(out);
189  cur += arrout[i].len;
190  }
191 
192  PG_FREE_IF_COPY(in, 0);
193  PG_RETURN_POINTER(out);
194 }

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

◆ tsvector_to_array()

Datum tsvector_to_array ( PG_FUNCTION_ARGS  )

Definition at line 716 of file tsvector_op.c.

717 {
718  TSVector tsin = PG_GETARG_TSVECTOR(0);
719  WordEntry *arrin = ARRPTR(tsin);
720  Datum *elements;
721  int i;
722  ArrayType *array;
723 
724  elements = palloc(tsin->size * sizeof(Datum));
725 
726  for (i = 0; i < tsin->size; i++)
727  {
728  elements[i] = PointerGetDatum(cstring_to_text_with_len(STRPTR(tsin) + arrin[i].pos,
729  arrin[i].len));
730  }
731 
732  array = construct_array_builtin(elements, tsin->size, TEXTOID);
733 
734  pfree(elements);
735  PG_FREE_IF_COPY(tsin, 0);
736  PG_RETURN_POINTER(array);
737 }
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3338
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201

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

◆ tsvector_unnest()

Datum tsvector_unnest ( PG_FUNCTION_ARGS  )

Definition at line 630 of file tsvector_op.c.

631 {
632  FuncCallContext *funcctx;
633  TSVector tsin;
634 
635  if (SRF_IS_FIRSTCALL())
636  {
637  MemoryContext oldcontext;
638  TupleDesc tupdesc;
639 
640  funcctx = SRF_FIRSTCALL_INIT();
641  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
642 
643  tupdesc = CreateTemplateTupleDesc(3);
644  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "lexeme",
645  TEXTOID, -1, 0);
646  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "positions",
647  INT2ARRAYOID, -1, 0);
648  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "weights",
649  TEXTARRAYOID, -1, 0);
650  funcctx->tuple_desc = BlessTupleDesc(tupdesc);
651 
652  funcctx->user_fctx = PG_GETARG_TSVECTOR_COPY(0);
653 
654  MemoryContextSwitchTo(oldcontext);
655  }
656 
657  funcctx = SRF_PERCALL_SETUP();
658  tsin = (TSVector) funcctx->user_fctx;
659 
660  if (funcctx->call_cntr < tsin->size)
661  {
662  WordEntry *arrin = ARRPTR(tsin);
663  char *data = STRPTR(tsin);
664  HeapTuple tuple;
665  int j,
666  i = funcctx->call_cntr;
667  bool nulls[] = {false, false, false};
668  Datum values[3];
669 
670  values[0] = PointerGetDatum(cstring_to_text_with_len(data + arrin[i].pos, arrin[i].len));
671 
672  if (arrin[i].haspos)
673  {
674  WordEntryPosVector *posv;
675  Datum *positions;
676  Datum *weights;
677  char weight;
678 
679  /*
680  * Internally tsvector stores position and weight in the same
681  * uint16 (2 bits for weight, 14 for position). Here we extract
682  * that in two separate arrays.
683  */
684  posv = _POSVECPTR(tsin, arrin + i);
685  positions = palloc(posv->npos * sizeof(Datum));
686  weights = palloc(posv->npos * sizeof(Datum));
687  for (j = 0; j < posv->npos; j++)
688  {
689  positions[j] = Int16GetDatum(WEP_GETPOS(posv->pos[j]));
690  weight = 'D' - WEP_GETWEIGHT(posv->pos[j]);
692  1));
693  }
694 
695  values[1] = PointerGetDatum(construct_array_builtin(positions, posv->npos, INT2OID));
697  }
698  else
699  {
700  nulls[1] = nulls[2] = true;
701  }
702 
703  tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
704  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
705  }
706  else
707  {
708  SRF_RETURN_DONE(funcctx);
709  }
710 }
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:520
uint64 call_cntr
Definition: funcapi.h:65
#define PG_GETARG_TSVECTOR_COPY(n)
Definition: ts_type.h:136

References _POSVECPTR, ARRPTR, BlessTupleDesc(), FuncCallContext::call_cntr, construct_array_builtin(), CreateTemplateTupleDesc(), cstring_to_text_with_len(), data, heap_form_tuple(), HeapTupleGetDatum(), i, if(), Int16GetDatum(), j, len, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, WordEntryPosVector::npos, palloc(), PG_GETARG_TSVECTOR_COPY, PointerGetDatum(), WordEntryPosVector::pos, TSVectorData::size, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, STRPTR, FuncCallContext::tuple_desc, TupleDescInitEntry(), FuncCallContext::user_fctx, values, weights, WEP_GETPOS, and WEP_GETWEIGHT.

◆ tsvector_update_trigger()

static Datum tsvector_update_trigger ( PG_FUNCTION_ARGS  ,
bool  config_column 
)
static

Definition at line 2569 of file tsvector_op.c.

2570 {
2571  TriggerData *trigdata;
2572  Trigger *trigger;
2573  Relation rel;
2574  HeapTuple rettuple = NULL;
2575  int tsvector_attr_num,
2576  i;
2577  ParsedText prs;
2578  Datum datum;
2579  bool isnull;
2580  text *txt;
2581  Oid cfgId;
2582  bool update_needed;
2583 
2584  /* Check call context */
2585  if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
2586  elog(ERROR, "tsvector_update_trigger: not fired by trigger manager");
2587 
2588  trigdata = (TriggerData *) fcinfo->context;
2589  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
2590  elog(ERROR, "tsvector_update_trigger: must be fired for row");
2591  if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
2592  elog(ERROR, "tsvector_update_trigger: must be fired BEFORE event");
2593 
2594  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
2595  {
2596  rettuple = trigdata->tg_trigtuple;
2597  update_needed = true;
2598  }
2599  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
2600  {
2601  rettuple = trigdata->tg_newtuple;
2602  update_needed = false; /* computed below */
2603  }
2604  else
2605  elog(ERROR, "tsvector_update_trigger: must be fired for INSERT or UPDATE");
2606 
2607  trigger = trigdata->tg_trigger;
2608  rel = trigdata->tg_relation;
2609 
2610  if (trigger->tgnargs < 3)
2611  elog(ERROR, "tsvector_update_trigger: arguments must be tsvector_field, ts_config, text_field1, ...)");
2612 
2613  /* Find the target tsvector column */
2614  tsvector_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
2615  if (tsvector_attr_num == SPI_ERROR_NOATTRIBUTE)
2616  ereport(ERROR,
2617  (errcode(ERRCODE_UNDEFINED_COLUMN),
2618  errmsg("tsvector column \"%s\" does not exist",
2619  trigger->tgargs[0])));
2620  /* This will effectively reject system columns, so no separate test: */
2621  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, tsvector_attr_num),
2622  TSVECTOROID))
2623  ereport(ERROR,
2624  (errcode(ERRCODE_DATATYPE_MISMATCH),
2625  errmsg("column \"%s\" is not of tsvector type",
2626  trigger->tgargs[0])));
2627 
2628  /* Find the configuration to use */
2629  if (config_column)
2630  {
2631  int config_attr_num;
2632 
2633  config_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[1]);
2634  if (config_attr_num == SPI_ERROR_NOATTRIBUTE)
2635  ereport(ERROR,
2636  (errcode(ERRCODE_UNDEFINED_COLUMN),
2637  errmsg("configuration column \"%s\" does not exist",
2638  trigger->tgargs[1])));
2639  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, config_attr_num),
2640  REGCONFIGOID))
2641  ereport(ERROR,
2642  (errcode(ERRCODE_DATATYPE_MISMATCH),
2643  errmsg("column \"%s\" is not of regconfig type",
2644  trigger->tgargs[1])));
2645 
2646  datum = SPI_getbinval(rettuple, rel->rd_att, config_attr_num, &isnull);
2647  if (isnull)
2648  ereport(ERROR,
2649  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2650  errmsg("configuration column \"%s\" must not be null",
2651  trigger->tgargs[1])));
2652  cfgId = DatumGetObjectId(datum);
2653  }
2654  else
2655  {
2656  List *names;
2657 
2658  names = stringToQualifiedNameList(trigger->tgargs[1]);
2659  /* require a schema so that results are not search path dependent */
2660  if (list_length(names) < 2)
2661  ereport(ERROR,
2662  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2663  errmsg("text search configuration name \"%s\" must be schema-qualified",
2664  trigger->tgargs[1])));
2665  cfgId = get_ts_config_oid(names, false);
2666  }
2667 
2668  /* initialize parse state */
2669  prs.lenwords = 32;
2670  prs.curwords = 0;
2671  prs.pos = 0;
2672  prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
2673 
2674  /* find all words in indexable column(s) */
2675  for (i = 2; i < trigger->tgnargs; i++)
2676  {
2677  int numattr;
2678 
2679  numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
2681  ereport(ERROR,
2682  (errcode(ERRCODE_UNDEFINED_COLUMN),
2683  errmsg("column \"%s\" does not exist",
2684  trigger->tgargs[i])));
2685  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, numattr), TEXTOID))
2686  ereport(ERROR,
2687  (errcode(ERRCODE_DATATYPE_MISMATCH),
2688  errmsg("column \"%s\" is not of a character type",
2689  trigger->tgargs[i])));
2690 
2692  update_needed = true;
2693 
2694  datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
2695  if (isnull)
2696  continue;
2697 
2698  txt = DatumGetTextPP(datum);
2699 
2700  parsetext(cfgId, &prs, VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
2701 
2702  if (txt != (text *) DatumGetPointer(datum))
2703  pfree(txt);
2704  }
2705 
2706  if (update_needed)
2707  {
2708  /* make tsvector value */
2709  datum = TSVectorGetDatum(make_tsvector(&prs));
2710  isnull = false;
2711 
2712  /* and insert it into tuple */
2713  rettuple = heap_modify_tuple_by_cols(rettuple, rel->rd_att,
2714  1, &tsvector_attr_num,
2715  &datum, &isnull);
2716 
2717  pfree(DatumGetPointer(datum));
2718  }
2719 
2720  return PointerGetDatum(rettuple);
2721 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:428
int numattr
Definition: bootstrap.c:66
#define DatumGetTextPP(X)
Definition: fmgr.h:292
HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple, TupleDesc tupleDesc, int nCols, int *replCols, Datum *replValues, bool *replIsnull)
Definition: heaptuple.c:1181
Oid get_ts_config_oid(List *names, bool missing_ok)
Definition: namespace.c:2722
static int list_length(const List *l)
Definition: pg_list.h:150
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:590
unsigned int Oid
Definition: postgres_ext.h:31
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1877
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1173
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
Definition: pg_list.h:52
int32 pos
Definition: ts_utils.h:103
int32 lenwords
Definition: ts_utils.h:101
int32 curwords
Definition: ts_utils.h:102
ParsedWord * words
Definition: ts_utils.h:100
TupleDesc rd_att
Definition: rel.h:111
Relation tg_relation
Definition: trigger.h:35
const Bitmapset * tg_updatedcols
Definition: trigger.h:43
TriggerEvent tg_event
Definition: trigger.h:34
HeapTuple tg_newtuple
Definition: trigger.h:37
Trigger * tg_trigger
Definition: trigger.h:38
HeapTuple tg_trigtuple
Definition: trigger.h:36
int16 tgnargs
Definition: reltrigger.h:38
char ** tgargs
Definition: reltrigger.h:41
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
TSVector make_tsvector(ParsedText *prs)
Definition: to_tsany.c:166
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:128
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:122
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:110
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
Definition: ts_parse.c:354

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

Referenced by tsvector_update_trigger_bycolumn(), and tsvector_update_trigger_byid().

◆ tsvector_update_trigger_bycolumn()

Datum tsvector_update_trigger_bycolumn ( PG_FUNCTION_ARGS  )

Definition at line 2563 of file tsvector_op.c.

2564 {
2565  return tsvector_update_trigger(fcinfo, true);
2566 }
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
Definition: tsvector_op.c:2569

References tsvector_update_trigger().

◆ tsvector_update_trigger_byid()

Datum tsvector_update_trigger_byid ( PG_FUNCTION_ARGS  )

Definition at line 2557 of file tsvector_op.c.

2558 {
2559  return tsvector_update_trigger(fcinfo, false);
2560 }

References tsvector_update_trigger().

◆ TSVECTORCMPFUNC() [1/7]

TSVECTORCMPFUNC ( cmp  ,
,
INT32   
)

◆ TSVECTORCMPFUNC() [2/7]

TSVECTORCMPFUNC ( eq  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [3/7]

TSVECTORCMPFUNC ( ge  ,
>=  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [4/7]

TSVECTORCMPFUNC ( gt  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [5/7]

TSVECTORCMPFUNC ( le  ,
<=  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [6/7]

TSVECTORCMPFUNC ( lt  )

◆ TSVECTORCMPFUNC() [7/7]

TSVECTORCMPFUNC ( ne  ,
,
BOOL   
)

◆ walkStatEntryTree()

static StatEntry* walkStatEntryTree ( TSVectorStat stat)
static

Definition at line 2319 of file tsvector_op.c.

2320 {
2321  StatEntry *node = stat->stack[stat->stackpos];
2322 
2323  if (node == NULL)
2324  return NULL;
2325 
2326  if (node->ndoc != 0)
2327  {
2328  /* return entry itself: we already was at left sublink */
2329  return node;
2330  }
2331  else if (node->right && node->right != stat->stack[stat->stackpos + 1])
2332  {
2333  /* go on right sublink */
2334  stat->stackpos++;
2335  node = node->right;
2336 
2337  /* find most-left value */
2338  for (;;)
2339  {
2340  stat->stack[stat->stackpos] = node;
2341  if (node->left)
2342  {
2343  stat->stackpos++;
2344  node = node->left;
2345  }
2346  else
2347  break;
2348  }
2349  Assert(stat->stackpos <= stat->maxdepth);
2350  }
2351  else
2352  {
2353  /* we already return all left subtree, itself and right subtree */
2354  if (stat->stackpos == 0)
2355  return NULL;
2356 
2357  stat->stackpos--;
2358  return walkStatEntryTree(stat);
2359  }
2360 
2361  return node;
2362 }

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

Referenced by ts_process_call().