PostgreSQL Source Code  git master
tsrank.c File Reference
#include "postgres.h"
#include <limits.h>
#include <math.h>
#include "miscadmin.h"
#include "tsearch/ts_utils.h"
#include "utils/array.h"
#include "utils/builtins.h"
Include dependency graph for tsrank.c:

Go to the source code of this file.

Data Structures

struct  DocRepresentation
 
struct  QueryRepresentationOperand
 
struct  QueryRepresentation
 
struct  CoverExt
 

Macros

#define wpos(wep)   ( w[ WEP_GETWEIGHT(wep) ] )
 
#define RANK_NO_NORM   0x00
 
#define RANK_NORM_LOGLENGTH   0x01
 
#define RANK_NORM_LENGTH   0x02
 
#define RANK_NORM_EXTDIST   0x04
 
#define RANK_NORM_UNIQ   0x08
 
#define RANK_NORM_LOGUNIQ   0x10
 
#define RANK_NORM_RDIVRPLUS1   0x20
 
#define DEF_NORM_METHOD   RANK_NO_NORM
 
#define WordECompareQueryItem(e, q, p, i, m)
 
#define MAXQROPOS   MAXENTRYPOS
 
#define QR_GET_OPERAND_DATA(q, v)   ( (q)->operandData + (((QueryItem*)(v)) - GETQUERY((q)->query)) )
 

Functions

static float calc_rank_or (const float *w, TSVector t, TSQuery q)
 
static float calc_rank_and (const float *w, TSVector t, TSQuery q)
 
static float4 word_distance (int32 w)
 
static int cnt_length (TSVector t)
 
static WordEntryfind_wordentry (TSVector t, TSQuery q, QueryOperand *item, int32 *nitem)
 
static int compareQueryOperand (const void *a, const void *b, void *arg)
 
static QueryOperand ** SortAndUniqItems (TSQuery q, int *size)
 
static float calc_rank (const float *w, TSVector t, TSQuery q, int32 method)
 
static const float * getWeights (ArrayType *win)
 
Datum ts_rank_wttf (PG_FUNCTION_ARGS)
 
Datum ts_rank_wtt (PG_FUNCTION_ARGS)
 
Datum ts_rank_ttf (PG_FUNCTION_ARGS)
 
Datum ts_rank_tt (PG_FUNCTION_ARGS)
 
static int compareDocR (const void *va, const void *vb)
 
static bool checkcondition_QueryOperand (void *checkval, QueryOperand *val, ExecPhraseData *data)
 
static void resetQueryRepresentation (QueryRepresentation *qr, bool reverseinsert)
 
static void fillQueryRepresentationData (QueryRepresentation *qr, DocRepresentation *entry)
 
static bool Cover (DocRepresentation *doc, int len, QueryRepresentation *qr, CoverExt *ext)
 
static DocRepresentationget_docrep (TSVector txt, QueryRepresentation *qr, int *doclen)
 
static float4 calc_rank_cd (const float4 *arrdata, TSVector txt, TSQuery query, int method)
 
Datum ts_rankcd_wttf (PG_FUNCTION_ARGS)
 
Datum ts_rankcd_wtt (PG_FUNCTION_ARGS)
 
Datum ts_rankcd_ttf (PG_FUNCTION_ARGS)
 
Datum ts_rankcd_tt (PG_FUNCTION_ARGS)
 

Variables

static const float weights [] = {0.1f, 0.2f, 0.4f, 1.0f}
 

Macro Definition Documentation

◆ DEF_NORM_METHOD

#define DEF_NORM_METHOD   RANK_NO_NORM

Definition at line 35 of file tsrank.c.

Referenced by ts_rank_tt(), ts_rank_wtt(), ts_rankcd_tt(), and ts_rankcd_wtt().

◆ MAXQROPOS

#define MAXQROPOS   MAXENTRYPOS

Definition at line 540 of file tsrank.c.

Referenced by checkcondition_QueryOperand(), and fillQueryRepresentationData().

◆ QR_GET_OPERAND_DATA

#define QR_GET_OPERAND_DATA (   q,
 
)    ( (q)->operandData + (((QueryItem*)(v)) - GETQUERY((q)->query)) )

Definition at line 556 of file tsrank.c.

Referenced by checkcondition_QueryOperand(), and fillQueryRepresentationData().

◆ RANK_NO_NORM

#define RANK_NO_NORM   0x00

Definition at line 28 of file tsrank.c.

◆ RANK_NORM_EXTDIST

#define RANK_NORM_EXTDIST   0x04

Definition at line 31 of file tsrank.c.

Referenced by calc_rank_cd().

◆ RANK_NORM_LENGTH

#define RANK_NORM_LENGTH   0x02

Definition at line 30 of file tsrank.c.

Referenced by calc_rank(), and calc_rank_cd().

◆ RANK_NORM_LOGLENGTH

#define RANK_NORM_LOGLENGTH   0x01

Definition at line 29 of file tsrank.c.

Referenced by calc_rank(), and calc_rank_cd().

◆ RANK_NORM_LOGUNIQ

#define RANK_NORM_LOGUNIQ   0x10

Definition at line 33 of file tsrank.c.

Referenced by calc_rank(), and calc_rank_cd().

◆ RANK_NORM_RDIVRPLUS1

#define RANK_NORM_RDIVRPLUS1   0x20

Definition at line 34 of file tsrank.c.

Referenced by calc_rank(), and calc_rank_cd().

◆ RANK_NORM_UNIQ

#define RANK_NORM_UNIQ   0x08

Definition at line 32 of file tsrank.c.

Referenced by calc_rank(), and calc_rank_cd().

◆ WordECompareQueryItem

#define WordECompareQueryItem (   e,
  q,
  p,
  i,
 
)
Value:
tsCompareString((q) + (i)->distance, (i)->length, \
(e) + (p)->pos, (p)->len, (m))
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1147
e
Definition: preproc-init.c:82
int i

Definition at line 75 of file tsrank.c.

Referenced by find_wordentry().

◆ wpos

#define wpos (   wep)    ( w[ WEP_GETWEIGHT(wep) ] )

Definition at line 26 of file tsrank.c.

Referenced by BufFileDumpBuffer(), calc_rank_and(), and calc_rank_or().

Function Documentation

◆ calc_rank()

static float calc_rank ( const float *  w,
TSVector  t,
TSQuery  q,
int32  method 
)
static

Definition at line 357 of file tsrank.c.

References calc_rank_and(), calc_rank_or(), cnt_length(), GETQUERY, OP_AND, OP_PHRASE, QueryOperator::oper, QI_OPR, QueryItem::qoperator, RANK_NORM_LENGTH, RANK_NORM_LOGLENGTH, RANK_NORM_LOGUNIQ, RANK_NORM_RDIVRPLUS1, RANK_NORM_UNIQ, TSVectorData::size, TSQueryData::size, and QueryItem::type.

