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 "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "parser/parse_coerce.h"
#include "tsearch/ts_utils.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/regproc.h"
#include "utils/rel.h"
Include dependency graph for tsvector_op.c:

Go to the source code of this file.

Data Structures

struct  CHKVAL
 
struct  StatEntry
 
struct  TSVectorStat
 

Macros

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

Typedefs

typedef struct StatEntry StatEntry
 

Functions

static TSTernaryValue TS_execute_recurse (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
 
static bool TS_execute_locations_recurse (QueryItem *curitem, void *arg, TSExecuteCallback chkcond, List **locations)
 
static int tsvector_bsearch (const 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)
 
ListTS_execute_locations (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
 
bool tsquery_requires_match (QueryItem *curitem)
 
Datum ts_match_qv (PG_FUNCTION_ARGS)
 
Datum ts_match_vq (PG_FUNCTION_ARGS)
 
Datum ts_match_tt (PG_FUNCTION_ARGS)
 
Datum ts_match_tq (PG_FUNCTION_ARGS)
 
static int check_weight (TSVector txt, WordEntry *wptr, int8 weight)
 
static void insertStatEntry (MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 off)
 
static void chooseNextStatEntry (MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 low, uint32 high, uint32 offset)
 
static TSVectorStatts_accum (MemoryContext persistentContext, TSVectorStat *stat, Datum data)
 
static void ts_setup_firstcall (FunctionCallInfo fcinfo, FuncCallContext *funcctx, TSVectorStat *stat)
 
static StatEntrywalkStatEntryTree (TSVectorStat *stat)
 
static Datum ts_process_call (FuncCallContext *funcctx)
 
static TSVectorStatts_stat_sql (MemoryContext persistentContext, text *txt, text *ws)
 
Datum ts_stat1 (PG_FUNCTION_ARGS)
 
Datum ts_stat2 (PG_FUNCTION_ARGS)
 
Datum tsvector_update_trigger_byid (PG_FUNCTION_ARGS)
 
Datum tsvector_update_trigger_bycolumn (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ compareEntry

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

Definition at line 354 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 2310 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 1465 of file tsvector_op.c.

◆ TSPO_L_ONLY

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

Definition at line 1463 of file tsvector_op.c.

◆ TSPO_R_ONLY

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

Definition at line 1464 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:86
const char * type

Definition at line 145 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 364 of file tsvector_op.c.

367 {
368  uint16 *clen = &_POSVECPTR(dest, destptr)->npos;
369  int i;
370  uint16 slen = POSDATALEN(src, srcptr),
371  startlen;
372  WordEntryPos *spos = POSDATAPTR(src, srcptr),
373  *dpos = POSDATAPTR(dest, destptr);
374 
375  if (!destptr->haspos)
376  *clen = 0;
377 
378  startlen = *clen;
379  for (i = 0;
380  i < slen && *clen < MAXNUMPOS &&
381  (*clen == 0 || WEP_GETPOS(dpos[*clen - 1]) != MAXENTRYPOS - 1);
382  i++)
383  {
384  WEP_SETWEIGHT(dpos[*clen], WEP_GETWEIGHT(spos[i]));
385  WEP_SETPOS(dpos[*clen], LIMITPOS(WEP_GETPOS(spos[i]) + maxpos));
386  (*clen)++;
387  }
388 
389  if (*clen != startlen)
390  destptr->haspos = 1;
391  return *clen - startlen;
392 }
uint16_t uint16
Definition: c.h:484
int i
Definition: isn.c:72
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 747 of file tsvector_op.c.

748 {
750  TSVector tsout;
751  Datum *dlexemes;
752  WordEntry *arrout;
753  bool *nulls;
754  int nitems,
755  i,
756  tslen,
757  datalen = 0;
758  char *cur;
759 
760  deconstruct_array_builtin(v, TEXTOID, &dlexemes, &nulls, &nitems);
761 
762  /*
763  * Reject nulls and zero length strings (maybe we should just ignore them,
764  * instead?)
765  */
766  for (i = 0; i < nitems; i++)
767  {
768  if (nulls[i])
769  ereport(ERROR,
770  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
771  errmsg("lexeme array may not contain nulls")));
772 
773  if (VARSIZE(dlexemes[i]) - VARHDRSZ == 0)
774  ereport(ERROR,
775  (errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
776  errmsg("lexeme array may not contain empty strings")));
777  }
778 
779  /* Sort and de-dup, because this is required for a valid tsvector. */
780  if (nitems > 1)
781  {
782  qsort(dlexemes, nitems, sizeof(Datum), compare_text_lexemes);
783  nitems = qunique(dlexemes, nitems, sizeof(Datum),
785  }
786 
787  /* Calculate space needed for surviving lexemes. */
788  for (i = 0; i < nitems; i++)
789  datalen += VARSIZE(dlexemes[i]) - VARHDRSZ;
790  tslen = CALCDATASIZE(nitems, datalen);
791 
792  /* Allocate and fill tsvector. */
793  tsout = (TSVector) palloc0(tslen);
794  SET_VARSIZE(tsout, tslen);
795  tsout->size = nitems;
796 
797  arrout = ARRPTR(tsout);
798  cur = STRPTR(tsout);
799  for (i = 0; i < nitems; i++)
800  {
801  char *lex = VARDATA(dlexemes[i]);
802  int lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
803 
804  memcpy(cur, lex, lex_len);
805  arrout[i].haspos = 0;
806  arrout[i].len = lex_len;
807  arrout[i].pos = cur - STRPTR(tsout);
808  cur += lex_len;
809  }
810 
811  PG_FREE_IF_COPY(v, 0);
812  PG_RETURN_POINTER(tsout);
813 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3697
#define VARHDRSZ
Definition: c.h:646
#define ARRPTR(x)
Definition: cube.c:25
struct cursor * cur
Definition: ecpg.c:29
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#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
#define nitems(x)
Definition: indent.h:31
void * palloc0(Size size)
Definition: mcxt.c:1347
#define qsort(a, b, c, d)
Definition: port.h:447
uintptr_t Datum
Definition: postgres.h:64
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:442
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
#define VARSIZE(PTR)
Definition: varatt.h:279

References ARRPTR, CALCDATASIZE, compare_text_lexemes(), cur, deconstruct_array_builtin(), ereport, errcode(), errmsg(), ERROR, WordEntry::haspos, i, WordEntry::len, nitems, 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 2295 of file tsvector_op.c.

2296 {
2297  int len = POSDATALEN(txt, wptr);
2298  int num = 0;
2299  WordEntryPos *ptr = POSDATAPTR(txt, wptr);
2300 
2301  while (len--)
2302  {
2303  if (weight & (1 << WEP_GETWEIGHT(*ptr)))
2304  num++;
2305  ptr++;
2306  }
2307  return num;
2308 }

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 1189 of file tsvector_op.c.

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

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 1295 of file tsvector_op.c.

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

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 2381 of file tsvector_op.c.

2383 {
2384  uint32 pos;
2385  uint32 middle = (low + high) >> 1;
2386 
2387  pos = (low + middle) >> 1;
2388  if (low != middle && pos >= offset && pos - offset < txt->size)
2389  insertStatEntry(persistentContext, stat, txt, pos - offset);
2390  pos = (high + middle + 1) >> 1;
2391  if (middle + 1 != high && pos >= offset && pos - offset < txt->size)
2392  insertStatEntry(persistentContext, stat, txt, pos - offset);
2393 
2394  if (low != middle)
2395  chooseNextStatEntry(persistentContext, stat, txt, low, middle, offset);
2396  if (high != middle + 1)
2397  chooseNextStatEntry(persistentContext, stat, txt, middle + 1, high, offset);
2398 }
uint32_t uint32
Definition: c.h:485
static pg_noinline void Size size
Definition: slab.c:607
static void chooseNextStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 low, uint32 high, uint32 offset)
Definition: tsvector_op.c:2381
static void insertStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 off)
Definition: tsvector_op.c:2316

References insertStatEntry(), and size.

Referenced by ts_accum().

◆ compare_int()

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

Definition at line 433 of file tsvector_op.c.

434 {
435  int a = *((const int *) va);
436  int b = *((const int *) vb);
437 
438  return pg_cmp_s32(a, b);
439 }
static int pg_cmp_s32(int32 a, int32 b)
Definition: int.h:598

References a, b, and pg_cmp_s32().

Referenced by tsvector_delete_by_indices().

◆ compare_text_lexemes()

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

Definition at line 442 of file tsvector_op.c.

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

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 2316 of file tsvector_op.c.

2317 {
2318  WordEntry *we = ARRPTR(txt) + off;
2319  StatEntry *node = stat->root,
2320  *pnode = NULL;
2321  int n,
2322  res = 0;
2323  uint32 depth = 1;
2324 
2325  if (stat->weight == 0)
2326  n = (we->haspos) ? POSDATALEN(txt, we) : 1;
2327  else
2328  n = (we->haspos) ? check_weight(txt, we, stat->weight) : 0;
2329 
2330  if (n == 0)
2331  return; /* nothing to insert */
2332 
2333  while (node)
2334  {
2335  res = compareStatWord(node, we, txt);
2336 
2337  if (res == 0)
2338  {
2339  break;
2340  }
2341  else
2342  {
2343  pnode = node;
2344  node = (res < 0) ? node->left : node->right;
2345  }
2346  depth++;
2347  }
2348 
2349  if (depth > stat->maxdepth)
2350  stat->maxdepth = depth;
2351 
2352  if (node == NULL)
2353  {
2354  node = MemoryContextAlloc(persistentContext, STATENTRYHDRSZ + we->len);
2355  node->left = node->right = NULL;
2356  node->ndoc = 1;
2357  node->nentry = n;
2358  node->lenlexeme = we->len;
2359  memcpy(node->lexeme, STRPTR(txt) + we->pos, node->lenlexeme);
2360 
2361  if (pnode == NULL)
2362  {
2363  stat->root = node;
2364  }
2365  else
2366  {
2367  if (res < 0)
2368  pnode->left = node;
2369  else
2370  pnode->right = node;
2371  }
2372  }
2373  else
2374  {
2375  node->ndoc++;
2376  node->nentry += n;
2377  }
2378 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
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:2295
#define compareStatWord(a, e, t)
Definition: tsvector_op.c:2310

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 86 of file tsvector_op.c.

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

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 2413 of file tsvector_op.c.

2414 {
2416  uint32 i,
2417  nbit = 0,
2418  offset;
2419 
2420  if (stat == NULL)
2421  { /* Init in first */
2422  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2423  stat->maxdepth = 1;
2424  }
2425 
2426  /* simple check of correctness */
2427  if (txt == NULL || txt->size == 0)
2428  {
2429  if (txt && txt != (TSVector) DatumGetPointer(data))
2430  pfree(txt);
2431  return stat;
2432  }
2433 
2434  i = txt->size - 1;
2435  for (; i > 0; i >>= 1)
2436  nbit++;
2437 
2438  nbit = 1 << nbit;
2439  offset = (nbit - txt->size) / 2;
2440 
2441  insertStatEntry(persistentContext, stat, txt, (nbit >> 1) - offset);
2442  chooseNextStatEntry(persistentContext, stat, txt, 0, nbit, offset);
2443 
2444  return stat;
2445 }
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1215
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static TSVector DatumGetTSVector(Datum X)
Definition: ts_type.h:118
#define stat
Definition: win32_port.h:284

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 1854 of file tsvector_op.c.

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

References arg, TS_execute_recurse(), and TS_NO.

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

◆ TS_execute_locations()

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

Definition at line 2007 of file tsvector_op.c.

2010 {
2011  List *result;
2012 
2013  /* No flags supported, as yet */
2014  Assert(flags == TS_EXEC_EMPTY);
2015  if (TS_execute_locations_recurse(curitem, arg, chkcond, &result))
2016  return result;
2017  return NIL;
2018 }
#define NIL
Definition: pg_list.h:68
Definition: pg_list.h:54
#define TS_EXEC_EMPTY
Definition: ts_utils.h:188
static bool TS_execute_locations_recurse(QueryItem *curitem, void *arg, TSExecuteCallback chkcond, List **locations)
Definition: tsvector_op.c:2025

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

Referenced by prsd_headline().

◆ TS_execute_locations_recurse()

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

Definition at line 2025 of file tsvector_op.c.

2028 {
2029  bool lmatch,
2030  rmatch;
2031  List *llocations,
2032  *rlocations;
2034 
2035  /* since this function recurses, it could be driven to stack overflow */
2037 
2038  /* ... and let's check for query cancel while we're at it */
2040 
2041  /* Default locations result is empty */
2042  *locations = NIL;
2043 
2044  if (curitem->type == QI_VAL)
2045  {
2047  if (chkcond(arg, (QueryOperand *) curitem, data) == TS_YES)
2048  {
2049  *locations = list_make1(data);
2050  return true;
2051  }
2052  pfree(data);
2053  return false;
2054  }
2055 
2056  switch (curitem->qoperator.oper)
2057  {
2058  case OP_NOT:
2059  if (!TS_execute_locations_recurse(curitem + 1, arg, chkcond,
2060  &llocations))
2061  return true; /* we don't pass back any locations */
2062  return false;
2063 
2064  case OP_AND:
2065  if (!TS_execute_locations_recurse(curitem + curitem->qoperator.left,
2066  arg, chkcond,
2067  &llocations))
2068  return false;
2069  if (!TS_execute_locations_recurse(curitem + 1,
2070  arg, chkcond,
2071  &rlocations))
2072  return false;
2073  *locations = list_concat(llocations, rlocations);
2074  return true;
2075 
2076  case OP_OR:
2077  lmatch = TS_execute_locations_recurse(curitem + curitem->qoperator.left,
2078  arg, chkcond,
2079  &llocations);
2080  rmatch = TS_execute_locations_recurse(curitem + 1,
2081  arg, chkcond,
2082  &rlocations);
2083  if (lmatch || rmatch)
2084  {
2085  /*
2086  * We generate an AND'able location struct from each
2087  * combination of sub-matches, following the disjunctive law
2088  * (A & B) | (C & D) = (A | C) & (A | D) & (B | C) & (B | D).
2089  *
2090  * However, if either input didn't produce locations (i.e., it
2091  * failed or was a NOT), we must just return the other list.
2092  */
2093  if (llocations == NIL)
2094  *locations = rlocations;
2095  else if (rlocations == NIL)
2096  *locations = llocations;
2097  else
2098  {
2099  ListCell *ll;
2100 
2101  foreach(ll, llocations)
2102  {
2103  ExecPhraseData *ldata = (ExecPhraseData *) lfirst(ll);
2104  ListCell *lr;
2105 
2106  foreach(lr, rlocations)
2107  {
2108  ExecPhraseData *rdata = (ExecPhraseData *) lfirst(lr);
2109 
2111  (void) TS_phrase_output(data, ldata, rdata,
2113  0, 0,
2114  ldata->npos + rdata->npos);
2115  /* Report the larger width, as explained above. */
2116  data->width = Max(ldata->width, rdata->width);
2117  *locations = lappend(*locations, data);
2118  }
2119  }
2120  }
2121 
2122  return true;
2123  }
2124  return false;
2125 
2126  case OP_PHRASE:
2127  /* We can hand this off to TS_phrase_execute */
2129  if (TS_phrase_execute(curitem, arg, TS_EXEC_EMPTY, chkcond,
2130  data) == TS_YES)
2131  {
2132  if (!data->negate)
2133  *locations = list_make1(data);
2134  return true;
2135  }
2136  pfree(data);
2137  return false;
2138 
2139  default:
2140  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
2141  }
2142 
2143  /* not reachable, but keep compiler quiet */
2144  return false;
2145 }
#define Max(x, y)
Definition: c.h:952
#define elog(elevel,...)
Definition: elog.h:225
#define palloc0_object(type)
Definition: fe_memutils.h:75
List * lappend(List *list, void *datum)
Definition: list.c:339
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define lfirst(lc)
Definition: pg_list.h:172
#define list_make1(x1)
Definition: pg_list.h:212
void check_stack_depth(void)
Definition: postgres.c:3574
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 TSPO_BOTH
Definition: tsvector_op.c:1465
#define TSPO_R_ONLY
Definition: tsvector_op.c:1464
static TSTernaryValue TS_phrase_output(ExecPhraseData *data, ExecPhraseData *Ldata, ExecPhraseData *Rdata, int emit, int Loffset, int Roffset, int max_npos)
Definition: tsvector_op.c:1468
#define TSPO_L_ONLY
Definition: tsvector_op.c:1463
static TSTernaryValue TS_phrase_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond, ExecPhraseData *data)
Definition: tsvector_op.c:1609
QueryOperator qoperator
Definition: ts_type.h:209
QueryItemType type
Definition: ts_type.h:208

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

Referenced by TS_execute_locations().

◆ TS_execute_recurse()

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

Definition at line 1883 of file tsvector_op.c.

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

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 1871 of file tsvector_op.c.

1873 {
1874  return TS_execute_recurse(curitem, arg, flags, chkcond);
1875 }

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 2206 of file tsvector_op.c.

2207 {
2209  PG_GETARG_DATUM(1),
2210  PG_GETARG_DATUM(0)));
2211 }
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#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:2214

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 2266 of file tsvector_op.c.

