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

Go to the source code of this file.

Data Structures

struct  CHKVAL
 
struct  StatEntry
 
struct  TSVectorStat
 

Macros

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

Typedefs

typedef struct StatEntry StatEntry
 

Functions

static Datum tsvector_update_trigger (PG_FUNCTION_ARGS, bool config_column)
 
static int tsvector_bsearch (const TSVector tsv, char *lexeme, int lexeme_len)
 
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 bool checkclass_str (CHKVAL *chkval, WordEntry *entry, QueryOperand *val, ExecPhraseData *data)
 
static bool checkcondition_str (void *checkval, QueryOperand *val, ExecPhraseData *data)
 
static bool TS_phrase_output (ExecPhraseData *data, ExecPhraseData *Ldata, ExecPhraseData *Rdata, int emit, int Loffset, int Roffset, int max_npos)
 
static bool TS_phrase_execute (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond, ExecPhraseData *data)
 
bool TS_execute (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,
 
)
Value:
tsCompareString((pa) + (a)->pos, (a)->len, \
(pb) + (b)->pos, (b)->len, \
false)
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1142

Definition at line 347 of file tsvector_op.c.

Referenced by tsvector_concat().

◆ compareStatWord

#define compareStatWord (   a,
  e,
 
)
Value:
tsCompareString((a)->lexeme, (a)->lenlexeme, \
STRPTR(t) + (e)->pos, (e)->len, \
false)
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1142
e
Definition: preproc-init.c:82
#define STRPTR(x)
Definition: hstore.h:76

Definition at line 1970 of file tsvector_op.c.

Referenced by insertStatEntry().

◆ STATENTRYHDRSZ

#define STATENTRYHDRSZ   (offsetof(StatEntry, lexeme))

Definition at line 56 of file tsvector_op.c.

Referenced by insertStatEntry().

◆ TSPO_BOTH

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

Definition at line 1382 of file tsvector_op.c.

Referenced by TS_phrase_execute(), and TS_phrase_output().

◆ TSPO_L_ONLY

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

Definition at line 1380 of file tsvector_op.c.

Referenced by TS_phrase_execute(), and TS_phrase_output().

◆ TSPO_R_ONLY

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

Definition at line 1381 of file tsvector_op.c.

Referenced by TS_phrase_execute(), and TS_phrase_output().

◆ 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); \
int res = silly_cmp_tsvector(a, b); \
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
static int silly_cmp_tsvector(const TSVector a, const TSVector b)
Definition: tsvector_op.c:77
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188

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

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

Referenced by tsvector_concat().

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

◆ array_to_tsvector()

Datum array_to_tsvector ( PG_FUNCTION_ARGS  )

Definition at line 744 of file tsvector_op.c.

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

745 {
747  TSVector tsout;
748  Datum *dlexemes;
749  WordEntry *arrout;
750  bool *nulls;
751  int nitems,
752  i,
753  tslen,
754  datalen = 0;
755  char *cur;
756 
757  deconstruct_array(v, TEXTOID, -1, false, TYPALIGN_INT, &dlexemes, &nulls, &nitems);
758 
759  /* Reject nulls (maybe we should just ignore them, instead?) */
760  for (i = 0; i < nitems; i++)
761  {
762  if (nulls[i])
763  ereport(ERROR,
764  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
765  errmsg("lexeme array may not contain nulls")));
766  }
767 
768  /* Sort and de-dup, because this is required for a valid tsvector. */
769  if (nitems > 1)
770  {
771  qsort(dlexemes, nitems, sizeof(Datum), compare_text_lexemes);
772  nitems = qunique(dlexemes, nitems, sizeof(Datum),
774  }
775 
776  /* Calculate space needed for surviving lexemes. */
777  for (i = 0; i < nitems; i++)
778  datalen += VARSIZE(dlexemes[i]) - VARHDRSZ;
779  tslen = CALCDATASIZE(nitems, datalen);
780 
781  /* Allocate and fill tsvector. */
782  tsout = (TSVector) palloc0(tslen);
783  SET_VARSIZE(tsout, tslen);
784  tsout->size = nitems;
785 
786  arrout = ARRPTR(tsout);
787  cur = STRPTR(tsout);
788  for (i = 0; i < nitems; i++)
789  {
790  char *lex = VARDATA(dlexemes[i]);
791  int lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
792 
793  memcpy(cur, lex, lex_len);
794  arrout[i].haspos = 0;
795  arrout[i].len = lex_len;
796  arrout[i].pos = cur - STRPTR(tsout);
797  cur += lex_len;
798  }
799 
800  PG_FREE_IF_COPY(v, 0);
801  PG_RETURN_POINTER(tsout);
802 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:355
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:561
uint32 len
Definition: ts_type.h:44
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
int32 size
Definition: ts_type.h:93
uint32 haspos
Definition: ts_type.h:44
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
TSVectorData * TSVector
Definition: ts_type.h:98
#define ereport(elevel,...)
Definition: elog.h:144
static int compare_text_lexemes(const void *va, const void *vb)
Definition: tsvector_op.c:437
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
uint32 pos
Definition: ts_type.h:44
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define STRPTR(x)
Definition: hstore.h:76
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define ARRPTR(x)
Definition: cube.c:24
#define qsort(a, b, c, d)
Definition: port.h:479

◆ check_weight()

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

Definition at line 1955 of file tsvector_op.c.

References POSDATALEN, POSDATAPTR, and WEP_GETWEIGHT.

Referenced by insertStatEntry().

1956 {
1957  int len = POSDATALEN(txt, wptr);
1958  int num = 0;
1959  WordEntryPos *ptr = POSDATAPTR(txt, wptr);
1960 
1961  while (len--)
1962  {
1963  if (weight & (1 << WEP_GETWEIGHT(*ptr)))
1964  num++;
1965  ptr++;
1966  }
1967  return num;
1968 }
uint16 WordEntryPos
Definition: ts_type.h:63
#define POSDATALEN(x, e)
Definition: ts_type.h:110
#define POSDATAPTR(x, e)
Definition: ts_type.h:111
#define WEP_GETWEIGHT(x)
Definition: ts_type.h:79

◆ checkclass_str()

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

Definition at line 1179 of file tsvector_op.c.

References ExecPhraseData::allocated, WordEntry::haspos, WordEntry::len, WordEntryPosVector::npos, ExecPhraseData::npos, palloc(), WordEntry::pos, WordEntryPosVector::pos, ExecPhraseData::pos, SHORTALIGN, CHKVAL::values, QueryOperand::weight, WEP_GETPOS, and WEP_GETWEIGHT.

Referenced by checkcondition_str().

1181 {
1182  bool result = false;
1183 
1184  if (entry->haspos && (val->weight || data))
1185  {
1186  WordEntryPosVector *posvec;
1187 
1188  /*
1189  * We can't use the _POSVECPTR macro here because the pointer to the
1190  * tsvector's lexeme storage is already contained in chkval->values.
1191  */
1192  posvec = (WordEntryPosVector *)
1193  (chkval->values + SHORTALIGN(entry->pos + entry->len));
1194 
1195  if (val->weight && data)
1196  {
1197  WordEntryPos *posvec_iter = posvec->pos;
1198  WordEntryPos *dptr;
1199 
1200  /*
1201  * Filter position information by weights
1202  */
1203  dptr = data->pos = palloc(sizeof(WordEntryPos) * posvec->npos);
1204  data->allocated = true;
1205 
1206  /* Is there a position with a matching weight? */
1207  while (posvec_iter < posvec->pos + posvec->npos)
1208  {
1209  /* If true, append this position to the data->pos */
1210  if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1211  {
1212  *dptr = WEP_GETPOS(*posvec_iter);
1213  dptr++;
1214  }
1215 
1216  posvec_iter++;
1217  }
1218 
1219  data->npos = dptr - data->pos;
1220 
1221  if (data->npos > 0)
1222  result = true;
1223  }
1224  else if (val->weight)
1225  {
1226  WordEntryPos *posvec_iter = posvec->pos;
1227 
1228  /* Is there a position with a matching weight? */
1229  while (posvec_iter < posvec->pos + posvec->npos)
1230  {
1231  if (val->weight & (1 << WEP_GETWEIGHT(*posvec_iter)))
1232  {
1233  result = true;
1234  break; /* no need to go further */
1235  }
1236 
1237  posvec_iter++;
1238  }
1239  }
1240  else /* data != NULL */
1241  {
1242  data->npos = posvec->npos;
1243  data->pos = posvec->pos;
1244  data->allocated = false;
1245  result = true;
1246  }
1247  }
1248  else
1249  {
1250  result = true;
1251  }
1252 
1253  return result;
1254 }
uint16 WordEntryPos
Definition: ts_type.h:63
bool allocated
Definition: ts_utils.h:152
uint32 len
Definition: ts_type.h:44
uint8 weight
Definition: ts_type.h:146
#define WEP_GETPOS(x)
Definition: ts_type.h:80
WordEntryPos pos[FLEXIBLE_ARRAY_MEMBER]
Definition: ts_type.h:68
WordEntryPos * pos
Definition: ts_utils.h:154
uint32 haspos
Definition: ts_type.h:44
uint32 pos
Definition: ts_type.h:44
char * values
Definition: tsvector_op.c:40
void * palloc(Size size)
Definition: mcxt.c:949
#define WEP_GETWEIGHT(x)
Definition: ts_type.h:79
#define SHORTALIGN(LEN)
Definition: c.h:687

◆ checkcondition_str()

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

Definition at line 1260 of file tsvector_op.c.

References ExecPhraseData::allocated, CHKVAL::arrb, CHKVAL::arre, checkclass_str(), compareWordEntryPos(), difference(), QueryOperand::distance, WordEntry::len, QueryOperand::length, ExecPhraseData::npos, CHKVAL::operand, palloc(), WordEntry::pos, ExecPhraseData::pos, QueryOperand::prefix, qsort, qunique(), repalloc(), tsCompareString(), and CHKVAL::values.

Referenced by ts_match_vq().

1261 {
1262  CHKVAL *chkval = (CHKVAL *) checkval;
1263  WordEntry *StopLow = chkval->arrb;
1264  WordEntry *StopHigh = chkval->arre;
1265  WordEntry *StopMiddle = StopHigh;
1266  int difference = -1;
1267  bool res = false;
1268 
1269  /* Loop invariant: StopLow <= val < StopHigh */
1270  while (StopLow < StopHigh)
1271  {
1272  StopMiddle = StopLow + (StopHigh - StopLow) / 2;
1273  difference = tsCompareString(chkval->operand + val->distance,
1274  val->length,
1275  chkval->values + StopMiddle->pos,
1276  StopMiddle->len,
1277  false);
1278 
1279  if (difference == 0)
1280  {
1281  /* Check weight info & fill 'data' with positions */
1282  res = checkclass_str(chkval, StopMiddle, val, data);
1283  break;
1284  }
1285  else if (difference > 0)
1286  StopLow = StopMiddle + 1;
1287  else
1288  StopHigh = StopMiddle;
1289  }
1290 
1291  if ((!res || data) && val->prefix)
1292  {
1293  WordEntryPos *allpos = NULL;
1294  int npos = 0,
1295  totalpos = 0;
1296 
1297  /*
1298  * there was a failed exact search, so we should scan further to find
1299  * a prefix match. We also need to do so if caller needs position info
1300  */
1301  if (StopLow >= StopHigh)
1302  StopMiddle = StopHigh;
1303 
1304  while ((!res || data) && StopMiddle < chkval->arre &&
1305  tsCompareString(chkval->operand + val->distance,
1306  val->length,
1307  chkval->values + StopMiddle->pos,
1308  StopMiddle->len,
1309  true) == 0)
1310  {
1311  if (data)
1312  {
1313  /*
1314  * We need to join position information
1315  */
1316  res = checkclass_str(chkval, StopMiddle, val, data);
1317 
1318  if (res)
1319  {
1320  while (npos + data->npos >= totalpos)
1321  {
1322  if (totalpos == 0)
1323  {
1324  totalpos = 256;
1325  allpos = palloc(sizeof(WordEntryPos) * totalpos);
1326  }
1327  else
1328  {
1329  totalpos *= 2;
1330  allpos = repalloc(allpos, sizeof(WordEntryPos) * totalpos);
1331  }
1332  }
1333 
1334  memcpy(allpos + npos, data->pos, sizeof(WordEntryPos) * data->npos);
1335  npos += data->npos;
1336  }
1337  }
1338  else
1339  {
1340  res = checkclass_str(chkval, StopMiddle, val, NULL);
1341  }
1342 
1343  StopMiddle++;
1344  }
1345 
1346  if (res && data)
1347  {
1348  /* Sort and make unique array of found positions */
1349  data->pos = allpos;
1350  qsort(data->pos, npos, sizeof(WordEntryPos), compareWordEntryPos);
1351  data->npos = qunique(data->pos, npos, sizeof(WordEntryPos),
1353  data->allocated = true;
1354  }
1355  }
1356 
1357  return res;
1358 }
uint16 WordEntryPos
Definition: ts_type.h:63
bool allocated
Definition: ts_utils.h:152
uint32 len
Definition: ts_type.h:44
uint32 distance
Definition: ts_type.h:158
int compareWordEntryPos(const void *a, const void *b)
Definition: tsvector.c:33
int32 * arrb
Definition: _int_bool.c:227
char * operand
Definition: ltxtquery_op.c:52
WordEntryPos * pos
Definition: ts_utils.h:154
Datum difference(PG_FUNCTION_ARGS)
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1142
uint32 pos
Definition: ts_type.h:44
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
uint32 length
Definition: ts_type.h:158
char * values
Definition: tsvector_op.c:40
void * palloc(Size size)
Definition: mcxt.c:949
int32 * arre
Definition: _int_bool.c:228
bool prefix
Definition: ts_type.h:150
#define qsort(a, b, c, d)
Definition: port.h:479
static bool checkclass_str(CHKVAL *chkval, WordEntry *entry, QueryOperand *val, ExecPhraseData *data)
Definition: tsvector_op.c:1179

◆ chooseNextStatEntry()

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

Definition at line 2042 of file tsvector_op.c.

References insertStatEntry().

Referenced by ts_accum().

2044 {
2045  uint32 pos;
2046  uint32 middle = (low + high) >> 1;
2047 
2048  pos = (low + middle) >> 1;
2049  if (low != middle && pos >= offset && pos - offset < txt->size)
2050  insertStatEntry(persistentContext, stat, txt, pos - offset);
2051  pos = (high + middle + 1) >> 1;
2052  if (middle + 1 != high && pos >= offset && pos - offset < txt->size)
2053  insertStatEntry(persistentContext, stat, txt, pos - offset);
2054 
2055  if (low != middle)
2056  chooseNextStatEntry(persistentContext, stat, txt, low, middle, offset);
2057  if (high != middle + 1)
2058  chooseNextStatEntry(persistentContext, stat, txt, middle + 1, high, offset);
2059 }
static void chooseNextStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 low, uint32 high, uint32 offset)
Definition: tsvector_op.c:2042
unsigned int uint32
Definition: c.h:367
static void insertStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 off)
Definition: tsvector_op.c:1976

