-
Notifications
You must be signed in to change notification settings - Fork 8
/
pgx_test.go
172 lines (151 loc) · 5.05 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package pgvector_test
import (
"context"
"math"
"reflect"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/pgvector/pgvector-go"
pgxvector "github.com/pgvector/pgvector-go/pgx"
)
type PgxItem struct {
Id int64
Embedding pgvector.Vector
HalfEmbedding pgvector.HalfVector
BinaryEmbedding string
SparseEmbedding pgvector.SparseVector
}
func CreatePgxItems(ctx context.Context, conn *pgx.Conn) {
items := []PgxItem{
PgxItem{Embedding: pgvector.NewVector([]float32{1, 1, 1}), HalfEmbedding: pgvector.NewHalfVector([]float32{1, 1, 1}), BinaryEmbedding: "000", SparseEmbedding: pgvector.NewSparseVector([]float32{1, 1, 1})},
PgxItem{Embedding: pgvector.NewVector([]float32{2, 2, 2}), HalfEmbedding: pgvector.NewHalfVector([]float32{2, 2, 2}), BinaryEmbedding: "101", SparseEmbedding: pgvector.NewSparseVector([]float32{2, 2, 2})},
PgxItem{Embedding: pgvector.NewVector([]float32{1, 1, 2}), HalfEmbedding: pgvector.NewHalfVector([]float32{1, 1, 2}), BinaryEmbedding: "111", SparseEmbedding: pgvector.NewSparseVector([]float32{1, 1, 2})},
}
for _, item := range items {
_, err := conn.Exec(ctx, "INSERT INTO pgx_items (embedding, half_embedding, binary_embedding, sparse_embedding) VALUES ($1, $2, $3, $4)", item.Embedding, item.HalfEmbedding, item.BinaryEmbedding, item.SparseEmbedding)
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)
_, err = conn.Exec(ctx, "CREATE EXTENSION IF NOT EXISTS vector")
if err != nil {
panic(err)
}
err = pgxvector.RegisterTypes(ctx, conn)
if err != nil {
panic(err)
}
_, err = conn.Exec(ctx, "DROP TABLE IF EXISTS pgx_items")
if err != nil {
panic(err)
}
_, err = conn.Exec(ctx, "CREATE TABLE pgx_items (id bigserial PRIMARY KEY, embedding vector(3), half_embedding halfvec(3), binary_embedding bit(3), sparse_embedding sparsevec(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(ctx, conn)
rows, err := conn.Query(ctx, "SELECT id, embedding, half_embedding, binary_embedding, sparse_embedding, 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 binaryEmbeddings []pgtype.Bits
var distances []float64
for rows.Next() {
var item PgxItem
var binaryEmbedding pgtype.Bits
var distance float64
err = rows.Scan(&item.Id, &item.Embedding, &item.HalfEmbedding, &binaryEmbedding, &item.SparseEmbedding, &distance)
if err != nil {
panic(err)
}
items = append(items, item)
binaryEmbeddings = append(binaryEmbeddings, binaryEmbedding)
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.Error()
}
if !reflect.DeepEqual(items[1].Embedding.Slice(), []float32{1, 1, 2}) {
t.Error()
}
if !reflect.DeepEqual(items[1].HalfEmbedding.Slice(), []float32{1, 1, 2}) {
t.Error()
}
if binaryEmbeddings[1].Bytes[0] != (7<<5) || binaryEmbeddings[1].Len != 3 {
t.Error()
}
if !reflect.DeepEqual(items[1].SparseEmbedding.Slice(), []float32{1, 1, 2}) {
t.Error()
}
if distances[0] != 0 || distances[1] != 1 || distances[2] != math.Sqrt(3) {
t.Error()
}
var item PgxItem
row := conn.QueryRow(ctx, "SELECT embedding, half_embedding, binary_embedding, sparse_embedding FROM pgx_items ORDER BY id DESC LIMIT 1", pgx.QueryResultFormats{pgx.TextFormatCode, pgx.TextFormatCode, pgx.TextFormatCode, pgx.TextFormatCode})
err = row.Scan(&item.Embedding, &item.HalfEmbedding, &item.BinaryEmbedding, &item.SparseEmbedding)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(item.Embedding.Slice(), []float32{1, 1, 2}) {
t.Error()
}
if !reflect.DeepEqual(item.HalfEmbedding.Slice(), []float32{1, 1, 2}) {
t.Error()
}
if item.BinaryEmbedding != "111" {
t.Error()
}
if !reflect.DeepEqual(item.SparseEmbedding.Slice(), []float32{1, 1, 2}) {
t.Error()
}
_, err = conn.CopyFrom(
ctx,
pgx.Identifier{"pgx_items"},
[]string{"embedding", "binary_embedding", "sparse_embedding"},
pgx.CopyFromSlice(1, func(i int) ([]any, error) {
return []interface{}{"[1,2,3]", "101", "{1:1,2:2,3:3}/3"}, nil
}),
)
if err != nil {
panic(err)
}
config, err := pgxpool.ParseConfig("postgres://localhost/pgvector_go_test")
if err != nil {
panic(err)
}
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
return pgxvector.RegisterTypes(ctx, conn)
}
pool, err := pgxpool.NewWithConfig(ctx, config)
defer pool.Close()
_, err = pool.CopyFrom(
ctx,
pgx.Identifier{"pgx_items"},
[]string{"embedding", "binary_embedding", "sparse_embedding"},
pgx.CopyFromSlice(1, func(i int) ([]any, error) {
return []interface{}{"[1,2,3]", "101", "{1:1,2:2,3:3}/3"}, nil
}),
)
if err != nil {
panic(err)
}
}