2267 {
2268  TSVector vector;
2269  TSQuery query = PG_GETARG_TSQUERY(1);
2270  bool res;
2271 
2273  PG_GETARG_DATUM(0)));
2274 
2276  TSVectorGetDatum(vector),
2277  TSQueryGetDatum(query)));
2278 
2279  pfree(vector);
2280  PG_FREE_IF_COPY(query, 1);
2281 
2283 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
Datum to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:270
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 2244 of file tsvector_op.c.

2245 {
2246  TSVector vector;
2247  TSQuery query;
2248  bool res;
2249 
2251  PG_GETARG_DATUM(0)));
2253  PG_GETARG_DATUM(1)));
2254 
2256  TSVectorGetDatum(vector),
2257  TSQueryGetDatum(query)));
2258 
2259  pfree(vector);
2260  pfree(query);
2261 
2263 }
Datum plainto_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:642
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 2214 of file tsvector_op.c.

2215 {
2217  TSQuery query = PG_GETARG_TSQUERY(1);
2218  CHKVAL chkval;
2219  bool result;
2220 
2221  /* empty query matches nothing */
2222  if (!query->size)
2223  {
2224  PG_FREE_IF_COPY(val, 0);
2225  PG_FREE_IF_COPY(query, 1);
2226  PG_RETURN_BOOL(false);
2227  }
2228 
2229  chkval.arrb = ARRPTR(val);
2230  chkval.arre = chkval.arrb + val->size;
2231  chkval.values = STRPTR(val);
2232  chkval.operand = GETOPERAND(query);
2233  result = TS_execute(GETQUERY(query),
2234  &chkval,
2235  TS_EXEC_EMPTY,
2237 
2238  PG_FREE_IF_COPY(val, 0);
2239  PG_FREE_IF_COPY(query, 1);
2240  PG_RETURN_BOOL(result);
2241 }
#define GETQUERY(x)
Definition: _int.h:157
#define GETOPERAND(x)
Definition: ltree.h:165
int32 size
Definition: ts_type.h:221
static TSTernaryValue checkcondition_str(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsvector_op.c:1295
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1854

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 1609 of file tsvector_op.c.

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

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_locations_recurse(), and 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 1468 of file tsvector_op.c.

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

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

Referenced by TS_execute_locations_recurse(), and TS_phrase_execute().

◆ ts_process_call()

static Datum ts_process_call ( FuncCallContext funcctx)
static

Definition at line 2535 of file tsvector_op.c.

2536 {
2537  TSVectorStat *st;
2538  StatEntry *entry;
2539 
2540  st = (TSVectorStat *) funcctx->user_fctx;
2541 
2542  entry = walkStatEntryTree(st);
2543 
2544  if (entry != NULL)
2545  {
2546  Datum result;
2547  char *values[3];
2548  char ndoc[16];
2549  char nentry[16];
2550  HeapTuple tuple;
2551 
2552  values[0] = palloc(entry->lenlexeme + 1);
2553  memcpy(values[0], entry->lexeme, entry->lenlexeme);
2554  (values[0])[entry->lenlexeme] = '\0';
2555  sprintf(ndoc, "%d", entry->ndoc);
2556  values[1] = ndoc;
2557  sprintf(nentry, "%d", entry->nentry);
2558  values[2] = nentry;
2559 
2560  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
2561  result = HeapTupleGetDatum(tuple);
2562 
2563  pfree(values[0]);
2564 
2565  /* mark entry as already visited */
2566  entry->ndoc = 0;
2567 
2568  return result;
2569  }
2570 
2571  return (Datum) 0;
2572 }
static Datum values[MAXATTR]
Definition: bootstrap.c:151
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2222
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:2489

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 2448 of file tsvector_op.c.

2450 {
2451  TupleDesc tupdesc;
2452  MemoryContext oldcontext;
2453  StatEntry *node;
2454 
2455  funcctx->user_fctx = stat;
2456 
2457  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2458 
2459  stat->stack = palloc0(sizeof(StatEntry *) * (stat->maxdepth + 1));
2460  stat->stackpos = 0;
2461 
2462  node = stat->root;
2463  /* find leftmost value */
2464  if (node == NULL)
2465  stat->stack[stat->stackpos] = NULL;
2466  else
2467  for (;;)
2468  {
2469  stat->stack[stat->stackpos] = node;
2470  if (node->left)
2471  {
2472  stat->stackpos++;
2473  node = node->left;
2474  }
2475  else
2476  break;
2477  }
2478  Assert(stat->stackpos <= stat->maxdepth);
2479 
2480  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
2481  elog(ERROR, "return type must be a row type");
2482  funcctx->tuple_desc = tupdesc;
2483  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
2484 
2485  MemoryContextSwitchTo(oldcontext);
2486 }
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2173
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
MemoryContextSwitchTo(old_ctx)
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
TupleDesc tuple_desc
Definition: funcapi.h:112

References Assert, FuncCallContext::attinmeta, elog, ERROR, get_call_result_type(), StatEntry::left, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc0(), stat, FuncCallContext::tuple_desc, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, and FuncCallContext::user_fctx.

Referenced by ts_stat1(), and ts_stat2().

◆ ts_stat1()

Datum ts_stat1 ( PG_FUNCTION_ARGS  )

Definition at line 2664 of file tsvector_op.c.

2665 {
2666  FuncCallContext *funcctx;
2667  Datum result;
2668 
2669  if (SRF_IS_FIRSTCALL())
2670  {
2671  TSVectorStat *stat;
2672  text *txt = PG_GETARG_TEXT_PP(0);
2673 
2674  funcctx = SRF_FIRSTCALL_INIT();
2675  SPI_connect();
2676  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, NULL);
2677  PG_FREE_IF_COPY(txt, 0);
2678  ts_setup_firstcall(fcinfo, funcctx, stat);
2679  SPI_finish();
2680  }
2681 
2682  funcctx = SRF_PERCALL_SETUP();
2683  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2684  SRF_RETURN_NEXT(funcctx, result);
2685  SRF_RETURN_DONE(funcctx);
2686 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
int SPI_connect(void)
Definition: spi.c:94
int SPI_finish(void)
Definition: spi.c:182
Definition: c.h:641
static Datum ts_process_call(FuncCallContext *funcctx)
Definition: tsvector_op.c:2535
static void ts_setup_firstcall(FunctionCallInfo fcinfo, FuncCallContext *funcctx, TSVectorStat *stat)
Definition: tsvector_op.c:2448
static TSVectorStat * ts_stat_sql(MemoryContext persistentContext, text *txt, text *ws)
Definition: tsvector_op.c:2575

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 2689 of file tsvector_op.c.

2690 {
2691  FuncCallContext *funcctx;
2692  Datum result;
2693 
2694  if (SRF_IS_FIRSTCALL())
2695  {
2696  TSVectorStat *stat;
2697  text *txt = PG_GETARG_TEXT_PP(0);
2698  text *ws = PG_GETARG_TEXT_PP(1);
2699 
2700  funcctx = SRF_FIRSTCALL_INIT();
2701  SPI_connect();
2702  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, ws);
2703  PG_FREE_IF_COPY(txt, 0);
2704  PG_FREE_IF_COPY(ws, 1);
2705  ts_setup_firstcall(fcinfo, funcctx, stat);
2706  SPI_finish();
2707  }
2708 
2709  funcctx = SRF_PERCALL_SETUP();
2710  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2711  SRF_RETURN_NEXT(funcctx, result);
2712  SRF_RETURN_DONE(funcctx);
2713 }

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 2575 of file tsvector_op.c.

2576 {
2577  char *query = text_to_cstring(txt);
2578  TSVectorStat *stat;
2579  bool isnull;
2580  Portal portal;
2581  SPIPlanPtr plan;
2582 
2583  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2584  /* internal error */
2585  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2586 
2587  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2588  /* internal error */
2589  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2590 
2591  SPI_cursor_fetch(portal, true, 100);
2592 
2593  if (SPI_tuptable == NULL ||
2594  SPI_tuptable->tupdesc->natts != 1 ||
2596  TSVECTOROID))
2597  ereport(ERROR,
2598  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2599  errmsg("ts_stat query must return one tsvector column")));
2600 
2601  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2602  stat->maxdepth = 1;
2603 
2604  if (ws)
2605  {
2606  char *buf;
2607 
2608  buf = VARDATA_ANY(ws);
2609  while (buf - VARDATA_ANY(ws) < VARSIZE_ANY_EXHDR(ws))
2610  {
2611  if (pg_mblen(buf) == 1)
2612  {
2613  switch (*buf)
2614  {
2615  case 'A':
2616  case 'a':
2617  stat->weight |= 1 << 3;
2618  break;
2619  case 'B':
2620  case 'b':
2621  stat->weight |= 1 << 2;
2622  break;
2623  case 'C':
2624  case 'c':
2625  stat->weight |= 1 << 1;
2626  break;
2627  case 'D':
2628  case 'd':
2629  stat->weight |= 1;
2630  break;
2631  default:
2632  stat->weight |= 0;
2633  }
2634  }
2635  buf += pg_mblen(buf);
2636  }
2637  }
2638 
2639  while (SPI_processed > 0)
2640  {
2641  uint64 i;
2642 
2643  for (i = 0; i < SPI_processed; i++)
2644  {
2646 
2647  if (!isnull)
2648  stat = ts_accum(persistentContext, stat, data);
2649  }
2650 
2652  SPI_cursor_fetch(portal, true, 100);
2653  }
2654 
2656  SPI_cursor_close(portal);
2657  SPI_freeplan(plan);
2658  pfree(query);
2659 
2660  return stat;
2661 }
uint64_t uint64
Definition: c.h:486
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1023
bool IsBinaryCoercible(Oid srctype, Oid targettype)
#define plan(x)
Definition: pg_regress.c:161
static char * buf
Definition: pg_test_fsync.c:72
uint64 SPI_processed
Definition: spi.c:44
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1308
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:1025
SPITupleTable * SPI_tuptable
Definition: spi.c:45
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1806
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1386
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1445
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:860
void SPI_cursor_close(Portal portal)
Definition: spi.c:1862
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1252
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:2413
char * text_to_cstring(const text *t)
Definition: varlena.c:217

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