◆ compare_int()

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

Definition at line 426 of file tsvector_op.c.

Referenced by tsvector_delete_by_indices().

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

◆ compare_text_lexemes()

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

Definition at line 437 of file tsvector_op.c.

References tsCompareString(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by array_to_tsvector().

438 {
439  Datum a = *((const Datum *) va);
440  Datum b = *((const Datum *) vb);
441  char *alex = VARDATA_ANY(a);
442  int alex_len = VARSIZE_ANY_EXHDR(a);
443  char *blex = VARDATA_ANY(b);
444  int blex_len = VARSIZE_ANY_EXHDR(b);
445 
446  return tsCompareString(alex, alex_len, blex, blex_len, false);
447 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
uintptr_t Datum
Definition: postgres.h:367
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1142
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ insertStatEntry()

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

Definition at line 1976 of file tsvector_op.c.

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

Referenced by chooseNextStatEntry(), and ts_accum().

1977 {
1978  WordEntry *we = ARRPTR(txt) + off;
1979  StatEntry *node = stat->root,
1980  *pnode = NULL;
1981  int n,
1982  res = 0;
1983  uint32 depth = 1;
1984 
1985  if (stat->weight == 0)
1986  n = (we->haspos) ? POSDATALEN(txt, we) : 1;
1987  else
1988  n = (we->haspos) ? check_weight(txt, we, stat->weight) : 0;
1989 
1990  if (n == 0)
1991  return; /* nothing to insert */
1992 
1993  while (node)
1994  {
1995  res = compareStatWord(node, we, txt);
1996 
1997  if (res == 0)
1998  {
1999  break;
2000  }
2001  else
2002  {
2003  pnode = node;
2004  node = (res < 0) ? node->left : node->right;
2005  }
2006  depth++;
2007  }
2008 
2009  if (depth > stat->maxdepth)
2010  stat->maxdepth = depth;
2011 
2012  if (node == NULL)
2013  {
2014  node = MemoryContextAlloc(persistentContext, STATENTRYHDRSZ + we->len);
2015  node->left = node->right = NULL;
2016  node->ndoc = 1;
2017  node->nentry = n;
2018  node->lenlexeme = we->len;
2019  memcpy(node->lexeme, STRPTR(txt) + we->pos, node->lenlexeme);
2020 
2021  if (pnode == NULL)
2022  {
2023  stat->root = node;
2024  }
2025  else
2026  {
2027  if (res < 0)
2028  pnode->left = node;
2029  else
2030  pnode->right = node;
2031  }
2032 
2033  }
2034  else
2035  {
2036  node->ndoc++;
2037  node->nentry += n;
2038  }
2039 }
uint32 nentry
Definition: tsvector_op.c:49
#define POSDATALEN(x, e)
Definition: ts_type.h:110
StatEntry * root
Definition: tsvector_op.c:67
uint32 len
Definition: ts_type.h:44
uint32 ndoc
Definition: tsvector_op.c:47
unsigned int uint32
Definition: c.h:367
uint32 lenlexeme
Definition: tsvector_op.c:52
uint32 haspos
Definition: ts_type.h:44
uint32 maxdepth
Definition: tsvector_op.c:62
struct StatEntry * left
Definition: tsvector_op.c:50
static int check_weight(TSVector txt, WordEntry *wptr, int8 weight)
Definition: tsvector_op.c:1955
uint32 pos
Definition: ts_type.h:44
#define STATENTRYHDRSZ
Definition: tsvector_op.c:56
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
#define STRPTR(x)
Definition: hstore.h:76
#define ARRPTR(x)
Definition: cube.c:24
#define compareStatWord(a, e, t)
Definition: tsvector_op.c:1970
struct StatEntry * right
Definition: tsvector_op.c:51
char lexeme[FLEXIBLE_ARRAY_MEMBER]
Definition: tsvector_op.c:53
int32 weight
Definition: tsvector_op.c:60

◆ silly_cmp_tsvector()

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

Definition at line 77 of file tsvector_op.c.

References ARRPTR, WordEntry::haspos, i, WordEntry::len, WordEntry::pos, POSDATALEN, POSDATAPTR, TSVectorData::size, STRPTR, tsCompareString(), VARSIZE, WEP_GETPOS, and WEP_GETWEIGHT.

78 {
79  if (VARSIZE(a) < VARSIZE(b))
80  return -1;
81  else if (VARSIZE(a) > VARSIZE(b))
82  return 1;
83  else if (a->size < b->size)
84  return -1;
85  else if (a->size > b->size)
86  return 1;
87  else
88  {
89  WordEntry *aptr = ARRPTR(a);
90  WordEntry *bptr = ARRPTR(b);
91  int i = 0;
92  int res;
93 
94 
95  for (i = 0; i < a->size; i++)
96  {
97  if (aptr->haspos != bptr->haspos)
98  {
99  return (aptr->haspos > bptr->haspos) ? -1 : 1;
100  }
101  else if ((res = tsCompareString(STRPTR(a) + aptr->pos, aptr->len, STRPTR(b) + bptr->pos, bptr->len, false)) != 0)
102  {
103  return res;
104  }
105  else if (aptr->haspos)
106  {
107  WordEntryPos *ap = POSDATAPTR(a, aptr);
108  WordEntryPos *bp = POSDATAPTR(b, bptr);
109  int j;
110 
111  if (POSDATALEN(a, aptr) != POSDATALEN(b, bptr))
112  return (POSDATALEN(a, aptr) > POSDATALEN(b, bptr)) ? -1 : 1;
113 
114  for (j = 0; j < POSDATALEN(a, aptr); j++)
115  {
116  if (WEP_GETPOS(*ap) != WEP_GETPOS(*bp))
117  {
118  return (WEP_GETPOS(*ap) > WEP_GETPOS(*bp)) ? -1 : 1;
119  }
120  else if (WEP_GETWEIGHT(*ap) != WEP_GETWEIGHT(*bp))
121  {
122  return (WEP_GETWEIGHT(*ap) > WEP_GETWEIGHT(*bp)) ? -1 : 1;
123  }
124  ap++, bp++;
125  }
126  }
127 
128  aptr++;
129  bptr++;
130  }
131  }
132 
133  return 0;
134 }
uint16 WordEntryPos
Definition: ts_type.h:63
#define POSDATALEN(x, e)
Definition: ts_type.h:110
#define VARSIZE(PTR)
Definition: postgres.h:303
uint32 len
Definition: ts_type.h:44
#define POSDATAPTR(x, e)
Definition: ts_type.h:111
#define WEP_GETPOS(x)
Definition: ts_type.h:80
int32 size
Definition: ts_type.h:93
uint32 haspos
Definition: ts_type.h:44
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1142
uint32 pos
Definition: ts_type.h:44
#define STRPTR(x)
Definition: hstore.h:76
int i
#define ARRPTR(x)
Definition: cube.c:24
#define WEP_GETWEIGHT(x)
Definition: ts_type.h:79

◆ ts_accum()

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

Definition at line 2074 of file tsvector_op.c.

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

Referenced by ts_stat_sql().

2075 {
2076  TSVector txt = DatumGetTSVector(data);
2077  uint32 i,
2078  nbit = 0,
2079  offset;
2080 
2081  if (stat == NULL)
2082  { /* Init in first */
2083  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2084  stat->maxdepth = 1;
2085  }
2086 
2087  /* simple check of correctness */
2088  if (txt == NULL || txt->size == 0)
2089  {
2090  if (txt && txt != (TSVector) DatumGetPointer(data))
2091  pfree(txt);
2092  return stat;
2093  }
2094 
2095  i = txt->size - 1;
2096  for (; i > 0; i >>= 1)
2097  nbit++;
2098 
2099  nbit = 1 << nbit;
2100  offset = (nbit - txt->size) / 2;
2101 
2102  insertStatEntry(persistentContext, stat, txt, (nbit >> 1) - offset);
2103  chooseNextStatEntry(persistentContext, stat, txt, 0, nbit, offset);
2104 
2105  return stat;
2106 }
static void chooseNextStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 low, uint32 high, uint32 offset)
Definition: tsvector_op.c:2042
void pfree(void *pointer)
Definition: mcxt.c:1056
int32 size
Definition: ts_type.h:93
unsigned int uint32
Definition: c.h:367
#define DatumGetTSVector(X)
Definition: ts_type.h:117
#define stat(a, b)
Definition: win32_port.h:255
static void insertStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 off)
Definition: tsvector_op.c:1976
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
uint32 maxdepth
Definition: tsvector_op.c:62
#define DatumGetPointer(X)
Definition: postgres.h:549
int i