Referenced by ts_rank_tt(), ts_rank_ttf(), ts_rank_wtt(), and ts_rank_wttf().

358 {
359  QueryItem *item = GETQUERY(q);
360  float res = 0.0;
361  int len;
362 
363  if (!t->size || !q->size)
364  return 0.0;
365 
366  /* XXX: What about NOT? */
367  res = (item->type == QI_OPR && (item->qoperator.oper == OP_AND ||
368  item->qoperator.oper == OP_PHRASE)) ?
369  calc_rank_and(w, t, q) :
370  calc_rank_or(w, t, q);
371 
372  if (res < 0)
373  res = 1e-20f;
374 
375  if ((method & RANK_NORM_LOGLENGTH) && t->size > 0)
376  res /= log((double) (cnt_length(t) + 1)) / log(2.0);
377 
378  if (method & RANK_NORM_LENGTH)
379  {
380  len = cnt_length(t);
381  if (len > 0)
382  res /= (float) len;
383  }
384 
385  /* RANK_NORM_EXTDIST not applicable */
386 
387  if ((method & RANK_NORM_UNIQ) && t->size > 0)
388  res /= (float) (t->size);
389 
390  if ((method & RANK_NORM_LOGUNIQ) && t->size > 0)
391  res /= log((double) (t->size + 1)) / log(2.0);
392 
393  if (method & RANK_NORM_RDIVRPLUS1)
394  res /= (res + 1);
395 
396  return res;
397 }
#define RANK_NORM_RDIVRPLUS1
Definition: tsrank.c:34
QueryOperator qoperator
Definition: ts_type.h:196
static float calc_rank_or(const float *w, TSVector t, TSQuery q)
Definition: tsrank.c:283
#define RANK_NORM_LOGLENGTH
Definition: tsrank.c:29
#define GETQUERY(x)
Definition: _int.h:136
#define RANK_NORM_UNIQ
Definition: tsrank.c:32
#define OP_AND
Definition: ts_type.h:167
static int cnt_length(TSVector t)
Definition: tsrank.c:53
int32 size
Definition: ts_type.h:93
#define RANK_NORM_LOGUNIQ
Definition: tsrank.c:33
#define QI_OPR
Definition: ts_type.h:135
QueryItemType type
Definition: ts_type.h:195
static float calc_rank_and(const float *w, TSVector t, TSQuery q)
Definition: tsrank.c:200
#define OP_PHRASE
Definition: ts_type.h:169
#define RANK_NORM_LENGTH
Definition: tsrank.c:30
e
Definition: preproc-init.c:82
int32 size
Definition: ts_type.h:208

◆ calc_rank_and()

static float calc_rank_and ( const float *  w,
TSVector  t,
TSQuery  q 
)
static

Definition at line 200 of file tsrank.c.

References _POSVECPTR, Abs, calc_rank_or(), find_wordentry(), WordEntry::haspos, i, MAXENTRYPOS, WordEntryPosVector::npos, WordEntryPosVector1::npos, palloc0(), pfree(), WordEntryPosVector::pos, WordEntryPosVector1::pos, TSQueryData::size, SortAndUniqItems(), WEP_GETPOS, WEP_SETPOS, word_distance(), and wpos.

Referenced by calc_rank().

201 {
202  WordEntryPosVector **pos;
203  WordEntryPosVector1 posnull;
204  WordEntryPosVector *POSNULL;
205  int i,
206  k,
207  l,
208  p;
209  WordEntry *entry,
210  *firstentry;
211  WordEntryPos *post,
212  *ct;
213  int32 dimt,
214  lenct,
215  dist,
216  nitem;
217  float res = -1.0;
218  QueryOperand **item;
219  int size = q->size;
220 
221  item = SortAndUniqItems(q, &size);
222  if (size < 2)
223  {
224  pfree(item);
225  return calc_rank_or(w, t, q);
226  }
227  pos = (WordEntryPosVector **) palloc0(sizeof(WordEntryPosVector *) * q->size);
228 
229  /* A dummy WordEntryPos array to use when haspos is false */
230  posnull.npos = 1;
231  posnull.pos[0] = 0;
232  WEP_SETPOS(posnull.pos[0], MAXENTRYPOS - 1);
233  POSNULL = (WordEntryPosVector *) &posnull;
234 
235  for (i = 0; i < size; i++)
236  {
237  firstentry = entry = find_wordentry(t, q, item[i], &nitem);
238  if (!entry)
239  continue;
240 
241  while (entry - firstentry < nitem)
242  {
243  if (entry->haspos)
244  pos[i] = _POSVECPTR(t, entry);
245  else
246  pos[i] = POSNULL;
247 
248  dimt = pos[i]->npos;
249  post = pos[i]->pos;
250  for (k = 0; k < i; k++)
251  {
252  if (!pos[k])
253  continue;
254  lenct = pos[k]->npos;
255  ct = pos[k]->pos;
256  for (l = 0; l < dimt; l++)
257  {
258  for (p = 0; p < lenct; p++)
259  {
260  dist = Abs((int) WEP_GETPOS(post[l]) - (int) WEP_GETPOS(ct[p]));
261  if (dist || (dist == 0 && (pos[i] == POSNULL || pos[k] == POSNULL)))
262  {
263  float curw;
264 
265  if (!dist)
266  dist = MAXENTRYPOS;
267  curw = sqrt(wpos(post[l]) * wpos(ct[p]) * word_distance(dist));
268  res = (res < 0) ? curw : 1.0 - (1.0 - res) * (1.0 - curw);
269  }
270  }
271  }
272  }
273 
274  entry++;
275  }
276  }
277  pfree(pos);
278  pfree(item);
279  return res;
280 }
uint16 WordEntryPos
Definition: ts_type.h:63
static float calc_rank_or(const float *w, TSVector t, TSQuery q)
Definition: tsrank.c:283
#define _POSVECPTR(x, e)
Definition: ts_type.h:109
#define WEP_SETPOS(x, v)
Definition: ts_type.h:83
signed int int32
Definition: c.h:347
WordEntryPos pos[1]
Definition: ts_type.h:75
#define Abs(x)
Definition: c.h:917
void pfree(void *pointer)
Definition: mcxt.c:1056
#define WEP_GETPOS(x)
Definition: ts_type.h:80
WordEntryPos pos[FLEXIBLE_ARRAY_MEMBER]
Definition: ts_type.h:68
static float4 word_distance(int32 w)
Definition: tsrank.c:44
uint32 haspos
Definition: ts_type.h:44
#define wpos(wep)
Definition: tsrank.c:26
static QueryOperand ** SortAndUniqItems(TSQuery q, int *size)
Definition: tsrank.c:154
void * palloc0(Size size)
Definition: mcxt.c:980
static WordEntry * find_wordentry(TSVector t, TSQuery q, QueryOperand *item, int32 *nitem)
Definition: tsrank.c:86
int32 size
Definition: ts_type.h:208
int i
#define MAXENTRYPOS
Definition: ts_type.h:85

