Skip to content

Commit

Permalink
Improved pgx test for bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jul 24, 2024
1 parent d82eb00 commit d5e5fc2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pgx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/pgvector/pgvector-go"
pgxvector "github.com/pgvector/pgvector-go/pgx"
)
Expand Down Expand Up @@ -70,23 +71,25 @@ func TestPgx(t *testing.T) {

CreatePgxItems(ctx, conn)

rows, err := conn.Query(ctx, "SELECT id, embedding, half_embedding, sparse_embedding, embedding <-> $1 FROM pgx_items ORDER BY embedding <-> $1 LIMIT 5", pgvector.NewVector([]float32{1, 1, 1}))
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
// TODO scan BinaryEmbedding
err = rows.Scan(&item.Id, &item.Embedding, &item.HalfEmbedding, &item.SparseEmbedding, &distance)
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)
}

Expand All @@ -103,6 +106,9 @@ func TestPgx(t *testing.T) {
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()
}
Expand Down

0 comments on commit d5e5fc2

Please sign in to comment.