◆ TS_execute()

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

Definition at line 1766 of file tsvector_op.c.

References check_stack_depth(), elog, ERROR, OP_AND, OP_NOT, OP_OR, OP_PHRASE, QI_VAL, TS_EXEC_CALC_NOT, TS_execute(), TS_phrase_execute(), and QueryItem::type.

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

1768 {
1769  /* since this function recurses, it could be driven to stack overflow */
1771 
1772  if (curitem->type == QI_VAL)
1773  return chkcond(arg, (QueryOperand *) curitem,
1774  NULL /* we don't need position info */ );
1775 
1776  switch (curitem->qoperator.oper)
1777  {
1778  case OP_NOT:
1779  if (flags & TS_EXEC_CALC_NOT)
1780  return !TS_execute(curitem + 1, arg, flags, chkcond);
1781  else
1782  return true;
1783 
1784  case OP_AND:
1785  if (TS_execute(curitem + curitem->qoperator.left, arg, flags, chkcond))
1786  return TS_execute(curitem + 1, arg, flags, chkcond);
1787  else
1788  return false;
1789 
1790  case OP_OR:
1791  if (TS_execute(curitem + curitem->qoperator.left, arg, flags, chkcond))
1792  return true;
1793  else
1794  return TS_execute(curitem + 1, arg, flags, chkcond);
1795 
1796  case OP_PHRASE:
1797  return TS_phrase_execute(curitem, arg, flags, chkcond, NULL);
1798 
1799  default:
1800  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1801  }
1802 
1803  /* not reachable, but keep compiler quiet */
1804  return false;
1805 }
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1766
QueryOperator qoperator
Definition: ts_type.h:196
#define TS_EXEC_CALC_NOT
Definition: ts_utils.h:183
#define QI_VAL
Definition: ts_type.h:134
#define OP_OR
Definition: ts_type.h:168
#define OP_AND
Definition: ts_type.h:167
#define ERROR
Definition: elog.h:43
void check_stack_depth(void)
Definition: postgres.c:3312
QueryItemType type
Definition: ts_type.h:195
#define OP_PHRASE
Definition: ts_type.h:169
uint32 left
Definition: ts_type.h:184
#define elog(elevel,...)
Definition: elog.h:214
void * arg
static bool TS_phrase_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond, ExecPhraseData *data)
Definition: tsvector_op.c:1523
#define OP_NOT
Definition: ts_type.h:166

◆ ts_match_qv()

Datum ts_match_qv ( PG_FUNCTION_ARGS  )

Definition at line 1866 of file tsvector_op.c.

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

