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

Go to the source code of this file.

Data Structures

struct  CHKVAL
 
struct  StatEntry
 
struct  TSVectorStat
 

Macros

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

Typedefs

typedef struct StatEntry StatEntry
 

Functions

static TSTernaryValue TS_execute_recurse (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
 
static int tsvector_bsearch (const TSVector tsv, char *lexeme, int lexeme_len)
 
static Datum tsvector_update_trigger (PG_FUNCTION_ARGS, bool config_column)
 
static int silly_cmp_tsvector (const TSVector a, const TSVector b)
 
 TSVECTORCMPFUNC (lt,<, BOOL)
 
 TSVECTORCMPFUNC (le,<=, BOOL)
 
 TSVECTORCMPFUNC (eq,==, BOOL)
 
 TSVECTORCMPFUNC (ge, >=, BOOL)
 
 TSVECTORCMPFUNC (gt, >, BOOL)
 
 TSVECTORCMPFUNC (ne, !=, BOOL)
 
 TSVECTORCMPFUNC (cmp,+, INT32)
 
Datum tsvector_strip (PG_FUNCTION_ARGS)
 
Datum tsvector_length (PG_FUNCTION_ARGS)
 
Datum tsvector_setweight (PG_FUNCTION_ARGS)
 
Datum tsvector_setweight_by_filter (PG_FUNCTION_ARGS)
 
static int32 add_pos (TSVector src, WordEntry *srcptr, TSVector dest, WordEntry *destptr, int32 maxpos)
 
static int compare_int (const void *va, const void *vb)
 
static int compare_text_lexemes (const void *va, const void *vb)
 
static TSVector tsvector_delete_by_indices (TSVector tsv, int *indices_to_delete, int indices_count)
 
Datum tsvector_delete_str (PG_FUNCTION_ARGS)
 
Datum tsvector_delete_arr (PG_FUNCTION_ARGS)
 
Datum tsvector_unnest (PG_FUNCTION_ARGS)
 
Datum tsvector_to_array (PG_FUNCTION_ARGS)
 
Datum array_to_tsvector (PG_FUNCTION_ARGS)
 
Datum tsvector_filter (PG_FUNCTION_ARGS)
 
Datum tsvector_concat (PG_FUNCTION_ARGS)
 
int32 tsCompareString (char *a, int lena, char *b, int lenb, bool prefix)
 
static TSTernaryValue checkclass_str (CHKVAL *chkval, WordEntry *entry, QueryOperand *val, ExecPhraseData *data)
 
static TSTernaryValue checkcondition_str (void *checkval, QueryOperand *val, ExecPhraseData *data)
 
static TSTernaryValue TS_phrase_output (ExecPhraseData *data, ExecPhraseData *Ldata, ExecPhraseData *Rdata, int emit, int Loffset, int Roffset, int max_npos)
 
static TSTernaryValue TS_phrase_execute (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond, ExecPhraseData *data)
 
bool TS_execute (QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
 
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:1147

Definition at line 352 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:1147
e
Definition: preproc-init.c:82
#define STRPTR(x)
Definition: hstore.h:76

Definition at line 2119 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 1460 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 1458 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 1459 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:82
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193

Definition at line 141 of file tsvector_op.c.

Typedef Documentation

◆ StatEntry

typedef struct StatEntry StatEntry

Function Documentation

◆ add_pos()

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

Definition at line 362 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().

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

750 {
752  TSVector tsout;
753  Datum *dlexemes;
754  WordEntry *arrout;
755  bool *nulls;
756  int nitems,
757  i,
758  tslen,
759  datalen = 0;
760  char *cur;
761 
762  deconstruct_array(v, TEXTOID, -1, false, TYPALIGN_INT, &dlexemes, &nulls, &nitems);
763 
764  /* Reject nulls (maybe we should just ignore them, instead?) */
765  for (i = 0; i < nitems; i++)
766  {
767  if (nulls[i])
768  ereport(ERROR,
769  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
770  errmsg("lexeme array may not contain nulls")));
771  }
772 
773  /* Sort and de-dup, because this is required for a valid tsvector. */
774  if (nitems > 1)
775  {
776  qsort(dlexemes, nitems, sizeof(Datum), compare_text_lexemes);
777  nitems = qunique(dlexemes, nitems, sizeof(Datum),
779  }
780 
781  /* Calculate space needed for surviving lexemes. */
782  for (i = 0; i < nitems; i++)
783  datalen += VARSIZE(dlexemes[i]) - VARHDRSZ;
784  tslen = CALCDATASIZE(nitems, datalen);
785 
786  /* Allocate and fill tsvector. */
787  tsout = (TSVector) palloc0(tslen);
788  SET_VARSIZE(tsout, tslen);
789  tsout->size = nitems;
790 
791  arrout = ARRPTR(tsout);
792  cur = STRPTR(tsout);
793  for (i = 0; i < nitems; i++)
794  {
795  char *lex = VARDATA(dlexemes[i]);
796  int lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
797 
798  memcpy(cur, lex, lex_len);
799  arrout[i].haspos = 0;
800  arrout[i].len = lex_len;
801  arrout[i].pos = cur - STRPTR(tsout);
802  cur += lex_len;
803  }
804 
805  PG_FREE_IF_COPY(v, 0);
806  PG_RETURN_POINTER(tsout);
807 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:568
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:981
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:442
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:260
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:3483
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:475

◆ check_weight()

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

Definition at line 2104 of file tsvector_op.c.

References POSDATALEN, POSDATAPTR, and WEP_GETWEIGHT.

Referenced by insertStatEntry().

2105 {
2106  int len = POSDATALEN(txt, wptr);
2107  int num = 0;
2108  WordEntryPos *ptr = POSDATAPTR(txt, wptr);
2109 
2110  while (len--)
2111  {
2112  if (weight & (1 << WEP_GETWEIGHT(*ptr)))
2113  num++;
2114  ptr++;
2115  }
2116  return num;
2117 }
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 TSTernaryValue checkclass_str ( CHKVAL chkval,
WordEntry entry,
QueryOperand val,
ExecPhraseData data 
)
static

Definition at line 1184 of file tsvector_op.c.

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

Referenced by checkcondition_str().

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

◆ checkcondition_str()

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

Definition at line 1290 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(), pfree(), WordEntry::pos, ExecPhraseData::pos, QueryOperand::prefix, qsort, qunique(), repalloc(), TS_MAYBE, TS_NO, TS_YES, tsCompareString(), and CHKVAL::values.

Referenced by ts_match_vq().

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

◆ chooseNextStatEntry()

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

Definition at line 2191 of file tsvector_op.c.

References insertStatEntry().

Referenced by ts_accum().

2193 {
2194  uint32 pos;
2195  uint32 middle = (low + high) >> 1;
2196 
2197  pos = (low + middle) >> 1;
2198  if (low != middle && pos >= offset && pos - offset < txt->size)
2199  insertStatEntry(persistentContext, stat, txt, pos - offset);
2200  pos = (high + middle + 1) >> 1;
2201  if (middle + 1 != high && pos >= offset && pos - offset < txt->size)
2202  insertStatEntry(persistentContext, stat, txt, pos - offset);
2203 
2204  if (low != middle)
2205  chooseNextStatEntry(persistentContext, stat, txt, low, middle, offset);
2206  if (high != middle + 1)
2207  chooseNextStatEntry(persistentContext, stat, txt, middle + 1, high, offset);
2208 }
static void chooseNextStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 low, uint32 high, uint32 offset)
Definition: tsvector_op.c:2191
unsigned int uint32
Definition: c.h:374
static void insertStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 off)
Definition: tsvector_op.c:2125

◆ compare_int()

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

Definition at line 431 of file tsvector_op.c.

Referenced by tsvector_delete_by_indices().

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

◆ compare_text_lexemes()

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

Definition at line 442 of file tsvector_op.c.

References tsCompareString(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by array_to_tsvector().

443 {
444  Datum a = *((const Datum *) va);
445  Datum b = *((const Datum *) vb);
446  char *alex = VARDATA_ANY(a);
447  int alex_len = VARSIZE_ANY_EXHDR(a);
448  char *blex = VARDATA_ANY(b);
449  int blex_len = VARSIZE_ANY_EXHDR(b);
450 
451  return tsCompareString(alex, alex_len, blex, blex_len, false);
452 }
#define VARDATA_ANY(PTR)
Definition: 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:1147
#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 2125 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().

2126 {
2127  WordEntry *we = ARRPTR(txt) + off;
2128  StatEntry *node = stat->root,
2129  *pnode = NULL;
2130  int n,
2131  res = 0;
2132  uint32 depth = 1;
2133 
2134  if (stat->weight == 0)
2135  n = (we->haspos) ? POSDATALEN(txt, we) : 1;
2136  else
2137  n = (we->haspos) ? check_weight(txt, we, stat->weight) : 0;
2138 
2139  if (n == 0)
2140  return; /* nothing to insert */
2141 
2142  while (node)
2143  {
2144  res = compareStatWord(node, we, txt);
2145 
2146  if (res == 0)
2147  {
2148  break;
2149  }
2150  else
2151  {
2152  pnode = node;
2153  node = (res < 0) ? node->left : node->right;
2154  }
2155  depth++;
2156  }
2157 
2158  if (depth > stat->maxdepth)
2159  stat->maxdepth = depth;
2160 
2161  if (node == NULL)
2162  {
2163  node = MemoryContextAlloc(persistentContext, STATENTRYHDRSZ + we->len);
2164  node->left = node->right = NULL;
2165  node->ndoc = 1;
2166  node->nentry = n;
2167  node->lenlexeme = we->len;
2168  memcpy(node->lexeme, STRPTR(txt) + we->pos, node->lenlexeme);
2169 
2170  if (pnode == NULL)
2171  {
2172  stat->root = node;
2173  }
2174  else
2175  {
2176  if (res < 0)
2177  pnode->left = node;
2178  else
2179  pnode->right = node;
2180  }
2181 
2182  }
2183  else
2184  {
2185  node->ndoc++;
2186  node->nentry += n;
2187  }
2188 }
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:374
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:2104
uint32 pos
Definition: ts_type.h:44
#define STATENTRYHDRSZ
Definition: tsvector_op.c:56
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define STRPTR(x)
Definition: hstore.h:76
#define ARRPTR(x)
Definition: cube.c:24
#define compareStatWord(a, e, t)
Definition: tsvector_op.c:2119
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 82 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.

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

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

Referenced by ts_stat_sql().

2224 {
2225  TSVector txt = DatumGetTSVector(data);
2226  uint32 i,
2227  nbit = 0,
2228  offset;
2229 
2230  if (stat == NULL)
2231  { /* Init in first */
2232  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2233  stat->maxdepth = 1;
2234  }
2235 
2236  /* simple check of correctness */
2237  if (txt == NULL || txt->size == 0)
2238  {
2239  if (txt && txt != (TSVector) DatumGetPointer(data))
2240  pfree(txt);
2241  return stat;
2242  }
2243 
2244  i = txt->size - 1;
2245  for (; i > 0; i >>= 1)
2246  nbit++;
2247 
2248  nbit = 1 << nbit;
2249  offset = (nbit - txt->size) / 2;
2250 
2251  insertStatEntry(persistentContext, stat, txt, (nbit >> 1) - offset);
2252  chooseNextStatEntry(persistentContext, stat, txt, 0, nbit, offset);
2253 
2254  return stat;
2255 }
static void chooseNextStatEntry(MemoryContext persistentContext, TSVectorStat *stat, TSVector txt, uint32 low, uint32 high, uint32 offset)
Definition: tsvector_op.c:2191
void pfree(void *pointer)
Definition: mcxt.c:1057
int32 size
Definition: ts_type.h:93
unsigned int uint32
Definition: c.h:374
#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:2125
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:840
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 1846 of file tsvector_op.c.

References TS_execute_recurse(), and TS_NO.

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

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

◆ TS_execute_recurse()

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

Definition at line 1863 of file tsvector_op.c.

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

Referenced by TS_execute().

1865 {
1866  TSTernaryValue lmatch;
1867 
1868  /* since this function recurses, it could be driven to stack overflow */
1870 
1871  /* ... and let's check for query cancel while we're at it */
1873 
1874  if (curitem->type == QI_VAL)
1875  return chkcond(arg, (QueryOperand *) curitem,
1876  NULL /* don't need position info */ );
1877 
1878  switch (curitem->qoperator.oper)
1879  {
1880  case OP_NOT:
1881  if (flags & TS_EXEC_SKIP_NOT)
1882  return TS_YES;
1883  switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1884  {
1885  case TS_NO:
1886  return TS_YES;
1887  case TS_YES:
1888  return TS_NO;
1889  case TS_MAYBE:
1890  return TS_MAYBE;
1891  }
1892  break;
1893 
1894  case OP_AND:
1895  lmatch = TS_execute_recurse(curitem + curitem->qoperator.left, arg,
1896  flags, chkcond);
1897  if (lmatch == TS_NO)
1898  return TS_NO;
1899  switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1900  {
1901  case TS_NO:
1902  return TS_NO;
1903  case TS_YES:
1904  return lmatch;
1905  case TS_MAYBE:
1906  return TS_MAYBE;
1907  }
1908  break;
1909 
1910  case OP_OR:
1911  lmatch = TS_execute_recurse(curitem + curitem->qoperator.left, arg,
1912  flags, chkcond);
1913  if (lmatch == TS_YES)
1914  return TS_YES;
1915  switch (TS_execute_recurse(curitem + 1, arg, flags, chkcond))
1916  {
1917  case TS_NO:
1918  return lmatch;
1919  case TS_YES:
1920  return TS_YES;
1921  case TS_MAYBE:
1922  return TS_MAYBE;
1923  }
1924  break;
1925 
1926  case OP_PHRASE:
1927 
1928  /*
1929  * If we get a MAYBE result, and the caller doesn't want that,
1930  * convert it to NO. It would be more consistent, perhaps, to
1931  * return the result of TS_phrase_execute() verbatim and then
1932  * convert MAYBE results at the top of the recursion. But
1933  * converting at the topmost phrase operator gives results that
1934  * are bug-compatible with the old implementation, so do it like
1935  * this for now.
1936  */
1937  switch (TS_phrase_execute(curitem, arg, flags, chkcond, NULL))
1938  {
1939  case TS_NO:
1940  return TS_NO;
1941  case TS_YES:
1942  return TS_YES;
1943  case TS_MAYBE:
1944  return (flags & TS_EXEC_PHRASE_NO_POS) ? TS_MAYBE : TS_NO;
1945  }
1946  break;
1947 
1948  default:
1949  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
1950  }
1951 
1952  /* not reachable, but keep compiler quiet */
1953  return TS_NO;
1954 }
QueryOperator qoperator
Definition: ts_type.h:196
static TSTernaryValue TS_execute_recurse(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1863
#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
#define TS_EXEC_SKIP_NOT
Definition: ts_utils.h:191
QueryItemType type
Definition: ts_type.h:195
#define OP_PHRASE
Definition: ts_type.h:169
#define TS_EXEC_PHRASE_NO_POS
Definition: ts_utils.h:198
uint32 left
Definition: ts_type.h:184
static TSTernaryValue TS_phrase_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond, ExecPhraseData *data)
Definition: tsvector_op.c:1604
#define elog(elevel,...)
Definition: elog.h:214
void * arg
TSTernaryValue
Definition: ts_utils.h:128
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define OP_NOT
Definition: ts_type.h:166

◆ ts_match_qv()

Datum ts_match_qv ( PG_FUNCTION_ARGS  )

Definition at line 2015 of file tsvector_op.c.

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

2016 {
2018  PG_GETARG_DATUM(1),
2019  PG_GETARG_DATUM(0)));
2020 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum ts_match_vq(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:2023
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ ts_match_tq()

Datum ts_match_tq ( PG_FUNCTION_ARGS  )

Definition at line 2075 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.

2076 {
2077  TSVector vector;
2078  TSQuery query = PG_GETARG_TSQUERY(1);
2079  bool res;
2080 
2082  PG_GETARG_DATUM(0)));
2083 
2085  TSVectorGetDatum(vector),
2086  TSQueryGetDatum(query)));
2087 
2088  pfree(vector);
2089  PG_FREE_IF_COPY(query, 1);
2090 
2091  PG_RETURN_BOOL(res);
2092 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
void pfree(void *pointer)
Definition: mcxt.c:1057
Datum ts_match_vq(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:2023
#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:358
Datum to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:259
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ ts_match_tt()

Datum ts_match_tt ( PG_FUNCTION_ARGS  )

Definition at line 2053 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.

2054 {
2055  TSVector vector;
2056  TSQuery query;
2057  bool res;
2058 
2060  PG_GETARG_DATUM(0)));
2062  PG_GETARG_DATUM(1)));
2063 
2065  TSVectorGetDatum(vector),
2066  TSQueryGetDatum(query)));
2067 
2068  pfree(vector);
2069  pfree(query);
2070 
2071  PG_RETURN_BOOL(res);
2072 }
#define DatumGetTSQuery(X)
Definition: ts_type.h:235
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum plainto_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:617
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
void pfree(void *pointer)
Definition: mcxt.c:1057
Datum ts_match_vq(PG_FUNCTION_ARGS)
Definition: tsvector_op.c:2023
#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:358
Datum to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:259
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ ts_match_vq()

