Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
my $node;
my @queries = ();
my $dim = 5;
my $array_sql = join(",", ('floor(random() * 4)::int - 2') x $dim);
 
# Initialize node
$node = PostgreSQL::Test::Cluster->new('node');
$node->init;
$node->start;
 
# Create table
$node->safe_psql("postgres", "CREATE TABLE tst (v vector($dim));");
$node->safe_psql("postgres",
    "INSERT INTO tst SELECT ARRAY[$array_sql] FROM generate_series(1, 10000) i;"
);
 
# Generate queries
for (1 .. 20)
{
    my @r = ();
    for (1 .. $dim)
    {
        push(@r, int(rand() * 4) - 2);
    }
    push(@queries, "[" . join(",", @r) . "]");
}
 
# Check each distance function
my @functions = ("l2_distance", "inner_product", "cosine_distance", "l1_distance");
 
for my $function (@functions)
{
    for my $query (@queries)
    {
        my $expected = $node->safe_psql("postgres", "SELECT $function(v, '$query') FROM tst");
 
        # Test halfvec
        my $actual = $node->safe_psql("postgres", "SELECT $function(v::halfvec, '$query'::vector::halfvec) FROM tst");
        is($expected, $actual, "halfvec $function");
 
        # Test sparsevec
        $actual = $node->safe_psql("postgres", "SELECT $function(v::sparsevec, '$query'::vector::sparsevec) FROM tst");
        is($expected, $actual, "sparsevec $function");
    }
}
 
done_testing();