1867 {
1869  PG_GETARG_DATUM(1),
1870  PG_GETARG_DATUM(0)));
1871 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
Datum ts_match_vq(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:1874
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:347
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:621

◆ ts_match_tq()

Datum ts_match_tq ( PG_FUNCTION_ARGS  )

Definition at line 1926 of file tsvector_op.c.

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

1927 {
1928  TSVector vector;
1929  TSQuery query = PG_GETARG_TSQUERY(1);
1930  bool res;
1931 
1933  PG_GETARG_DATUM(0)));
1934 
1936  TSVectorGetDatum(vector),
1937  TSQueryGetDatum(query)));
1938 
1939  pfree(vector);
1940  PG_FREE_IF_COPY(query, 1);
1941 
1942  PG_RETURN_BOOL(res);
1943 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:619
void pfree(void *pointer)
Definition: mcxt.c:1056
Datum ts_match_vq(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:1874
#define DatumGetBool(X)
Definition: postgres.h:393
#define TSVectorGetDatum(X)
Definition: ts_type.h:119
#define DatumGetTSVector(X)
Definition: ts_type.h:117
#define TSQueryGetDatum(X)
Definition: ts_type.h:237
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:353
Datum to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:259
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:621

◆ ts_match_tt()

Datum ts_match_tt ( PG_FUNCTION_ARGS  )

Definition at line 1904 of file tsvector_op.c.

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

1905 {
1906  TSVector vector;
1907  TSQuery query;
1908  bool res;
1909 
1911  PG_GETARG_DATUM(0)));
1913  PG_GETARG_DATUM(1)));
1914 
1916  TSVectorGetDatum(vector),
1917  TSQueryGetDatum(query)));
1918 
1919  pfree(vector);
1920  pfree(query);
1921 
1922  PG_RETURN_BOOL(res);
1923 }
#define DatumGetTSQuery(X)
Definition: ts_type.h:235
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
Datum plainto_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:617
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:619
void pfree(void *pointer)
Definition: mcxt.c:1056
Datum ts_match_vq(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:1874
#define DatumGetBool(X)
Definition: postgres.h:393
#define TSVectorGetDatum(X)
Definition: ts_type.h:119
#define DatumGetTSVector(X)
Definition: ts_type.h:117
#define TSQueryGetDatum(X)
Definition: ts_type.h:237
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:353
Datum to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:259
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:621

◆ ts_match_vq()

Datum ts_match_vq ( PG_FUNCTION_ARGS  )

Definition at line 1874 of file tsvector_op.c.

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, TSVectorData::size, TSQueryData::size, STRPTR, TS_EXEC_CALC_NOT, TS_execute(), val, and CHKVAL::values.

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

1875 {
1877  TSQuery query = PG_GETARG_TSQUERY(1);
1878  CHKVAL chkval;
1879  bool result;
1880 
1881  /* empty query matches nothing */
1882  if (!query->size)
1883  {
1884  PG_FREE_IF_COPY(val, 0);
1885  PG_FREE_IF_COPY(query, 1);
1886  PG_RETURN_BOOL(false);
1887  }
1888 
1889  chkval.arrb = ARRPTR(val);
1890  chkval.arre = chkval.arrb + val->size;
1891  chkval.values = STRPTR(val);
1892  chkval.operand = GETOPERAND(query);
1893  result = TS_execute(GETQUERY(query),
1894  &chkval,
1897 
1898  PG_FREE_IF_COPY(val, 0);
1899  PG_FREE_IF_COPY(query, 1);
1900  PG_RETURN_BOOL(result);
1901 }
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1766
#define TS_EXEC_CALC_NOT
Definition: ts_utils.h:183
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
#define GETQUERY(x)
Definition: _int.h:157
#define GETOPERAND(x)
Definition: ltree.h:151
int32 * arrb
Definition: _int_bool.c:227
static bool checkcondition_str(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsvector_op.c:1260
char * operand
Definition: ltxtquery_op.c:52
int32 size
Definition: ts_type.h:93
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:353
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
char * values
Definition: tsvector_op.c:40
int32 * arre
Definition: _int_bool.c:228
#define STRPTR(x)
Definition: hstore.h:76
int32 size
Definition: ts_type.h:208
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
#define ARRPTR(x)
Definition: cube.c:24
long val
Definition: informix.c:664

◆ TS_phrase_execute()

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

Definition at line 1523 of file tsvector_op.c.

References Assert, check_stack_depth(), QueryOperand::distance, elog, ERROR, Max, Min, ExecPhraseData::negate, ExecPhraseData::npos, OP_AND, OP_NOT, OP_OR, OP_PHRASE, QI_VAL, true, TS_EXEC_PHRASE_NO_POS, TS_phrase_output(), TSPO_BOTH, TSPO_L_ONLY, TSPO_R_ONLY, QueryItem::type, and ExecPhraseData::width.

Referenced by TS_execute().

1526 {
1527  ExecPhraseData Ldata,
1528  Rdata;
1529  bool lmatch,
1530  rmatch;
1531  int Loffset,
1532  Roffset,
1533  maxwidth;
1534 
1535  /* since this function recurses, it could be driven to stack overflow */
1537 
1538  if (curitem->type == QI_VAL)
1539  return chkcond(arg, (QueryOperand *) curitem, data);
1540 
1541  switch (curitem->qoperator.oper)
1542  {
1543  case OP_NOT:
1544 
1545  /*
1546  * Because a "true" result with no specific positions is taken as
1547  * uncertain, we need no special care here for !TS_EXEC_CALC_NOT.
1548  * If it's a false positive, the right things happen anyway.
1549  *
1550  * Also, we need not touch data->width, since a NOT operation does
1551  * not change the match width.
1552  */
1553  if (TS_phrase_execute(curitem + 1, arg, flags, chkcond, data))
1554  {
1555  if (data->npos > 0)
1556  {
1557  /* we have some positions, invert negate flag */
1558  data->negate = !data->negate;
1559  return true;
1560  }
1561  else if (data->negate)
1562  {
1563  /* change "match everywhere" to "match nowhere" */
1564  data->negate = false;
1565  return false;
1566  }
1567  /* match positions are, and remain, uncertain */
1568  return true;
1569  }
1570  else
1571  {
1572  /* change "match nowhere" to "match everywhere" */
1573  Assert(data->npos == 0 && !data->negate);
1574  data->negate = true;
1575  return true;
1576  }
1577 
1578  case OP_PHRASE:
1579  case OP_AND:
1580  memset(&Ldata, 0, sizeof(Ldata));
1581  memset(&Rdata, 0, sizeof(Rdata));
1582 
1583  if (!TS_phrase_execute(curitem + curitem->qoperator.left,
1584  arg, flags, chkcond, &Ldata))
1585  return false;
1586 
1587  if (!TS_phrase_execute(curitem + 1,
1588  arg, flags, chkcond, &Rdata))
1589  return false;
1590 
1591  /*
1592  * If either operand has no position information, then we can't
1593  * return position data, only a "possible match" result. "Possible
1594  * match" answers are only wanted when TS_EXEC_PHRASE_NO_POS flag
1595  * is set, otherwise return false.
1596  */
1597  if ((Ldata.npos == 0 && !Ldata.negate) ||
1598  (Rdata.npos == 0 && !Rdata.negate))
1599  return (flags & TS_EXEC_PHRASE_NO_POS) ? true : false;
1600 
1601  if (curitem->qoperator.oper == OP_PHRASE)
1602  {
1603  /*
1604  * Compute Loffset and Roffset suitable for phrase match, and
1605  * compute overall width of whole phrase match.
1606  */
1607  Loffset = curitem->qoperator.distance + Rdata.width;
1608  Roffset = 0;
1609  if (data)
1610  data->width = curitem->qoperator.distance +
1611  Ldata.width + Rdata.width;
1612  }
1613  else
1614  {
1615  /*
1616  * For OP_AND, set output width and alignment like OP_OR (see
1617  * comment below)
1618  */
1619  maxwidth = Max(Ldata.width, Rdata.width);
1620  Loffset = maxwidth - Ldata.width;
1621  Roffset = maxwidth - Rdata.width;
1622  if (data)
1623  data->width = maxwidth;
1624  }
1625 
1626  if (Ldata.negate && Rdata.negate)
1627  {
1628  /* !L & !R: treat as !(L | R) */
1629  (void) TS_phrase_output(data, &Ldata, &Rdata,
1631  Loffset, Roffset,
1632  Ldata.npos + Rdata.npos);
1633  if (data)
1634  data->negate = true;
1635  return true;
1636  }
1637  else if (Ldata.negate)
1638  {
1639  /* !L & R */
1640  return TS_phrase_output(data, &Ldata, &Rdata,
1641  TSPO_R_ONLY,
1642  Loffset, Roffset,
1643  Rdata.npos);
1644  }
1645  else if (Rdata.negate)
1646  {
1647  /* L & !R */
1648  return TS_phrase_output(data, &Ldata, &Rdata,
1649  TSPO_L_ONLY,
1650  Loffset, Roffset,
1651  Ldata.npos);
1652  }
1653  else
1654  {
1655  /* straight AND */
1656  return TS_phrase_output(data, &Ldata, &Rdata,
1657  TSPO_BOTH,
1658  Loffset, Roffset,
1659  Min(Ldata.npos, Rdata.npos));
1660  }
1661 
1662  case OP_OR:
1663  memset(&Ldata, 0, sizeof(Ldata));
1664  memset(&Rdata, 0, sizeof(Rdata));
1665 
1666  lmatch = TS_phrase_execute(curitem + curitem->qoperator.left,
1667  arg, flags, chkcond, &Ldata);
1668  rmatch = TS_phrase_execute(curitem + 1,
1669  arg, flags, chkcond, &Rdata);
1670 
1671  if (!lmatch && !rmatch)
1672  return false;
1673 
1674  /*
1675  * If a valid operand has no position information, then we can't
1676  * return position data, only a "possible match" result. "Possible
1677  * match" answers are only wanted when TS_EXEC_PHRASE_NO_POS flag
1678  * is set, otherwise return false.
1679  */
1680  if ((lmatch && Ldata.npos == 0 && !Ldata.negate) ||
1681  (rmatch && Rdata.npos == 0 && !Rdata.negate))
1682  return (flags & TS_EXEC_PHRASE_NO_POS) ? true : false;
1683 
1684  /*
1685  * Cope with undefined output width from failed submatch. (This
1686  * takes less code than trying to ensure that all failure returns
1687  * set data->width to zero.)
1688  */
1689  if (!lmatch)
1690  Ldata.width = 0;
1691  if (!rmatch)
1692  Rdata.width = 0;
1693 
1694  /*
1695  * For OP_AND and OP_OR, report the width of the wider of the two
1696  * inputs, and align the narrower input's positions to the right
1697  * end of that width. This rule deals at least somewhat
1698  * reasonably with cases like "x <-> (y | z <-> q)".
1699  */
1700  maxwidth = Max(Ldata.width, Rdata.width);
1701  Loffset = maxwidth - Ldata.width;
1702  Roffset = maxwidth - Rdata.width;
1703  data->width = maxwidth;
1704 
1705  if (Ldata.negate && Rdata.negate)
1706  {
1707  /* !L | !R: treat as !(L & R) */
1708  (void) TS_phrase_output(data, &Ldata, &Rdata,
1709  TSPO_BOTH,
1710  Loffset, Roffset,
1711  Min(Ldata.npos, Rdata.npos));
1712  data->negate = true;
1713  return true;
1714  }
1715  else if (Ldata.negate)
1716  {
1717  /* !L | R: treat as !(L & !R) */
1718  (void) TS_phrase_output(data, &Ldata, &Rdata,
1719  TSPO_L_ONLY,
1720  Loffset, Roffset,
1721  Ldata.npos);
1722  data->negate = true;
1723  return true;
1724  }
1725  else if (Rdata.negate)
1726  {
1727  /* L | !R: treat as !(!L & R) */
1728  (void) TS_phrase_output(data, &Ldata, &Rdata,
1729  TSPO_R_ONLY,
1730  Loffset, Roffset,
1731  Rdata.npos);
1732  data->negate = true;
1733  return true;
1734  }
1735  else
1736  {
1737  /* straight OR */
1738  return TS_phrase_output(data, &Ldata, &Rdata,
1740  Loffset, Roffset,
1741  Ldata.npos + Rdata.npos);
1742  }
1743 
1744  default:
1745  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1746  }
1747 
1748  /* not reachable, but keep compiler quiet */
1749  return false;
1750 }
QueryOperator qoperator
Definition: ts_type.h:196
#define TSPO_R_ONLY
Definition: tsvector_op.c:1381
#define Min(x, y)
Definition: c.h:920
#define QI_VAL
Definition: ts_type.h:134
int16 distance
Definition: ts_type.h:183
#define TSPO_BOTH
Definition: tsvector_op.c:1382
#define OP_OR
Definition: ts_type.h:168
static bool TS_phrase_output(ExecPhraseData *data, ExecPhraseData *Ldata, ExecPhraseData *Rdata, int emit, int Loffset, int Roffset, int max_npos)
Definition: tsvector_op.c:1385
#define true
Definition: c.h:321
#define OP_AND
Definition: ts_type.h:167
#define ERROR
Definition: elog.h:43
void check_stack_depth(void)
Definition: postgres.c:3312
#define TSPO_L_ONLY
Definition: tsvector_op.c:1380
QueryItemType type
Definition: ts_type.h:195
#define Max(x, y)
Definition: c.h:914
#define Assert(condition)
Definition: c.h:738
#define OP_PHRASE
Definition: ts_type.h:169
#define TS_EXEC_PHRASE_NO_POS
Definition: ts_utils.h:190
uint32 left
Definition: ts_type.h:184
#define elog(elevel,...)
Definition: elog.h:214
void * arg
static bool TS_phrase_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond, ExecPhraseData *data)
Definition: tsvector_op.c:1523
#define OP_NOT
Definition: ts_type.h:166

◆ TS_phrase_output()

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

Definition at line 1385 of file tsvector_op.c.

References ExecPhraseData::allocated, Assert, ExecPhraseData::npos, palloc(), ExecPhraseData::pos, TSPO_BOTH, TSPO_L_ONLY, TSPO_R_ONLY, and WEP_GETPOS.

Referenced by TS_phrase_execute().

1392 {
1393  int Lindex,
1394  Rindex;
1395 
1396  /* Loop until both inputs are exhausted */
1397  Lindex = Rindex = 0;
1398  while (Lindex < Ldata->npos || Rindex < Rdata->npos)
1399  {
1400  int Lpos,
1401  Rpos;
1402  int output_pos = 0;
1403 
1404  /*
1405  * Fetch current values to compare. WEP_GETPOS() is needed because
1406  * ExecPhraseData->data can point to a tsvector's WordEntryPosVector.
1407  */
1408  if (Lindex < Ldata->npos)
1409  Lpos = WEP_GETPOS(Ldata->pos[Lindex]) + Loffset;
1410  else
1411  {
1412  /* L array exhausted, so we're done if R_ONLY isn't set */
1413  if (!(emit & TSPO_R_ONLY))
1414  break;
1415  Lpos = INT_MAX;
1416  }
1417  if (Rindex < Rdata->npos)
1418  Rpos = WEP_GETPOS(Rdata->pos[Rindex]) + Roffset;
1419  else
1420  {
1421  /* R array exhausted, so we're done if L_ONLY isn't set */
1422  if (!(emit & TSPO_L_ONLY))
1423  break;
1424  Rpos = INT_MAX;
1425  }
1426 
1427  /* Merge-join the two input lists */
1428  if (Lpos < Rpos)
1429  {
1430  /* Lpos is not matched in Rdata, should we output it? */
1431  if (emit & TSPO_L_ONLY)
1432  output_pos = Lpos;
1433  Lindex++;
1434  }
1435  else if (Lpos == Rpos)
1436  {
1437  /* Lpos and Rpos match ... should we output it? */
1438  if (emit & TSPO_BOTH)
1439  output_pos = Rpos;
1440  Lindex++;
1441  Rindex++;
1442  }
1443  else /* Lpos > Rpos */
1444  {
1445  /* Rpos is not matched in Ldata, should we output it? */
1446  if (emit & TSPO_R_ONLY)
1447  output_pos = Rpos;
1448  Rindex++;
1449  }
1450 
1451  if (output_pos > 0)
1452  {
1453  if (data)
1454  {
1455  /* Store position, first allocating output array if needed */
1456  if (data->pos == NULL)
1457  {
1458  data->pos = (WordEntryPos *)
1459  palloc(max_npos * sizeof(WordEntryPos));
1460  data->allocated = true;
1461  }
1462  data->pos[data->npos++] = output_pos;
1463  }
1464  else
1465  {
1466  /*
1467  * Exact positions not needed, so return true as soon as we
1468  * know there is at least one.
1469  */
1470  return true;
1471  }
1472  }
1473  }
1474 
1475  if (data && data->npos > 0)
1476  {
1477  /* Let's assert we didn't overrun the array */
1478  Assert(data->npos <= max_npos);
1479  return true;
1480  }
1481  return false;
1482 }
uint16 WordEntryPos
Definition: ts_type.h:63
#define TSPO_R_ONLY
Definition: tsvector_op.c:1381
bool allocated
Definition: ts_utils.h:152
#define TSPO_BOTH
Definition: tsvector_op.c:1382
#define WEP_GETPOS(x)
Definition: ts_type.h:80
WordEntryPos * pos
Definition: ts_utils.h:154
#define TSPO_L_ONLY
Definition: tsvector_op.c:1380
#define Assert(condition)
Definition: c.h:738
void * palloc(Size size)
Definition: mcxt.c:949

◆ ts_process_call()

static Datum ts_process_call ( FuncCallContext funcctx)
static

Definition at line 2201 of file tsvector_op.c.

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().

2202 {
2203  TSVectorStat *st;
2204  StatEntry *entry;
2205 
2206  st = (TSVectorStat *) funcctx->user_fctx;
2207 
2208  entry = walkStatEntryTree(st);
2209 
2210  if (entry != NULL)
2211  {
2212  Datum result;
2213  char *values[3];
2214  char ndoc[16];
2215  char nentry[16];
2216  HeapTuple tuple;
2217 
2218  values[0] = palloc(entry->lenlexeme + 1);
2219  memcpy(values[0], entry->lexeme, entry->lenlexeme);
2220  (values[0])[entry->lenlexeme] = '\0';
2221  sprintf(ndoc, "%d", entry->ndoc);
2222  values[1] = ndoc;
2223  sprintf(nentry, "%d", entry->nentry);
2224  values[2] = nentry;
2225 
2226  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
2227  result = HeapTupleGetDatum(tuple);
2228 
2229  pfree(values[0]);
2230 
2231  /* mark entry as already visited */
2232  entry->ndoc = 0;
2233 
2234  return result;
2235  }
2236 
2237  return (Datum) 0;
2238 }
uint32 nentry
Definition: tsvector_op.c:49
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2116
#define sprintf
Definition: port.h:195
uint32 ndoc
Definition: tsvector_op.c:47
void pfree(void *pointer)
Definition: mcxt.c:1056
static StatEntry * walkStatEntryTree(TSVectorStat *stat)
Definition: tsvector_op.c:2155
AttInMetadata * attinmeta
Definition: funcapi.h:91
uint32 lenlexeme
Definition: tsvector_op.c:52
uintptr_t Datum
Definition: postgres.h:367
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
char lexeme[FLEXIBLE_ARRAY_MEMBER]
Definition: tsvector_op.c:53

◆ ts_setup_firstcall()

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

Definition at line 2109 of file tsvector_op.c.

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

Referenced by ts_stat1(), and ts_stat2().

2111 {
2112  TupleDesc tupdesc;
2113  MemoryContext oldcontext;
2114  StatEntry *node;
2115 
2116  funcctx->user_fctx = (void *) stat;
2117 
2118  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2119 
2120  stat->stack = palloc0(sizeof(StatEntry *) * (stat->maxdepth + 1));
2121  stat->stackpos = 0;
2122 
2123  node = stat->root;
2124  /* find leftmost value */
2125  if (node == NULL)
2126  stat->stack[stat->stackpos] = NULL;
2127  else
2128  for (;;)
2129  {
2130  stat->stack[stat->stackpos] = node;
2131  if (node->left)
2132  {
2133  stat->stackpos++;
2134  node = node->left;
2135  }
2136  else
2137  break;
2138  }
2139  Assert(stat->stackpos <= stat->maxdepth);
2140 
2141  tupdesc = CreateTemplateTupleDesc(3);
2142  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "word",
2143  TEXTOID, -1, 0);
2144  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "ndoc",
2145  INT4OID, -1, 0);
2146  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "nentry",
2147  INT4OID, -1, 0);
2148  funcctx->tuple_desc = BlessTupleDesc(tupdesc);
2149  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
2150 
2151  MemoryContextSwitchTo(oldcontext);
2152 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
uint32 stackpos
Definition: tsvector_op.c:65
StatEntry * root
Definition: tsvector_op.c:67
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
TupleDesc tuple_desc
Definition: funcapi.h:112
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
AttInMetadata * attinmeta
Definition: funcapi.h:91
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
StatEntry ** stack
Definition: tsvector_op.c:64
void * palloc0(Size size)
Definition: mcxt.c:980
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
#define Assert(condition)
Definition: c.h:738
uint32 maxdepth
Definition: tsvector_op.c:62
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
struct StatEntry * left
Definition: tsvector_op.c:50
void * user_fctx
Definition: funcapi.h:82
int16 AttrNumber
Definition: attnum.h:21

