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

Fix l2 operator bug when used with int arrays #243

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add test showing implicit type casts are banned for indexes
Ngalstyan4 committed Dec 11, 2023
commit 397cf77741527d96a6cd84900c5c6bd8f85cebae
9 changes: 8 additions & 1 deletion test/expected/hnsw_insert.out
Original file line number Diff line number Diff line change
@@ -9,7 +9,11 @@ set work_mem = '64kB';
set client_min_messages = 'ERROR';
CREATE TABLE small_world (
id SERIAL PRIMARY KEY,
v REAL[2]
v REAL[2] -- this demonstates that postgres actually does not enforce real[] length as we actually insert vectors of length 3
);
CREATE TABLE small_world_int (
id SERIAL PRIMARY KEY,
v INTEGER[]
);
CREATE INDEX ON small_world USING hnsw (v) WITH (dim=3);
INFO: done init usearch index
@@ -28,6 +32,9 @@ INSERT INTO small_world (v) VALUES ('{0,0,1}'), ('{0,1,0}');
INSERT INTO small_world (v) VALUES (NULL);
-- Attempt to insert a row with an incorrect vector length
\set ON_ERROR_STOP off
-- Cannot create an hnsw index with implicit typecasts (trying to cast integer[] to real[], in this case)
CREATE INDEX ON small_world_int USING hnsw (v dist_l2sq_ops) WITH (dim=3);
ERROR: operator class "dist_l2sq_ops" does not accept data type integer[]
INSERT INTO small_world (v) VALUES ('{1,1,1,1}');
ERROR: Wrong number of dimensions: 4 instead of 3 expected
\set ON_ERROR_STOP on
10 changes: 9 additions & 1 deletion test/sql/hnsw_insert.sql
Original file line number Diff line number Diff line change
@@ -10,8 +10,14 @@ set client_min_messages = 'ERROR';

CREATE TABLE small_world (
id SERIAL PRIMARY KEY,
v REAL[2]
v REAL[2] -- this demonstates that postgres actually does not enforce real[] length as we actually insert vectors of length 3
);

CREATE TABLE small_world_int (
id SERIAL PRIMARY KEY,
v INTEGER[]
);

CREATE INDEX ON small_world USING hnsw (v) WITH (dim=3);
SELECT _lantern_internal.validate_index('small_world_v_idx', false);

@@ -21,6 +27,8 @@ INSERT INTO small_world (v) VALUES (NULL);

-- Attempt to insert a row with an incorrect vector length
\set ON_ERROR_STOP off
-- Cannot create an hnsw index with implicit typecasts (trying to cast integer[] to real[], in this case)
CREATE INDEX ON small_world_int USING hnsw (v dist_l2sq_ops) WITH (dim=3);
INSERT INTO small_world (v) VALUES ('{1,1,1,1}');
\set ON_ERROR_STOP on