blib/lib/DBIO/PostgreSQL/ResultSet.pm

filter: Statementstmt 100% 28 / 28 Branchbran 100% 12 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 96.0% 49 / 51 CC 8 SCAR 17.4
CC
8
Cov
98%
nearest_neighbors
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.7
« blib/lib/DBIO/PostgreSQL/Diff/CaseOnly.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Extension.pm »
1 package DBIO::PostgreSQL::ResultSet;
2 # ABSTRACT: ResultSet with pgvector nearest-neighbor search
3  
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 28 our $VERSION = '0.900007';
8  
9 28 ▶use base 'DBIO::ResultSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 28 ▶use DBIO::PostgreSQL::Vector qw(vector);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Distance keyword -> DBIO::PostgreSQL::Vector::Op method. The keyword set is
14 # the one the Rails "neighbor" gem uses, so a query ported from that ecosystem
15 # keeps its strings; the aliases (l2, l1) are the metric names pgvector's own
16 # documentation uses for the same operators.
17 28 my %DISTANCE_METHOD = (
18 euclidean => 'l2_distance',
19 l2 => 'l2_distance',
20 cosine => 'cosine_distance',
21 inner_product => 'max_inner_product',
22 taxicab => 'l1_distance',
23 l1 => 'l1_distance',
24 hamming => 'hamming_distance',
25 jaccard => 'jaccard_distance',
26 );
27  
28 28 my %KNOWN_OPTION = map { $_ => 1 } qw( distance rows cast );
29  
30 22 ▶sub nearest_neighbors {
Subroutine nearest_neighbors: called CC=8 SCAR=20.8
Pod documented
31 22 my ( $self, $column, $query_vector, %opts ) = @_;
32  
33 22 my @unknown = sort grep { !$KNOWN_OPTION{$_} } keys %opts;
34 $self->throw_exception( 'nearest_neighbors: unknown option '
35 22 ▶ . join( ', ', map { "'$_'" } @unknown )
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 21 22
36 . ' (known: '
37 . join( ', ', sort keys %KNOWN_OPTION )
38 . '); chain ->search for everything else' )
39 if @unknown;
40  
41 # Required on purpose: the metric is what has to match the operator class of
42 # the index (see the OPERATOR CLASSES section), so it is never guessed here.
43 $self->throw_exception( 'nearest_neighbors: distance is required (one of: '
44 . join( ', ', sort keys %DISTANCE_METHOD )
45 . ')' )
46 21 ▶ unless defined $opts{distance} && length $opts{distance};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"distance"} and length $opts{"distance"} 1 20 21
Condition66%2/3
defined $opts{"distance"} and length $opts{"distance"}
!ll&&!rl&&r
1020
MC/DC50%1/2
defined $opts{"distance"} and length $opts{"distance"}
defined $opts{"distance"} length $opts{"distance"}
Truth table66%2/3
A and B
A= defined $opts{"distance"}B= length $opts{"distance"}
ABresult
0X0
100
111
47  
48 20 my $method = $DISTANCE_METHOD{ lc $opts{distance} };
49 20 ▶ $self->throw_exception( "nearest_neighbors: unknown distance '$opts{distance}' (known: "
Branch: 1 branch
BranchTrueFalseTotal
unless $method 1 19 20
50 . join( ', ', sort keys %DISTANCE_METHOD )
51 . ')' )
52 unless $method;
53  
54 # An unqualified name belongs to this resultset's own source; a caller who
55 # spelled out an alias (a joined source, or plain 'me.') keeps their string.
56 19 ▶ my $col =
Branch: 1 branch
BranchTrueFalseTotal
index($column, '.') != -1 ? : 1 18 19
57 index( $column, '.' ) >= 0
58 ? $column
59 : $self->current_source_alias . '.' . $column;
60  
61 19 ▶ my $op = vector( $col, ( defined $opts{cast} ? ( cast => $opts{cast} ) : () ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'cast'} ? : 2 17 19
62 ->$method($query_vector);
63  
64 return $self->search(
65 undef,
66 {
67 order_by => $op->as_order,
68 '+select' => [ $op->as_select('neighbor_distance') ],
69 '+as' => ['neighbor_distance'],
70 17 ▶ ( defined $opts{rows} ? ( rows => $opts{rows} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'rows'} ? : 1 16 17
71 },
72 );
73 }
74  
75  
76  
77 28 1;
78  
79 __END__
« blib/lib/DBIO/PostgreSQL/Diff/CaseOnly.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Extension.pm »