|
|
|
|
@ -21,7 +21,8 @@ |
|
|
|
|
#include "utils/array.h" |
|
|
|
|
#include "utils/fmgrprotos.h" |
|
|
|
|
|
|
|
|
|
static const float weights[] = {0.1f, 0.2f, 0.4f, 1.0f}; |
|
|
|
|
#define NUM_WEIGHTS 4 |
|
|
|
|
static const float default_weights[NUM_WEIGHTS] = {0.1f, 0.2f, 0.4f, 1.0f}; |
|
|
|
|
|
|
|
|
|
#define wpos(wep) ( w[ WEP_GETWEIGHT(wep) ] ) |
|
|
|
|
|
|
|
|
|
@ -396,22 +397,24 @@ calc_rank(const float *w, TSVector t, TSQuery q, int32 method) |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static const float * |
|
|
|
|
getWeights(ArrayType *win) |
|
|
|
|
/*
|
|
|
|
|
* Extract weights from an array. The weights are stored in *ws, which must |
|
|
|
|
* have space for NUM_WEIGHTS elements. |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
getWeights(ArrayType *win, float *ws) |
|
|
|
|
{ |
|
|
|
|
static float ws[lengthof(weights)]; |
|
|
|
|
int i; |
|
|
|
|
float4 *arrdata; |
|
|
|
|
|
|
|
|
|
if (win == NULL) |
|
|
|
|
return weights; |
|
|
|
|
Assert(win != NULL); |
|
|
|
|
|
|
|
|
|
if (ARR_NDIM(win) != 1) |
|
|
|
|
ereport(ERROR, |
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
|
|
|
|
errmsg("array of weight must be one-dimensional"))); |
|
|
|
|
|
|
|
|
|
if (ArrayGetNItems(ARR_NDIM(win), ARR_DIMS(win)) < lengthof(weights)) |
|
|
|
|
if (ArrayGetNItems(ARR_NDIM(win), ARR_DIMS(win)) < NUM_WEIGHTS) |
|
|
|
|
ereport(ERROR, |
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
|
|
|
|
errmsg("array of weight is too short"))); |
|
|
|
|
@ -422,16 +425,14 @@ getWeights(ArrayType *win) |
|
|
|
|
errmsg("array of weight must not contain nulls"))); |
|
|
|
|
|
|
|
|
|
arrdata = (float4 *) ARR_DATA_PTR(win); |
|
|
|
|
for (i = 0; i < lengthof(weights); i++) |
|
|
|
|
for (i = 0; i < NUM_WEIGHTS; i++) |
|
|
|
|
{ |
|
|
|
|
ws[i] = (arrdata[i] >= 0) ? arrdata[i] : weights[i]; |
|
|
|
|
ws[i] = (arrdata[i] >= 0) ? arrdata[i] : default_weights[i]; |
|
|
|
|
if (ws[i] > 1.0) |
|
|
|
|
ereport(ERROR, |
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
|
|
|
|
errmsg("weight out of range"))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ws; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Datum |
|
|
|
|
@ -441,9 +442,11 @@ ts_rank_wttf(PG_FUNCTION_ARGS) |
|
|
|
|
TSVector txt = PG_GETARG_TSVECTOR(1); |
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(2); |
|
|
|
|
int method = PG_GETARG_INT32(3); |
|
|
|
|
float weights[NUM_WEIGHTS]; |
|
|
|
|
float res; |
|
|
|
|
|
|
|
|
|
res = calc_rank(getWeights(win), txt, query, method); |
|
|
|
|
getWeights(win, weights); |
|
|
|
|
res = calc_rank(weights, txt, query, method); |
|
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(win, 0); |
|
|
|
|
PG_FREE_IF_COPY(txt, 1); |
|
|
|
|
@ -457,9 +460,11 @@ ts_rank_wtt(PG_FUNCTION_ARGS) |
|
|
|
|
ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); |
|
|
|
|
TSVector txt = PG_GETARG_TSVECTOR(1); |
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(2); |
|
|
|
|
float weights[NUM_WEIGHTS]; |
|
|
|
|
float res; |
|
|
|
|
|
|
|
|
|
res = calc_rank(getWeights(win), txt, query, DEF_NORM_METHOD); |
|
|
|
|
getWeights(win, weights); |
|
|
|
|
res = calc_rank(weights, txt, query, DEF_NORM_METHOD); |
|
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(win, 0); |
|
|
|
|
PG_FREE_IF_COPY(txt, 1); |
|
|
|
|
@ -475,7 +480,7 @@ ts_rank_ttf(PG_FUNCTION_ARGS) |
|
|
|
|
int method = PG_GETARG_INT32(2); |
|
|
|
|
float res; |
|
|
|
|
|
|
|
|
|
res = calc_rank(getWeights(NULL), txt, query, method); |
|
|
|
|
res = calc_rank(default_weights, txt, query, method); |
|
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(txt, 0); |
|
|
|
|
PG_FREE_IF_COPY(query, 1); |
|
|
|
|
@ -489,7 +494,7 @@ ts_rank_tt(PG_FUNCTION_ARGS) |
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(1); |
|
|
|
|
float res; |
|
|
|
|
|
|
|
|
|
res = calc_rank(getWeights(NULL), txt, query, DEF_NORM_METHOD); |
|
|
|
|
res = calc_rank(default_weights, txt, query, DEF_NORM_METHOD); |
|
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(txt, 0); |
|
|
|
|
PG_FREE_IF_COPY(query, 1); |
|
|
|
|
@ -855,16 +860,16 @@ calc_rank_cd(const float4 *arrdata, TSVector txt, TSQuery query, int method) |
|
|
|
|
doclen = 0; |
|
|
|
|
CoverExt ext; |
|
|
|
|
double Wdoc = 0.0; |
|
|
|
|
double invws[lengthof(weights)]; |
|
|
|
|
double invws[NUM_WEIGHTS]; |
|
|
|
|
double SumDist = 0.0, |
|
|
|
|
PrevExtPos = 0.0; |
|
|
|
|
int NExtent = 0; |
|
|
|
|
QueryRepresentation qr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < lengthof(weights); i++) |
|
|
|
|
for (i = 0; i < NUM_WEIGHTS; i++) |
|
|
|
|
{ |
|
|
|
|
invws[i] = ((double) ((arrdata[i] >= 0) ? arrdata[i] : weights[i])); |
|
|
|
|
invws[i] = ((double) ((arrdata[i] >= 0) ? arrdata[i] : default_weights[i])); |
|
|
|
|
if (invws[i] > 1.0) |
|
|
|
|
ereport(ERROR, |
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
|
|
|
|
@ -956,9 +961,11 @@ ts_rankcd_wttf(PG_FUNCTION_ARGS) |
|
|
|
|
TSVector txt = PG_GETARG_TSVECTOR(1); |
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(2); |
|
|
|
|
int method = PG_GETARG_INT32(3); |
|
|
|
|
float weights[NUM_WEIGHTS]; |
|
|
|
|
float res; |
|
|
|
|
|
|
|
|
|
res = calc_rank_cd(getWeights(win), txt, query, method); |
|
|
|
|
getWeights(win, weights); |
|
|
|
|
res = calc_rank_cd(weights, txt, query, method); |
|
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(win, 0); |
|
|
|
|
PG_FREE_IF_COPY(txt, 1); |
|
|
|
|
@ -972,9 +979,11 @@ ts_rankcd_wtt(PG_FUNCTION_ARGS) |
|
|
|
|
ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); |
|
|
|
|
TSVector txt = PG_GETARG_TSVECTOR(1); |
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(2); |
|
|
|
|
float weights[NUM_WEIGHTS]; |
|
|
|
|
float res; |
|
|
|
|
|
|
|
|
|
res = calc_rank_cd(getWeights(win), txt, query, DEF_NORM_METHOD); |
|
|
|
|
getWeights(win, weights); |
|
|
|
|
res = calc_rank_cd(weights, txt, query, DEF_NORM_METHOD); |
|
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(win, 0); |
|
|
|
|
PG_FREE_IF_COPY(txt, 1); |
|
|
|
|
@ -990,7 +999,7 @@ ts_rankcd_ttf(PG_FUNCTION_ARGS) |
|
|
|
|
int method = PG_GETARG_INT32(2); |
|
|
|
|
float res; |
|
|
|
|
|
|
|
|
|
res = calc_rank_cd(getWeights(NULL), txt, query, method); |
|
|
|
|
res = calc_rank_cd(default_weights, txt, query, method); |
|
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(txt, 0); |
|
|
|
|
PG_FREE_IF_COPY(query, 1); |
|
|
|
|
@ -1004,7 +1013,7 @@ ts_rankcd_tt(PG_FUNCTION_ARGS) |
|
|
|
|
TSQuery query = PG_GETARG_TSQUERY(1); |
|
|
|
|
float res; |
|
|
|
|
|
|
|
|
|
res = calc_rank_cd(getWeights(NULL), txt, query, DEF_NORM_METHOD); |
|
|
|
|
res = calc_rank_cd(default_weights, txt, query, DEF_NORM_METHOD); |
|
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(txt, 0); |
|
|
|
|
PG_FREE_IF_COPY(query, 1); |
|
|
|
|
|