22 #include "utils/fmgrprotos.h"
27 #define wpos(wep) ( w[ WEP_GETWEIGHT(wep) ] )
29 #define RANK_NO_NORM 0x00
30 #define RANK_NORM_LOGLENGTH 0x01
31 #define RANK_NORM_LENGTH 0x02
32 #define RANK_NORM_EXTDIST 0x04
33 #define RANK_NORM_UNIQ 0x08
34 #define RANK_NORM_LOGUNIQ 0x10
35 #define RANK_NORM_RDIVRPLUS1 0x20
36 #define DEF_NORM_METHOD RANK_NO_NORM
50 return 1.0 / (1.005 + 0.05 * exp(((
float4) w) / 1.5 - 2));
76 #define WordECompareQueryItem(e,q,p,i,m) \
77 tsCompareString((q) + (i)->distance, (i)->length, \
78 (e) + (p)->pos, (p)->len, (m))
97 while (StopLow < StopHigh)
99 StopMiddle = StopLow + (StopHigh - StopLow) / 2;
103 StopHigh = StopMiddle;
108 StopLow = StopMiddle + 1;
110 StopHigh = StopMiddle;
115 if (StopLow >= StopHigh)
116 StopMiddle = StopHigh;
128 return (*nitem > 0) ? StopHigh : NULL;
138 char *operand = (
char *)
arg;
242 while (entry - firstentry < nitem)
251 for (k = 0; k <
i; k++)
255 lenct = pos[k]->
npos;
257 for (l = 0; l < dimt; l++)
259 for (p = 0; p < lenct; p++)
262 if (dist || (dist == 0 && (pos[
i] == POSNULL || pos[k] == POSNULL)))
269 res = (
res < 0) ? curw : 1.0 - (1.0 -
res) * (1.0 - curw);
314 while (entry - firstentry < nitem)
330 for (
j = 0;
j < dimt;
j++)
332 resj = resj +
wpos(post[
j]) / ((
j + 1) * (
j + 1));
333 if (
wpos(post[
j]) > wjm)
346 res =
res + (wjm + resj - wjm / ((jm + 1) * (jm + 1))) / 1.64493406685;
392 res /= log((
double) (t->
size + 1)) / log(2.0);
414 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
415 errmsg(
"array of weight must be one-dimensional")));
419 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
420 errmsg(
"array of weight is too short")));
424 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
425 errmsg(
"array of weight must not contain nulls")));
433 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
434 errmsg(
"weight out of range")));
533 if (
a->data.map.entry ==
b->data.map.entry)
536 return (
a->data.map.entry >
b->data.map.entry) ? 1 : -1;
545 #define MAXQROPOS MAXENTRYPOS
561 #define QR_GET_OPERAND_DATA(q, v) \
562 ( (q)->operandData + (((QueryItem*)(v)) - GETQUERY((q)->query)) )
626 if (opData->
npos == 0)
629 opData->
pos[lastPos] = entry->
pos;
644 opData->
pos[lastPos] = entry->
pos;
654 int lastpos = ext->
pos;
667 ptr = doc + ext->
pos;
670 while (ptr - doc <
len)
697 while (ptr >= doc + ext->
pos)
717 if (ext->
p <= ext->
q)
723 ext->
pos = (ptr - doc) + 1;
766 while (entry - firstentry < nitem)
787 for (
j = 0;
j < dimt;
j++)
789 if (curoperand->
weight == 0 ||
822 while (rptr - doc <
cur)
824 if (rptr->
pos == (rptr - 1)->pos &&
825 rptr->
data.
map.entry == (rptr - 1)->data.map.entry)
846 *doclen = wptr - doc;
864 double SumDist = 0.0,
875 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
876 errmsg(
"weight out of range")));
877 invws[
i] = 1.0 / invws[
i];
892 while (
Cover(doc, doclen, &qr, &ext))
900 while (ptr <= ext.
end)
906 Cpos = ((double) (ext.
end - ext.
begin + 1)) / InvSum;
913 nNoise = (ext.
q - ext.
p) - (ext.
end - ext.
begin);
916 Wdoc += Cpos / ((double) (1 + nNoise));
918 CurExtPos = ((double) (ext.
q + ext.
p)) / 2.0;
919 if (NExtent > 0 && CurExtPos > PrevExtPos
922 SumDist += 1.0 / (CurExtPos - PrevExtPos);
924 PrevExtPos = CurExtPos;
935 Wdoc /= (double)
len;
939 Wdoc /= ((
double) NExtent) / SumDist;
942 Wdoc /= (
double) (txt->
size);
945 Wdoc /= log((
double) (txt->
size + 1)) / log(2.0);
bool array_contains_nulls(ArrayType *array)
int ArrayGetNItems(int ndim, const int *dims)
#define Assert(condition)
#define MemSet(start, val, len)
static void PGresult * res
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define PG_FREE_IF_COPY(ptr, n)
#define PG_GETARG_DATUM(n)
#define PG_DETOAST_DATUM(datum)
#define PG_GETARG_INT32(n)
#define PG_RETURN_FLOAT4(x)
Datum difference(PG_FUNCTION_ARGS)
void pfree(void *pointer)
void * palloc0(Size size)
void * repalloc(void *pointer, Size size)
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
#define qsort(a, b, c, d)
void check_stack_depth(void)
static pg_noinline void Size size
DocRepresentation * begin
struct DocRepresentation::@30::@32 map
struct DocRepresentation::@30::@31 query
union DocRepresentation::@30 data
WordEntryPos pos[MAXQROPOS]
QueryRepresentationOperand * operandData
WordEntryPos pos[FLEXIBLE_ARRAY_MEMBER]
#define PG_GETARG_TSVECTOR(n)
#define PG_GETARG_TSQUERY(n)
#define RANK_NORM_RDIVRPLUS1
Datum ts_rankcd_wtt(PG_FUNCTION_ARGS)
Datum ts_rankcd_wttf(PG_FUNCTION_ARGS)
#define WordECompareQueryItem(e, q, p, i, m)
static int compareDocR(const void *va, const void *vb)
#define RANK_NORM_LOGLENGTH
static TSTernaryValue checkcondition_QueryOperand(void *checkval, QueryOperand *val, ExecPhraseData *data)
static void resetQueryRepresentation(QueryRepresentation *qr, bool reverseinsert)
static float4 calc_rank_cd(const float4 *arrdata, TSVector txt, TSQuery query, int method)
static float calc_rank(const float *w, TSVector t, TSQuery q, int32 method)
static int cnt_length(TSVector t)
static float4 word_distance(int32 w)
static DocRepresentation * get_docrep(TSVector txt, QueryRepresentation *qr, int *doclen)
static int compareQueryOperand(const void *a, const void *b, void *arg)
Datum ts_rank_ttf(PG_FUNCTION_ARGS)
Datum ts_rank_wttf(PG_FUNCTION_ARGS)
Datum ts_rank_tt(PG_FUNCTION_ARGS)
static WordEntry * find_wordentry(TSVector t, TSQuery q, QueryOperand *item, int32 *nitem)
static QueryOperand ** SortAndUniqItems(TSQuery q, int *size)
static void getWeights(ArrayType *win, float *ws)
static float calc_rank_or(const float *w, TSVector t, TSQuery q)
static void fillQueryRepresentationData(QueryRepresentation *qr, DocRepresentation *entry)
static const float default_weights[NUM_WEIGHTS]
Datum ts_rank_wtt(PG_FUNCTION_ARGS)
#define QR_GET_OPERAND_DATA(q, v)
Datum ts_rankcd_ttf(PG_FUNCTION_ARGS)
Datum ts_rankcd_tt(PG_FUNCTION_ARGS)
#define RANK_NORM_EXTDIST
static float calc_rank_and(const float *w, TSVector t, TSQuery q)
static bool Cover(DocRepresentation *doc, int len, QueryRepresentation *qr, CoverExt *ext)
#define RANK_NORM_LOGUNIQ
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)