-
Notifications
You must be signed in to change notification settings - Fork 8
/
pgx_test.go
89 lines (74 loc) · 2 KB
/
pgx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package pgvector_test
import (
"context"
"math"
"reflect"
"testing"
"github.com/jackc/pgx/v5"
"github.com/pgvector/pgvector-go"
)
type PgxItem struct {
Id int64
Embedding pgvector.Vector
}
func CreatePgxItems(conn *pgx.Conn, ctx context.Context) {
items := []PgxItem{
PgxItem{Embedding: pgvector.NewVector([]float32{1, 1, 1})},
PgxItem{Embedding: pgvector.NewVector([]float32{2, 2, 2})},
PgxItem{Embedding: pgvector.NewVector([]float32{1, 1, 2})},
}
for _, item := range items {
_, err := conn.Exec(ctx, "INSERT INTO pgx_items (embedding) VALUES ($1)", item.Embedding)
if err != nil {
panic(err)
}
}
}
func TestPgx(t *testing.T) {
ctx := context.Background()
conn, err := pgx.Connect(ctx, "postgres://localhost/pgvector_go_test")
if err != nil {
panic(err)
}
defer conn.Close(ctx)
conn.Exec(ctx, "CREATE EXTENSION IF NOT EXISTS vector")
conn.Exec(ctx, "DROP TABLE IF EXISTS pgx_items")
_, err = conn.Exec(ctx, "CREATE TABLE pgx_items (id bigserial PRIMARY KEY, embedding vector(3))")
if err != nil {
panic(err)
}
_, err = conn.Exec(ctx, "CREATE INDEX ON pgx_items USING hnsw (embedding vector_l2_ops)")
if err != nil {
panic(err)
}
CreatePgxItems(conn, ctx)
rows, err := conn.Query(ctx, "SELECT *, embedding <-> $1 FROM pgx_items ORDER BY embedding <-> $1 LIMIT 5", pgvector.NewVector([]float32{1, 1, 1}))
if err != nil {
panic(err)
}
defer rows.Close()
var items []PgxItem
var distances []float64
for rows.Next() {
var item PgxItem
var distance float64
err = rows.Scan(&item.Id, &item.Embedding, &distance)
if err != nil {
panic(err)
}
items = append(items, item)
distances = append(distances, distance)
}
if rows.Err() != nil {
panic(rows.Err())
}
if items[0].Id != 1 || items[1].Id != 3 || items[2].Id != 2 {
t.Errorf("Bad ids")
}
if !reflect.DeepEqual(items[1].Embedding.Slice(), []float32{1, 1, 2}) {
t.Errorf("Bad embedding")
}
if distances[0] != 0 || distances[1] != 1 || distances[2] != math.Sqrt(3) {
t.Errorf("Bad distances")
}
}