Datum ts_match_vq ( PG_FUNCTION_ARGS  )

Definition at line 2023 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_EMPTY, TS_execute(), val, and CHKVAL::values.

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

2024 {
2026  TSQuery query = PG_GETARG_TSQUERY(1);
2027  CHKVAL chkval;
2028  bool result;
2029 
2030  /* empty query matches nothing */
2031  if (!query->size)
2032  {
2033  PG_FREE_IF_COPY(val, 0);
2034  PG_FREE_IF_COPY(query, 1);
2035  PG_RETURN_BOOL(false);
2036  }
2037 
2038  chkval.arrb = ARRPTR(val);
2039  chkval.arre = chkval.arrb + val->size;
2040  chkval.values = STRPTR(val);
2041  chkval.operand = GETOPERAND(query);
2042  result = TS_execute(GETQUERY(query),
2043  &chkval,
2044  TS_EXEC_EMPTY,
2046 
2047  PG_FREE_IF_COPY(val, 0);
2048  PG_FREE_IF_COPY(query, 1);
2049  PG_RETURN_BOOL(result);
2050 }
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1846
#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
char * operand
Definition: ltxtquery_op.c:52
int32 size
Definition: ts_type.h:93
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static TSTernaryValue checkcondition_str(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsvector_op.c:1290
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
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
#define TS_EXEC_EMPTY
Definition: ts_utils.h:184
long val
Definition: informix.c:664

◆ TS_phrase_execute()

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

Definition at line 1604 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, TS_EXEC_SKIP_NOT, TS_MAYBE, TS_NO, TS_phrase_output(), TS_YES, TSPO_BOTH, TSPO_L_ONLY, TSPO_R_ONLY, QueryItem::type, and ExecPhraseData::width.

Referenced by TS_execute_recurse().

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

◆ TS_phrase_output()

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

Definition at line 1463 of file tsvector_op.c.

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

Referenced by TS_phrase_execute().

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

◆ ts_process_call()

static Datum ts_process_call ( FuncCallContext funcctx)
static

Definition at line 2350 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().

2351 {
2352  TSVectorStat *st;
2353  StatEntry *entry;
2354 
2355  st = (TSVectorStat *) funcctx->user_fctx;
2356 
2357  entry = walkStatEntryTree(st);
2358 
2359  if (entry != NULL)
2360  {
2361  Datum result;
2362  char *values[3];
2363  char ndoc[16];
2364  char nentry[16];
2365  HeapTuple tuple;
2366 
2367  values[0] = palloc(entry->lenlexeme + 1);
2368  memcpy(values[0], entry->lexeme, entry->lenlexeme);
2369  (values[0])[entry->lenlexeme] = '\0';
2370  sprintf(ndoc, "%d", entry->ndoc);
2371  values[1] = ndoc;
2372  sprintf(nentry, "%d", entry->nentry);
2373  values[2] = nentry;
2374 
2375  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
2376  result = HeapTupleGetDatum(tuple);
2377 
2378  pfree(values[0]);
2379 
2380  /* mark entry as already visited */
2381  entry->ndoc = 0;
2382 
2383  return result;
2384  }
2385 
2386  return (Datum) 0;
2387 }
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:1057
static StatEntry * walkStatEntryTree(TSVectorStat *stat)
Definition: tsvector_op.c:2304
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:165
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:950
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 2258 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().

2260 {
2261  TupleDesc tupdesc;
2262  MemoryContext oldcontext;
2263  StatEntry *node;
2264 
2265  funcctx->user_fctx = (void *) stat;
2266 
2267  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2268 
2269  stat->stack = palloc0(sizeof(StatEntry *) * (stat->maxdepth + 1));
2270  stat->stackpos = 0;
2271 
2272  node = stat->root;
2273  /* find leftmost value */
2274  if (node == NULL)
2275  stat->stack[stat->stackpos] = NULL;
2276  else
2277  for (;;)
2278  {
2279  stat->stack[stat->stackpos] = node;
2280  if (node->left)
2281  {
2282  stat->stackpos++;
2283  node = node->left;
2284  }
2285  else
2286  break;
2287  }
2288  Assert(stat->stackpos <= stat->maxdepth);
2289 
2290  tupdesc = CreateTemplateTupleDesc(3);
2291  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "word",
2292  TEXTOID, -1, 0);
2293  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "ndoc",
2294  INT4OID, -1, 0);
2295  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "nentry",
2296  INT4OID, -1, 0);
2297  funcctx->tuple_desc = BlessTupleDesc(tupdesc);
2298  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
2299 
2300  MemoryContextSwitchTo(oldcontext);
2301 }
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:981
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
#define Assert(condition)
Definition: c.h:745
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 2479 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().

