0
0
mirror of https://github.com/tursodatabase/libsql.git synced 2024-12-15 21:29:01 +00:00
libsql/libsql-sqlite3/test/libsql_vector.test

144 lines
5.7 KiB
Plaintext

# 2024-07-04
#
# Copyright 2024 the libSQL authors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#***********************************************************************
# This file implements regression tests for libSQL library. The
# focus of this file is vector search.
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix vector
do_execsql_test vector-1-inserts {
CREATE TABLE t1( xv FLOAT32(3) );
INSERT INTO t1(rowid,xv) VALUES(1, vector('[1,2,3]'));
INSERT INTO t1(rowid,xv) VALUES(2, vector('[2,3,4]'));
INSERT INTO t1(rowid,xv) VALUES(3, vector('[5,6,7]'));
} {}
do_execsql_test vector-1-func-valid {
SELECT vector_extract(vector('[]'));
SELECT vector_extract(vector(x''));
SELECT vector_extract(vector(' [ 1 , 2 , 3 ] '));
SELECT vector_extract(vector('[-1000000000000000000]'));
SELECT hex(vector('[1.10101010101010101010101010]'));
SELECT hex(vector32('[1.10101010101010101010101010]'));
SELECT hex(vector64('[1.10101010101010101010101010]'));
SELECT vector_extract(x'E6ED8C3F');
SELECT vector_extract(x'F37686C4BC9DF13F02');
SELECT vector_extract(vector(x'F37686C4BC9DF13F01'));
SELECT vector_distance_cos('[1,1]', '[1,1]');
SELECT vector_distance_cos('[1,1]', '[-1,-1]');
SELECT vector_distance_cos('[1,1]', '[-1,1]');
SELECT vector_distance_cos('[1,2]', '[2,1]');
SELECT
vector_extract(vector_mult(-0.5, '[2,-4]')),
vector_extract(vector_mult('[2,-4]', -0.5)),
vector_extract(vector_mult(-1, '[2,-4]')),
vector_extract(vector_mult(-0.5, vector('[2,-4]')));
SELECT vector_extract(vector_sum(
'[1,2]',
vector('[2,3]'),
vector_mult(2, vector('[3,4]'))
));
} {
{[]}
{[]}
{[1,2,3]}
{[-1e+18]}
{E6ED8C3F}
{E6ED8C3F}
{F37686C4BC9DF13F02}
{[1.10101]}
{[1.10101]}
{[-1075.72,1.88763]}
{0.0}
{2.0}
{1.0}
{0.200000002980232}
{[-1,2]} {[-1,2]} {[-2,4]} {[-1,2]}
{[9,13]}
}
do_execsql_test vector-1-agg-valid {
CREATE TABLE t_vec_agg ( a FLOAT32(2), b FLOAT32(2), c FLOAT32(2), k FLOAT );
INSERT INTO t_vec_agg VALUES (vector('[1,2]'), '[2,3]', '[3,4]', 1);
INSERT INTO t_vec_agg VALUES (vector('[2,3]'), '[3,4]', vector('[4,5]'), 2);
SELECT vector_extract(vector_sum(a)) FROM t_vec_agg;
SELECT vector_extract(vector_sum(b)) FROM t_vec_agg;
SELECT vector_extract(vector_sum(c)) FROM t_vec_agg;
INSERT INTO t_vec_agg VALUES (vector('[3,4]'), '[4,5]', '6', 3);
SELECT vector_extract(vector_sum(a)) FROM t_vec_agg;
SELECT vector_extract(vector_sum(vector_mult(b, k))) FROM t_vec_agg;
} {
{[3,5]}
{[5,7]}
{[7,9]}
{[6,9]}
{[20,26]}
}
proc error_messages {sql} {
set ret ""
set stmt [sqlite3_prepare db $sql -1 dummy]
sqlite3_step $stmt
sqlite3_finalize $stmt
set ret [sqlite3_errmsg db]
}
do_test vector-1-func-errors {
set ret [list]
lappend ret [error_messages {SELECT vector(1.2)}]
lappend ret [error_messages {SELECT vector(10)}]
lappend ret [error_messages {SELECT vector(NULL)}]
lappend ret [error_messages {SELECT vector('')}]
lappend ret [error_messages {SELECT vector('test')}]
lappend ret [error_messages {SELECT vector('[1]]')}]
lappend ret [error_messages {SELECT vector('[[1]')}]
lappend ret [error_messages {SELECT vector('[1, 2, 1.1.1, 4]')}]
lappend ret [error_messages {SELECT vector('[1.2')}]
lappend ret [error_messages {SELECT vector(x'0000000000')}]
lappend ret [error_messages {SELECT vector_distance_cos('[1,2,3]', '[1,2]')}]
lappend ret [error_messages {SELECT vector_distance_cos(vector32('[1,2,3]'), vector64('[1,2,3]'))}]
lappend ret [error_messages {SELECT vector_sum(vector32('[1,2,3]'), vector64('[1,2,3]'))}]
lappend ret [error_messages {SELECT vector_sum(vector('[1,2,3]'), vector('[1,2]'))}]
lappend ret [error_messages {SELECT vector_sum(1, vector64('[1,2,3]'))}]
lappend ret [error_messages {SELECT vector_mult(1, 2)}]
} [list {*}{
{vector: unexpected value type: got FLOAT, expected TEXT or BLOB}
{vector: unexpected value type: got INTEGER, expected TEXT or BLOB}
{vector: unexpected value type: got NULL, expected TEXT or BLOB}
{vector: must start with '['}
{vector: must start with '['}
{vector: non-space symbols after closing ']' are forbidden}
{vector: invalid float at position 0: '[1'}
{vector: invalid float at position 2: '1.1.1'}
{vector: must end with ']'}
{vector: unexpected binary type: got 0, expected 1 or 2}
{vector_distance_cos: vectors must have the same length: 3 != 2}
{vector_distance_cos: vectors must have the same type: 1 != 2}
{vector_sum: vectors must have the same type: 1 != 2}
{vector_sum: vectors must have the same length: 3 != 2}
{vector: unexpected value type: got INTEGER, expected TEXT or BLOB}
{vector_mult: unexpected parameters: got INTEGER and INTEGER, but expected vector-compatible and float-compatible types}
}]