Skip to content

Commit

Permalink
Fixed to_binary for Bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jul 15, 2024
1 parent 59bf427 commit 716e4eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/pgvector/bit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def to_a

def to_binary
buffer = [@data.length].pack("l>")
@data.split(/.{8}/).pack("B*", buffer: buffer)
# TODO improve
@data.scan(/.{1,8}/).each do |b|
[b].pack("B*", buffer: buffer)
end
buffer
end
end
Expand Down
14 changes: 7 additions & 7 deletions test/pg_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_halfvec_text
end

def test_bit_text
embedding = "101"
embedding = "1010000001"
conn.exec_params("INSERT INTO pg_items (binary_embedding) VALUES ($1), (NULL)", [embedding])

res = conn.exec("SELECT * FROM pg_items ORDER BY id").to_a
Expand All @@ -42,7 +42,7 @@ def test_bit_text
end

def test_bit_binary
embedding = "101"
embedding = "1010000001"
conn.exec_params("INSERT INTO pg_items (binary_embedding) VALUES ($1), (NULL)", [embedding])

res = conn.exec_params("SELECT * FROM pg_items ORDER BY id", [], 1).to_a
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_type_map_binary
def test_copy_text
embedding = Pgvector::Vector.new([1, 2, 3])
half_embedding = Pgvector::HalfVector.new([1, 2, 3])
binary_embedding = Pgvector::Bit.new([true, false, true])
binary_embedding = Pgvector::Bit.new([true, false, true, false, false, false, false, false, false, true])
sparse_embedding = Pgvector::SparseVector.new([1, 2, 3])
coder = PG::TextEncoder::CopyRow.new
conn.copy_data("COPY pg_items (embedding, half_embedding, binary_embedding, sparse_embedding) FROM STDIN", coder) do
Expand All @@ -108,21 +108,21 @@ def test_copy_text
res = conn.exec("SELECT * FROM pg_items").first
assert_equal [1, 2, 3], res["embedding"]
assert_equal [1, 2, 3], res["half_embedding"].to_a
assert_equal "101", res["binary_embedding"]
assert_equal "1010000001", res["binary_embedding"]
assert_equal [1, 2, 3], res["sparse_embedding"].to_a
end

def test_copy_binary
embedding = Pgvector::Vector.new([1, 2, 3])
binary_embedding = Pgvector::Bit.new([true, false, true])
binary_embedding = Pgvector::Bit.new([true, false, true, false, false, false, false, false, false, true])
sparse_embedding = Pgvector::SparseVector.new([1, 2, 3])
coder = PG::BinaryEncoder::CopyRow.new
conn.copy_data("COPY pg_items (embedding, binary_embedding, sparse_embedding) FROM STDIN WITH (FORMAT BINARY)", coder) do
conn.put_copy_data([embedding.to_binary, binary_embedding.to_binary, sparse_embedding.to_binary])
end
res = conn.exec("SELECT * FROM pg_items").first
assert_equal [1, 2, 3], res["embedding"]
assert_equal "101", res["binary_embedding"]
assert_equal "1010000001", res["binary_embedding"]
assert_equal [1, 2, 3], res["sparse_embedding"].to_a
end

Expand All @@ -134,7 +134,7 @@ def conn
conn.exec("CREATE EXTENSION IF NOT EXISTS vector")
end
conn.exec("DROP TABLE IF EXISTS pg_items")
conn.exec("CREATE TABLE pg_items (id bigserial PRIMARY KEY, embedding vector(3), half_embedding halfvec(3), binary_embedding bit(3), sparse_embedding sparsevec(3))")
conn.exec("CREATE TABLE pg_items (id bigserial PRIMARY KEY, embedding vector(3), half_embedding halfvec(3), binary_embedding bit(10), sparse_embedding sparsevec(3))")

registry = PG::BasicTypeRegistry.new.define_default_types
Pgvector::PG.register_vector(registry)
Expand Down

0 comments on commit 716e4eb

Please sign in to comment.