3 * Teodor Sigaev <teodor@sigaev.ru>
8 #include "access/gist.h"
9 #include "access/itup.h"
10 #include "utils/builtins.h"
13 #include "storage/bufpage.h"
14 #include "executor/spi.h"
15 #include "commands/trigger.h"
16 #include "nodes/pg_list.h"
17 #include "catalog/namespace.h"
19 #include "utils/array.h"
25 PG_FUNCTION_INFO_V1(rank);
26 Datum rank(PG_FUNCTION_ARGS);
28 PG_FUNCTION_INFO_V1(rank_def);
29 Datum rank_def(PG_FUNCTION_ARGS);
31 PG_FUNCTION_INFO_V1(rank_cd);
32 Datum rank_cd(PG_FUNCTION_ARGS);
34 PG_FUNCTION_INFO_V1(rank_cd_def);
35 Datum rank_cd_def(PG_FUNCTION_ARGS);
37 PG_FUNCTION_INFO_V1(get_covers);
38 Datum get_covers(PG_FUNCTION_ARGS);
40 static float weights[] = {0.1, 0.2, 0.4, 1.0};
42 #define wpos(wep) ( w[ WEP_GETWEIGHT(wep) ] )
44 #define DEF_NORM_METHOD 0
46 static float calc_rank_or(float *w, tsvector * t, QUERYTYPE * q);
47 static float calc_rank_and(float *w, tsvector * t, QUERYTYPE * q);
50 * Returns a weight of a word collocation
58 return 1.0 / (1.005 + 0.05 * exp(((float4) w) / 1.5 - 2));
62 cnt_length(tsvector * t)
64 WordEntry *ptr = ARRPTR(t),
65 *end = (WordEntry *) STRPTR(t);
71 if ((clen = POSDATALEN(t, ptr)) == 0)
82 WordECompareITEM(char *eval, char *qval, WordEntry * ptr, ITEM * item)
84 if (ptr->len == item->length)
87 qval + item->distance,
90 return (ptr->len > item->length) ? 1 : -1;
94 find_wordentry(tsvector * t, QUERYTYPE * q, ITEM * item)
96 WordEntry *StopLow = ARRPTR(t);
97 WordEntry *StopHigh = (WordEntry *) STRPTR(t);
98 WordEntry *StopMiddle;
101 /* Loop invariant: StopLow <= item < StopHigh */
103 while (StopLow < StopHigh)
105 StopMiddle = StopLow + (StopHigh - StopLow) / 2;
106 difference = WordECompareITEM(STRPTR(t), GETOPERAND(q), StopMiddle, item);
109 else if (difference < 0)
110 StopLow = StopMiddle + 1;
112 StopHigh = StopMiddle;
119 static char *SortAndUniqOperand = NULL;
122 compareITEM(const void *a, const void *b)
124 if ((*(ITEM **) a)->length == (*(ITEM **) b)->length)
125 return strncmp(SortAndUniqOperand + (*(ITEM **) a)->distance,
126 SortAndUniqOperand + (*(ITEM **) b)->distance,
127 (*(ITEM **) b)->length);
129 return ((*(ITEM **) a)->length > (*(ITEM **) b)->length) ? 1 : -1;
133 SortAndUniqItems(char *operand, ITEM * item, int *size)
139 ptr = res = (ITEM **) palloc(sizeof(ITEM *) * *size);
143 if (item->type == VAL)
155 SortAndUniqOperand = operand;
156 qsort(res, *size, sizeof(ITEM **), compareITEM);
161 while (ptr - res < *size)
163 if (compareITEM((void *) ptr, (void *) prevptr) != 0)
171 *size = prevptr + 1 - res;
175 static WordEntryPos POSNULL[] = {
181 calc_rank_and(float *w, tsvector * t, QUERYTYPE * q)
198 item = SortAndUniqItems(GETOPERAND(q), GETQUERY(q), &size);
202 return calc_rank_or(w, t, q);
204 pos = (uint16 **) palloc(sizeof(uint16 *) * q->size);
205 memset(pos, 0, sizeof(uint16 *) * q->size);
206 *(uint16 *) POSNULL = lengthof(POSNULL) - 1;
207 WEP_SETPOS(POSNULL[1], MAXENTRYPOS - 1);
209 for (i = 0; i < size; i++)
211 entry = find_wordentry(t, q, item[i]);
216 pos[i] = (uint16 *) _POSDATAPTR(t, entry);
218 pos[i] = (uint16 *) POSNULL;
221 dimt = *(uint16 *) (pos[i]);
222 post = (WordEntryPos *) (pos[i] + 1);
223 for (k = 0; k < i; k++)
227 lenct = *(uint16 *) (pos[k]);
228 ct = (WordEntryPos *) (pos[k] + 1);
229 for (l = 0; l < dimt; l++)
231 for (p = 0; p < lenct; p++)
233 dist = Abs((int) WEP_GETPOS(post[l]) - (int) WEP_GETPOS(ct[p]));
234 if (dist || (dist == 0 && (pos[i] == (uint16 *) POSNULL || pos[k] == (uint16 *) POSNULL)))
240 curw = sqrt(wpos(post[l]) * wpos(ct[p]) * word_distance(dist));
241 res = (res < 0) ? curw : 1.0 - (1.0 - res) * (1.0 - curw);
253 calc_rank_or(float *w, tsvector * t, QUERYTYPE * q)
264 *(uint16 *) POSNULL = lengthof(POSNULL) - 1;
265 item = SortAndUniqItems(GETOPERAND(q), GETQUERY(q), &size);
267 for (i = 0; i < size; i++)
269 entry = find_wordentry(t, q, item[i]);
275 dimt = POSDATALEN(t, entry);
276 post = POSDATAPTR(t, entry);
280 dimt = *(uint16 *) POSNULL;
284 for (j = 0; j < dimt; j++)
289 res = 1.0 - (1.0 - res) * (1.0 - wpos(post[j]));
297 calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method)
299 ITEM *item = GETQUERY(q);
303 if (!t->size || !q->size)
306 res = (item->type != VAL && item->val == (int4) '&') ?
307 calc_rank_and(w, t, q) : calc_rank_or(w, t, q);
317 res /= log((float) (cnt_length(t) + 1)) / log(2.0);
326 elog(ERROR, "unrecognized normalization method: %d", method);
333 rank(PG_FUNCTION_ARGS)
335 ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
336 tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
337 QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2));
338 int method = DEF_NORM_METHOD;
340 float ws[lengthof(weights)];
343 if (ARR_NDIM(win) != 1)
345 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
346 errmsg("array of weight must be one-dimensional")));
348 if (ARRNELEMS(win) < lengthof(weights))
350 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
351 errmsg("array of weight is too short")));
353 for (i = 0; i < lengthof(weights); i++)
355 ws[i] = (((float4 *) ARR_DATA_PTR(win))[i] >= 0) ? ((float4 *) ARR_DATA_PTR(win))[i] : weights[i];
358 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
359 errmsg("weight out of range")));
363 method = PG_GETARG_INT32(3);
365 res = calc_rank(ws, txt, query, method);
367 PG_FREE_IF_COPY(win, 0);
368 PG_FREE_IF_COPY(txt, 1);
369 PG_FREE_IF_COPY(query, 2);
370 PG_RETURN_FLOAT4(res);
374 rank_def(PG_FUNCTION_ARGS)
376 tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
377 QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
379 int method = DEF_NORM_METHOD;
382 method = PG_GETARG_INT32(2);
384 res = calc_rank(weights, txt, query, method);
386 PG_FREE_IF_COPY(txt, 0);
387 PG_FREE_IF_COPY(query, 1);
388 PG_RETURN_FLOAT4(res);
399 compareDocR(const void *a, const void *b)
401 if (((DocRepresentation *) a)->pos == ((DocRepresentation *) b)->pos)
403 return (((DocRepresentation *) a)->pos > ((DocRepresentation *) b)->pos) ? 1 : -1;
409 DocRepresentation *doc;
414 checkcondition_DR(void *checkval, ITEM * val)
416 DocRepresentation *ptr = ((ChkDocR *) checkval)->doc;
418 while (ptr - ((ChkDocR *) checkval)->doc < ((ChkDocR *) checkval)->len)
420 if (val == ptr->item || compareITEM(&val, &(ptr->item)) == 0)
430 Cover(DocRepresentation * doc, int len, QUERYTYPE * query, int *pos, int *p, int *q)
433 DocRepresentation *ptr,
434 *f = (DocRepresentation *) 0xffffffff;
435 ITEM *item = GETQUERY(query);
442 for (i = 0; i < query->size; i++)
444 if (item->type != VAL)
451 while (ptr - doc < len)
453 if (ptr->item == item)
472 { /* already check this pos */
474 return Cover(doc, len, query, pos, p, q);
477 item = GETQUERY(query);
478 for (i = 0; i < query->size; i++)
480 if (item->type != VAL)
487 while (ptr >= doc + *pos)
489 if (ptr->item == item)
508 ch.len = (doc + lastpos) - f + 1;
510 SortAndUniqOperand = GETOPERAND(query);
511 if (TS_execute(GETQUERY(query), &ch, false, checkcondition_DR))
514 * elog(NOTICE,"OP:%d NP:%d P:%d Q:%d", *pos, lastpos, *p, *q);
519 return Cover(doc, len, query, pos, p, q);
525 static DocRepresentation *
526 get_docrep(tsvector * txt, QUERYTYPE * query, int *doclen)
528 ITEM *item = GETQUERY(query);
534 int len = query->size * 4,
536 DocRepresentation *doc;
538 *(uint16 *) POSNULL = lengthof(POSNULL) - 1;
539 doc = (DocRepresentation *) palloc(sizeof(DocRepresentation) * len);
540 for (i = 0; i < query->size; i++)
542 if (item[i].type != VAL)
545 entry = find_wordentry(txt, query, &(item[i]));
551 dimt = POSDATALEN(txt, entry);
552 post = POSDATAPTR(txt, entry);
556 dimt = *(uint16 *) POSNULL;
560 while (cur + dimt >= len)
563 doc = (DocRepresentation *) repalloc(doc, sizeof(DocRepresentation) * len);
566 for (j = 0; j < dimt; j++)
568 doc[cur].item = &(item[i]);
569 doc[cur].pos = WEP_GETPOS(post[j]);
579 qsort((void *) doc, cur, sizeof(DocRepresentation), compareDocR);
589 rank_cd(PG_FUNCTION_ARGS)
591 int K = PG_GETARG_INT32(0);
592 tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
593 QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2));
594 int method = DEF_NORM_METHOD;
595 DocRepresentation *doc;
602 doc = get_docrep(txt, query, &len);
605 PG_FREE_IF_COPY(txt, 1);
606 PG_FREE_IF_COPY(query, 2);
607 PG_RETURN_FLOAT4(0.0);
613 while (Cover(doc, len, query, &cur, &p, &q))
614 res += (q - p + 1 > K) ? ((float) K) / ((float) (q - p + 1)) : 1.0;
617 method = PG_GETARG_INT32(3);
624 res /= log((float) (cnt_length(txt) + 1));
627 len = cnt_length(txt);
633 elog(ERROR, "unrecognized normalization method: %d", method);
637 PG_FREE_IF_COPY(txt, 1);
638 PG_FREE_IF_COPY(query, 2);
640 PG_RETURN_FLOAT4(res);
645 rank_cd_def(PG_FUNCTION_ARGS)
647 PG_RETURN_DATUM(DirectFunctionCall4(
652 (PG_NARGS() == 3) ? PG_GETARG_DATUM(2) : Int32GetDatum(DEF_NORM_METHOD)
656 /**************debug*************/
668 compareDocWord(const void *a, const void *b)
670 if (((DocWord *) a)->pos == ((DocWord *) b)->pos)
672 return (((DocWord *) a)->pos > ((DocWord *) b)->pos) ? 1 : -1;
677 get_covers(PG_FUNCTION_ARGS)
679 tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
680 QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
681 WordEntry *pptr = ARRPTR(txt);
692 DocRepresentation *doc;
699 doc = get_docrep(txt, query, &rlen);
703 out = palloc(VARHDRSZ);
704 VARATT_SIZEP(out) = VARHDRSZ;
705 PG_FREE_IF_COPY(txt, 0);
706 PG_FREE_IF_COPY(query, 1);
707 PG_RETURN_POINTER(out);
710 for (i = 0; i < txt->size; i++)
714 (errcode(ERRCODE_SYNTAX_ERROR),
715 errmsg("no pos info")));
716 dlen += POSDATALEN(txt, &(pptr[i]));
719 dwptr = dw = palloc(sizeof(DocWord) * dlen);
720 memset(dw, 0, sizeof(DocWord) * dlen);
722 for (i = 0; i < txt->size; i++)
724 WordEntryPos *posdata = POSDATAPTR(txt, &(pptr[i]));
726 for (j = 0; j < POSDATALEN(txt, &(pptr[i])); j++)
728 dw[cur].w = STRPTR(txt) + pptr[i].pos;
729 dw[cur].len = pptr[i].len;
730 dw[cur].pos = WEP_GETPOS(posdata[j]);
733 len += (pptr[i].len + 1) * (int) POSDATALEN(txt, &(pptr[i]));
735 qsort((void *) dw, dlen, sizeof(DocWord), compareDocWord);
737 while (Cover(doc, rlen, query, &pos, &p, &q))
739 dwptr = dw + olddwpos;
740 while (dwptr->pos < p && dwptr - dw < dlen)
742 olddwpos = dwptr - dw;
743 dwptr->start = ncover;
744 while (dwptr->pos < q + 1 && dwptr - dw < dlen)
746 (dwptr - 1)->finish = ncover;
747 len += 4 /* {}+two spaces */ + 2 * 16 /* numbers */ ;
751 out = palloc(VARHDRSZ + len);
752 cptr = ((char *) out) + VARHDRSZ;
755 while (dwptr - dw < dlen)
759 sprintf(cptr, "{%d ", dwptr->start);
760 cptr = strchr(cptr, '\0');
762 memcpy(cptr, dwptr->w, dwptr->len);
768 sprintf(cptr, "}%d ", dwptr->finish);
769 cptr = strchr(cptr, '\0');
774 VARATT_SIZEP(out) = cptr - ((char *) out);
779 PG_FREE_IF_COPY(txt, 0);
780 PG_FREE_IF_COPY(query, 1);
781 PG_RETURN_POINTER(out);