2480 {
2481  FuncCallContext *funcctx;
2482  Datum result;
2483 
2484  if (SRF_IS_FIRSTCALL())
2485  {
2486  TSVectorStat *stat;
2487  text *txt = PG_GETARG_TEXT_PP(0);
2488 
2489  funcctx = SRF_FIRSTCALL_INIT();
2490  SPI_connect();
2491  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, NULL);
2492  PG_FREE_IF_COPY(txt, 0);
2493  ts_setup_firstcall(fcinfo, funcctx, stat);
2494  SPI_finish();
2495  }
2496 
2497  funcctx = SRF_PERCALL_SETUP();
2498  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2499  SRF_RETURN_NEXT(funcctx, result);
2500  SRF_RETURN_DONE(funcctx);
2501 }
int SPI_connect(void)
Definition: spi.c:90
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
int SPI_finish(void)
Definition: spi.c:177
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#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:2390
static void ts_setup_firstcall(FunctionCallInfo fcinfo, FuncCallContext *funcctx, TSVectorStat *stat)
Definition: tsvector_op.c:2258
#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:2350
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:562
#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 2504 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().

2505 {
2506  FuncCallContext *funcctx;
2507  Datum result;
2508 
2509  if (SRF_IS_FIRSTCALL())
2510  {
2511  TSVectorStat *stat;
2512  text *txt = PG_GETARG_TEXT_PP(0);
2513  text *ws = PG_GETARG_TEXT_PP(1);
2514 
2515  funcctx = SRF_FIRSTCALL_INIT();
2516  SPI_connect();
2517  stat = ts_stat_sql(funcctx->multi_call_memory_ctx, txt, ws);
2518  PG_FREE_IF_COPY(txt, 0);
2519  PG_FREE_IF_COPY(ws, 1);
2520  ts_setup_firstcall(fcinfo, funcctx, stat);
2521  SPI_finish();
2522  }
2523 
2524  funcctx = SRF_PERCALL_SETUP();
2525  if ((result = ts_process_call(funcctx)) != (Datum) 0)
2526  SRF_RETURN_NEXT(funcctx, result);
2527  SRF_RETURN_DONE(funcctx);
2528 }
int SPI_connect(void)
Definition: spi.c:90
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
int SPI_finish(void)
Definition: spi.c:177
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#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:2390
static void ts_setup_firstcall(FunctionCallInfo fcinfo, FuncCallContext *funcctx, TSVectorStat *stat)
Definition: tsvector_op.c:2258
#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:2350
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
Definition: c.h:562
#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 2390 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().

