Attean/QueryPlanner.pm

filter: Statementstmt 56.6% 377 / 665 Branchbran 38.0% 83 / 218 Conditioncond 23.5% 12 / 51 MC/DCmcdc 9.6% 3 / 31 Subroutinesub 89.1% 33 / 37 Podpod 100% 11 / 11 totaltotal 51.2% 519 / 1013 CC 161 SCAR 72.6
CC
161
Cov
51%
plans_for_algebra
2464.9
simplify_path
90.0
_package
20.0
CRAP 1420.3
« Attean/SimpleQueryEvaluator.pm ↑ Summary Test/Attean/W3CManifestTestSuite.pm »
1 51 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Attean::QueryPlanner - Query planner
7  
8 =head1 VERSION
9  
10 This document describes Attean::QueryPlanner version 0.041
11  
12 =head1 SYNOPSIS
13  
14 use v5.14;
15 use Attean;
16 my $planner = Attean::QueryPlanner->new();
17 my $default_graphs = [ Attean::IRI->new('http://example.org/') ];
18 my $plan = $planner->plan_for_algebra( $algebra, $model, $default_graphs );
19 my $iter = $plan->evaluate($model);
20 while (my $result = $iter->next()) {
21 say $result->as_string;
22 }
23  
24 =head1 DESCRIPTION
25  
26 The Attean::QueryPlanner class is a base class implementing common behavior for
27 query planners. Subclasses will need to consume or compose the
28 L<Attean::API::JoinPlanner> role.
29  
30 Trivial sub-classes may consume L<Attean::API::NaiveJoinPlanner>, while more
31 complex planners may choose to implement complex join planning (e.g.
32 L<Attean::IDPQueryPlanner>).
33  
34 =head1 ATTRIBUTES
35  
36 =over 4
37  
38 =cut
39  
40 51 ▶use Attean::Algebra;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 51 ▶use Attean::Plan;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 51 ▶use Attean::Expression;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 51 package Attean::QueryPlanner 0.041 {
45 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 51 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 51 ▶ use Attean::RDF;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 51 ▶ use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 51 ▶ use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 51 ▶ use List::Util qw(all any);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 51 ▶ use Types::Standard qw(Int Bool ConsumerOf InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 51 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 51 ▶ use Algorithm::Combinatorics qw(subsets);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 51 ▶ use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 51 ▶ use Math::Cartesian::Product;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 51 with 'Attean::API::QueryPlanner', 'MooX::Log::Any';
59 51 has 'counter' => (is => 'rw', isa => Int, default => 0);
60 51 has 'table_threshold' => (is => 'rw', isa => Int, default => 10);
61
62 # use this to suppress the use of the query-defined dataset (e.g. in cases where the dataset is defined via protocol and implemented via a Model-layer wrapper)
63 51 has 'suppress_dataset' => (is => 'rw', isa => Bool, default => 0);
64  
65 =back
66  
67 =head1 METHODS
68  
69 =over 4
70  
71 =item C<< new_temporary( $type ) >>
72  
73 Returns a new unique (in the context of the query planner) ID string that may
74 be used for things like fresh (temporary) variables. The C<< $type >> string is
75 used in the generated name to aid in identifying different uses for the names.
76  
77 =cut
78  
79 15 ▶ sub new_temporary {
Subroutine new_temporary: called CC=1 SCAR=0.0
Pod documented
80 15 my $self = shift;
81 15 my $type = shift;
82 15 my $c = $self->counter;
83 15 $self->counter($c+1);
84 15 return sprintf('.%s-%d', $type, $c);
85 }
86  
87 =item C<< plan_for_algebra( $algebra, $model, \@active_graphs, \@default_graphs ) >>
88  
89 Returns the first plan returned from C<< plans_for_algebra >>.
90  
91 =cut
92  
93 26 ▶ sub plan_for_algebra {
Subroutine plan_for_algebra: called CC=1 SCAR=0.0
Pod documented
94 26 my $self = shift;
95 26 my @plans = $self->plans_for_algebra(@_);
96 25 return shift(@plans);
97 }
98
99 =item C<< plans_for_algebra( $algebra, $model, \@active_graphs, \@default_graphs ) >>
100  
101 Returns L<Attean::API::Plan> objects representing alternate query plans for
102 evaluating the query C<< $algebra >> against the C<< $model >>, using
103 the supplied C<< $active_graph >>.
104  
105 =cut
106  
107 134 ▶ sub plans_for_algebra {
Subroutine plans_for_algebra: called CC=113 SCAR=78.1
Pod documented
108 134 my $self = shift;
109 134 my $algebra = shift;
110 134 my $model = shift;
111 134 my $active_graphs = shift;
112 134 my $default_graphs = shift;
113 134 my %args = @_;
114
115 134 ▶ if ($model->does('Attean::API::CostPlanner')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->does("Attean::API::CostPlanner")) 134 0 134
116 134 my @plans = $model->plans_for_algebra($algebra, $self, $active_graphs, $default_graphs, %args);
117 134 ▶ if (@plans) {
Branch: 1 branch
BranchTrueFalseTotal
if (@plans) { } 2 132 134
118 2 return @plans; # trust that the model knows better than us what plans are best
119 } else {
120 132 $self->log->info("*** Model did not provide plans: $model");
121 }
122 }
123
124 132 ▶ Carp::confess "No algebra passed for evaluation" unless ($algebra);
Branch: 1 branch
BranchTrueFalseTotal
unless $algebra 0 132 132
125
126 # TODO: propagate interesting orders
127 132 my $interesting = [];
128
129 132 my @children = @{ $algebra->children };
130 132 my ($child) = $children[0];
131 132 ▶ if ($algebra->isa('Attean::Algebra::Query') or $algebra->isa('Attean::Algebra::Update')) {
Branch: 27 branches
BranchTrueFalseTotal
if ($algebra->isa('Attean::Algebra::Query') or $algebra->isa('Attean::Algebra::Update')) { } 8 124 132
elsif ($algebra->isa('Attean::Algebra::BGP')) { } 69 55 124
elsif ($algebra->isa('Attean::Algebra::Join')) { } 11 44 55
elsif ($algebra->isa('Attean::Algebra::Distinct') or $algebra->isa('Attean::Algebra::Reduced')) { } 6 38 44
elsif ($algebra->isa('Attean::Algebra::Filter')) { } 6 32 38
elsif ($algebra->isa('Attean::Algebra::OrderBy')) { } 3 29 32
elsif ($algebra->isa('Attean::Algebra::LeftJoin')) { } 0 29 29
elsif ($algebra->isa('Attean::Algebra::Minus')) { } 0 29 29
elsif ($algebra->isa('Attean::Algebra::Project')) { } 8 21 29
elsif ($algebra->isa('Attean::Algebra::Graph')) { } 10 11 21
elsif ($algebra->isa('Attean::Algebra::Table')) { } 0 11 11
elsif ($algebra->isa('Attean::Algebra::Service')) { } 4 7 11
elsif ($algebra->isa('Attean::Algebra::Slice')) { } 0 7 7
elsif ($algebra->isa('Attean::Algebra::Union')) { } 0 7 7
elsif ($algebra->isa('Attean::Algebra::Extend')) { } 1 6 7
elsif ($algebra->isa('Attean::Algebra::Group')) { } 1 5 6
elsif ($algebra->isa('Attean::Algebra::Ask')) { } 4 1 5
elsif ($algebra->isa('Attean::Algebra::Path')) { } 0 1 1
elsif ($algebra->isa('Attean::Algebra::Construct')) { } 0 1 1
elsif ($algebra->isa('Attean::Algebra::Describe')) { } 1 0 1
elsif ($algebra->isa('Attean::Algebra::Clear')) { } 0 0 0
elsif ($algebra->isa('Attean::Algebra::Add')) { } 0 0 0
elsif ($algebra->isa('Attean::Algebra::Modify')) { } 0 0 0
elsif ($algebra->isa('Attean::Algebra::Load')) { } 0 0 0
elsif ($algebra->isa('Attean::Algebra::Create')) { } 0 0 0
elsif ($algebra->isa('Attean::Algebra::Sequence')) { } 0 0 0
elsif ($algebra->isa('Attean::Algebra::Unfold')) { } 0 0 0
Condition66%2/3
$algebra->isa("Attean::Algebra::Query") or $algebra->isa("Attean::Algebra::Update")
l!l&&r!l&&!r
80124
MC/DC50%1/2
$algebra->isa("Attean::Algebra::Query") or $algebra->isa("Attean::Algebra::Update")
$algebra->isa("Attean::Algebra::Query") $algebra->isa("Attean::Algebra::Update")
Truth table66%2/3
A or B
A= $algebra->isa("Attean::Algebra::Query")B= $algebra->isa("Attean::Algebra::Update")
ABresult
1X1
011
000
132  
133 8 my @active_graphs = @$active_graphs;
134 8 my @default_graphs = @$default_graphs;
135
136 8 ▶ if ($algebra->can('dataset') and not $self->suppress_dataset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->can("dataset") and not $self->suppress_dataset) 8 0 8
Condition33%1/3
$algebra->can("dataset") and not $self->suppress_dataset
!ll&&!rl&&r
008
MC/DC0%0/2
$algebra->can("dataset") and not $self->suppress_dataset
$algebra->can("dataset") not $self->suppress_dataset
Truth table33%1/3
A and not(B)
A= $algebra->can("dataset")B= not $self->suppress_dataset
ABresult
0X0
100
111
137 8 my $dataset = $algebra->dataset;
138 8 ▶ my @default = @{ $dataset->{'default'} || [] };
Condition50%1/2
$dataset->{'default'} || []
l!l
08
MC/DC0%0/1
$dataset->{'default'} || []
$dataset->{'default'}
Truth table50%1/2
A
A= $dataset->{'default'}
Aresult
11
00
139 8 my $named = $dataset->{'named'};
140 8 ▶ my @named = (ref($named) eq 'ARRAY') ? @$named : (values %{ $named || {} });
Branch: 1 branch
BranchTrueFalseTotal
ref $named eq 'ARRAY' ? : 0 8 8
Condition50%1/2
$named || {}
l!l
08
MC/DC0%0/1
$named || {}
$named
Truth table50%1/2
A
A= $named
Aresult
11
00
141
142 8 ▶ if (scalar(@default) or scalar(@named)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @default or scalar @named) 0 8 8
Condition50%1/2
scalar @default or scalar @named
l!l
08
MC/DC0%0/1
scalar @default or scalar @named
scalar @default
Truth table50%1/2
A
A= scalar @default
Aresult
11
00
143 # change the available named graphs
144 # change the active graph(s)
145 0 @active_graphs = @default;
146 0 @default_graphs = @default;
147 0 $args{ available_graphs } = [@named];
148 }
149 }
150  
151 8 return $self->plans_for_algebra($algebra->child, $model, \@active_graphs, \@default_graphs, %args);
152 } elsif ($algebra->isa('Attean::Algebra::BGP')) {
153 69 my $triples = $algebra->triples;
154 69 my @triples = @$triples;
155 69 my %blanks;
156 69 $self->replace_blanks_with_variables(\%blanks, \@triples);
157 69 my $bgp = Attean::Algebra::BGP->new( triples => \@triples );
158 my @plans = $self->bgp_join_plans($bgp, $model, $active_graphs, $default_graphs, $interesting, map {
159 69 [$self->access_plans($model, $active_graphs, $_)]
160 } @triples);
161 68 return @plans;
162 } elsif ($algebra->isa('Attean::Algebra::Join')) {
163 return $self->group_join_plans($model, $active_graphs, $default_graphs, $interesting, map {
164 11 [$self->plans_for_algebra($_, $model, $active_graphs, $default_graphs, %args)]
165 } @children);
166 ▶ } elsif ($algebra->isa('Attean::Algebra::Distinct') or $algebra->isa('Attean::Algebra::Reduced')) {
Condition66%2/3
$algebra->isa("Attean::Algebra::Distinct") or $algebra->isa("Attean::Algebra::Reduced")
l!l&&r!l&&!r
6038
MC/DC50%1/2
$algebra->isa("Attean::Algebra::Distinct") or $algebra->isa("Attean::Algebra::Reduced")
$algebra->isa("Attean::Algebra::Distinct") $algebra->isa("Attean::Algebra::Reduced")
Truth table66%2/3
A or B
A= $algebra->isa("Attean::Algebra::Distinct")B= $algebra->isa("Attean::Algebra::Reduced")
ABresult
1X1
011
000
167 6 my @plans = $self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args);
168 6 my @dist;
169 6 foreach my $p (@plans) {
170 38 ▶ if ($p->distinct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->distinct) { } 12 26 38
171 12 push(@dist, $p);
172 } else {
173 26 my @vars = @{ $p->in_scope_variables };
174 26 my $cmps = $p->ordered;
175 26 ▶ if ($self->_comparators_are_stable_and_cover_vars($cmps, @vars)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_comparators_are_stable_and_cover_vars($cmps, @vars)) { } 0 26 26
176 # the plan has a stable ordering which covers all the variables, so we can just uniq the iterator
177 0 push(@dist, Attean::Plan::Unique->new(children => [$p], distinct => 1, ordered => $p->ordered));
178 } else {
179 # TODO: if the plan isn't distinct, but is ordered, we can use a batched implementation
180 26 push(@dist, Attean::Plan::HashDistinct->new(children => [$p], distinct => 1, ordered => $p->ordered));
181 }
182 }
183 }
184 6 return @dist;
185 } elsif ($algebra->isa('Attean::Algebra::Filter')) {
186 # TODO: simple range relation filters can be handled differently if that filter operates on a variable that is part of the ordering
187 6 my $expr = $algebra->expression;
188 6 my $w = Attean::TreeRewriter->new(types => ['Attean::API::DirectedAcyclicGraph']);
189 12 ▶ $w->register_pre_handler(sub {
Subroutine __ANON__: called CC=3 SCAR=17.9
190 12 my ($t, $parent, $thunk) = @_;
191 12 ▶ if ($t->isa('Attean::ExistsExpression')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->isa("Attean::ExistsExpression")) 0 12 12
192 0 my $pattern = $t->pattern;
193 0 my $plan = $self->plan_for_algebra($pattern, $model, $active_graphs, $default_graphs, @_);
194 0 ▶ unless ($plan->does('Attean::API::BindingSubstitutionPlan')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plan->does("Attean::API::BindingSubstitutionPlan")) 0 0 0
195 0 die 'Exists plan does not consume Attean::API::BindingSubstitutionPlan: ' . $plan->as_string;
196 }
197 0 my $new = Attean::ExistsPlanExpression->new(
198 plan => $plan,
199 );
200 0 return (1, 0, $new);
201 }
202 12 return (0, 1, $t);
203 6 });
204 6 my ($changed, $rewritten) = $w->rewrite($expr, {});
205 6 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 0 6 6
206 0 $expr = $rewritten;
207 }
208
209 6 my $var = $self->new_temporary('filter');
210 6 my %exprs = ($var => $expr);
211
212 6 my @plans;
213 6 foreach my $plan ($self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args)) {
214 6 my $distinct = $plan->distinct;
215 6 my $ordered = $plan->ordered;
216 6 ▶ if ($expr->isa('Attean::ValueExpression') and $expr->value->does('Attean::API::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->isa('Attean::ValueExpression') and $expr->value->does('Attean::API::Variable')) { } 3 3 6
Condition66%2/3
$expr->isa("Attean::ValueExpression") and $expr->value->does("Attean::API::Variable")
!ll&&!rl&&r
303
MC/DC50%1/2
$expr->isa("Attean::ValueExpression") and $expr->value->does("Attean::API::Variable")
$expr->isa("Attean::ValueExpression") $expr->value->does("Attean::API::Variable")
Truth table66%2/3
A and B
A= $expr->isa("Attean::ValueExpression")B= $expr->value->does("Attean::API::Variable")
ABresult
0X0
100
111
217 3 my $filtered = Attean::Plan::EBVFilter->new(children => [$plan], variable => $expr->value->value, distinct => $distinct, ordered => $ordered);
218 3 push(@plans, $filtered);
219 } else {
220 3 my @vars = ($var);
221 3 my @inscope = ($var, @{ $plan->in_scope_variables });
222 3 my @pvars = map { Attean::Variable->new($_) } @{ $plan->in_scope_variables };
223 3 my $extend = Attean::Plan::Extend->new(children => [$plan], expressions => \%exprs, distinct => 0, ordered => $ordered, active_graphs => $active_graphs);
224 3 my $filtered = Attean::Plan::EBVFilter->new(children => [$extend], variable => $var, distinct => 0, ordered => $ordered);
225 3 my $proj = $self->new_projection($filtered, $distinct, @{ $plan->in_scope_variables });
226 3 push(@plans, $proj);
227 }
228 }
229 6 return @plans;
230 } elsif ($algebra->isa('Attean::Algebra::OrderBy')) {
231 # TODO: no-op if already ordered
232 3 my @cmps = @{ $algebra->comparators };
233 3 my ($exprs, $ascending, $svars) = $self->_order_by($algebra);
234 3 my @plans;
235 3 foreach my $plan ($self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, interesting_order => $algebra->comparators, %args)) {
236 3 my $distinct = $plan->distinct;
237  
238 3 ▶ if (scalar(@cmps) == 1 and $cmps[0]->expression->isa('Attean::ValueExpression') and $cmps[0]->expression->value->does('Attean::API::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @cmps == 1 and $cmps[0]->expression->isa('Attean::ValueExpression') and $cmps[0]->expression->value->does('Attean::API::Variable')) { } 3 0 3
Condition33%2/6
scalar @cmps == 1 and $cmps[0]->expression->isa("Attean::ValueExpression") and $cmps[0]->expression->value->does("Attean::API::Variable")
!ll&&!rl&&r
003
scalar @cmps == 1 and $cmps[0]->expression->isa("Attean::ValueExpression")
!ll&&!rl&&r
003
MC/DC0%0/3
scalar @cmps == 1 and $cmps[0]->expression->isa("Attean::ValueExpression") and $cmps[0]->expression->value->does("Attean::API::Variable")
scalar @cmps == 1 $cmps[0]->expression->isa("Attean::ValueExpression") $cmps[0]->expression->value->does("Attean::API::Variable")
Truth table25%1/4
A and B and C
A= scalar @cmps == 1B= $cmps[0]->expression->isa("Attean::ValueExpression")C= $cmps[0]->expression->value->does("Attean::API::Variable")
ABCresult
0XX0
10X0
1100
1111
239 # TODO: extend this to handle more than one comparator, so long as they are *all* just variables (and not complex expressions)
240 # If we're sorting by just a variable name, don't bother creating new variables for the sort expressions, use the underlying variable directy
241 3 my @vars = @{ $plan->in_scope_variables };
242 3 my @pvars = map { Attean::Variable->new($_) } @{ $plan->in_scope_variables };
243 3 my $var = $cmps[0]->expression->value->value;
244 3 my $ascending = { $var => $cmps[0]->ascending };
245 3 my $ordered = Attean::Plan::OrderBy->new(children => [$plan], variables => [$var], ascending => $ascending, distinct => $distinct, ordered => \@cmps);
246 3 push(@plans, $ordered);
247 } else {
248 0 my @vars = (@{ $plan->in_scope_variables }, keys %$exprs);
249 0 my @pvars = map { Attean::Variable->new($_) } @{ $plan->in_scope_variables };
250 0 my $extend = Attean::Plan::Extend->new(children => [$plan], expressions => $exprs, distinct => 0, ordered => $plan->ordered, active_graphs => $active_graphs);
251 0 my $ordered = Attean::Plan::OrderBy->new(children => [$extend], variables => $svars, ascending => $ascending, distinct => 0, ordered => \@cmps);
252 0 my $proj = $self->new_projection($ordered, $distinct, @{ $plan->in_scope_variables });
253 0 push(@plans, $proj);
254 }
255 }
256
257 3 return @plans;
258 } elsif ($algebra->isa('Attean::Algebra::LeftJoin')) {
259 0 my $l = [$self->plans_for_algebra($children[0], $model, $active_graphs, $default_graphs, %args)];
260 0 my $r = [$self->plans_for_algebra($children[1], $model, $active_graphs, $default_graphs, %args)];
261 0 return $self->join_plans($model, $active_graphs, $default_graphs, $l, $r, 'left', $algebra->expression);
262 } elsif ($algebra->isa('Attean::Algebra::Minus')) {
263 0 my $l = [$self->plans_for_algebra($children[0], $model, $active_graphs, $default_graphs, %args)];
264 0 my $r = [$self->plans_for_algebra($children[1], $model, $active_graphs, $default_graphs, %args)];
265 0 return $self->join_plans($model, $active_graphs, $default_graphs, $l, $r, 'minus');
266 } elsif ($algebra->isa('Attean::Algebra::Project')) {
267 8 my $vars = $algebra->variables;
268 8 my @vars = map { $_->value } @{ $vars };
269 8 my $vars_key = join(' ', sort @vars);
270 8 my $distinct = 0;
271 my @plans = map {
272 8 ▶ ($vars_key eq join(' ', sort @{ $_->in_scope_variables }))
Branch: 1 branch
BranchTrueFalseTotal
$vars_key eq join(' ', sort(@{$_->in_scope_variables;})) ? : 7 7 14
273 ? $_ # no-op if plan is already properly-projected
274 : $self->new_projection($_, $distinct, @vars)
275 } $self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args);
276 8 return @plans;
277 } elsif ($algebra->isa('Attean::Algebra::Graph')) {
278 10 my $graph = $algebra->graph;
279 10 ▶ if ($graph->does('Attean::API::Term')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($graph->does('Attean::API::Term')) { } 5 5 10
280 5 ▶ if (my $available = $args{available_graphs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $available = $args{"available_graphs"}) 2 3 5
281 # the list of available graphs has been restricted, and this
282 # graph is not available so return an empty table plan.
283 2 ▶ unless (any { $_->equals($graph) } @$available) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_->equals($graph); } , @$available)) 1 1 2
284 1 my $plan = Attean::Plan::Table->new( variables => [], rows => [], distinct => 0, ordered => [] );
285 1 return $plan;
286 }
287 }
288 4 return $self->plans_for_algebra($child, $model, [$graph], $default_graphs, %args);
289 } else {
290 5 my $gvar = $graph->value;
291 5 my $graphs = $model->get_graphs;
292
293 5 my @plans;
294 5 my %vars = map { $_ => 1 } $child->in_scope_variables;
295 5 $vars{ $gvar }++;
296 5 my @vars = keys %vars;
297
298 5 my %available;
299 5 ▶ if (my $available = $args{available_graphs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $available = $args{"available_graphs"}) 2 3 5
300 2 foreach my $a (@$available) {
301 4 $available{ $a->value }++;
302 }
303 2 ▶ $graphs = $graphs->grep(sub { $available{ $_->value } });
Subroutine __ANON__: called CC=1 SCAR=0.0
304 }
305
306 5 my @branches;
307 # ignore default graphs unless they are listed as available named graphs
308 5 my %ignore = map { $_->value => 1 } grep { not exists $available{ $_->value} } @$default_graphs;
309 5 ▶ while (my $graph = $graphs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $graph = $graphs->next) 3 5 8
310 3 ▶ next if $ignore{ $graph->value };
Branch: 1 branch
BranchTrueFalseTotal
if $ignore{$graph->value} 0 3 3
311 3 my %exprs = ($gvar => Attean::ValueExpression->new(value => $graph));
312 # TODO: rewrite $child pattern here to replace any occurrences of the variable $gvar to $graph
313 my @plans = map {
314 3 Attean::Plan::Extend->new(children => [$_], expressions => \%exprs, distinct => 0, ordered => $_->ordered, active_graphs => $active_graphs);
315 } $self->plans_for_algebra($child, $model, [$graph], $default_graphs, %args);
316 3 push(@branches, \@plans);
317 }
318
319 5 ▶ if (scalar(@branches) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @branches == 1) { } 1 4 5
320 1 @plans = @{ shift(@branches) };
321 } else {
322 4 ▶ cartesian { push(@plans, Attean::Plan::Union->new(children => [@_], distinct => 0, ordered => [])) } @branches;
Subroutine __ANON__: called CC=1 SCAR=0.0
323 }
324 5 return @plans;
325 }
326 } elsif ($algebra->isa('Attean::Algebra::Table')) {
327 0 my $rows = $algebra->rows;
328 0 my $vars = $algebra->variables;
329 0 my @vars = map { $_->value } @{ $vars };
330
331 0 ▶ if (scalar(@$rows) < $self->table_threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$rows < $self->table_threshold) { } 0 0 0
332 0 return Attean::Plan::Table->new( variables => $vars, rows => $rows, distinct => 0, ordered => [] );
333 } else {
334 0 my $iter = Attean::ListIterator->new(
335 item_type => 'Attean::API::Result',
336 variables => \@vars,
337 values => $rows
338 );
339 0 return Attean::Plan::Iterator->new( iterator => $iter, distinct => 0, ordered => [] );
340 }
341 } elsif ($algebra->isa('Attean::Algebra::Service')) {
342 4 my $endpoint = $algebra->endpoint;
343 4 my $silent = $algebra->silent;
344 4 my $sparql = sprintf('SELECT * WHERE { %s }', $child->as_sparql);
345 4 my @vars = $child->in_scope_variables;
346 4 my $plan = Attean::Plan::Service->new(
347 request_signer => $self->request_signer,
348 endpoint => $endpoint,
349 silent => $silent,
350 sparql => $sparql,
351 distinct => 0,
352 in_scope_variables => \@vars,
353 ordered => []
354 );
355 4 return $plan;
356 } elsif ($algebra->isa('Attean::Algebra::Slice')) {
357 0 my $limit = $algebra->limit;
358 0 my $offset = $algebra->offset;
359 0 my @plans;
360 0 foreach my $plan ($self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args)) {
361 0 my $vars = $plan->in_scope_variables;
362 0 push(@plans, Attean::Plan::Slice->new(children => [$plan], limit => $limit, offset => $offset, distinct => $plan->distinct, ordered => $plan->ordered));
363 }
364 0 return @plans;
365 } elsif ($algebra->isa('Attean::Algebra::Union')) {
366 # TODO: if both branches are similarly ordered, we can use Attean::Plan::Merge to keep the resulting plan ordered
367 0 my @vars = keys %{ { map { map { $_ => 1 } $_->in_scope_variables } @children } };
368 0 my @plansets = map { [$self->plans_for_algebra($_, $model, $active_graphs, $default_graphs, %args)] } @children;
369  
370 0 my @plans;
371 0 ▶ cartesian {
Subroutine __ANON__: not called CC=1 SCAR=6.9
372 0 push(@plans, Attean::Plan::Union->new(children => \@_, distinct => 0, ordered => []))
373 0 } @plansets;
374 0 return @plans;
375 } elsif ($algebra->isa('Attean::Algebra::Extend')) {
376 1 my $var = $algebra->variable->value;
377 1 my $expr = $algebra->expression;
378 1 my %exprs = ($var => $expr);
379 1 my @vars = $algebra->in_scope_variables;
380  
381 1 my @plans;
382 1 foreach my $plan ($self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args)) {
383 4 my $extend = Attean::Plan::Extend->new(children => [$plan], expressions => \%exprs, distinct => 0, ordered => $plan->ordered, active_graphs => $active_graphs);
384 4 push(@plans, $extend);
385 }
386 1 return @plans;
387 } elsif ($algebra->isa('Attean::Algebra::Group')) {
388 1 my $aggs = $algebra->aggregates;
389 1 my $groups = $algebra->groupby;
390 1 my %exprs;
391 1 foreach my $expr (@$aggs) {
392 1 my $var = $expr->variable->value;
393 1 $exprs{$var} = $expr;
394 }
395 1 my @plans;
396 1 foreach my $plan ($self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args)) {
397 4 my $extend = Attean::Plan::Aggregate->new(children => [$plan], aggregates => \%exprs, groups => $groups, distinct => 0, ordered => [], active_graphs => $active_graphs);
398 4 push(@plans, $extend);
399 }
400 1 return @plans;
401 } elsif ($algebra->isa('Attean::Algebra::Ask')) {
402 4 my @plans;
403 4 foreach my $plan ($self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args)) {
404 4 return Attean::Plan::Exists->new(children => [$plan], distinct => 1, ordered => []);
405 }
406 0 return @plans;
407 } elsif ($algebra->isa('Attean::Algebra::Path')) {
408 0 my $s = $algebra->subject;
409 0 my $path = $algebra->path;
410 0 my $o = $algebra->object;
411
412 0 my @algebra = $self->simplify_path($s, $path, $o);
413
414 0 my @join;
415 0 ▶ if (scalar(@algebra)) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @algebra) { } 0 0 0
elsif ($path->isa('Attean::Algebra::ZeroOrMorePath') or $path->isa('Attean::Algebra::OneOrMorePath')) { } 0 0 0
elsif ($path->isa('Attean::Algebra::ZeroOrOnePath')) { } 0 0 0
416 0 my @triples;
417 0 ▶ while (my $pa = shift(@algebra)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pa = shift @algebra) 0 0 0
418 0 ▶ if ($pa->isa('Attean::TriplePattern')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pa->isa('Attean::TriplePattern')) { } 0 0 0
419 0 push(@triples, $pa);
420 } else {
421 0 ▶ if (scalar(@triples)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @triples) 0 0 0
422 0 push(@join, Attean::Algebra::BGP->new( triples => [@triples] ));
423 0 @triples = ();
424 }
425 0 push(@join, $pa);
426 }
427 }
428 0 ▶ if (scalar(@triples)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @triples) 0 0 0
429 0 push(@join, Attean::Algebra::BGP->new( triples => [@triples] ));
430 }
431
432 0 my @vars = $algebra->in_scope_variables;
433
434 my @joins = $self->group_join_plans($model, $active_graphs, $default_graphs, $interesting, map {
435 0 [$self->plans_for_algebra($_, $model, $active_graphs, $default_graphs, %args)]
436 } @join);
437
438 0 my @plans;
439 0 foreach my $j (@joins) {
440 0 push(@plans, Attean::Plan::Project->new(children => [$j], variables => [map { Attean::Variable->new($_) } @vars], distinct => 0, ordered => []));
441 }
442 0 return @plans;
443
444 ▶ } elsif ($path->isa('Attean::Algebra::ZeroOrMorePath') or $path->isa('Attean::Algebra::OneOrMorePath')) {
Condition0%0/3
$path->isa("Attean::Algebra::ZeroOrMorePath") or $path->isa("Attean::Algebra::OneOrMorePath")
l!l&&r!l&&!r
000
MC/DC0%0/2
$path->isa("Attean::Algebra::ZeroOrMorePath") or $path->isa("Attean::Algebra::OneOrMorePath")
$path->isa("Attean::Algebra::ZeroOrMorePath") $path->isa("Attean::Algebra::OneOrMorePath")
Truth table0%0/3
A or B
A= $path->isa("Attean::Algebra::ZeroOrMorePath")B= $path->isa("Attean::Algebra::OneOrMorePath")
ABresult
1X1
011
000
445 0 ▶ my $skip = $path->isa('Attean::Algebra::OneOrMorePath') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$path->isa('Attean::Algebra::OneOrMorePath') ? : 0 0 0
446 0 my $begin = Attean::Variable->new(value => $self->new_temporary('pp'));
447 0 my $end = Attean::Variable->new(value => $self->new_temporary('pp'));
448 0 my $s_var = $s->does('Attean::API::Variable');
449 0 my $o_var = $o->does('Attean::API::Variable');
450
451 0 my $child = $path->children->[0];
452 0 my $a;
453 0 ▶ if ($s_var and not($o_var)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_var and not $o_var) { } 0 0 0
Condition0%0/3
$s_var and not $o_var
!ll&&!rl&&r
000
MC/DC0%0/2
$s_var and not $o_var
$s_var not $o_var
Truth table0%0/3
A and not(B)
A= $s_varB= not $o_var
ABresult
0X0
100
111
454 0 my $inv = Attean::Algebra::InversePath->new( children => [$child] );
455 0 $a = Attean::Algebra::Path->new( subject => $begin, path => $inv, object => $end );
456 0 ($s, $o) = ($o, $s);
457 } else {
458 0 $a = Attean::Algebra::Path->new( subject => $begin, path => $child, object => $end );
459 }
460 0 my @cplans = $self->plans_for_algebra($a, $model, $active_graphs, $default_graphs, %args);
461 0 my @plans;
462 0 foreach my $cplan (@cplans) {
463 0 my $plan = Attean::Plan::ALPPath->new(
464 subject => $s,
465 children => [$cplan],
466 object => $o,
467 graph => $active_graphs,
468 skip => $skip,
469 step_begin => $begin,
470 step_end => $end,
471 distinct => 0,
472 ordered => []
473 );
474 0 push(@plans, $plan);
475 }
476 0 return @plans;
477 } elsif ($path->isa('Attean::Algebra::ZeroOrOnePath')) {
478 0 my $a = Attean::Algebra::Path->new( subject => $s, path => $path->children->[0], object => $o );
479 0 my @children = $self->plans_for_algebra($a, $model, $active_graphs, $default_graphs, %args);
480 0 my @plans;
481 0 foreach my $plan (@children) {
482 0 push(@plans, Attean::Plan::ZeroOrOnePath->new(
483 subject => $s,
484 children => [$plan],
485 object => $o,
486 graph => $active_graphs,
487 distinct => 0,
488 ordered => []
489 ));
490 }
491 0 return @plans;
492 } else {
493 0 die "Cannot simplify property path $path: " . $algebra->as_string;
494 }
495 } elsif ($algebra->isa('Attean::Algebra::Construct')) {
496 0 my @children = $self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args);
497 0 my @plans;
498 0 foreach my $plan (@children) {
499 0 push(@plans, Attean::Plan::Construct->new(triples => $algebra->triples, children => [$plan], distinct => 0, ordered => []));
500 }
501 0 return @plans;
502 } elsif ($algebra->isa('Attean::Algebra::Describe')) {
503 1 my @children = $self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args);
504 1 my @plans;
505 1 foreach my $plan (@children) {
506 1 push(@plans, Attean::Plan::Describe->new(terms => $algebra->terms, graph => $active_graphs, children => [$plan], distinct => 0, ordered => []));
507 }
508 1 return @plans;
509 } elsif ($algebra->isa('Attean::Algebra::Clear')) {
510 0 ▶ my $plan_class = $algebra->drop ? 'Attean::Plan::Drop' : 'Attean::Plan::Clear';
Branch: 1 branch
BranchTrueFalseTotal
$algebra->drop ? : 0 0 0
511 0 my $target = $algebra->target;
512 0 ▶ if ($target eq 'GRAPH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target eq 'GRAPH') { } 0 0 0
513 0 return Attean::Plan::Clear->new(graphs => [$algebra->graph]);
514 } else {
515 0 my %default = map { $_->value => 1 } @$active_graphs;
516 0 my $graphs = $model->get_graphs;
517 0 my @graphs;
518 0 ▶ while (my $graph = $graphs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $graph = $graphs->next) 0 0 0
519 0 ▶ if ($target eq 'ALL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target eq 'ALL') { } 0 0 0
520 0 push(@graphs, $graph);
521 } else {
522 0 ▶ if ($target eq 'DEFAULT' and $default{ $graph->value }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($target eq 'DEFAULT' and $default{$graph->value}) { } 0 0 0
elsif ($target eq 'NAMED' and not $default{$graph->value}) { } 0 0 0
Condition0%0/3
$target eq "DEFAULT" and $default{$graph->value}
!ll&&!rl&&r
000
MC/DC0%0/2
$target eq "DEFAULT" and $default{$graph->value}
$target eq "DEFAULT" $default{$graph->value}
Truth table0%0/3
A and B
A= $target eq "DEFAULT"B= $default{$graph->value}
ABresult
0X0
100
111
523 0 push(@graphs, $graph);
524 ▶ } elsif ($target eq 'NAMED' and not $default{ $graph->value }) {
Condition0%0/3
$target eq "NAMED" and not $default{$graph->value}
!ll&&!rl&&r
000
MC/DC0%0/2
$target eq "NAMED" and not $default{$graph->value}
$target eq "NAMED" not $default{$graph->value}
Truth table0%0/3
A and not(B)
A= $target eq "NAMED"B= not $default{$graph->value}
ABresult
0X0
100
111
525 0 push(@graphs, $graph);
526 }
527 }
528 }
529 0 return $plan_class->new(graphs => \@graphs);
530 }
531 } elsif ($algebra->isa('Attean::Algebra::Add')) {
532 0 my $triple = triplepattern(variable('s'), variable('p'), variable('o'));
533 0 my $child;
534 0 my $default_source = 0;
535 0 ▶ if (my $from = $algebra->source) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $from = $algebra->source) { } 0 0 0
536 0 ($child) = $self->access_plans( $model, $active_graphs, $triple->as_quad_pattern($from) );
537 } else {
538 0 $default_source++;
539 0 my $bgp = Attean::Algebra::BGP->new( triples => [$triple] );
540 0 ($child) = $self->plans_for_algebra($bgp, $model, $active_graphs, $default_graphs, %args);
541 }
542
543 0 my $dest;
544 0 my $default_dest = 0;
545 0 ▶ if (my $g = $algebra->destination) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $algebra->destination) { } 0 0 0
546 0 $dest = $triple->as_quad_pattern($g);
547 } else {
548 0 $default_dest++;
549 0 $dest = $triple->as_quad_pattern($default_graphs->[0]);
550 }
551  
552  
553 0 my @plans;
554 0 my $run_update = 1;
555 0 ▶ if ($default_dest and $default_source) {
Branch: 3 branches
BranchTrueFalseTotal
if ($default_dest and $default_source) { } 0 0 0
elsif ($default_dest or $default_source) { } 0 0 0
elsif ($algebra->source->equals($algebra->destination)) { } 0 0 0
Condition0%0/3
$default_dest and $default_source
!ll&&!rl&&r
000
MC/DC0%0/2
$default_dest and $default_source
$default_dest $default_source
Truth table0%0/3
A and B
A= $default_destB= $default_source
ABresult
0X0
100
111
556 0 $run_update = 0;
557 ▶ } elsif ($default_dest or $default_source) {
Condition0%0/3
$default_dest or $default_source
l!l&&r!l&&!r
000
MC/DC0%0/2
$default_dest or $default_source
$default_dest $default_source
Truth table0%0/3
A or B
A= $default_destB= $default_source
ABresult
1X1
011
000
558 #
559 } elsif ($algebra->source->equals($algebra->destination)) {
560 0 $run_update = 0;
561 }
562
563 0 ▶ if ($run_update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run_update) 0 0 0
564 0 ▶ if ($algebra->drop_destination) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->drop_destination) 0 0 0
565 0 ▶ my @graphs = $algebra->has_destination ? $algebra->destination : @$default_graphs;
Branch: 1 branch
BranchTrueFalseTotal
$algebra->has_destination ? : 0 0 0
566 0 unshift(@plans, Attean::Plan::Clear->new(graphs => [@graphs]));
567 }
568
569 0 push(@plans, Attean::Plan::TripleTemplateToModelQuadMethod->new(
570 graph => $default_graphs->[0],
571 order => ['add_quad'],
572 patterns => {'add_quad' => [$dest]},
573 children => [$child],
574 ));
575
576 0 ▶ if ($algebra->drop_source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->drop_source) 0 0 0
577 0 ▶ my @graphs = $algebra->has_source ? $algebra->source : @$default_graphs;
Branch: 1 branch
BranchTrueFalseTotal
$algebra->has_source ? : 0 0 0
578 0 push(@plans, Attean::Plan::Clear->new(graphs => [@graphs]));
579 }
580 }
581 0 ▶ my $plan = (scalar(@plans) == 1) ? shift(@plans) : Attean::Plan::Sequence->new( children => \@plans );
Branch: 1 branch
BranchTrueFalseTotal
scalar @plans == 1 ? : 0 0 0
582 0 return $plan;
583 } elsif ($algebra->isa('Attean::Algebra::Modify')) {
584 0 ▶ unless ($child) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child) 0 0 0
585 # This is an INSERT/DELETE DATA algebra with ground data and no pattern
586 0 $child = Attean::Algebra::BGP->new( triples => [] );
587 }
588
589 0 my $dataset = $algebra->dataset;
590 0 ▶ my @default = @{ $dataset->{default} || [] };
Condition0%0/2
$dataset->{'default'} || []
l!l
00
MC/DC0%0/1
$dataset->{'default'} || []
$dataset->{'default'}
Truth table0%0/2
A
A= $dataset->{'default'}
Aresult
11
00
591 0 ▶ my @named = values %{ $dataset->{named} || {} };
Condition0%0/2
$dataset->{'named'} || {}
l!l
00
MC/DC0%0/1
$dataset->{'named'} || {}
$dataset->{'named'}
Truth table0%0/2
A
A= $dataset->{'named'}
Aresult
11
00
592
593 0 my @active_graphs = @$active_graphs;
594 0 my @default_graphs = @$default_graphs;
595
596 0 ▶ if (scalar(@default) or scalar(@named)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @default or scalar @named) { } 0 0 0
Condition0%0/2
scalar @default or scalar @named
l!l
00
MC/DC0%0/1
scalar @default or scalar @named
scalar @default
Truth table0%0/2
A
A= scalar @default
Aresult
11
00
597 # change the available named graphs
598 # change the active graph(s)
599 0 @active_graphs = @default;
600 0 @default_graphs = @default;
601 0 $args{ available_graphs } = [@named];
602 } else {
603 # no custom dataset
604 }
605
606 0 my @children = $self->plans_for_algebra($child, $model, \@active_graphs, \@default_graphs, %args);
607 0 $algebra->materialize;
608 0 my $i = $algebra->insert;
609 0 my $d = $algebra->delete;
610 0 my %patterns;
611 my @order;
612 0 ▶ if ($d->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->size) 0 0 0
613 0 push(@order, 'remove_quad');
614 0 $patterns{ 'remove_quad' } = [$d->elements];
615 }
616 0 ▶ if ($i->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i->size) 0 0 0
617 0 push(@order, 'add_quad');
618 0 $patterns{ 'add_quad' } = [$i->elements];
619 }
620 return map {
621 0 Attean::Plan::TripleTemplateToModelQuadMethod->new(
622 graph => $default_graphs->[0],
623 order => \@order,
624 patterns => \%patterns,
625 children => [$_],
626 )
627 } @children;
628 } elsif ($algebra->isa('Attean::Algebra::Load')) {
629 0 my $pattern = triplepattern(variable('subject'), variable('predicate'), variable('object'));
630 0 my $load = Attean::Plan::Load->new( url => $algebra->url->value, silent => $algebra->silent );
631 0 ▶ my $graph = $algebra->has_graph ? $algebra->graph : $default_graphs->[0];
Branch: 1 branch
BranchTrueFalseTotal
$algebra->has_graph ? : 0 0 0
632 0 my $plan = Attean::Plan::TripleTemplateToModelQuadMethod->new(
633 graph => $graph,
634 order => ['add_quad'],
635 patterns => {'add_quad' => [$pattern]},
636 children => [$load],
637 );
638 0 return $plan;
639 } elsif ($algebra->isa('Attean::Algebra::Create')) {
640 0 return Attean::Plan::Sequence->new( children => [] );
641 } elsif ($algebra->isa('Attean::Algebra::Sequence')) {
642 0 my @plans;
643 0 foreach my $child (@{ $algebra->children }) {
644 0 my ($plan) = $self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args);
645 0 push(@plans, $plan);
646 }
647 0 return Attean::Plan::Sequence->new( children => \@plans );
648 } elsif ($algebra->isa('Attean::Algebra::Unfold')) {
649 0 my @plans = $self->plans_for_algebra($child, $model, $active_graphs, $default_graphs, %args);
650 0 my @unfold;
651 0 foreach my $p (@plans) {
652 0 push(@unfold, Attean::Plan::Unfold->new( children => [$p], expression => $algebra->expression, variables => $algebra->variables, active_graphs => $active_graphs ));
653 }
654 0 return @unfold;
655 }
656 0 die "Unimplemented algebra evaluation for: " . $algebra->as_string;
657 }
658
659 =item C<< replace_blanks_with_variables ( \%blanks, \@triples ) >>
660  
661 For each of the triples in the supplied arrayref, replace (in-place) any
662 blank nodes with fresh variables, using C<%blanks> as a cache of blank node
663 label to variable mappings.
664  
665 =cut
666  
667 69 ▶ sub replace_blanks_with_variables {
Subroutine replace_blanks_with_variables: called CC=8 SCAR=21.7
Pod documented
668 69 my $self = shift;
669 69 my $blanks = shift;
670 69 my $triples = shift;
671 69 my $any_changed = 0;
672 69 foreach my $i (0 .. $#{ $triples }) {
673 100 my $t = $triples->[$i];
674 100 my @nodes = $t->values;
675 100 my $changed = 0;
676 100 foreach (@nodes) {
677 300 ▶ if ($_->does('Attean::API::Blank')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->does('Attean::API::Blank')) { } 6 294 300
elsif ($_->does('Attean::API::TriplePattern')) { } 0 294 294
678 6 $changed++;
679 6 my $id = $_->value;
680 6 ▶ unless (exists $blanks->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $blanks->{$id}) 6 0 6
681 6 $blanks->{$id} = Attean::Variable->new(value => $self->new_temporary('blank'));
682 }
683 6 $_ = $blanks->{$id};
684 } elsif ($_->does('Attean::API::TriplePattern')) {
685 0 my $tt = [$_];
686 0 my $changed = $self->replace_blanks_with_variables($blanks, $tt);
687 0 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 0 0 0
688 0 $_ = $tt->[0];
689 }
690 }
691 }
692
693 100 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 6 94 100
694 6 $any_changed++;
695 6 my $new = Attean::TriplePattern->new(@nodes);
696 6 $triples->[$i] = $new;
697 }
698 }
699 69 return $any_changed;
700 }
701
702 # sub plans_for_unbounded_path {
703 # my $self = shift;
704 # my $algebra = shift;
705 # my $model = shift;
706 # my $active_graphs = shift;
707 # my $default_graphs = shift;
708 # my %args = @_;
709 #
710 # my $s = $algebra->subject;
711 # my $path = $algebra->path;
712 # my $o = $algebra->object;
713 #
714 # return Attean::Plan::ALPPath->new(distinct => 0, ordered => []);
715 # }
716
717 0 ▶ sub _package {
Subroutine _package: not called CC=4 SCAR=30.0
718 0 my $self = shift;
719 0 my @args = @_;
720
721 0 my @bgptriples = map { @{ $_->triples } } grep { $_->isa('Attean::Algebra::BGP') } @args;
722 0 my @triples = grep { $_->isa('Attean::TriplePattern') } @args;
723 0 ▶ my @rest = grep { not $_->isa('Attean::Algebra::BGP') and not $_->isa('Attean::TriplePattern') } @args;
Condition0%0/3
$_->isa('Attean::Algebra::BGP') || $_->isa('Attean::TriplePattern')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa('Attean::Algebra::BGP') || $_->isa('Attean::TriplePattern')
$_->isa('Attean::Algebra::BGP') $_->isa('Attean::TriplePattern')
Truth table0%0/3
A || B
A= $_->isa('Attean::Algebra::BGP')B= $_->isa('Attean::TriplePattern')
ABresult
1X1
011
000
724 0 ▶ if (scalar(@rest) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rest == 0) { } 0 0 0
725 0 return Attean::Algebra::BGP->new( triples => [@bgptriples, @triples] );
726 } else {
727 0 my $p = Attean::Algebra::BGP->new( triples => [@bgptriples, @triples] );
728 0 ▶ while (scalar(@rest) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rest > 0) 0 0 0
729 0 $p = Attean::Algebra::Join->new( children => [$p, shift(@rest)] );
730 }
731 0 return $p;
732 }
733 }
734
735 =item C<< simplify_path( $subject, $path, $object ) >>
736  
737 Return a simplified L<Attean::API::Algebra> object corresponding to the given
738 property path.
739  
740 =cut
741  
742 0 ▶ sub simplify_path {
Subroutine simplify_path: not called CC=9 SCAR=45.0
Pod documented
743 0 my $self = shift;
744 0 my $s = shift;
745 0 my $path = shift;
746 0 my $o = shift;
747 0 ▶ if ($path->isa('Attean::Algebra::SequencePath')) {
Branch: 5 branches
BranchTrueFalseTotal
if ($path->isa('Attean::Algebra::SequencePath')) { } 0 0 0
elsif ($path->isa('Attean::Algebra::InversePath')) { } 0 0 0
elsif ($path->isa('Attean::Algebra::PredicatePath')) { } 0 0 0
elsif ($path->isa('Attean::Algebra::AlternativePath')) { } 0 0 0
elsif ($path->isa('Attean::Algebra::NegatedPropertySet')) { } 0 0 0
748 0 my $jvar = Attean::Variable->new(value => $self->new_temporary('pp'));
749 0 my ($lhs, $rhs) = @{ $path->children };
750 0 my @paths;
751 0 push(@paths, $self->simplify_path($s, $lhs, $jvar));
752 0 push(@paths, $self->simplify_path($jvar, $rhs, $o));
753 0 return $self->_package(@paths);
754 } elsif ($path->isa('Attean::Algebra::InversePath')) {
755 0 my ($ipath) = @{ $path->children };
756 0 return $self->simplify_path($o, $ipath, $s);
757 } elsif ($path->isa('Attean::Algebra::PredicatePath')) {
758 0 my $pred = $path->predicate;
759 0 return Attean::TriplePattern->new($s, $pred, $o);
760 } elsif ($path->isa('Attean::Algebra::AlternativePath')) {
761 0 my ($l, $r) = @{ $path->children };
762 0 my $la = $self->_package($self->simplify_path($s, $l, $o));
763 0 my $ra = $self->_package($self->simplify_path($s, $r, $o));
764 0 return Attean::Algebra::Union->new( children => [$la, $ra] );
765 } elsif ($path->isa('Attean::Algebra::NegatedPropertySet')) {
766 0 my @branches;
767
768 0 my @preds = @{ $path->predicates };
769 0 ▶ if (scalar(@preds)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @preds) 0 0 0
770 0 my $pvar = Attean::Variable->new(value => $self->new_temporary('nps'));
771 0 my $pvar_e = Attean::ValueExpression->new( value => $pvar );
772 0 my $t = Attean::TriplePattern->new($s, $pvar, $o);
773 0 my @vals = map { Attean::ValueExpression->new( value => $_ ) } @preds;
774 0 my $expr = Attean::FunctionExpression->new( children => [$pvar_e, @vals], operator => 'notin' );
775 0 my $bgp = Attean::Algebra::BGP->new( triples => [$t] );
776 0 my $f_fwd = Attean::Algebra::Filter->new( children => [$bgp], expression => $expr );
777 0 push(@branches, $f_fwd);
778 }
779  
780 0 my @rev = @{ $path->reversed };
781 0 ▶ if (scalar(@rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rev) 0 0 0
782 0 my $pvar = Attean::Variable->new(value => $self->new_temporary('nps_rev'));
783 0 my $pvar_e = Attean::ValueExpression->new( value => $pvar );
784 0 my $t = Attean::TriplePattern->new($o, $pvar, $s);
785 0 my @vals = map { Attean::ValueExpression->new( value => $_ ) } @rev;
786 0 my $expr = Attean::FunctionExpression->new( children => [$pvar_e, @vals], operator => 'notin' );
787 0 my $bgp = Attean::Algebra::BGP->new( triples => [$t] );
788 0 my $f_rev = Attean::Algebra::Filter->new( children => [$bgp], expression => $expr );
789 0 push(@branches, $f_rev);
790 }
791  
792 0 ▶ if (scalar(@branches) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @branches == 1) { } 0 0 0
793 0 return shift(@branches);
794 } else {
795 0 return Attean::Algebra::Union->new( children => \@branches );
796 }
797 } else {
798 0 return;
799 }
800 }
801
802 =item C<< new_projection( $plan, $distinct, @variable_names ) >>
803  
804 Return a new L<< Attean::Plan::Project >> plan over C<< $plan >>, projecting
805 the named variables. C<< $disctinct >> should be true if the caller can
806 guarantee that the resulting plan will produce distinct results, false otherwise.
807  
808 This method takes care of computing plan metadata such as the resulting ordering.
809  
810 =cut
811  
812 10 ▶ sub new_projection {
Subroutine new_projection: called CC=4 SCAR=15.4
Pod documented
813 10 my $self = shift;
814 10 my $plan = shift;
815 10 my $distinct = shift;
816 10 my @vars = @_;
817 10 my $order = $plan->ordered;
818 10 my @pvars = map { Attean::Variable->new($_) } @vars;
819
820 10 my %pvars = map { $_ => 1 } @vars;
821 10 my @porder;
822 10 CMP: foreach my $cmp (@{ $order }) {
823 0 my @cmpvars = $self->_comparator_referenced_variables($cmp);
824 0 foreach my $v (@cmpvars) {
825 0 ▶ unless ($pvars{ $v }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pvars{$v}) 0 0 0
826 # projection is dropping a variable used in this comparator
827 # so we lose any remaining ordering that the sub-plan had.
828 0 last CMP;
829 }
830 }
831
832 # all the variables used by this comparator are available after
833 # projection, so the resulting plan will continue to be ordered
834 # by this comparator
835 0 push(@porder, $cmp);
836 }
837
838 10 return Attean::Plan::Project->new(children => [$plan], variables => \@pvars, distinct => $distinct, ordered => \@porder);
839 }
840  
841 =item C<< bgp_join_plans( $bgp, $model, \@active_graphs, \@default_graphs, \@interesting_order, \@plansA, \@plansB, ... ) >>
842  
843 Returns a list of alternative plans for the join of a set of triples.
844 The arguments C<@plansA>, C<@plansB>, etc. represent alternative plans for each
845 triple participating in the join.
846  
847 =cut
848  
849 69 ▶ sub bgp_join_plans {
Subroutine bgp_join_plans: called CC=8 SCAR=20.8
Pod documented
850 69 my $self = shift;
851 69 my $bgp = shift;
852 69 my $model = shift;
853 69 my $active = shift;
854 69 my $default = shift;
855 69 my $interesting = shift;
856 69 my @triples = @_;
857
858 69 ▶ if (scalar(@triples)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @triples) { } 62 7 69
859 62 my @plans = $self->joins_for_plan_alternatives($model, $active, $default, $interesting, @triples);
860 61 my @triples = @{ $bgp->triples };
861
862 # If the BGP does not contain any blanks, then the results are
863 # guaranteed to be distinct. Otherwise, we have to assume they're
864 # not distinct.
865 61 my $distinct = 1;
866 61 LOOP: foreach my $t (@triples) {
867 97 foreach my $b ($t->values_consuming_role('Attean::API::Blank')) {
868 0 $distinct = 0;
869 0 last LOOP;
870 }
871 97 foreach my $b ($t->values_consuming_role('Attean::API::Variable')) {
872 141 ▶ if ($b->value =~ /^[.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->value =~ /^[.]/u) 6 135 141
873 # variable names starting with a dot represent placeholders introduced during query planning (with C<new_temporary>)
874 # they are not projectable, and so may cause an otherwise distinct result to become non-distinct
875 6 $distinct = 0;
876 6 last LOOP;
877 }
878 }
879 }
880
881 # Set the distinct flag on each of the top-level join plans that
882 # represents the entire BGP. (Sub-plans won't ever be marked as
883 # distinct, but that shouldn't matter to the rest of the planning
884 # process.)
885 61 ▶ if ($distinct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distinct) 55 6 61
886 55 foreach my $p (@plans) {
887 119 $p->distinct(1);
888 }
889 }
890  
891 61 return @plans;
892 } else {
893 # The empty BGP is a special case -- it results in a single join-identity result
894 7 my $r = Attean::Result->new( bindings => {} );
895 7 my $plan = Attean::Plan::Table->new( rows => [$r], variables => [], distinct => 1, ordered => [] );
896 7 return $plan;
897 }
898 }
899  
900 =item C<< group_join_plans( $model, \@active_graphs, \@default_graphs, \@interesting_order, \@plansA, \@plansB, ... ) >>
901  
902 Returns a list of alternative plans for the join of a set of sub-plans.
903 The arguments C<@plansA>, C<@plansB>, etc. represent alternative plans for each
904 sub-plan participating in the join.
905  
906 =cut
907  
908 11 ▶ sub group_join_plans {
Subroutine group_join_plans: called CC=1 SCAR=0.0
Pod documented
909 11 my $self = shift;
910 11 return $self->joins_for_plan_alternatives(@_);
911 }
912
913 =item C<< joins_for_plan_alternatives( $model, \@active_graphs, \@default_graphs, $interesting, \@plan_A, \@plan_B, ... ) >>
914  
915 Returns a list of alternative plans that may all be used to produce results
916 matching the join of C<< plan_A >>, C< plan_B >>, etc. Each plan array here
917 (e.g. C<< @plan_A >>) should contain equivalent plans.
918  
919 =cut
920  
921 1 ▶ sub joins_for_plan_alternatives {
Subroutine joins_for_plan_alternatives: called CC=1 SCAR=0.0
Pod documented
922 1 my $self = shift;
923 1 my $model = shift;
924 1 my $active_graphs = shift;
925 1 my $default_graphs = shift;
926 1 my $interesting = shift;
927 1 my @args = @_; # each $args[$i] here is an array reference containing alternate plans for element $i
928 1 die "This query planner does not seem to consume a Attean::API::JoinPlanner role (which is necessary for query planning)";
929 }
930
931 =item C<< access_plans( $model, $active_graphs, $pattern ) >>
932  
933 Returns a list of alternative L<Attean::API::Plan> objects that may be used to
934 produce results matching the L<Attean::API::TripleOrQuadPattern> $pattern in
935 the context of C<< $active_graphs >>.
936  
937 =cut
938  
939 # $pattern is a Attean::API::TripleOrQuadPattern object
940 # Return a Attean::API::Plan object that represents the evaluation of $pattern.
941 # e.g. different plans might represent different ways of producing the matches (table scan, index match, etc.)
942 100 ▶ sub access_plans {
Subroutine access_plans: called CC=4 SCAR=13.9
Pod documented
943 100 my $self = shift;
944 100 my $model = shift;
945 100 my $active_graphs = shift;
946 100 my $pattern = shift;
947 100 my @vars = map { $_->value } $pattern->values_consuming_role('Attean::API::Variable');
948 100 my %vars;
949 100 my $dup = 0;
950 100 foreach my $v (@vars) {
951 144 ▶ $dup++ if ($vars{$v}++);
Branch: 1 branch
BranchTrueFalseTotal
if $vars{$v}++ 0 144 144
952 }
953
954 100 my $distinct = 0; # TODO: is this pattern distinct? does it have blank nodes?
955
956 100 my @nodes = $pattern->values;
957 100 ▶ unless ($nodes[3]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nodes[3]) 100 0 100
958 100 $nodes[3] = $active_graphs;
959 }
960 100 my $plan = Attean::Plan::Quad->new(
961 subject => $nodes[0],
962 predicate => $nodes[1],
963 object => $nodes[2],
964 graph => $nodes[3],
965 values => \@nodes,
966 distinct => $distinct,
967 ordered => [],
968 );
969 100 return $plan;
970 }
971
972 =item C<< join_plans( $model, \@active_graphs, \@default_graphs, \@plan_left, \@plan_right, $type [, $expr] ) >>
973  
974 Returns a list of alternative plans for the join of one plan from C<< @plan_left >>
975 and one plan from C<< @plan_right >>. The join C<< $type >> must be one of
976 C<< 'inner' >>, C<< 'left' >>, or C<< 'minus' >>, indicating the join algorithm
977 to be used. If C<< $type >> is C<< 'left' >>, then the optional C<< $expr >>
978 may be used to supply a filter expression that should be used by the SPARQL
979 left-join algorithm.
980  
981 =cut
982  
983 # $lhs and $rhs are both Attean::API::Plan objects
984 # Return a Attean::API::Plan object that represents the evaluation of $lhs ⋈ $rhs.
985 # The $left and $minus flags indicate the type of the join to be performed (⟕ and ▷, respectively).
986 # e.g. different plans might represent different join algorithms (nested loop join, hash join, etc.) or different orderings ($lhs ⋈ $rhs or $rhs ⋈ $lhs)
987 263 ▶ sub join_plans {
Subroutine join_plans: called CC=10 SCAR=23.7
Pod documented
988 263 my $self = shift;
989 263 my $model = shift;
990 263 my $active_graphs = shift;
991 263 my $default_graphs = shift;
992 263 my $lplans = shift;
993 263 my $rplans = shift;
994 263 my $type = shift;
995 263 my $left = ($type eq 'left');
996 263 my $minus = ($type eq 'minus');
997 263 my $expr = shift;
998
999 263 my @plans;
1000 263 ▶ Carp::confess unless (reftype($lplans) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($lplans) eq "ARRAY" 0 263 263
1001 263 foreach my $lhs (@{ $lplans }) {
1002 353 foreach my $rhs (@{ $rplans }) {
1003 366 my @vars = (@{ $lhs->in_scope_variables }, @{ $rhs->in_scope_variables });
1004 366 my %vars;
1005 my %join_vars;
1006 366 foreach my $v (@vars) {
1007 1308 ▶ if ($vars{$v}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{$v}++) 363 945 1308
1008 363 $join_vars{$v}++;
1009 }
1010 }
1011 366 my @join_vars = keys %join_vars;
1012
1013 366 ▶ if ($left) {
Branch: 2 branches
BranchTrueFalseTotal
if ($left) { } 0 366 366
elsif ($minus) { } 0 366 366
1014 0 ▶ if (scalar(@join_vars) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @join_vars > 0) 0 0 0
1015 0 push(@plans, Attean::Plan::HashJoin->new(children => [$lhs, $rhs], left => 1, expression => $expr, join_variables => \@join_vars, distinct => 0, ordered => []));
1016 }
1017 0 push(@plans, Attean::Plan::NestedLoopJoin->new(children => [$lhs, $rhs], left => 1, expression => $expr, join_variables => \@join_vars, distinct => 0, ordered => $lhs->ordered));
1018 } elsif ($minus) {
1019 # we can't use a hash join for MINUS queries, because of the definition of MINUS having a special case for compatible results that have disjoint domains
1020 0 push(@plans, Attean::Plan::NestedLoopJoin->new(children => [$lhs, $rhs], anti => 1, join_variables => \@join_vars, distinct => 0, ordered => $lhs->ordered));
1021 } else {
1022 366 ▶ if (scalar(@join_vars) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @join_vars > 0) 311 55 366
1023 # if there's shared variables (hopefully), we can also use a hash join
1024 311 push(@plans, Attean::Plan::HashJoin->new(children => [$lhs, $rhs], join_variables => \@join_vars, distinct => 0, ordered => []));
1025 311 push(@plans, Attean::Plan::HashJoin->new(children => [$rhs, $lhs], join_variables => \@join_vars, distinct => 0, ordered => []));
1026 # } else {
1027 # warn "No join vars for $lhs ⋈ $rhs";
1028 }
1029  
1030 # nested loop joins work in all cases
1031 366 push(@plans, Attean::Plan::NestedLoopJoin->new(children => [$lhs, $rhs], join_variables => \@join_vars, distinct => 0, ordered => $lhs->ordered));
1032 366 push(@plans, Attean::Plan::NestedLoopJoin->new(children => [$rhs, $lhs], join_variables => \@join_vars, distinct => 0, ordered => $rhs->ordered));
1033 }
1034 }
1035 }
1036
1037 263 return @plans;
1038 }
1039
1040
1041 0 ▶ sub _comparator_referenced_variables {
Subroutine _comparator_referenced_variables: not called CC=3 SCAR=24.8
1042 0 my $self = shift;
1043 0 my %vars;
1044 0 ▶ while (my $c = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = shift()) 0 0 0
1045 0 my $expr = $c->expression;
1046 0 foreach my $v ($expr->in_scope_variables) {
1047 0 $vars{$v}++;
1048 }
1049 }
1050 0 return keys %vars;
1051 }
1052
1053 26 ▶ sub _comparators_are_stable_and_cover_vars {
Subroutine _comparators_are_stable_and_cover_vars: called CC=4 SCAR=15.9
1054 26 my $self = shift;
1055 26 my $cmps = shift;
1056 26 my @vars = @_;
1057 26 my %unseen = map { $_ => 1 } @vars;
1058 26 foreach my $c (@$cmps) {
1059 0 ▶ return 0 unless ($c->expression->is_stable);
Branch: 1 branch
BranchTrueFalseTotal
unless $c->expression->is_stable 0 0 0
1060 0 foreach my $v ($self->_comparator_referenced_variables($c)) {
1061 0 delete $unseen{$v};
1062 }
1063 }
1064 26 my @keys = keys %unseen;
1065 26 return (scalar(@keys) == 0);
1066 }
1067
1068 3 ▶ sub _order_by {
Subroutine _order_by: called CC=2 SCAR=6.9
1069 3 my $self = shift;
1070 3 my $algebra = shift;
1071 3 my ($exprs, $ascending, $svars);
1072 3 my @cmps = @{ $algebra->comparators };
1073 3 my %ascending;
1074 my %exprs;
1075 3 my @svars;
1076 3 foreach my $i (0 .. $#cmps) {
1077 3 my $var = $self->new_temporary('order');
1078 3 my $cmp = $cmps[$i];
1079 3 push(@svars, $var);
1080 3 $ascending{$var} = $cmp->ascending;
1081 3 $exprs{$var} = $cmp->expression;
1082 }
1083 3 return (\%exprs, \%ascending, \@svars);
1084 }
1085 }
1086  
1087 51 1;
1088  
1089 __END__
« Attean/SimpleQueryEvaluator.pm ↑ Summary Test/Attean/W3CManifestTestSuite.pm »