◆ ts_stat1()

Datum ts_stat1 ( PG_FUNCTION_ARGS  )

Definition at line 2330 of file tsvector_op.c.

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().

2331 {
2332  FuncCallContext *funcctx;
2333  Datum result;
2334 
2335  if (SRF_IS_FIRSTCALL())
2336  {
2337  TSVectorStat *stat;
2338  text *txt = PG_GETARG_TEXT_PP(0);
2339 
2340  funcctx = SRF_FIRSTCALL_INIT();
2341  SPI_connect();
2342  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, NULL);
2343  PG_FREE_IF_COPY(txt, 0);
2344  ts_setup_firstcall(fcinfo, funcctx, stat);
2345  SPI_finish();
2346  }
2347 
2348  funcctx = SRF_PERCALL_SETUP();
2349  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2350  SRF_RETURN_NEXT(funcctx, result);
2351  SRF_RETURN_DONE(funcctx);
2352 }
int SPI_connect(void)
Definition: spi.c:89
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
int SPI_finish(void)
Definition: spi.c:176
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
static TSVectorStat * ts_stat_sql(MemoryContext persistentContext, text *txt, text *ws)
Definition: tsvector_op.c:2241
static void ts_setup_firstcall(FunctionCallInfo fcinfo, FuncCallContext *funcctx, TSVectorStat *stat)
Definition: tsvector_op.c:2109
#define stat(a, b)
Definition: win32_port.h:255
uintptr_t Datum
Definition: postgres.h:367
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
static Datum ts_process_call(FuncCallContext *funcctx)
Definition: tsvector_op.c:2201
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:555
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ ts_stat2()

Datum ts_stat2 ( PG_FUNCTION_ARGS  )

Definition at line 2355 of file tsvector_op.c.

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().

2356 {
2357  FuncCallContext *funcctx;
2358  Datum result;
2359 
2360  if (SRF_IS_FIRSTCALL())
2361  {
2362  TSVectorStat *stat;
2363  text *txt = PG_GETARG_TEXT_PP(0);
2364  text *ws = PG_GETARG_TEXT_PP(1);
2365 
2366  funcctx = SRF_FIRSTCALL_INIT();
2367  SPI_connect();
2368  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, ws);
2369  PG_FREE_IF_COPY(txt, 0);
2370  PG_FREE_IF_COPY(ws, 1);
2371  ts_setup_firstcall(fcinfo, funcctx, stat);
2372  SPI_finish();
2373  }
2374 
2375  funcctx = SRF_PERCALL_SETUP();
2376  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2377  SRF_RETURN_NEXT(funcctx, result);
2378  SRF_RETURN_DONE(funcctx);
2379 }
int SPI_connect(void)
Definition: spi.c:89
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
int SPI_finish(void)
Definition: spi.c:176
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
static TSVectorStat * ts_stat_sql(MemoryContext persistentContext, text *txt, text *ws)
Definition: tsvector_op.c:2241
static void ts_setup_firstcall(FunctionCallInfo fcinfo, FuncCallContext *funcctx, TSVectorStat *stat)
Definition: tsvector_op.c:2109
#define stat(a, b)
Definition: win32_port.h:255
uintptr_t Datum
Definition: postgres.h:367
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
static Datum ts_process_call(FuncCallContext *funcctx)
Definition: tsvector_op.c:2201
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
Definition: c.h:555
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ ts_stat_sql()

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

Definition at line 2241 of file tsvector_op.c.

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

Referenced by ts_stat1(), and ts_stat2().

2242 {
2243  char *query = text_to_cstring(txt);
2244  TSVectorStat *stat;
2245  bool isnull;
2246  Portal portal;
2247  SPIPlanPtr plan;
2248 
2249  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2250  /* internal error */
2251  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2252 
2253  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2254  /* internal error */
2255  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2256 
2257  SPI_cursor_fetch(portal, true, 100);
2258 
2259  if (SPI_tuptable == NULL ||
2260  SPI_tuptable->tupdesc->natts != 1 ||
2262  TSVECTOROID))
2263  ereport(ERROR,
2264  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2265  errmsg("ts_stat query must return one tsvector column")));
2266 
2267  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2268  stat->maxdepth = 1;
2269 
2270  if (ws)
2271  {
2272  char *buf;
2273 
2274  buf = VARDATA_ANY(ws);
2275  while (buf - VARDATA_ANY(ws) < VARSIZE_ANY_EXHDR(ws))
2276  {
2277  if (pg_mblen(buf) == 1)
2278  {
2279  switch (*buf)
2280  {
2281  case 'A':
2282  case 'a':
2283  stat->weight |= 1 << 3;
2284  break;
2285  case 'B':
2286  case 'b':
2287  stat->weight |= 1 << 2;
2288  break;
2289  case 'C':
2290  case 'c':
2291  stat->weight |= 1 << 1;
2292  break;
2293  case 'D':
2294  case 'd':
2295  stat->weight |= 1;
2296  break;
2297  default:
2298  stat->weight |= 0;
2299  }
2300  }
2301  buf += pg_mblen(buf);
2302  }
2303  }
2304 
2305  while (SPI_processed > 0)
2306  {
2307  uint64 i;
2308 
2309  for (i = 0; i < SPI_processed; i++)
2310  {
2311  Datum data = SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull);
2312 
2313  if (!isnull)
2314  stat = ts_accum(persistentContext, stat, data);
2315  }
2316 
2318  SPI_cursor_fetch(portal, true, 100);
2319  }
2320 
2322  SPI_cursor_close(portal);
2323  SPI_freeplan(plan);
2324  pfree(query);
2325 
2326  return stat;
2327 }
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1084
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
SPITupleTable * SPI_tuptable
Definition: spi.c:46
int errcode(int sqlerrcode)
Definition: elog.c:610
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1221
HeapTuple * vals
Definition: spi.h:26
uint64 SPI_processed
Definition: spi.c:45
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1028
static char * buf
Definition: pg_test_fsync.c:67
bool IsBinaryCoercible(Oid srctype, Oid targettype)
#define stat(a, b)
Definition: win32_port.h:255
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1162
uintptr_t Datum
Definition: postgres.h:367
TupleDesc tupdesc
Definition: spi.h:25
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
#define ereport(elevel,...)
Definition: elog.h:144
uint32 maxdepth
Definition: tsvector_op.c:62
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:801
void SPI_cursor_close(Portal portal)
Definition: spi.c:1589
char * text_to_cstring(const text *t)
Definition: varlena.c:205
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
int i
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1533
static TSVectorStat * ts_accum(MemoryContext persistentContext, TSVectorStat *stat, Datum data)
Definition: tsvector_op.c:2074
int32 weight
Definition: tsvector_op.c:60

◆ tsCompareString()

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

Definition at line 1142 of file tsvector_op.c.

References 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().

1143 {
1144  int cmp;
1145 
1146  if (lena == 0)
1147  {
1148  if (prefix)
1149  cmp = 0; /* empty string is prefix of anything */
1150  else
1151  cmp = (lenb > 0) ? -1 : 0;
1152  }
1153  else if (lenb == 0)
1154  {
1155  cmp = (lena > 0) ? 1 : 0;
1156  }
1157  else
1158  {
1159  cmp = memcmp(a, b, Min(lena, lenb));
1160 
1161  if (prefix)
1162  {
1163  if (cmp == 0 && lena > lenb)
1164  cmp = 1; /* a is longer, so not a prefix of b */
1165  }
1166  else if (cmp == 0 && lena != lenb)
1167  {
1168  cmp = (lena < lenb) ? -1 : 1;
1169  }
1170  }
1171 
1172  return cmp;
1173 }
#define Min(x, y)
Definition: c.h:920
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ tsquery_requires_match()

bool tsquery_requires_match ( QueryItem curitem)

Definition at line 1816 of file tsvector_op.c.

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

Referenced by gin_extract_tsquery(), and tsquery_requires_match().