2391 {
2392  char *query = text_to_cstring(txt);
2393  TSVectorStat *stat;
2394  bool isnull;
2395  Portal portal;
2396  SPIPlanPtr plan;
2397 
2398  if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
2399  /* internal error */
2400  elog(ERROR, "SPI_prepare(\"%s\") failed", query);
2401 
2402  if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
2403  /* internal error */
2404  elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
2405 
2406  SPI_cursor_fetch(portal, true, 100);
2407 
2408  if (SPI_tuptable == NULL ||
2409  SPI_tuptable->tupdesc->natts != 1 ||
2411  TSVECTOROID))
2412  ereport(ERROR,
2413  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2414  errmsg("ts_stat query must return one tsvector column")));
2415 
2416  stat = MemoryContextAllocZero(persistentContext, sizeof(TSVectorStat));
2417  stat->maxdepth = 1;
2418 
2419  if (ws)
2420  {
2421  char *buf;
2422 
2423  buf = VARDATA_ANY(ws);
2424  while (buf - VARDATA_ANY(ws) < VARSIZE_ANY_EXHDR(ws))
2425  {
2426  if (pg_mblen(buf) == 1)
2427  {
2428  switch (*buf)
2429  {
2430  case 'A':
2431  case 'a':
2432  stat->weight |= 1 << 3;
2433  break;
2434  case 'B':
2435  case 'b':
2436  stat->weight |= 1 << 2;
2437  break;
2438  case 'C':
2439  case 'c':
2440  stat->weight |= 1 << 1;
2441  break;
2442  case 'D':
2443  case 'd':
2444  stat->weight |= 1;
2445  break;
2446  default:
2447  stat->weight |= 0;
2448  }
2449  }
2450  buf += pg_mblen(buf);
2451  }
2452  }
2453 
2454  while (SPI_processed > 0)
2455  {
2456  uint64 i;
2457 
2458  for (i = 0; i < SPI_processed; i++)
2459  {
2460  Datum data = SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull);
2461 
2462  if (!isnull)
2463  stat = ts_accum(persistentContext, stat, data);
2464  }
2465 
2467  SPI_cursor_fetch(portal, true, 100);
2468  }
2469 
2471  SPI_cursor_close(portal);
2472  SPI_freeplan(plan);
2473  pfree(query);
2474 
2475  return stat;
2476 }
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1157
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:747
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:1294
HeapTuple * vals
Definition: spi.h:26
uint64 SPI_processed
Definition: spi.c:45
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1101
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:1235
uintptr_t Datum
Definition: postgres.h:367
TupleDesc tupdesc
Definition: spi.h:25
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:840
#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:874
void SPI_cursor_close(Portal portal)
Definition: spi.c:1705
char * text_to_cstring(const text *t)
Definition: varlena.c:221
#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:1649
static TSVectorStat * ts_accum(MemoryContext persistentContext, TSVectorStat *stat, Datum data)
Definition: tsvector_op.c:2223
int32 weight
Definition: tsvector_op.c:60

