diff --git a/src/sparsevec.c b/src/sparsevec.c index 7ca6416..9fec38a 100644 --- a/src/sparsevec.c +++ b/src/sparsevec.c @@ -94,33 +94,45 @@ CheckNnz(int nnz, int dim) * Ensure valid index */ static inline void -CheckIndex(int32 *indices, int i, int dim) +CheckIndex(int32 *indices, int i, int dim, bool text) { int32 index = indices[i]; - /* TODO Better error message for binary format */ if (index < 0) - ereport(ERROR, - (errcode(ERRCODE_DATA_EXCEPTION), - errmsg("index must be greater than zero"))); + { + if (text) + ereport(ERROR, + (errcode(ERRCODE_DATA_EXCEPTION), + errmsg("sparsevec index out of bounds (< 1)"))); + else + ereport(ERROR, + (errcode(ERRCODE_DATA_EXCEPTION), + errmsg("sparsevec index out of bounds for binary representation (< 0)"))); + } - /* TODO Better error message for binary format */ if (index >= dim) - ereport(ERROR, - (errcode(ERRCODE_DATA_EXCEPTION), - errmsg("index must be less than or equal to dimensions"))); + { + if (text) + ereport(ERROR, + (errcode(ERRCODE_DATA_EXCEPTION), + errmsg("sparsevec index out of bounds (> dimensions)"))); + else + ereport(ERROR, + (errcode(ERRCODE_DATA_EXCEPTION), + errmsg("sparsevec index out of bounds for binary representation (>= dimensions)"))); + } if (i > 0) { if (index < indices[i - 1]) ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION), - errmsg("indexes must be in ascending order"))); + errmsg("sparsevec indices must be in ascending order"))); if (index == indices[i - 1]) ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION), - errmsg("indexes must not contain duplicates"))); + errmsg("sparsevec indices must not contain duplicates"))); } } @@ -392,7 +404,7 @@ sparsevec_in(PG_FUNCTION_ARGS) result->indices[i] = elements[i].index; rvalues[i] = elements[i].value; - CheckIndex(result->indices, i, dim); + CheckIndex(result->indices, i, dim, true); } PG_RETURN_POINTER(result); @@ -530,7 +542,7 @@ sparsevec_recv(PG_FUNCTION_ARGS) for (int i = 0; i < nnz; i++) { result->indices[i] = pq_getmsgint(buf, sizeof(int32)); - CheckIndex(result->indices, i, dim); + CheckIndex(result->indices, i, dim, false); } for (int i = 0; i < nnz; i++) diff --git a/test/expected/sparsevec.out b/test/expected/sparsevec.out index eadd37b..1f653f8 100644 --- a/test/expected/sparsevec.out +++ b/test/expected/sparsevec.out @@ -170,11 +170,11 @@ SELECT '{2:1,1:1}/2'::sparsevec; (1 row) SELECT '{1:1,1:1}/2'::sparsevec; -ERROR: indexes must not contain duplicates +ERROR: sparsevec indices must not contain duplicates LINE 1: SELECT '{1:1,1:1}/2'::sparsevec; ^ SELECT '{1:1,2:1,1:1}/2'::sparsevec; -ERROR: indexes must not contain duplicates +ERROR: sparsevec indices must not contain duplicates LINE 1: SELECT '{1:1,2:1,1:1}/2'::sparsevec; ^ SELECT '{}/5'::sparsevec; @@ -208,27 +208,27 @@ ERROR: sparsevec must have at least 1 dimension LINE 1: SELECT '{}/-9223372036854775809'::sparsevec; ^ SELECT '{2147483647:1}/1'::sparsevec; -ERROR: index must be less than or equal to dimensions +ERROR: sparsevec index out of bounds (> dimensions) LINE 1: SELECT '{2147483647:1}/1'::sparsevec; ^ SELECT '{2147483648:1}/1'::sparsevec; -ERROR: index must be less than or equal to dimensions +ERROR: sparsevec index out of bounds (> dimensions) LINE 1: SELECT '{2147483648:1}/1'::sparsevec; ^ SELECT '{-2147483648:1}/1'::sparsevec; -ERROR: index must be greater than zero +ERROR: sparsevec index out of bounds (< 1) LINE 1: SELECT '{-2147483648:1}/1'::sparsevec; ^ SELECT '{-2147483649:1}/1'::sparsevec; -ERROR: index must be greater than zero +ERROR: sparsevec index out of bounds (< 1) LINE 1: SELECT '{-2147483649:1}/1'::sparsevec; ^ SELECT '{0:1}/1'::sparsevec; -ERROR: index must be greater than zero +ERROR: sparsevec index out of bounds (< 1) LINE 1: SELECT '{0:1}/1'::sparsevec; ^ SELECT '{2:1}/1'::sparsevec; -ERROR: index must be less than or equal to dimensions +ERROR: sparsevec index out of bounds (> dimensions) LINE 1: SELECT '{2:1}/1'::sparsevec; ^ SELECT '{}/3'::sparsevec(3);