| 1 | 50 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 50 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 50 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Attean::API::Plan - Query plan | ||||||||||
| 8 | |||||||||||
| 9 | =head1 VERSION | ||||||||||
| 10 | |||||||||||
| 11 | This document describes Attean::API::Plan version 0.040 | ||||||||||
| 12 | |||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||
| 14 | |||||||||||
| 15 | The Attean::API::Plan role defines a common API for all query plans. | ||||||||||
| 16 | |||||||||||
| 17 | =head1 ATTRIBUTES | ||||||||||
| 18 | |||||||||||
| 19 | =over 4 | ||||||||||
| 20 | |||||||||||
| 21 | =item C<< cost >> | ||||||||||
| 22 | |||||||||||
| 23 | =item C<< distinct >> | ||||||||||
| 24 | |||||||||||
| 25 | =item C<< item_type >> | ||||||||||
| 26 | |||||||||||
| 27 | =item C<< in_scope_variables >> | ||||||||||
| 28 | |||||||||||
| 29 | =item C<< ordered >> | ||||||||||
| 30 | |||||||||||
| 31 | =back | ||||||||||
| 32 | |||||||||||
| 33 | =head1 REQUIRED METHODS | ||||||||||
| 34 | |||||||||||
| 35 | The following methods are required by the L<Attean::API::Plan> role: | ||||||||||
| 36 | |||||||||||
| 37 | =over 4 | ||||||||||
| 38 | |||||||||||
| 39 | =item C<< impl( $model ) >> | ||||||||||
| 40 | |||||||||||
| 41 | Returns a code reference that when called (without arguments), returns an | ||||||||||
| 42 | L<Attean::API::Iterator> object. | ||||||||||
| 43 | |||||||||||
| 44 | =back | ||||||||||
| 45 | |||||||||||
| 46 | =head1 METHODS | ||||||||||
| 47 | |||||||||||
| 48 | =over 4 | ||||||||||
| 49 | |||||||||||
| 50 | =item C<< has_cost >> | ||||||||||
| 51 | |||||||||||
| 52 | =cut | ||||||||||
| 53 | |||||||||||
| 54 | 50 | ▶ | use Type::Tiny::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 55 | |||||||||||
| 56 | 50 | package Attean::API::Plan 0.040 { | |||||||||
| 57 | 50 | ▶ | use Scalar::Util qw(blessed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 50 | ▶ | use Types::Standard qw(ArrayRef CodeRef Str Object InstanceOf Bool Num Int); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 59 | |||||||||||
| 60 | 50 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | 50 | has 'cost' => (is => 'rw', isa => Int, predicate => 'has_cost'); | |||||||||
| 63 | 50 | has 'distinct' => (is => 'rw', isa => Bool, required => 1, default => 0); | |||||||||
| 64 | 50 | has 'item_type' => (is => 'ro', isa => Str, required => 1, default => 'Attean::API::Result'); | |||||||||
| 65 | 50 | has 'in_scope_variables' => (is => 'ro', isa => ArrayRef[Str], required => 1); | |||||||||
| 66 | 66 | ▶ | has 'ordered' => (is => 'ro', isa => ArrayRef, required => 1, default => sub { [] }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 67 | |||||||||||
| 68 | 50 | requires 'impl'; | |||||||||
| 69 | 50 | requires 'plan_as_string'; | |||||||||
| 70 | |||||||||||
| 71 | =item C<< as_string >> | ||||||||||
| 72 | |||||||||||
| 73 | Returns a tree-structured string representation of this plan, including children. | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | |||||||||||
| 77 | 13 | ▶ | sub as_string { | ||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 78 | 13 | my $self = shift; | |||||||||
| 79 | 13 | my $string = ''; | |||||||||
| 80 | 23 | ▶ | $self->walk( prefix => sub { | ||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||
| 81 | 23 | my $a = shift; | |||||||||
| 82 | 23 | my $level = shift; | |||||||||
| 83 | 23 | my $parent = shift; | |||||||||
| 84 | 23 | my $indent = ' ' x $level; | |||||||||
| 85 | 23 | my @flags; | |||||||||
| 86 | 23 | ▶ | push(@flags, 'distinct') if ($a->distinct); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 23 | ▶ | if (scalar(@{ $a->ordered })) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 1 | my @orders; | |||||||||
| 89 | 1 | foreach my $c (@{ $a->ordered }) { | |||||||||
| 90 | 1 | ▶ | my $dir = $c->ascending ? "↑" : "↓"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 1 | my $s = $dir . $c->expression->as_string; | |||||||||
| 92 | 1 | push(@orders, $s); | |||||||||
| 93 | } | ||||||||||
| 94 | 1 | push(@flags, "order: " . join('; ', @orders)); | |||||||||
| 95 | } | ||||||||||
| 96 | 23 | ▶ | if (defined(my $cost = $a->cost)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 9 | push(@flags, "cost: $cost"); | |||||||||
| 98 | } | ||||||||||
| 99 | 23 | $string .= "-$indent " . $a->plan_as_string($level); | |||||||||
| 100 | 23 | ▶ | if (scalar(@flags)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 14 | $string .= ' (' . join(' ', @flags) . ")"; | |||||||||
| 102 | } | ||||||||||
| 103 | 23 | $string .= "\n"; | |||||||||
| 104 | 13 | }); | |||||||||
| 105 | 13 | return $string; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | =item C<< evaluate( $model ) >> | ||||||||||
| 109 | |||||||||||
| 110 | Evaluates this plan and returns the resulting iterator. | ||||||||||
| 111 | |||||||||||
| 112 | =cut | ||||||||||
| 113 | |||||||||||
| 114 | 8 | ▶ | sub evaluate { | ||||||||
|
Subroutine
evaluate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 115 | 8 | my $self = shift; | |||||||||
| 116 | 8 | my $impl = $self->impl(@_); | |||||||||
| 117 | 8 | return $impl->(); | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | =item C<< in_scope_variables_union( @plans ) >> | ||||||||||
| 121 | |||||||||||
| 122 | Returns the set union of C<< in_scope_variables >> of the given plan objects. | ||||||||||
| 123 | |||||||||||
| 124 | =cut | ||||||||||
| 125 | |||||||||||
| 126 | 1464 | ▶ | sub in_scope_variables_union { | ||||||||
|
Subroutine
in_scope_variables_union: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 127 | 1464 | my @plans = grep { blessed($_) } @_; | |||||||||
| 128 | 1464 | my %vars = map { $_ => 1 } map { @{ $_->in_scope_variables } } @plans; | |||||||||
| 129 | 1464 | return keys %vars; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | =item C<< subplans_of_type_are_variable_connected( $type ) >> | ||||||||||
| 133 | |||||||||||
| 134 | Returns true if the subpatterns of the given C<< $type >> are all connected | ||||||||||
| 135 | through their C<< in_scope_variables >>, false otherwise (implying a cartesian | ||||||||||
| 136 | product if the connecting plans perform some form of join. | ||||||||||
| 137 | |||||||||||
| 138 | =cut | ||||||||||
| 139 | |||||||||||
| 140 | 7 | ▶ | sub subplans_of_type_are_variable_connected { | ||||||||
|
Subroutine
subplans_of_type_are_variable_connected: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 141 | 7 | my $self = shift; | |||||||||
| 142 | 7 | my @types = @_; | |||||||||
| 143 | 7 | my @c = $self->subpatterns_of_type(@types); | |||||||||
| 144 | 7 | return $self->_plans_are_variable_connected(@c); | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | =item C<< children_are_variable_connected( $type ) >> | ||||||||||
| 148 | |||||||||||
| 149 | Returns true if the children of this plan are all connected | ||||||||||
| 150 | through their C<< in_scope_variables >>, false otherwise (implying a cartesian | ||||||||||
| 151 | product if this plan performs some form of join. | ||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||
| 154 | |||||||||||
| 155 | 1120 | ▶ | sub children_are_variable_connected { | ||||||||
|
Subroutine
children_are_variable_connected: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 156 | 1120 | my $self = shift; | |||||||||
| 157 | 1120 | my @c = @{ $self->children }; | |||||||||
| 158 | 1120 | return $self->_plans_are_variable_connected(@c); | |||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | 1127 | ▶ | sub _plans_are_variable_connected { | ||||||||
|
Subroutine
_plans_are_variable_connected: called CC=10 SCAR=23.0
| |||||||||||
| 162 | # TODO: In the worst case, this is going to run in O(n^2) in the number | ||||||||||
| 163 | # of children. Better indexing of the children by variables can speed | ||||||||||
| 164 | # this up. | ||||||||||
| 165 | 1127 | my $self = shift; | |||||||||
| 166 | 1127 | my @c = @_; | |||||||||
| 167 | # warn "===========================\n"; | ||||||||||
| 168 | # foreach my $c (@c) { | ||||||||||
| 169 | # warn $c->as_string; | ||||||||||
| 170 | # } | ||||||||||
| 171 | 1127 | ▶ | return 1 unless (scalar(@c)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | |||||||||||
| 173 | 1126 | my %vars_by_child; | |||||||||
| 174 | 1126 | foreach my $i (0 .. $#c) { | |||||||||
| 175 | 2257 | my $c = $c[$i]; | |||||||||
| 176 | 2257 | foreach my $var (@{ $c->in_scope_variables }) { | |||||||||
| 177 | 3672 | $vars_by_child{$i}{$var}++; | |||||||||
| 178 | } | ||||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | # | ||||||||||
| 182 | 1126 | my @remaining = keys %vars_by_child; | |||||||||
| 183 | 1126 | ▶ | return 1 unless (scalar(@remaining)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 184 | 1126 | my $current = shift(@remaining); | |||||||||
| 185 | # warn 'Starting with ' . $c[$current]->as_string; | ||||||||||
| 186 | 1126 | my %seen_vars = %{ $vars_by_child{$current} }; | |||||||||
| 187 | 1126 | ▶ | LOOP: while (scalar(@remaining)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | 1130 | foreach my $i (0 .. $#remaining) { | |||||||||
| 189 | 1135 | my $candidate = $remaining[$i]; | |||||||||
| 190 | 1135 | my @candidate_vars = keys %{ $vars_by_child{$candidate} }; | |||||||||
| 191 | 1135 | foreach my $var (@candidate_vars) { | |||||||||
| 192 | 1331 | ▶ | if (exists $seen_vars{ $var }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | 1076 | foreach my $var (@candidate_vars) { | |||||||||
| 194 | 1772 | $seen_vars{$var}++; | |||||||||
| 195 | } | ||||||||||
| 196 | # warn "connected with $var: " . $c[$candidate]->as_string; | ||||||||||
| 197 | 1076 | splice(@remaining, $i, 1); | |||||||||
| 198 | 1076 | next LOOP; | |||||||||
| 199 | } | ||||||||||
| 200 | } | ||||||||||
| 201 | } | ||||||||||
| 202 | # warn 'Not fully connected'; | ||||||||||
| 203 | 54 | return 0; | |||||||||
| 204 | } | ||||||||||
| 205 | # warn 'Fully connected'; | ||||||||||
| 206 | 1072 | return 1; | |||||||||
| 207 | } | ||||||||||
| 208 | } | ||||||||||
| 209 | |||||||||||
| 210 | 50 | package Attean::API::BindingSubstitutionPlan 0.040 { | |||||||||
| 211 | 50 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 212 | 50 | with 'Attean::API::Plan'; | |||||||||
| 213 | 50 | requires 'substitute_impl'; # $code = $plan->impl($model, $binding); | |||||||||
| 214 | |||||||||||
| 215 | 0 | ▶ | sub impl { | ||||||||
|
Subroutine
impl: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 216 | 0 | my $self = shift; | |||||||||
| 217 | 0 | my $model = shift; | |||||||||
| 218 | 0 | my $b = Attean::Result->new(); | |||||||||
| 219 | 0 | return $self->substitute_impl($model, $b); | |||||||||
| 220 | } | ||||||||||
| 221 | } | ||||||||||
| 222 | |||||||||||
| 223 | 50 | package Attean::API::UnionScopeVariablesPlan 0.040 { | |||||||||
| 224 | 50 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 225 | |||||||||||
| 226 | 50 | with 'Attean::API::Plan'; | |||||||||
| 227 | |||||||||||
| 228 | 1464 | ▶ | around 'BUILDARGS' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 229 | 1464 | my $orig = shift; | |||||||||
| 230 | 1464 | my $class = shift; | |||||||||
| 231 | 1464 | my %args = @_; | |||||||||
| 232 | 1464 | my @vars = Attean::API::Plan->in_scope_variables_union( @{ $args{children} } ); | |||||||||
| 233 | |||||||||||
| 234 | 1464 | ▶ | if (exists $args{in_scope_variables}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 235 | 0 | Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor"; | |||||||||
| 236 | } | ||||||||||
| 237 | 1464 | $args{in_scope_variables} = [@vars]; | |||||||||
| 238 | |||||||||||
| 239 | 1464 | return $orig->( $class, %args ); | |||||||||
| 240 | 50 | }; | |||||||||
| 241 | } | ||||||||||
| 242 | |||||||||||
| 243 | 50 | package Attean::API::Plan::Join 0.040 { | |||||||||
| 244 | 50 | ▶ | use Types::Standard qw(CodeRef); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 245 | 50 | ▶ | use Types::Standard qw(ArrayRef Str ConsumerOf Bool); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 246 | |||||||||||
| 247 | 50 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 248 | |||||||||||
| 249 | 50 | with 'Attean::API::Plan', 'Attean::API::BinaryQueryTree'; | |||||||||
| 250 | 50 | with 'Attean::API::UnionScopeVariablesPlan'; | |||||||||
| 251 | |||||||||||
| 252 | 50 | has 'join_variables' => (is => 'ro', isa => ArrayRef[Str], required => 1); | |||||||||
| 253 | 50 | has 'anti' => (is => 'ro', isa => Bool, default => 0); # is this an anti-join | |||||||||
| 254 | 50 | has 'left' => (is => 'ro', isa => Bool, default => 0); # is this a left, outer-join | |||||||||
| 255 | |||||||||||
| 256 | # if this is a left, outer-join, this is the filter expression that acts as part of the join operation (see the SPARQL semantics for LeftJoin for more details) | ||||||||||
| 257 | 1354 | ▶ | has 'expression' => (is => 'ro', isa => ConsumerOf['Attean::API::Expression'], required => 0, default => sub { Attean::ValueExpression->new( value => Attean::Literal->true ) }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 258 | } | ||||||||||
| 259 | |||||||||||
| 260 | 50 | 1; | |||||||||
| 261 | |||||||||||
| 262 | __END__ | ||||||||||