Improved error messages for halfvec input [skip ci]

This commit is contained in:
Andrew Kane
2024-04-03 22:20:13 -07:00
parent 7667abe9a0
commit 9f61dcff5d
2 changed files with 17 additions and 6 deletions

View File

@@ -435,6 +435,8 @@ halfvec_in(PG_FUNCTION_ARGS)
while (pt != NULL && *stringEnd != ']')
{
float val;
if (dim == HALFVEC_MAX_DIM)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
@@ -450,15 +452,24 @@ halfvec_in(PG_FUNCTION_ARGS)
errmsg("invalid input syntax for type halfvec: \"%s\"", lit)));
/* Use strtof like float4in to avoid a double-rounding problem */
x[dim] = Float4ToHalf(strtof(pt, &stringEnd));
CheckElement(x[dim]);
dim++;
errno = 0;
val = strtof(pt, &stringEnd);
if (stringEnd == pt)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type halfvec: \"%s\"", lit)));
x[dim] = Float4ToHalfUnchecked(val);
if ((errno == ERANGE && isinf(val)) || (HalfIsInf(x[dim]) && !isinf(val)) || (HalfIsZero(x[dim]) && val != 0))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("\"%s\" is out of range for type halfvec", pt)));
CheckElement(x[dim]);
dim++;
while (halfvec_isspace(*stringEnd))
stringEnd++;

View File

@@ -51,15 +51,15 @@ SELECT '[65519,-65519]'::halfvec;
(1 row)
SELECT '[65520,-65520]'::halfvec;
ERROR: value out of range: overflow
ERROR: "65520" is out of range for type halfvec
LINE 1: SELECT '[65520,-65520]'::halfvec;
^
SELECT '[1e-8,-1e-8]'::halfvec;
ERROR: value out of range: underflow
ERROR: "1e-8" is out of range for type halfvec
LINE 1: SELECT '[1e-8,-1e-8]'::halfvec;
^
SELECT '[4e38,1]'::halfvec;
ERROR: infinite value not allowed in halfvec
ERROR: "4e38" is out of range for type halfvec
LINE 1: SELECT '[4e38,1]'::halfvec;
^
SELECT '[1,2,3'::halfvec;