◆ tsCompareString()

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

Definition at line 1147 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().

1148 {
1149  int cmp;
1150 
1151  if (lena == 0)
1152  {
1153  if (prefix)
1154  cmp = 0; /* empty string is prefix of anything */
1155  else
1156  cmp = (lenb > 0) ? -1 : 0;
1157  }
1158  else if (lenb == 0)
1159  {
1160  cmp = (lena > 0) ? 1 : 0;
1161  }
1162  else
1163  {
1164  cmp = memcmp(a, b, Min(lena, lenb));
1165 
1166  if (prefix)
1167  {
1168  if (cmp == 0 && lena > lenb)
1169  cmp = 1; /* a is longer, so not a prefix of b */
1170  }
1171  else if (cmp == 0 && lena != lenb)
1172  {
1173  cmp = (lena < lenb) ? -1 : 1;
1174  }
1175  }
1176 
1177  return cmp;
1178 }
#define Min(x, y)
Definition: c.h:927
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 1965 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().

1966 {
1967  /* since this function recurses, it could be driven to stack overflow */
1969 
1970  if (curitem->type == QI_VAL)
1971  return true;
1972 
1973  switch (curitem->qoperator.oper)
1974  {
1975  case OP_NOT:
1976 
1977  /*
1978  * Assume there are no required matches underneath a NOT. For
1979  * some cases with nested NOTs, we could prove there's a required
1980  * match, but it seems unlikely to be worth the trouble.
1981  */
1982  return false;
1983 
1984  case OP_PHRASE:
1985 
1986  /*
1987  * Treat OP_PHRASE as OP_AND here
1988  */
1989  case OP_AND:
1990  /* If either side requires a match, we're good */
1991  if (tsquery_requires_match(curitem + curitem->qoperator.left))
1992  return true;
1993  else
1994  return tsquery_requires_match(curitem + 1);
1995 
1996  case OP_OR:
1997  /* Both sides must require a match */
1998  if (tsquery_requires_match(curitem + curitem->qoperator.left))
1999  return tsquery_requires_match(curitem + 1);
2000  else
2001  return false;
2002 
2003  default:
2004  elog(ERROR, "unrecognized operator: %d", curitem->qoperator.oper);
2005  }
2006 
2007  /* not reachable, but keep compiler quiet */
2008  return false;
2009 }
QueryOperator qoperator
Definition: ts_type.h:196
bool tsquery_requires_match(QueryItem *curitem)
Definition: tsvector_op.c:1965
#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 398 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().

399 {
400  WordEntry *arrin = ARRPTR(tsv);
401  int StopLow = 0,
402  StopHigh = tsv->size,
403  StopMiddle,
404  cmp;
405 
406  while (StopLow < StopHigh)
407  {
408  StopMiddle = (StopLow + StopHigh) / 2;
409 
410  cmp = tsCompareString(lexeme, lexeme_len,
411  STRPTR(tsv) + arrin[StopMiddle].pos,
412  arrin[StopMiddle].len,
413  false);
414 
415  if (cmp < 0)
416  StopHigh = StopMiddle;
417  else if (cmp > 0)
418  StopLow = StopMiddle + 1;
419  else /* found it */
420  return StopMiddle;
421  }
422 
423  return -1;
424 }
int32 size
Definition: ts_type.h:93
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1147
#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 920 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.

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

◆ tsvector_delete_arr()

Datum tsvector_delete_arr ( PG_FUNCTION_ARGS  )

Definition at line 578 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.

