mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-11 15:16:54 +08:00
Improved vector_in
This commit is contained in:
45
src/vector.c
45
src/vector.c
@@ -181,26 +181,24 @@ vector_in(PG_FUNCTION_ARGS)
|
|||||||
int32 typmod = PG_GETARG_INT32(2);
|
int32 typmod = PG_GETARG_INT32(2);
|
||||||
float x[VECTOR_MAX_DIM];
|
float x[VECTOR_MAX_DIM];
|
||||||
int dim = 0;
|
int dim = 0;
|
||||||
char *pt;
|
char *pt = lit;
|
||||||
char *stringEnd;
|
char *stringEnd;
|
||||||
Vector *result;
|
Vector *result;
|
||||||
char *litcopy = pstrdup(lit);
|
bool malformed = false;
|
||||||
char *str = litcopy;
|
|
||||||
|
|
||||||
while (vector_isspace(*str))
|
while (vector_isspace(*pt))
|
||||||
str++;
|
pt++;
|
||||||
|
|
||||||
if (*str != '[')
|
if (*pt != '[')
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
||||||
errmsg("malformed vector literal: \"%s\"", lit),
|
errmsg("malformed vector literal: \"%s\"", lit),
|
||||||
errdetail("Vector contents must start with \"[\".")));
|
errdetail("Vector contents must start with \"[\".")));
|
||||||
|
|
||||||
str++;
|
pt++;
|
||||||
pt = strtok(str, ",");
|
|
||||||
stringEnd = pt;
|
stringEnd = pt;
|
||||||
|
|
||||||
while (pt != NULL && *stringEnd != ']')
|
while (*pt != '\0' && *pt != ',' && *stringEnd != ']')
|
||||||
{
|
{
|
||||||
if (dim == VECTOR_MAX_DIM)
|
if (dim == VECTOR_MAX_DIM)
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
@@ -229,12 +227,22 @@ vector_in(PG_FUNCTION_ARGS)
|
|||||||
while (vector_isspace(*stringEnd))
|
while (vector_isspace(*stringEnd))
|
||||||
stringEnd++;
|
stringEnd++;
|
||||||
|
|
||||||
if (*stringEnd != '\0' && *stringEnd != ']')
|
if (*stringEnd == '\0')
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (*stringEnd != ',' && *stringEnd != ']')
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
||||||
errmsg("invalid input syntax for type vector: \"%s\"", lit)));
|
errmsg("invalid input syntax for type vector: \"%s\"", lit)));
|
||||||
|
|
||||||
pt = strtok(NULL, ",");
|
pt = stringEnd + 1;
|
||||||
|
|
||||||
|
/* TODO Remove in 0.6.0 */
|
||||||
|
while (*pt == ',')
|
||||||
|
{
|
||||||
|
malformed = true;
|
||||||
|
pt++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stringEnd == NULL || *stringEnd != ']')
|
if (stringEnd == NULL || *stringEnd != ']')
|
||||||
@@ -255,22 +263,17 @@ vector_in(PG_FUNCTION_ARGS)
|
|||||||
errmsg("malformed vector literal: \"%s\"", lit),
|
errmsg("malformed vector literal: \"%s\"", lit),
|
||||||
errdetail("Junk after closing right brace.")));
|
errdetail("Junk after closing right brace.")));
|
||||||
|
|
||||||
/* Ensure no consecutive delimiters since strtok skips */
|
/* TODO Remove in 0.6.0 */
|
||||||
for (pt = lit + 1; *pt != '\0'; pt++)
|
if (malformed)
|
||||||
{
|
ereport(ERROR,
|
||||||
if (pt[-1] == ',' && *pt == ',')
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
||||||
ereport(ERROR,
|
errmsg("malformed vector literal: \"%s\"", lit)));
|
||||||
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
||||||
errmsg("malformed vector literal: \"%s\"", lit)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dim < 1)
|
if (dim < 1)
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_DATA_EXCEPTION),
|
(errcode(ERRCODE_DATA_EXCEPTION),
|
||||||
errmsg("vector must have at least 1 dimension")));
|
errmsg("vector must have at least 1 dimension")));
|
||||||
|
|
||||||
pfree(litcopy);
|
|
||||||
|
|
||||||
CheckExpectedDim(typmod, dim);
|
CheckExpectedDim(typmod, dim);
|
||||||
|
|
||||||
result = InitVector(dim);
|
result = InitVector(dim);
|
||||||
|
|||||||
Reference in New Issue
Block a user