◆ calc_rank_cd()

static float4 calc_rank_cd ( const float4 arrdata,
TSVector  txt,
TSQuery  query,
int  method 
)
static

Definition at line 846 of file tsrank.c.

References CoverExt::begin, cnt_length(), Cover(), CoverExt::end, ereport, errcode(), errmsg(), ERROR, get_docrep(), i, lengthof, MemSet, QueryRepresentation::operandData, CoverExt::p, palloc0(), pfree(), DocRepresentation::pos, CoverExt::q, QueryRepresentation::query, RANK_NORM_EXTDIST, RANK_NORM_LENGTH, RANK_NORM_LOGLENGTH, RANK_NORM_LOGUNIQ, RANK_NORM_RDIVRPLUS1, RANK_NORM_UNIQ, TSVectorData::size, TSQueryData::size, weights, and WEP_GETWEIGHT.

Referenced by ts_rankcd_tt(), ts_rankcd_ttf(), ts_rankcd_wtt(), and ts_rankcd_wttf().

847 {
848  DocRepresentation *doc;
849  int len,
850  i,
851  doclen = 0;
852  CoverExt ext;
853  double Wdoc = 0.0;
854  double invws[lengthof(weights)];
855  double SumDist = 0.0,
856  PrevExtPos = 0.0,
857  CurExtPos = 0.0;
858  int NExtent = 0;
860 
861 
862  for (i = 0; i < lengthof(weights); i++)
863  {
864  invws[i] = ((double) ((arrdata[i] >= 0) ? arrdata[i] : weights[i]));
865  if (invws[i] > 1.0)
866  ereport(ERROR,
867  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
868  errmsg("weight out of range")));
869  invws[i] = 1.0 / invws[i];
870  }
871 
872  qr.query = query;
874  palloc0(sizeof(QueryRepresentationOperand) * query->size);
875 
876  doc = get_docrep(txt, &qr, &doclen);
877  if (!doc)
878  {
879  pfree(qr.operandData);
880  return 0.0;
881  }
882 
883  MemSet(&ext, 0, sizeof(CoverExt));
884  while (Cover(doc, doclen, &qr, &ext))
885  {
886  double Cpos = 0.0;
887  double InvSum = 0.0;
888  int nNoise;
889  DocRepresentation *ptr = ext.begin;
890 
891  while (ptr <= ext.end)
892  {
893  InvSum += invws[WEP_GETWEIGHT(ptr->pos)];
894  ptr++;
895  }
896 
897  Cpos = ((double) (ext.end - ext.begin + 1)) / InvSum;
898 
899  /*
900  * if doc are big enough then ext.q may be equal to ext.p due to limit
901  * of positional information. In this case we approximate number of
902  * noise word as half cover's length
903  */
904  nNoise = (ext.q - ext.p) - (ext.end - ext.begin);
905  if (nNoise < 0)
906  nNoise = (ext.end - ext.begin) / 2;
907  Wdoc += Cpos / ((double) (1 + nNoise));
908 
909  CurExtPos = ((double) (ext.q + ext.p)) / 2.0;
910  if (NExtent > 0 && CurExtPos > PrevExtPos /* prevent division by
911  * zero in a case of
912  * multiple lexize */ )
913  SumDist += 1.0 / (CurExtPos - PrevExtPos);
914 
915  PrevExtPos = CurExtPos;
916  NExtent++;
917  }
918 
919  if ((method & RANK_NORM_LOGLENGTH) && txt->size > 0)
920  Wdoc /= log((double) (cnt_length(txt) + 1));
921 
922  if (method & RANK_NORM_LENGTH)
923  {
924  len = cnt_length(txt);
925  if (len > 0)
926  Wdoc /= (double) len;
927  }
928 
929  if ((method & RANK_NORM_EXTDIST) && NExtent > 0 && SumDist > 0)
930  Wdoc /= ((double) NExtent) / SumDist;
931 
932  if ((method & RANK_NORM_UNIQ) && txt->size > 0)
933  Wdoc /= (double) (txt->size);
934 
935  if ((method & RANK_NORM_LOGUNIQ) && txt->size > 0)
936  Wdoc /= log((double) (txt->size + 1)) / log(2.0);
937 
938  if (method & RANK_NORM_RDIVRPLUS1)
939  Wdoc /= (Wdoc + 1);
940 
941  pfree(doc);
942 
943  pfree(qr.operandData);
944 
945  return (float4) Wdoc;
946 }
#define RANK_NORM_RDIVRPLUS1
Definition: tsrank.c:34
DocRepresentation * end
Definition: tsrank.c:585
#define RANK_NORM_EXTDIST
Definition: tsrank.c:31
static bool Cover(DocRepresentation *doc, int len, QueryRepresentation *qr, CoverExt *ext)
Definition: tsrank.c:642
#define RANK_NORM_LOGLENGTH
Definition: tsrank.c:29
int errcode(int sqlerrcode)
Definition: elog.c:608
#define MemSet(start, val, len)
Definition: c.h:962
#define lengthof(array)
Definition: c.h:669
static DocRepresentation * get_docrep(TSVector txt, QueryRepresentation *qr, int *doclen)
Definition: tsrank.c:723
#define RANK_NORM_UNIQ
Definition: tsrank.c:32
static int cnt_length(TSVector t)
Definition: tsrank.c:53
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
int p
Definition: tsrank.c:582
int q
Definition: tsrank.c:583
int32 size
Definition: ts_type.h:93
#define RANK_NORM_LOGUNIQ
Definition: tsrank.c:33
#define ereport(elevel, rest)
Definition: elog.h:141
float float4
Definition: c.h:491
void * palloc0(Size size)
Definition: mcxt.c:980
QueryRepresentationOperand * operandData
Definition: tsrank.c:553
#define RANK_NORM_LENGTH
Definition: tsrank.c:30
int errmsg(const char *fmt,...)
Definition: elog.c:822
int32 size
Definition: ts_type.h:208
int i
#define WEP_GETWEIGHT(x)
Definition: ts_type.h:79
DocRepresentation * begin
Definition: tsrank.c:584
WordEntryPos pos
Definition: tsrank.c:515
static const float weights[]
Definition: tsrank.c:24

◆ calc_rank_or()

static float calc_rank_or ( const float *  w,
TSVector  t,
TSQuery  q 
)
static

Definition at line 283 of file tsrank.c.

References find_wordentry(), WordEntry::haspos, i, WordEntryPosVector1::npos, pfree(), WordEntryPosVector1::pos, POSDATALEN, POSDATAPTR, TSQueryData::size, SortAndUniqItems(), and wpos.