579 {
580  TSVector tsin = PG_GETARG_TSVECTOR(0),
581  tsout;
582  ArrayType *lexemes = PG_GETARG_ARRAYTYPE_P(1);
583  int i,
584  nlex,
585  skip_count,
586  *skip_indices;
587  Datum *dlexemes;
588  bool *nulls;
589 
590  deconstruct_array(lexemes, TEXTOID, -1, false, TYPALIGN_INT,
591  &dlexemes, &nulls, &nlex);
592 
593  /*
594  * In typical use case array of lexemes to delete is relatively small. So
595  * here we optimize things for that scenario: iterate through lexarr
596  * performing binary search of each lexeme from lexarr in tsvector.
597  */
598  skip_indices = palloc0(nlex * sizeof(int));
599  for (i = skip_count = 0; i < nlex; i++)
600  {
601  char *lex;
602  int lex_len,
603  lex_pos;
604 
605  if (nulls[i])
606  ereport(ERROR,
607  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
608  errmsg("lexeme array may not contain nulls")));
609 
610  lex = VARDATA(dlexemes[i]);
611  lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
612  lex_pos = tsvector_bsearch(tsin, lex, lex_len);
613 
614  if (lex_pos >= 0)
615  skip_indices[skip_count++] = lex_pos;
616  }
617 
618  tsout = tsvector_delete_by_indices(tsin, skip_indices, skip_count);
619 
620  pfree(skip_indices);
621  PG_FREE_IF_COPY(tsin, 0);
622  PG_FREE_IF_COPY(lexemes, 1);
623 
624  PG_RETURN_POINTER(tsout);
625 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:568
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:1057
#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:464
void * palloc0(Size size)
Definition: mcxt.c:981
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:398
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3483
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 464 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().

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

◆ tsvector_delete_str()

Datum tsvector_delete_str ( PG_FUNCTION_ARGS  )

Definition at line 554 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.

555 {
556  TSVector tsin = PG_GETARG_TSVECTOR(0),
557  tsout;
558  text *tlexeme = PG_GETARG_TEXT_PP(1);
559  char *lexeme = VARDATA_ANY(tlexeme);
560  int lexeme_len = VARSIZE_ANY_EXHDR(tlexeme),
561  skip_index;
562 
563  if ((skip_index = tsvector_bsearch(tsin, lexeme, lexeme_len)) == -1)
564  PG_RETURN_POINTER(tsin);
565 
566  tsout = tsvector_delete_by_indices(tsin, &skip_index, 1);
567 
568  PG_FREE_IF_COPY(tsin, 0);
569  PG_FREE_IF_COPY(tlexeme, 1);
570  PG_RETURN_POINTER(tsout);
571 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static TSVector tsvector_delete_by_indices(TSVector tsv, int *indices_to_delete, int indices_count)
Definition: tsvector_op.c:464
static int tsvector_bsearch(const TSVector tsv, char *lexeme, int lexeme_len)
Definition: tsvector_op.c:398
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
Definition: c.h:562

◆ tsvector_filter()

Datum tsvector_filter ( PG_FUNCTION_ARGS  )

Definition at line 813 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.

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

◆ tsvector_length()

Datum tsvector_length ( PG_FUNCTION_ARGS  )

Definition at line 197 of file tsvector_op.c.

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

198 {
200  int32 ret = in->size;
201 
202  PG_FREE_IF_COPY(in, 0);
203  PG_RETURN_INT32(ret);
204 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
signed int int32
Definition: c.h:362
int32 size
Definition: ts_type.h:93
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120

◆ tsvector_setweight()

Datum tsvector_setweight ( PG_FUNCTION_ARGS  )

Definition at line 207 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.

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

◆ tsvector_setweight_by_filter()

Datum tsvector_setweight_by_filter ( PG_FUNCTION_ARGS  )

Definition at line 269 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.

270 {
271  TSVector tsin = PG_GETARG_TSVECTOR(0);
272  char char_weight = PG_GETARG_CHAR(1);
273  ArrayType *lexemes = PG_GETARG_ARRAYTYPE_P(2);
274 
275  TSVector tsout;
276  int i,
277  j,
278  nlexemes,
279  weight;
280  WordEntry *entry;
281  Datum *dlexemes;
282  bool *nulls;
283 
284  switch (char_weight)
285  {
286  case 'A':
287  case 'a':
288  weight = 3;
289  break;
290  case 'B':
291  case 'b':
292  weight = 2;
293  break;
294  case 'C':
295  case 'c':
296  weight = 1;
297  break;
298  case 'D':
299  case 'd':
300  weight = 0;
301  break;
302  default:
303  /* internal error */
304  elog(ERROR, "unrecognized weight: %c", char_weight);
305  }
306 
307  tsout = (TSVector) palloc(VARSIZE(tsin));
308  memcpy(tsout, tsin, VARSIZE(tsin));
309  entry = ARRPTR(tsout);
310 
311  deconstruct_array(lexemes, TEXTOID, -1, false, TYPALIGN_INT,
312  &dlexemes, &nulls, &nlexemes);
313 
314  /*
315  * Assuming that lexemes array is significantly shorter than tsvector we
316  * can iterate through lexemes performing binary search of each lexeme
317  * from lexemes in tsvector.
318  */
319  for (i = 0; i < nlexemes; i++)
320  {
321  char *lex;
322  int lex_len,
323  lex_pos;
324 
325  if (nulls[i])
326  ereport(ERROR,
327  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
328  errmsg("lexeme array may not contain nulls")));
329 
330  lex = VARDATA(dlexemes[i]);
331  lex_len = VARSIZE(dlexemes[i]) - VARHDRSZ;
332  lex_pos = tsvector_bsearch(tsout, lex, lex_len);
333 
334  if (lex_pos >= 0 && (j = POSDATALEN(tsout, entry + lex_pos)) != 0)
335  {
336  WordEntryPos *p = POSDATAPTR(tsout, entry + lex_pos);
337 
338  while (j--)
339  {
340  WEP_SETWEIGHT(*p, weight);
341  p++;
342  }
343  }
344  }
345 
346  PG_FREE_IF_COPY(tsin, 0);
347  PG_FREE_IF_COPY(lexemes, 2);
348 
349  PG_RETURN_POINTER(tsout);
350 }
uint16 WordEntryPos
Definition: ts_type.h:63
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#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:568
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:398
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3483
void * palloc(Size size)
Definition: mcxt.c:950
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:273

◆ tsvector_strip()

Datum tsvector_strip ( PG_FUNCTION_ARGS  )

Definition at line 164 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.

165 {
167  TSVector out;
168  int i,
169  len = 0;
170  WordEntry *arrin = ARRPTR(in),
171  *arrout;
172  char *cur;
173 
174  for (i = 0; i < in->size; i++)
175  len += arrin[i].len;
176 
177  len = CALCDATASIZE(in->size, len);
178  out = (TSVector) palloc0(len);
179  SET_VARSIZE(out, len);
180  out->size = in->size;
181  arrout = ARRPTR(out);
182  cur = STRPTR(out);
183  for (i = 0; i < in->size; i++)
184  {
185  memcpy(cur, STRPTR(in) + arrin[i].pos, arrin[i].len);
186  arrout[i].haspos = 0;
187  arrout[i].len = arrin[i].len;
188  arrout[i].pos = cur - STRPTR(out);
189  cur += arrout[i].len;
190  }
191 
192  PG_FREE_IF_COPY(in, 0);
193  PG_RETURN_POINTER(out);
194 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
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:981
TSVectorData * TSVector
Definition: ts_type.h:98
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#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 722 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.

723 {
724  TSVector tsin = PG_GETARG_TSVECTOR(0);
725  WordEntry *arrin = ARRPTR(tsin);
726  Datum *elements;
727  int i;
728  ArrayType *array;
729 
730  elements = palloc(tsin->size * sizeof(Datum));
731 
732  for (i = 0; i < tsin->size; i++)
733  {
734  elements[i] = PointerGetDatum(cstring_to_text_with_len(STRPTR(tsin) + arrin[i].pos,
735  arrin[i].len));
736  }
737 
738  array = construct_array(elements, tsin->size, TEXTOID, -1, false, TYPALIGN_INT);
739 
740  pfree(elements);
741  PG_FREE_IF_COPY(tsin, 0);
742  PG_RETURN_POINTER(array);
743 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
#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:3313
void pfree(void *pointer)
Definition: mcxt.c:1057
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
int32 size
Definition: ts_type.h:93
uintptr_t Datum
Definition: postgres.h:367
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
void * palloc(Size size)
Definition: mcxt.c:950
#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 634 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.

635 {
636  FuncCallContext *funcctx;
637  TSVector tsin;
638 
639  if (SRF_IS_FIRSTCALL())
640  {
641  MemoryContext oldcontext;
642  TupleDesc tupdesc;
643 
644  funcctx = SRF_FIRSTCALL_INIT();
645  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
646 
647  tupdesc = CreateTemplateTupleDesc(3);
648  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "lexeme",
649  TEXTOID, -1, 0);
650  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "positions",
651  INT2ARRAYOID, -1, 0);
652  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "weights",
653  TEXTARRAYOID, -1, 0);
654  funcctx->tuple_desc = BlessTupleDesc(tupdesc);
655 
656  funcctx->user_fctx = PG_GETARG_TSVECTOR_COPY(0);
657 
658  MemoryContextSwitchTo(oldcontext);
659  }
660 
661  funcctx = SRF_PERCALL_SETUP();
662  tsin = (TSVector) funcctx->user_fctx;
663 
664  if (funcctx->call_cntr < tsin->size)
665  {
666  WordEntry *arrin = ARRPTR(tsin);
667  char *data = STRPTR(tsin);
668  HeapTuple tuple;
669  int j,
670  i = funcctx->call_cntr;
671  bool nulls[] = {false, false, false};
672  Datum values[3];
673 
674  values[0] = PointerGetDatum(cstring_to_text_with_len(data + arrin[i].pos, arrin[i].len));
675 
676  if (arrin[i].haspos)
677  {
678  WordEntryPosVector *posv;
679  Datum *positions;
680  Datum *weights;
681  char weight;
682 
683  /*
684  * Internally tsvector stores position and weight in the same
685  * uint16 (2 bits for weight, 14 for position). Here we extract
686  * that in two separate arrays.
687  */
688  posv = _POSVECPTR(tsin, arrin + i);
689  positions = palloc(posv->npos * sizeof(Datum));
690  weights = palloc(posv->npos * sizeof(Datum));
691  for (j = 0; j < posv->npos; j++)
692  {
693  positions[j] = Int16GetDatum(WEP_GETPOS(posv->pos[j]));
694  weight = 'D' - WEP_GETWEIGHT(posv->pos[j]);
695  weights[j] = PointerGetDatum(cstring_to_text_with_len(&weight,
696  1));
697  }
698 
699  values[1] = PointerGetDatum(construct_array(positions, posv->npos,
700  INT2OID, 2, true, TYPALIGN_SHORT));
701  values[2] = PointerGetDatum(construct_array(weights, posv->npos,
702  TEXTOID, -1, false, TYPALIGN_INT));
703  }
704  else
705  {
706  nulls[1] = nulls[2] = true;
707  }
708 
709  tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
710  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
711  }
712  else
713  {
714  SRF_RETURN_DONE(funcctx);
715  }
716 }
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:3313
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:200
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:165
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:950
#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 2554 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().

