2024-11-02 22:04:45 +00:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
makeTest,
|
2024-02-18 15:53:15 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
2024-11-02 20:24:48 +00:00
|
|
|
inherit (pkgs) lib;
|
|
|
|
|
2024-02-18 15:53:15 +00:00
|
|
|
# Test cases from https://docs.pgvecto.rs/use-cases/hybrid-search.html
|
|
|
|
test-sql = pkgs.writeText "postgresql-test" ''
|
|
|
|
CREATE EXTENSION vectors;
|
|
|
|
|
|
|
|
CREATE TABLE items (
|
|
|
|
id bigserial PRIMARY KEY,
|
|
|
|
content text NOT NULL,
|
|
|
|
embedding vectors.vector(3) NOT NULL -- 3 dimensions
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO items (content, embedding) VALUES
|
|
|
|
('a fat cat sat on a mat and ate a fat rat', '[1, 2, 3]'),
|
|
|
|
('a fat dog sat on a mat and ate a fat rat', '[4, 5, 6]'),
|
|
|
|
('a thin cat sat on a mat and ate a thin rat', '[7, 8, 9]'),
|
|
|
|
('a thin dog sat on a mat and ate a thin rat', '[10, 11, 12]');
|
|
|
|
'';
|
2024-11-01 18:47:08 +00:00
|
|
|
|
2024-11-02 22:04:45 +00:00
|
|
|
makeTestFor =
|
2024-11-12 20:02:10 +00:00
|
|
|
package:
|
2024-11-02 20:24:48 +00:00
|
|
|
makeTest {
|
2024-11-12 20:02:10 +00:00
|
|
|
name = "pgvecto-rs-${package.name}";
|
2024-11-02 20:24:48 +00:00
|
|
|
meta = with lib.maintainers; {
|
|
|
|
maintainers = [ diogotcorreia ];
|
2024-02-18 15:53:15 +00:00
|
|
|
};
|
|
|
|
|
2024-11-02 22:04:45 +00:00
|
|
|
nodes.machine =
|
|
|
|
{ ... }:
|
2024-11-02 20:24:48 +00:00
|
|
|
{
|
|
|
|
services.postgresql = {
|
2024-11-12 20:02:10 +00:00
|
|
|
inherit package;
|
2024-11-02 20:24:48 +00:00
|
|
|
enable = true;
|
2024-11-12 20:02:10 +00:00
|
|
|
enableJIT = lib.hasInfix "-jit-" package.name;
|
2024-11-11 22:23:46 +00:00
|
|
|
extensions =
|
2024-11-02 22:04:45 +00:00
|
|
|
ps: with ps; [
|
|
|
|
pgvecto-rs
|
|
|
|
];
|
2024-11-02 20:24:48 +00:00
|
|
|
settings.shared_preload_libraries = "vectors";
|
|
|
|
};
|
|
|
|
};
|
2024-02-18 15:53:15 +00:00
|
|
|
|
2024-11-02 22:04:45 +00:00
|
|
|
testScript =
|
|
|
|
{ nodes, ... }:
|
|
|
|
let
|
|
|
|
inherit (nodes.machine.services.postgresql.package.pkgs) pgvecto-rs;
|
|
|
|
in
|
|
|
|
''
|
|
|
|
def check_count(statement, lines):
|
|
|
|
return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
|
|
|
|
statement, lines
|
|
|
|
)
|
2024-02-18 15:53:15 +00:00
|
|
|
|
|
|
|
|
2024-11-02 22:04:45 +00:00
|
|
|
machine.start()
|
|
|
|
machine.wait_for_unit("postgresql")
|
2024-02-18 15:53:15 +00:00
|
|
|
|
2024-11-02 22:04:45 +00:00
|
|
|
with subtest("Postgresql with extension vectors is available just after unit start"):
|
|
|
|
machine.succeed(check_count("SELECT * FROM pg_available_extensions WHERE name = 'vectors' AND default_version = '${pgvecto-rs.version}';", 1))
|
2024-02-18 15:53:15 +00:00
|
|
|
|
2024-11-02 22:04:45 +00:00
|
|
|
machine.succeed("sudo -u postgres psql -f ${test-sql}")
|
2024-02-18 15:53:15 +00:00
|
|
|
|
2024-11-02 22:04:45 +00:00
|
|
|
machine.succeed(check_count("SELECT content, embedding FROM items WHERE to_tsvector('english', content) @@ 'cat & rat'::tsquery;", 2))
|
2024-02-18 15:53:15 +00:00
|
|
|
|
2024-11-02 22:04:45 +00:00
|
|
|
machine.shutdown()
|
|
|
|
'';
|
2024-11-02 20:24:48 +00:00
|
|
|
};
|
2024-02-18 15:53:15 +00:00
|
|
|
in
|
2024-11-02 20:24:48 +00:00
|
|
|
lib.recurseIntoAttrs (
|
2024-11-02 22:04:45 +00:00
|
|
|
lib.concatMapAttrs (n: p: { ${n} = makeTestFor p; }) (
|
|
|
|
lib.filterAttrs (_: p: !p.pkgs.pgvecto-rs.meta.broken) pkgs.postgresqlVersions
|
|
|
|
)
|
2024-11-02 17:58:52 +00:00
|
|
|
// {
|
2024-11-02 20:24:48 +00:00
|
|
|
passthru.override = p: makeTestFor p;
|
2024-11-02 17:58:52 +00:00
|
|
|
}
|
|
|
|
)
|