Improved sparsevec error messages [skip ci]

This commit is contained in:
Andrew Kane
2024-04-26 17:11:11 -07:00
parent c9fb66d54d
commit 1e94907179
2 changed files with 33 additions and 21 deletions

View File

@@ -94,33 +94,45 @@ CheckNnz(int nnz, int dim)
* Ensure valid index * Ensure valid index
*/ */
static inline void static inline void
CheckIndex(int32 *indices, int i, int dim) CheckIndex(int32 *indices, int i, int dim, bool text)
{ {
int32 index = indices[i]; int32 index = indices[i];
/* TODO Better error message for binary format */
if (index < 0) if (index < 0)
ereport(ERROR, {
(errcode(ERRCODE_DATA_EXCEPTION), if (text)
errmsg("index must be greater than zero"))); 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) if (index >= dim)
ereport(ERROR, {
(errcode(ERRCODE_DATA_EXCEPTION), if (text)
errmsg("index must be less than or equal to dimensions"))); 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 (i > 0)
{ {
if (index < indices[i - 1]) if (index < indices[i - 1])
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION), (errcode(ERRCODE_DATA_EXCEPTION),
errmsg("indexes must be in ascending order"))); errmsg("sparsevec indices must be in ascending order")));
if (index == indices[i - 1]) if (index == indices[i - 1])
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATA_EXCEPTION), (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; result->indices[i] = elements[i].index;
rvalues[i] = elements[i].value; rvalues[i] = elements[i].value;
CheckIndex(result->indices, i, dim); CheckIndex(result->indices, i, dim, true);
} }
PG_RETURN_POINTER(result); PG_RETURN_POINTER(result);
@@ -530,7 +542,7 @@ sparsevec_recv(PG_FUNCTION_ARGS)
for (int i = 0; i < nnz; i++) for (int i = 0; i < nnz; i++)
{ {
result->indices[i] = pq_getmsgint(buf, sizeof(int32)); 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++) for (int i = 0; i < nnz; i++)

View File

@@ -170,11 +170,11 @@ SELECT '{2:1,1:1}/2'::sparsevec;
(1 row) (1 row)
SELECT '{1:1,1:1}/2'::sparsevec; 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; LINE 1: SELECT '{1:1,1:1}/2'::sparsevec;
^ ^
SELECT '{1:1,2: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; LINE 1: SELECT '{1:1,2:1,1:1}/2'::sparsevec;
^ ^
SELECT '{}/5'::sparsevec; SELECT '{}/5'::sparsevec;
@@ -208,27 +208,27 @@ ERROR: sparsevec must have at least 1 dimension
LINE 1: SELECT '{}/-9223372036854775809'::sparsevec; LINE 1: SELECT '{}/-9223372036854775809'::sparsevec;
^ ^
SELECT '{2147483647:1}/1'::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; LINE 1: SELECT '{2147483647:1}/1'::sparsevec;
^ ^
SELECT '{2147483648: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; LINE 1: SELECT '{2147483648:1}/1'::sparsevec;
^ ^
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; LINE 1: SELECT '{-2147483648:1}/1'::sparsevec;
^ ^
SELECT '{-2147483649: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; LINE 1: SELECT '{-2147483649:1}/1'::sparsevec;
^ ^
SELECT '{0: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; LINE 1: SELECT '{0:1}/1'::sparsevec;
^ ^
SELECT '{2: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; LINE 1: SELECT '{2:1}/1'::sparsevec;
^ ^
SELECT '{}/3'::sparsevec(3); SELECT '{}/3'::sparsevec(3);