2555 {
2556  TriggerData *trigdata;
2557  Trigger *trigger;
2558  Relation rel;
2559  HeapTuple rettuple = NULL;
2560  int tsvector_attr_num,
2561  i;
2562  ParsedText prs;
2563  Datum datum;
2564  bool isnull;
2565  text *txt;
2566  Oid cfgId;
2567  bool update_needed;
2568 
2569  /* Check call context */
2570  if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
2571  elog(ERROR, "tsvector_update_trigger: not fired by trigger manager");
2572 
2573  trigdata = (TriggerData *) fcinfo->context;
2574  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
2575  elog(ERROR, "tsvector_update_trigger: must be fired for row");
2576  if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
2577  elog(ERROR, "tsvector_update_trigger: must be fired BEFORE event");
2578 
2579  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
2580  {
2581  rettuple = trigdata->tg_trigtuple;
2582  update_needed = true;
2583  }
2584  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
2585  {
2586  rettuple = trigdata->tg_newtuple;
2587  update_needed = false; /* computed below */
2588  }
2589  else
2590  elog(ERROR, "tsvector_update_trigger: must be fired for INSERT or UPDATE");
2591 
2592  trigger = trigdata->tg_trigger;
2593  rel = trigdata->tg_relation;
2594 
2595  if (trigger->tgnargs < 3)
2596  elog(ERROR, "tsvector_update_trigger: arguments must be tsvector_field, ts_config, text_field1, ...)");
2597 
2598  /* Find the target tsvector column */
2599  tsvector_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
2600  if (tsvector_attr_num == SPI_ERROR_NOATTRIBUTE)
2601  ereport(ERROR,
2602  (errcode(ERRCODE_UNDEFINED_COLUMN),
2603  errmsg("tsvector column \"%s\" does not exist",
2604  trigger->tgargs[0])));
2605  /* This will effectively reject system columns, so no separate test: */
2606  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, tsvector_attr_num),
2607  TSVECTOROID))
2608  ereport(ERROR,
2609  (errcode(ERRCODE_DATATYPE_MISMATCH),
2610  errmsg("column \"%s\" is not of tsvector type",
2611  trigger->tgargs[0])));
2612 
2613  /* Find the configuration to use */
2614  if (config_column)
2615  {
2616  int config_attr_num;
2617 
2618  config_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[1]);
2619  if (config_attr_num == SPI_ERROR_NOATTRIBUTE)
2620  ereport(ERROR,
2621  (errcode(ERRCODE_UNDEFINED_COLUMN),
2622  errmsg("configuration column \"%s\" does not exist",
2623  trigger->tgargs[1])));
2624  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, config_attr_num),
2625  REGCONFIGOID))
2626  ereport(ERROR,
2627  (errcode(ERRCODE_DATATYPE_MISMATCH),
2628  errmsg("column \"%s\" is not of regconfig type",
2629  trigger->tgargs[1])));
2630 
2631  datum = SPI_getbinval(rettuple, rel->rd_att, config_attr_num, &isnull);
2632  if (isnull)
2633  ereport(ERROR,
2634  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2635  errmsg("configuration column \"%s\" must not be null",
2636  trigger->tgargs[1])));
2637  cfgId = DatumGetObjectId(datum);
2638  }
2639  else
2640  {
2641  List *names;
2642 
2643  names = stringToQualifiedNameList(trigger->tgargs[1]);
2644  /* require a schema so that results are not search path dependent */
2645  if (list_length(names) < 2)
2646  ereport(ERROR,
2647  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2648  errmsg("text search configuration name \"%s\" must be schema-qualified",
2649  trigger->tgargs[1])));
2650  cfgId = get_ts_config_oid(names, false);
2651  }
2652 
2653  /* initialize parse state */
2654  prs.lenwords = 32;
2655  prs.curwords = 0;
2656  prs.pos = 0;
2657  prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
2658 
2659  /* find all words in indexable column(s) */
2660  for (i = 2; i < trigger->tgnargs; i++)
2661  {
2662  int numattr;
2663 
2664  numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
2665  if (numattr == SPI_ERROR_NOATTRIBUTE)
2666  ereport(ERROR,
2667  (errcode(ERRCODE_UNDEFINED_COLUMN),
2668  errmsg("column \"%s\" does not exist",
2669  trigger->tgargs[i])));
2670  if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, numattr), TEXTOID))
2671  ereport(ERROR,
2672  (errcode(ERRCODE_DATATYPE_MISMATCH),
2673  errmsg("column \"%s\" is not of a character type",
2674  trigger->tgargs[i])));
2675 
2677  update_needed = true;
2678 
2679  datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
2680  if (isnull)
2681  continue;
2682 
2683  txt = DatumGetTextPP(datum);
2684 
2685  parsetext(cfgId, &prs, VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
2686 
2687  if (txt != (text *) DatumGetPointer(datum))
2688  pfree(txt);
2689  }
2690 
2691  if (update_needed)
2692  {
2693  /* make tsvector value */
2694  datum = TSVectorGetDatum(make_tsvector(&prs));
2695  isnull = false;
2696 
2697  /* and insert it into tuple */
2698  rettuple = heap_modify_tuple_by_cols(rettuple, rel->rd_att,
2699  1, &tsvector_attr_num,
2700  &datum, &isnull);
2701 
2702  pfree(DatumGetPointer(datum));
2703  }
2704 
2705  return PointerGetDatum(rettuple);
2706 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1024
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1157
TSVector make_tsvector(ParsedText *prs)
Definition: to_tsany.c:156
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int numattr
Definition: bootstrap.c:75
#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:291
int errcode(int sqlerrcode)
Definition: elog.c:610
Oid get_ts_config_oid(List *names, bool missing_ok)
Definition: namespace.c:2678
#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:1057
#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:1101
#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:1876
#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:950
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:562
#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 2548 of file tsvector_op.c.