Referenced by calc_rank(), and calc_rank_and().

284 {
285  WordEntry *entry,
286  *firstentry;
287  WordEntryPosVector1 posnull;
288  WordEntryPos *post;
289  int32 dimt,
290  j,
291  i,
292  nitem;
293  float res = 0.0;
294  QueryOperand **item;
295  int size = q->size;
296 
297  /* A dummy WordEntryPos array to use when haspos is false */
298  posnull.npos = 1;
299  posnull.pos[0] = 0;
300 
301  item = SortAndUniqItems(q, &size);
302 
303  for (i = 0; i < size; i++)
304  {
305  float resj,
306  wjm;
307  int32 jm;
308 
309  firstentry = entry = find_wordentry(t, q, item[i], &nitem);
310  if (!entry)
311  continue;
312 
313  while (entry - firstentry < nitem)
314  {
315  if (entry->haspos)
316  {
317  dimt = POSDATALEN(t, entry);
318  post = POSDATAPTR(t, entry);
319  }
320  else
321  {
322  dimt = posnull.npos;
323  post = posnull.pos;
324  }
325 
326  resj = 0.0;
327  wjm = -1.0;
328  jm = 0;
329  for (j = 0; j < dimt; j++)
330  {
331  resj = resj + wpos(post[j]) / ((j + 1) * (j + 1));
332  if (wpos(post[j]) > wjm)
333  {
334  wjm = wpos(post[j]);
335  jm = j;
336  }
337  }
338 /*
339  limit (sum(i/i^2),i->inf) = pi^2/6
340  resj = sum(wi/i^2),i=1,noccurence,
341  wi - should be sorted desc,
342  don't sort for now, just choose maximum weight. This should be corrected
343  Oleg Bartunov
344 */
345  res = res + (wjm + resj - wjm / ((jm + 1) * (jm + 1))) / 1.64493406685;
346 
347  entry++;
348  }
349  }
350  if (size > 0)
351  res = res / size;
352  pfree(item);
353  return res;
354 }
uint16 WordEntryPos
Definition: ts_type.h:63
#define POSDATALEN(x, e)
Definition: ts_type.h:110
signed int int32
Definition: c.h:347
WordEntryPos pos[1]
Definition: ts_type.h:75
#define POSDATAPTR(x, e)
Definition: ts_type.h:111
void pfree(void *pointer)
Definition: mcxt.c:1056
uint32 haspos
Definition: ts_type.h:44
#define wpos(wep)
Definition: tsrank.c:26
static QueryOperand ** SortAndUniqItems(TSQuery q, int *size)
Definition: tsrank.c:154
static WordEntry * find_wordentry(TSVector t, TSQuery q, QueryOperand *item, int32 *nitem)
Definition: tsrank.c:86
int32 size
Definition: ts_type.h:208
int i

◆ checkcondition_QueryOperand()

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

Definition at line 560 of file tsrank.c.

References MAXQROPOS, ExecPhraseData::npos, QueryRepresentationOperand::npos, QueryRepresentationOperand::operandexists, ExecPhraseData::pos, QueryRepresentationOperand::pos, QR_GET_OPERAND_DATA, and QueryRepresentationOperand::reverseinsert.

Referenced by Cover().

561 {
562  QueryRepresentation *qr = (QueryRepresentation *) checkval;
564 
565  if (!opData->operandexists)
566  return false;
567 
568  if (data)
569  {
570  data->npos = opData->npos;
571  data->pos = opData->pos;
572  if (opData->reverseinsert)
573  data->pos += MAXQROPOS - opData->npos;
574  }
575 
576  return true;
577 }
#define QR_GET_OPERAND_DATA(q, v)
Definition: tsrank.c:556
WordEntryPos * pos
Definition: ts_utils.h:154
#define MAXQROPOS
Definition: tsrank.c:540
WordEntryPos pos[MAXQROPOS]
Definition: tsrank.c:547

◆ cnt_length()

static int cnt_length ( TSVector  t)
static

Definition at line 53 of file tsrank.c.

References ARRPTR, POSDATALEN, and STRPTR.

Referenced by calc_rank(), and calc_rank_cd().

54 {
55  WordEntry *ptr = ARRPTR(t),
56  *end = (WordEntry *) STRPTR(t);
57  int len = 0;
58 
59  while (ptr < end)
60  {
61  int clen = POSDATALEN(t, ptr);
62 
63  if (clen == 0)
64  len += 1;
65  else
66  len += clen;
67 
68  ptr++;
69  }
70 
71  return len;
72 }
#define POSDATALEN(x, e)
Definition: ts_type.h:110
#define STRPTR(x)
Definition: hstore.h:76
#define ARRPTR(x)
Definition: cube.c:24

◆ compareDocR()

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

Definition at line 519 of file tsrank.c.

References DocRepresentation::data, DocRepresentation::map, DocRepresentation::pos, WEP_GETPOS, and WEP_GETWEIGHT.

Referenced by get_docrep().

520 {
521  const DocRepresentation *a = (const DocRepresentation *) va;
522  const DocRepresentation *b = (const DocRepresentation *) vb;
523 
524  if (WEP_GETPOS(a->pos) == WEP_GETPOS(b->pos))
525  {
526  if (WEP_GETWEIGHT(a->pos) == WEP_GETWEIGHT(b->pos))
527  {
528  if (a->data.map.entry == b->data.map.entry)
529  return 0;
530 
531  return (a->data.map.entry > b->data.map.entry) ? 1 : -1;
532  }
533 
534  return (WEP_GETWEIGHT(a->pos) > WEP_GETWEIGHT(b->pos)) ? 1 : -1;
535  }
536 
537  return (WEP_GETPOS(a->pos) > WEP_GETPOS(b->pos)) ? 1 : -1;
538 }
struct DocRepresentation::@33::@35 map
union DocRepresentation::@33 data
#define WEP_GETPOS(x)
Definition: ts_type.h:80
#define WEP_GETWEIGHT(x)
Definition: ts_type.h:79
WordEntryPos pos
Definition: tsrank.c:515

◆ compareQueryOperand()

static int compareQueryOperand ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 135 of file tsrank.c.

References QueryOperand::distance, QueryOperand::length, and tsCompareString().

Referenced by SortAndUniqItems().

136 {
137  char *operand = (char *) arg;
138  QueryOperand *qa = (*(QueryOperand *const *) a);
139  QueryOperand *qb = (*(QueryOperand *const *) b);
140 
141  return tsCompareString(operand + qa->distance, qa->length,
142  operand + qb->distance, qb->length,
143  false);
144 }
uint32 distance
Definition: ts_type.h:158
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1147
uint32 length
Definition: ts_type.h:158
void * arg

◆ Cover()

static bool Cover ( DocRepresentation doc,
int  len,
QueryRepresentation qr,
CoverExt ext 
)
static

Definition at line 642 of file tsrank.c.

