Query/Plan/Quad.pm

filter: Statementstmt 75.0% 123 / 164 Branchbran 64.5% 31 / 48 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 85.7% 18 / 21 Podpod 100% 13 / 13 totaltotal 74.2% 193 / 260 CC 38 SCAR 22.2
CC
38
Cov
72%
bf
20.0
new
14.1
explain
12.0
CRAP 9.3
« Query/Plan/Join/PushDownNestedLoop.pm ↑ Summary Query/Algebra.pm »
1 # RDF::Query::Plan::Quad
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Plan::Quad - Executable query plan for Quads.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Plan::Quad 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> class.
16  
17 =over 4
18  
19 =cut
20  
21 package RDF::Query::Plan::Quad;
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);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 35 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 35 ▶use RDF::Query::ExecutionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 35 ▶use RDF::Query::VariableBindings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 ######################################################################
33  
34 35 our ($VERSION);
35 35 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
36 35 $VERSION = '2.919';
37 }
38  
39 ######################################################################
40  
41 =item C<< new ( @quad ) >>
42  
43 =cut
44  
45 132 ▶sub new {
Subroutine new: called CC=14 SCAR=26.5
Pod documented
46 132 my $class = shift;
47 132 my @quad = @_;
48 132 my $self = $class->SUPER::new( @quad );
49
50 ### the next two loops look for repeated variables because some backends
51 ### can't distinguish a pattern like { ?a ?a ?b }
52 ### from { ?a ?b ?c }. if we find repeated variables (there can be at most
53 ### two since there are only four nodes in a quad), we save the positions
54 ### in the quad that hold the variable(s), and the code in next() will filter
55 ### out any results that don't have the same value in those positions.
56 ###
57 ### in the first pass, we also set up the mapping that will let us pull out
58 ### values from the result quads to construct result bindings.
59
60 132 my %var_to_position;
61 132 my @methodmap = qw(subject predicate object context);
62 132 my %counts;
63 my @dup_vars;
64 132 foreach my $idx (0 .. 3) {
65 528 my $node = $quad[ $idx ];
66 528 ▶ if (blessed($node) and $node->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($node) and $node->isa("RDF::Trine::Node::Variable")) 186 342 528
Condition66%2/3
builtin::blessed($node) and $node->isa("RDF::Trine::Node::Variable")
!ll&&!rl&&r
0342186
MC/DC50%1/2
builtin::blessed($node) and $node->isa("RDF::Trine::Node::Variable")
builtin::blessed($node) $node->isa("RDF::Trine::Node::Variable")
Truth table66%2/3
A and B
A= builtin::blessed($node)B= $node->isa("RDF::Trine::Node::Variable")
ABresult
0X0
100
111
67 186 my $name = $node->name;
68 186 $var_to_position{ $name } = $methodmap[ $idx ];
69 186 $counts{ $name }++;
70 186 ▶ if ($counts{ $name } >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counts{$name} >= 2) 1 185 186
71 1 push(@dup_vars, $name);
72 }
73 }
74 }
75 132 $self->[0]{referenced_variables} = [ keys %counts ];
76
77 132 my %positions;
78 132 ▶ if (@dup_vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dup_vars) 1 131 132
79 1 foreach my $dup_var (@dup_vars) {
80 1 foreach my $idx (0 .. 3) {
81 4 my $var = $quad[ $idx ];
82 4 ▶ if (blessed($var) and ($var->isa('RDF::Trine::Node::Variable') or $var->isa('RDF::Trine::Node::Blank'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($var) and $var->isa("RDF::Trine::Node::Variable") || $var->isa("RDF::Trine::Node::Blank")) 3 1 4
Condition66%4/6
builtin::blessed($var) and $var->isa("RDF::Trine::Node::Variable") || $var->isa("RDF::Trine::Node::Blank")
!ll&&!rl&&r
013
$var->isa("RDF::Trine::Node::Variable") or $var->isa("RDF::Trine::Node::Blank")
l!l&&r!l&&!r
301
MC/DC33%1/3
builtin::blessed($var) and $var->isa("RDF::Trine::Node::Variable") || $var->isa("RDF::Trine::Node::Blank")
builtin::blessed($var) $var->isa("RDF::Trine::Node::Variable") $var->isa("RDF::Trine::Node::Blank")
Truth table50%2/4
A and B or C
A= builtin::blessed($var)B= $var->isa("RDF::Trine::Node::Variable")C= $var->isa("RDF::Trine::Node::Blank")
ABCresult
0XX0
1000
11X1
1011
83 3 ▶ my $name = ($var->isa('RDF::Trine::Node::Blank')) ? '__' . $var->blank_identifier : $var->name;
Branch: 1 branch
BranchTrueFalseTotal
$var->isa('RDF::Trine::Node::Blank') ? : 0 3 3
84 3 ▶ if ($name eq $dup_var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $dup_var) 2 1 3
85 2 push(@{ $positions{ $dup_var } }, $methodmap[ $idx ]);
86 }
87 }
88 }
89 }
90 }
91
92 132 $self->[0]{mappings} = \%var_to_position;
93
94 132 ▶ if (%positions) {
Branch: 1 branch
BranchTrueFalseTotal
if (%positions) 1 131 132
95 1 $self->[0]{dups} = \%positions;
96 }
97
98 132 return $self;
99 }
100  
101 =item C<< execute ( $execution_context ) >>
102  
103 =cut
104  
105 183 ▶sub execute ($) {
Subroutine execute: called CC=8 SCAR=20.9
Pod documented
106 183 my $self = shift;
107 183 my $context = shift;
108 183 $self->[0]{delegate} = $context->delegate;
109 183 ▶ if ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state == $self->OPEN) 0 183 183
110 0 throw RDF::Query::Error::ExecutionError -text => "QUAD plan can't be executed while already open";
111 }
112
113 183 my $l = Log::Log4perl->get_logger("rdf.query.plan.quad");
114 183 $l->trace( "executing RDF::Query::Plan::Quad:" );
115
116 183 my @quad = @{ $self }[ 1..4 ];
117 183 my $bound = $context->bound;
118 183 ▶ if (%$bound) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$bound) 86 97 183
119 86 foreach my $i (0 .. $#quad) {
120 344 ▶ next unless ($quad[$i]->isa('RDF::Trine::Node::Variable'));
Branch: 1 branch
BranchTrueFalseTotal
unless $quad[$i]->isa("RDF::Trine::Node::Variable") 184 160 344
121 160 ▶ next unless (blessed($bound->{ $quad[$i]->name }));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($$bound{$quad[$i]->name}) 75 85 160
122 85 $quad[ $i ] = $bound->{ $quad[$i]->name };
123 }
124 }
125
126 183 my $model = $context->model;
127
128 183 my @names = qw(subject predicate object context);
129 183 foreach my $i (0 .. 3) {
130 732 $l->trace( sprintf("- quad %10s: %s", $names[$i], $quad[$i]) );
131 }
132
133 183 my $iter = $model->get_statements( @quad[0..3] );
134 183 ▶ if (blessed($iter)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($iter)) { } 183 0 183
135 183 $l->trace("got quad iterator");
136 183 $self->[0]{iter} = $iter;
137 183 $self->[0]{bound} = $bound;
138 183 $self->state( $self->OPEN );
139 } else {
140 0 warn "no iterator in execute()";
141 }
142 183 $self;
143 }
144  
145 =item C<< next >>
146  
147 =cut
148  
149 340 ▶sub next {
Subroutine next: called CC=11 SCAR=24.6
Pod documented
150 340 my $self = shift;
151 340 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 340 340
152 0 throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open QUAD";
153 }
154 340 my $iter = $self->[0]{iter};
155
156 340 my $l = Log::Log4perl->get_logger("rdf.query.plan.quad");
157 340 $l->trace("next() called on Quad plan");
158 340 ▶ LOOP: while (my $row = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 315 162 477
159 315 $l->trace("got quad: " . $row->as_string);
160 315 ▶ if (my $data = $self->[0]{dups}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->[0]{"dups"}) 138 177 315
161 138 foreach my $pos (values %$data) {
162 138 my @pos = @$pos;
163 138 my $first_method = shift(@pos);
164 138 my $first = $row->$first_method();
165 138 foreach my $p (@pos) {
166 138 ▶ unless ($first->equal( $row->$p() )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first->equal($row->$p)) 137 1 138
167 35 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 137 $l->trace("Quad $first_method and $p didn't match: " . Dumper($first, $row->$p()));
169 137 next LOOP;
170 }
171 }
172 }
173 }
174
175 # if ($row->context->isa('RDF::Trine::Node::Nil')) {
176 # next;
177 # }
178
179 178 my $binding = {};
180 178 foreach my $key (keys %{ $self->[0]{mappings} }) {
181 299 my $method = $self->[0]{mappings}{ $key };
182 299 $binding->{ $key } = $row->$method();
183 }
184 178 my $pre_bound = $self->[0]{bound};
185 178 my $bindings = RDF::Query::VariableBindings->new( $binding );
186 178 ▶ if ($row->can('label')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->can("label")) 0 178 178
187 0 ▶ if (my $o = $row->label('origin')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $row->label("origin")) 0 0 0
188 0 $bindings->label( origin => [ $o ] );
189 }
190 }
191 178 @{ $bindings }{ keys %$pre_bound } = values %$pre_bound;
192 178 ▶ if (my $d = $self->delegate) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $self->delegate) 0 178 178
193 0 $d->log_result( $self, $bindings );
194 }
195 178 return $bindings;
196 }
197 162 $l->trace("No more quads");
198 162 return;
199 }
200  
201 =item C<< close >>
202  
203 =cut
204  
205 182 ▶sub close {
Subroutine close: called CC=2 SCAR=7.6
Pod documented
206 182 my $self = shift;
207 182 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 182 182
208 0 Carp::cluck;
209 0 throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open QUAD";
210 }
211 182 delete $self->[0]{iter};
212 182 delete $self->[0]{bound};
213 182 $self->SUPER::close();
214 }
215  
216 =item C<< nodes () >>
217  
218 =cut
219  
220 150 ▶sub nodes {
Subroutine nodes: called CC=1 SCAR=0.0
Pod documented
221 150 my $self = shift;
222 150 return @{ $self }[1,2,3,4];
223 }
224  
225 =item C<< bf () >>
226  
227 Returns a string representing the state of the nodes of the triple (bound or free).
228  
229 =cut
230  
231 0 ▶sub bf {
Subroutine bf: not called CC=4 SCAR=30.0
Pod documented
232 0 my $self = shift;
233 0 my $context = shift;
234 0 my $bf = '';
235 0 my $bound = $context->bound;
236 0 foreach my $n (@{ $self }[1,2,3,4]) {
237 0 ▶ if ($n->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isa('RDF::Trine::Node::Variable')) { } 0 0 0
238 0 ▶ if (my $b = $bound->{ $n->name }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $b = $$bound{$n->name}) { } 0 0 0
239 0 $bf .= 'b';
240 } else {
241 0 $bf .= 'f';
242 }
243 } else {
244 0 $bf .= 'b';
245 }
246 }
247 0 return $bf;
248 }
249  
250 =item C<< distinct >>
251  
252 Returns true if the pattern is guaranteed to return distinct results.
253  
254 =cut
255  
256 38 ▶sub distinct {
Subroutine distinct: called CC=1 SCAR=0.0
Pod documented
257 38 return 0;
258 }
259  
260 =item C<< ordered >>
261  
262 Returns true if the pattern is guaranteed to return ordered results.
263  
264 =cut
265  
266 29 ▶sub ordered {
Subroutine ordered: called CC=1 SCAR=0.0
Pod documented
267 29 return [];
268 }
269  
270  
271 =item C<< plan_node_name >>
272  
273 Returns the string name of this plan node, suitable for use in serialization.
274  
275 =cut
276  
277 110 ▶sub plan_node_name {
Subroutine plan_node_name: called CC=1 SCAR=0.0
Pod documented
278 110 return 'quad';
279 }
280  
281 =item C<< plan_prototype >>
282  
283 Returns a list of scalar identifiers for the type of the content (children)
284 nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable
285 identifiers.
286  
287 =cut
288  
289 110 ▶sub plan_prototype {
Subroutine plan_prototype: called CC=1 SCAR=0.0
Pod documented
290 110 my $self = shift;
291 110 return qw(N N N N);
292 }
293  
294 =item C<< plan_node_data >>
295  
296 Returns the data for this plan node that corresponds to the values described by
297 the signature returned by C<< plan_prototype >>.
298  
299 =cut
300  
301 150 ▶sub plan_node_data {
Subroutine plan_node_data: called CC=1 SCAR=0.0
Pod documented
302 150 my $self = shift;
303 150 return ($self->nodes);
304 }
305  
306 =item C<< explain >>
307  
308 Returns a string serialization of the query plan appropriate for display
309 on the command line.
310  
311 =cut
312  
313 0 ▶sub explain {
Subroutine explain: not called CC=3 SCAR=24.8
Pod documented
314 0 my $self = shift;
315 0 my ($s, $count) = (' ', 0);
316 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
317 0 $s = shift;
318 0 $count = shift;
319 }
320 0 my $indent = '' . ($s x $count);
321 0 my $type = $self->plan_node_name;
322 my $string = sprintf("%s%s (0x%x)\n", $indent, $type, refaddr($self))
323 . "${indent}${s}"
324 0 ▶ . join(' ', map { ($_->isa('RDF::Trine::Node::Nil')) ? "(nil)" : $_->as_sparql } $self->plan_node_data) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('RDF::Trine::Node::Nil') ? : 0 0 0
325 0 return $string;
326 }
327  
328 =item C<< graph ( $g ) >>
329  
330 =cut
331  
332 0 ▶sub graph {
Subroutine graph: not called CC=2 SCAR=17.9
Pod documented
333 0 my $self = shift;
334 0 my $g = shift;
335 0 $g->add_node( "$self", label => "Quad" . $self->graph_labels );
336 0 my @names = qw(subject predicate object context);
337 0 foreach my $i (0 .. 3) {
338 0 my $n = $self->[ $i + 1 ];
339 0 my $rel = $names[ $i ];
340 0 my $str = $n->sse( {}, '' );
341 0 $g->add_node( "${self}$n", label => $str );
342 0 $g->add_edge( "$self" => "${self}$n", label => $names[ $i ] );
343 }
344 0 return "$self";
345 }
346  
347 35 1;
348  
349 __END__
« Query/Plan/Join/PushDownNestedLoop.pm ↑ Summary Query/Algebra.pm »