Skip to content

Commit 9265750

Browse files
dqiigrubdragon
andauthored
Fix range of HNSW's M parameter to be >= 2, add tests (#95)
Co-authored-by: Aryan Agal <20280502+grubdragon@users.noreply.github.com>
1 parent 3eaec57 commit 9265750

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/hnsw/options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void _PG_init(void)
188188
"m",
189189
"HNSW M hyperparameter",
190190
HNSW_DEFAULT_M,
191-
1,
191+
2,
192192
HNSW_MAX_M
193193
#if PG_VERSION_NUM >= 130000
194194
,

test/expected/hnsw_create.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,19 @@ EXPLAIN (COSTS FALSE) SELECT * FROM sift_base10k order by v <-> :'v4444' LIMIT 1
5353
Order By: (v <-> '{55,61,11,4,5,2,13,24,65,49,13,9,23,37,94,38,54,11,14,14,40,31,50,44,53,4,0,0,27,17,8,34,12,10,4,4,22,52,68,53,9,2,0,0,2,116,119,64,119,2,0,0,2,30,119,119,116,5,0,8,47,9,5,60,7,7,10,23,56,50,23,5,28,68,6,18,24,65,50,9,119,75,3,0,1,8,12,85,119,11,4,6,8,9,5,74,25,11,8,20,18,12,2,21,11,90,25,32,33,15,2,9,84,67,8,4,22,31,11,33,119,30,3,6,0,0,0,26}'::real[])
5454
(3 rows)
5555

56+
--- Validate that M values inside the allowed range [2, 128] do not throw an error
57+
CREATE INDEX ON small_world USING hnsw (v) WITH (M=2);
58+
INFO: done init usearch index
59+
INFO: inserted 8 elements
60+
INFO: done saving 8 vectors
61+
CREATE INDEX ON small_world USING hnsw (v) WITH (M=128);
62+
INFO: done init usearch index
63+
INFO: inserted 8 elements
64+
INFO: done saving 8 vectors
65+
---- Validate that M values outside the allowed range [2, 128] throw an error
66+
\set ON_ERROR_STOP off
67+
CREATE INDEX ON small_world USING hnsw (v) WITH (M=1);
68+
ERROR: value 1 out of bounds for option "m"
69+
CREATE INDEX ON small_world USING hnsw (v) WITH (M=129);
70+
ERROR: value 129 out of bounds for option "m"
71+
\set ON_ERROR_STOP on

test/sql/hnsw_create.sql

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ CREATE TABLE sift_base10k (
1818
\COPY sift_base10k (v) FROM '/tmp/lanterndb/vector_datasets/siftsmall_base_arrays.csv' WITH CSV;
1919
CREATE INDEX hnsw_idx ON sift_base10k USING hnsw (v dist_l2sq_ops) WITH (M=2, ef_construction=10, ef=4, dims=128);
2020
SELECT v AS v4444 FROM sift_base10k WHERE id = 4444 \gset
21-
EXPLAIN (COSTS FALSE) SELECT * FROM sift_base10k order by v <-> :'v4444' LIMIT 10;
21+
EXPLAIN (COSTS FALSE) SELECT * FROM sift_base10k order by v <-> :'v4444' LIMIT 10;
22+
23+
--- Validate that M values inside the allowed range [2, 128] do not throw an error
24+
CREATE INDEX ON small_world USING hnsw (v) WITH (M=2);
25+
CREATE INDEX ON small_world USING hnsw (v) WITH (M=128);
26+
27+
---- Validate that M values outside the allowed range [2, 128] throw an error
28+
\set ON_ERROR_STOP off
29+
CREATE INDEX ON small_world USING hnsw (v) WITH (M=1);
30+
CREATE INDEX ON small_world USING hnsw (v) WITH (M=129);
31+
\set ON_ERROR_STOP on

0 commit comments

Comments
 (0)