You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pgvector.lua
+35-2Lines changed: 35 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,12 @@ local vector_mt = {
6
6
end
7
7
}
8
8
9
+
localhalfvec_mt= {
10
+
pgmoon_serialize=function(v)
11
+
return0, halfvec_serialize(v)
12
+
end
13
+
}
14
+
9
15
localsparsevec_mt= {
10
16
pgmoon_serialize=function(v)
11
17
return0, sparsevec_serialize(v)
@@ -36,6 +42,30 @@ function pgvector.deserialize(v)
36
42
returnsetmetatable(vec, vector_mt)
37
43
end
38
44
45
+
functionpgvector.halfvec(v)
46
+
localvec= {}
47
+
for_, xinipairs(v) do
48
+
table.insert(vec, x)
49
+
end
50
+
returnsetmetatable(vec, halfvec_mt)
51
+
end
52
+
53
+
functionhalfvec_serialize(v)
54
+
for_, vinipairs(v) do
55
+
assert(type(v) =="number")
56
+
end
57
+
return"[" ..table.concat(v, ",") .."]"
58
+
end
59
+
60
+
functionhalfvec_deserialize(v)
61
+
localvec= {}
62
+
forxinstring.gmatch(string.sub(v, 2, -2), "[^,]+") do
63
+
table.insert(vec, tonumber(x))
64
+
end
65
+
-- pgvector.halfvec without copy
66
+
returnsetmetatable(vec, halfvec_mt)
67
+
end
68
+
39
69
functionpgvector.sparsevec(elements, dim)
40
70
fork, vinpairs(elements) do
41
71
assert(type(k) =="number")
@@ -62,10 +92,13 @@ function sparsevec_deserialize(v)
62
92
end
63
93
64
94
functionpgvector.setup_vector(pg)
65
-
localrow=pg:query("SELECT to_regtype('vector')::oid AS vector_oid, to_regtype('sparsevec')::oid AS sparsevec_oid")[1]
95
+
localrow=pg:query("SELECT to_regtype('vector')::oid AS vector_oid, to_regtype('halfvec')::oid AS halfvec_oid, to_regtype('sparsevec')::oid AS sparsevec_oid")[1]
66
96
assert(row["vector_oid"], "vector type not found in the database")
0 commit comments