blib/lib/DBIO/PostgreSQL/InflateColumn/Vector.pm

filter: Statementstmt 100% 85 / 85 Branchbran 75.0% 30 / 40 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 87.7% 143 / 163 CC 26 SCAR 13.3
CC
26
Cov
89%
register_column
7.1
_deflate_sparsevec
5.0
_inflate_pgvector_value
4.1
CRAP 3.8
« blib/lib/DBIO/PostgreSQL/Diff/Schema.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/NoStatement.pm »
1 package DBIO::PostgreSQL::InflateColumn::Vector;
2 # ABSTRACT: Auto-inflate/deflate pgvector columns to/from Perl data structures
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::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 28 __PACKAGE__->load_components('InflateColumn');
14  
15  
16  
17 518 ▶sub register_column {
Subroutine register_column: called CC=7 SCAR=19.7
Pod documented
18 518 my ($class, $column, $info, @rest) = @_;
19  
20 518 $class->next::method($column, $info, @rest);
21  
22 518 my ($type, $dims) = $class->_pgvector_type_info($info->{data_type});
23 518 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 495 23 518
24  
25 # this bailout is intentional
26 23 ▶ return if exists $info->{inflate_pgvector} && !$info->{inflate_pgvector};
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"inflate_pgvector"} and not $info->{"inflate_pgvector"} 1 22 23
Condition66%2/3
exists $info->{"inflate_pgvector"} and not $info->{"inflate_pgvector"}
!ll&&!rl&&r
2201
MC/DC50%1/2
exists $info->{"inflate_pgvector"} and not $info->{"inflate_pgvector"}
exists $info->{"inflate_pgvector"} not $info->{"inflate_pgvector"}
Truth table66%2/3
A and not(B)
A= exists $info->{"inflate_pgvector"}B= not $info->{"inflate_pgvector"}
ABresult
0X0
100
111
27  
28 22 ▶ $dims //= $info->{size} if defined $info->{size} && $info->{size} =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"size"} and $info->{"size"} =~ /\A\d+\z/ 7 15 22
Condition50%3/6
defined $info->{"size"} and $info->{"size"} =~ /\A\d+\z/
!ll&&!rl&&r
1507
$dims //= $info->{"size"}
l!l&&r!l&&!r
070
MC/DC50%1/2
defined $info->{"size"} and $info->{"size"} =~ /\A\d+\z/
defined $info->{"size"} $info->{"size"} =~ /\A\d+\z/
MC/DC0%0/2
$dims //= $info->{"size"}
$dims $info->{"size"}
Truth table66%2/3
A and B
A= defined $info->{"size"}B= $info->{"size"} =~ /\A\d+\z/
ABresult
0X0
100
111
Truth table33%1/3
A //= B
A= $dimsB= $info->{"size"}
ABresult
1X1
011
000
29  
30 22 my $infcopy = { %$info };
31 $class->inflate_column(
32 $column => {
33 4 ▶ inflate => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 4 my ($value, $obj) = @_;
35 4 return $class->_inflate_pgvector_value($type, $value);
36 },
37 7 ▶ deflate => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 7 my ($value, $obj) = @_;
39 7 return $class->_deflate_pgvector_value($type, $value, $dims);
40 },
41 }
42 22 );
43  
44 22 return;
45 }
46  
47 # Parses a pgvector data_type into (type, dims). Returns () for anything that
48 # is not a pgvector type. 'vector(3)' -> ('vector', 3); 'halfvec' -> ('halfvec', undef).
49 1036 ▶sub _pgvector_type_info {
Subroutine _pgvector_type_info: called CC=4 SCAR=13.9
50 1036 my ($class, $data_type) = @_;
51 1036 ▶ return unless defined $data_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_type 0 1036 1036
52  
53 1036 my ($type, $dims) = $data_type =~ /\A\s*(vector|halfvec|sparsevec)(?:\(\s*(\d+)\s*\))?\s*\z/i;
54 1036 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 990 46 1036
55  
56 46 ▶ return (lc $type, defined $dims ? 0 + $dims : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $dims ? : 24 22 46
57 }
58  
59 4 ▶sub _inflate_pgvector_value {
Subroutine _inflate_pgvector_value: called CC=4 SCAR=14.1
60 4 my ($class, $type, $value) = @_;
61  
62 4 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
63 4 ▶ return $value if ref $value; # already a structure — pass through
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 4 4
64  
65 4 ▶ return $class->_inflate_sparsevec($value) if $type eq 'sparsevec';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sparsevec" 1 3 4
66 3 return $class->_inflate_dense($value);
67 }
68  
69 7 ▶sub _deflate_pgvector_value {
Subroutine _deflate_pgvector_value: called CC=3 SCAR=11.0
70 7 my ($class, $type, $value, $dims) = @_;
71  
72 # plain strings pass through verbatim — never double-serialize
73 7 ▶ return $value unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 7 7
74  
75 7 ▶ return $class->_deflate_sparsevec($value, $dims) if $type eq 'sparsevec';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sparsevec" 4 3 7
76 3 return $class->_deflate_dense($value);
77 }
78  
79 3 ▶sub _inflate_dense {
Subroutine _inflate_dense: called CC=3 SCAR=11.0
80 3 my ($class, $value) = @_;
81  
82 3 ▶ return $value unless $value =~ /\A\s*\[.*\]\s*\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A\s*\[.*\]\s*\z/s 1 2 3
83  
84 2 my $inner = $value;
85 2 $inner =~ s/\A\s*\[//;
86 2 $inner =~ s/\]\s*\z//;
87  
88 2 ▶ return [] if $inner =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $inner =~ /\A\s*\z/ 0 2 2
89  
90 2 return [ map { $class->_coerce_number($_) } map { _trim($_) } split /,/, $inner ];
91 }
92  
93 3 ▶sub _deflate_dense {
Subroutine _deflate_dense: called CC=2 SCAR=7.1
94 3 my ($class, $value) = @_;
95  
96 3 ▶ Carp::croak('a vector/halfvec column expects an arrayref of numbers or a pgvector string')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 3 3
97 unless ref $value eq 'ARRAY';
98  
99 3 return '[' . join(',', @$value) . ']';
100 }
101  
102 1 ▶sub _inflate_sparsevec {
Subroutine _inflate_sparsevec: called CC=4 SCAR=14.0
103 1 my ($class, $value) = @_;
104  
105 1 ▶ return $value unless $value =~ /\A\s*\{.*\}\s*\/\s*\d+\s*\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ m[\A\s*\{.*\}\s*/\s*\d+\s*\z]s 0 1 1
106  
107 1 my ($body, $dims) = $value =~ /\A\s*\{(.*)\}\s*\/\s*(\d+)\s*\z/s;
108  
109 1 my %values;
110 1 for my $pair (split /,/, $body) {
111 2 ▶ next unless $pair =~ /\A\s*(\d+)\s*:\s*(.*?)\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pair =~ /\A\s*(\d+)\s*:\s*(.*?)\s*\z/ 0 2 2
112 2 $values{$1} = $class->_coerce_number($2);
113 }
114  
115 1 return { dims => 0 + $dims, values => \%values };
116 }
117  
118 4 ▶sub _deflate_sparsevec {
Subroutine _deflate_sparsevec: called CC=5 SCAR=16.1
119 4 my ($class, $value, $dims) = @_;
120  
121 4 ▶ Carp::croak('a sparsevec column expects a hashref or a pgvector string')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 0 4 4
122 unless ref $value eq 'HASH';
123  
124 4 my ($d, $values);
125 4 ▶ if (exists $value->{dims} && ref $value->{values} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{'dims'} and ref $value->{'values'} eq 'HASH') { } 1 3 4
Condition66%2/3
exists $value->{"dims"} and ref $value->{"values"} eq "HASH"
!ll&&!rl&&r
301
MC/DC50%1/2
exists $value->{"dims"} and ref $value->{"values"} eq "HASH"
exists $value->{"dims"} ref $value->{"values"} eq "HASH"
Truth table66%2/3
A and B
A= exists $value->{"dims"}B= ref $value->{"values"} eq "HASH"
ABresult
0X0
100
111
126 1 $d = $value->{dims};
127 1 $values = $value->{values};
128 }
129 else {
130 3 $values = $value;
131 3 $d = $dims;
132 }
133  
134 4 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 1 3 4
135 'cannot deflate a sparsevec hashref without dimensions: '
136 . 'declare the column as sparsevec(N) or pass { dims => N, values => { idx => val } }'
137 ) unless defined $d;
138  
139 3 my $body = join(',', map { "$_:$values->{$_}" } sort { $a <=> $b } keys %$values);
140 3 return "{$body}/$d";
141 }
142  
143 # Numeric-looking values come back as numbers (the natural Perl shape for a
144 # vector); 'NaN'/'Infinity' and anything else stay strings.
145 7 ▶sub _coerce_number {
Subroutine _coerce_number: called CC=2 SCAR=7.1
146 7 my ($class, $value) = @_;
147  
148 7 ▶ return $value unless $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/ 0 7 7
149 7 return 0 + $value;
150 }
151  
152 5 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
153 5 my ($value) = @_;
154 5 $value =~ s/\A\s+//;
155 5 $value =~ s/\s+\z//;
156 5 return $value;
157 }
158  
159  
160 28 1;
161  
162 __END__
« blib/lib/DBIO/PostgreSQL/Diff/Schema.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/NoStatement.pm »