pgvector.git / summary / log / commit / refs

commit    ffeef73db19cefc412e78eda3e1974f999465416
Author:   Andrew Kane <andrew@ankane.org>
Date:     Tue Oct 27 06:29:35 2026 +0000

    Moved element checks to AppendElement [skip ci]


src/sparsevec.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/sparsevec.c b/src/sparsevec.c index 5f197d77..ae46f85c 100644 --- a/src/sparsevec.c +++ b/src/sparsevec.c @@ -145,6 +145,11 @@ CheckElement(float value) ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION), errmsg("infinite value not allowed in sparsevec"))); + + if (value == 0) + ereport(ERROR, + (errcode(ERRCODE_DATA_EXCEPTION), + errmsg("binary representation of sparsevec cannot contain zero values"))); } /* @@ -177,6 +182,8 @@ AppendElement(SparseVector * vec, int i, int32 index, float value) if (i < 0 || i >= vec->nnz) elog(ERROR, "index out of bounds"); + CheckElement(value); + vec->indices[i] = index; values[i] = value; } @@ -329,8 +336,6 @@ sparsevec_in(PG_FUNCTION_ARGS) (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("\"%s\" is out of range for type sparsevec", pnstrdup(pt, stringEnd - pt)))); - CheckElement(value); - /* Convert 1-based numbering (SQL) to 0-based (C) */ elements[elementsLength].index = index - 1; elements[elementsLength].value = value; @@ -569,11 +574,6 @@ sparsevec_recv(PG_FUNCTION_ARGS) { values[i] = pq_getmsgfloat4(buf); CheckElement(values[i]); - - if (values[i] == 0) - ereport(ERROR, - (errcode(ERRCODE_DATA_EXCEPTION), - errmsg("binary representation of sparsevec cannot contain zero values"))); } PG_RETURN_POINTER(result); @@ -715,7 +715,6 @@ array_to_sparsevec(PG_FUNCTION_ARGS) Datum *elemsp; int nelemsp; int nnz = 0; - float *values; int j = 0; if (ARR_NDIM(array) > 1) @@ -770,7 +769,6 @@ array_to_sparsevec(PG_FUNCTION_ARGS) CheckNnz(nnz, nelemsp); result = InitSparseVector(nelemsp, nnz); - values = SPARSEVEC_VALUES(result); #define PROCESS_ARRAY_ELEM(elem) \ do { \ @@ -819,10 +817,6 @@ array_to_sparsevec(PG_FUNCTION_ARGS) FinishAppend(result, j); - /* Check elements */ - for (int i = 0; i < result->nnz; i++) - CheckElement(values[i]); - PG_RETURN_POINTER(result); } [parent: fa99ce6e9467]