Referenced by ts_stat1(), and ts_stat2().

◆ tsCompareString()

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

Definition at line 1152 of file tsvector_op.c.

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

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 2156 of file tsvector_op.c.

2157 {
2158  /* since this function recurses, it could be driven to stack overflow */
2160 
2161  if (curitem->type == QI_VAL)
2162  return true;
2163 
2164  switch (curitem->qoperator.oper)
2165  {
2166  case OP_NOT:
2167 
2168  /*
2169  * Assume there are no required matches underneath a NOT. For
2170  * some cases with nested NOTs, we could prove there's a required
2171  * match, but it seems unlikely to be worth the trouble.
2172  */
2173  return false;
2174 
2175  case OP_PHRASE:
2176 
2177  /*
2178  * Treat OP_PHRASE as OP_AND here
2179  */
2180  case OP_AND:
2181  /* If either side requires a match, we're good */
2182  if (tsquery_requires_match(curitem + curitem->qoperator.left))
2183  return true;
2184  else
2185  return tsquery_requires_match(curitem + 1);
2186 
2187  case OP_OR:
2188  /* Both sides must require a match */
2189  if (tsquery_requires_match(curitem + curitem->qoperator.left))
2190  return tsquery_requires_match(curitem + 1);
2191  else
2192  return false;
2193 
2194  default:
2195  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
2196  }
2197 
2198  /* not reachable, but keep compiler quiet */
2199  return false;
2200 }
bool tsquery_requires_match(QueryItem *curitem)
Definition: tsvector_op.c:2156

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 400 of file tsvector_op.c.

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

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 925 of file tsvector_op.c.

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

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, size, TSVectorData::size, STRPTR, VARSIZE, and WEP_GETPOS.