References CoverExt::begin, check_stack_depth(), checkcondition_QueryOperand(), CoverExt::end, fillQueryRepresentationData(), GETQUERY, CoverExt::p, DocRepresentation::pos, CoverExt::pos, CoverExt::q, QueryRepresentation::query, resetQueryRepresentation(), TS_EXEC_CALC_NOT, TS_EXEC_EMPTY, TS_execute(), and WEP_GETPOS.

Referenced by calc_rank_cd().

643 {
644  DocRepresentation *ptr;
645  int lastpos = ext->pos;
646  bool found = false;
647 
648  /*
649  * since this function recurses, it could be driven to stack overflow.
650  * (though any decent compiler will optimize away the tail-recursion.
651  */
653 
654  resetQueryRepresentation(qr, false);
655 
656  ext->p = INT_MAX;
657  ext->q = 0;
658  ptr = doc + ext->pos;
659 
660  /* find upper bound of cover from current position, move up */
661  while (ptr - doc < len)
662  {
664 
665  if (TS_execute(GETQUERY(qr->query), (void *) qr,
667  {
668  if (WEP_GETPOS(ptr->pos) > ext->q)
669  {
670  ext->q = WEP_GETPOS(ptr->pos);
671  ext->end = ptr;
672  lastpos = ptr - doc;
673  found = true;
674  }
675  break;
676  }
677  ptr++;
678  }
679 
680  if (!found)
681  return false;
682 
683  resetQueryRepresentation(qr, true);
684 
685  ptr = doc + lastpos;
686 
687  /* find lower bound of cover from found upper bound, move down */
688  while (ptr >= doc + ext->pos)
689  {
690  /*
691  * we scan doc from right to left, so pos info in reverse order!
692  */
694 
695  if (TS_execute(GETQUERY(qr->query), (void *) qr,
697  {
698  if (WEP_GETPOS(ptr->pos) < ext->p)
699  {
700  ext->begin = ptr;
701  ext->p = WEP_GETPOS(ptr->pos);
702  }
703  break;
704  }
705  ptr--;
706  }
707 
708  if (ext->p <= ext->q)
709  {
710  /*
711  * set position for next try to next lexeme after beginning of found
712  * cover
713  */
714  ext->pos = (ptr - doc) + 1;
715  return true;
716  }
717 
718  ext->pos++;
719  return Cover(doc, len, qr, ext);
720 }
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
Definition: tsvector_op.c:1771
DocRepresentation * end
Definition: tsrank.c:585
#define TS_EXEC_CALC_NOT
Definition: ts_utils.h:183
static bool Cover(DocRepresentation *doc, int len, QueryRepresentation *qr, CoverExt *ext)
Definition: tsrank.c:642
static void fillQueryRepresentationData(QueryRepresentation *qr, DocRepresentation *entry)
Definition: tsrank.c:602
#define GETQUERY(x)
Definition: _int.h:136
int p
Definition: tsrank.c:582
#define WEP_GETPOS(x)
Definition: ts_type.h:80
int pos
Definition: tsrank.c:581
void check_stack_depth(void)
Definition: postgres.c:3288
int q
Definition: tsrank.c:583
static void resetQueryRepresentation(QueryRepresentation *qr, bool reverseinsert)
Definition: tsrank.c:589
static bool checkcondition_QueryOperand(void *checkval, QueryOperand *val, ExecPhraseData *data)
Definition: tsrank.c:560
#define TS_EXEC_EMPTY
Definition: ts_utils.h:175
DocRepresentation * begin
Definition: tsrank.c:584
WordEntryPos pos
Definition: tsrank.c:515

◆ fillQueryRepresentationData()

static void fillQueryRepresentationData ( QueryRepresentation qr,
DocRepresentation entry 
)
static

Definition at line 602 of file tsrank.c.

References DocRepresentation::data, i, MAXQROPOS, QueryRepresentationOperand::npos, QueryRepresentationOperand::operandexists, DocRepresentation::pos, QueryRepresentationOperand::pos, QI_VAL, QR_GET_OPERAND_DATA, DocRepresentation::query, QueryRepresentationOperand::reverseinsert, and WEP_GETPOS.

Referenced by Cover().

603 {
604  int i;
605  int lastPos;
607 
608  for (i = 0; i < entry->data.query.nitem; i++)
609  {
610  if (entry->data.query.items[i]->type != QI_VAL)
611  continue;
612 
613  opData = QR_GET_OPERAND_DATA(qr, entry->data.query.items[i]);
614 
615  opData->operandexists = true;
616 
617  if (opData->npos == 0)
618  {
619  lastPos = (opData->reverseinsert) ? (MAXQROPOS - 1) : 0;
620  opData->pos[lastPos] = entry->pos;
621  opData->npos++;
622  continue;
623  }
624 
625  lastPos = opData->reverseinsert ?
626  (MAXQROPOS - opData->npos) :
627  (opData->npos - 1);
628 
629  if (WEP_GETPOS(opData->pos[lastPos]) != WEP_GETPOS(entry->pos))
630  {
631  lastPos = opData->reverseinsert ?
632  (MAXQROPOS - 1 - opData->npos) :
633  (opData->npos);
634 
635  opData->pos[lastPos] = entry->pos;
636  opData->npos++;
637  }
638  }
639 }
#define QR_GET_OPERAND_DATA(q, v)
Definition: tsrank.c:556
#define QI_VAL
Definition: ts_type.h:134
union DocRepresentation::@33 data
#define WEP_GETPOS(x)
Definition: ts_type.h:80
#define MAXQROPOS
Definition: tsrank.c:540
int i
struct DocRepresentation::@33::@34 query
WordEntryPos pos[MAXQROPOS]
Definition: tsrank.c:547
WordEntryPos pos
Definition: tsrank.c:515

◆ find_wordentry()

static WordEntry* find_wordentry ( TSVector  t,
TSQuery  q,
QueryOperand item,
int32 nitem 
)
static

Definition at line 86 of file tsrank.c.

References ARRPTR, difference(), GETOPERAND, QueryOperand::prefix, STRPTR, and WordECompareQueryItem.

Referenced by calc_rank_and(), calc_rank_or(), and get_docrep().

87 {
88  WordEntry *StopLow = ARRPTR(t);
89  WordEntry *StopHigh = (WordEntry *) STRPTR(t);
90  WordEntry *StopMiddle = StopHigh;
91  int difference;
92 
93  *nitem = 0;
94 
95  /* Loop invariant: StopLow <= item < StopHigh */
96  while (StopLow < StopHigh)
97  {
98  StopMiddle = StopLow + (StopHigh - StopLow) / 2;
99  difference = WordECompareQueryItem(STRPTR(t), GETOPERAND(q), StopMiddle, item, false);
100  if (difference == 0)
101  {
102  StopHigh = StopMiddle;
103  *nitem = 1;
104  break;
105  }
106  else if (difference > 0)
107  StopLow = StopMiddle + 1;
108  else
109  StopHigh = StopMiddle;
110  }
111 
112  if (item->prefix)
113  {
114  if (StopLow >= StopHigh)
115  StopMiddle = StopHigh;
116 
117  *nitem = 0;
118 
119  while (StopMiddle < (WordEntry *) STRPTR(t) &&
120  WordECompareQueryItem(STRPTR(t), GETOPERAND(q), StopMiddle, item, true) == 0)
121  {
122  (*nitem)++;
123  StopMiddle++;
124  }
125  }
126 
127  return (*nitem > 0) ? StopHigh : NULL;
128 }
#define GETOPERAND(x)
Definition: ltree.h:118
#define WordECompareQueryItem(e, q, p, i, m)
Definition: tsrank.c:75
Datum difference(PG_FUNCTION_ARGS)
#define STRPTR(x)
Definition: hstore.h:76
bool prefix
Definition: ts_type.h:150
#define ARRPTR(x)
Definition: cube.c:24

◆ get_docrep()

static DocRepresentation* get_docrep ( TSVector  txt,
QueryRepresentation qr,
int *  doclen 
)
static

Definition at line 723 of file tsrank.c.

References compareDocR(), cur, DocRepresentation::data, find_wordentry(), GETQUERY, WordEntry::haspos, i, DocRepresentation::map, palloc(), pfree(), DocRepresentation::pos, POSDATALEN, POSDATAPTR, QI_VAL, QueryItem::qoperand, qsort, QueryRepresentation::query, repalloc(), TSQueryData::size, generate_unaccent_rules::type, QueryOperand::weight, and WEP_GETWEIGHT.

Referenced by calc_rank_cd().

724 {
725  QueryItem *item = GETQUERY(qr->query);
726  WordEntry *entry,
727  *firstentry;
728  WordEntryPos *post;
729  int32 dimt, /* number of 'post' items */
730  j,
731  i,
732  nitem;
733  int len = qr->query->size * 4,
734  cur = 0;
735  DocRepresentation *doc;
736 
737  doc = (DocRepresentation *) palloc(sizeof(DocRepresentation) * len);
738 
739  /*
740  * Iterate through query to make DocRepresentation for words and it's
741  * entries satisfied by query
742  */
743  for (i = 0; i < qr->query->size; i++)
744  {
745  QueryOperand *curoperand;
746 
747  if (item[i].type != QI_VAL)
748  continue;
749 
750  curoperand = &item[i].qoperand;
751 
752  firstentry = entry = find_wordentry(txt, qr->query, curoperand, &nitem);
753  if (!entry)
754  continue;
755 
756  /* iterations over entries in tsvector */
757  while (entry - firstentry < nitem)
758  {
759  if (entry->haspos)
760  {
761  dimt = POSDATALEN(txt, entry);
762  post = POSDATAPTR(txt, entry);
763  }
764  else
765  {
766  /* ignore words without positions */
767  entry++;
768  continue;
769  }
770 
771  while (cur + dimt >= len)
772  {
773  len *= 2;
774  doc = (DocRepresentation *) repalloc(doc, sizeof(DocRepresentation) * len);
775  }
776 
777  /* iterations over entry's positions */
778  for (j = 0; j < dimt; j++)
779  {
780  if (curoperand->weight == 0 ||
781  curoperand->weight & (1 << WEP_GETWEIGHT(post[j])))
782  {
783  doc[cur].pos = post[j];
784  doc[cur].data.map.entry = entry;
785  doc[cur].data.map.item = (QueryItem *) curoperand;
786  cur++;
787  }
788  }
789 
790  entry++;
791  }
792  }
793 
794  if (cur > 0)
795  {
796  DocRepresentation *rptr = doc + 1,
797  *wptr = doc,
798  storage;
799 
800  /*
801  * Sort representation in ascending order by pos and entry
802  */
803  qsort((void *) doc, cur, sizeof(DocRepresentation), compareDocR);
804 
805  /*
806  * Join QueryItem per WordEntry and it's position
807  */
808  storage.pos = doc->pos;
809  storage.data.query.items = palloc(sizeof(QueryItem *) * qr->query->size);
810  storage.data.query.items[0] = doc->data.map.item;
811  storage.data.query.nitem = 1;
812 
813  while (rptr - doc < cur)
814  {
815  if (rptr->pos == (rptr - 1)->pos &&
816  rptr->data.map.entry == (rptr - 1)->data.map.entry)
817  {
818  storage.data.query.items[storage.data.query.nitem] = rptr->data.map.item;
819  storage.data.query.nitem++;
820  }
821  else
822  {
823  *wptr = storage;
824  wptr++;
825  storage.pos = rptr->pos;
826  storage.data.query.items = palloc(sizeof(QueryItem *) * qr->query->size);
827  storage.data.query.items[0] = rptr->data.map.item;
828  storage.data.query.nitem = 1;
829  }
830 
831  rptr++;
832  }
833 
834  *wptr = storage;
835  wptr++;
836 
837  *doclen = wptr - doc;
838  return doc;
839  }
840 
841  pfree(doc);
842  return NULL;
843 }
uint16 WordEntryPos
Definition: ts_type.h:63
struct DocRepresentation::@33::@35 map
#define POSDATALEN(x, e)
Definition: ts_type.h:110
struct cursor * cur
Definition: ecpg.c:28
#define QI_VAL
Definition: ts_type.h:134
static int compareDocR(const void *va, const void *vb)
Definition: tsrank.c:519
#define GETQUERY(x)
Definition: _int.h:136
signed int int32
Definition: c.h:347
#define POSDATAPTR(x, e)
Definition: ts_type.h:111
void pfree(void *pointer)
Definition: mcxt.c:1056
union DocRepresentation::@33 data
uint8 weight
Definition: ts_type.h:146
uint32 haspos
Definition: ts_type.h:44
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
static WordEntry * find_wordentry(TSVector t, TSQuery q, QueryOperand *item, int32 *nitem)
Definition: tsrank.c:86
void * palloc(Size size)
Definition: mcxt.c:949
int32 size
Definition: ts_type.h:208
int i
#define WEP_GETWEIGHT(x)
Definition: ts_type.h:79
QueryOperand qoperand
Definition: ts_type.h:197
#define qsort(a, b, c, d)
Definition: port.h:491
WordEntryPos pos
Definition: tsrank.c:515

◆ getWeights()

static const float* getWeights ( ArrayType win)
static

Definition at line 400 of file tsrank.c.

References ARR_DATA_PTR, ARR_DIMS, ARR_NDIM, array_contains_nulls(), ArrayGetNItems(), ereport, errcode(), errmsg(), ERROR, i, lengthof, and weights.

Referenced by ts_rank_tt(), ts_rank_ttf(), ts_rank_wtt(), ts_rank_wttf(), ts_rankcd_tt(), ts_rankcd_ttf(), ts_rankcd_wtt(), and ts_rankcd_wttf().

401 {
402  static float ws[lengthof(weights)];
403  int i;
404  float4 *arrdata;
405 
406  if (win == NULL)
407  return weights;
408 
409  if (ARR_NDIM(win) != 1)
410  ereport(ERROR,
411  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
412  errmsg("array of weight must be one-dimensional")));
413 
414  if (ArrayGetNItems(ARR_NDIM(win), ARR_DIMS(win)) < lengthof(weights))
415  ereport(ERROR,
416  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
417  errmsg("array of weight is too short")));
418 
419  if (array_contains_nulls(win))
420  ereport(ERROR,
421  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
422  errmsg("array of weight must not contain nulls")));
423 
424  arrdata = (float4 *) ARR_DATA_PTR(win);
425  for (i = 0; i < lengthof(weights); i++)
426  {
427  ws[i] = (arrdata[i] >= 0) ? arrdata[i] : weights[i];
428  if (ws[i] > 1.0)
429  ereport(ERROR,
430  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
431  errmsg("weight out of range")));
432  }
433 
434  return ws;
435 }
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
int errcode(int sqlerrcode)
Definition: elog.c:608
#define lengthof(array)
Definition: c.h:669
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:282
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ereport(elevel, rest)
Definition: elog.h:141
float float4
Definition: c.h:491
#define ARR_NDIM(a)
Definition: array.h:278
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3528
static const float weights[]
Definition: tsrank.c:24

