File: 033_comparison.pl

package info (click to toggle)
pgvector 0.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,124 kB
  • sloc: ansic: 8,966; perl: 2,786; sql: 2,016; makefile: 50; sh: 1
file content (48 lines) | stat: -rw-r--r-- 1,308 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;

my $node;
my $array_sql = join(",", ('floor(random() * 2)::int - 1') x 3);

# Initialize node
$node = PostgreSQL::Test::Cluster->new('node');
$node->init;
$node->start;

# Create table
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
$node->safe_psql("postgres", "CREATE TABLE tst (v real[]);");
$node->safe_psql("postgres",
	"INSERT INTO tst SELECT ARRAY[$array_sql] FROM generate_series(1, 10000) i;"
);

for (1 .. 50)
{
	# Generate query
	my @r = ();
	for (1 .. (int(rand() * 3) + 2))
	{
		push(@r, int(rand() * 2) - 1);
	}
	my $query = "{" . join(",", @r) . "}";

	# Get expected result
	my $expected = $node->safe_psql("postgres", "SELECT btarraycmp(v, '$query') FROM tst");

	# Test vector
	my $actual = $node->safe_psql("postgres", "SELECT vector_cmp(v::vector, '$query'::real[]::vector) FROM tst");
	is($expected, $actual);

	# Test halfvec
	$actual = $node->safe_psql("postgres", "SELECT halfvec_cmp(v::halfvec, '$query'::real[]::halfvec) FROM tst");
	is($expected, $actual);

	# Test sparsevec
	$actual = $node->safe_psql("postgres", "SELECT sparsevec_cmp(v::vector::sparsevec, '$query'::real[]::vector::sparsevec) FROM tst");
	is($expected, $actual);
}

done_testing();