1817 {
1818  /* since this function recurses, it could be driven to stack overflow */
1820 
1821  if (curitem->type == QI_VAL)
1822  return true;
1823 
1824  switch (curitem->qoperator.oper)
1825  {
1826  case OP_NOT:
1827 
1828  /*
1829  * Assume there are no required matches underneath a NOT. For
1830  * some cases with nested NOTs, we could prove there's a required
1831  * match, but it seems unlikely to be worth the trouble.
1832  */
1833  return false;
1834 
1835  case OP_PHRASE:
1836 
1837  /*
1838  * Treat OP_PHRASE as OP_AND here
1839  */
1840  case OP_AND:
1841  /* If either side requires a match, we're good */
1842  if (tsquery_requires_match(curitem + curitem->qoperator.left))
1843  return true;
1844  else
1845  return tsquery_requires_match(curitem + 1);
1846 
1847  case OP_OR:
1848  /* Both sides must require a match */
1849  if (tsquery_requires_match(curitem + curitem->qoperator.left))
1850  return tsquery_requires_match(curitem + 1);
1851  else
1852  return false;
1853 
1854  default:
1855  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1856  }
1857 
1858  /* not reachable, but keep compiler quiet */
1859  return false;
1860 }
QueryOperator qoperator
Definition: ts_type.h:196
bool tsquery_requires_match(QueryItem *curitem)
Definition: tsvector_op.c:1816
#define QI_VAL
Definition: ts_type.h:134
#define OP_OR
Definition: ts_type.h:168
#define OP_AND
Definition: ts_type.h:167
#define ERROR
Definition: elog.h:43
void check_stack_depth(void)
Definition: postgres.c:3312
QueryItemType type
Definition: ts_type.h:195
#define OP_PHRASE
Definition: ts_type.h:169
uint32 left
Definition: ts_type.h:184
#define elog(elevel,...)
Definition: elog.h:214
#define OP_NOT
Definition: ts_type.h:166

◆ tsvector_bsearch()

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

Definition at line 393 of file tsvector_op.c.

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

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

394 {
395  WordEntry *arrin = ARRPTR(tsv);
396  int StopLow = 0,
397  StopHigh = tsv->size,
398  StopMiddle,
399  cmp;
400 
401  while (StopLow < StopHigh)
402  {
403  StopMiddle = (StopLow + StopHigh) / 2;
404 
405  cmp = tsCompareString(lexeme, lexeme_len,
406  STRPTR(tsv) + arrin[StopMiddle].pos,
407  arrin[StopMiddle].len,
408  false);
409 
410  if (cmp < 0)
411  StopHigh = StopMiddle;
412  else if (cmp > 0)
413  StopLow = StopMiddle + 1;
414  else /* found it */
415  return StopMiddle;
416  }
417 
418  return -1;
419 }
int32 size
Definition: ts_type.h:93
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1142
#define STRPTR(x)
Definition: hstore.h:76
#define ARRPTR(x)
Definition: cube.c:24
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ tsvector_concat()

Datum tsvector_concat ( PG_FUNCTION_ARGS  )

Definition at line 915 of file tsvector_op.c.

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

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

◆ tsvector_delete_arr()

Datum tsvector_delete_arr ( PG_FUNCTION_ARGS  )

Definition at line 573 of file tsvector_op.c.

References deconstruct_array(), ereport, errcode(), errmsg(), ERROR, 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.

574 {
575  TSVector tsin = PG_GETARG_TSVECTOR(0),
576  tsout;
577  ArrayType *lexemes = PG_GETARG_ARRAYTYPE_P(1);
578  int i,
579  nlex,
580  skip_count,
581  *skip_indices;
582  Datum *dlexemes;
583  bool *nulls;
584 
585  deconstruct_array(lexemes, TEXTOID, -1, false, TYPALIGN_INT,
586  &dlexemes, &nulls, &nlex);
587 
588  /*
589  * In typical use case array of lexemes to delete is relatively small. So
590  * here we optimize things for that scenario: iterate through lexarr
591  * performing binary search of each lexeme from lexarr in tsvector.
592  */
593  skip_indices = palloc0(nlex * sizeof(int));
594  for (i = skip_count = 0; i < nlex; i++)
595  {
596  char *lex;
597  int lex_len,
598  lex_pos;
599 
600  if (nulls[i])
601  ereport(ERROR,
602  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
603  errmsg("lexeme array may not contain nulls")));
604 
605  lex = VARDATA(dlexemes[i]);
606  lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
607  lex_pos = tsvector_bsearch(tsin, lex, lex_len);
608 
609  if (lex_pos >= 0)
610  skip_indices[skip_count++] = lex_pos;
611  }
612 
613  tsout = tsvector_delete_by_indices(tsin, skip_indices, skip_count);
614 
615  pfree(skip_indices);
616  PG_FREE_IF_COPY(tsin, 0);
617  PG_FREE_IF_COPY(lexemes, 1);
618 
619  PG_RETURN_POINTER(tsout);
620 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:355
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:561
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
static TSVector tsvector_delete_by_indices(TSVector tsv, int *indices_to_delete, int indices_count)
Definition: tsvector_op.c:459
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
#define ereport(elevel,...)
Definition: elog.h:144
static int tsvector_bsearch(const TSVector tsv, char *lexeme, int lexeme_len)
Definition: tsvector_op.c:393
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120

◆ tsvector_delete_by_indices()

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

Definition at line 459 of file tsvector_op.c.

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

Referenced by tsvector_delete_arr(), and tsvector_delete_str().

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

◆ tsvector_delete_str()

Datum tsvector_delete_str ( PG_FUNCTION_ARGS  )

Definition at line 549 of file tsvector_op.c.

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.

550 {
551  TSVector tsin = PG_GETARG_TSVECTOR(0),
552  tsout;
553  text *tlexeme = PG_GETARG_TEXT_PP(1);
554  char *lexeme = VARDATA_ANY(tlexeme);
555  int lexeme_len = VARSIZE_ANY_EXHDR(tlexeme),
556  skip_index;
557 
558  if ((skip_index = tsvector_bsearch(tsin, lexeme, lexeme_len)) == -1)
559  PG_RETURN_POINTER(tsin);
560 
561  tsout = tsvector_delete_by_indices(tsin, &skip_index, 1);
562 
563  PG_FREE_IF_COPY(tsin, 0);
564  PG_FREE_IF_COPY(tlexeme, 1);
565  PG_RETURN_POINTER(tsout);
566 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:355
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
static TSVector tsvector_delete_by_indices(TSVector tsv, int *indices_to_delete, int indices_count)
Definition: tsvector_op.c:459
static int tsvector_bsearch(const TSVector tsv, char *lexeme, int lexeme_len)
Definition: tsvector_op.c:393
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
Definition: c.h:555

◆ tsvector_filter()

Datum tsvector_filter ( PG_FUNCTION_ARGS  )

Definition at line 808 of file tsvector_op.c.

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

809 {
810  TSVector tsin = PG_GETARG_TSVECTOR(0),
811  tsout;
813  WordEntry *arrin = ARRPTR(tsin),
814  *arrout;
815  char *datain = STRPTR(tsin),
816  *dataout;
817  Datum *dweights;
818  bool *nulls;
819  int nweights;
820  int i,
821  j;
822  int cur_pos = 0;
823  char mask = 0;
824 
825  deconstruct_array(weights, CHAROID, 1, true, TYPALIGN_CHAR,
826  &dweights, &nulls, &nweights);
827 
828  for (i = 0; i < nweights; i++)
829  {
830  char char_weight;
831 
832  if (nulls[i])
833  ereport(ERROR,
834  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
835  errmsg("weight array may not contain nulls")));
836 
837  char_weight = DatumGetChar(dweights[i]);
838  switch (char_weight)
839  {
840  case 'A':
841  case 'a':
842  mask = mask | 8;
843  break;
844  case 'B':
845  case 'b':
846  mask = mask | 4;
847  break;
848  case 'C':
849  case 'c':
850  mask = mask | 2;
851  break;
852  case 'D':
853  case 'd':
854  mask = mask | 1;
855  break;
856  default:
857  ereport(ERROR,
858  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
859  errmsg("unrecognized weight: \"%c\"", char_weight)));
860  }
861  }
862 
863  tsout = (TSVector) palloc0(VARSIZE(tsin));
864  tsout->size = tsin->size;
865  arrout = ARRPTR(tsout);
866  dataout = STRPTR(tsout);
867 
868  for (i = j = 0; i < tsin->size; i++)
869  {
870  WordEntryPosVector *posvin,
871  *posvout;
872  int npos = 0;
873  int k;
874 
875  if (!arrin[i].haspos)
876  continue;
877 
878  posvin = _POSVECPTR(tsin, arrin + i);
879  posvout = (WordEntryPosVector *)
880  (dataout + SHORTALIGN(cur_pos + arrin[i].len));
881 
882  for (k = 0; k < posvin->npos; k++)
883  {
884  if (mask & (1 << WEP_GETWEIGHT(posvin->pos[k])))
885  posvout->pos[npos++] = posvin->pos[k];
886  }
887 
888  /* if no satisfactory positions found, skip lexeme */
889  if (!npos)
890  continue;
891 
892  arrout[j].haspos = true;
893  arrout[j].len = arrin[i].len;
894  arrout[j].pos = cur_pos;
895 
896  memcpy(dataout + cur_pos, datain + arrin[i].pos, arrin[i].len);
897  posvout->npos = npos;
898  cur_pos += SHORTALIGN(arrin[i].len);
899  cur_pos += POSDATALEN(tsout, arrout + j) * sizeof(WordEntryPos) +
900  sizeof(uint16);
901  j++;
902  }
903 
904  tsout->size = j;
905  if (dataout != STRPTR(tsout))
906  memmove(STRPTR(tsout), dataout, cur_pos);
907 
908  SET_VARSIZE(tsout, CALCDATASIZE(tsout->size, cur_pos));
909 
910  PG_FREE_IF_COPY(tsin, 0);
911  PG_RETURN_POINTER(tsout);
912 }
uint16 WordEntryPos
Definition: ts_type.h:63
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:355
#define POSDATALEN(x, e)
Definition: ts_type.h:110
#define VARSIZE(PTR)
Definition: postgres.h:303
#define _POSVECPTR(x, e)
Definition: ts_type.h:109
uint32 len
Definition: ts_type.h:44
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
unsigned short uint16
Definition: c.h:366
#define ERROR
Definition: elog.h:43
WordEntryPos pos[FLEXIBLE_ARRAY_MEMBER]
Definition: ts_type.h:68
int32 size
Definition: ts_type.h:93
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
TSVectorData * TSVector
Definition: ts_type.h:98
#define DatumGetChar(X)
Definition: postgres.h:409
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define STRPTR(x)
Definition: hstore.h:76
int i
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define ARRPTR(x)
Definition: cube.c:24
#define WEP_GETWEIGHT(x)
Definition: ts_type.h:79
#define SHORTALIGN(LEN)
Definition: c.h:687
static const float weights[]
Definition: tsrank.c:24

◆ tsvector_length()

Datum tsvector_length ( PG_FUNCTION_ARGS  )

Definition at line 192 of file tsvector_op.c.

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