◆ tsvector_delete_arr()

Datum tsvector_delete_arr ( PG_FUNCTION_ARGS  )

Definition at line 578 of file tsvector_op.c.

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

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 464 of file tsvector_op.c.

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

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 554 of file tsvector_op.c.

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

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 819 of file tsvector_op.c.

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

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, and WEP_GETWEIGHT.

◆ tsvector_length()

Datum tsvector_length ( PG_FUNCTION_ARGS  )

Definition at line 201 of file tsvector_op.c.

202 {
204  int32 ret = in->size;
205 
206  PG_FREE_IF_COPY(in, 0);
207  PG_RETURN_INT32(ret);
208 }
int32_t int32
Definition: c.h:481
#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 211 of file tsvector_op.c.

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

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

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 168 of file tsvector_op.c.

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

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 720 of file tsvector_op.c.

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

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 632 of file tsvector_op.c.

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

References _POSVECPTR, ARRPTR, FuncCallContext::call_cntr, construct_array_builtin(), CreateTemplateTupleDesc(), cstring_to_text_with_len(), data, elog, ERROR, get_call_result_type(), 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(), TYPEFUNC_COMPOSITE, FuncCallContext::user_fctx, values, WEP_GETPOS, and WEP_GETWEIGHT.

◆ tsvector_update_trigger()