◆ resetQueryRepresentation()

static void resetQueryRepresentation ( QueryRepresentation qr,
bool  reverseinsert 
)
static

Definition at line 589 of file tsrank.c.

References i, QueryRepresentationOperand::npos, QueryRepresentation::operandData, QueryRepresentationOperand::operandexists, QueryRepresentation::query, QueryRepresentationOperand::reverseinsert, and TSQueryData::size.

Referenced by Cover().

590 {
591  int i;
592 
593  for (i = 0; i < qr->query->size; i++)
594  {
595  qr->operandData[i].operandexists = false;
596  qr->operandData[i].reverseinsert = reverseinsert;
597  qr->operandData[i].npos = 0;
598  }
599 }
QueryRepresentationOperand * operandData
Definition: tsrank.c:553
int32 size
Definition: ts_type.h:208
int i

◆ SortAndUniqItems()

static QueryOperand** SortAndUniqItems ( TSQuery  q,
int *  size 
)
static

Definition at line 154 of file tsrank.c.

References compareQueryOperand(), GETOPERAND, GETQUERY, palloc(), QI_VAL, qsort_arg(), and QueryItem::type.

Referenced by calc_rank_and(), and calc_rank_or().

155 {
156  char *operand = GETOPERAND(q);
157  QueryItem *item = GETQUERY(q);
158  QueryOperand **res,
159  **ptr,
160  **prevptr;
161 
162  ptr = res = (QueryOperand **) palloc(sizeof(QueryOperand *) * *size);
163 
164  /* Collect all operands from the tree to res */
165  while ((*size)--)
166  {
167  if (item->type == QI_VAL)
168  {
169  *ptr = (QueryOperand *) item;
170  ptr++;
171  }
172  item++;
173  }
174 
175  *size = ptr - res;
176  if (*size < 2)
177  return res;
178 
179  qsort_arg(res, *size, sizeof(QueryOperand *), compareQueryOperand, (void *) operand);
180 
181  ptr = res + 1;
182  prevptr = res;
183 
184  /* remove duplicates */
185  while (ptr - res < *size)
186  {
187  if (compareQueryOperand((void *) ptr, (void *) prevptr, (void *) operand) != 0)
188  {
189  prevptr++;
190  *prevptr = *ptr;
191  }
192  ptr++;
193  }
194 
195  *size = prevptr + 1 - res;
196  return res;
197 }
#define QI_VAL
Definition: ts_type.h:134
#define GETQUERY(x)
Definition: _int.h:136
#define GETOPERAND(x)
Definition: ltree.h:118
static int compareQueryOperand(const void *a, const void *b, void *arg)
Definition: tsrank.c:135
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
Definition: qsort_arg.c:113
QueryItemType type
Definition: ts_type.h:195
void * palloc(Size size)
Definition: mcxt.c:949