193 {
195  int32 ret = in->size;
196 
197  PG_FREE_IF_COPY(in, 0);
198  PG_RETURN_INT32(ret);
199 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:348
signed int int32
Definition: c.h:355
int32 size
Definition: ts_type.h:93
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120

◆ tsvector_setweight()

Datum tsvector_setweight ( PG_FUNCTION_ARGS  )

Definition at line 202 of file tsvector_op.c.

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

203 {
205  char cw = PG_GETARG_CHAR(1);
206  TSVector out;
207  int i,
208  j;
209  WordEntry *entry;
210  WordEntryPos *p;
211  int w = 0;
212 
213  switch (cw)
214  {
215  case 'A':
216  case 'a':
217  w = 3;
218  break;
219  case 'B':
220  case 'b':
221  w = 2;
222  break;
223  case 'C':
224  case 'c':
225  w = 1;
226  break;
227  case 'D':
228  case 'd':
229  w = 0;
230  break;
231  default:
232  /* internal error */
233  elog(ERROR, "unrecognized weight: %d", cw);
234  }
235 
236  out = (TSVector) palloc(VARSIZE(in));
237  memcpy(out, in, VARSIZE(in));
238  entry = ARRPTR(out);
239  i = out->size;
240  while (i--)
241  {
242  if ((j = POSDATALEN(out, entry)) != 0)
243  {
244  p = POSDATAPTR(out, entry);
245  while (j--)
246  {
247  WEP_SETWEIGHT(*p, w);
248  p++;
249  }
250  }
251  entry++;
252  }
253 
254  PG_FREE_IF_COPY(in, 0);
255  PG_RETURN_POINTER(out);
256 }
uint16 WordEntryPos
Definition: ts_type.h:63
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:355
#define POSDATALEN(x, e)
Definition: ts_type.h:110
#define VARSIZE(PTR)
Definition: postgres.h:303
#define POSDATAPTR(x, e)
Definition: ts_type.h:111
#define ERROR
Definition: elog.h:43
#define WEP_SETWEIGHT(x, v)
Definition: ts_type.h:82
int32 size
Definition: ts_type.h:93
TSVectorData * TSVector
Definition: ts_type.h:98
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
int i
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
#define ARRPTR(x)
Definition: cube.c:24
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:268

◆ tsvector_setweight_by_filter()

Datum tsvector_setweight_by_filter ( PG_FUNCTION_ARGS  )

Definition at line 264 of file tsvector_op.c.

References ARRPTR, deconstruct_array(), elog, ereport, errcode(), errmsg(), ERROR, i, 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.

265 {
266  TSVector tsin = PG_GETARG_TSVECTOR(0);
267  char char_weight = PG_GETARG_CHAR(1);
268  ArrayType *lexemes = PG_GETARG_ARRAYTYPE_P(2);
269 
270  TSVector tsout;
271  int i,
272  j,
273  nlexemes,
274  weight;
275  WordEntry *entry;
276  Datum *dlexemes;
277  bool *nulls;
278 
279  switch (char_weight)
280  {
281  case 'A':
282  case 'a':
283  weight = 3;
284  break;
285  case 'B':
286  case 'b':
287  weight = 2;
288  break;
289  case 'C':
290  case 'c':
291  weight = 1;
292  break;
293  case 'D':
294  case 'd':
295  weight = 0;
296  break;
297  default:
298  /* internal error */
299  elog(ERROR, "unrecognized weight: %c", char_weight);
300  }
301 
302  tsout = (TSVector) palloc(VARSIZE(tsin));
303  memcpy(tsout, tsin, VARSIZE(tsin));
304  entry = ARRPTR(tsout);
305 
306  deconstruct_array(lexemes, TEXTOID, -1, false, TYPALIGN_INT,
307  &dlexemes, &nulls, &nlexemes);
308 
309  /*
310  * Assuming that lexemes array is significantly shorter than tsvector we
311  * can iterate through lexemes performing binary search of each lexeme
312  * from lexemes in tsvector.
313  */
314  for (i = 0; i < nlexemes; i++)
315  {
316  char *lex;
317  int lex_len,
318  lex_pos;
319 
320  if (nulls[i])
321  ereport(ERROR,
322  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
323  errmsg("lexeme array may not contain nulls")));
324 
325  lex = VARDATA(dlexemes[i]);
326  lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
327  lex_pos = tsvector_bsearch(tsout, lex, lex_len);
328 
329  if (lex_pos >= 0 && (j = POSDATALEN(tsout, entry + lex_pos)) != 0)
330  {
331  WordEntryPos *p = POSDATAPTR(tsout, entry + lex_pos);
332 
333  while (j--)
334  {
335  WEP_SETWEIGHT(*p, weight);
336  p++;
337  }
338  }
339  }
340 
341  PG_FREE_IF_COPY(tsin, 0);
342  PG_FREE_IF_COPY(lexemes, 2);
343 
344  PG_RETURN_POINTER(tsout);
345 }
uint16 WordEntryPos
Definition: ts_type.h:63
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:355
#define VARDATA(PTR)
Definition: postgres.h:302
#define POSDATALEN(x, e)
Definition: ts_type.h:110
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:561
int errcode(int sqlerrcode)
Definition: elog.c:610
#define POSDATAPTR(x, e)
Definition: ts_type.h:111
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define WEP_SETWEIGHT(x, v)
Definition: ts_type.h:82
uintptr_t Datum
Definition: postgres.h:367
TSVectorData * TSVector
Definition: ts_type.h:98
#define ereport(elevel,...)
Definition: elog.h:144
static int tsvector_bsearch(const TSVector tsv, char *lexeme, int lexeme_len)
Definition: tsvector_op.c:393
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
int i
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
#define ARRPTR(x)
Definition: cube.c:24
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:268

◆ tsvector_strip()

Datum tsvector_strip ( PG_FUNCTION_ARGS  )

Definition at line 159 of file tsvector_op.c.

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

160 {
162  TSVector out;
163  int i,
164  len = 0;
165  WordEntry *arrin = ARRPTR(in),
166  *arrout;
167  char *cur;
168 
169  for (i = 0; i < in->size; i++)
170  len += arrin[i].len;
171 
172  len = CALCDATASIZE(in->size, len);
173  out = (TSVector) palloc0(len);
174  SET_VARSIZE(out, len);
175  out->size = in->size;
176  arrout = ARRPTR(out);
177  cur = STRPTR(out);
178  for (i = 0; i < in->size; i++)
179  {
180  memcpy(cur, STRPTR(in) + arrin[i].pos, arrin[i].len);
181  arrout[i].haspos = 0;
182  arrout[i].len = arrin[i].len;
183  arrout[i].pos = cur - STRPTR(out);
184  cur += arrout[i].len;
185  }
186 
187  PG_FREE_IF_COPY(in, 0);
188  PG_RETURN_POINTER(out);
189 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:355
uint32 len
Definition: ts_type.h:44
struct cursor * cur
Definition: ecpg.c:28
int32 size
Definition: ts_type.h:93
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
void * palloc0(Size size)
Definition: mcxt.c:980
TSVectorData * TSVector
Definition: ts_type.h:98
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define STRPTR(x)
Definition: hstore.h:76
int i
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define ARRPTR(x)
Definition: cube.c:24

◆ tsvector_to_array()

Datum tsvector_to_array ( PG_FUNCTION_ARGS  )

Definition at line 717 of file tsvector_op.c.

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

718 {
719  TSVector tsin = PG_GETARG_TSVECTOR(0);
720  WordEntry *arrin = ARRPTR(tsin);
721  Datum *elements;
722  int i;
723  ArrayType *array;
724 
725  elements = palloc(tsin->size * sizeof(Datum));
726 
727  for (i = 0; i < tsin->size; i++)
728  {
729  elements[i] = PointerGetDatum(cstring_to_text_with_len(STRPTR(tsin) + arrin[i].pos,
730  arrin[i].len));
731  }
732 
733  array = construct_array(elements, tsin->size, TEXTOID, -1, false, TYPALIGN_INT);
734 
735  pfree(elements);
736  PG_FREE_IF_COPY(tsin, 0);
737  PG_RETURN_POINTER(array);
738 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:355
#define PointerGetDatum(X)
Definition: postgres.h:556
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
void pfree(void *pointer)
Definition: mcxt.c:1056
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:184
int32 size
Definition: ts_type.h:93
uintptr_t Datum
Definition: postgres.h:367
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
void * palloc(Size size)
Definition: mcxt.c:949
#define STRPTR(x)
Definition: hstore.h:76
int i
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
#define ARRPTR(x)
Definition: cube.c:24

◆ tsvector_unnest()

Datum tsvector_unnest ( PG_FUNCTION_ARGS  )

Definition at line 629 of file tsvector_op.c.

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

630 {
631  FuncCallContext *funcctx;
632  TSVector tsin;
633 
634  if (SRF_IS_FIRSTCALL())
635  {
636  MemoryContext oldcontext;
637  TupleDesc tupdesc;
638 
639  funcctx = SRF_FIRSTCALL_INIT();
640  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
641 
642  tupdesc = CreateTemplateTupleDesc(3);
643  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "lexeme",
644  TEXTOID, -1, 0);
645  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "positions",
646  INT2ARRAYOID, -1, 0);
647  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "weights",
648  TEXTARRAYOID, -1, 0);
649  funcctx->tuple_desc = BlessTupleDesc(tupdesc);
650 
651  funcctx->user_fctx = PG_GETARG_TSVECTOR_COPY(0);
652 
653  MemoryContextSwitchTo(oldcontext);
654  }
655 
656  funcctx = SRF_PERCALL_SETUP();
657  tsin = (TSVector) funcctx->user_fctx;
658 
659  if (funcctx->call_cntr < tsin->size)
660  {
661  WordEntry *arrin = ARRPTR(tsin);
662  char *data = STRPTR(tsin);
663  HeapTuple tuple;
664  int j,
665  i = funcctx->call_cntr;
666  bool nulls[] = {false, false, false};
667  Datum values[3];
668 
669  values[0] = PointerGetDatum(cstring_to_text_with_len(data + arrin[i].pos, arrin[i].len));
670 
671  if (arrin[i].haspos)
672  {
673  WordEntryPosVector *posv;
674  Datum *positions;
675  Datum *weights;
676  char weight;
677 
678  /*
679  * Internally tsvector stores position and weight in the same
680  * uint16 (2 bits for weight, 14 for position). Here we extract
681  * that in two separate arrays.
682  */
683  posv = _POSVECPTR(tsin, arrin + i);
684  positions = palloc(posv->npos * sizeof(Datum));
685  weights = palloc(posv->npos * sizeof(Datum));
686  for (j = 0; j < posv->npos; j++)
687  {
688  positions[j] = Int16GetDatum(WEP_GETPOS(posv->pos[j]));
689  weight = 'D' - WEP_GETWEIGHT(posv->pos[j]);
690  weights[j] = PointerGetDatum(cstring_to_text_with_len(&weight,
691  1));
692  }
693 
694  values[1] = PointerGetDatum(construct_array(positions, posv->npos,
695  INT2OID, 2, true, TYPALIGN_SHORT));
696  values[2] = PointerGetDatum(construct_array(weights, posv->npos,
697  TEXTOID, -1, false, TYPALIGN_INT));
698  }
699  else
700  {
701  nulls[1] = nulls[2] = true;
702  }
703 
704  tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
705  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
706  }
707  else
708  {
709  SRF_RETURN_DONE(funcctx);
710  }
711 }
uint64 call_cntr
Definition: funcapi.h:65
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define PointerGetDatum(X)
Definition: postgres.h:556
#define _POSVECPTR(x, e)
Definition: ts_type.h:109
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define Int16GetDatum(X)
Definition: postgres.h:451
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
TupleDesc tuple_desc
Definition: funcapi.h:112
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define WEP_GETPOS(x)
Definition: ts_type.h:80
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
WordEntryPos pos[FLEXIBLE_ARRAY_MEMBER]
Definition: ts_type.h:68
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:184
int32 size
Definition: ts_type.h:93
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
uintptr_t Datum
Definition: postgres.h:367
TSVectorData * TSVector
Definition: ts_type.h:98
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
#define STRPTR(x)
Definition: hstore.h:76
int i
#define PG_GETARG_TSVECTOR_COPY(n)
Definition: ts_type.h:121
#define ARRPTR(x)
Definition: cube.c:24
#define WEP_GETWEIGHT(x)
Definition: ts_type.h:79
int16 AttrNumber
Definition: attnum.h:21
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
static const float weights[]
Definition: tsrank.c:24
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ tsvector_update_trigger()

