Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Decoder for pgvector #57

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
export MINI_SQL_MYSQL_HOST=127.0.0.1
export MINI_SQL_MYSQL_PORT=33306

docker run --name mini-sql-postgres --rm -it -p 55432:5432 -e POSTGRES_DB=test_mini_sql -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres
docker run --name mini-sql-postgres --rm -it -p 55432:5432 -e POSTGRES_DB=test_mini_sql -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres # or ankane/pgvector for testing vector type decoder
export MINI_SQL_PG_USER=postgres
export MINI_SQL_PG_HOST=127.0.0.1
export MINI_SQL_PG_PORT=55432
Expand Down
14 changes: 14 additions & 0 deletions lib/mini_sql/postgres/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ def self.type_map(conn)
else
map.add_coder(MiniSql::Postgres::Coders::TimestampUtc.new(name: "timestamp", oid: 1114, format: 0))
end

if defined? Pgvector::PG
vector_oid =
PG::BasicTypeRegistry::CoderMapsBundle
.new(conn)
.typenames_by_oid
.find { |k, v| v == "vector" }
&.first

if !vector_oid.nil?
map.add_coder(Pgvector::PG::TextDecoder::Vector.new(name: "vector", oid: vector_oid, format: 0))
map.add_coder(Pgvector::PG::BinaryDecoder::Vector.new(name: "vector", oid: vector_oid, format: 1))
end
end
map
end
end
Expand Down
3 changes: 3 additions & 0 deletions mini_sql.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "mysql2"
spec.add_development_dependency "sqlite3", "~> 1.4.4"
spec.add_development_dependency "activerecord", "~> 7.0.0"
if RUBY_VERSION >= "3.0"
spec.add_development_dependency "pgvector", "~> 0.2.1"
end
end
end
10 changes: 10 additions & 0 deletions test/mini_sql/postgres/connection_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "test_helper"
require "pgvector" if RUBY_ENGINE != "jruby" && RUBY_VERSION >= "3.0"

class MiniSql::Postgres::TestConnection < Minitest::Test
def setup
Expand Down Expand Up @@ -48,6 +49,15 @@ def test_cidr
assert_equal(IPAddr.new("1.2.3.0/24"), ip)
end

def test_vector
skip if @connection.query("SELECT 1 FROM pg_available_extensions WHERE name = 'vector';").empty?

vector = [0.1, -0.2, 0.3]
@connection.exec("SET client_min_messages TO WARNING; CREATE EXTENSION IF NOT EXISTS vector;")
result = @connection.query_single("SELECT '[:vector]'::vector", vector: vector)
assert_equal(vector, result.first)
end

def test_bool
b = @connection.query_single("select true").first
assert_equal(true, b)
Expand Down