pgvector.git / summary / log / commit / refs
commit 41c68bf692e363d894be29642876a407638e79c7
Author: yihong <zouzou0208@gmail.com>
Commit: GitHub <noreply@github.com>
Date: Fri Jun 09 02:51:20 2023 +0000
fix: input function does not allow tailing spaces (#148)
src/vector.c | 31 ++++++++++++++++++++++++++++++-
test/expected/input.out | 6 ++++++
test/sql/input.sql | 1 +
3 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/src/vector.c b/src/vector.c
index 5e1e0b65..8c0d2d2d 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -86,6 +86,24 @@ CheckElement(float value)
errmsg("infinite value not allowed in vector")));
}
+/*
+ * PostgreSQL has with the array_isspace function for the character checking which is reimplemented
+ * as vector_isspace, as it's static, source code link:
+ * https://github.com/postgres/postgres/blob/378d73ef204d0dcbeab834d52478e8cb90578ab7/src/backend/utils/adt/arrayfuncs.c#L438
+ */
+static inline bool
+vector_isspace(char ch)
+{
+ if (ch == ' ' ||
+ ch == '\t' ||
+ ch == '\n' ||
+ ch == '\r' ||
+ ch == '\v' ||
+ ch == '\f')
+ return true;
+ return false;
+}
+
/*
* Check state array
*/
@@ -150,6 +168,9 @@ vector_in(PG_FUNCTION_ARGS)
char *stringEnd;
Vector *result;
+ while (vector_isspace(*str))
+ str++;
+
if (*str != '[')
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
@@ -172,6 +193,9 @@ vector_in(PG_FUNCTION_ARGS)
CheckElement(x[dim]);
dim++;
+ while (vector_isspace(*stringEnd))
+ stringEnd++;
+
if (stringEnd == pt)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
@@ -191,7 +215,12 @@ vector_in(PG_FUNCTION_ARGS)
errmsg("malformed vector literal"),
errdetail("Unexpected end of input.")));
- if (stringEnd[1] != '\0')
+ stringEnd++;
+ /* only whitespace is allowed after the closing brace */
+ while (vector_isspace(*stringEnd))
+ stringEnd++;
+
+ if (*stringEnd != '\0')
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed vector literal"),
diff --git a/test/expected/input.out b/test/expected/input.out
index 99fc3444..b6ccca4b 100644
--- a/test/expected/input.out
+++ b/test/expected/input.out
@@ -10,6 +10,12 @@ SELECT '[-1,2,3]'::vector;
[-1,2,3]
(1 row)
+SELECT ' [ 1, 2 , 3 ] '::vector(3);
+ vector
+---------
+ [1,2,3]
+(1 row)
+
SELECT '[1.23456]'::vector;
vector
-----------
diff --git a/test/sql/input.sql b/test/sql/input.sql
index b3d2e53a..7d74bc93 100644
--- a/test/sql/input.sql
+++ b/test/sql/input.sql
@@ -1,5 +1,6 @@
SELECT '[1,2,3]'::vector;
SELECT '[-1,2,3]'::vector;
+SELECT ' [ 1, 2 , 3 ] '::vector(3);
SELECT '[1.23456]'::vector;
SELECT '[hello,1]'::vector;
SELECT '[NaN,1]'::vector;
[parent: 13cf29088d0e]