References tsvector_update_trigger().

2549 {
2550  return tsvector_update_trigger(fcinfo, true);
2551 }
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
Definition: tsvector_op.c:2554

◆ tsvector_update_trigger_byid()

Datum tsvector_update_trigger_byid ( PG_FUNCTION_ARGS  )

Definition at line 2542 of file tsvector_op.c.

References tsvector_update_trigger().

2543 {
2544  return tsvector_update_trigger(fcinfo, false);
2545 }
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
Definition: tsvector_op.c:2554

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

2305 {
2306  StatEntry *node = stat->stack[stat->stackpos];
2307 
2308  if (node == NULL)
2309  return NULL;
2310 
2311  if (node->ndoc != 0)
2312  {
2313  /* return entry itself: we already was at left sublink */
2314  return node;
2315  }
2316  else if (node->right && node->right != stat->stack[stat->stackpos + 1])
2317  {
2318  /* go on right sublink */
2319  stat->stackpos++;
2320  node = node->right;
2321 
2322  /* find most-left value */
2323  for (;;)
2324  {
2325  stat->stack[stat->stackpos] = node;
2326  if (node->left)
2327  {
2328  stat->stackpos++;
2329  node = node->left;
2330  }
2331  else
2332  break;
2333  }
2334  Assert(stat->stackpos <= stat->maxdepth);
2335  }
2336  else
2337  {
2338  /* we already return all left subtree, itself and right subtree */
2339  if (stat->stackpos == 0)
2340  return NULL;
2341 
2342  stat->stackpos--;
2343  return walkStatEntryTree(stat);
2344  }
2345 
2346  return node;
2347 }
uint32 stackpos
Definition: tsvector_op.c:65
uint32 ndoc
Definition: tsvector_op.c:47
static StatEntry * walkStatEntryTree(TSVectorStat *stat)
Definition: tsvector_op.c:2304
StatEntry ** stack
Definition: tsvector_op.c:64
#define Assert(condition)
Definition: c.h:745
uint32 maxdepth
Definition: tsvector_op.c:62
struct StatEntry * left
Definition: tsvector_op.c:50
struct StatEntry * right
Definition: tsvector_op.c:51