* Build tsvector from array of lexemes. */
| 746 | * Build tsvector from array of lexemes. |
| 747 | */ |
| 748 | Datum |
| 749 | array_to_tsvector(PG_FUNCTION_ARGS) |
| 750 | { |
| 751 | ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); |
| 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), |
| 778 | compare_text_lexemes); |
| 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); |
nothing calls this directly
no test coverage detected