Query/Plan/Join/NestedLoop.pm

filter: Statementstmt 73.0% 103 / 141 Branchbran 39.4% 15 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.2% 16 / 19 Podpod 100% 6 / 6 totaltotal 68.6% 140 / 204 CC 20 SCAR 19.3
CC
20
Cov
66%
close
17.6
next
10.2
plan_node_name
6.0
CRAP 6.9
« Query/Algebra/Move.pm ↑ Summary Query/Algebra/GroupGraphPattern.pm »
1 # RDF::Query::Plan::Join::NestedLoop
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Plan::Join::NestedLoop - Executable query plan for nested loop joins.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Plan::Join::NestedLoop version 2.919.
11  
12 =head1 METHODS
13  
14 Beyond the methods documented below, this class inherits methods from the
15 L<RDF::Query::Plan::Join> class.
16  
17 =over 4
18  
19 =cut
20  
21 package RDF::Query::Plan::Join::NestedLoop;
22  
23 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 35 ▶use base qw(RDF::Query::Plan::Join);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 35 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 35 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 35 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 35 ▶use RDF::Query::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 35 ▶use RDF::Query::ExecutionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 ######################################################################
35  
36 35 our ($VERSION);
37 35 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
38 35 $VERSION = '2.919';
39 35 $RDF::Query::Plan::Join::JOIN_CLASSES{ 'RDF::Query::Plan::Join::NestedLoop' }++;
40 }
41  
42 ######################################################################
43  
44 =item C<< new ( $lhs, $rhs, $opt, [ \%logging_keys ] ) >>
45  
46 =cut
47  
48 31 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
49 31 my $class = shift;
50 31 my $lhs = shift;
51 31 my $rhs = shift;
52 31 my $opt = shift;
53 31 my $keys = shift;
54 31 ▶ if ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) 11 20 31
55 11 throw RDF::Query::Error::MethodInvocationError -text => "NestedLoop join does not support optional joins (use PushDownNestedLoop instead)";
56 }
57 20 my $self = $class->SUPER::new( $lhs, $rhs, $opt );
58
59 20 $self->[0]{logging_keys} = $keys;
60 20 return $self;
61 }
62  
63 =item C<< execute ( $execution_context ) >>
64  
65 =cut
66  
67 1 ▶sub execute ($) {
Subroutine execute: called CC=4 SCAR=14.0
Pod documented
68 1 my $self = shift;
69 1 my $context = shift;
70 1 $self->[0]{delegate} = $context->delegate;
71 1 ▶ if ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state == $self->OPEN) 0 1 1
72 0 throw RDF::Query::Error::ExecutionError -text => "NestedLoop join plan can't be executed while already open";
73 }
74
75 1 my $l = Log::Log4perl->get_logger("rdf.query.plan.join.nestedloop");
76 1 $self->[0]{start_time} = [gettimeofday];
77 # if ($self->optional) {
78 # my (@inner, @outer);
79 # $self->rhs->execute( $context );
80 # while (my $row = $self->rhs->next) {
81 # $l->trace("loading inner row: " . $row);
82 # push(@inner, $row);
83 # }
84 #
85 # my @results;
86 # $self->lhs->execute( $context );
87 # while (my $outer = $self->lhs->next) {
88 # $l->trace("loading outer row: " . $outer);
89 # my $count = 0;
90 # foreach my $inner (@inner) {
91 # if (my $joined = $inner->join( $outer )) {
92 # $count++;
93 # if ($l->is_trace) {
94 # $l->trace("joined bindings: $outer ⋈ $inner");
95 # }
96 # # warn "-> joined\n";
97 # $self->[0]{count}++;
98 # push(@results, $joined);
99 # }
100 # }
101 # if ($count == 0) {
102 # # left-join branch
103 # push(@results, $outer);
104 # }
105 # }
106 #
107 # warn Dumper(\@results);
108 #
109 # $self->[0]{results} = \@results;
110 # } else {
111 1 my @inner;
112 1 $self->rhs->execute( $context );
113 1 ▶ while (my $row = $self->rhs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->rhs->next) 2 1 3
114 2 $l->trace("loading inner row cache with: " . $row);
115 2 push(@inner, $row);
116 }
117 1 $self->lhs->execute( $context );
118 1 ▶ if ($self->lhs->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lhs->state == $self->OPEN) { } 1 0 1
119 1 $self->[0]{inner} = \@inner;
120 1 $self->[0]{outer} = $self->lhs;
121 1 $self->[0]{inner_index} = 0;
122 1 $self->[0]{needs_new_outer} = 1;
123 1 $self->[0]{inner_count} = 0;
124 1 $self->[0]{count} = 0;
125 1 $self->[0]{logger} = $context->logger;
126 1 $self->state( $self->OPEN );
127 } else {
128 0 warn "no iterator in execute()";
129 }
130 # }
131 # warn '########################################';
132 1 $self;
133 }
134  
135 =item C<< next >>
136  
137 =cut
138  
139 1 ▶sub next {
Subroutine next: called CC=8 SCAR=23.2
Pod documented
140 1 my $self = shift;
141 1 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 1 1
142 0 throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open NestedLoop join";
143 }
144
145 # if ($self->optional) {
146 # my $result = shift(@{ $self->[0]{results} });
147 # if (my $d = $self->delegate) {
148 # $d->log_result( $self, $result );
149 # }
150 # return $result;
151 # }
152
153 1 my $outer = $self->[0]{outer};
154 1 my $inner = $self->[0]{inner};
155
156 1 my $l = Log::Log4perl->get_logger("rdf.query.plan.join.nestedloop");
157 1 while (1) {
158 1 ▶ if ($self->[0]{needs_new_outer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0]{"needs_new_outer"}) 1 0 1
159 1 $self->[0]{outer_row} = $outer->next;
160 1 ▶ if (ref($self->[0]{outer_row})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->[0]{'outer_row'}) { } 1 0 1
161 1 $self->[0]{needs_new_outer} = 0;
162 1 $self->[0]{inner_index} = 0;
163 1 $self->[0]{inner_count} = 0;
164 # warn "got new outer row: " . Dumper($self->[0]{outer_row});
165 } else {
166 # we've exhausted the outer iterator. we're now done.
167 # warn "exhausted";
168 0 return undef;
169 }
170 }
171
172 1 ▶ while ($self->[0]{inner_index} < scalar(@$inner)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0]{"inner_index"} < scalar @$inner) 1 0 1
173 1 my $inner_row = $inner->[ $self->[0]{inner_index}++ ];
174 # warn "using inner row: " . Dumper($inner_row);
175 1 ▶ if (my $joined = $inner_row->join( $self->[0]{outer_row} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $joined = $inner_row->join($self->[0]{'outer_row'})) { } 1 0 1
176 1 ▶ if ($l->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_trace) 0 1 1
177 0 $l->trace("joined bindings: $inner_row ⋈ $self->[0]{outer_row}");
178 }
179 # warn "-> joined\n";
180 1 $self->[0]{inner_count}++;
181 1 $self->[0]{count}++;
182 1 ▶ if (my $d = $self->delegate) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $self->delegate) 0 1 1
183 0 $d->log_result( $self, $joined );
184 }
185 1 return $joined;
186 } else {
187 0 $l->trace("failed to join bindings: $inner_row ⋈ $self->[0]{outer_row}");
188 }
189 }
190
191 0 $self->[0]{needs_new_outer} = 1;
192 }
193 }
194  
195 =item C<< close >>
196  
197 =cut
198  
199 1 ▶sub close {
Subroutine close: called CC=7 SCAR=28.7
Pod documented
200 1 my $self = shift;
201 1 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 1 1
202 0 throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open NestedLoop join";
203 }
204
205 1 my $l = Log::Log4perl->get_logger("rdf.query.plan.join.nestedloop");
206 1 my $t0 = delete $self->[0]{start_time};
207 1 my $count = delete $self->[0]{count};
208 1 ▶ if (my $log = delete $self->[0]{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log = delete $self->[0]{"logger"}) 0 1 1
209 0 $l->debug("logging nestedloop join execution statistics");
210 0 my $elapsed = tv_interval ( $t0 );
211 0 ▶ if (my $sparql = $self->logging_keys->{sparql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sparql = $self->logging_keys->{"sparql"}) 0 0 0
212 0 ▶ if ($l->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_trace) 0 0 0
213 0 $l->trace("- SPARQL: $sparql");
214 0 $l->trace("- elapsed: $elapsed");
215 0 $l->trace("- count: $count");
216 }
217 0 $log->push_key_value( 'execute_time-nestedloop', $sparql, $elapsed );
218 0 $log->push_key_value( 'cardinality-nestedloop', $sparql, $count );
219 }
220 0 ▶ if (my $bf = $self->logging_keys->{bf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bf = $self->logging_keys->{"bf"}) 0 0 0
221 0 ▶ if ($l->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_trace) 0 0 0
222 0 $l->trace("- bf: $bf");
223 }
224 0 $log->push_key_value( 'cardinality-bf-nestedloop', $bf, $count );
225 }
226 }
227 1 delete $self->[0]{inner};
228 1 delete $self->[0]{outer};
229 1 delete $self->[0]{inner_index};
230 1 delete $self->[0]{needs_new_outer};
231 1 delete $self->[0]{inner_count};
232 1 $self->lhs->close();
233 1 $self->rhs->close();
234 1 $self->SUPER::close();
235 }
236  
237 =item C<< plan_node_name >>
238  
239 Returns the string name of this plan node, suitable for use in serialization.
240  
241 =cut
242  
243 0 ▶sub plan_node_name {
Subroutine plan_node_name: not called CC=2 SCAR=17.9
Pod documented
244 0 my $self = shift;
245 0 ▶ my $jtype = $self->optional ? 'leftjoin' : 'join';
Branch: 1 branch
BranchTrueFalseTotal
$self->optional ? : 0 0 0
246 0 return "nestedloop-$jtype";
247 }
248  
249 =item C<< graph ( $g ) >>
250  
251 =cut
252  
253 0 ▶sub graph {
Subroutine graph: not called CC=2 SCAR=17.9
Pod documented
254 0 my $self = shift;
255 0 my $g = shift;
256 0 ▶ my $jtype = $self->optional ? 'Left Join' : 'Join';
Branch: 1 branch
BranchTrueFalseTotal
$self->optional ? : 0 0 0
257 0 my ($l, $r) = map { $_->graph( $g ) } ($self->lhs, $self->rhs);
258 0 $g->add_node( "$self", label => "$jtype (NL)" . $self->graph_labels );
259 0 $g->add_edge( "$self", $l );
260 0 $g->add_edge( "$self", $r );
261 0 return "$self";
262 }
263  
264  
265 package RDF::Query::Plan::Join::NestedLoop::Left;
266  
267 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
268 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 35 ▶use base qw(RDF::Query::Plan::Join::NestedLoop);
Subroutine BEGIN: called CC=1 SCAR=0.0
270  
271 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
272 0 my $class = shift;
273 0 my $lhs = shift;
274 0 my $rhs = shift;
275 0 return $class->SUPER::new( $lhs, $rhs, 1 );
276 }
277  
278  
279 35 1;
280  
281 __END__
« Query/Algebra/Move.pm ↑ Summary Query/Algebra/GroupGraphPattern.pm »