pgvector.git / summary / log / commit / refs
commit 34b09cc062b36a8354ffa1bfe9a7d8aabdb6e455
Author: Andrew Kane <andrew@ankane.org>
Date: Wed Apr 10 20:10:37 2024 +0000
Added test for HNSW insert recall with sparsevec
test/t/013_hnsw_insert_recall.pl | 1 +
test/t/023_hnsw_bit_insert_recall.pl | 1 +
test/t/024_hnsw_halfvec_insert_recall.pl | 1 +
test/t/025_hnsw_sparsevec_insert_recall.pl | 109 +++++++++++++++++++++++++++++
4 files changed, 112 insertions(+)
diff --git a/test/t/013_hnsw_insert_recall.pl b/test/t/013_hnsw_insert_recall.pl
index d0c24f87..2c871088 100644
--- a/test/t/013_hnsw_insert_recall.pl
+++ b/test/t/013_hnsw_insert_recall.pl
@@ -98,6 +98,7 @@ for my $i (0 .. $#operators)
push(@expected, $res);
}
+ # Test approximate results
my $min = $operator eq "<#>" ? 0.80 : 0.99;
test_recall($min, $operator);
diff --git a/test/t/023_hnsw_bit_insert_recall.pl b/test/t/023_hnsw_bit_insert_recall.pl
index 04ead4cf..bee72d79 100644
--- a/test/t/023_hnsw_bit_insert_recall.pl
+++ b/test/t/023_hnsw_bit_insert_recall.pl
@@ -105,6 +105,7 @@ for my $i (0 .. $#operators)
push(@expected, $res);
}
+ # Test approximate results
my $min = $operator eq "<\%>" ? 0.95 : 0.98;
test_recall($min, $operator);
diff --git a/test/t/024_hnsw_halfvec_insert_recall.pl b/test/t/024_hnsw_halfvec_insert_recall.pl
index dd38d0aa..35b02feb 100644
--- a/test/t/024_hnsw_halfvec_insert_recall.pl
+++ b/test/t/024_hnsw_halfvec_insert_recall.pl
@@ -102,6 +102,7 @@ for my $i (0 .. $#operators)
push(@expected, $res);
}
+ # Test approximate results
my $min = $operator eq "<#>" ? 0.95 : 0.98;
test_recall($min, $operator);
diff --git a/test/t/025_hnsw_sparsevec_insert_recall.pl b/test/t/025_hnsw_sparsevec_insert_recall.pl
new file mode 100644
index 00000000..19877447
--- /dev/null
+++ b/test/t/025_hnsw_sparsevec_insert_recall.pl
@@ -0,0 +1,109 @@
+use strict;
+use warnings;
+use PostgresNode;
+use TestLib;
+use Test::More;
+
+my $node;
+my @queries = ();
+my @expected;
+my $limit = 20;
+
+sub test_recall
+{
+ my ($min, $operator) = @_;
+ my $correct = 0;
+ my $total = 0;
+
+ my $explain = $node->safe_psql("postgres", qq(
+ SET enable_seqscan = off;
+ EXPLAIN ANALYZE SELECT i FROM tst ORDER BY v $operator '$queries[0]' LIMIT $limit;
+ ));
+ like($explain, qr/Index Scan/);
+
+ for my $i (0 .. $#queries)
+ {
+ my $actual = $node->safe_psql("postgres", qq(
+ SET enable_seqscan = off;
+ SELECT i FROM tst ORDER BY v $operator '$queries[$i]' LIMIT $limit;
+ ));
+ my @actual_ids = split("\n", $actual);
+ my %actual_set = map { $_ => 1 } @actual_ids;
+
+ my @expected_ids = split("\n", $expected[$i]);
+
+ foreach (@expected_ids)
+ {
+ if (exists($actual_set{$_}))
+ {
+ $correct++;
+ }
+ $total++;
+ }
+ }
+
+ cmp_ok($correct / $total, ">=", $min, $operator);
+}
+
+# Initialize node
+$node = get_new_node('node');
+$node->init;
+$node->start;
+
+# Create table
+$node->safe_psql("postgres", "CREATE EXTENSION vector;");
+$node->safe_psql("postgres", "CREATE TABLE tst (i serial, v sparsevec(3));");
+
+# Generate queries
+for (1 .. 20)
+{
+ my $r1 = rand();
+ my $r2 = rand();
+ my $r3 = rand();
+ push(@queries, "{1:$r1,2:$r2,3:$r3}/3");
+}
+
+# Check each index type
+my @operators = ("<->", "<#>", "<=>");
+my @opclasses = ("sparsevec_l2_ops", "sparsevec_ip_ops", "sparsevec_cosine_ops");
+
+for my $i (0 .. $#operators)
+{
+ my $operator = $operators[$i];
+ my $opclass = $opclasses[$i];
+
+ # Add index
+ $node->safe_psql("postgres", "CREATE INDEX idx ON tst USING hnsw (v $opclass);");
+
+ # Use concurrent inserts
+ $node->pgbench(
+ "--no-vacuum --client=10 --transactions=1000",
+ 0,
+ [qr{actually processed}],
+ [qr{^$}],
+ "concurrent INSERTs",
+ {
+ "025_hnsw_sparsevec_insert_recall_$opclass" => "INSERT INTO tst (v) VALUES (ARRAY[random(), random(), random()]::vector::sparsevec);"
+ }
+ );
+
+ # Get exact results
+ @expected = ();
+ foreach (@queries)
+ {
+ my $res = $node->safe_psql("postgres", qq(
+ SET enable_indexscan = off;
+ SELECT i FROM tst ORDER BY v $operator '$_' LIMIT $limit;
+ ));
+ push(@expected, $res);
+ }
+
+ # Test approximate results
+ my $min = $operator eq "<#>" ? 0.80 : 0.99;
+ test_recall($min, $operator);
+
+ $node->safe_psql("postgres", "DROP INDEX idx;");
+ $node->safe_psql("postgres", "TRUNCATE tst;");
+}
+
+done_testing();
[parent: d8f3e18db604]