static Datum tsvector_update_trigger ( PG_FUNCTION_ARGS  ,
bool  config_column 
)
static

Definition at line 2739 of file tsvector_op.c.

2740 {
2741  TriggerData *trigdata;
2742  Trigger *trigger;
2743  Relation rel;
2744  HeapTuple rettuple = NULL;
2745  int tsvector_attr_num,
2746  i;
2747  ParsedText prs;
2748  Datum datum;
2749  bool isnull;
2750  text *txt;
2751  Oid cfgId;
2752  bool update_needed;
2753 
2754  /* Check call context */
2755  if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
2756  elog(ERROR, "tsvector_update_trigger: not fired by trigger manager");
2757 
2758  trigdata = (TriggerData *) fcinfo->context;
2759  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
2760  elog(ERROR, "tsvector_update_trigger: must be fired for row");
2761  if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
2762  elog(ERROR, "tsvector_update_trigger: must be fired BEFORE event");
2763 
2764  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
2765  {
2766  rettuple = trigdata->tg_trigtuple;
2767  update_needed = true;
2768  }
2769  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
2770  {
2771  rettuple = trigdata->tg_newtuple;
2772  update_needed = false; /* computed below */
2773  }
2774  else
2775  elog(ERROR, "tsvector_update_trigger: must be fired for INSERT or UPDATE");
2776 
2777  trigger = trigdata->tg_trigger;
2778  rel = trigdata->tg_relation;
2779 
2780  if (trigger->tgnargs < 3)
2781  elog(ERROR, "tsvector_update_trigger: arguments must be tsvector_field, ts_config, text_field1, ...)");
2782 
2783  /* Find the target tsvector column */
2784  tsvector_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
2785  if (tsvector_attr_num == SPI_ERROR_NOATTRIBUTE)
2786  ereport(ERROR,
2787  (errcode(ERRCODE_UNDEFINED_COLUMN),
2788  errmsg("tsvector column \"%s\" does not exist",
2789  trigger->tgargs[0])));
2790  /* This will effectively reject system columns, so no separate test: */
2791  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, tsvector_attr_num),
2792  TSVECTOROID))
2793  ereport(ERROR,
2794  (errcode(ERRCODE_DATATYPE_MISMATCH),
2795  errmsg("column \"%s\" is not of tsvector type",
2796  trigger->tgargs[0])));
2797 
2798  /* Find the configuration to use */
2799  if (config_column)
2800  {
2801  int config_attr_num;
2802 
2803  config_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[1]);
2804  if (config_attr_num == SPI_ERROR_NOATTRIBUTE)
2805  ereport(ERROR,
2806  (errcode(ERRCODE_UNDEFINED_COLUMN),
2807  errmsg("configuration column \"%s\" does not exist",
2808  trigger->tgargs[1])));
2809  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, config_attr_num),
2810  REGCONFIGOID))
2811  ereport(ERROR,
2812  (errcode(ERRCODE_DATATYPE_MISMATCH),
2813  errmsg("column \"%s\" is not of regconfig type",
2814  trigger->tgargs[1])));
2815 
2816  datum = SPI_getbinval(rettuple, rel->rd_att, config_attr_num, &isnull);
2817  if (isnull)
2818  ereport(ERROR,
2819  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2820  errmsg("configuration column \"%s\" must not be null",
2821  trigger->tgargs[1])));
2822  cfgId = DatumGetObjectId(datum);
2823  }
2824  else
2825  {
2826  List *names;
2827 
2828  names = stringToQualifiedNameList(trigger->tgargs[1], NULL);
2829  /* require a schema so that results are not search path dependent */
2830  if (list_length(names) < 2)
2831  ereport(ERROR,
2832  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2833  errmsg("text search configuration name \"%s\" must be schema-qualified",
2834  trigger->tgargs[1])));
2835  cfgId = get_ts_config_oid(names, false);
2836  }
2837 
2838  /* initialize parse state */
2839  prs.lenwords = 32;
2840  prs.curwords = 0;
2841  prs.pos = 0;
2842  prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
2843 
2844  /* find all words in indexable column(s) */
2845  for (i = 2; i < trigger->tgnargs; i++)
2846  {
2847  int numattr;
2848 
2849  numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
2851  ereport(ERROR,
2852  (errcode(ERRCODE_UNDEFINED_COLUMN),
2853  errmsg("column \"%s\" does not exist",
2854  trigger->tgargs[i])));
2855  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, numattr), TEXTOID))
2856  ereport(ERROR,
2857  (errcode(ERRCODE_DATATYPE_MISMATCH),
2858  errmsg("column \"%s\" is not of a character type",
2859  trigger->tgargs[i])));
2860 
2862  update_needed = true;
2863 
2864  datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
2865  if (isnull)
2866  continue;
2867 
2868  txt = DatumGetTextPP(datum);
2869 
2870  parsetext(cfgId, &prs, VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
2871 
2872  if (txt != (text *) DatumGetPointer(datum))
2873  pfree(txt);
2874  }
2875 
2876  if (update_needed)
2877  {
2878  /* make tsvector value */
2879  datum = TSVectorGetDatum(make_tsvector(&prs));
2880  isnull = false;
2881 
2882  /* and insert it into tuple */
2883  rettuple = heap_modify_tuple_by_cols(rettuple, rel->rd_att,
2884  1, &tsvector_attr_num,
2885  &datum, &isnull);
2886 
2887  pfree(DatumGetPointer(datum));
2888  }
2889 
2890  return PointerGetDatum(rettuple);
2891 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
int numattr
Definition: bootstrap.c:61
#define DatumGetTextPP(X)
Definition: fmgr.h:292
HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple, TupleDesc tupleDesc, int nCols, const int *replCols, const Datum *replValues, const bool *replIsnull)
Definition: heaptuple.c:1277
Oid get_ts_config_oid(List *names, bool missing_ok)
Definition: namespace.c:3152
static int list_length(const List *l)
Definition: pg_list.h:152
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:242
unsigned int Oid
Definition: postgres_ext.h:31
List * stringToQualifiedNameList(const char *string, Node *escontext)
Definition: regproc.c:1797
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1175
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
int32 pos
Definition: ts_utils.h:107
int32 lenwords
Definition: ts_utils.h:105
int32 curwords
Definition: ts_utils.h:106
ParsedWord * words
Definition: ts_utils.h:104
TupleDesc rd_att
Definition: rel.h:112
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:165
#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:355

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 2733 of file tsvector_op.c.