◆ ts_rank_tt()

Datum ts_rank_tt ( PG_FUNCTION_ARGS  )

Definition at line 486 of file tsrank.c.

References calc_rank(), DEF_NORM_METHOD, getWeights(), PG_FREE_IF_COPY, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, and PG_RETURN_FLOAT4.

487 {
488  TSVector txt = PG_GETARG_TSVECTOR(0);
489  TSQuery query = PG_GETARG_TSQUERY(1);
490  float res;
491 
492  res = calc_rank(getWeights(NULL), txt, query, DEF_NORM_METHOD);
493 
494  PG_FREE_IF_COPY(txt, 0);
495  PG_FREE_IF_COPY(query, 1);
496  PG_RETURN_FLOAT4(res);
497 }
#define DEF_NORM_METHOD
Definition: tsrank.c:35
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
static float calc_rank(const float *w, TSVector t, TSQuery q, int32 method)
Definition: tsrank.c:357
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
static const float * getWeights(ArrayType *win)
Definition: tsrank.c:400

◆ ts_rank_ttf()

Datum ts_rank_ttf ( PG_FUNCTION_ARGS  )

Definition at line 471 of file tsrank.c.

References calc_rank(), getWeights(), PG_FREE_IF_COPY, PG_GETARG_INT32, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, and PG_RETURN_FLOAT4.

