MCPcopy Create free account
hub / github.com/apache/cloudberry / array_to_tsvector

Function array_to_tsvector

src/backend/utils/adt/tsvector_op.c:748–807  ·  view source on GitHub ↗

* Build tsvector from array of lexemes. */

Source from the content-addressed store, hash-verified

746 * Build tsvector from array of lexemes.
747 */
748Datum
749array_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);

Callers

nothing calls this directly

Calls 5

deconstruct_arrayFunction · 0.85
quniqueFunction · 0.85
errcodeFunction · 0.50
errmsgFunction · 0.50
palloc0Function · 0.50

Tested by

no test coverage detected