blib/lib/DBIO/Storage/QueryRewrite.pm

filter: Statementstmt 96.9% 539 / 556 Branchbran 86.4% 306 / 354 Conditioncond 72.0% 253 / 351 MC/DCmcdc 50.8% 95 / 187 Subroutinesub 100% 27 / 27 Podpod n/a totaltotal 82.7% 1220 / 1475 CC 350 SCAR 37.8
CC
350
Cov
87%
_collapse_cond
78.8
_resolve_aliastypes_from_select_args
57.8
_adjust_select_args_for_complex_prefetch
54.8
CRAP 43.9
« blib/lib/DBIO/Storage/DBI/DataTypeClassifier.pm ↑ Summary blib/lib/DBIO/Optional/Dependencies.pm »
1 package DBIO::Storage::QueryRewrite;
2 # ABSTRACT: SQL analysis and query rewrite utilities
3  
4 344 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 344 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 344 our $VERSION = '0.900007';
8  
9 344 ▶use base 'DBIO::Storage';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 344 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 344 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 344 ▶use DBIO::Util qw(UNRESOLVABLE_CONDITION serialize);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 344 ▶use DBIO::Util qw(is_plain_value is_literal_value);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 344 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 344 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 137 ▶sub _prune_unused_joins {
Subroutine _prune_unused_joins: called CC=17 SCAR=28.7
20 137 my ($self, $attrs) = @_;
21  
22 # only standard {from} specs are supported, and we could be disabled in general
23 return ($attrs->{from}, {}) unless (
24 ref $attrs->{from} eq 'ARRAY'
25 and
26 137 ▶ @{$attrs->{from}} > 1
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_join_optimizer_override'} ? : 33 104 137
27 and
28 ref $attrs->{from}[0] eq 'HASH'
29 and
30 ref $attrs->{from}[1] eq 'ARRAY'
31 and
32 # _adjust_select_args_for_complex_prefetch forces the optimizer on for the
33 # inner subquery and off again once that {from} is pruned, and it needs a
34 # dynamically scoped switch to do it. It used to local()ise the raw
35 # _use_join_optimizer slot, i.e. the private storage of an accessor in the
36 # dbms_capability group -- which stopped working the moment capabilities
37 # became delegable (on a replicated storage the read goes to the
38 # master backend and never sees a slot set here). The override is its own
39 # slot now: join pruning is a core query rewrite, not an engine fact, so it
40 # has no business borrowing a capability's storage.
41 ( exists $self->{_join_optimizer_override}
42 ? $self->{_join_optimizer_override}
43 137 ▶ : $self->_use_join_optimizer )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 and ref $attrs->{"from"}[0] eq "HASH" and ref $attrs->{"from"}[1] eq "ARRAY" and exists $self->{"_join_optimizer_override"} ? $self->{"_join_optimizer_override"} : $self->_use_join_optimizer 13 124 137
Condition41%5/12
ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 and ref $attrs->{"from"}[0] eq "HASH" and ref $attrs->{"from"}[1] eq "ARRAY" and exists $self->{"_join_optimizer_override"} ? $self->{"_join_optimizer_override"} : $self->_use_join_optimizer
!ll&&!rl&&r
013124
ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 and ref $attrs->{"from"}[0] eq "HASH" and ref $attrs->{"from"}[1] eq "ARRAY"
!ll&&!rl&&r
00137
ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 and ref $attrs->{"from"}[0] eq "HASH"
!ll&&!rl&&r
00137
ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1
!ll&&!rl&&r
00137
MC/DC20%1/5
ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 and ref $attrs->{"from"}[0] eq "HASH" and ref $attrs->{"from"}[1] eq "ARRAY" and exists $self->{"_join_optimizer_override"} ? $self->{"_join_optimizer_override"} : $self->_use_join_optimizer
ref $attrs->{"from"} eq "ARRAY" @{$attrs->{"from"};} > 1 ref $attrs->{"from"}[0] eq "HASH" ref $attrs->{"from"}[1] eq "ARRAY" exists $self->{"_join_optimizer_override"} ? $self->{"_join_optimizer_override"} : $self->_use_join_optimizer
Truth table33%2/6
A and B and C and D and E
A= ref $attrs->{"from"} eq "ARRAY"B= @{$attrs->{"from"};} > 1C= ref $attrs->{"from"}[0] eq "HASH"D= ref $attrs->{"from"}[1] eq "ARRAY"E= exists $self->{"_join_optimizer_override"} ? $self->{"_join_optimizer_override"} : $self->_use_join_optimizer
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
44 );
45  
46 124 my $orig_aliastypes = $self->_resolve_aliastypes_from_select_args($attrs);
47  
48 124 my $new_aliastypes = { %$orig_aliastypes };
49  
50 # we will be recreating this entirely
51 124 my @reclassify = 'joining';
52  
53 # a grouped set will not be affected by amount of rows. Thus any
54 # purely multiplicator classifications can go
55 # (will be reintroduced below if needed by something else)
56 push @reclassify, qw(multiplying premultiplied)
57 124 ▶ if $attrs->{_force_prune_multiplying_joins} or $attrs->{group_by};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"_force_prune_multiplying_joins"} or $attrs->{"group_by"} 44 80 124
Condition100%3/3
$attrs->{"_force_prune_multiplying_joins"} or $attrs->{"group_by"}
l!l&&r!l&&!r
202480
MC/DC100%2/2
$attrs->{"_force_prune_multiplying_joins"} or $attrs->{"group_by"}
$attrs->{"_force_prune_multiplying_joins"} $attrs->{"group_by"}
Truth table100%3/3
A or B
A= $attrs->{"_force_prune_multiplying_joins"}B= $attrs->{"group_by"}
ABresult
1X1
011
000
58  
59 # nuke what will be recalculated
60 124 delete @{$new_aliastypes}{@reclassify};
61  
62 124 my @newfrom = $attrs->{from}[0]; # FROM head is always present
63  
64 # recalculate what we need once the multipliers are potentially gone
65 # ignore premultiplies, since they do not add any value to anything
66 124 my %need_joins;
67 124 for ( @{$new_aliastypes}{grep { $_ ne 'premultiplied' } keys %$new_aliastypes }) {
68 # add all requested aliases
69 341 $need_joins{$_} = 1 for keys %$_;
70  
71 # add all their parents (as per joinpath which is an AoH { table => alias })
72 341 $need_joins{$_} = 1 for map { values %$_ } map { @{$_->{-parents}} } values %$_;
73 }
74  
75 124 for my $j (@{$attrs->{from}}[1..$#{$attrs->{from}}]) {
76 push @newfrom, $j if (
77 (! defined $j->[0]{-alias}) # legacy crap
78 ||
79 $need_joins{$j->[0]{-alias}}
80 218 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $j->[0]{"-alias"} or $need_joins{$j->[0]{"-alias"}} 199 19 218
Condition100%3/3
not defined $j->[0]{"-alias"} or $need_joins{$j->[0]{"-alias"}}
l!l&&r!l&&!r
219719
MC/DC100%2/2
not defined $j->[0]{"-alias"} or $need_joins{$j->[0]{"-alias"}}
not defined $j->[0]{"-alias"} $need_joins{$j->[0]{"-alias"}}
Truth table100%3/3
not(A) or B
A= not defined $j->[0]{"-alias"}B= $need_joins{$j->[0]{"-alias"}}
ABresult
1X1
011
000
81 }
82  
83 # we have a new set of joiners - for everything we nuked pull the classification
84 # off the original stack
85 124 for my $ctype (@reclassify) {
86 $new_aliastypes->{$ctype} = { map
87 391 ▶ { $need_joins{$_} ? ( $_ => $orig_aliastypes->{$ctype}{$_} ) : () }
Branch: 1 branch
BranchTrueFalseTotal
$need_joins{$_} ? : 357 34 391
88 212 keys %{$orig_aliastypes->{$ctype}}
89 }
90 }
91  
92 124 return ( \@newfrom, $new_aliastypes );
93 }
94  
95  
96 20 ▶sub _adjust_select_args_for_complex_prefetch {
Subroutine _adjust_select_args_for_complex_prefetch: called CC=46 SCAR=40.0
97 20 my ($self, $attrs) = @_;
98  
99 $self->throw_exception ('Complex prefetches are not supported on resultsets with a custom from attribute') unless (
100 ref $attrs->{from} eq 'ARRAY'
101 and
102 20 @{$attrs->{from}} > 1
103 and
104 ref $attrs->{from}[0] eq 'HASH'
105 and
106 20 ▶ ref $attrs->{from}[1] eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 and ref $attrs->{"from"}[0] eq "HASH" and ref $attrs->{"from"}[1] eq "ARRAY" 0 20 20
Condition33%3/9
ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 and ref $attrs->{"from"}[0] eq "HASH" and ref $attrs->{"from"}[1] eq "ARRAY"
!ll&&!rl&&r
0020
ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 and ref $attrs->{"from"}[0] eq "HASH"
!ll&&!rl&&r
0020
ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1
!ll&&!rl&&r
0020
MC/DC0%0/4
ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 and ref $attrs->{"from"}[0] eq "HASH" and ref $attrs->{"from"}[1] eq "ARRAY"
ref $attrs->{"from"} eq "ARRAY" @{$attrs->{"from"};} > 1 ref $attrs->{"from"}[0] eq "HASH" ref $attrs->{"from"}[1] eq "ARRAY"
Truth table20%1/5
A and B and C and D
A= ref $attrs->{"from"} eq "ARRAY"B= @{$attrs->{"from"};} > 1C= ref $attrs->{"from"}[0] eq "HASH"D= ref $attrs->{"from"}[1] eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
107 );
108  
109 20 my $root_alias = $attrs->{alias};
110  
111 # generate inner/outer attribute lists, remove stuff that doesn't apply
112 20 my $outer_attrs = { %$attrs };
113 20 delete @{$outer_attrs}{qw(from bind rows offset group_by _grouped_by_distinct having)};
114  
115 20 my $inner_attrs = { %$attrs, _simple_passthrough_construction => 1 };
116 20 delete @{$inner_attrs}{qw(for collapse select as)};
117  
118 # there is no point of ordering the insides if there is no limit
119 delete $inner_attrs->{order_by} if (
120 delete $inner_attrs->{_order_is_artificial}
121 or
122 ! $inner_attrs->{rows}
123 20 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if delete $inner_attrs->{"_order_is_artificial"} or not $inner_attrs->{"rows"} 4 16 20
Condition66%2/3
delete $inner_attrs->{"_order_is_artificial"} or not $inner_attrs->{"rows"}
l!l&&r!l&&!r
0416
MC/DC50%1/2
delete $inner_attrs->{"_order_is_artificial"} or not $inner_attrs->{"rows"}
delete $inner_attrs->{"_order_is_artificial"} not $inner_attrs->{"rows"}
Truth table66%2/3
A or not(B)
A= delete $inner_attrs->{"_order_is_artificial"}B= not $inner_attrs->{"rows"}
ABresult
1X1
011
000
124  
125 # generate the inner/outer select lists
126 # for inside we consider only stuff *not* brought in by the prefetch
127 # on the outside we substitute any function for its alias
128 20 $outer_attrs->{select} = [ @{$attrs->{select}} ];
129  
130 20 my ($root_node, $root_node_offset);
131  
132 20 for my $i (0 .. $#{$inner_attrs->{from}}) {
133 22 my $node = $inner_attrs->{from}[$i];
134 22 ▶ my $h = (ref $node eq 'HASH') ? $node
Branch: 2 branches
BranchTrueFalseTotal
ref $node eq 'HASH' ? : 20 2 22
ref $node eq 'ARRAY' && ref $node->[0] eq 'HASH' ? : 2 0 2
Condition33%1/3
ref $node eq "ARRAY" and ref $node->[0] eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $node eq "ARRAY" and ref $node->[0] eq "HASH"
ref $node eq "ARRAY" ref $node->[0] eq "HASH"
Truth table33%1/3
A and B
A= ref $node eq "ARRAY"B= ref $node->[0] eq "HASH"
ABresult
0X0
100
111
135 : (ref $node eq 'ARRAY' and ref $node->[0] eq 'HASH') ? $node->[0]
136 : next
137 ;
138  
139 22 ▶ if ( ($h->{-alias}||'') eq $root_alias and $h->{-rsrc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($h->{"-alias"} || "") eq $root_alias and $h->{"-rsrc"}) 20 2 22
Condition60%3/5
($h->{"-alias"} || "") eq $root_alias and $h->{"-rsrc"}
!ll&&!rl&&r
2020
$h->{'-alias'} || ''
l!l
220
MC/DC0%0/2
($h->{"-alias"} || "") eq $root_alias and $h->{"-rsrc"}
$h->{'-alias'} $h->{"-rsrc"}
Truth table33%1/3
A and B
A= $h->{'-alias'}B= $h->{"-rsrc"}
ABresult
0X0
100
111
140 20 $root_node = $h;
141 20 $root_node_offset = $i;
142 20 last;
143 }
144 }
145  
146 20 ▶ $self->throw_exception ('Complex prefetches are not supported on resultsets with a custom from attribute')
Branch: 1 branch
BranchTrueFalseTotal
unless $root_node 0 20 20
147 unless $root_node;
148  
149 # use the heavy duty resolver to take care of aliased/nonaliased naming
150 20 my $colinfo = $self->_resolve_column_info($inner_attrs->{from});
151 20 my $selected_root_columns;
152  
153 20 for my $i (0 .. $#{$outer_attrs->{select}}) {
154 211 my $sel = $outer_attrs->{select}->[$i];
155  
156 next if (
157 211 ▶ $colinfo->{$sel} and $colinfo->{$sel}{-source_alias} ne $root_alias
Branch: 1 branch
BranchTrueFalseTotal
if $colinfo->{$sel} and $colinfo->{$sel}{"-source_alias"} ne $root_alias 119 92 211
Condition100%3/3
$colinfo->{$sel} and $colinfo->{$sel}{"-source_alias"} ne $root_alias
!ll&&!rl&&r
1379119
MC/DC100%2/2
$colinfo->{$sel} and $colinfo->{$sel}{"-source_alias"} ne $root_alias
$colinfo->{$sel} $colinfo->{$sel}{"-source_alias"} ne $root_alias
Truth table100%3/3
A and B
A= $colinfo->{$sel}B= $colinfo->{$sel}{"-source_alias"} ne $root_alias
ABresult
0X0
100
111
158 );
159  
160 92 ▶ if (ref $sel eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $sel eq 'HASH') { } 7 85 92
elsif (not ref $sel and my $ci = $colinfo->{$sel}) { } 79 12 91
161 7 ▶ $sel->{-as} ||= $attrs->{as}[$i];
Condition66%2/3
$sel->{"-as"} ||= $attrs->{"as"}[$i]
l!l&&r!l&&!r
340
MC/DC0%0/2
$sel->{"-as"} ||= $attrs->{"as"}[$i]
$sel->{"-as"} $attrs->{"as"}[$i]
Truth table66%2/3
A ||= B
A= $sel->{"-as"}B= $attrs->{"as"}[$i]
ABresult
1X1
011
000
162 7 ▶ $outer_attrs->{select}->[$i] = join ('.', $root_alias, ($sel->{-as} || "inner_column_$i") );
Condition50%1/2
$sel->{'-as'} || "inner_column_$i"
l!l
70
MC/DC0%0/1
$sel->{'-as'} || "inner_column_$i"
$sel->{'-as'}
Truth table50%1/2
A
A= $sel->{'-as'}
Aresult
11
00
163 }
164 ▶ elsif (! ref $sel and my $ci = $colinfo->{$sel}) {
Condition66%2/3
not ref $sel and my $ci = $colinfo->{$sel}
!ll&&!rl&&r
6079
MC/DC50%1/2
not ref $sel and my $ci = $colinfo->{$sel}
not ref $sel my $ci = $colinfo->{$sel}
Truth table66%2/3
not(A) and B
A= not ref $selB= my $ci = $colinfo->{$sel}
ABresult
0X0
100
111
165 79 $selected_root_columns->{$ci->{-colname}} = 1;
166 }
167  
168 92 push @{$inner_attrs->{select}}, $sel;
169  
170 92 push @{$inner_attrs->{as}}, $attrs->{as}[$i];
171 }
172  
173 # We will need to fetch all native columns in the inner subquery, which may
174 # be a part of an *outer* join condition, or an order_by (which needs to be
175 # preserved outside), or wheres. In other words everything but the inner
176 # selector
177 # We can not just fetch everything because a potential has_many restricting
178 # join collapse *will not work* on heavy data types.
179 20 my $connecting_aliastypes = $self->_resolve_aliastypes_from_select_args({
180 %$inner_attrs,
181 select => [],
182 });
183  
184 20 ▶ for (sort map { keys %{$_->{-seen_columns}||{}} } map { values %$_ } values %$connecting_aliastypes) {
Condition100%2/2
$_->{'-seen_columns'} || {}
l!l
9250
MC/DC100%1/1
$_->{'-seen_columns'} || {}
$_->{'-seen_columns'}
Truth table100%2/2
A
A= $_->{'-seen_columns'}
Aresult
11
00
185 121 ▶ my $ci = $colinfo->{$_} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ci = $colinfo->{$_} or next 121 0 121
186 121 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($ci->{"-source_alias"} eq $root_alias and not $$selected_root_columns{$ci->{"-colname"}}++) 1 120 121
Condition100%3/3
$ci->{"-source_alias"} eq $root_alias and not $$selected_root_columns{$ci->{"-colname"}}++
!ll&&!rl&&r
54661
MC/DC100%2/2
$ci->{"-source_alias"} eq $root_alias and not $$selected_root_columns{$ci->{"-colname"}}++
$ci->{"-source_alias"} eq $root_alias not $$selected_root_columns{$ci->{"-colname"}}++
Truth table100%3/3
A and not(B)
A= $ci->{"-source_alias"} eq $root_aliasB= not $$selected_root_columns{$ci->{"-colname"}}++
ABresult
0X0
100
111
187 $ci->{-source_alias} eq $root_alias
188 and
189 ! $selected_root_columns->{$ci->{-colname}}++
190 ) {
191 # adding it to both to keep limits not supporting dark selectors happy
192 1 push @{$inner_attrs->{select}}, $ci->{-fq_colname};
193 1 push @{$inner_attrs->{as}}, $ci->{-fq_colname};
194 }
195 }
196  
197 # construct the inner {from} and lock it in a subquery
198 # we need to prune first, because this will determine if we need a group_by below
199 # throw away all non-selecting, non-restricting multijoins
200 # (since we def. do not care about multiplication of the contents of the subquery)
201 20 my $inner_subq = do {
202  
203 # must use it here regardless of user requests (vastly gentler on optimizer)
204 20 local $self->{_join_optimizer_override} = 1;
205  
206 # throw away multijoins since we def. do not care about those inside the subquery
207 20 ($inner_attrs->{from}, my $inner_aliastypes) = $self->_prune_unused_joins ({
208 %$inner_attrs, _force_prune_multiplying_joins => 1
209 });
210  
211 # uh-oh a multiplier (which is not us) left in, this is a problem for limits
212 # we will need to add a group_by to collapse the resultset for proper counts
213 20 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ ne $root_alias;} keys %{$inner_aliastypes->{"multiplying"} or {};} and !$inner_aliastypes->{"grouping"} || $inner_attrs->{"_grouped_by_distinct"}) 7 13 20
Condition100%3/3
grep {$_ ne $root_alias;} keys %{$inner_aliastypes->{"multiplying"} or {};} and !$inner_aliastypes->{"grouping"} || $inner_attrs->{"_grouped_by_distinct"}
!ll&&!rl&&r
1127
MC/DC100%2/2
grep {$_ ne $root_alias;} keys %{$inner_aliastypes->{"multiplying"} or {};} and !$inner_aliastypes->{"grouping"} || $inner_attrs->{"_grouped_by_distinct"}
grep {$_ ne $root_alias;} keys %{$inner_aliastypes->{"multiplying"} or {};} !$inner_aliastypes->{"grouping"} || $inner_attrs->{"_grouped_by_distinct"}
Truth table100%3/3
A and B
A= grep {$_ ne $root_alias;} keys %{$inner_aliastypes->{"multiplying"} or {};}B= !$inner_aliastypes->{"grouping"} || $inner_attrs->{"_grouped_by_distinct"}
ABresult
0X0
100
111
214 12 ▶ grep { $_ ne $root_alias } keys %{ $inner_aliastypes->{multiplying} || {} }
Condition80%4/5
$inner_aliastypes->{'multiplying'} || {}
l!l
200
not $inner_aliastypes->{"grouping"} or $inner_attrs->{"_grouped_by_distinct"}
l!l&&r!l&&!r
432
MC/DC0%0/1
$inner_aliastypes->{'multiplying'} || {}
$inner_aliastypes->{'multiplying'}
MC/DC100%2/2
not $inner_aliastypes->{"grouping"} or $inner_attrs->{"_grouped_by_distinct"}
not $inner_aliastypes->{"grouping"} $inner_attrs->{"_grouped_by_distinct"}
Truth table50%1/2
A
A= $inner_aliastypes->{'multiplying'}
Aresult
11
00
Truth table100%3/3
not(A) or B
A= not $inner_aliastypes->{"grouping"}B= $inner_attrs->{"_grouped_by_distinct"}
ABresult
1X1
011
000
215 and
216 # if there are user-supplied groups - assume user knows wtf they are up to
217 ( ! $inner_aliastypes->{grouping} or $inner_attrs->{_grouped_by_distinct} )
218 ) {
219  
220 7 my $cur_sel = { map { $_ => 1 } @{$inner_attrs->{select}} };
221  
222 # *possibly* supplement the main selection with pks if not already
223 # there, as they will have to be a part of the group_by to collapse
224 # things properly
225 7 my $inner_select_with_extras;
226 7 my @pks = map { "$root_alias.$_" } $root_node->{-rsrc}->primary_columns
227 or $self->throw_exception( sprintf
228 'Unable to perform complex limited prefetch off %s without declared primary key',
229 $root_node->{-rsrc}->source_name,
230 7 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
my(@pks) = map({"$root_alias.$_";} $root_node->{"-rsrc"}->primary_columns) or $self->throw_exception(sprintf("Unable to perform complex limited prefetch off %s without declared primary key", $root_node->{"-rsrc"}->source_name)) 7 0 7
231 7 for my $col (@pks) {
232 0 ▶ push @{ $inner_select_with_extras ||= [ @{$inner_attrs->{select}} ] }, $col
Condition0%0/2
$inner_select_with_extras ||= [@{$inner_attrs->{"select"};}]
l!l
00
MC/DC0%0/1
$inner_select_with_extras ||= [@{$inner_attrs->{"select"};}]
$inner_select_with_extras
Truth table0%0/2
A
A= $inner_select_with_extras
Aresult
11
00
233 7 ▶ unless $cur_sel->{$col}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $cur_sel->{$col}++ 0 7 7
234 }
235  
236 7 ▶ ($inner_attrs->{group_by}, $inner_attrs->{order_by}) = $self->_group_over_selection({
Branch: 1 branch
BranchTrueFalseTotal
$inner_select_with_extras ? : 0 7 7
237 %$inner_attrs,
238 $inner_select_with_extras ? ( select => $inner_select_with_extras ) : (),
239 _aliastypes => $inner_aliastypes,
240 });
241 }
242  
243 # we already optimized $inner_attrs->{from} above
244 # and already local()ized
245 19 $self->{_join_optimizer_override} = 0;
246  
247 # generate the subquery
248 $self->_select_args_to_query (
249 19 @{$inner_attrs}{qw(from select where)},
250 $inner_attrs,
251 );
252 };
253  
254 # Generate the outer from - this is relatively easy (really just replace
255 # the join slot with the subquery), with a major caveat - we can not
256 # join anything that is non-selecting (not part of the prefetch), but at
257 # the same time is a multi-type relationship, as it will explode the result.
258 #
259 # There are two possibilities here
260 # - either the join is non-restricting, in which case we simply throw it away
261 # - it is part of the restrictions, in which case we need to collapse the outer
262 # result by tackling yet another group_by to the outside of the query
263  
264 # work on a shallow copy
265 19 my @orig_from = @{$attrs->{from}};
266  
267  
268 19 $outer_attrs->{from} = \ my @outer_from;
269  
270 # we may not be the head
271 19 ▶ if ($root_node_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root_node_offset) { } 1 18 19
272 # first generate the outer_from, up to the substitution point
273 1 @outer_from = splice @orig_from, 0, $root_node_offset;
274  
275 # substitute the subq at the right spot
276 push @outer_from, [
277 {
278 -alias => $root_alias,
279 -rsrc => $root_node->{-rsrc},
280 $root_alias => $inner_subq,
281 },
282 # preserve attrs from what is now the head of the from after the splice
283 1 @{$orig_from[0]}[1 .. $#{$orig_from[0]}],
284 ];
285 }
286 else {
287 @outer_from = {
288 -alias => $root_alias,
289 -rsrc => $root_node->{-rsrc},
290 18 $root_alias => $inner_subq,
291 };
292 }
293  
294 19 shift @orig_from; # what we just replaced above
295  
296 # scan the *remaining* from spec against different attributes, and see which joins are needed
297 # in what role
298 my $outer_aliastypes = $outer_attrs->{_aliastypes} =
299 19 $self->_resolve_aliastypes_from_select_args({ %$outer_attrs, from => \@orig_from });
300  
301 # unroll parents
302 19 my ($outer_select_chain, @outer_nonselecting_chains) = map { +{
303 76 ▶ map { $_ => 1 } map { values %$_} map { @{$_->{-parents}} } values %{ $outer_aliastypes->{$_} || {} }
Condition100%2/2
$outer_aliastypes->{$_} || {}
l!l
3244
MC/DC100%1/1
$outer_aliastypes->{$_} || {}
$outer_aliastypes->{$_}
Truth table100%2/2
A
A= $outer_aliastypes->{$_}
Aresult
11
00
304 } } qw/selecting restricting grouping ordering/;
305  
306 # see what's left - throw away if not selecting/restricting
307 19 my $may_need_outer_group_by;
308 19 ▶ while (my $j = shift @orig_from) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $j = shift @orig_from) 31 19 50
309 31 my $alias = $j->[0]{-alias};
310  
311 31 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($outer_select_chain->{$alias}) { } 26 5 31
elsif (grep {$_->{$alias};} @outer_nonselecting_chains) { } 5 0 5
312 $outer_select_chain->{$alias}
313 ) {
314 26 push @outer_from, $j
315 }
316 15 elsif (grep { $_->{$alias} } @outer_nonselecting_chains ) {
317 5 push @outer_from, $j;
318 5 ▶ $may_need_outer_group_by ||= $outer_aliastypes->{multiplying}{$alias} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$outer_aliastypes->{'multiplying'}{$alias} ? : 2 3 5
Condition66%2/3
$may_need_outer_group_by ||= $outer_aliastypes->{"multiplying"}{$alias} ? 1 : 0
l!l&&r!l&&!r
023
MC/DC50%1/2
$may_need_outer_group_by ||= $outer_aliastypes->{"multiplying"}{$alias} ? 1 : 0
$may_need_outer_group_by $outer_aliastypes->{"multiplying"}{$alias} ? 1 : 0
Truth table66%2/3
A ||= B
A= $may_need_outer_group_byB= $outer_aliastypes->{"multiplying"}{$alias} ? 1 : 0
ABresult
1X1
011
000
319 }
320 }
321  
322 # also throw in a synthetic group_by if a non-selecting multiplier,
323 # to guard against cross-join explosions
324 # the logic is somewhat fragile, but relies on the idea that if a user supplied
325 # a group by on their own - they know what they were doing
326 19 ▶ if ( $may_need_outer_group_by and $attrs->{_grouped_by_distinct} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($may_need_outer_group_by and $attrs->{"_grouped_by_distinct"}) 0 19 19
Condition66%2/3
$may_need_outer_group_by and $attrs->{"_grouped_by_distinct"}
!ll&&!rl&&r
1720
MC/DC0%0/2
$may_need_outer_group_by and $attrs->{"_grouped_by_distinct"}
$may_need_outer_group_by $attrs->{"_grouped_by_distinct"}
Truth table66%2/3
A and B
A= $may_need_outer_group_byB= $attrs->{"_grouped_by_distinct"}
ABresult
0X0
100
111
327 0 ($outer_attrs->{group_by}, $outer_attrs->{order_by}) = $self->_group_over_selection ({
328 %$outer_attrs,
329 from => \@outer_from,
330 });
331 }
332  
333 # FIXME: The {where} ends up in both the inner and outer query, i.e. *twice*
334 #
335 # This is rather horrific, and while we currently *do* have enough
336 # introspection tooling available to attempt a stab at properly deciding
337 # whether or not to include the where condition on the outside, the
338 # machinery is still too slow to apply it here.
339 # Thus for the time being we do not attempt any sanitation of the where
340 # clause and just pass it through on both sides of the subquery. This *will*
341 # be addressed at a later stage, most likely after folding the SQL generator
342 # into SQLMaker proper
343 #
344 # OTOH it can be seen as a plus: <ash> (notes that this query would make a DBA cry ;)
345 #
346 19 return $outer_attrs;
347 }
348  
349 #
350 # This is probably the ickiest, yet most relied upon part of the codebase:
351 # this is the place where we take arbitrary SQL input and break it into its
352 # constituent parts, making sure we know which *sources* are used in what
353 # *capacity* ( selecting / restricting / grouping / ordering / joining, etc. )
354 # Although the method is pretty horrific, the worst thing that can happen is
355 # for a classification failure, which in turn will result in a vocal exception,
356 # and will lead to a relatively prompt fix.
357 # The code has been slowly improving and is covered with a formiddable battery
358 # of tests, so can be considered "reliably stable" at this point (Oct 2015).
359 #
360 # A note to implementors attempting to "replace" this - keep in mind that while
361 # there are multiple optimization avenues, the actual "scan literal elements"
362 # part *MAY NEVER BE REMOVED*, even if in the future it is limited to only AST
363 # nodes that are deemed opaque (i.e. contain literal expressions). The use and
364 # comprehension of blackbox literals is at this point firmly a user-facing API,
365 # and is one of *the* reasons DBIO remains as flexible as it is.
366 #
367 # In other words, when working on this keep in mind that the following is both
368 # a widespread and *encouraged* way of using DBIO in the wild when push comes
369 # to shove:
370 #
371 # $rs->search( {}, {
372 # select => \[ $random, @stuff],
373 # from => \[ $random, @stuff ],
374 # where => \[ $random, @stuff ],
375 # group_by => \[ $random, @stuff ],
376 # order_by => \[ $random, @stuff ],
377 # } )
378 #
379 # Various incarnations of the above are reflected in many of the tests. If one
380 # gets to fail, or if a user complains: you get to fix it. A stance amounting
381 # to "this is crazy, nobody does that" is not acceptable going forward.
382 #
383  
384 172 ▶sub _resolve_aliastypes_from_select_args {
Subroutine _resolve_aliastypes_from_select_args: called CC=53 SCAR=40.6
385 172 my ( $self, $attrs ) = @_;
386  
387 $self->throw_exception ('Unable to analyze custom {from}')
388 172 ▶ if ref $attrs->{from} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $attrs->{"from"} ne "ARRAY" 0 172 172
389  
390 # what we will return
391 172 my $aliases_by_type;
392  
393 # see what aliases are there to work with
394 # and record who is a multiplier and who is premultiplied
395 my $alias_list;
396 172 for my $node (@{$attrs->{from}}) {
397  
398 452 my $j = $node;
399 452 ▶ $j = $j->[0] if ref $j eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $j eq "ARRAY" 299 153 452
400 my $al = $j->{-alias}
401 452 ▶ or next;
Branch: 1 branch
BranchTrueFalseTotal
my $al = $j->{"-alias"} or next 448 4 452
402  
403 448 $alias_list->{$al} = $j;
404  
405 $aliases_by_type->{multiplying}{$al} ||= { -parents => $j->{-join_path}||[] }
406 # not array == {from} head == can't be multiplying
407 448 ▶ if ref($node) eq 'ARRAY' and ! $j->{-is_single};
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "ARRAY" and not $j->{"-is_single"} 155 293 448
Condition71%5/7
ref $node eq "ARRAY" and not $j->{"-is_single"}
!ll&&!rl&&r
151142155
$aliases_by_type->{"multiplying"}{$al} ||= {"-parents", $j->{"-join_path"} || []}
l!l
0155
$j->{'-join_path'} || []
l!l
1550
MC/DC100%2/2
ref $node eq "ARRAY" and not $j->{"-is_single"}
ref $node eq "ARRAY" not $j->{"-is_single"}
MC/DC0%0/1
$aliases_by_type->{"multiplying"}{$al} ||= {"-parents", $j->{"-join_path"} || []}
$aliases_by_type->{"multiplying"}{$al}
MC/DC0%0/1
$j->{'-join_path'} || []
$j->{'-join_path'}
Truth table100%3/3
A and not(B)
A= ref $node eq "ARRAY"B= not $j->{"-is_single"}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $aliases_by_type->{"multiplying"}{$al}
Aresult
11
00
Truth table50%1/2
A
A= $j->{'-join_path'}
Aresult
11
00
408  
409 $aliases_by_type->{premultiplied}{$al} ||= { -parents => $j->{-join_path}||[] }
410 # parts of the path that are not us but are multiplying
411 157 if grep { $aliases_by_type->{multiplying}{$_} }
412 454 grep { $_ ne $al }
413 454 map { values %$_ }
414 448 ▶ @{ $j->{-join_path}||[] }
Branch: 1 branch
BranchTrueFalseTotal
if grep {$aliases_by_type->{"multiplying"}{$_};} grep({$_ ne $al;} map({values %$_;} @{$j->{"-join_path"} or [];})) 59 389 448
Condition66%4/6
$j->{'-join_path'} || []
l!l
297151
$aliases_by_type->{"premultiplied"}{$al} ||= {"-parents", $j->{"-join_path"} || []}
l!l
059
$j->{'-join_path'} || []
l!l
590
MC/DC100%1/1
$j->{'-join_path'} || []
$j->{'-join_path'}
MC/DC0%0/1
$aliases_by_type->{"premultiplied"}{$al} ||= {"-parents", $j->{"-join_path"} || []}
$aliases_by_type->{"premultiplied"}{$al}
MC/DC0%0/1
$j->{'-join_path'} || []
$j->{'-join_path'}
Truth table100%2/2
A
A= $j->{'-join_path'}
Aresult
11
00
Truth table50%1/2
A
A= $aliases_by_type->{"premultiplied"}{$al}
Aresult
11
00
Truth table50%1/2
A
A= $j->{'-join_path'}
Aresult
11
00
415 }
416  
417 # get a column to source/alias map (including unambiguous unqualified ones)
418 172 my $colinfo = $self->_resolve_column_info ($attrs->{from});
419  
420 # set up a botched SQLMaker
421 172 my $sql_maker = $self->sql_maker;
422  
423 # these are throw away results, do not pollute the bind stack
424 172 local $sql_maker->{where_bind};
425 172 local $sql_maker->{group_bind};
426 172 local $sql_maker->{having_bind};
427 172 local $sql_maker->{from_bind};
428  
429 # we can't scan properly without any quoting (\b doesn't cut it
430 # everywhere), so unless there is proper quoting set - use our
431 # own weird impossible character.
432 # Also in the case of no quoting, we need to explicitly disable
433 # name_sep, otherwise sorry nasty legacy syntax like
434 # { 'count(foo.id)' => { '>' => 3 } } will stop working >:(
435 172 local $sql_maker->{quote_char} = $sql_maker->{quote_char};
436 172 local $sql_maker->{name_sep} = $sql_maker->{name_sep};
437  
438 172 ▶ unless (defined $sql_maker->{quote_char} and length $sql_maker->{quote_char}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sql_maker->{"quote_char"} and length $sql_maker->{"quote_char"}) 1 171 172
Condition66%2/3
defined $sql_maker->{"quote_char"} and length $sql_maker->{"quote_char"}
!ll&&!rl&&r
10171
MC/DC50%1/2
defined $sql_maker->{"quote_char"} and length $sql_maker->{"quote_char"}
defined $sql_maker->{"quote_char"} length $sql_maker->{"quote_char"}
Truth table66%2/3
A and B
A= defined $sql_maker->{"quote_char"}B= length $sql_maker->{"quote_char"}
ABresult
0X0
100
111
439 1 $sql_maker->{quote_char} = ["\x00", "\xFF"];
440 # if we don't unset it we screw up retarded but unfortunately working
441 # 'MAX(foo.bar)' => { '>', 3 }
442 1 $sql_maker->{name_sep} = '';
443 }
444  
445 172 my ($lquote, $rquote, $sep) = map { quotemeta $_ } ($sql_maker->_quote_chars, $sql_maker->name_sep);
446  
447 # generate sql chunks
448 my $to_scan = {
449 restricting => [
450 ($sql_maker->_recurse_where ($attrs->{where}))[0],
451 $sql_maker->_parse_rs_attrs ({ having => $attrs->{having} }),
452 ],
453 grouping => [
454 $sql_maker->_parse_rs_attrs ({ group_by => $attrs->{group_by} }),
455 ],
456 joining => [
457 $sql_maker->_recurse_from (
458 ref $attrs->{from}[0] eq 'ARRAY' ? $attrs->{from}[0][0] : $attrs->{from}[0],
459 172 @{$attrs->{from}}[1 .. $#{$attrs->{from}}],
460 ),
461 ],
462 selecting => [
463 840 map { ($sql_maker->_recurse_fields($_))[0] } @{$attrs->{select}},
464 ],
465 ordering => [
466 172 ▶ map { $_->[0] } $self->_extract_order_criteria ($attrs->{order_by}, $sql_maker),
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs->{'from'}[0] eq 'ARRAY' ? : 19 153 172
467 ],
468 };
469  
470 # throw away empty chunks and all 2-value arrayrefs: the thinking is that these are
471 # bind value specs left in by the sloppy renderer above. It is ok to do this
472 # at this point, since we are going to end up rewriting this crap anyway
473 172 for my $v (values %$to_scan) {
474 860 my @nv;
475 860 for (@$v) {
476 next if (
477 1609 ▶ ! defined $_
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ or ref $_ eq "ARRAY" and @$_ == 0 || @$_ == 2 66 1543 1609
Condition33%3/9
not defined $_ or ref $_ eq "ARRAY" and @$_ == 0 || @$_ == 2
l!l&&r!l&&!r
6601543
ref $_ eq "ARRAY" and @$_ == 0 || @$_ == 2
!ll&&!rl&&r
154300
@$_ == 0 or @$_ == 2
l!l&&r!l&&!r
000
MC/DC25%1/4
not defined $_ or ref $_ eq "ARRAY" and @$_ == 0 || @$_ == 2
not defined $_ ref $_ eq "ARRAY" @$_ == 0 @$_ == 2
Truth table40%2/5
not(A) or B and C or D
A= not defined $_B= ref $_ eq "ARRAY"C= @$_ == 0D= @$_ == 2
ABCDresult
1XXX1
011X1
01011
00XX0
01000
478 or
479 (
480 ref $_ eq 'ARRAY'
481 and
482 ( @$_ == 0 or @$_ == 2 )
483 )
484 );
485  
486 1543 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) 0 1543 1543
487 0 $self->throw_exception("Unexpected " . ref($_) . " reference in scan-plan");
488 }
489  
490 1543 push @nv, $_;
491 }
492  
493 860 $v = \@nv;
494 }
495  
496 # kill all selectors which look like a proper subquery
497 # this is a sucky heuristic *BUT* - if we get it wrong the query will simply
498 # fail to run, so we are relatively safe
499 $to_scan->{selecting} = [ grep {
500 840 $_ !~ / \A \s* \( \s* SELECT \s+ .+? \s+ FROM \s+ .+? \) \s* \z /xsi
501 172 ▶ } @{ $to_scan->{selecting} || [] } ];
Condition50%1/2
$to_scan->{'selecting'} || []
l!l
1720
MC/DC0%0/1
$to_scan->{'selecting'} || []
$to_scan->{'selecting'}
Truth table50%1/2
A
A= $to_scan->{'selecting'}
Aresult
11
00
502  
503 # first see if we have any exact matches (qualified or unqualified)
504 172 for my $type (keys %$to_scan) {
505 860 for my $piece (@{$to_scan->{$type}}) {
506 1531 ▶ if ($colinfo->{$piece} and my $alias = $colinfo->{$piece}{-source_alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colinfo->{$piece} and my $alias = $colinfo->{$piece}{"-source_alias"}) 53 1478 1531
Condition66%2/3
$colinfo->{$piece} and my $alias = $colinfo->{$piece}{"-source_alias"}
!ll&&!rl&&r
1478053
MC/DC50%1/2
$colinfo->{$piece} and my $alias = $colinfo->{$piece}{"-source_alias"}
$colinfo->{$piece} my $alias = $colinfo->{$piece}{"-source_alias"}
Truth table66%2/3
A and B
A= $colinfo->{$piece}B= my $alias = $colinfo->{$piece}{"-source_alias"}
ABresult
0X0
100
111
507 53 ▶ $aliases_by_type->{$type}{$alias} ||= { -parents => $alias_list->{$alias}{-join_path}||[] };
Condition75%3/4
$aliases_by_type->{$type}{$alias} ||= {"-parents", $alias_list->{$alias}{"-join_path"} || []}
l!l
053
$alias_list->{$alias}{'-join_path'} || []
l!l
2528
MC/DC0%0/1
$aliases_by_type->{$type}{$alias} ||= {"-parents", $alias_list->{$alias}{"-join_path"} || []}
$aliases_by_type->{$type}{$alias}
MC/DC100%1/1
$alias_list->{$alias}{'-join_path'} || []
$alias_list->{$alias}{'-join_path'}
Truth table50%1/2
A
A= $aliases_by_type->{$type}{$alias}
Aresult
11
00
Truth table100%2/2
A
A= $alias_list->{$alias}{'-join_path'}
Aresult
11
00
508 53 $aliases_by_type->{$type}{$alias}{-seen_columns}{$colinfo->{$piece}{-fq_colname}} = $piece;
509 }
510 }
511 }
512  
513 # now loop through all fully qualified columns and get the corresponding
514 # alias (should work even if they are in scalarrefs)
515 172 for my $alias (keys %$alias_list) {
516 448 my $al_re = qr/
517 $lquote $alias $rquote $sep (?: $lquote ([^$rquote]+) $rquote )?
518 |
519 \b $alias \. ([^\s\)\($rquote]+)?
520 /x;
521  
522 448 for my $type (keys %$to_scan) {
523 2240 for my $piece (@{$to_scan->{$type}}) {
524 3817 ▶ if (my @matches = $piece =~ /$al_re/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@matches) = $piece =~ /$al_re/g) 1351 2466 3817
525 1351 ▶ $aliases_by_type->{$type}{$alias} ||= { -parents => $alias_list->{$alias}{-join_path}||[] };
Condition100%4/4
$aliases_by_type->{$type}{$alias} ||= {"-parents", $alias_list->{$alias}{"-join_path"} || []}
l!l
591760
$alias_list->{$alias}{'-join_path'} || []
l!l
439321
MC/DC100%1/1
$aliases_by_type->{$type}{$alias} ||= {"-parents", $alias_list->{$alias}{"-join_path"} || []}
$aliases_by_type->{$type}{$alias}
MC/DC100%1/1
$alias_list->{$alias}{'-join_path'} || []
$alias_list->{$alias}{'-join_path'}
Truth table100%2/2
A
A= $aliases_by_type->{$type}{$alias}
Aresult
11
00
Truth table100%2/2
A
A= $alias_list->{$alias}{'-join_path'}
Aresult
11
00
526 $aliases_by_type->{$type}{$alias}{-seen_columns}{"$alias.$_"} = "$alias.$_"
527 1351 for grep { defined $_ } @matches;
528 }
529 }
530 }
531 }
532  
533 # now loop through unqualified column names, and try to locate them within
534 # the chunks
535 172 for my $col (keys %$colinfo) {
536 3779 ▶ next if $col =~ / \. /x; # if column is qualified it was caught by the above
Branch: 1 branch
BranchTrueFalseTotal
if $col =~ / \. /x 2077 1702 3779
537  
538 1702 my $col_re = qr/ $lquote ($col) $rquote /x;
539  
540 1702 for my $type (keys %$to_scan) {
541 8510 for my $piece (@{$to_scan->{$type}}) {
542 14795 ▶ if ( my @matches = $piece =~ /$col_re/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@matches) = $piece =~ /$col_re/g) 1280 13515 14795
543 1280 my $alias = $colinfo->{$col}{-source_alias};
544 1280 ▶ $aliases_by_type->{$type}{$alias} ||= { -parents => $alias_list->{$alias}{-join_path}||[] };
Condition100%4/4
$aliases_by_type->{$type}{$alias} ||= {"-parents", $alias_list->{$alias}{"-join_path"} || []}
l!l
124139
$alias_list->{$alias}{'-join_path'} || []
l!l
1227
MC/DC100%1/1
$aliases_by_type->{$type}{$alias} ||= {"-parents", $alias_list->{$alias}{"-join_path"} || []}
$aliases_by_type->{$type}{$alias}
MC/DC100%1/1
$alias_list->{$alias}{'-join_path'} || []
$alias_list->{$alias}{'-join_path'}
Truth table100%2/2
A
A= $aliases_by_type->{$type}{$alias}
Aresult
11
00
Truth table100%2/2
A
A= $alias_list->{$alias}{'-join_path'}
Aresult
11
00
545 $aliases_by_type->{$type}{$alias}{-seen_columns}{"$alias.$_"} = $_
546 1280 for grep { defined $_ } @matches;
547 }
548 }
549 }
550 }
551  
552 # Add any non-left joins to the restriction list (such joins are indeed restrictions)
553 172 for my $j (values %$alias_list) {
554 448 ▶ my $alias = $j->{-alias} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $alias = $j->{"-alias"} or next 448 0 448
555 $aliases_by_type->{restricting}{$alias} ||= { -parents => $j->{-join_path}||[] } if (
556 (not $j->{-join_type})
557 or
558 448 ▶ ($j->{-join_type} !~ /^left (?: \s+ outer)? $/xi)
Branch: 1 branch
BranchTrueFalseTotal
unless $j->{"-join_type"} and $j->{"-join_type"} =~ /^left (?: \s+ outer)? $/xi 281 167 448
Condition85%6/7
$j->{"-join_type"} and $j->{"-join_type"} =~ /^left (?: \s+ outer)? $/xi
!ll&&!rl&&r
2810167
$aliases_by_type->{"restricting"}{$alias} ||= {"-parents", $j->{"-join_path"} || []}
l!l
103178
$j->{'-join_path'} || []
l!l
9979
MC/DC50%1/2
$j->{"-join_type"} and $j->{"-join_type"} =~ /^left (?: \s+ outer)? $/xi
$j->{"-join_type"} $j->{"-join_type"} =~ /^left (?: \s+ outer)? $/xi
MC/DC100%1/1
$aliases_by_type->{"restricting"}{$alias} ||= {"-parents", $j->{"-join_path"} || []}
$aliases_by_type->{"restricting"}{$alias}
MC/DC100%1/1
$j->{'-join_path'} || []
$j->{'-join_path'}
Truth table66%2/3
A and B
A= $j->{"-join_type"}B= $j->{"-join_type"} =~ /^left (?: \s+ outer)? $/xi
ABresult
0X0
100
111
Truth table100%2/2
A
A= $aliases_by_type->{"restricting"}{$alias}
Aresult
11
00
Truth table100%2/2
A
A= $j->{'-join_path'}
Aresult
11
00
559 );
560 }
561  
562 172 for (keys %$aliases_by_type) {
563 697 ▶ delete $aliases_by_type->{$_} unless keys %{$aliases_by_type->{$_}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$aliases_by_type{$_};} 2 695 697
564 }
565  
566 172 return $aliases_by_type;
567 }
568  
569 # This is the engine behind { distinct => 1 } and the general
570 # complex prefetch grouper
571  
572 40 ▶sub _group_over_selection {
Subroutine _group_over_selection: called CC=30 SCAR=34.7
573 40 my ($self, $attrs) = @_;
574  
575 40 my $colinfos = $self->_resolve_column_info ($attrs->{from});
576  
577 40 my (@group_by, %group_index);
578  
579 # the logic is: if it is a { func => val } we assume an aggregate,
580 # otherwise if \'...' or \[...] we assume the user knows what is
581 # going on thus group over it
582 40 for (@{$attrs->{select}}) {
583 108 ▶ if (! ref($_) or ref ($_) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_ or ref $_ ne "HASH") 102 6 108
Condition100%3/3
not ref $_ or ref $_ ne "HASH"
l!l&&r!l&&!r
91116
MC/DC100%2/2
not ref $_ or ref $_ ne "HASH"
not ref $_ ref $_ ne "HASH"
Truth table100%3/3
not(A) or B
A= not ref $_B= ref $_ ne "HASH"
ABresult
1X1
011
000
584 102 push @group_by, $_;
585 102 $group_index{$_}++;
586 102 ▶ if ($colinfos->{$_} and $_ !~ /\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colinfos->{$_} and not $_ =~ /\./) 0 102 102
Condition66%2/3
$colinfos->{$_} and not $_ =~ /\./
!ll&&!rl&&r
11910
MC/DC0%0/2
$colinfos->{$_} and not $_ =~ /\./
$colinfos->{$_} not $_ =~ /\./
Truth table66%2/3
A and not(B)
A= $colinfos->{$_}B= not $_ =~ /\./
ABresult
0X0
100
111
587 # add a fully qualified version as well
588 0 $group_index{"$colinfos->{$_}{-source_alias}.$_"}++;
589 }
590 }
591 }
592  
593 my @order_by = $self->_extract_order_criteria($attrs->{order_by})
594 40 ▶ or return (\@group_by, $attrs->{order_by});
Branch: 1 branch
BranchTrueFalseTotal
my(@order_by) = $self->_extract_order_criteria($attrs->{"order_by"}) or return \@group_by, $attrs->{"order_by"} 31 9 40
595  
596 # add any order_by parts that are not already present in the group_by
597 # to maintain SQL cross-compatibility and general sanity
598 #
599 # also in case the original selection is *not* unique, or in case part
600 # of the ORDER BY refers to a multiplier - we will need to replace the
601 # skipped order_by elements with their MIN/MAX equivalents as to maintain
602 # the proper overall order without polluting the group criteria (and
603 # possibly changing the outcome entirely)
604  
605 31 my ($leftovers, $sql_maker, @new_order_by, $order_chunks, $aliastypes);
606  
607 31 my $group_already_unique = $self->_columns_comprise_identifying_set($colinfos, \@group_by);
608  
609 31 for my $o_idx (0 .. $#order_by) {
610  
611 # if the chunk is already a min/max function - there is nothing left to touch
612 31 ▶ next if $order_by[$o_idx][0] =~ /^ (?: min | max ) \s* \( .+ \) $/ix;
Branch: 1 branch
BranchTrueFalseTotal
if $order_by[$o_idx][0] =~ /^ (?: min | max ) \s* \( .+ \) $/xi 0 31 31
613  
614 # only consider real columns (for functions the user got to do an explicit group_by)
615 31 my $chunk_ci;
616 31 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (@{$order_by[$o_idx];} != 1 or not $chunk_ci = $$colinfos{$order_by[$o_idx][0]} and $attrs->{"_aliastypes"}) 1 30 31
Condition66%2/3
@{$order_by[$o_idx];} != 1 or not $chunk_ci = $$colinfos{$order_by[$o_idx][0]} and $attrs->{"_aliastypes"}
l!l&&r!l&&!r
0130
MC/DC50%1/2
@{$order_by[$o_idx];} != 1 or not $chunk_ci = $$colinfos{$order_by[$o_idx][0]} and $attrs->{"_aliastypes"}
@{$order_by[$o_idx];} != 1 not $chunk_ci = $$colinfos{$order_by[$o_idx][0]} and $attrs->{"_aliastypes"}
Truth table66%2/3
A or B
A= @{$order_by[$o_idx];} != 1B= not $chunk_ci = $$colinfos{$order_by[$o_idx][0]} and $attrs->{"_aliastypes"}
ABresult
1X1
011
000
617 31 ▶ @{$order_by[$o_idx]} != 1
Condition100%3/3
not $chunk_ci = $$colinfos{$order_by[$o_idx][0]} and $attrs->{"_aliastypes"}
!ll&&!rl&&r
2821
MC/DC100%2/2
not $chunk_ci = $$colinfos{$order_by[$o_idx][0]} and $attrs->{"_aliastypes"}
not $chunk_ci = $$colinfos{$order_by[$o_idx][0]} $attrs->{"_aliastypes"}
Truth table100%3/3
not(A) and B
A= not $chunk_ci = $$colinfos{$order_by[$o_idx][0]}B= $attrs->{"_aliastypes"}
ABresult
0X0
100
111
618 or
619 # only declare an unknown *plain* identifier as "leftover" if we are called with
620 # aliastypes to examine. If there are none - we are still in _resolve_attrs, and
621 # can just assume the user knows what they want
622 ( ! ( $chunk_ci = $colinfos->{$order_by[$o_idx][0]} ) and $attrs->{_aliastypes} )
623 ) {
624 1 push @$leftovers, $order_by[$o_idx][0];
625 }
626  
627 31 ▶ next unless $chunk_ci;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk_ci 3 28 31
628  
629 # no duplication of group criteria
630 28 ▶ next if $group_index{$chunk_ci->{-fq_colname}};
Branch: 1 branch
BranchTrueFalseTotal
if $group_index{$chunk_ci->{"-fq_colname"}} 7 21 28
631  
632 $aliastypes ||= (
633 $attrs->{_aliastypes}
634 or
635 $self->_resolve_aliastypes_from_select_args({
636 from => $attrs->{from},
637 order_by => $attrs->{order_by},
638 })
639 21 ▶ ) if $group_already_unique;
Branch: 1 branch
BranchTrueFalseTotal
if $group_already_unique 10 11 21
Condition50%3/6
$aliastypes ||= $attrs->{"_aliastypes"} || $self->_resolve_aliastypes_from_select_args({"from", $attrs->{"from"}, "order_by", $attrs->{"order_by"}})
l!l&&r!l&&!r
0100
$attrs->{'_aliastypes'} || $self->_resolve_aliastypes_from_select_args({'from', $attrs->{'from'}, 'order_by', $attrs->{'order_by'}})
l!l&&r!l&&!r
460
MC/DC0%0/3
$aliastypes ||= $attrs->{"_aliastypes"} || $self->_resolve_aliastypes_from_select_args({"from", $attrs->{"from"}, "order_by", $attrs->{"order_by"}})
$aliastypes $attrs->{'_aliastypes'} $self->_resolve_aliastypes_from_select_args({'from', $attrs->{'from'}, 'order_by', $attrs->{'order_by'}})
Truth table0%0/4
A ||= B || C
A= $aliastypesB= $attrs->{'_aliastypes'}C= $self->_resolve_aliastypes_from_select_args({'from', $attrs->{'from'}, 'order_by', $attrs->{'order_by'}})
ABCresult
1XX1
01X1
0011
0000
640  
641 # check that we are not ordering by a multiplier (if a check is requested at all)
642 21 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($group_already_unique and not $aliastypes->{'multiplying'}{$chunk_ci->{'-source_alias'}} and not $aliastypes->{'premultiplied'}{$chunk_ci->{'-source_alias'}}) { } 4 17 21
Condition100%6/6
$group_already_unique and not $aliastypes->{"multiplying"}{$chunk_ci->{"-source_alias"}} and not $aliastypes->{"premultiplied"}{$chunk_ci->{"-source_alias"}}
!ll&&!rl&&r
1614
$group_already_unique and not $aliastypes->{"multiplying"}{$chunk_ci->{"-source_alias"}}
!ll&&!rl&&r
1155
MC/DC100%3/3
$group_already_unique and not $aliastypes->{"multiplying"}{$chunk_ci->{"-source_alias"}} and not $aliastypes->{"premultiplied"}{$chunk_ci->{"-source_alias"}}
$group_already_unique not $aliastypes->{"multiplying"}{$chunk_ci->{"-source_alias"}} not $aliastypes->{"premultiplied"}{$chunk_ci->{"-source_alias"}}
Truth table100%4/4
A and not(B) and not(C)
A= $group_already_uniqueB= not $aliastypes->{"multiplying"}{$chunk_ci->{"-source_alias"}}C= not $aliastypes->{"premultiplied"}{$chunk_ci->{"-source_alias"}}
ABCresult
0XX0
10X0
1100
1111
643 $group_already_unique
644 and
645 ! $aliastypes->{multiplying}{$chunk_ci->{-source_alias}}
646 and
647 ! $aliastypes->{premultiplied}{$chunk_ci->{-source_alias}}
648 ) {
649 4 push @group_by, $chunk_ci->{-fq_colname};
650 4 $group_index{$chunk_ci->{-fq_colname}}++
651 }
652 else {
653 # We need to order by external columns without adding them to the group
654 # (eiehter a non-unique selection, or a multi-external)
655 #
656 # This doesn't really make sense in SQL, however from DBICs point
657 # of view is rather valid (e.g. order the leftmost objects by whatever
658 # criteria and get the offset/rows many). There is a way around
659 # this however in SQL - we simply tae the direction of each piece
660 # of the external order and convert them to MIN(X) for ASC or MAX(X)
661 # for DESC, and group_by the root columns. The end result should be
662 # exactly what we expect
663 #
664  
665 # both populated on the first loop over $o_idx
666 17 ▶ $sql_maker ||= $self->sql_maker;
Condition33%1/3
$sql_maker ||= $self->sql_maker
l!l&&r!l&&!r
0170
MC/DC0%0/2
$sql_maker ||= $self->sql_maker
$sql_maker $self->sql_maker
Truth table33%1/3
A ||= B
A= $sql_makerB= $self->sql_maker
ABresult
1X1
011
000
667 $order_chunks ||= [
668 17 ▶ map { ref $_ eq 'ARRAY' ? $_ : [ $_ ] } $sql_maker->_order_by_chunks($attrs->{order_by})
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 4 13 17
669 17 ▶ ];
Condition50%1/2
$order_chunks ||= [map({ref $_ eq "ARRAY" ? $_ : [$_];} $sql_maker->_order_by_chunks($attrs->{"order_by"}))]
l!l
017
MC/DC0%0/1
$order_chunks ||= [map({ref $_ eq "ARRAY" ? $_ : [$_];} $sql_maker->_order_by_chunks($attrs->{"order_by"}))]
$order_chunks
Truth table50%1/2
A
A= $order_chunks
Aresult
11
00
670  
671 17 my ($chunk, $is_desc, $nulls) = $sql_maker->_split_order_chunk($order_chunks->[$o_idx][0]);
672  
673 $new_order_by[$o_idx] = \[
674 sprintf( '%s( %s )%s%s',
675 $self->_minmax_operator_for_datatype($chunk_ci->{data_type}, $is_desc),
676 $chunk,
677 ($is_desc ? ' DESC' : ''),
678 ($nulls ? " NULLS $nulls" : ''),
679 ),
680 17 ▶ @ {$order_chunks->[$o_idx]} [ 1 .. $#{$order_chunks->[$o_idx]} ]
Branch: 2 branches
BranchTrueFalseTotal
$is_desc ? : 4 13 17
$nulls ? : 0 17 17
681 ];
682 }
683 }
684  
685 $self->throw_exception ( sprintf
686 'Unable to programatically derive a required group_by from the supplied '
687 . 'order_by criteria. To proceed either add an explicit group_by, or '
688 . 'simplify your order_by to only include plain columns '
689 . '(supplied order_by: %s)',
690 31 ▶ join ', ', map { "'$_'" } @$leftovers,
Branch: 1 branch
BranchTrueFalseTotal
if $leftovers 1 30 31
691 ) if $leftovers;
692  
693 # recreate the untouched order parts
694 30 ▶ if (@new_order_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (@new_order_by) 17 13 30
695 17 ▶ $new_order_by[$_] ||= \ $order_chunks->[$_] for ( 0 .. $#$order_chunks );
Condition50%1/2
$new_order_by[$_] ||= \$order_chunks->[$_]
l!l
170
MC/DC0%0/1
$new_order_by[$_] ||= \$order_chunks->[$_]
$new_order_by[$_]
Truth table50%1/2
A
A= $new_order_by[$_]
Aresult
11
00
696 }
697  
698 return (
699 \@group_by,
700 30 ▶ (@new_order_by ? \@new_order_by : $attrs->{order_by} ), # same ref as original == unchanged
Branch: 1 branch
BranchTrueFalseTotal
@new_order_by ? : 17 13 30
701 );
702 }
703  
704  
705 17 ▶sub _minmax_operator_for_datatype {
Subroutine _minmax_operator_for_datatype: called CC=2 SCAR=6.9
706 #my ($self, $datatype, $want_max) = @_;
707  
708 17 ▶ $_[2] ? 'MAX' : 'MIN';
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 4 13 17
709 }
710  
711  
712 2542 ▶sub _resolve_ident_sources {
Subroutine _resolve_ident_sources: called CC=9 SCAR=22.5
713 2542 my ($self, $ident) = @_;
714  
715 2542 my $alias2source = {};
716  
717 # the reason this is so contrived is that $ident may be a {from}
718 # structure, specifying multiple tables to join
719 2542 ▶ if ( blessed $ident && $ident->isa("DBIO::ResultSource") ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($ident) and $ident->isa('DBIO::ResultSource')) { } 0 2542 2542
elsif (ref $ident eq 'ARRAY') { } 2541 1 2542
Condition33%1/3
builtin::blessed($ident) and $ident->isa("DBIO::ResultSource")
!ll&&!rl&&r
254200
MC/DC0%0/2
builtin::blessed($ident) and $ident->isa("DBIO::ResultSource")
builtin::blessed($ident) $ident->isa("DBIO::ResultSource")
Truth table33%1/3
A and B
A= builtin::blessed($ident)B= $ident->isa("DBIO::ResultSource")
ABresult
0X0
100
111
720 # this is compat mode for insert/update/delete which do not deal with aliases
721 0 $alias2source->{me} = $ident;
722 }
723 elsif (ref $ident eq 'ARRAY') {
724  
725 2541 for (@$ident) {
726 3061 my $tabinfo;
727 3061 ▶ if (ref $_ eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "HASH") 2523 538 3061
728 2523 $tabinfo = $_;
729 }
730 3061 ▶ if (ref $_ eq 'ARRAY' and ref $_->[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "ARRAY" and ref $_->[0] eq "HASH") 538 2523 3061
Condition66%2/3
ref $_ eq "ARRAY" and ref $_->[0] eq "HASH"
!ll&&!rl&&r
25230538
MC/DC50%1/2
ref $_ eq "ARRAY" and ref $_->[0] eq "HASH"
ref $_ eq "ARRAY" ref $_->[0] eq "HASH"
Truth table66%2/3
A and B
A= ref $_ eq "ARRAY"B= ref $_->[0] eq "HASH"
ABresult
0X0
100
111
731 538 $tabinfo = $_->[0];
732 }
733  
734 $alias2source->{$tabinfo->{-alias}} = $tabinfo->{-rsrc}
735 3061 ▶ if ($tabinfo->{-rsrc});
Branch: 1 branch
BranchTrueFalseTotal
if $tabinfo->{"-rsrc"} 3053 8 3061
736 }
737 }
738  
739 2542 return $alias2source;
740 }
741  
742 # Takes $ident, \@column_names
743 #
744 # returns { $column_name => \%column_info, ... }
745 # also note: this adds -result_source => $rsrc to the column info
746 #
747 # If no columns_names are supplied returns info about *all* columns
748 # for all sources
749  
750 2542 ▶sub _resolve_column_info {
Subroutine _resolve_column_info: called CC=17 SCAR=29.1
751 2542 my ($self, $ident, $colnames) = @_;
752  
753 2542 ▶ return {} if $colnames and ! @$colnames;
Branch: 1 branch
BranchTrueFalseTotal
if $colnames and not @$colnames 0 2542 2542
Condition66%2/3
$colnames and not @$colnames
!ll&&!rl&&r
253930
MC/DC0%0/2
$colnames and not @$colnames
$colnames not @$colnames
Truth table66%2/3
A and not(B)
A= $colnamesB= not @$colnames
ABresult
0X0
100
111
754  
755 2542 my $alias2src = $self->_resolve_ident_sources($ident);
756  
757 2542 my (%seen_cols, @auto_colnames);
758  
759 # compile a global list of column names, to be able to properly
760 # disambiguate unqualified column names (if at all possible)
761 2542 for my $alias (keys %$alias2src) {
762 3053 my $rsrc = $alias2src->{$alias};
763 3053 for my $colname ($rsrc->columns) {
764 10738 push @{$seen_cols{$colname}}, $alias;
765 10738 ▶ push @auto_colnames, "$alias.$colname" unless $colnames;
Branch: 1 branch
BranchTrueFalseTotal
unless $colnames 10720 18 10738
766 }
767 }
768  
769 $colnames ||= [
770 @auto_colnames,
771 2542 ▶ grep { @{$seen_cols{$_}} == 1 } (keys %seen_cols),
Condition100%2/2
$colnames ||= [@auto_colnames, grep({@{$seen_cols{$_};} == 1;} keys %seen_cols)]
l!l
32539
MC/DC100%1/1
$colnames ||= [@auto_colnames, grep({@{$seen_cols{$_};} == 1;} keys %seen_cols)]
$colnames
Truth table100%2/2
A
A= $colnames
Aresult
11
00
772 ];
773  
774 2542 my (%return, $colinfos);
775 2542 foreach my $col (@$colnames) {
776 20775 my ($source_alias, $colname) = $col =~ m/^ (?: ([^\.]+) \. )? (.+) $/x;
777  
778 # if the column was seen exactly once - we know which rsrc it came from
779 $source_alias ||= $seen_cols{$colname}[0]
780 20775 ▶ if ($seen_cols{$colname} and @{$seen_cols{$colname}} == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $seen_cols{$colname} and @{$seen_cols{$colname};} == 1 20104 671 20775
Condition66%4/6
$seen_cols{$colname} and @{$seen_cols{$colname};} == 1
!ll&&!rl&&r
067120104
$source_alias ||= $seen_cols{$colname}[0]
l!l&&r!l&&!r
10055100490
MC/DC50%1/2
$seen_cols{$colname} and @{$seen_cols{$colname};} == 1
$seen_cols{$colname} @{$seen_cols{$colname};} == 1
MC/DC0%0/2
$source_alias ||= $seen_cols{$colname}[0]
$source_alias $seen_cols{$colname}[0]
Truth table66%2/3
A and B
A= $seen_cols{$colname}B= @{$seen_cols{$colname};} == 1
ABresult
0X0
100
111
Truth table66%2/3
A ||= B
A= $source_aliasB= $seen_cols{$colname}[0]
ABresult
1X1
011
000
781  
782 20775 ▶ next unless $source_alias;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_alias 0 20775 20775
783  
784 20775 ▶ my $rsrc = $alias2src->{$source_alias}
Branch: 1 branch
BranchTrueFalseTotal
my $rsrc = $alias2src->{$source_alias} or next 20775 0 20775
785 or next;
786  
787 $return{$col} = {
788 %{
789 20775 ▶ ( $colinfos->{$source_alias} ||= $rsrc->columns_info )->{$colname}
Branch: 1 branch
BranchTrueFalseTotal
$col eq $colname ? : 10049 10726 20775
Condition50%3/6
+($colinfos->{$source_alias} ||= $rsrc->columns_info)->{$colname} || $self->throw_exception("No such column '${colname}' on source " . $rsrc->source_name)
l!l&&r!l&&!r
2077500
$colinfos->{$source_alias} ||= $rsrc->columns_info
l!l&&r!l&&!r
1772230530
MC/DC0%0/2
+($colinfos->{$source_alias} ||= $rsrc->columns_info)->{$colname} || $self->throw_exception("No such column '${colname}' on source " . $rsrc->source_name)
+($colinfos->{$source_alias} ||= $rsrc->columns_info)->{$colname} $self->throw_exception("No such column '${colname}' on source " . $rsrc->source_name)
MC/DC0%0/2
$colinfos->{$source_alias} ||= $rsrc->columns_info
$colinfos->{$source_alias} $rsrc->columns_info
Truth table33%1/3
A || B
A= +($colinfos->{$source_alias} ||= $rsrc->columns_info)->{$colname}B= $self->throw_exception("No such column '${colname}' on source " . $rsrc->source_name)
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $colinfos->{$source_alias}B= $rsrc->columns_info
ABresult
1X1
011
000
790 ||
791 $self->throw_exception(
792 "No such column '$colname' on source " . $rsrc->source_name
793 );
794 },
795 -result_source => $rsrc,
796 -source_alias => $source_alias,
797 -fq_colname => $col eq $colname ? "$source_alias.$col" : $col,
798 -colname => $colname,
799 };
800  
801 20775 ▶ $return{"$source_alias.$colname"} = $return{$col} if $col eq $colname;
Branch: 1 branch
BranchTrueFalseTotal
if $col eq $colname 10049 10726 20775
802 }
803  
804 2542 return \%return;
805 }
806  
807 # The DBIO relationship chaining implementation is pretty simple - every
808 # new related_relationship is pushed onto the {from} stack, and the {select}
809 # window simply slides further in. This means that when we count somewhere
810 # in the middle, we got to make sure that everything in the join chain is an
811 # actual inner join, otherwise the count will come back with unpredictable
812 # results (a resultset may be generated with _some_ rows regardless of if
813 # the relation which the $rs currently selects has rows or not). E.g.
814 # $artist_rs->cds->count - normally generates:
815 # SELECT COUNT( * ) FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid
816 # which actually returns the number of artists * (number of cds || 1)
817 #
818 # So what we do here is crawl {from}, determine if the current alias is at
819 # the top of the stack, and if not - make sure the chain is inner-joined down
820 # to the root.
821 #
822  
823 18 ▶sub _inner_join_to_node {
Subroutine _inner_join_to_node: called CC=5 SCAR=16.1
824 18 my ($self, $from, $alias) = @_;
825  
826 18 my $switch_branch = $self->_find_join_path_to_node($from, $alias);
827  
828 18 ▶ return $from unless @{$switch_branch||[]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$switch_branch or [];} 0 18 18
Condition50%1/2
$switch_branch || []
l!l
180
MC/DC0%0/1
$switch_branch || []
$switch_branch
Truth table50%1/2
A
A= $switch_branch
Aresult
11
00
829  
830 # So it looks like we will have to switch some stuff around.
831 # local() is useless here as we will be leaving the scope
832 # anyway, and deep cloning is just too fucking expensive
833 # So replace the first hashref in the node arrayref manually
834 18 my @new_from = ($from->[0]);
835 18 my $sw_idx = { map { (values %$_), 1 } @$switch_branch }; #there's one k/v per join-path
836  
837 18 for my $j (@{$from}[1 .. $#$from]) {
838 35 my $jalias = $j->[0]{-alias};
839  
840 35 ▶ if ($sw_idx->{$jalias}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sw_idx->{$jalias}) { } 25 10 35
841 25 my %attrs = %{$j->[0]};
842 25 delete $attrs{-join_type};
843 push @new_from, [
844 \%attrs,
845 25 @{$j}[ 1 .. $#$j ],
846 ];
847 }
848 else {
849 10 push @new_from, $j;
850 }
851 }
852  
853 18 return \@new_from;
854 }
855  
856  
857 27 ▶sub _find_join_path_to_node {
Subroutine _find_join_path_to_node: called CC=8 SCAR=23.7
858 27 my ($self, $from, $target_alias) = @_;
859  
860 # subqueries and other oddness are naturally not supported
861 return undef if (
862 ref $from ne 'ARRAY'
863 ||
864 ref $from->[0] ne 'HASH'
865 ||
866 ! defined $from->[0]{-alias}
867 27 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if ref $from ne "ARRAY" or ref $from->[0] ne "HASH" or not defined $from->[0]{"-alias"} 0 27 27
Condition33%2/6
ref $from ne "ARRAY" or ref $from->[0] ne "HASH" or not defined $from->[0]{"-alias"}
l!l&&r!l&&!r
0027
ref $from ne "ARRAY" or ref $from->[0] ne "HASH"
l!l&&r!l&&!r
0027
MC/DC0%0/3
ref $from ne "ARRAY" or ref $from->[0] ne "HASH" or not defined $from->[0]{"-alias"}
ref $from ne "ARRAY" ref $from->[0] ne "HASH" not defined $from->[0]{"-alias"}
Truth table25%1/4
A or B or not(C)
A= ref $from ne "ARRAY"B= ref $from->[0] ne "HASH"C= not defined $from->[0]{"-alias"}
ABCresult
1XX1
01X1
0011
0000
868  
869 # no path - the head is the alias
870 27 ▶ return [] if $from->[0]{-alias} eq $target_alias;
Branch: 1 branch
BranchTrueFalseTotal
if $from->[0]{"-alias"} eq $target_alias 9 18 27
871  
872 18 for my $i (1 .. $#$from) {
873 33 ▶ return $from->[$i][0]{-join_path} if ( ($from->[$i][0]{-alias}||'') eq $target_alias );
Branch: 1 branch
BranchTrueFalseTotal
if ($from->[$i][0]{"-alias"} || "") eq $target_alias 18 15 33
Condition50%1/2
$from->[$i][0]{'-alias'} || ''
l!l
330
MC/DC0%0/1
$from->[$i][0]{'-alias'} || ''
$from->[$i][0]{'-alias'}
Truth table50%1/2
A
A= $from->[$i][0]{'-alias'}
Aresult
11
00
874 }
875  
876 # something else went quite wrong
877 0 return undef;
878 }
879  
880  
881 497 ▶sub _extract_order_criteria {
Subroutine _extract_order_criteria: called CC=2 SCAR=6.9
882 497 my ($self, $order_by, $sql_maker) = @_;
883  
884 496 ▶ my $parser = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
885 496 my ($sql_maker, $order_by, $orig_quote_chars) = @_;
886  
887 496 ▶ return scalar $sql_maker->_order_by_chunks ($order_by)
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 14 482 496
888 unless wantarray;
889  
890 482 ▶ my ($lq, $rq, $sep) = map { quotemeta($_) } (
Branch: 1 branch
BranchTrueFalseTotal
$orig_quote_chars ? : 310 172 482
891 ($orig_quote_chars ? @$orig_quote_chars : $sql_maker->_quote_chars),
892 $sql_maker->name_sep
893 );
894  
895 482 my @chunks;
896 482 for ($sql_maker->_order_by_chunks ($order_by) ) {
897 150 ▶ my $chunk = ref $_ ? [ @$_ ] : [ $_ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 51 99 150
898 150 ($chunk->[0]) = $sql_maker->_split_order_chunk($chunk->[0]);
899  
900 # order criteria may have come back pre-quoted (literals and whatnot)
901 # this is fragile, but the best we can currently do
902 150 ▶ $chunk->[0] =~ s/^ $lq (.+?) $rq $sep $lq (.+?) $rq $/"$1.$2"/xe
Branch: 1 branch
BranchTrueFalseTotal
$chunk->[0] =~ s/^ $lq (.+?) $rq $sep $lq (.+?) $rq $/"$1.$2";/ex or $chunk->[0] =~ s/^ $lq (.+) $rq $/$1/x 49 101 150
903 or $chunk->[0] =~ s/^ $lq (.+) $rq $/$1/x;
904  
905 150 push @chunks, $chunk;
906 }
907  
908 482 return @chunks;
909 497 };
910  
911 497 ▶ if ($sql_maker) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql_maker) { } 172 325 497
912 172 return $parser->($sql_maker, $order_by);
913 }
914 else {
915 325 $sql_maker = $self->sql_maker;
916  
917 # pass these in to deal with literals coming from
918 # the user or the deep guts of prefetch
919 324 my $orig_quote_chars = [$sql_maker->_quote_chars];
920  
921 324 local $sql_maker->{quote_char};
922 324 return $parser->($sql_maker, $order_by, $orig_quote_chars);
923 }
924 }
925  
926  
927 3 ▶sub _order_by_is_stable {
Subroutine _order_by_is_stable: called CC=4 SCAR=15.2
928 3 my ($self, $ident, $order_by, $where) = @_;
929  
930 my @cols = (
931 6 ▶ ( map { $_->[0] } $self->_extract_order_criteria($order_by) ),
Branch: 1 branch
BranchTrueFalseTotal
$where ? : 0 3 3
932 0 ▶ ( $where ? keys %{ $self->_extract_fixed_condition_columns($where) } : () ),
Branch: 1 branch
BranchTrueFalseTotal
my(@cols) = (map({$_->[0];} $self->_extract_order_criteria($order_by)), $where ? keys %{$self->_extract_fixed_condition_columns($where);} : ()) or return 0 3 0 3
933 ) or return 0;
934  
935 3 my $colinfo = $self->_resolve_column_info($ident, \@cols);
936  
937 3 ▶ return keys %$colinfo
Branch: 1 branch
BranchTrueFalseTotal
keys %$colinfo ? : 3 0 3
938 ? $self->_columns_comprise_identifying_set( $colinfo, \@cols )
939 : 0
940 ;
941 }
942  
943  
944 34 ▶sub _columns_comprise_identifying_set {
Subroutine _columns_comprise_identifying_set: called CC=4 SCAR=13.9
945 34 my ($self, $colinfo, $columns) = @_;
946  
947 34 my $cols_per_src;
948 $cols_per_src -> {$_->{-source_alias}} -> {$_->{-colname}} = $_
949 34 for grep { defined $_ } @{$colinfo}{@$columns};
950  
951 34 for (values %$cols_per_src) {
952 27 my $src = (values %$_)[0]->{-result_source};
953 27 ▶ return 1 if $src->_identifying_column_set($_);
Branch: 1 branch
BranchTrueFalseTotal
if $src->_identifying_column_set($_) 18 9 27
954 }
955  
956 16 return 0;
957 }
958  
959 # this is almost similar to _order_by_is_stable, except it takes
960 # a single rsrc, and will succeed only if the first portion of the order
961 # by is stable.
962 # returns that portion as a colinfo hashref on success
963  
964 9 ▶sub _extract_colinfo_of_stable_main_source_order_by_portion {
Subroutine _extract_colinfo_of_stable_main_source_order_by_portion: called CC=12 SCAR=29.9
965 9 my ($self, $attrs) = @_;
966  
967 9 my $nodes = $self->_find_join_path_to_node($attrs->{from}, $attrs->{alias});
968  
969 9 ▶ return unless defined $nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nodes 0 9 9
970  
971 my @ord_cols = map
972 17 { $_->[0] }
973 9 ( $self->_extract_order_criteria($attrs->{order_by}) )
974 ;
975 9 ▶ return unless @ord_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @ord_cols 0 9 9
976  
977 9 my $valid_aliases = { map { $_ => 1 } (
978 $attrs->{from}[0]{-alias},
979 0 map { values %$_ } @$nodes,
980 ) };
981  
982 9 my $colinfos = $self->_resolve_column_info($attrs->{from});
983  
984 9 my ($colinfos_to_return, $seen_main_src_cols);
985  
986 9 for my $col (@ord_cols) {
987 # if order criteria is unresolvable - there is nothing we can do
988 17 ▶ my $colinfo = $colinfos->{$col} or last;
Branch: 1 branch
BranchTrueFalseTotal
my $colinfo = $colinfos->{$col} or last 17 0 17
989  
990 # if we reached the end of the allowed aliases - also nothing we can do
991 17 ▶ last unless $valid_aliases->{$colinfo->{-source_alias}};
Branch: 1 branch
BranchTrueFalseTotal
unless $$valid_aliases{$colinfo->{"-source_alias"}} 0 17 17
992  
993 17 $colinfos_to_return->{$col} = $colinfo;
994  
995 $seen_main_src_cols->{$colinfo->{-colname}} = 1
996 17 ▶ if $colinfo->{-source_alias} eq $attrs->{alias};
Branch: 1 branch
BranchTrueFalseTotal
if $colinfo->{"-source_alias"} eq $attrs->{"alias"} 17 0 17
997 }
998  
999 # FIXME the condition may be singling out things on its own, so we
1000 # conceivable could come back wi "stable-ordered by nothing"
1001 # not confient enough in the parser yet, so punt for the time being
1002 9 ▶ return unless $seen_main_src_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_main_src_cols 0 9 9
1003  
1004 my $main_src_fixed_cols_from_cond = [ $attrs->{where}
1005 ? (
1006 map
1007 {
1008 ( $colinfos->{$_} and $colinfos->{$_}{-source_alias} eq $attrs->{alias} )
1009 ? $colinfos->{$_}{-colname}
1010 : ()
1011 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$colinfos->{$_} && $colinfos->{$_}{'-source_alias'} eq $attrs->{'alias'} ? : 0 0 0
Condition0%0/3
$colinfos->{$_} and $colinfos->{$_}{"-source_alias"} eq $attrs->{"alias"}
!ll&&!rl&&r
000
MC/DC0%0/2
$colinfos->{$_} and $colinfos->{$_}{"-source_alias"} eq $attrs->{"alias"}
$colinfos->{$_} $colinfos->{$_}{"-source_alias"} eq $attrs->{"alias"}
Truth table0%0/3
A and B
A= $colinfos->{$_}B= $colinfos->{$_}{"-source_alias"} eq $attrs->{"alias"}
ABresult
0X0
100
111
1012 0 ▶ keys %{ $self->_extract_fixed_condition_columns($attrs->{where}) }
Branch: 1 branch
BranchTrueFalseTotal
$attrs->{'where'} ? : 0 9 9
1013 )
1014 : ()
1015 ];
1016  
1017 return $attrs->{result_source}->_identifying_column_set([
1018 9 ▶ keys %$seen_main_src_cols,
Branch: 1 branch
BranchTrueFalseTotal
$attrs->{'result_source'}->_identifying_column_set([keys %$seen_main_src_cols, @$main_src_fixed_cols_from_cond]) ? : 9 0 9
1019 @$main_src_fixed_cols_from_cond,
1020 ]) ? $colinfos_to_return : ();
1021 }
1022  
1023 # Attempts to flatten a passed in SQLAC condition as much as possible towards
1024 # a plain hashref, *without* altering its semantics. Required by
1025 # create/populate being able to extract definitive conditions from preexisting
1026 # resultset {where} stacks
1027 #
1028 # FIXME - while relatively robust, this is still imperfect, one of the first
1029 # things to tackle when we get access to a formalized AST. Note that this code
1030 # is covered by a *ridiculous* amount of tests, so starting with porting this
1031 # code would be a rather good exercise
1032 # A collapsed condition is FALSE when it is exactly { -or => [] }; a condition
1033 # that collapsed to nothing is TRUE. The same truth rule as the SQLMaker
1034 # (ADR 0069): a FALSE part restricts, a TRUE part drops out.
1035 25628 ▶sub _is_false_cond {
Subroutine _is_false_cond: called CC=5 SCAR=16.1
1036 25628 my ($c) = @_;
1037 return (
1038 ref $c eq 'HASH' and keys %$c == 1
1039 25628 ▶ and ref $c->{-or} eq 'ARRAY' and ! @{ $c->{-or} }
Branch: 1 branch
BranchTrueFalseTotal
ref $c eq 'HASH' && keys %$c == 1 && ref $c->{'-or'} eq 'ARRAY' && !@{$c->{'-or'};} ? : 564 25064 25628
Condition88%8/9
ref $c eq "HASH" and keys %$c == 1 and ref $c->{"-or"} eq "ARRAY" and not @{$c->{"-or"};}
!ll&&!rl&&r
236701394564
ref $c eq "HASH" and keys %$c == 1 and ref $c->{"-or"} eq "ARRAY"
!ll&&!rl&&r
795228751958
ref $c eq "HASH" and keys %$c == 1
!ll&&!rl&&r
079524833
MC/DC75%3/4
ref $c eq "HASH" and keys %$c == 1 and ref $c->{"-or"} eq "ARRAY" and not @{$c->{"-or"};}
ref $c eq "HASH" keys %$c == 1 ref $c->{"-or"} eq "ARRAY" not @{$c->{"-or"};}
Truth table80%4/5
A and B and C and not(D)
A= ref $c eq "HASH"B= keys %$c == 1C= ref $c->{"-or"} eq "ARRAY"D= not @{$c->{"-or"};}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1040 ) ? 1 : 0;
1041 }
1042  
1043  
1044 23526 ▶sub _collapse_cond {
Subroutine _collapse_cond: called CC=74 SCAR=43.7
1045 23526 my ($self, $where, $where_is_anded_array) = @_;
1046  
1047 23526 my $fin;
1048  
1049 23526 ▶ if (! $where) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $where) { } 176 23350 23526
elsif ($where_is_anded_array or ref $where eq 'HASH') { } 20115 3235 23350
elsif (ref $where eq 'ARRAY') { } 2977 258 3235
1050 176 return;
1051 }
1052 ▶ elsif ($where_is_anded_array or ref $where eq 'HASH') {
Condition100%3/3
$where_is_anded_array or ref $where eq "HASH"
l!l&&r!l&&!r
762193533235
MC/DC100%2/2
$where_is_anded_array or ref $where eq "HASH"
$where_is_anded_array ref $where eq "HASH"
Truth table100%3/3
A or B
A= $where_is_anded_arrayB= ref $where eq "HASH"
ABresult
1X1
011
000
1053  
1054 20115 my @pairs;
1055  
1056 20115 ▶ my @pieces = $where_is_anded_array ? @$where : $where;
Branch: 1 branch
BranchTrueFalseTotal
$where_is_anded_array ? : 762 19353 20115
1057 20115 ▶ while (@pieces) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pieces) 20880 20037 40917
1058 20880 my $chunk = shift @pieces;
1059  
1060 20880 ▶ if (ref $chunk eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $chunk eq 'HASH') { } 20121 759 20880
elsif (ref $chunk eq 'ARRAY') { } 165 594 759
elsif (not length ref $chunk) { } 431 163 594
1061 20121 for (sort keys %$chunk) {
1062  
1063 # Match SQLAC 1.79 behavior
1064 21343 ▶ if ($_ eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "") 128 21215 21343
1065 128 ▶ is_literal_value($chunk->{$_})
Branch: 1 branch
BranchTrueFalseTotal
&is_literal_value($chunk->{$_}) ? : 60 68 128
1066 ? carp 'Hash-pairs consisting of an empty string with a literal are deprecated, use -and => [ $literal ] instead'
1067 : $self->throw_exception("Supplying an empty left hand side argument is not supported in hash-pairs")
1068 ;
1069 }
1070  
1071 21275 push @pairs, $_ => $chunk->{$_};
1072 }
1073 }
1074 elsif (ref $chunk eq 'ARRAY') {
1075 # [] is FALSE and stays (ADR 0069)
1076 165 push @pairs, -or => $chunk;
1077 }
1078 elsif ( ! length ref $chunk) {
1079  
1080 # Match SQLAC 1.79 behavior
1081 431 ▶ $self->throw_exception("Supplying an empty left hand side argument is not supported in array-pairs")
Branch: 1 branch
BranchTrueFalseTotal
if $where_is_anded_array and !defined($chunk) || $chunk eq "" 10 421 431
Condition83%5/6
$where_is_anded_array and !defined($chunk) || $chunk eq ""
!ll&&!rl&&r
042110
not defined $chunk or $chunk eq ""
l!l&&r!l&&!r
55421
MC/DC66%2/3
$where_is_anded_array and !defined($chunk) || $chunk eq ""
$where_is_anded_array not defined $chunk $chunk eq ""
Truth table75%3/4
A and not(B) or C
A= $where_is_anded_arrayB= not defined $chunkC= $chunk eq ""
ABCresult
0XX0
1000
11X1
1011
1082 if $where_is_anded_array and (! defined $chunk or $chunk eq '');
1083  
1084 421 push @pairs, $chunk, shift @pieces;
1085 }
1086 else {
1087 163 push @pairs, '', $chunk;
1088 }
1089 }
1090  
1091 20037 ▶ return unless @pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 262 19775 20037
1092  
1093 19775 ▶ my @conds = $self->_collapse_cond_unroll_pairs(\@pairs)
Branch: 1 branch
BranchTrueFalseTotal
my(@conds) = $self->_collapse_cond_unroll_pairs(\@pairs) or return 19187 250 19437
1094 or return;
1095  
1096 # Consolidate various @conds back into something more compact
1097 19187 my $has_false;
1098 19187 for my $c (@conds) {
1099 # FALSE once is FALSE
1100 21515 ▶ if (_is_false_cond($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_false_cond($c)) 266 21249 21515
1101 266 ▶ next if $has_false++;
Branch: 1 branch
BranchTrueFalseTotal
if $has_false++ 12 254 266
1102 }
1103 21503 ▶ if (ref $c ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c ne 'HASH') { } 0 21503 21503
1104 0 push @{$fin->{-and}}, $c;
1105 }
1106 else {
1107 21503 for my $col (sort keys %$c) {
1108  
1109 # consolidate all -and nodes
1110 22414 ▶ if ($col =~ /^\-and$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($col =~ /^\-and$/i) { } 553 21861 22414
elsif ($col =~ /^\-/) { } 1562 20299 21861
elsif (exists $fin->{$col}) { } 396 19903 20299
1111 553 ▶ push @{$fin->{-and}},
Branch: 1 branch
BranchTrueFalseTotal
ref $c->{$col} eq 'ARRAY' ? : 553 0 553
1112 553 ▶ ref $c->{$col} eq 'ARRAY' ? @{$c->{$col}}
Branch: 1 branch
BranchTrueFalseTotal
ref $c->{$col} eq 'HASH' ? : 0 0 0
1113 0 : ref $c->{$col} eq 'HASH' ? %{$c->{$col}}
1114 553 : { $col => $c->{$col} }
1115 ;
1116 }
1117 elsif ($col =~ /^\-/) {
1118 1562 push @{$fin->{-and}}, { $col => $c->{$col} };
1119 }
1120 elsif (exists $fin->{$col}) {
1121 $fin->{$col} = [ -and => map {
1122 (ref $_ eq 'ARRAY' and ($_->[0]||'') =~ /^\-and$/i )
1123 792 ▶ ? @{$_}[1..$#$_]
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' && ($_->[0] || '') =~ /^\-and$/i ? : 93 699 792
Condition100%5/5
ref $_ eq "ARRAY" and ($_->[0] || "") =~ /^\-and$/i
!ll&&!rl&&r
6811893
$_->[0] || ''
l!l
9318
MC/DC100%2/2
ref $_ eq "ARRAY" and ($_->[0] || "") =~ /^\-and$/i
ref $_ eq "ARRAY" $_->[0]
Truth table100%3/3
A and B
A= ref $_ eq "ARRAY"B= $_->[0]
ABresult
0X0
100
111
1124 : $_
1125 ;
1126 396 } ($fin->{$col}, $c->{$col}) ];
1127 }
1128 else {
1129 19903 $fin->{$col} = $c->{$col};
1130 }
1131 }
1132 }
1133 }
1134 }
1135 elsif (ref $where eq 'ARRAY') {
1136 # we are always at top-level here, it is safe to dump empty *standalone* pieces
1137 2977 my $fin_idx;
1138 my $has_true;
1139  
1140 2977 ▶ for (my $i = 0; $i <= $#$where; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $where->$#*) 4862 2643 7505
1141  
1142 # Match SQLAC 1.79 behavior
1143 4862 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $where->[$i] and length $where->[$i] 156 4706 4862
Condition100%3/3
defined $where->[$i] and length $where->[$i]
!ll&&!rl&&r
68884706
MC/DC100%2/2
defined $where->[$i] and length $where->[$i]
defined $where->[$i] length $where->[$i]
Truth table100%3/3
A and B
A= defined $where->[$i]B= length $where->[$i]
ABresult
0X0
100
111
1144 "Supplying an empty left hand side argument is not supported in array-pairs"
1145 ) if (! defined $where->[$i] or ! length $where->[$i]);
1146  
1147 4706 ▶ my $logic_mod = lc ( ($where->[$i] =~ /^(\-(?:and|or))$/i)[0] || '' );
Condition100%2/2
($where->[$i] =~ /^(\-(?:and|or))$/i)[0] || ''
l!l
10703636
MC/DC100%1/1
($where->[$i] =~ /^(\-(?:and|or))$/i)[0] || ''
($where->[$i] =~ /^(\-(?:and|or))$/i)[0]
Truth table100%2/2
A
A= ($where->[$i] =~ /^(\-(?:and|or))$/i)[0]
Aresult
11
00
1148  
1149 4706 ▶ if ($logic_mod) {
Branch: 2 branches
BranchTrueFalseTotal
if ($logic_mod) { } 1070 3636 4706
elsif (not length ref $where->[$i]) { } 2875 761 3636
1150 1070 $i++;
1151 1070 ▶ $self->throw_exception("Unsupported top-level op/arg pair: [ $logic_mod => $where->[$i] ]")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $where->[$i] eq "HASH" or ref $where->[$i] eq "ARRAY" 0 1070 1070
Condition66%2/3
ref $where->[$i] eq "HASH" or ref $where->[$i] eq "ARRAY"
l!l&&r!l&&!r
7992710
MC/DC0%0/2
ref $where->[$i] eq "HASH" or ref $where->[$i] eq "ARRAY"
ref $where->[$i] eq "HASH" ref $where->[$i] eq "ARRAY"
Truth table66%2/3
A or B
A= ref $where->[$i] eq "HASH"B= ref $where->[$i] eq "ARRAY"
ABresult
1X1
011
000
1152 unless ref $where->[$i] eq 'HASH' or ref $where->[$i] eq 'ARRAY';
1153  
1154 1070 my $sub_elt = $self->_collapse_cond({ $logic_mod => $where->[$i] });
1155 # nothing left is TRUE: it makes the or TRUE; FALSE drops out
1156 920 ▶ if (! $sub_elt) { $has_true = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub_elt) 99 821 920
1157 821 ▶ next if _is_false_cond($sub_elt);
Branch: 1 branch
BranchTrueFalseTotal
if _is_false_cond($sub_elt) 40 781 821
1158  
1159 781 my @keys = keys %$sub_elt;
1160 781 ▶ if ( @keys == 1 and $keys[0] !~ /^\-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys == 1 and not $keys[0] =~ /^\-/) { } 316 465 781
Condition100%3/3
@keys == 1 and not $keys[0] =~ /^\-/
!ll&&!rl&&r
194271316
MC/DC100%2/2
@keys == 1 and not $keys[0] =~ /^\-/
@keys == 1 not $keys[0] =~ /^\-/
Truth table100%3/3
A and not(B)
A= @keys == 1B= not $keys[0] =~ /^\-/
ABresult
0X0
100
111
1161 316 $fin_idx->{ "COL_$keys[0]_" . serialize $sub_elt } = $sub_elt;
1162 }
1163 else {
1164 465 $fin_idx->{ "SER_" . serialize $sub_elt } = $sub_elt;
1165 }
1166 }
1167 elsif (! length ref $where->[$i] ) {
1168 2875 my $sub_elt = $self->_collapse_cond({ @{$where}[$i, $i+1] });
1169 2875 ▶ if (! $sub_elt) { $has_true = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub_elt) 1 2874 2875
1170 2874 ▶ next if _is_false_cond($sub_elt);
Branch: 1 branch
BranchTrueFalseTotal
if _is_false_cond($sub_elt) 0 2874 2874
1171  
1172 2874 $fin_idx->{ "COL_$where->[$i]_" . serialize $sub_elt } = $sub_elt;
1173 2874 $i++;
1174 }
1175 else {
1176 761 my $sub_elt = $self->_collapse_cond( $where->[$i] );
1177 733 ▶ if (! $sub_elt) { $has_true = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub_elt) 315 418 733
1178 418 ▶ next if _is_false_cond($sub_elt);
Branch: 1 branch
BranchTrueFalseTotal
if _is_false_cond($sub_elt) 258 160 418
1179 160 $fin_idx->{ "SER_" . serialize $where->[$i] } = $sub_elt;
1180 }
1181 }
1182  
1183 2643 ▶ if ($has_true) {
Branch: 3 branches
BranchTrueFalseTotal
if ($has_true) { } 360 2283 2643
elsif (not $fin_idx) { } 492 1791 2283
elsif (keys %$fin_idx == 1) { } 801 990 1791
1184 360 return;
1185 }
1186 elsif (! $fin_idx) {
1187 # an or of nothing (or of FALSE only) is FALSE
1188 492 return { -or => [] };
1189 }
1190 elsif ( keys %$fin_idx == 1 ) {
1191 801 $fin = (values %$fin_idx)[0];
1192 }
1193 else {
1194 990 my @or;
1195  
1196 # at this point everything is at most one level deep - unroll if needed
1197 990 for (sort keys %$fin_idx) {
1198 2860 ▶ if ( ref $fin_idx->{$_} eq 'HASH' and keys %{$fin_idx->{$_}} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fin_idx->{$_} eq 'HASH' and keys %{$$fin_idx{$_};} == 1) { } 2846 14 2860
Condition66%2/3
ref $fin_idx->{$_} eq "HASH" and keys %{$$fin_idx{$_};} == 1
!ll&&!rl&&r
0142846
MC/DC50%1/2
ref $fin_idx->{$_} eq "HASH" and keys %{$$fin_idx{$_};} == 1
ref $fin_idx->{$_} eq "HASH" keys %{$$fin_idx{$_};} == 1
Truth table66%2/3
A and B
A= ref $fin_idx->{$_} eq "HASH"B= keys %{$$fin_idx{$_};} == 1
ABresult
0X0
100
111
1199 2846 my ($l, $r) = %{$fin_idx->{$_}};
1200  
1201 2846 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (ref $r eq 'ARRAY' and @$r == 1 && $l =~ /^\-and$/i || $l =~ /^\-or$/i) { } 28 2818 2846
elsif (ref $r eq 'HASH' and keys %$r == 1 and $l =~ /^\-(?:and|or)$/i) { } 0 2818 2818
Condition44%4/9
ref $r eq "ARRAY" and @$r == 1 && $l =~ /^\-and$/i || $l =~ /^\-or$/i
!ll&&!rl&&r
2818028
@$r == 1 and $l =~ /^\-and$/i or $l =~ /^\-or$/i
l!l&&r!l&&!r
2800
@$r == 1 and $l =~ /^\-and$/i
!ll&&!rl&&r
0028
MC/DC25%1/4
ref $r eq "ARRAY" and @$r == 1 && $l =~ /^\-and$/i || $l =~ /^\-or$/i
ref $r eq "ARRAY" @$r == 1 $l =~ /^\-and$/i $l =~ /^\-or$/i
Truth table33%2/6
A and B and C or D
A= ref $r eq "ARRAY"B= @$r == 1C= $l =~ /^\-and$/iD= $l =~ /^\-or$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1202 ref $r eq 'ARRAY'
1203 and
1204 (
1205 ( @$r == 1 and $l =~ /^\-and$/i )
1206 or
1207 $l =~ /^\-or$/i
1208 )
1209 ) {
1210 28 push @or, @$r
1211 }
1212  
1213 ▶ elsif (
Condition66%4/6
ref $r eq "HASH" and keys %$r == 1 and $l =~ /^\-(?:and|or)$/i
!ll&&!rl&&r
22156030
ref $r eq "HASH" and keys %$r == 1
!ll&&!rl&&r
22150603
MC/DC0%0/3
ref $r eq "HASH" and keys %$r == 1 and $l =~ /^\-(?:and|or)$/i
ref $r eq "HASH" keys %$r == 1 $l =~ /^\-(?:and|or)$/i
Truth table50%2/4
A and B and C
A= ref $r eq "HASH"B= keys %$r == 1C= $l =~ /^\-(?:and|or)$/i
ABCresult
0XX0
10X0
1100
1111
1214 ref $r eq 'HASH'
1215 and
1216 keys %$r == 1
1217 and
1218 $l =~ /^\-(?:and|or)$/i
1219 ) {
1220 0 push @or, %$r;
1221 }
1222  
1223 else {
1224 2818 push @or, $l, $r;
1225 }
1226 }
1227 else {
1228 14 push @or, $fin_idx->{$_};
1229 }
1230 }
1231  
1232 990 $fin->{-or} = \@or;
1233 }
1234 }
1235 else {
1236 # not a hash not an array
1237 258 $fin = { -and => [ $where ] };
1238 }
1239  
1240 # unroll single-element -and's
1241 21236 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($fin->{"-and"} and @{$fin->{"-and"};} < 2) 1961 20595 22556
Condition100%3/3
$fin->{"-and"} and @{$fin->{"-and"};} < 2
!ll&&!rl&&r
202943011961
MC/DC100%2/2
$fin->{"-and"} and @{$fin->{"-and"};} < 2
$fin->{"-and"} @{$fin->{"-and"};} < 2
Truth table100%3/3
A and B
A= $fin->{"-and"}B= @{$fin->{"-and"};} < 2
ABresult
0X0
100
111
1242 $fin->{-and}
1243 and
1244 2262 @{$fin->{-and}} < 2
1245 ) {
1246 1961 my $and = delete $fin->{-and};
1247 1961 ▶ last if @$and == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$and == 0 0 1961 1961
1248  
1249 # at this point we have @$and == 1
1250 1961 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $and->[0] eq 'HASH' and not grep({exists $fin->{$_};} keys %{$$and[0];})) { } 1320 641 1961
Condition66%2/3
ref $and->[0] eq "HASH" and not grep({exists $fin->{$_};} keys %{$$and[0];})
!ll&&!rl&&r
64101320
MC/DC50%1/2
ref $and->[0] eq "HASH" and not grep({exists $fin->{$_};} keys %{$$and[0];})
ref $and->[0] eq "HASH" not grep({exists $fin->{$_};} keys %{$$and[0];})
Truth table66%2/3
A and not(B)
A= ref $and->[0] eq "HASH"B= not grep({exists $fin->{$_};} keys %{$$and[0];})
ABresult
0X0
100
111
1251 ref $and->[0] eq 'HASH'
1252 and
1253 1320 ! grep { exists $fin->{$_} } keys %{$and->[0]}
1254 ) {
1255 $fin = {
1256 1320 %$fin, %{$and->[0]}
1257 };
1258 }
1259 else {
1260 641 $fin->{-and} = $and;
1261 641 last;
1262 }
1263 }
1264  
1265 # compress same-column conds found in $fin
1266 21236 for my $col ( grep { $_ !~ /^\-/ } keys %$fin ) {
1267 20704 ▶ next unless ref $fin->{$col} eq 'ARRAY' and ($fin->{$col}[0]||'') =~ /^\-and$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fin->{$col} eq "ARRAY" and ($fin->{$col}[0] || "") =~ /^\-and$/i 20140 564 20704
Condition100%5/5
ref $fin->{$col} eq "ARRAY" and ($fin->{$col}[0] || "") =~ /^\-and$/i
!ll&&!rl&&r
2005387564
$fin->{$col}[0] || ''
l!l
58764
MC/DC100%2/2
ref $fin->{$col} eq "ARRAY" and ($fin->{$col}[0] || "") =~ /^\-and$/i
ref $fin->{$col} eq "ARRAY" $fin->{$col}[0]
Truth table100%3/3
A and B
A= ref $fin->{$col} eq "ARRAY"B= $fin->{$col}[0]
ABresult
0X0
100
111
1268 my $val_bag = { map {
1269 1267 ▶ (! defined $_ ) ? ( UNDEF => undef )
Branch: 2 branches
BranchTrueFalseTotal
!defined($_) ? : 175 1092 1267
!length(ref $_) || &is_plain_value($_) ? : 301 791 1092
Condition66%2/3
not length ref $_ or &is_plain_value($_)
l!l&&r!l&&!r
3010791
MC/DC50%1/2
not length ref $_ or &is_plain_value($_)
not length ref $_ &is_plain_value($_)
Truth table66%2/3
not(A) or B
A= not length ref $_B= &is_plain_value($_)
ABresult
1X1
011
000
1270 : ( ! length ref $_ or is_plain_value $_ ) ? ( "VAL_$_" => $_ )
1271 : ( ( 'SER_' . serialize $_ ) => $_ )
1272 564 } @{$fin->{$col}}[1 .. $#{$fin->{$col}}] };
1273  
1274 564 ▶ if (keys %$val_bag == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$val_bag == 1) { } 26 538 564
1275 26 ($fin->{$col}) = values %$val_bag;
1276 }
1277 else {
1278 538 $fin->{$col} = [ -and => map { $val_bag->{$_} } sort keys %$val_bag ];
1279 }
1280 }
1281  
1282 21236 ▶ return keys %$fin ? $fin : ();
Branch: 1 branch
BranchTrueFalseTotal
keys %$fin ? : 21236 0 21236
1283 }
1284  
1285  
1286 20167 ▶sub _collapse_cond_unroll_pairs {
Subroutine _collapse_cond_unroll_pairs: called CC=45 SCAR=38.7
1287 20167 my ($self, $pairs) = @_;
1288  
1289 20167 my @conds;
1290  
1291 20167 ▶ while (@$pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pairs) 22719 19829 42548
1292 22719 my ($lhs, $rhs) = splice @$pairs, 0, 2;
1293  
1294 22719 ▶ if ($lhs eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if ($lhs eq '') { } 223 22496 22719
elsif ($lhs =~ /^\-and$/i) { } 1507 20989 22496
elsif ($lhs =~ /^\-or$/i) { } 1940 19049 20989
1295 223 push @conds, $self->_collapse_cond($rhs);
1296 }
1297 elsif ( $lhs =~ /^\-and$/i ) {
1298 1507 push @conds, $self->_collapse_cond($rhs, (ref $rhs eq 'ARRAY'));
1299 }
1300 elsif ( $lhs =~ /^\-or$/i ) {
1301 push @conds, $self->_collapse_cond(
1302 1940 ▶ (ref $rhs eq 'HASH') ? [ map { $_ => $rhs->{$_} } sort keys %$rhs ] : $rhs
Branch: 1 branch
BranchTrueFalseTotal
ref $rhs eq 'HASH' ? : 305 1635 1940
1303 );
1304 }
1305 else {
1306 19049 ▶ if (ref $rhs eq 'HASH' and ! keys %$rhs) {
Branch: 6 branches
BranchTrueFalseTotal
if (ref $rhs eq 'HASH' and not keys %$rhs) { } 6 19043 19049
elsif (ref $rhs eq 'HASH' and keys %$rhs == 1 and exists $rhs->{'-ident'}) { } 285 18758 19043
elsif (ref $rhs eq 'HASH' and keys %$rhs == 1 and exists $rhs->{'-value'} and !length(ref $rhs->{'-value'}) || defined builtin::blessed($rhs->{'-value'}) && &is_plain_value($rhs->{'-value'})) { } 33 18725 18758
elsif (ref $rhs eq 'HASH' and keys %$rhs == 1 and exists $rhs->{'='}) { } 851 17874 18725
elsif (ref $rhs eq 'ARRAY') { } 268 17606 17874
elsif (ref $rhs eq 'HASH' and (my($subop) = keys %$rhs) == 1 and ref((values %$rhs)[0]) eq 'HASH' and keys %{(values %$rhs)[0];} == 1 and exists +(values %$rhs)[0]->{'-value'} and !length(ref +(values %$rhs)[0]->{'-value'}) || defined builtin::blessed(+(values %$rhs)[0]->{'-value'}) && &is_plain_value(+(values %$rhs)[0]->{'-value'})) { } 7 17599 17606
Condition100%3/3
ref $rhs eq "HASH" and not keys %$rhs
!ll&&!rl&&r
1721118326
MC/DC100%2/2
ref $rhs eq "HASH" and not keys %$rhs
ref $rhs eq "HASH" not keys %$rhs
Truth table100%3/3
A and not(B)
A= ref $rhs eq "HASH"B= not keys %$rhs
ABresult
0X0
100
111
1307 # FIXME - SQLAC seems to be doing... nothing...?
1308 }
1309 # normalize top level -ident, for saner extract_fixed_condition_columns code
1310 ▶ elsif (ref $rhs eq 'HASH' and keys %$rhs == 1 and exists $rhs->{-ident}) {
Condition83%5/6
ref $rhs eq "HASH" and keys %$rhs == 1 and exists $rhs->{"-ident"}
!ll&&!rl&&r
172111547285
ref $rhs eq "HASH" and keys %$rhs == 1
!ll&&!rl&&r
1721101832
MC/DC66%2/3
ref $rhs eq "HASH" and keys %$rhs == 1 and exists $rhs->{"-ident"}
ref $rhs eq "HASH" keys %$rhs == 1 exists $rhs->{"-ident"}
Truth table75%3/4
A and B and C
A= ref $rhs eq "HASH"B= keys %$rhs == 1C= exists $rhs->{"-ident"}
ABCresult
0XX0
10X0
1100
1111
1311 285 push @conds, { $lhs => { '=', $rhs } };
1312 }
1313 # can't simply use is_plain_value result, as we need to
1314 # preserve the -value marker where necessary (non-blessed ref)
1315 ▶ elsif (
Condition73%11/15
ref $rhs eq "HASH" and keys %$rhs == 1 and exists $rhs->{"-value"} and !length(ref $rhs->{"-value"}) || defined builtin::blessed($rhs->{"-value"}) && &is_plain_value($rhs->{"-value"})
!ll&&!rl&&r
187071833
ref $rhs eq "HASH" and keys %$rhs == 1 and exists $rhs->{"-value"}
!ll&&!rl&&r
17211149651
ref $rhs eq "HASH" and keys %$rhs == 1
!ll&&!rl&&r
1721101547
not length ref $rhs->{"-value"} or defined builtin::blessed($rhs->{"-value"}) and &is_plain_value($rhs->{"-value"})
l!l&&r!l&&!r
33018
defined builtin::blessed($rhs->{"-value"}) and &is_plain_value($rhs->{"-value"})
!ll&&!rl&&r
1800
MC/DC50%3/6
ref $rhs eq "HASH" and keys %$rhs == 1 and exists $rhs->{"-value"} and !length(ref $rhs->{"-value"}) || defined builtin::blessed($rhs->{"-value"}) && &is_plain_value($rhs->{"-value"})
ref $rhs eq "HASH" keys %$rhs == 1 exists $rhs->{"-value"} not length ref $rhs->{"-value"} defined builtin::blessed($rhs->{"-value"}) &is_plain_value($rhs->{"-value"})
Truth table57%4/7
A and B and C and not(D) or E and F
A= ref $rhs eq "HASH"B= keys %$rhs == 1C= exists $rhs->{"-value"}D= not length ref $rhs->{"-value"}E= defined builtin::blessed($rhs->{"-value"})F= &is_plain_value($rhs->{"-value"})
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
11100X0
1110100
1111XX1
1110111
1316 ref $rhs eq 'HASH'
1317 and
1318 keys %$rhs == 1
1319 and
1320 exists $rhs->{-value}
1321 and
1322 (
1323 ! length ref( $rhs->{-value} )
1324 or
1325 (
1326 defined( blessed $rhs->{-value} )
1327 and
1328 is_plain_value $rhs->{-value}
1329 )
1330 )
1331 ) {
1332 33 push @conds, { $lhs => $rhs->{-value} };
1333 }
1334 ▶ elsif (ref $rhs eq 'HASH' and keys %$rhs == 1 and exists $rhs->{'='}) {
Condition83%5/6
ref $rhs eq "HASH" and keys %$rhs == 1 and exists $rhs->{"="}
!ll&&!rl&&r
17211663851
ref $rhs eq "HASH" and keys %$rhs == 1
!ll&&!rl&&r
1721101514
MC/DC66%2/3
ref $rhs eq "HASH" and keys %$rhs == 1 and exists $rhs->{"="}
ref $rhs eq "HASH" keys %$rhs == 1 exists $rhs->{"="}
Truth table75%3/4
A and B and C
A= ref $rhs eq "HASH"B= keys %$rhs == 1C= exists $rhs->{"="}
ABCresult
0XX0
10X0
1100
1111
1335 851 ▶ if ( length ref $rhs->{'='} and is_literal_value $rhs->{'='} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $rhs->{'='} and &is_literal_value($rhs->{'='})) { } 459 392 851
Condition100%3/3
length ref $rhs->{"="} and &is_literal_value($rhs->{"="})
!ll&&!rl&&r
98294459
MC/DC100%2/2
length ref $rhs->{"="} and &is_literal_value($rhs->{"="})
length ref $rhs->{"="} &is_literal_value($rhs->{"="})
Truth table100%3/3
A and B
A= length ref $rhs->{"="}B= &is_literal_value($rhs->{"="})
ABresult
0X0
100
111
1336 459 push @conds, { $lhs => $rhs };
1337 }
1338 else {
1339 392 for my $p ($self->_collapse_cond_unroll_pairs([ $lhs => $rhs->{'='} ])) {
1340  
1341 # extra sanity check
1342 397 ▶ if (keys %$p > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$p > 1) 0 397 397
1343 0 $self->throw_exception(
1344 "Internal error: unexpected collapse unroll for '$lhs' into "
1345 . scalar(keys %$p) . ' pairs'
1346 );
1347 }
1348  
1349 397 my ($l, $r) = %$p;
1350  
1351 push @conds, (
1352 ! length ref $r
1353 or
1354 # the unroller recursion may return a '=' prepended value already
1355 397 ▶ ref $r eq 'HASH' and keys %$rhs == 1 and exists $rhs->{'='}
Branch: 1 branch
BranchTrueFalseTotal
!length(ref $r) || ref $r eq 'HASH' && keys %$rhs == 1 && exists $rhs->{'='} || &is_plain_value($r) ? : 377 20 397
Condition83%10/12
not length ref $r or ref $r eq "HASH" and keys %$rhs == 1 and exists $rhs->{"="} or &is_plain_value($r)
l!l&&r!l&&!r
3651220
not length ref $r or ref $r eq "HASH" and keys %$rhs == 1 and exists $rhs->{"="}
l!l&&r!l&&!r
12623932
ref $r eq "HASH" and keys %$rhs == 1 and exists $rhs->{"="}
!ll&&!rl&&r
320239
ref $r eq "HASH" and keys %$rhs == 1
!ll&&!rl&&r
320239
MC/DC60%3/5
not length ref $r or ref $r eq "HASH" and keys %$rhs == 1 and exists $rhs->{"="} or &is_plain_value($r)
not length ref $r ref $r eq "HASH" keys %$rhs == 1 exists $rhs->{"="} &is_plain_value($r)
Truth table50%4/8
not(A) or B and C and D or E
A= not length ref $rB= ref $r eq "HASH"C= keys %$rhs == 1D= exists $rhs->{"="}E= &is_plain_value($r)
ABCDEresult
1XXXX1
0111X1
00XX11
010X11
011011
00XX00
010X00
011000
1356 or
1357 is_plain_value($r)
1358 )
1359 ? { $l => $r }
1360 : { $l => { '=' => $r } }
1361 ;
1362 }
1363 }
1364 }
1365 elsif (ref $rhs eq 'ARRAY') {
1366 # some of these conditionals encounter multi-values - roll them out using
1367 # an unshift, which will cause extra looping in the while{} above
1368 268 ▶ if (! @$rhs ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not @$rhs) { } 60 208 268
elsif (($rhs->[0] || '') =~ /^\-(?:and|or)$/i) { } 172 36 208
elsif (@$rhs == 1) { } 15 21 36
1369 60 push @conds, { $lhs => [] };
1370 }
1371 ▶ elsif ( ($rhs->[0]||'') =~ /^\-(?:and|or)$/i ) {
Condition100%2/2
$rhs->[0] || ''
l!l
2035
MC/DC100%1/1
$rhs->[0] || ''
$rhs->[0]
Truth table100%2/2
A
A= $rhs->[0]
Aresult
11
00
1372 172 ▶ $self->throw_exception("Value modifier not followed by any values: $lhs => [ $rhs->[0] ] ")
Branch: 1 branch
BranchTrueFalseTotal
if @$rhs == 1 0 172 172
1373 if @$rhs == 1;
1374  
1375 172 ▶ if( $rhs->[0] =~ /^\-and$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rhs->[0] =~ /^\-and$/i) { } 162 10 172
elsif (@$rhs == 2) { } 5 5 10
1376 162 unshift @$pairs, map { $lhs => $_ } @{$rhs}[1..$#$rhs];
1377 }
1378 # if not an AND then it's an OR
1379 elsif(@$rhs == 2) {
1380 5 unshift @$pairs, $lhs => $rhs->[1];
1381 }
1382 else {
1383 5 push @conds, { $lhs => [ @{$rhs}[1..$#$rhs] ] };
1384 }
1385 }
1386 elsif (@$rhs == 1) {
1387 15 unshift @$pairs, $lhs => $rhs->[0];
1388 }
1389 else {
1390 21 push @conds, { $lhs => $rhs };
1391 }
1392 }
1393 # unroll func + { -value => ... }
1394 # can't simply use is_plain_value result, as we need to
1395 # preserve the -value marker where necessary (non-blessed ref)
1396 ▶ elsif (
Condition86%13/15
ref $rhs eq "HASH" and (my($subop) = keys %$rhs) == 1 and ref((values %$rhs)[0]) eq "HASH" and keys %{(values %$rhs)[0];} == 1 and exists +(values %$rhs)[0]->{"-value"} and !length(ref +(values %$rhs)[0]->{"-value"}) || defined builtin::blessed(+(values %$rhs)[0]->{"-value"}) && &is_plain_value(+(values %$rhs)[0]->{"-value"})
!ll&&!rl&&r
17563367
ref $rhs eq "HASH" and (my($subop) = keys %$rhs) == 1 and ref((values %$rhs)[0]) eq "HASH" and keys %{(values %$rhs)[0];} == 1 and exists +(values %$rhs)[0]->{"-value"}
!ll&&!rl&&r
175491443
ref $rhs eq "HASH" and (my($subop) = keys %$rhs) == 1 and ref((values %$rhs)[0]) eq "HASH" and keys %{(values %$rhs)[0];} == 1
!ll&&!rl&&r
17549057
ref $rhs eq "HASH" and (my($subop) = keys %$rhs) == 1 and ref((values %$rhs)[0]) eq "HASH"
!ll&&!rl&&r
1694360657
ref $rhs eq "HASH" and (my($subop) = keys %$rhs) == 1
!ll&&!rl&&r
169430663
MC/DC66%4/6
ref $rhs eq "HASH" and (my($subop) = keys %$rhs) == 1 and ref((values %$rhs)[0]) eq "HASH" and keys %{(values %$rhs)[0];} == 1 and exists +(values %$rhs)[0]->{"-value"} and !length(ref +(values %$rhs)[0]->{"-value"}) || defined builtin::blessed(+(values %$rhs)[0]->{"-value"}) && &is_plain_value(+(values %$rhs)[0]->{"-value"})
ref $rhs eq "HASH" (my($subop) = keys %$rhs) == 1 ref((values %$rhs)[0]) eq "HASH" keys %{(values %$rhs)[0];} == 1 exists +(values %$rhs)[0]->{"-value"} !length(ref +(values %$rhs)[0]->{"-value"}) || defined builtin::blessed(+(values %$rhs)[0]->{"-value"}) && &is_plain_value(+(values %$rhs)[0]->{"-value"})
Truth table71%5/7
A and B and C and D and E and F
A= ref $rhs eq "HASH"B= (my($subop) = keys %$rhs) == 1C= ref((values %$rhs)[0]) eq "HASH"D= keys %{(values %$rhs)[0];} == 1E= exists +(values %$rhs)[0]->{"-value"}F= !length(ref +(values %$rhs)[0]->{"-value"}) || defined builtin::blessed(+(values %$rhs)[0]->{"-value"}) && &is_plain_value(+(values %$rhs)[0]->{"-value"})
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
1397 ref $rhs eq 'HASH'
1398 and
1399 ( my ($subop) = keys %$rhs ) == 1
1400 and
1401 ref( (values %$rhs)[0] ) eq 'HASH'
1402 and
1403 57 ▶ keys %{ (values %$rhs)[0] } == 1
Condition50%3/6
not length ref +(values %$rhs)[0]->{"-value"} or defined builtin::blessed(+(values %$rhs)[0]->{"-value"}) and &is_plain_value(+(values %$rhs)[0]->{"-value"})
l!l&&r!l&&!r
7036
defined builtin::blessed(+(values %$rhs)[0]->{"-value"}) and &is_plain_value(+(values %$rhs)[0]->{"-value"})
!ll&&!rl&&r
3600
MC/DC0%0/3
not length ref +(values %$rhs)[0]->{"-value"} or defined builtin::blessed(+(values %$rhs)[0]->{"-value"}) and &is_plain_value(+(values %$rhs)[0]->{"-value"})
not length ref +(values %$rhs)[0]->{"-value"} defined builtin::blessed(+(values %$rhs)[0]->{"-value"}) &is_plain_value(+(values %$rhs)[0]->{"-value"})
Truth table0%0/4
not(A) or B and C
A= not length ref +(values %$rhs)[0]->{"-value"}B= defined builtin::blessed(+(values %$rhs)[0]->{"-value"})C= &is_plain_value(+(values %$rhs)[0]->{"-value"})
ABCresult
1XX1
0111
00X0
0100
1404 and
1405 exists( (values %$rhs)[0]->{-value} )
1406 and
1407 (
1408 ! length ref( (values %$rhs)[0]->{-value} )
1409 or
1410 (
1411 defined( blessed( (values %$rhs)[0]->{-value} ) )
1412 and
1413 is_plain_value( (values %$rhs)[0]->{-value} )
1414 )
1415 )
1416 ) {
1417 7 push @conds, { $lhs => { $subop => (values %$rhs)[0]->{-value} } };
1418 }
1419 else {
1420 17599 push @conds, { $lhs => $rhs };
1421 }
1422 }
1423 }
1424  
1425 19829 return @conds;
1426 }
1427  
1428 # Analyzes a given condition and attempts to extract all columns
1429 # with a definitive fixed-condition criteria. Returns a hashref
1430 # of k/v pairs suitable to be passed to set_columns(), with a
1431 # MAJOR CAVEAT - multi-value (contradictory) equalities are still
1432 # represented as a reference to the UNRESOVABLE_CONDITION constant
1433 # The reason we do this is that some codepaths only care about the
1434 # codition being stable, as opposed to actually making sense
1435 #
1436 # The normal mode is used to figure out if a resultset is constrained
1437 # to a column which is part of a unique constraint, which in turn
1438 # allows us to better predict how ordering will behave etc.
1439 #
1440 # With the optional "consider_nulls" boolean argument, the function
1441 # is instead used to infer inambiguous values from conditions
1442 # (e.g. the inheritance of resultset conditions on new_result)
1443 #
1444  
1445 13687 ▶sub _extract_fixed_condition_columns {
Subroutine _extract_fixed_condition_columns: called CC=28 SCAR=33.6
1446 13687 my ($self, $where, $consider_nulls) = @_;
1447 13687 my $where_hash = $self->_collapse_cond($_[1]);
1448  
1449 13687 my $res = {};
1450 13687 my ($c, $v);
1451 13687 for $c (keys %$where_hash) {
1452 14433 my $vals;
1453  
1454 14433 ▶ if (!defined ($v = $where_hash->{$c}) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined($v = $where_hash->{$c})) { } 161 14272 14433
elsif (ref $v eq 'HASH' and keys %$v == 1) { } 187 14085 14272
elsif (not length ref $v or &is_plain_value($v)) { } 13266 819 14085
elsif (ref $v eq 'ARRAY' and ($v->[0] || '') eq '-and') { } 71 804 875
1455 161 ▶ $vals->{UNDEF} = $v if $consider_nulls
Branch: 1 branch
BranchTrueFalseTotal
if $consider_nulls 102 59 161
1456 }
1457 ▶ elsif (
Condition66%2/3
ref $v eq "HASH" and keys %$v == 1
!ll&&!rl&&r
140850187
MC/DC50%1/2
ref $v eq "HASH" and keys %$v == 1
ref $v eq "HASH" keys %$v == 1
Truth table66%2/3
A and B
A= ref $v eq "HASH"B= keys %$v == 1
ABresult
0X0
100
111
1458 ref $v eq 'HASH'
1459 and
1460 keys %$v == 1
1461 ) {
1462 187 ▶ if (exists $v->{-value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $v->{'-value'}) { } 12 175 187
elsif (length ref $v->{'='} and ref $v->{'='} eq 'HASH' && keys %{$$v{'='};} == 1 && exists $v->{'='}{'-ident'} || &is_literal_value($v->{'='})) { } 121 103 224
1463 12 ▶ if (defined $v->{-value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $v->{'-value'}) { } 12 0 12
elsif ($consider_nulls) { } 0 0 0
1464 $vals->{"VAL_$v->{-value}"} = $v->{-value}
1465 12 }
1466 elsif( $consider_nulls ) {
1467 0 $vals->{UNDEF} = $v->{-value};
1468 }
1469 }
1470 # do not need to check for plain values - _collapse_cond did it for us
1471 ▶ elsif(
Condition83%10/12
length ref $v->{"="} and ref $v->{"="} eq "HASH" && keys %{$$v{"="};} == 1 && exists $v->{"="}{"-ident"} || &is_literal_value($v->{"="})
!ll&&!rl&&r
495121
ref $v->{"="} eq "HASH" and keys %{$$v{"="};} == 1 and exists $v->{"="}{"-ident"} or &is_literal_value($v->{"="})
l!l&&r!l&&!r
56655
ref $v->{"="} eq "HASH" and keys %{$$v{"="};} == 1 and exists $v->{"="}{"-ident"}
!ll&&!rl&&r
70056
ref $v->{"="} eq "HASH" and keys %{$$v{"="};} == 1
!ll&&!rl&&r
70056
MC/DC60%3/5
length ref $v->{"="} and ref $v->{"="} eq "HASH" && keys %{$$v{"="};} == 1 && exists $v->{"="}{"-ident"} || &is_literal_value($v->{"="})
length ref $v->{"="} ref $v->{"="} eq "HASH" keys %{$$v{"="};} == 1 exists $v->{"="}{"-ident"} &is_literal_value($v->{"="})
Truth table50%4/8
A and B and C and D or E
A= length ref $v->{"="}B= ref $v->{"="} eq "HASH"C= keys %{$$v{"="};} == 1D= exists $v->{"="}{"-ident"}E= &is_literal_value($v->{"="})
ABCDEresult
0XXXX0
10XX00
110X00
111000
1111X1
10XX11
110X11
111011
1472 length ref $v->{'='}
1473 and
1474 (
1475 56 ( ref $v->{'='} eq 'HASH' and keys %{$v->{'='}} == 1 and exists $v->{'='}{-ident} )
1476 or
1477 is_literal_value($v->{'='})
1478 )
1479 ) {
1480 121 $vals->{ 'SER_' . serialize $v->{'='} } = $v->{'='};
1481 }
1482 }
1483 ▶ elsif (
Condition100%3/3
not length ref $v or &is_plain_value($v)
l!l&&r!l&&!r
1324422819
MC/DC100%2/2
not length ref $v or &is_plain_value($v)
not length ref $v &is_plain_value($v)
Truth table100%3/3
not(A) or B
A= not length ref $vB= &is_plain_value($v)
ABresult
1X1
011
000
1484 ! length ref $v
1485 or
1486 is_plain_value ($v)
1487 ) {
1488 13266 $vals->{"VAL_$v"} = $v;
1489 }
1490 ▶ elsif (ref $v eq 'ARRAY' and ($v->[0]||'') eq '-and') {
Condition100%5/5
ref $v eq "ARRAY" and ($v->[0] || "") eq "-and"
!ll&&!rl&&r
5669271
$v->[0] || ''
l!l
303460
MC/DC100%2/2
ref $v eq "ARRAY" and ($v->[0] || "") eq "-and"
ref $v eq "ARRAY" $v->[0]
Truth table100%3/3
A and B
A= ref $v eq "ARRAY"B= $v->[0]
ABresult
0X0
100
111
1491 71 for ( @{$v}[1..$#$v] ) {
1492 166 my $subval = $self->_extract_fixed_condition_columns({ $c => $_ }, 'consider nulls'); # always fish nulls out on recursion
1493 166 ▶ next unless exists $subval->{$c}; # didn't find anything
Branch: 1 branch
BranchTrueFalseTotal
unless exists $subval->{$c} 44 122 166
1494 $vals->{
1495 ! defined $subval->{$c} ? 'UNDEF'
1496 : ( ! length ref $subval->{$c} or is_plain_value $subval->{$c} ) ? "VAL_$subval->{$c}"
1497 : ( 'SER_' . serialize $subval->{$c} )
1498 122 ▶ } = $subval->{$c};
Branch: 2 branches
BranchTrueFalseTotal
!defined($subval->{$c}) ? : 22 100 122
!length(ref $subval->{$c}) || &is_plain_value($subval->{$c}) ? : 66 34 100
Condition66%2/3
not length ref $subval->{$c} or &is_plain_value($subval->{$c})
l!l&&r!l&&!r
66034
MC/DC50%1/2
not length ref $subval->{$c} or &is_plain_value($subval->{$c})
not length ref $subval->{$c} &is_plain_value($subval->{$c})
Truth table66%2/3
not(A) or B
A= not length ref $subval->{$c}B= &is_plain_value($subval->{$c})
ABresult
1X1
011
000
1499 }
1500 }
1501  
1502 14433 ▶ if (keys %$vals == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %$vals == 1) { } 13521 912 14433
elsif (keys %$vals > 1) { } 51 861 912
1503 ($res->{$c}) = (values %$vals)
1504 13521 ▶ unless !$consider_nulls and exists $vals->{UNDEF};
Branch: 1 branch
BranchTrueFalseTotal
unless not $consider_nulls and exists $vals->{"UNDEF"} 13516 5 13521
Condition100%3/3
not $consider_nulls and exists $vals->{"UNDEF"}
!ll&&!rl&&r
966938475
MC/DC100%2/2
not $consider_nulls and exists $vals->{"UNDEF"}
not $consider_nulls exists $vals->{"UNDEF"}
Truth table100%3/3
not(A) and B
A= not $consider_nullsB= exists $vals->{"UNDEF"}
ABresult
0X0
100
111
1505 }
1506 elsif (keys %$vals > 1) {
1507 51 $res->{$c} = UNRESOLVABLE_CONDITION;
1508 }
1509 }
1510  
1511 13687 $res;
1512 }
1513  
1514 344 1;
1515  
1516 __END__
« blib/lib/DBIO/Storage/DBI/DataTypeClassifier.pm ↑ Summary blib/lib/DBIO/Optional/Dependencies.pm »