472 {
473  TSVector txt = PG_GETARG_TSVECTOR(0);
474  TSQuery query = PG_GETARG_TSQUERY(1);
475  int method = PG_GETARG_INT32(2);
476  float res;
477 
478  res = calc_rank(getWeights(NULL), txt, query, method);
479 
480  PG_FREE_IF_COPY(txt, 0);
481  PG_FREE_IF_COPY(query, 1);
482  PG_RETURN_FLOAT4(res);
483 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
static float calc_rank(const float *w, TSVector t, TSQuery q, int32 method)
Definition: tsrank.c:357
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
static const float * getWeights(ArrayType *win)
Definition: tsrank.c:400

◆ ts_rank_wtt()

Datum ts_rank_wtt ( PG_FUNCTION_ARGS  )

Definition at line 455 of file tsrank.c.

References calc_rank(), DEF_NORM_METHOD, getWeights(), PG_DETOAST_DATUM, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, and PG_RETURN_FLOAT4.

456 {
458  TSVector txt = PG_GETARG_TSVECTOR(1);
459  TSQuery query = PG_GETARG_TSQUERY(2);
460  float res;
461 
462  res = calc_rank(getWeights(win), txt, query, DEF_NORM_METHOD);
463 
464  PG_FREE_IF_COPY(win, 0);
465  PG_FREE_IF_COPY(txt, 1);
466  PG_FREE_IF_COPY(query, 2);
467  PG_RETURN_FLOAT4(res);
468 }
#define DEF_NORM_METHOD
Definition: tsrank.c:35
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
static float calc_rank(const float *w, TSVector t, TSQuery q, int32 method)
Definition: tsrank.c:357
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:235
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
static const float * getWeights(ArrayType *win)
Definition: tsrank.c:400

◆ ts_rank_wttf()

Datum ts_rank_wttf ( PG_FUNCTION_ARGS  )

Definition at line 438 of file tsrank.c.

References calc_rank(), getWeights(), PG_DETOAST_DATUM, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_GETARG_INT32, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, and PG_RETURN_FLOAT4.

439 {
441  TSVector txt = PG_GETARG_TSVECTOR(1);
442  TSQuery query = PG_GETARG_TSQUERY(2);
443  int method = PG_GETARG_INT32(3);
444  float res;
445 
446  res = calc_rank(getWeights(win), txt, query, method);
447 
448  PG_FREE_IF_COPY(win, 0);
449  PG_FREE_IF_COPY(txt, 1);
450  PG_FREE_IF_COPY(query, 2);
451  PG_RETURN_FLOAT4(res);
452 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
static float calc_rank(const float *w, TSVector t, TSQuery q, int32 method)
Definition: tsrank.c:357
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:235
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
static const float * getWeights(ArrayType *win)
Definition: tsrank.c:400

◆ ts_rankcd_tt()

Datum ts_rankcd_tt ( PG_FUNCTION_ARGS  )

Definition at line 997 of file tsrank.c.

References calc_rank_cd(), DEF_NORM_METHOD, getWeights(), PG_FREE_IF_COPY, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, and PG_RETURN_FLOAT4.

998 {
999  TSVector txt = PG_GETARG_TSVECTOR(0);
1000  TSQuery query = PG_GETARG_TSQUERY(1);
1001  float res;
1002 
1003  res = calc_rank_cd(getWeights(NULL), txt, query, DEF_NORM_METHOD);
1004 
1005  PG_FREE_IF_COPY(txt, 0);
1006  PG_FREE_IF_COPY(query, 1);
1007  PG_RETURN_FLOAT4(res);
1008 }
#define DEF_NORM_METHOD
Definition: tsrank.c:35
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
static float4 calc_rank_cd(const float4 *arrdata, TSVector txt, TSQuery query, int method)
Definition: tsrank.c:846
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
static const float * getWeights(ArrayType *win)
Definition: tsrank.c:400

◆ ts_rankcd_ttf()

Datum ts_rankcd_ttf ( PG_FUNCTION_ARGS  )

Definition at line 982 of file tsrank.c.

References calc_rank_cd(), getWeights(), PG_FREE_IF_COPY, PG_GETARG_INT32, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, and PG_RETURN_FLOAT4.

983 {
984  TSVector txt = PG_GETARG_TSVECTOR(0);
985  TSQuery query = PG_GETARG_TSQUERY(1);
986  int method = PG_GETARG_INT32(2);
987  float res;
988 
989  res = calc_rank_cd(getWeights(NULL), txt, query, method);
990 
991  PG_FREE_IF_COPY(txt, 0);
992  PG_FREE_IF_COPY(query, 1);
993  PG_RETURN_FLOAT4(res);
994 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
static float4 calc_rank_cd(const float4 *arrdata, TSVector txt, TSQuery query, int method)
Definition: tsrank.c:846
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
static const float * getWeights(ArrayType *win)
Definition: tsrank.c:400

◆ ts_rankcd_wtt()

Datum ts_rankcd_wtt ( PG_FUNCTION_ARGS  )

Definition at line 966 of file tsrank.c.

References calc_rank_cd(), DEF_NORM_METHOD, getWeights(), PG_DETOAST_DATUM, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, and PG_RETURN_FLOAT4.

967 {
969  TSVector txt = PG_GETARG_TSVECTOR(1);
970  TSQuery query = PG_GETARG_TSQUERY(2);
971  float res;
972 
973  res = calc_rank_cd(getWeights(win), txt, query, DEF_NORM_METHOD);
974 
975  PG_FREE_IF_COPY(win, 0);
976  PG_FREE_IF_COPY(txt, 1);
977  PG_FREE_IF_COPY(query, 2);
978  PG_RETURN_FLOAT4(res);
979 }
#define DEF_NORM_METHOD
Definition: tsrank.c:35
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
static float4 calc_rank_cd(const float4 *arrdata, TSVector txt, TSQuery query, int method)
Definition: tsrank.c:846
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:235
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
static const float * getWeights(ArrayType *win)
Definition: tsrank.c:400

◆ ts_rankcd_wttf()

Datum ts_rankcd_wttf ( PG_FUNCTION_ARGS  )

Definition at line 949 of file tsrank.c.

References calc_rank_cd(), getWeights(), PG_DETOAST_DATUM, PG_FREE_IF_COPY, PG_GETARG_DATUM, PG_GETARG_INT32, PG_GETARG_TSQUERY, PG_GETARG_TSVECTOR, and PG_RETURN_FLOAT4.

950 {
952  TSVector txt = PG_GETARG_TSVECTOR(1);
953  TSQuery query = PG_GETARG_TSQUERY(2);
954  int method = PG_GETARG_INT32(3);
955  float res;
956 
957  res = calc_rank_cd(getWeights(win), txt, query, method);
958 
959  PG_FREE_IF_COPY(win, 0);
960  PG_FREE_IF_COPY(txt, 1);
961  PG_FREE_IF_COPY(query, 2);
962  PG_RETURN_FLOAT4(res);
963 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
static float4 calc_rank_cd(const float4 *arrdata, TSVector txt, TSQuery query, int method)
Definition: tsrank.c:846
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:235
#define PG_GETARG_TSVECTOR(n)
Definition: ts_type.h:120
static const float * getWeights(ArrayType *win)
Definition: tsrank.c:400

◆ word_distance()

static float4 word_distance ( int32  w)
static

Definition at line 44 of file tsrank.c.

Referenced by calc_rank_and().

45 {
46  if (w > 100)
47  return 1e-30f;
48 
49  return 1.0 / (1.005 + 0.05 * exp(((float4) w) / 1.5 - 2));
50 }
float float4
Definition: c.h:491
e
Definition: preproc-init.c:82

Variable Documentation

◆ weights

const float weights[] = {0.1f, 0.2f, 0.4f, 1.0f}
static

Definition at line 24 of file tsrank.c.

Referenced by calc_rank_cd(), getWeights(), tsvector_filter(), and tsvector_unnest().