Query/Plan.pm

filter: Statementstmt 70.1% 595 / 848 Branchbran 56.5% 182 / 322 Conditioncond 50.0% 48 / 96 MC/DCmcdc 25.0% 14 / 56 Subroutinesub 88.8% 72 / 81 Podpod 100% 16 / 16 totaltotal 65.3% 927 / 1419 CC 239 SCAR 54.4
CC
239
Cov
65%
generate_plans
577.9
__path_plan
417.2
explain
56.5
CRAP 231.1
« Query/Compiler/SQL.pm ↑ Summary Query/Node/Literal.pm »
1 # RDF::Query::Plan
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Plan - Executable query plan nodes.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Plan version 2.919.
11  
12 =head1 METHODS
13  
14 =over 4
15  
16 =cut
17  
18 package RDF::Query::Plan;
19  
20 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 35 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 35 ▶use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 35 ▶use Scalar::Util qw(blessed reftype refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 35 ▶use RDF::Query::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 35 ▶use RDF::Query::BGPOptimizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 35 ▶use RDF::Query::Plan::Aggregate;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 35 ▶use RDF::Query::Plan::BasicGraphPattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 35 ▶use RDF::Query::Plan::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 35 ▶use RDF::Query::Plan::Construct;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 35 ▶use RDF::Query::Plan::Distinct;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 35 ▶use RDF::Query::Plan::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 35 ▶use RDF::Query::Plan::Join::NestedLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 35 ▶use RDF::Query::Plan::Join::PushDownNestedLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 35 ▶use RDF::Query::Plan::Limit;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 35 ▶use RDF::Query::Plan::Offset;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 35 ▶use RDF::Query::Plan::Project;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 35 ▶use RDF::Query::Plan::Extend;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 35 ▶use RDF::Query::Plan::Quad;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 35 ▶use RDF::Query::Plan::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 35 ▶use RDF::Query::Plan::Sort;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 35 ▶use RDF::Query::Plan::ComputedStatement;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 35 ▶use RDF::Query::Plan::ThresholdUnion;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 35 ▶use RDF::Query::Plan::Union;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 35 ▶use RDF::Query::Plan::SubSelect;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 35 ▶use RDF::Query::Plan::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 35 ▶use RDF::Query::Plan::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 35 ▶use RDF::Query::Plan::Clear;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 35 ▶use RDF::Query::Plan::Update;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 35 ▶use RDF::Query::Plan::Minus;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 35 ▶use RDF::Query::Plan::Sequence;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 35 ▶use RDF::Query::Plan::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 35 ▶use RDF::Query::Plan::NamedGraph;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 35 ▶use RDF::Query::Plan::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 35 ▶use RDF::Query::Plan::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 35 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 35 ▶use RDF::Trine::Statement::Quad;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 35 ▶use constant READY => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 35 ▶use constant OPEN => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 35 ▶use constant CLOSED => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 ######################################################################
66  
67 35 our ($VERSION, %PLAN_CLASSES);
68 35 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
69 35 $VERSION = '2.919';
70 35 %PLAN_CLASSES = (
71 service => 'RDF::Query::Plan::Service',
72 );
73 }
74  
75 ######################################################################
76  
77 =item C<< new >>
78  
79 =cut
80  
81 556 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
82 556 my $class = shift;
83 556 my @args = @_;
84 556 return bless( [ { __state => $class->READY }, @args ], $class );
85 }
86  
87 =item C<< execute ( $execution_context ) >>
88  
89 =cut
90  
91 sub execute ($);
92  
93 =item C<< next >>
94  
95 =cut
96  
97 sub next;
98  
99 =item C<< get_all >>
100  
101 Returns all remaining rows.
102  
103 =cut
104  
105 12 ▶sub get_all {
Subroutine get_all: called CC=3 SCAR=11.2
Pod documented
106 12 my $self = shift;
107 12 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 12 12
108 0 throw RDF::Query::Error::ExecutionError -text => "get_all can't be called on an unopen plan";
109 }
110 12 my @rows;
111 12 ▶ while (my $row = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->next) 34 12 46
112 34 push(@rows, $row);
113 }
114 12 return @rows;
115 }
116  
117 =item C<< close >>
118  
119 =cut
120  
121 590 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
122 590 my $self = shift;
123 590 $self->state( CLOSED );
124 }
125  
126 =item C<< state ( [ $state ] ) >>
127  
128 Returns the current state of the plan (either READY, OPEN, or CLOSED).
129 If C<< $state >> is provided, updates the plan to a new state.
130  
131 =cut
132  
133 5078 ▶sub state {
Subroutine state: called CC=2 SCAR=6.9
Pod documented
134 5078 my $self = shift;
135 5078 ▶ if (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 1190 3888 5078
136 1190 $self->[0]{__state} = shift;
137 }
138 5078 return $self->[0]{__state};
139 }
140  
141 =item C<< logging_keys >>
142  
143 =cut
144  
145 0 ▶sub logging_keys {
Subroutine logging_keys: not called CC=2 SCAR=17.9
Pod documented
146 0 my $self = shift;
147 0 ▶ return $self->[0]{logging_keys} || {};
Condition0%0/2
$self->[0]{'logging_keys'} || {}
l!l
00
MC/DC0%0/1
$self->[0]{'logging_keys'} || {}
$self->[0]{'logging_keys'}
Truth table0%0/2
A
A= $self->[0]{'logging_keys'}
Aresult
11
00
148 }
149  
150 =item C<< explain >>
151  
152 Returns a string serialization of the query plan appropriate for display
153 on the command line.
154  
155 =cut
156  
157 0 ▶sub explain {
Subroutine explain: not called CC=8 SCAR=40.3
Pod documented
158 0 my $self = shift;
159 # warn 'Explaining query plan: ' . $self->serialize();
160 0 my ($s, $count) = (' ', 0);
161 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
162 0 $s = shift;
163 0 $count = shift;
164 }
165 0 my $indent = '' . ($s x $count);
166 0 my $type = $self->plan_node_name;
167 0 my $string = sprintf("%s%s (0x%x)\n", $indent, $type, refaddr($self));
168 0 foreach my $p ($self->plan_node_data) {
169 0 ▶ if (blessed($p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($p)) { } 0 0 0
elsif (ref $p) { } 0 0 0
170 0 ▶ if ($p->isa('RDF::Trine::Statement::Quad')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->isa('RDF::Trine::Statement::Quad')) { } 0 0 0
elsif ($p->isa('RDF::Trine::Node::Nil')) { } 0 0 0
171 0 ▶ $string .= "${indent}${s}" . join(' ', map { ($_->isa('RDF::Trine::Node::Nil')) ? "(nil)" : $_->as_sparql } $p->nodes) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('RDF::Trine::Node::Nil') ? : 0 0 0
172 } elsif ($p->isa('RDF::Trine::Node::Nil')) {
173 0 $string .= "${indent}${s}(nil)\n";
174 } else {
175 0 $string .= $p->explain( $s, $count+1 );
176 }
177 } elsif (ref($p)) {
178 0 $string .= "${indent}${s}" . Dumper($p);
179 0 Carp::cluck 'unexpected non-blessed ref in RDF::Query::Plan->explain: ' . Dumper($p);
180 } else {
181 35 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
182 0 $string .= "${indent}${s}$p\n";
183 }
184 }
185 0 return $string;
186 }
187  
188 =item C<< sse >>
189  
190 =cut
191  
192 188 ▶sub sse {
Subroutine sse: called CC=5 SCAR=16.1
Pod documented
193 188 my $self = shift;
194 188 ▶ my $context = shift || {};
Condition100%2/2
shift() || {}
l!l
45143
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
195 188 ▶ my $indent = shift || '';
Condition100%2/2
shift() || ''
l!l
45143
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
196 188 my $more = ' ';
197 188 my @proto = $self->plan_prototype;
198 188 my @data = $self->plan_node_data;
199 188 my $name = $self->plan_node_name;
200
201 188 my @args;
202 188 my $list = \@data;
203 188 foreach my $i (0 .. $#proto) {
204 583 my $p = $proto[ $i ];
205 583 push(@args, $self->_sse( $context, $indent, $more, $p, $list ));
206 }
207 188 ▶ return "(${name} " . join(' ', map { defined($_) ? $_ : '()' } @args) . ")";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 582 1 583
208 }
209  
210 687 ▶sub _sse {
Subroutine _sse: called CC=10 SCAR=26.0
211 687 my $self = shift;
212 687 my $context = shift;
213 687 my $indent = shift;
214 687 my $more = shift;
215 687 my $p = shift;
216 687 my $list = shift;
217 687 ▶ if ($p =~ m/^[PQTNWEJVqibswu]$/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($p =~ /^[PQTNWEJVqibswu]$/) { } 630 57 687
elsif ($p eq 'A') { } 0 57 57
elsif (substr($p, 0, 1) eq '\\') { } 26 31 57
elsif (substr($p, 0, 1) eq '*') { } 31 0 31
elsif ($p =~ /^[PQTNWEJVqibswu\\*]{2,}$/) { } 0 0 0
218 630 my $v = shift(@$list);
219 630 return $self->_sse_atom($context, $indent, $more, $p, $v);
220 } elsif ($p eq 'A') {
221 0 my $v = shift(@$list);
222 0 ▶ if (blessed($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v)) { } 0 0 0
223 0 return $v->sse( $context, $indent );
224 } else {
225 0 return '()';
226 }
227 } elsif (substr($p, 0, 1) eq '\\') {
228 26 my $rest = substr($p, 1);
229 26 my $v = shift(@$list);
230 26 my @args;
231 26 ▶ while (@$v) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$v) 30 26 56
232 30 push(@args, $self->_sse( $context, $indent, $more, $rest, $v ));
233 }
234 26 return '(' . join(' ', @args) . ')';
235 } elsif (substr($p, 0, 1) eq '*') {
236 31 my $rest = substr($p, 1);
237 31 my @args;
238 31 ▶ while (@$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 74 31 105
239 74 push(@args, $self->_sse( $context, $indent, $more, $rest, $list ));
240 }
241 35 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
242 31 return join("\n${indent}${more}", '', @args);
243 } elsif ($p =~ m/^[PQTNWEJVqibswu\\*]{2,}$/) {
244 0 my @args;
245 0 foreach my $p2 (split(//, $p)) {
246 0 my $v = shift(@$list);
247 0 push(@args, $self->_sse($context, $indent, $more, $p2, [$v]));
248 }
249 0 return '(' . join(' ', @args) . ')';
250 } else {
251 0 Carp::confess "unrecognized plan node prototype '$p'";
252 }
253 }
254  
255 630 ▶sub _sse_atom {
Subroutine _sse_atom: called CC=16 SCAR=30.0
256 630 my $self = shift;
257 630 ▶ my $context = shift || {};
Condition50%1/2
shift() || {}
l!l
6300
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
258 630 my $indent = shift;
259 630 my $more = shift;
260 630 my $p = shift;
261 630 my $v = shift;
262 35 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
263
264 630 ▶ my $ns = $context->{ namespaces } || {};
Condition100%2/2
$context->{'namespaces'} || {}
l!l
149481
MC/DC100%1/1
$context->{'namespaces'} || {}
$context->{'namespaces'}
Truth table100%2/2
A
A= $context->{'namespaces'}
Aresult
11
00
265 630 my %ns = %$ns;
266
267 630 ▶ if ($p eq 's') {
Branch: 8 branches
BranchTrueFalseTotal
if ($p eq 's') { } 8 622 630
elsif ($p eq 'w') { } 0 622 622
elsif ($p eq 'u') { } 0 622 622
elsif ($p eq 'i') { } 3 619 622
elsif ($p eq 'b') { } 0 619 619
elsif ($p eq 'W') { } 0 619 619
elsif ($p =~ /^[PNETV]$/) { } 589 30 619
elsif ($p eq 'J') { } 30 0 30
268 8 for ($v) {
269 8 s/\\/\\\\/g;
270 8 s/"/\\"/g;
271 8 s/\n/\\n/g;
272 8 s/\t/\\t/g;
273 }
274 8 return qq["$v"];
275 } elsif ($p eq 'w') {
276 0 return $v;
277 } elsif ($p eq 'u') {
278 0 return qq[<$v>];
279 } elsif ($p eq 'i') {
280 3 return $v;
281 } elsif ($p eq 'b') {
282 0 return $v;
283 } elsif ($p eq 'W') {
284 0 ▶ if (blessed($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v)) { } 0 0 0
285 0 return $v->sse( { namespaces => \%ns }, "${indent}${more}" );
286 } else {
287 0 return $v;
288 }
289 } elsif ($p =~ m/^[PNETV]$/) {
290 589 ▶ if (blessed($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v)) { } 580 9 589
291
292 580 ▶ Carp::cluck unless ($v->can('sse'));
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("sse") 0 580 580
293 580 return $v->sse( { namespaces => \%ns }, "${indent}${more}" );
294 } else {
295 9 return '()';
296 }
297 } elsif ($p eq 'J') {
298 30 ▶ if ($v->isa('RDF::Query::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->isa('RDF::Query::Node::Variable')) { } 2 28 30
299 2 return $v->name;
300 } else {
301 28 return $v->sse( { namespaces => \%ns }, "${indent}${more}" );
302 }
303 }
304 }
305  
306 =item C<< serialize >>
307  
308 Return a serialization of the query plan.
309  
310 =cut
311  
312 0 ▶sub serialize {
Subroutine serialize: not called CC=1 SCAR=6.9
Pod documented
313 0 my $self = shift;
314
315 }
316  
317 =item C<< delegate >>
318  
319 Returns the delegate object if available.
320  
321 =cut
322  
323 978 ▶sub delegate {
Subroutine delegate: called CC=1 SCAR=0.0
Pod documented
324 978 my $self = shift;
325 978 return $self->[0]{delegate};
326 }
327  
328 =item C<< referenced_variables >>
329  
330 Returns a list of variable names that are referenced by this plan.
331  
332 =cut
333  
334 366 ▶sub referenced_variables {
Subroutine referenced_variables: called CC=1 SCAR=0.0
Pod documented
335 366 my $self = shift;
336 366 my $refs = $self->[0]{referenced_variables};
337 366 return @{ $refs };
338 }
339  
340 =item C<< as_iterator ( $context ) >>
341  
342 Returns an RDF::Trine::Iterator object for the current (already executed) plan.
343  
344 =cut
345  
346 153 ▶sub as_iterator {
Subroutine as_iterator: called CC=1 SCAR=0.0
Pod documented
347 153 my $self = shift;
348 153 my $context = shift;
349 153 my $vars = $context->requested_variables;
350 153 ▶ my $stream = RDF::Trine::Iterator::Bindings->new( sub { $self->next }, $vars, distinct => $self->distinct, sorted_by => $self->ordered );
Subroutine __ANON__: called CC=1 SCAR=0.0
351 153 return $stream;
352 }
353  
354 =item C<< is_update >>
355  
356 Returns true if the plan represents an update operation.
357  
358 =cut
359  
360 0 ▶sub is_update {
Subroutine is_update: not called CC=1 SCAR=6.9
Pod documented
361 0 return 0;
362 }
363  
364 =item C<< label ( $label => $value ) >>
365  
366 Sets the named C<< $label >> to C<< $value >> for this plan object.
367 If no C<< $value >> is given, returns the current label value, or undef if none
368 exists.
369  
370 =cut
371  
372 779 ▶sub label {
Subroutine label: called CC=2 SCAR=7.0
Pod documented
373 779 my $self = shift;
374 779 my $label = shift;
375 779 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 779 0 779
376 779 my $value = shift;
377 779 $self->[0]{labels}{ $label } = $value;
378 }
379 779 return $self->[0]{labels}{ $label };
380 }
381  
382 =item C<< graph_labels >>
383  
384 =cut
385  
386 0 ▶sub graph_labels {
Subroutine graph_labels: not called CC=4 SCAR=30.0
Pod documented
387 0 my $self = shift;
388 0 my @labels;
389 0 ▶ foreach my $k (keys %{ $self->[0]{labels} || {} }) {
Condition0%0/2
$self->[0]{'labels'} || {}
l!l
00
MC/DC0%0/1
$self->[0]{'labels'} || {}
$self->[0]{'labels'}
Truth table0%0/2
A
A= $self->[0]{'labels'}
Aresult
11
00
390 0 ▶ next if ($k eq 'algebra');
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "algebra" 0 0 0
391 0 my $v = $self->label( $k );
392 0 local($Data::Dumper::Indent) = 0;
393 0 my $l = Data::Dumper->Dump([$v], [$k]);
394 0 push(@labels, $l);
395 }
396 0 my $label = join(", ", @labels);
397 0 return ' ' . $label;
398 }
399  
400 546 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
401 546 my $self = shift;
402 546 ▶ if ($self->state == OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state == 2) 205 341 546
403 205 $self->close;
404 }
405 }
406  
407 ################################################################################
408  
409 =item C<< generate_plans ( $algebra, $execution_context, %args ) >>
410  
411 Returns a list of equivalent query plan objects for the given algebra object.
412  
413 =cut
414  
415 773 ▶sub generate_plans {
Subroutine generate_plans: called CC=100 SCAR=63.6
Pod documented
416 773 my $self = shift;
417 773 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
07730
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
418 773 my $algebra = shift;
419 773 my $context = shift;
420 773 ▶ my $config = $context->options || {};
Condition100%2/2
$context->options || {}
l!l
7685
MC/DC100%1/1
$context->options || {}
$context->options
Truth table100%2/2
A
A= $context->options
Aresult
11
00
421
422 773 my %args = @_;
423 773 ▶ my $active_graph = $args{ active_graph } || RDF::Trine::Node::Nil->new();
Condition66%2/3
$args{'active_graph'} || 'RDF::Trine::Node::Nil'->new
l!l&&r!l&&!r
1366370
MC/DC0%0/2
$args{'active_graph'} || 'RDF::Trine::Node::Nil'->new
$args{'active_graph'} 'RDF::Trine::Node::Nil'->new
Truth table66%2/3
A || B
A= $args{'active_graph'}B= 'RDF::Trine::Node::Nil'->new
ABresult
1X1
011
000
424
425 773 my $l = Log::Log4perl->get_logger("rdf.query.plan");
426 773 ▶ unless (blessed($algebra) and $algebra->isa('RDF::Query::Algebra')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($algebra) and $algebra->isa("RDF::Query::Algebra")) 0 773 773
Condition33%1/3
builtin::blessed($algebra) and $algebra->isa("RDF::Query::Algebra")
!ll&&!rl&&r
00773
MC/DC0%0/2
builtin::blessed($algebra) and $algebra->isa("RDF::Query::Algebra")
builtin::blessed($algebra) $algebra->isa("RDF::Query::Algebra")
Truth table33%1/3
A and B
A= builtin::blessed($algebra)B= $algebra->isa("RDF::Query::Algebra")
ABresult
0X0
100
111
427 0 throw RDF::Query::Error::MethodInvocationError (-text => "Cannot generate an execution plan with a non-algebra object $algebra");
428 }
429
430 773 $l->trace("generating query plan for " . $algebra->sse({ indent => ' ' }, ''));
431
432 ############################################################################
433 ### Optimize simple COUNT(*) aggregates over BGPs
434 773 ▶ if ($algebra->isa('RDF::Query::Algebra::Extend')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->isa("RDF::Query::Algebra::Extend")) 24 749 773
435 24 my $agg = $algebra->pattern;
436 24 ▶ if ($agg->isa('RDF::Query::Algebra::Aggregate')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agg->isa("RDF::Query::Algebra::Aggregate")) 15 9 24
437 15 my @group = $agg->groupby;
438 15 ▶ if (scalar(@group) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @group == 0) 9 6 15
439 9 my @ops = $agg->ops;
440 9 ▶ if (scalar(@ops) == 1 and $ops[0][0] eq 'COUNT(*)') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ops == 1 and $ops[0][0] eq "COUNT(*)") 0 9 9
Condition66%2/3
scalar @ops == 1 and $ops[0][0] eq "COUNT(*)"
!ll&&!rl&&r
270
MC/DC0%0/2
scalar @ops == 1 and $ops[0][0] eq "COUNT(*)"
scalar @ops == 1 $ops[0][0] eq "COUNT(*)"
Truth table66%2/3
A and B
A= scalar @ops == 1B= $ops[0][0] eq "COUNT(*)"
ABresult
0X0
100
111
441 0 my $ggp = $agg->pattern;
442 0 ▶ if ($ggp->isa('RDF::Query::Algebra::GroupGraphPattern')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ggp->isa("RDF::Query::Algebra::GroupGraphPattern")) 0 0 0
443 0 my @bgp = $ggp->patterns;
444 0 ▶ if (scalar(@bgp) == 1 and ($bgp[0]->isa('RDF::Query::Algebra::BasicGraphPattern'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @bgp == 1 and $bgp[0]->isa("RDF::Query::Algebra::BasicGraphPattern")) 0 0 0
Condition0%0/3
scalar @bgp == 1 and $bgp[0]->isa("RDF::Query::Algebra::BasicGraphPattern")
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @bgp == 1 and $bgp[0]->isa("RDF::Query::Algebra::BasicGraphPattern")
scalar @bgp == 1 $bgp[0]->isa("RDF::Query::Algebra::BasicGraphPattern")
Truth table0%0/3
A and B
A= scalar @bgp == 1B= $bgp[0]->isa("RDF::Query::Algebra::BasicGraphPattern")
ABresult
0X0
100
111
445 0 my $bgp = $bgp[0];
446 0 my @triples = $bgp->triples;
447 0 ▶ if (scalar(@triples) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @triples == 1) 0 0 0
448 0 $l->debug("Optimizing for COUNT(*) on 1-triple BGP: " . $bgp->sse({ indent => ' ' }, ''));
449 0 my $vars = $algebra->vars;
450 0 my $alias = $vars->[0];
451 0 my $name = $alias->name;
452 0 my $done = 0;
453 0 my $model = $context->model;
454 0 ▶ my $code = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
455 0 ▶ return if ($done);
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
456 0 $done = 1;
457 #warn Dumper(\@triples); # XXX
458 0 my $count = $model->count_statements( $triples[0]->nodes );
459 0 my $lit = RDF::Query::Node::Literal->new($count, undef, 'http://www.w3.org/2001/XMLSchema#integer');
460 0 my $vb = RDF::Query::VariableBindings->new( {
461 $name => $lit,
462 'COUNT(*)' => $lit, # this has to be kept around in case a HAVING clause needs it without the alias $name
463 } );
464 0 };
465 0 my $iter = RDF::Trine::Iterator::Bindings->new( $code, [] );
466 0 return RDF::Query::Plan::Iterator->new( $iter );
467 }
468 }
469 }
470 }
471 }
472 }
473 }
474 ############################################################################
475
476 773 my ($project);
477 773 my $constant = delete $args{ constants };
478
479 773 ▶ if ($algebra->isa('RDF::Query::Algebra::Sort') or not($algebra->is_solution_modifier)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->isa("RDF::Query::Algebra::Sort") or not $algebra->is_solution_modifier) 594 179 773
Condition100%3/3
$algebra->isa("RDF::Query::Algebra::Sort") or not $algebra->is_solution_modifier
l!l&&r!l&&!r
12582179
MC/DC100%2/2
$algebra->isa("RDF::Query::Algebra::Sort") or not $algebra->is_solution_modifier
$algebra->isa("RDF::Query::Algebra::Sort") not $algebra->is_solution_modifier
Truth table100%3/3
A or not(B)
A= $algebra->isa("RDF::Query::Algebra::Sort")B= not $algebra->is_solution_modifier
ABresult
1X1
011
000
480 # projection has to happen *after* sorting, since a sort expr might reference a variable that we project away
481 594 $project = delete $args{ project };
482 }
483
484 773 my @return_plans;
485 773 my $aclass = ref($algebra);
486 773 my ($type) = ($aclass =~ m<::(\w+)$>);
487 773 ▶ if ($type eq 'Aggregate') {
Branch: 27 branches
BranchTrueFalseTotal
if ($type eq 'Aggregate') { } 16 757 773
elsif ($type eq 'Construct') { } 4 753 757
elsif ($type eq 'Distinct') { } 10 743 753
elsif ($type eq 'Filter') { } 37 706 743
elsif ($type eq 'BasicGraphPattern') { } 146 560 706
elsif ($type eq 'GroupGraphPattern') { } 205 355 560
elsif ($type eq 'Limit') { } 8 347 355
elsif ($type eq 'NamedGraph') { } 14 333 347
elsif ($type eq 'Offset') { } 3 330 333
elsif ($type eq 'Optional') { } 11 319 330
elsif ($type eq 'Minus') { } 0 319 319
elsif ($type eq 'Project') { } 134 185 319
elsif ($type eq 'Extend') { } 24 161 185
elsif ($type eq 'Service') { } 0 161 161
elsif ($type eq 'SubSelect') { } 1 160 161
elsif ($type eq 'Sort') { } 12 148 160
elsif ($type eq 'Triple' or $type eq 'Quad') { } 133 15 148
elsif ($type eq 'Path') { } 5 10 15
elsif ($type eq 'Union') { } 2 8 10
elsif ($type eq 'Sequence') { } 0 8 8
elsif ($type eq 'Load') { } 0 8 8
elsif ($type eq 'Update') { } 8 0 8
elsif ($type eq 'Clear') { } 0 0 0
elsif ($type eq 'Create') { } 0 0 0
elsif ($type eq 'Copy') { } 0 0 0
elsif ($type eq 'Move') { } 0 0 0
elsif ($type eq 'Table') { } 0 0 0
488 16 my @base = $self->generate_plans( $algebra->pattern, $context, %args );
489 16 my @groups = $algebra->groupby;
490 16 my @ops;
491 16 foreach my $o ($algebra->ops) {
492 18 my ($alias, $op, $opts, @cols) = @$o;
493 18 push(@ops, [ $alias, $op, $opts, @cols ]);
494 }
495 16 my @plans = map { RDF::Query::Plan::Aggregate->new( $_, \@groups, expressions => \@ops ) } @base;
496 16 push(@return_plans, @plans);
497 } elsif ($type eq 'Construct') {
498 4 my $triples = $algebra->triples;
499 4 my @base = $self->generate_plans( $algebra->pattern, $context, %args );
500 4 my @plans = map { RDF::Query::Plan::Construct->new( $_, $triples ) } @base;
501 4 push(@return_plans, @plans);
502 } elsif ($type eq 'Distinct') {
503 10 my @base = $self->generate_plans( $algebra->pattern, $context, %args );
504 10 my @plans = map { RDF::Query::Plan::Distinct->new( $_ ) } @base;
505 10 push(@return_plans, @plans);
506 } elsif ($type eq 'Filter') {
507 37 my @base = $self->generate_plans( $algebra->pattern, $context, %args, active_graph => $active_graph );
508 37 my $expr = $algebra->expr;
509 37 my @plans = map { RDF::Query::Plan::Filter->new( $expr, $_, $active_graph ) } @base;
510 37 push(@return_plans, @plans);
511 } elsif ($type eq 'BasicGraphPattern') {
512 my @triples = map {
513 146 ($args{ prevent_distinguishing_bnodes })
514 277 ▶ ? $_
Branch: 1 branch
BranchTrueFalseTotal
$args{'prevent_distinguishing_bnodes'} ? : 6 271 277
515 : $_->distinguish_bnode_variables
516 } $algebra->triples;
517 146 my @normal_triples;
518 my @csg_triples;
519 146 foreach my $t (@triples) {
520 277 ▶ if (my @csg_plans = $self->_csg_plans( $context, $t )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@csg_plans) = $self->_csg_plans($context, $t)) { } 1 276 277
521 1 push(@csg_triples, $t);
522 } else {
523 276 my @nodes = $t->nodes;
524 276 $t = RDF::Query::Algebra::Quad->new( @nodes[ 0..2 ], $active_graph );
525 # if (my $g = $args{ named_graph }) {
526 # my @nodes = $t->nodes;
527 # $t = RDF::Query::Algebra::Quad->new( @nodes[0..2], $g );
528 # }
529 276 push(@normal_triples, $t);
530 }
531 }
532
533 146 my @plans;
534 146 ▶ if (scalar(@normal_triples) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @normal_triples == 0) { } 0 146 146
elsif (scalar @normal_triples == 1) { } 70 76 146
535 0 my $v = RDF::Query::VariableBindings->new( {} );
536 0 my $plan = RDF::Query::Plan::Constant->new( $v );
537 0 push(@plans, $plan);
538 } elsif (scalar(@normal_triples) == 1) {
539 70 push(@plans, $self->generate_plans( @normal_triples, $context, %args ));
540 } else {
541 76 my $plan = RDF::Query::Plan::BasicGraphPattern->new( @normal_triples );
542 76 push(@plans, $plan);
543 }
544
545 146 ▶ if (@csg_triples) {
Branch: 1 branch
BranchTrueFalseTotal
if (@csg_triples) { } 1 145 146
546 1 my @csg_plans;
547 1 foreach my $t (@csg_triples) {
548 1 push(@csg_plans, [ $self->generate_plans( $t, $context, %args ) ]);
549 }
550 1 my @join_types = RDF::Query::Plan::Join->join_classes( $config );
551 1 ▶ while (my $cps = shift(@csg_plans)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cps = shift @csg_plans) 1 1 2
552 1 my @temp_plans = @plans;
553 1 @plans = ();
554 1 foreach my $p (@temp_plans) {
555 1 foreach my $cp (@$cps) {
556 1 foreach my $join_type (@join_types) {
557 2 my $plan = $join_type->new( $p, $cp, 0, {} );
558 2 push(@plans, $plan);
559 }
560 }
561 }
562 }
563 1 push(@return_plans, @plans);
564 } else {
565 145 push(@return_plans, @plans);
566 }
567
568 } elsif ($type eq 'GroupGraphPattern') {
569 205 my @input = $algebra->patterns();
570 205 my @patterns;
571 205 ▶ while (my $a = shift(@input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = shift @input) 201 205 406
572 201 ▶ if ($a->isa('RDF::Query::Algebra::Service')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->isa("RDF::Query::Algebra::Service")) 0 201 201
573 0 ▶ if (scalar(@input) and $input[0]->isa('RDF::Query::Algebra::Service') and $a->endpoint->value eq $input[0]->endpoint->value) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @input and $input[0]->isa("RDF::Query::Algebra::Service") and $a->endpoint->value eq $input[0]->endpoint->value) 0 0 0
Condition0%0/6
scalar @input and $input[0]->isa("RDF::Query::Algebra::Service") and $a->endpoint->value eq $input[0]->endpoint->value
!ll&&!rl&&r
000
scalar @input and $input[0]->isa("RDF::Query::Algebra::Service")
!ll&&!rl&&r
000
MC/DC0%0/3
scalar @input and $input[0]->isa("RDF::Query::Algebra::Service") and $a->endpoint->value eq $input[0]->endpoint->value
scalar @input $input[0]->isa("RDF::Query::Algebra::Service") $a->endpoint->value eq $input[0]->endpoint->value
Truth table0%0/4
A and B and C
A= scalar @inputB= $input[0]->isa("RDF::Query::Algebra::Service")C= $a->endpoint->value eq $input[0]->endpoint->value
ABCresult
0XX0
10X0
1100
1111
574 0 my $b = shift(@input);
575 0 ▶ if ($a->silent == $b->silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->silent == $b->silent) 0 0 0
576 0 my $p = RDF::Query::Algebra::GroupGraphPattern->new( map { $_->pattern } ($a, $b) );
577 0 my $s = RDF::Query::Algebra::Service->new( $a->endpoint, $p, $a->silent );
578 0 push(@patterns, $s);
579 0 next;
580 }
581 }
582 }
583 201 push(@patterns, $a);
584 }
585
586 205 my @plans;
587 205 ▶ if (scalar(@patterns) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @patterns == 0) { } 18 187 205
elsif (scalar @patterns == 1) { } 174 13 187
588 18 my $v = RDF::Query::VariableBindings->new( {} );
589 18 my $plan = RDF::Query::Plan::Constant->new( $v );
590 18 push(@plans, $plan);
591 } elsif (scalar(@patterns) == 1) {
592 174 push(@plans, $self->generate_plans( @patterns, $context, %args ));
593 } else {
594 13 push(@plans, map { $_->[0] } $self->_join_plans( $context, \@patterns, %args, method => 'patterns' ));
595 }
596
597 205 push(@return_plans, @plans);
598 } elsif ($type eq 'Limit') {
599 8 my @base = $self->generate_plans( $algebra->pattern, $context, %args );
600 8 my @plans = map { RDF::Query::Plan::Limit->new( $algebra->limit, $_ ) } @base;
601 8 push(@return_plans, @plans);
602 } elsif ($type eq 'NamedGraph') {
603 14 my @plans;
604 14 ▶ if ($algebra->graph->isa('RDF::Query::Node::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->graph->isa('RDF::Query::Node::Resource')) { } 4 10 14
605 4 @plans = $self->generate_plans( $algebra->pattern, $context, %args, active_graph => $algebra->graph );
606 } else {
607 10 @plans = map { RDF::Query::Plan::NamedGraph->new( $algebra->graph, $_ ) } $self->generate_plans( $algebra->pattern, $context, %args, active_graph => $algebra->graph );
608 }
609 14 push(@return_plans, @plans);
610 } elsif ($type eq 'Offset') {
611 3 my @base = $self->generate_plans( $algebra->pattern, $context, %args );
612 3 my @plans = map { RDF::Query::Plan::Offset->new( $algebra->offset, $_ ) } @base;
613 3 push(@return_plans, @plans);
614 } elsif ($type eq 'Optional') {
615 # just like a BGP or GGP, but we have to pass the optional flag to the join constructor
616 11 my @patterns = ($algebra->pattern, $algebra->optional);
617 11 my @base_plans = map { [ $self->generate_plans( $_, $context, %args ) ] } @patterns;
618 11 my @join_types = RDF::Query::Plan::Join->join_classes( $config );
619 # XXX this is currently only considering left-deep trees. maybe it should produce all trees?
620 11 my @plans;
621 11 my $base_a = shift(@base_plans);
622 11 my $base_b = shift(@base_plans);
623 11 foreach my $i (0 .. $#{ $base_a }) {
624 11 foreach my $j (0 .. $#{ $base_b }) {
625 11 my $a = $base_a->[ $i ];
626 11 my $b = $base_b->[ $j ];
627 11 foreach my $join_type (@join_types) {
628 20 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
629 20 my $plan = $join_type->new( $a, $b, 1, {} );
630 10 push( @plans, $plan );
631 10 ▶ } catch RDF::Query::Error::MethodInvocationError with {
Subroutine __ANON__: called CC=1 SCAR=0.0
632 # my $e = shift;
633 # warn "caught MethodInvocationError: " . Dumper($e);
634 22 };
635 }
636 }
637 }
638 11 push(@return_plans, @plans);
639 } elsif ($type eq 'Minus') {
640 0 my @patterns = ($algebra->pattern, $algebra->minus);
641 0 my @base_plans = map { [ $self->generate_plans( $_, $context, %args ) ] } @patterns;
642 0 my @plans;
643 0 my $base_a = shift(@base_plans);
644 0 my $base_b = shift(@base_plans);
645 0 foreach my $i (0 .. $#{ $base_a }) {
646 0 foreach my $j (0 .. $#{ $base_b }) {
647 0 my $a = $base_a->[ $i ];
648 0 my $b = $base_b->[ $j ];
649 0 my $plan = RDF::Query::Plan::Minus->new( $a, $b );
650 0 push( @plans, $plan );
651 }
652 }
653 0 push(@return_plans, @plans);
654 } elsif ($type eq 'Project') {
655 134 my $pattern = $algebra->pattern;
656 134 my $vars = $algebra->vars;
657 134 my @base = $self->generate_plans( $pattern, $context, %args );
658
659 134 ▶ if ($constant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constant) 3 131 134
660 # if there's constant data to be joined, we better do it now in case
661 # the project gets rid of variables needed for the join
662 3 my @plans = splice( @base );
663 3 @base = $self->_add_constant_join( $context, $constant, @plans );
664 3 $constant = undef;
665 }
666
667 134 my @plans;
668 134 foreach my $plan (@base) {
669 138 push(@return_plans, RDF::Query::Plan::Project->new( $plan, $vars ));
670 }
671 134 push(@return_plans, @plans);
672 } elsif ($type eq 'Extend') {
673 24 my $pattern = $algebra->pattern;
674 24 my $vars = $algebra->vars;
675 24 my @base = $self->generate_plans( $pattern, $context, %args );
676 24 my @plans;
677 24 foreach my $plan (@base) {
678 24 push(@plans, RDF::Query::Plan::Extend->new( $plan, $vars ));
679 }
680 24 push(@return_plans, @plans);
681 } elsif ($type eq 'Service') {
682 0 my $pattern = $algebra->pattern;
683 0 my @base = $self->generate_plans( $pattern, $context, %args );
684 0 my @plans;
685 0 foreach my $plan (@base) {
686 0 ▶ my $sparqlcb = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
687 0 my $row = shift;
688 0 my $p = $pattern;
689 0 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 0 0 0
690 0 $p = $p->bind_variables( $row );
691 }
692 0 my $ns = $context->ns;
693 0 my $pstr = $p->as_sparql({namespaces => $ns}, '');
694 0 ▶ unless (substr($pstr, 0, 1) eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($pstr, 0, 1) eq "{") 0 0 0
695 0 $pstr = "{ $pstr }";
696 }
697 my $sparql = join("\n",
698 0 ▶ (map { sprintf("PREFIX %s: <%s>", ($_ eq '__DEFAULT__' ? '' : $_), $ns->{$_}) } (keys %$ns)),
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '__DEFAULT__' ? : 0 0 0
699 sprintf("SELECT * WHERE %s", $pstr)
700 );
701 0 return $sparql;
702 0 };
703
704 # unless ($algebra->endpoint->can('uri_value')) {
705 # throw RDF::Query::Error::UnimplementedError (-text => "Support for variable-endpoint SERVICE blocks is not implemented");
706 # }
707
708 0 ▶ if (my $ggp = $algebra->lhs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ggp = $algebra->lhs) { } 0 0 0
709 0 my @lhs_base = $self->generate_plans( $ggp, $context, %args );
710 0 foreach my $lhs_plan (@lhs_base) {
711 0 my $splan = RDF::Query::Plan::Service->new( $algebra->endpoint, $plan, $algebra->silent, $sparqlcb, $lhs_plan );
712 0 push(@plans, $splan);
713 }
714 } else {
715 0 push(@plans, $PLAN_CLASSES{'service'}->new( $algebra->endpoint, $plan, $algebra->silent, $sparqlcb ));
716 }
717 }
718 0 push(@return_plans, @plans);
719 } elsif ($type eq 'SubSelect') {
720 1 my $query = $algebra->query;
721 1 my $model = $context->model;
722 1 my %pargs = %args;
723 1 my $ag = $args{ active_graph };
724 1 ▶ if (blessed($ag) and $ag->isa('RDF::Query::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($ag) and $ag->isa('RDF::Query::Node::Variable')) { } 0 1 1
Condition33%1/3
builtin::blessed($ag) and $ag->isa("RDF::Query::Node::Variable")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($ag) and $ag->isa("RDF::Query::Node::Variable")
builtin::blessed($ag) $ag->isa("RDF::Query::Node::Variable")
Truth table33%1/3
A and B
A= builtin::blessed($ag)B= $ag->isa("RDF::Query::Node::Variable")
ABresult
0X0
100
111
725 0 my %vars = map { $_ => 1 } $query->pattern->referenced_variables;
726 0 ▶ if ($vars{ $ag->name }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{$ag->name}) 0 0 0
727 0 my $new_ag = RDF::Query::Node::Variable->new();
728 0 my ($pattern) = $query->pattern;
729 0 my $new_pattern = $pattern->bind_variables( { $ag->name => $new_ag } );
730 0 my $apattern = RDF::Query::Algebra::Extend->new(
731 $new_pattern,
732 [
733 RDF::Query::Expression::Alias->new( 'alias', $ag, $new_ag )
734 ]
735 );
736 0 $query->{parsed}{triples} = [$apattern];
737 }
738 0 my ($plan) = $self->generate_plans( $query->pattern, $context, %args );
739 0 push(@return_plans, RDF::Query::Plan::SubSelect->new( $query, $plan ));
740 } else {
741 1 my ($plan) = $query->prepare( $context->model, planner_args => \%pargs );
742 1 push(@return_plans, RDF::Query::Plan::SubSelect->new( $query, $plan ));
743 }
744 } elsif ($type eq 'Sort') {
745 12 my @base = $self->generate_plans( $algebra->pattern, $context, %args );
746 12 my @order = $algebra->orderby;
747 12 my @neworder;
748 12 foreach my $o (@order) {
749 12 my ($dirname, $expr) = @$o;
750 12 ▶ my $dir = ($dirname eq 'ASC') ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$dirname eq 'ASC' ? : 9 3 12
751 12 push(@neworder, [$expr, $dir]);
752 }
753 12 my @plans = map { RDF::Query::Plan::Sort->new( $_, @neworder ) } @base;
754 12 push(@return_plans, @plans);
755 ▶ } elsif ($type eq 'Triple' or $type eq 'Quad') {
Condition100%3/3
$type eq "Triple" or $type eq "Quad"
l!l&&r!l&&!r
2510815
MC/DC100%2/2
$type eq "Triple" or $type eq "Quad"
$type eq "Triple" $type eq "Quad"
Truth table100%3/3
A or B
A= $type eq "Triple"B= $type eq "Quad"
ABresult
1X1
011
000
756 133 my $st;
757 133 ▶ if ($args{ prevent_distinguishing_bnodes }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'prevent_distinguishing_bnodes'}) { } 40 93 133
758 40 $st = $algebra;
759 } else {
760 93 $st = $algebra->distinguish_bnode_variables;
761 }
762 133 my $pred = $st->predicate;
763 133 my @nodes = $st->nodes;
764
765 133 ▶ if (my @csg_plans = $self->_csg_plans( $context, $st )) {
Branch: 2 branches
BranchTrueFalseTotal
if (my(@csg_plans) = $self->_csg_plans($context, $st)) { } 1 132 133
elsif ($type eq 'Triple') { } 24 108 132
766 1 push(@return_plans, @csg_plans);
767 } elsif ($type eq 'Triple') {
768 24 my $plan = RDF::Query::Plan::Quad->new( @nodes[0..2], $active_graph, { sparql => $algebra->as_sparql, bf => $algebra->bf } );
769 24 push(@return_plans, $plan);
770 } else {
771 108 ▶ my $plan = (scalar(@nodes) == 4)
Branch: 1 branch
BranchTrueFalseTotal
scalar @nodes == 4 ? : 108 0 108
772 ? RDF::Query::Plan::Quad->new( @nodes, { sparql => $algebra->as_sparql } )
773 : RDF::Query::Plan::Quad->new( @nodes, RDF::Trine::Node::Nil->new(), { sparql => $algebra->as_sparql, bf => $algebra->bf } );
774 108 push(@return_plans, $plan);
775 }
776 } elsif ($type eq 'Path') {
777 5 my @plans = $self->_path_plans( $algebra, $context, %args );
778 5 push(@return_plans, @plans);
779 } elsif ($type eq 'Union') {
780 2 my @plans = map { [ $self->generate_plans( $_, $context, %args ) ] } $algebra->patterns;
781 2 my $plan = RDF::Query::Plan::Union->new( map { $_->[0] } @plans );
782 2 push(@return_plans, $plan);
783 } elsif ($type eq 'Sequence') {
784 0 my @pat = $algebra->patterns;
785 0 ▶ if (@pat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pat) { } 0 0 0
786 0 my @plans = map { [ $self->generate_plans( $_, $context, %args ) ] } @pat;
787 0 my $plan = RDF::Query::Plan::Sequence->new( map { $_->[0] } @plans );
788 0 push(@return_plans, $plan);
789 } else {
790 0 ▶ my $stream = RDF::Trine::Iterator::Bindings->new( sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
791 0 push(@return_plans, $stream);
792 }
793 } elsif ($type eq 'Load') {
794 0 push(@return_plans, RDF::Query::Plan::Load->new( $algebra->url, $algebra->graph ));
795 } elsif ($type eq 'Update') {
796 8 ▶ my $ds = $algebra->dataset || {};
Condition100%2/2
$algebra->dataset || {}
l!l
26
MC/DC100%1/1
$algebra->dataset || {}
$algebra->dataset
Truth table100%2/2
A
A= $algebra->dataset
Aresult
11
00
797 8 ▶ my $default = $ds->{'default'} || [];
Condition100%2/2
$ds->{'default'} || []
l!l
17
MC/DC100%1/1
$ds->{'default'} || []
$ds->{'default'}
Truth table100%2/2
A
A= $ds->{'default'}
Aresult
11
00
798 8 ▶ my $named = $ds->{'named'} || {};
Condition50%1/2
$ds->{'named'} || {}
l!l
08
MC/DC0%0/1
$ds->{'named'} || {}
$ds->{'named'}
Truth table50%1/2
A
A= $ds->{'named'}
Aresult
11
00
799 8 my $dcount = scalar(@$default);
800 8 my $ncount = scalar(@{[ keys %$named ]});
801 # warn 'Update dataset: ' . Dumper($algebra->dataset);
802 8 my @plans;
803
804 8 my @dataset = ($ds);
805 8 ▶ if ($dcount == 1 and $ncount == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dcount == 1 and $ncount == 0) { } 1 7 8
elsif ($dcount == 0 and $ncount == 0) { } 7 0 7
Condition66%2/3
$dcount == 1 and $ncount == 0
!ll&&!rl&&r
701
MC/DC50%1/2
$dcount == 1 and $ncount == 0
$dcount == 1 $ncount == 0
Truth table66%2/3
A and B
A= $dcount == 1B= $ncount == 0
ABresult
0X0
100
111
806 # if it's just a single named graph to be used as the default graph,
807 # then rewrite the pattern to use the named graph (and check to make
808 # sure there aren't any GRAPH blocks)
809 1 @dataset = ();
810 1 @plans = $self->generate_plans( $algebra->pattern, $context, %args, active_graph => $default->[0] );
811 ▶ } elsif ($dcount == 0 and $ncount == 0) {
Condition33%1/3
$dcount == 0 and $ncount == 0
!ll&&!rl&&r
007
MC/DC0%0/2
$dcount == 0 and $ncount == 0
$dcount == 0 $ncount == 0
Truth table33%1/3
A and B
A= $dcount == 0B= $ncount == 0
ABresult
0X0
100
111
812 7 @dataset = ();
813 7 @plans = $self->generate_plans( $algebra->pattern, $context, %args );
814 } else {
815 0 @plans = $self->generate_plans( $algebra->pattern, $context, %args );
816 }
817 8 foreach my $p (@plans) {
818 8 push(@return_plans, RDF::Query::Plan::Update->new( $algebra->delete_template, $algebra->insert_template, $p, @dataset ));
819 }
820 } elsif ($type eq 'Clear') {
821 0 push(@return_plans, RDF::Query::Plan::Clear->new( $algebra->graph ));
822 } elsif ($type eq 'Create') {
823 0 my $plan = RDF::Query::Plan::Constant->new();
824 0 push(@return_plans, $plan);
825 } elsif ($type eq 'Copy') {
826 0 my $plan = RDF::Query::Plan::Copy->new( $algebra->from, $algebra->to, $algebra->silent );
827 0 push(@return_plans, $plan);
828 } elsif ($type eq 'Move') {
829 0 my $plan = RDF::Query::Plan::Move->new( $algebra->from, $algebra->to, $algebra->silent );
830 0 push(@return_plans, $plan);
831 } elsif ($type eq 'Table') {
832 0 my $plan = RDF::Query::Plan::Constant->new( $algebra->rows );
833 0 push(@return_plans, $plan);
834 } else {
835 0 throw RDF::Query::Error::MethodInvocationError (-text => "Cannot generate an execution plan for unknown algebra class $aclass");
836 }
837
838 773 ▶ if ($constant and scalar(@$constant)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constant and scalar @$constant) 0 773 773
Condition50%1/2
$constant and scalar @$constant
!ll
7730
MC/DC0%0/1
$constant and scalar @$constant
$constant
Truth table50%1/2
A
A= $constant
Aresult
00
11
839 0 my @plans = splice( @return_plans );
840 0 @return_plans = $self->_add_constant_join( $context, $constant, @plans );
841 }
842
843 773 foreach my $p (@return_plans) {
844 779 ▶ Carp::confess 'not a plan: ' . Dumper($p) unless ($p->isa('RDF::Query::Plan'));
Branch: 1 branch
BranchTrueFalseTotal
unless $p->isa("RDF::Query::Plan") 0 779 779
845 779 $p->label( algebra => $algebra );
846 }
847
848 773 ▶ unless (scalar(@return_plans)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @return_plans) 0 773 773
849 0 throw RDF::Query::Error::CompilationError (-text => "Cannot generate an execution plan for algebra of type $type", -object => $algebra);
850 }
851 773 return @return_plans;
852 }
853  
854 410 ▶sub _csg_plans {
Subroutine _csg_plans: called CC=6 SCAR=18.0
855 410 my $self = shift;
856 410 my $context = shift;
857 410 my $st = shift;
858 410 my $pred = $st->predicate;
859 410 ▶ return unless (blessed($context));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($context) 0 410 410
860 410 my $query = $context->query;
861 410 my @return_plans;
862 410 ▶ if (blessed($query) and $pred->isa('RDF::Trine::Node::Resource') and scalar(@{ $query->get_computed_statement_generators( $st->predicate->uri_value ) })) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($query) and $pred->isa("RDF::Trine::Node::Resource") and scalar @{$query->get_computed_statement_generators($st->predicate->uri_value);}) 2 408 410
Condition80%4/5
builtin::blessed($query) and $pred->isa("RDF::Trine::Node::Resource") and scalar @{$query->get_computed_statement_generators($st->predicate->uri_value);}
!ll
4406
builtin::blessed($query) and $pred->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
04406
MC/DC50%1/2
builtin::blessed($query) and $pred->isa("RDF::Trine::Node::Resource") and scalar @{$query->get_computed_statement_generators($st->predicate->uri_value);}
builtin::blessed($query) $pred->isa("RDF::Trine::Node::Resource")
Truth table66%2/3
A and B
A= builtin::blessed($query)B= $pred->isa("RDF::Trine::Node::Resource")
ABresult
0X0
100
111
863 2 my $csg = $query->get_computed_statement_generators( $pred->uri_value );
864 2 my @nodes = $st->nodes;
865 2 ▶ my $quad = (scalar(@nodes) == 4) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @nodes == 4 ? : 0 2 2
866 2 my $mp = RDF::Query::Plan::ComputedStatement->new( @nodes[0..3], $quad );
867 2 push(@return_plans, $mp);
868 }
869 410 return @return_plans;
870 }
871  
872 27 ▶sub _join_plans {
Subroutine _join_plans: called CC=17 SCAR=31.1
873 27 my $self = shift;
874 27 my $context = shift;
875 27 my $triples = shift;
876 27 my %args = @_;
877 27 ▶ my $config = $context->options || {};
Condition50%1/2
$context->options || {}
l!l
270
MC/DC0%0/1
$context->options || {}
$context->options
Truth table50%1/2
A
A= $context->options
Aresult
11
00
878
879 27 my $method = $args{ method };
880 27 my @join_types = RDF::Query::Plan::Join->join_classes( $config );
881
882 27 my @plans;
883 27 my $opt = $context->optimize;
884 0 ▶ my @slice = ($opt) ? (0 .. $#{ $triples }) : (0);
Branch: 1 branch
BranchTrueFalseTotal
$opt ? : 0 27 27
885 27 foreach my $i (@slice) {
886 27 my @triples = @$triples;
887 # pick a triple to use as the LHS
888 27 my ($t) = splice( @triples, $i, 1 );
889
890 27 my @_lhs = $self->generate_plans( $t, $context, %args );
891 27 my @lhs_plans = map { [ $_, [$t] ] } @_lhs;
892 27 ▶ if (@triples) {
Branch: 1 branch
BranchTrueFalseTotal
if (@triples) { } 14 13 27
893 14 my @rhs_plans = $self->_join_plans( $context, \@triples, %args );
894 14 foreach my $i (0 .. $#lhs_plans) {
895 14 foreach my $j (0 .. $#rhs_plans) {
896 14 my $a = $lhs_plans[ $i ][0];
897 14 my $b = $rhs_plans[ $j ][0];
898 14 my $algebra_a = $lhs_plans[ $i ][1];
899 14 my $algebra_b = $rhs_plans[ $j ][1];
900 14 ▶ Carp::confess 'no lhs for join: ' . Dumper(\@lhs_plans) unless (blessed($a));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($a) 0 14 14
901 14 ▶ Carp::confess 'no rhs for join: ' . Dumper(\@triples, \@rhs_plans) unless (blessed($b));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($b) 0 14 14
902 14 foreach ($algebra_a, $algebra_b) {
903 28 ▶ unless (ref($_) and reftype($_) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_ and builtin::reftype($_) eq "ARRAY") 0 28 28
Condition33%1/3
ref $_ and builtin::reftype($_) eq "ARRAY"
!ll&&!rl&&r
0028
MC/DC0%0/2
ref $_ and builtin::reftype($_) eq "ARRAY"
ref $_ builtin::reftype($_) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $_B= builtin::reftype($_) eq "ARRAY"
ABresult
0X0
100
111
904 0 Carp::cluck Dumper($_)
905 }
906 }
907 14 foreach my $join_type (@join_types) {
908 28 ▶ next if ($join_type eq 'RDF::Query::Plan::Join::PushDownNestedLoop' and $b->subplans_of_type('RDF::Query::Plan::Service'));
Branch: 1 branch
BranchTrueFalseTotal
if $join_type eq "RDF::Query::Plan::Join::PushDownNestedLoop" and $b->subplans_of_type("RDF::Query::Plan::Service") 0 28 28
Condition66%2/3
$join_type eq "RDF::Query::Plan::Join::PushDownNestedLoop" and $b->subplans_of_type("RDF::Query::Plan::Service")
!ll&&!rl&&r
14140
MC/DC0%0/2
$join_type eq "RDF::Query::Plan::Join::PushDownNestedLoop" and $b->subplans_of_type("RDF::Query::Plan::Service")
$join_type eq "RDF::Query::Plan::Join::PushDownNestedLoop" $b->subplans_of_type("RDF::Query::Plan::Service")
Truth table66%2/3
A and B
A= $join_type eq "RDF::Query::Plan::Join::PushDownNestedLoop"B= $b->subplans_of_type("RDF::Query::Plan::Service")
ABresult
0X0
100
111
909 28 ▶ try {
Subroutine __ANON__: called CC=4 SCAR=15.4
910 28 my @algebras;
911 28 foreach ($algebra_a, $algebra_b) {
912 56 ▶ if (reftype($_) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($_) eq "ARRAY") 56 0 56
913 56 push(@algebras, @$_);
914 }
915 }
916 28 my %logging_keys;
917 28 ▶ if ($method eq 'triples') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'triples') { } 0 28 28
918 0 my $bgp = RDF::Query::Algebra::BasicGraphPattern->new( @algebras );
919 0 my $sparql = $bgp->as_sparql;
920 0 my $bf = $bgp->bf;
921 0 $logging_keys{ bf } = $bf;
922 0 $logging_keys{ sparql } = $sparql;
923 } else {
924 28 my $ggp = RDF::Query::Algebra::GroupGraphPattern->new( @algebras );
925 28 my $sparql = $ggp->as_sparql;
926 28 $logging_keys{ sparql } = $sparql;
927 }
928 28 my $plan = $join_type->new( $b, $a, 0, \%logging_keys );
929 27 push( @plans, [ $plan, [ @algebras ] ] );
930 1 ▶ } catch RDF::Query::Error::MethodInvocationError with {
Subroutine __ANON__: called CC=1 SCAR=0.0
931 # warn "caught MethodInvocationError.";
932 28 };
933 }
934 }
935 }
936 } else {
937 13 @plans = @lhs_plans;
938 }
939 }
940
941 27 ▶ if ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) { } 0 27 27
942 0 return @plans;
943 } else {
944 27 ▶ if (@plans) {
Branch: 1 branch
BranchTrueFalseTotal
if (@plans) { } 27 0 27
945 27 return $plans[0]; # XXX need to figure out what's the 'best' plan here
946 } else {
947 0 return;
948 }
949 }
950 }
951  
952 3 ▶sub _add_constant_join {
Subroutine _add_constant_join: called CC=5 SCAR=16.1
953 3 my $self = shift;
954 3 my $context = shift;
955 3 my $constant = shift;
956 3 my @return_plans = @_;
957 3 ▶ my $config = $context->options || {};
Condition50%1/2
$context->options || {}
l!l
30
MC/DC0%0/1
$context->options || {}
$context->options
Truth table50%1/2
A
A= $context->options
Aresult
11
00
958 3 my @join_types = RDF::Query::Plan::Join->join_classes( $config );
959 3 ▶ while (my $const = shift(@$constant)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $const = shift @$constant) 3 3 6
960 3 my @plans = splice(@return_plans);
961 3 foreach my $p (@plans) {
962 3 foreach my $join_type (@join_types) {
963 6 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
964 6 my $plan = $join_type->new( $p, $const );
965 6 push( @return_plans, $plan );
966 0 ▶ } catch RDF::Query::Error::MethodInvocationError with {
Subroutine __ANON__: not called CC=1 SCAR=0.0
967 # warn "caught MethodInvocationError.";
968 6 };
969 }
970 }
971 }
972 3 return @return_plans;
973 }
974  
975 5 ▶sub _path_plans {
Subroutine _path_plans: called CC=3 SCAR=11.1
976 5 my $self = shift;
977 5 my $algebra = shift;
978 5 my $context = shift;
979 5 my %args = @_;
980 5 my $path = $algebra->path;
981 5 my $start = $algebra->start;
982 5 my $end = $algebra->end;
983 5 for ($start, $end) {
984 10 ▶ if ($_->isa('RDF::Query::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->isa("RDF::Query::Node::Blank")) 0 10 10
985 0 $_ = $_->make_distinguished_variable;
986 }
987 }
988
989 5 my $npath = $self->_normalize_path( $path );
990 5 return $self->__path_plan( $start, $npath, $end, $args{ active_graph }, $context, %args );
991 }
992  
993 17 ▶sub _normalize_path {
Subroutine _normalize_path: called CC=9 SCAR=27.7
994 17 my $self = shift;
995 17 my $path = shift;
996 17 ▶ if (blessed($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($path)) 10 7 17
997 10 return $path;
998 }
999
1000 7 my $op = $path->[0];
1001 7 my @nodes = map { $self->_normalize_path($_) } @{ $path }[ 1 .. $#{ $path } ];
1002 7 ▶ if ($op eq '0-') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '0-') { } 0 7 7
elsif ($op eq '1-') { } 0 7 7
elsif ($op eq '0-1') { } 0 7 7
elsif ($op =~ /^-\d+$/) { } 0 7 7
1003 0 $op = '*';
1004 } elsif ($op eq '1-') {
1005 0 $op = '+';
1006 } elsif ($op eq '0-1') {
1007 0 $op = '?';
1008 } elsif ($op =~ /^-\d+$/) {
1009 0 $op = "0$op";
1010 }
1011
1012 7 ▶ if ($op eq '!') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "!") 0 7 7
1013 # re-order the nodes so that forward predicates come first, followed by backwards predicates
1014 # !(:fwd1|:fwd2|:fwd3|^:bkw1|^:bkw2|^:bkw3)
1015 0 ▶ @nodes = sort { blessed($a) ? -1 : (($a->[0] eq '^') ? 1 : -1) } @nodes;
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($a) ? : 0 0 0
$RDF::Query::Plan::a->[0] eq '^' ? : 0 0 0
1016 }
1017
1018 7 return [$op, @nodes];
1019 }
1020  
1021 47 ▶sub __path_plan {
Subroutine __path_plan: called CC=33 SCAR=60.3
1022 47 my $self = shift;
1023 47 my $start = shift;
1024 47 my $path = shift;
1025 47 my $end = shift;
1026 47 my $graph = shift;
1027 47 my $context = shift;
1028 47 my %args = @_;
1029 47 my $distinct = 1; #$args{distinct} ? 1 : 0;
1030 47 ▶ my $config = $context->options || {};
Condition50%1/2
$context->options || {}
l!l
470
MC/DC0%0/1
$context->options || {}
$context->options
Truth table50%1/2
A
A= $context->options
Aresult
11
00
1031 47 my $l = Log::Log4perl->get_logger("rdf.query.plan.path");
1032
1033
1034 # _simple_path will return an algebra object if the path can be expanded
1035 # into a simple basic graph pattern (for fixed-length paths)
1036 47 ▶ if (my $a = $self->_simple_path( $start, $path, $end, $graph )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = $self->_simple_path($start, $path, $end, $graph)) 43 4 47
1037 43 my ($plan) = $self->generate_plans( $a, $context, %args, prevent_distinguishing_bnodes => 1 );
1038 43 $l->trace('expanded path to pattern: ' . $plan->sse);
1039 43 return $plan;
1040 }
1041
1042
1043 4 ▶ if (blessed($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($path)) 0 4 4
1044 ### X iri Y
1045 # $path is a resource object: this is a triple (a path of length 1)
1046 0 my $s = $start;
1047 0 my $e = $end;
1048 0 ▶ my $algebra = $graph
Branch: 1 branch
BranchTrueFalseTotal
$graph ? : 0 0 0
1049 ? RDF::Query::Algebra::Quad->new( $s, $path, $e, $graph )
1050 : RDF::Query::Algebra::Triple->new( $s, $path, $e );
1051 0 my ($plan) = $self->generate_plans( $algebra, $context, %args, prevent_distinguishing_bnodes => 1 );
1052 0 $l->trace('expanded path to pattern: ' . $plan->sse);
1053 0 return $plan;
1054 }
1055
1056 4 my ($op, @nodes) = @$path;
1057
1058 # if ($op eq 'DISTINCT') {
1059 # return $self->__path_plan( $start, $nodes[0], $end, $graph, $context, %args, distinct => 1 );
1060 # }
1061 4 ▶ if ($op eq '!') {
Branch: 11 branches
BranchTrueFalseTotal
if ($op eq '!') { } 0 4 4
elsif ($op eq '^') { } 0 4 4
elsif ($op eq '/') { } 2 2 4
elsif ($op eq '|') { } 0 2 2
elsif ($op eq '?') { } 0 2 2
elsif ($op eq '*') { } 1 1 2
elsif ($op eq '+') { } 1 0 1
elsif ($op eq '0') { } 0 0 0
elsif ($op =~ /^\d+$/) { } 0 0 0
elsif ($op =~ /^(\d+)-(\d+)$/) { } 0 0 0
elsif ($op =~ /^(\d+)-$/) { } 0 0 0
1062 0 my $total = scalar(@nodes);
1063 0 ▶ my $neg = scalar(@{ [ grep { not(blessed($_)) and $_->[0] eq '^' } @nodes ] });
Condition0%0/3
!builtin::blessed($_) && $_->[0] eq '^'
!ll&&!rl&&r
000
MC/DC0%0/2
!builtin::blessed($_) && $_->[0] eq '^'
!builtin::blessed($_) $_->[0] eq '^'
Truth table0%0/3
not(A) && B
A= !builtin::blessed($_)B= $_->[0] eq '^'
ABresult
0X0
100
111
1064 0 my $pos = $total - $neg;
1065 0 ▶ if ($pos == $total) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pos == $total) { } 0 0 0
elsif ($neg == $total) { } 0 0 0
1066 ### X !(:iri1|...|:irin) Y
1067 0 return RDF::Query::Plan::Path->new( 'NegatedPropertySet', $start, [@nodes], $end, $graph, $distinct, %args );
1068 } elsif ($neg == $total) {
1069 ### X !(^:iri1|...|^:irin)Y
1070 0 my @preds = map { $_->[1] } @nodes;
1071 0 return $self->__path_plan($start, ['^', ['!', @preds]], $end, $graph, $context, %args);
1072 } else {
1073 ### X !(:iri1|...|:irii|^:irii+1|...|^:irim) Y
1074 0 my @fwd = grep { blessed($_) } @nodes;
1075 0 my @bwd = grep { not(blessed($_)) } @nodes;
1076 0 my $fplan = $self->__path_plan($start, ['!', @fwd], $end, $graph, $context, %args);
1077 0 my $bplan = $self->__path_plan($start, ['!', @bwd], $end, $graph, $context, %args);
1078 0 return RDF::Query::Plan::Union->new($fplan, $bplan);
1079 }
1080 } elsif ($op eq '^') {
1081 ### X ^path Y
1082 0 return $self->__path_plan( $end, $nodes[0], $start, $graph, $context, %args);
1083 } elsif ($op eq '/') {
1084 2 my $count = scalar(@nodes);
1085 2 ▶ if ($count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) { } 0 2 2
1086 0 return $self->__path_plan( $start, $nodes[0], $end, $graph, $context, %args );
1087 } else {
1088 2 my $joinvar = RDF::Query::Node::Variable->new();
1089 2 my @plans = $self->__path_plan( $start, $nodes[0], $joinvar, $graph, $context, %args );
1090 2 foreach my $i (2 .. $count) {
1091 2 ▶ my $endvar = ($i == $count) ? $end : RDF::Query::Node::Variable->new();
Branch: 1 branch
BranchTrueFalseTotal
$i == $count ? : 2 0 2
1092 2 my ($rhs) = $self->__path_plan( $joinvar, $nodes[$i-1], $endvar, $graph, $context, %args );
1093 2 push(@plans, $rhs);
1094 2 $joinvar = $endvar;
1095 }
1096 2 my @join_types = RDF::Query::Plan::Join->join_classes( $config );
1097 2 my @jplans;
1098 2 foreach my $jclass (@join_types) {
1099 4 push(@jplans, $jclass->new( @plans[0,1], 0 ));
1100 }
1101 2 $l->trace("expanded /-path to: " . $jplans[0]->sse);
1102 2 return $jplans[0];
1103 }
1104 } elsif ($op eq '|') {
1105 ### X path1 | path2 Y
1106 0 my @plans = map { $self->__path_plan($start, $_, $end, $graph, $context, %args) } @nodes;
1107 0 return RDF::Query::Plan::Union->new(@plans);
1108 } elsif ($op eq '?') {
1109 ### X path? Y
1110 0 my $upath = $nodes[0];
1111 0 my $zplan = $self->__path_plan($start, ['0', $upath], $end, $graph, $context, %args );
1112 0 my $oplan = $self->__path_plan($start, $upath, $end, $graph, $context, %args);
1113
1114 # project away any non-distinguished variables introduced by plan-to-bgp expansion
1115 0 ▶ my @vars = grep { blessed($_) and $_->isa('RDF::Query::Node::Variable') } ($start, $end);
Condition0%0/3
builtin::blessed($_) && $_->isa('RDF::Query::Node::Variable')
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) && $_->isa('RDF::Query::Node::Variable')
builtin::blessed($_) $_->isa('RDF::Query::Node::Variable')
Truth table0%0/3
A && B
A= builtin::blessed($_)B= $_->isa('RDF::Query::Node::Variable')
ABresult
0X0
100
111
1116 0 my $odplan = RDF::Query::Plan::Project->new( $oplan, \@vars );
1117
1118 0 my $pplan = RDF::Query::Plan::Union->new($zplan, $odplan);
1119
1120 # distinct the results
1121 0 my $plan = RDF::Query::Plan::Distinct->new( $pplan );
1122 0 return $plan;
1123 } elsif ($op eq '*') {
1124 ### X path* Y
1125 1 return RDF::Query::Plan::Path->new( 'ZeroOrMorePath', $start, $nodes[0], $end, $graph, $distinct, %args );
1126 } elsif ($op eq '+') {
1127 ### X path+ Y
1128 1 return RDF::Query::Plan::Path->new( 'OneOrMorePath', $start, $nodes[0], $end, $graph, $distinct, %args );
1129 } elsif ($op eq '0') {
1130 ### X path{0} Y
1131 0 return RDF::Query::Plan::Path->new( 'ZeroLengthPath', $start, $nodes[0], $end, $graph, $distinct, %args );
1132 } elsif ($op =~ /^\d+$/) {
1133 ### X path{n} Y where n > 0
1134 0 my $count = $op;
1135 0 ▶ if ($count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) { } 0 0 0
1136 0 return $self->__path_plan( $start, $nodes[0], $end, $graph, $context, %args );
1137 } else {
1138 0 my $joinvar = RDF::Query::Node::Variable->new();
1139 0 my @plans = $self->__path_plan( $start, $nodes[0], $joinvar, $graph, $context, %args );
1140 0 foreach my $i (2 .. $count) {
1141 0 ▶ my $endvar = ($i == $count) ? $end : RDF::Query::Node::Variable->new();
Branch: 1 branch
BranchTrueFalseTotal
$i == $count ? : 0 0 0
1142 0 my ($rhs) = $self->__path_plan( $joinvar, $nodes[0], $endvar, $graph, $context, %args );
1143 0 push(@plans, $rhs);
1144 0 $joinvar = $endvar;
1145 }
1146 0 my @join_types = RDF::Query::Plan::Join->join_classes( $config );
1147 0 my @jplans;
1148
1149 0 my @plan = shift(@plans);
1150 0 ▶ while (@plans) {
Branch: 1 branch
BranchTrueFalseTotal
if (@plans) 0 0 0
1151 0 my $q = shift(@plans);
1152 0 my @p;
1153 0 foreach my $p (@plan) {
1154 0 foreach my $jclass (@join_types) {
1155 0 push(@p, $jclass->new( $p, $q, 0 ));
1156 }
1157 }
1158 0 @plan = @p;
1159 }
1160 0 return $plan[0];
1161 }
1162 } elsif ($op =~ /^(\d+)-(\d+)$/) {
1163 ### X path{n,m} Y
1164 0 my ($n,$m) = split('-', $op, 2);
1165 # warn "$1- to $2-length path";
1166 0 my @range = sort { $a <=> $b } ($n, $m);
1167 0 my $from = $range[0];
1168 0 my $to = $range[1];
1169 0 my @plans;
1170 0 foreach my $i ($from .. $to) {
1171 0 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
1172 0 push(@plans, $self->__path_plan($start, ['0', []], $end, $graph, $context, %args ));
1173 } else {
1174 0 push(@plans, $self->__path_plan( $start, [$i, $nodes[0]], $end, $graph, $context, %args ));
1175 }
1176 }
1177 0 ▶ while (scalar(@plans) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @plans > 1) 0 0 0
1178 0 my $lhs = shift(@plans);
1179 0 my $rhs = shift(@plans);
1180 0 unshift(@plans, RDF::Query::Plan::Union->new( $lhs, $rhs ));
1181 }
1182 0 return $plans[0];
1183 } elsif ($op =~ /^(\d+)-$/) {
1184 ### X path{n,} Y where n > 0
1185 0 my ($min) = split('-', $op);
1186 # expand :p{n,} into :p{n}/:p*
1187 0 my $path = [ '/', [ $1, @nodes ], [ '*', @nodes ] ];
1188 0 my $plan = $self->__path_plan( $start, $path, $end, $graph, $context, %args );
1189 0 return $plan;
1190 } else {
1191 0 throw RDF::Query::Error -text => "Cannot generate plan for unknown path type $op";
1192 }
1193 }
1194  
1195 55 ▶sub _simple_path {
Subroutine _simple_path: called CC=16 SCAR=29.7
1196 55 my $self = shift;
1197 55 my $start = shift;
1198 55 my $path = shift;
1199 55 my $end = shift;
1200 55 my $graph = shift;
1201 55 ▶ if (blessed($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($path)) 46 9 55
1202 46 ▶ return ($graph)
Branch: 1 branch
BranchTrueFalseTotal
$graph ? : 42 4 46
1203 ? RDF::Query::Algebra::Quad->new( $start, $path, $end, $graph )
1204 : RDF::Query::Algebra::Triple->new( $start, $path, $end );
1205 }
1206 9 ▶ return unless (reftype($path) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($path) eq "ARRAY" 0 9 9
1207 9 my $op = $path->[0];
1208 9 ▶ if ($op eq '/') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq '/') { } 5 4 9
elsif ($op eq '^' and scalar @$path == 2 and builtin::blessed($path->[1])) { } 0 4 4
elsif ($op =~ /^\d+$/ and $op == 1) { } 0 8 8
1209 5 my @patterns;
1210 5 my @jvars = map { RDF::Query::Node::Variable->new() } (2 .. $#{ $path });
1211 5 foreach my $i (1 .. $#{ $path }) {
1212 8 ▶ my $s = ($i == 1) ? $start : $jvars[ $i-2 ];
Branch: 1 branch
BranchTrueFalseTotal
$i == 1 ? : 5 3 8
1213 8 ▶ my $e = ($i == $#{ $path }) ? $end : $jvars[ $i-1 ];
Branch: 1 branch
BranchTrueFalseTotal
$i == $#{$path;} ? : 3 5 8
1214 8 my $triple = $self->_simple_path( $s, $path->[ $i ], $e, $graph );
1215 8 ▶ return unless ($triple);
Branch: 1 branch
BranchTrueFalseTotal
unless $triple 2 6 8
1216 6 push(@patterns, $triple);
1217 }
1218 3 ▶ my @triples = map { $_->isa('RDF::Query::Algebra::BasicGraphPattern') ? $_->triples : $_ } @patterns;
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('RDF::Query::Algebra::BasicGraphPattern') ? : 0 6 6
1219 3 return RDF::Query::Algebra::BasicGraphPattern->new( @triples );
1220 ▶ } elsif ($op eq '^' and scalar(@$path) == 2 and blessed($path->[1])) {
Condition33%2/6
$op eq "^" and scalar @$path == 2 and builtin::blessed($path->[1])
!ll&&!rl&&r
400
$op eq "^" and scalar @$path == 2
!ll&&!rl&&r
400
MC/DC0%0/3
$op eq "^" and scalar @$path == 2 and builtin::blessed($path->[1])
$op eq "^" scalar @$path == 2 builtin::blessed($path->[1])
Truth table25%1/4
A and B and C
A= $op eq "^"B= scalar @$path == 2C= builtin::blessed($path->[1])
ABCresult
0XX0
10X0
1100
1111
1221 0 ▶ return ($graph)
Branch: 1 branch
BranchTrueFalseTotal
$graph ? : 0 0 0
1222 ? RDF::Query::Algebra::Quad->new( $end, $path->[1], $start, $graph )
1223 : RDF::Query::Algebra::Triple->new( $end, $path->[1], $start );
1224 ▶ } elsif ($op =~ /^\d+$/ and $op == 1) {
Condition33%1/3
$op =~ /^\d+$/ and $op == 1
!ll&&!rl&&r
400
MC/DC0%0/2
$op =~ /^\d+$/ and $op == 1
$op =~ /^\d+$/ $op == 1
Truth table33%1/3
A and B
A= $op =~ /^\d+$/B= $op == 1
ABresult
0X0
100
111
1225 0 return $self->_simple_path( $start, $path->[1], $end, $graph );
1226 }
1227
1228 4 return;
1229 }
1230  
1231 =item C<< plan_node_name >>
1232  
1233 Returns the string name of this plan node, suitable for use in serialization.
1234  
1235 =cut
1236  
1237 sub plan_node_name;
1238  
1239 =item C<< plan_prototype >>
1240  
1241 Returns a list of scalar identifiers for the type of the content (children)
1242 nodes of this plan node. These identifiers are recognized:
1243  
1244 * 'A' - An RDF::Query::Algebra object
1245 * 'b' - A boolean integer value (0 or 1)
1246 * 'E' - An expression (either an RDF::Query::Expression object or an RDF node)
1247 * 'i' - An integer
1248 * 'J' - A valid Project node (an RDF::Query::Expression object or an Variable node)
1249 * 'N' - An RDF node
1250 * 'P' - A RDF::Query::Plan object
1251 * 'q' - A RDF::Query object
1252 * 'Q' - An RDF::Trine::Statement::Quad object
1253 * 's' - A string
1254 * 'T' - An RDF::Trine::Statement object
1255 * 'u' - A valid URI string
1256 * 'V' - A variable binding set (an object of type RDF::Query::VariableBindings)
1257 * 'w' - A bareword string
1258 * 'W' - An RDF node or wildcard ('*')
1259 * '*X' - A list of X nodes (where X is another identifier scalar)
1260 * '\X' - An array reference of X nodes (where X is another identifier scalar)
1261  
1262 =cut
1263  
1264 sub plan_prototype;
1265  
1266 =item C<< plan_node_data >>
1267  
1268 Returns the data for this plan node that corresponds to the values described by
1269 the signature returned by C<< plan_prototype >>.
1270  
1271 =cut
1272  
1273 sub plan_node_data;
1274  
1275  
1276 =item C<< subplans_of_type ( $type [, $block] ) >>
1277  
1278 Returns a list of Plan objects matching C<< $type >> (tested with C<< isa >>).
1279 If C<< $block >> is given, then matching stops descending a subtree if the current
1280 node is of type C<< $block >>, continuing matching on other subtrees.
1281 This list includes the current plan object if it matches C<< $type >>, and is
1282 generated in infix order.
1283  
1284 =cut
1285  
1286 55 ▶sub subplans_of_type {
Subroutine subplans_of_type: called CC=7 SCAR=19.9
Pod documented
1287 55 my $self = shift;
1288 55 my $type = shift;
1289 55 my $block = shift;
1290
1291 55 ▶ return if ($block and $self->isa($block));
Branch: 1 branch
BranchTrueFalseTotal
if $block and $self->isa($block) 0 55 55
Condition33%1/3
$block and $self->isa($block)
!ll&&!rl&&r
5500
MC/DC0%0/2
$block and $self->isa($block)
$block $self->isa($block)
Truth table33%1/3
A and B
A= $blockB= $self->isa($block)
ABresult
0X0
100
111
1292
1293 55 my @patterns;
1294 55 ▶ push(@patterns, $self) if ($self->isa($type));
Branch: 1 branch
BranchTrueFalseTotal
if $self->isa($type) 0 55 55
1295
1296
1297 55 foreach my $p ($self->plan_node_data) {
1298 192 ▶ if (blessed($p) and $p->isa('RDF::Query::Plan')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($p) and $p->isa("RDF::Query::Plan")) 11 181 192
Condition100%3/3
builtin::blessed($p) and $p->isa("RDF::Query::Plan")
!ll&&!rl&&r
617511
MC/DC100%2/2
builtin::blessed($p) and $p->isa("RDF::Query::Plan")
builtin::blessed($p) $p->isa("RDF::Query::Plan")
Truth table100%3/3
A and B
A= builtin::blessed($p)B= $p->isa("RDF::Query::Plan")
ABresult
0X0
100
111
1299 11 push(@patterns, $p->subplans_of_type($type, $block));
1300 }
1301 }
1302 55 return @patterns;
1303 }
1304  
1305 35 1;
1306  
1307 __END__
« Query/Compiler/SQL.pm ↑ Summary Query/Node/Literal.pm »