static Datum tsvector_update_trigger ( PG_FUNCTION_ARGS  ,
bool  config_column 
)
static

Definition at line 2405 of file tsvector_op.c.

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, 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().

2406 {
2407  TriggerData *trigdata;
2408  Trigger *trigger;
2409  Relation rel;
2410  HeapTuple rettuple = NULL;
2411  int tsvector_attr_num,
2412  i;
2413  ParsedText prs;
2414  Datum datum;
2415  bool isnull;
2416  text *txt;
2417  Oid cfgId;
2418  bool update_needed;
2419 
2420  /* Check call context */
2421  if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
2422  elog(ERROR, "tsvector_update_trigger: not fired by trigger manager");
2423 
2424  trigdata = (TriggerData *) fcinfo->context;
2425  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
2426  elog(ERROR, "tsvector_update_trigger: must be fired for row");
2427  if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
2428  elog(ERROR, "tsvector_update_trigger: must be fired BEFORE event");
2429 
2430  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
2431  {
2432  rettuple = trigdata->tg_trigtuple;
2433  update_needed = true;
2434  }
2435  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
2436  {
2437  rettuple = trigdata->tg_newtuple;
2438  update_needed = false; /* computed below */
2439  }
2440  else
2441  elog(ERROR, "tsvector_update_trigger: must be fired for INSERT or UPDATE");
2442 
2443  trigger = trigdata->tg_trigger;
2444  rel = trigdata->tg_relation;
2445 
2446  if (trigger->tgnargs < 3)
2447  elog(ERROR, "tsvector_update_trigger: arguments must be tsvector_field, ts_config, text_field1, ...)");
2448 
2449  /* Find the target tsvector column */
2450  tsvector_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
2451  if (tsvector_attr_num == SPI_ERROR_NOATTRIBUTE)
2452  ereport(ERROR,
2453  (errcode(ERRCODE_UNDEFINED_COLUMN),
2454  errmsg("tsvector column \"%s\" does not exist",
2455  trigger->tgargs[0])));
2456  /* This will effectively reject system columns, so no separate test: */
2457  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, tsvector_attr_num),
2458  TSVECTOROID))
2459  ereport(ERROR,
2460  (errcode(ERRCODE_DATATYPE_MISMATCH),
2461  errmsg("column \"%s\" is not of tsvector type",
2462  trigger->tgargs[0])));
2463 
2464  /* Find the configuration to use */
2465  if (config_column)
2466  {
2467  int config_attr_num;
2468 
2469  config_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[1]);
2470  if (config_attr_num == SPI_ERROR_NOATTRIBUTE)
2471  ereport(ERROR,
2472  (errcode(ERRCODE_UNDEFINED_COLUMN),
2473  errmsg("configuration column \"%s\" does not exist",
2474  trigger->tgargs[1])));
2475  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, config_attr_num),
2476  REGCONFIGOID))
2477  ereport(ERROR,
2478  (errcode(ERRCODE_DATATYPE_MISMATCH),
2479  errmsg("column \"%s\" is not of regconfig type",
2480  trigger->tgargs[1])));
2481 
2482  datum = SPI_getbinval(rettuple, rel->rd_att, config_attr_num, &isnull);
2483  if (isnull)
2484  ereport(ERROR,
2485  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2486  errmsg("configuration column \"%s\" must not be null",
2487  trigger->tgargs[1])));
2488  cfgId = DatumGetObjectId(datum);
2489  }
2490  else
2491  {
2492  List *names;
2493 
2494  names = stringToQualifiedNameList(trigger->tgargs[1]);
2495  /* require a schema so that results are not search path dependent */
2496  if (list_length(names) < 2)
2497  ereport(ERROR,
2498  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2499  errmsg("text search configuration name \"%s\" must be schema-qualified",
2500  trigger->tgargs[1])));
2501  cfgId = get_ts_config_oid(names, false);
2502  }
2503 
2504  /* initialize parse state */
2505  prs.lenwords = 32;
2506  prs.curwords = 0;
2507  prs.pos = 0;
2508  prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
2509 
2510  /* find all words in indexable column(s) */
2511  for (i = 2; i < trigger->tgnargs; i++)
2512  {
2513  int numattr;
2514 
2515  numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
2516  if (numattr == SPI_ERROR_NOATTRIBUTE)
2517  ereport(ERROR,
2518  (errcode(ERRCODE_UNDEFINED_COLUMN),
2519  errmsg("column \"%s\" does not exist",
2520  trigger->tgargs[i])));
2521  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, numattr), TEXTOID))
2522  ereport(ERROR,
2523  (errcode(ERRCODE_DATATYPE_MISMATCH),
2524  errmsg("column \"%s\" is not of a character type",
2525  trigger->tgargs[i])));
2526 
2528  update_needed = true;
2529 
2530  datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
2531  if (isnull)
2532  continue;
2533 
2534  txt = DatumGetTextPP(datum);
2535 
2536  parsetext(cfgId, &prs, VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
2537 
2538  if (txt != (text *) DatumGetPointer(datum))
2539  pfree(txt);
2540  }
2541 
2542  if (update_needed)
2543  {
2544  /* make tsvector value */
2545  datum = TSVectorGetDatum(make_tsvector(&prs));
2546  isnull = false;
2547 
2548  /* and insert it into tuple */
2549  rettuple = heap_modify_tuple_by_cols(rettuple, rel->rd_att,
2550  1, &tsvector_attr_num,
2551  &datum, &isnull);
2552 
2553  pfree(DatumGetPointer(datum));
2554  }
2555 
2556  return PointerGetDatum(rettuple);
2557 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:951
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1084
TSVector make_tsvector(ParsedText *prs)
Definition: to_tsany.c:156
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int numattr
Definition: bootstrap.c:77
#define PointerGetDatum(X)
Definition: postgres.h:556
#define DatumGetObjectId(X)
Definition: postgres.h:500
const Bitmapset * tg_updatedcols
Definition: trigger.h:42
#define DatumGetTextPP(X)
Definition: fmgr.h:286
int errcode(int sqlerrcode)
Definition: elog.c:610
Oid get_ts_config_oid(List *names, bool missing_ok)
Definition: namespace.c:2679
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
int32 lenwords
Definition: ts_utils.h:101
unsigned int Oid
Definition: postgres_ext.h:31
int32 curwords
Definition: ts_utils.h:102
HeapTuple tg_trigtuple
Definition: trigger.h:35
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
Definition: ts_parse.c:354
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1028
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
ParsedWord * words
Definition: ts_utils.h:100
#define TSVectorGetDatum(X)
Definition: ts_type.h:119
bool IsBinaryCoercible(Oid srctype, Oid targettype)
char ** tgargs
Definition: reltrigger.h:41
uintptr_t Datum
Definition: postgres.h:367
Trigger * tg_trigger
Definition: trigger.h:37
TupleDesc rd_att
Definition: rel.h:110
HeapTuple tg_newtuple
Definition: trigger.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
TriggerEvent tg_event
Definition: trigger.h:33
int32 pos
Definition: ts_utils.h:103
static int list_length(const List *l)
Definition: pg_list.h:169
HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple, TupleDesc tupleDesc, int nCols, int *replCols, Datum *replValues, bool *replIsnull)
Definition: heaptuple.c:1181
#define DatumGetPointer(X)
Definition: postgres.h:549
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:135
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1839
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:117
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
int i
int16 tgnargs
Definition: reltrigger.h:38
Definition: c.h:555
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:129
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:123
Relation tg_relation
Definition: trigger.h:34

◆ tsvector_update_trigger_bycolumn()

Datum tsvector_update_trigger_bycolumn ( PG_FUNCTION_ARGS  )

Definition at line 2399 of file tsvector_op.c.

References tsvector_update_trigger().

2400 {
2401  return tsvector_update_trigger(fcinfo, true);
2402 }
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
Definition: tsvector_op.c:2405

◆ tsvector_update_trigger_byid()

Datum tsvector_update_trigger_byid ( PG_FUNCTION_ARGS  )

Definition at line 2393 of file tsvector_op.c.

References tsvector_update_trigger().

2394 {
2395  return tsvector_update_trigger(fcinfo, false);
2396 }
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
Definition: tsvector_op.c:2405

◆ TSVECTORCMPFUNC() [1/7]

TSVECTORCMPFUNC ( lt  )

◆ TSVECTORCMPFUNC() [2/7]

TSVECTORCMPFUNC ( le  ,
<=  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [3/7]

TSVECTORCMPFUNC ( eq  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [4/7]

TSVECTORCMPFUNC ( ge  ,
>=  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [5/7]

TSVECTORCMPFUNC ( gt  ,
BOOL   
)

◆ TSVECTORCMPFUNC() [6/7]

TSVECTORCMPFUNC ( ne  ,
,
BOOL   
)

◆ TSVECTORCMPFUNC() [7/7]

TSVECTORCMPFUNC ( cmp  ,
,
INT32   
)

◆ walkStatEntryTree()

static StatEntry* walkStatEntryTree ( TSVectorStat stat)
static

Definition at line 2155 of file tsvector_op.c.

References Assert, StatEntry::left, TSVectorStat::maxdepth, StatEntry::ndoc, StatEntry::right, TSVectorStat::stack, and TSVectorStat::stackpos.

Referenced by ts_process_call().

2156 {
2157  StatEntry *node = stat->stack[stat->stackpos];
2158 
2159  if (node == NULL)
2160  return NULL;
2161 
2162  if (node->ndoc != 0)
2163  {
2164  /* return entry itself: we already was at left sublink */
2165  return node;
2166  }
2167  else if (node->right && node->right != stat->stack[stat->stackpos + 1])
2168  {
2169  /* go on right sublink */
2170  stat->stackpos++;
2171  node = node->right;
2172 
2173  /* find most-left value */
2174  for (;;)
2175  {
2176  stat->stack[stat->stackpos] = node;
2177  if (node->left)
2178  {
2179  stat->stackpos++;
2180  node = node->left;
2181  }
2182  else
2183  break;
2184  }
2185  Assert(stat->stackpos <= stat->maxdepth);
2186  }
2187  else
2188  {
2189  /* we already return all left subtree, itself and right subtree */
2190  if (stat->stackpos == 0)
2191  return NULL;
2192 
2193  stat->stackpos--;
2194  return walkStatEntryTree(stat);
2195  }
2196 
2197  return node;
2198 }
uint32 stackpos
Definition: tsvector_op.c:65
uint32 ndoc
Definition: tsvector_op.c:47
static StatEntry * walkStatEntryTree(TSVectorStat *stat)
Definition: tsvector_op.c:2155
StatEntry ** stack
Definition: tsvector_op.c:64
#define Assert(condition)
Definition: c.h:738
uint32 maxdepth
Definition: tsvector_op.c:62
struct StatEntry * left
Definition: tsvector_op.c:50
struct StatEntry * right
Definition: tsvector_op.c:51