2734 {
2735  return tsvector_update_trigger(fcinfo, true);
2736 }
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
Definition: tsvector_op.c:2739

References tsvector_update_trigger().

◆ tsvector_update_trigger_byid()

Datum tsvector_update_trigger_byid ( PG_FUNCTION_ARGS  )

Definition at line 2727 of file tsvector_op.c.

2728 {
2729  return tsvector_update_trigger(fcinfo, false);
2730 }

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 2489 of file tsvector_op.c.

2490 {
2491  StatEntry *node = stat->stack[stat->stackpos];
2492 
2493  if (node == NULL)
2494  return NULL;
2495 
2496  if (node->ndoc != 0)
2497  {
2498  /* return entry itself: we already was at left sublink */
2499  return node;
2500  }
2501  else if (node->right && node->right != stat->stack[stat->stackpos + 1])
2502  {
2503  /* go on right sublink */
2504  stat->stackpos++;
2505  node = node->right;
2506 
2507  /* find most-left value */
2508  for (;;)
2509  {
2510  stat->stack[stat->stackpos] = node;
2511  if (node->left)
2512  {
2513  stat->stackpos++;
2514  node = node->left;
2515  }
2516  else
2517  break;
2518  }
2519  Assert(stat->stackpos <= stat->maxdepth);
2520  }
2521  else
2522  {
2523  /* we already return all left subtree, itself and right subtree */
2524  if (stat->stackpos == 0)
2525  return NULL;
2526 
2527  stat->stackpos--;
2528  return walkStatEntryTree(stat);
2529  }
2530 
2531  return node;
2532 }

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

Referenced by ts_process_call().