/usr/share/perl5/Attean/API/QueryPlanner.pm is in libattean-perl 0.019-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | use v5.14;
use warnings;
=head1 NAME
Attean::API::IDPJoinPlanner - Iterative dynamic programming query planning role
=head1 VERSION
This document describes Attean::API::IDPJoinPlanner version 0.019
=head1 SYNOPSIS
extends 'Attean::QueryPlanner';
with 'Attean::API::IDPJoinPlanner';
=head1 DESCRIPTION
The Attean::API::IDPJoinPlanner role provides a query planner the
C<< joins_for_plan_alternatives >> method, as well as the cost estimation
methods that consume the L<Attean::API::CostPlanner> role.
=head1 ATTRIBUTES
=over 4
=back
=head1 METHODS
=over 4
=cut
package Attean::API::QueryPlanner 0.019 {
use Types::Standard qw(CodeRef);
use Moo::Role;
requires 'plan_for_algebra'; # plan_for_algebra($algebra, $model, \@default_graphs)
}
package Attean::API::CostPlanner 0.019 {
use Scalar::Util qw(refaddr);
use Types::Standard qw(CodeRef);
use Moo::Role;
use namespace::clean;
with 'Attean::API::QueryPlanner';
requires 'plans_for_algebra'; # plans_for_algebra($algebra, $model, \@active_graphs, \@default_graphs)
requires 'cost_for_plan'; # cost_for_plan($plan, $model)
before 'cost_for_plan' => sub {
my $self = shift;
my $plan = shift;
my $model = shift;
if (refaddr($self) == refaddr($model)) {
Carp::confess "Model and planner objects cannot be the same in call to cost_for_plan";
} elsif ($self->does('Attean::API::Model') and $model->does('Attean::API::Model')) {
Carp::confess "Model and planner objects cannot both consume Attean::API::Model in call to cost_for_plan";
}
};
sub plan_for_algebra {
my $self = shift;
my $algebra = shift;
my $model = shift;
my $default_graphs = shift;
my $active_graphs = $default_graphs;
my @plans = sort { $self->cost_for_plan($a, $model) <=> $self->cost_for_plan($b, $model) } $self->plans_for_algebra($algebra, $model, $active_graphs, $default_graphs);
my $plan = shift(@plans);
return $plan;
}
}
package Attean::API::JoinPlanner 0.019 {
use Moo::Role;
requires 'joins_for_plan_alternatives';
}
package Attean::API::NaiveJoinPlanner 0.019 {
use Math::Cartesian::Product;
use Moo::Role;
with 'Attean::API::JoinPlanner';
with 'Attean::API::QueryPlanner';
sub joins_for_plan_alternatives {
my $self = shift;
my $model = shift;
my $active_graphs = shift;
my $default_graphs = shift;
my $interesting = shift;
my @args = @_; # each $args[$i] here is an array reference containing alternate plans for element $i
my $plans = shift(@args);
while (scalar(@args)) {
my $next = shift(@args);
my @plans = $self->join_plans($model, $active_graphs, $default_graphs, $plans, $next, 'inner');
$plans = \@plans;
}
my @plans = @$plans;
return @plans;
}
}
package Attean::API::SimpleCostPlanner 0.019 {
use Types::Standard qw(Int);
use Scalar::Util qw(blessed);
use Moo::Role;
with 'Attean::API::CostPlanner';
with 'MooX::Log::Any';
has 'keep' => (is => 'ro', isa => Int, default => 5);
around 'joins_for_plan_alternatives' => sub {
my $orig = shift;
my $self = shift;
my $model = shift;
my @plans = $orig->($self, $model, @_);
return $self->prune_plans($model, [], \@plans);
};
sub prune_plans {
my $self = shift;
my $model = shift;
my $interesting = shift;
my @plans = @{ shift || [] };
no sort 'stable';
my @sorted = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [$self->cost_for_plan($_, $model), $_] } @plans;
return ($self->keep) ? splice(@sorted, 0, $self->keep) : @sorted;
}
sub cost_for_plan {
my $self = shift;
my $plan = shift;
my $model = shift;
Carp::confess "No model given" unless (blessed($model) and $model->does('Attean::API::Model'));
if ($plan->has_cost) {
return $plan->cost;
} else {
if ($model->does('Attean::API::CostPlanner')) {
if (defined(my $cost = $model->cost_for_plan($plan, $self))) {
$plan->cost($cost);
$self->log->info('Model \''.ref($model).'\' did cost planning for \''.ref($plan).'\' and got cost '.$cost);
return $cost;
}
}
my $cost = 1;
my @children = @{ $plan->children };
if ($plan->isa('Attean::Plan::Quad')) {
my @vars = map { $_->value } grep { blessed($_) and $_->does('Attean::API::Variable') } $plan->values;
return scalar(@vars);
} elsif ($plan->isa('Attean::Plan::Table')) {
my $rows = $plan->rows;
$cost = scalar(@$rows);
} elsif ($plan->isa('Attean::Plan::NestedLoopJoin')) {
my $lcost = $self->cost_for_plan($children[0], $model);
my $rcost = $self->cost_for_plan($children[1], $model);
if ($lcost == 0) {
$cost = $rcost;
} elsif ($rcost == 0) {
$cost = $lcost;
} else {
$cost = $lcost * $rcost;
}
# a cartesian nested loop join is bad, but the algorithm already
# has to check for all possible joins, so it's not as bad as
# a cartesian hash join (below)
$cost *= 10 unless ($plan->children_are_variable_connected);
} elsif ($plan->isa('Attean::Plan::HashJoin')) {
my $joined = $plan->children_are_variable_connected;
my $lcost = $self->cost_for_plan($children[0], $model);
my $rcost = $self->cost_for_plan($children[1], $model);
$cost = ($lcost + $rcost);
$cost += ($lcost < $rcost); # To let the plan with cheaper rhs win
$cost *= 100 unless ($plan->children_are_variable_connected);
} elsif ($plan->isa('Attean::Plan::Service')) {
my $scost = 10;
foreach my $c (@{ $plan->children }) {
$scost += $self->cost_for_plan($c, $model);
}
$cost = 5 * $scost;
} elsif ($plan->isa('Attean::Plan::Unique')) {
$cost = 0; # consider a filter on the iterator (like unique) to be essentially free
foreach my $c (@{ $plan->children }) {
$cost += $self->cost_for_plan($c, $model);
}
} else {
foreach my $c (@{ $plan->children }) {
$cost += $self->cost_for_plan($c, $model);
}
}
$plan->cost($cost);
if ($self->log->is_trace) {
$self->log->trace("Cost $cost estimated for\n".$plan->as_string);
} else {
$self->log->debug('Estimated cost for \''.ref($plan).'\' is '.$cost);
}
return $cost;
}
}
}
package Attean::API::IDPJoinPlanner 0.019 {
use Encode qw(encode);
use Attean::RDF;
use LWP::UserAgent;
use Scalar::Util qw(blessed reftype);
use List::Util qw(reduce);
use List::MoreUtils qw(all any);
use Types::Standard qw(Int ConsumerOf InstanceOf);
use URI::Escape;
use Algorithm::Combinatorics qw(subsets);
use List::Util qw(min);
use Math::Cartesian::Product;
use Moo::Role;
with 'Attean::API::JoinPlanner';
with 'Attean::API::SimpleCostPlanner';
sub joins_for_plan_alternatives {
my $self = shift;
my $model = shift;
my $active_graphs = shift;
my $default_graphs = shift;
my $interesting = shift;
my @args = @_; # each $args[$i] here is an array reference containing alternate plans for element $i
my $k = 3; # this is the batch size over which to do full dynamic programming
# initialize $optPlan{$i} to be a set of alternate plans for evaluating element $i
my %optPlan;
foreach my $i (0 .. $#args) {
$optPlan{$i} = [$self->prune_plans($model, $interesting, $args[$i])];
}
my @todo = (0 .. $#args); # initialize the todo list to all elements
my $next_symbol = 'a'; # when we start batching together sub-plans, we'll rename them with letters (e.g. elements 1, 2, and 4 might become 'a', and then 3, 5, and 'a' become 'b')
# until we've joined all the elements in todo and are left with a set of plans for the join of all elements
while (scalar(@todo) > 1) {
$k = ($k < scalar(@todo)) ? $k : scalar(@todo); # in case we're joining fewer than the batch size
foreach my $i (2 .. $k) { # we've already initialized plans for evaluating single elements; now consider plans for groups of elements (with group sizes 2, 3, ..., $k)
foreach my $s (subsets(\@todo, $i)) { # pick a subset of size $i of the elements that need to be planned
my $s_key = join('.', sort @$s);
$optPlan{$s_key} = [];
foreach my $o (subsets($s)) { # partition the subset s into two (o and not_o)
next if (scalar(@$o) == 0); # only consider proper, non-empty subsets
next if (scalar(@$o) == scalar(@$s)); # only consider proper, non-empty subsets
my $o_key = join('.', sort @$o);
my %o = map { $_ => 1 } @$o;
my $not_o_key = join('.', sort grep { not exists $o{$_} } @$s);
my $lhs = $optPlan{$o_key}; # get the plans for evaluating o
my $rhs = $optPlan{$not_o_key}; # get the plans for evaluating not_o
# compute and store all the possible ways to evaluate s (o ⋈ not_o)
push(@{ $optPlan{$s_key} }, $self->join_plans($model, $active_graphs, $default_graphs, $lhs, $rhs, 'inner'));
$optPlan{$s_key} = [$self->prune_plans($model, $interesting, $optPlan{$s_key})];
}
}
}
# find the minimum cost plan $p that computes the join over $k elements (the elements end up in @v)
my %min_plans;
foreach my $w (subsets(\@todo, $k)) {
my $w_key = join('.', sort @$w);
my $plans = $optPlan{$w_key};
my @costs = map { $self->cost_for_plan($_, $model) => [$_, $w] } @$plans;
my %costs = @costs;
my $min = min keys %costs;
my @min_plans;
while (my ($cost, $data) = splice(@costs, 0, 2)) {
if ($cost == $min) {
push(@min_plans, $data);
}
}
$min_plans{ $min } = \@min_plans;
}
my $min_cost = min keys %min_plans;
my $min_plans = $min_plans{$min_cost};
my @min_plans;
my $min_key;
foreach my $d (@$min_plans) {
my ($p, $v) = @$d;
my $v_key = join('.', sort @$v);
if (not(defined($min_key)) or $min_key eq $v_key) {
push(@min_plans, $p);
$min_key = $v_key;
}
}
# my ($p, $v) = @$min_plan;
# my $v_key = join('.', sort @$v);
# warn "Choosing join for $v_key\n";
# generate a new symbol $t to stand in for $p, the join over the elements in @v
my $t = $next_symbol++;
# remove elements in @v from the todo list, and replace them by the new composite element $t
$optPlan{$t} = [@min_plans];
my %v = map { $_ => 1 } split(/[.]/, $min_key);
push(@todo, $t);
@todo = grep { not exists $v{$_} } @todo;
# also remove subsets of @v from the optPlan hash as they are now covered by $optPlan{$t}
foreach my $o (subsets([keys %v])) {
my $o_key = join('.', sort @$o);
# warn "deleting $o_key\n";
delete $optPlan{$o_key};
}
}
my $final_key = join('.', sort @todo);
# use Data::Dumper;
# warn Dumper($optPlan{$final_key});
return $self->prune_plans($model, $interesting, $optPlan{$final_key});
}
sub prune_plans {
my $self = shift;
my $model = shift;
my $interesting = shift;
my @plans = @{ shift || [] };
no sort 'stable';
my @sorted = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [$self->cost_for_plan($_, $model), $_] } @plans;
if ($self->log->is_trace) {
$self->log->trace('============= Plan iteration separator ==============');
foreach my $plan (@sorted){
$self->log->trace("Cost: " . $self->cost_for_plan($plan, $model) . " for plan:\n". $plan->as_string);
}
}
return splice(@sorted, 0, 5);
}
# Return a cost value for $plan. This value is basically opaque, except
# that it will be used to sort plans by cost when determining which is the
# cheapest plan to evaluate.
sub cost_for_plan {
my $self = shift;
my $plan = shift;
my $model = shift;
Carp::confess "No model given" unless (blessed($model) and $model->does('Attean::API::Model'));
if ($plan->has_cost) {
return $plan->cost;
} else {
if ($model->does('Attean::API::CostPlanner')) {
if (defined(my $cost = $model->cost_for_plan($plan, $self))) {
$plan->cost($cost);
$self->log->info('Model \''.ref($model).'\' did cost planning for \''.ref($plan).'\' and got cost '.$cost);
return $cost;
}
}
my $cost = 1;
my @children = @{ $plan->children };
if ($plan->isa('Attean::Plan::Quad')) {
my @vars = map { $_->value } grep { blessed($_) and $_->does('Attean::API::Variable') } $plan->values;
# This gives a cost increasing at a reasonable pace
$cost = $self->_hsp_heuristic_triple_sum($plan) * scalar(@vars);
} elsif ($plan->isa('Attean::Plan::Table')) {
my $rows = $plan->rows;
$cost = scalar(@$rows);
} elsif ($plan->isa('Attean::Plan::NestedLoopJoin')) {
my $lcost = $self->cost_for_plan($children[0], $model);
my $rcost = $self->cost_for_plan($children[1], $model);
if ($lcost == 0) {
$cost = $rcost;
} elsif ($rcost == 0) {
$cost = $lcost;
} else {
my $mult = $self->_penalize_joins($plan);
# warn "$mult * ($lcost * $rcost) [$children[0] $children[1]]";
$cost = $mult * $lcost * $rcost;
}
} elsif ($plan->isa('Attean::Plan::HashJoin')) {
my $lcost = $self->cost_for_plan($children[0], $model);
my $rcost = $self->cost_for_plan($children[1], $model);
if ($lcost == 0) {
$cost = $rcost;
} elsif ($rcost == 0) {
$cost = $lcost;
} else {
my $mult = $self->_penalize_joins($plan);
# warn "$mult * ($lcost + $rcost)";
$cost = $mult * ($lcost + $rcost);
$cost += ($lcost < $rcost); # To let the plan with cheaper rhs win
}
} elsif ($plan->isa('Attean::Plan::Service')) {
my $scost = 10;
foreach my $c (@{ $plan->children }) {
$scost += $self->cost_for_plan($c, $model);
}
$cost = 5 * $scost;
} elsif ($plan->isa('Attean::Plan::Unique')) {
$cost = 0; # consider a filter on the iterator (like unique) to be essentially free
foreach my $c (@{ $plan->children }) {
$cost += $self->cost_for_plan($c, $model);
}
} else {
foreach my $c (@{ $plan->children }) {
$cost += $self->cost_for_plan($c, $model);
}
}
# Costs must be integers for comparisons to work in the IDP planning algorithm
$cost = int($cost);
$plan->cost($cost);
return $cost;
}
}
# The below function finds a number to aid sorting
# It takes into account Heuristic 1 and 4 of the HSP paper, see REFERENCES
# as well as that it was noted in the text that rdf:type is usually less selective.
# By assigning the integers to nodes, depending on whether they are in
# triple (subject, predicate, object), variables, rdf:type and
# literals, and sum them, they may be sorted. See code for the actual
# values used.
# Denoting s for bound subject, p for bound predicate, a for rdf:type
# as predicate, o for bound object and l for literal object and ? for
# variable, we get the following order, most of which are identical to
# the HSP:
# spl: 6
# spo: 8
# sao: 10
# s?l: 14
# s?o: 16
# ?pl: 25
# ?po: 27
# ?ao: 29
# sp?: 30
# sa?: 32
# ??l: 33
# ??o: 35
# s??: 38
# ?p?: 49
# ?a?: 51
# ???: 57
# Note that this number is not intended as an estimate of selectivity,
# merely a sorting key, but further research may possibly create such
# numbers.
sub _hsp_heuristic_triple_sum {
my ($self, $t) = @_;
my $sum = 0;
if ($t->subject->does('Attean::API::Variable')) {
$sum = 20;
} else {
$sum = 1;
}
if ($t->predicate->does('Attean::API::Variable')) {
$sum += 10;
} else {
if ($t->predicate->equals(iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'))) {
$sum += 4;
} else {
$sum += 2;
}
}
if ($t->object->does('Attean::API::Variable')) {
$sum += 27;
} elsif ($t->object->does('Attean::API::Literal')) {
$sum += 3;
} else {
$sum += 5;
}
return $sum;
}
# The following method returns a factor used to penalize certain types of joins.
# It penalizes cartesian joins heavily, but also uses HSP Heuristic 2 (see REFERENCES)
sub _penalize_joins {
my ($self, $plan) = @_;
my $jv = $plan->join_variables;
my @children = @{ $plan->children };
my $mult = 1;
if (scalar(@$jv)) {
if ( all { $_->isa('Attean::Plan::Quad') } @children[0..1]) {
my $var = ${$jv}[0]; # We will join on this
my @lnodes = $children[0]->values;
my @rnodes = $children[1]->values;
# Now, find where the join variables are in the triple patterns
my %joinpos;
for (my $i = 0; $i <= 2; $i++) {
if ($lnodes[$i]->does('Attean::API::Variable') && $lnodes[$i]->value eq $var) {
$joinpos{l} = $i;
}
if ($rnodes[$i]->does('Attean::API::Variable') && $rnodes[$i]->value eq $var) {
$joinpos{r} = $i;
}
last if scalar keys(%joinpos) >= 2; # Perhaps a bit premature optimization
}
my $joinpos = join("", sort values(%joinpos)); # We can now match on this string
my %costs = ('12' => 1.1, # The penalty numbers come mostly out from thin air
'01' => 1.2,
'02' => 1.5,
'22' => 1.6,
'00' => 1.8,
'11' => 2);
if (exists $costs{$joinpos}) {
$mult = $costs{$joinpos};
}
#warn "Penalty: $mult for quads:\n" . $children[0]->as_string . $children[1]->as_string
}
} else {
$mult = 5; # penalize cartesian joins
}
return $mult;
}
}
1;
__END__
=back
=head1 BUGS
Please report any bugs or feature requests to through the GitHub web interface
at L<https://github.com/kasei/attean/issues>.
=head1 REFERENCES
The seminal reference for Iterative Dynamic Programming is "Iterative
dynamic programming: a new class of query optimization algorithms" by
D. Kossmann and K. Stocker, ACM Transactions on Database Systems
(2000).
The heuristics to order triple patterns in this module is
influenced by L<The ICS-FORTH Heuristics-based SPARQL Planner
(HSP)|http://www.ics.forth.gr/isl/index_main.php?l=e&c=645>.
=head1 SEE ALSO
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2014--2018 Gregory Todd Williams.
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|