| 1 | # RDF::Query::BGPOptimizer | ||||||||||
| 2 | # ----------------------------------------------------------------------------- | ||||||||||
| 3 | |||||||||||
| 4 | =head1 NAME | ||||||||||
| 5 | |||||||||||
| 6 | RDF::Query::BGPOptimizer - Optimizer for ordering the joins of triple patterns in a BGP | ||||||||||
| 7 | |||||||||||
| 8 | =head1 VERSION | ||||||||||
| 9 | |||||||||||
| 10 | This document describes RDF::Query::BGPOptimizer version 2.919. | ||||||||||
| 11 | |||||||||||
| 12 | =head1 STATUS | ||||||||||
| 13 | |||||||||||
| 14 | This module's API and functionality should be considered unstable. | ||||||||||
| 15 | In the future, this module may change in backwards-incompatible ways, | ||||||||||
| 16 | or be removed entirely. If you need functionality that this module provides, | ||||||||||
| 17 | please L<get in touch|http://www.perlrdf.org/>. | ||||||||||
| 18 | |||||||||||
| 19 | =head1 METHODS | ||||||||||
| 20 | |||||||||||
| 21 | =over 4 | ||||||||||
| 22 | |||||||||||
| 23 | =cut | ||||||||||
| 24 | |||||||||||
| 25 | package RDF::Query::BGPOptimizer; | ||||||||||
| 26 | |||||||||||
| 27 | 35 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 35 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 35 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 35 | ▶ | use List::Util qw(reduce); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 35 | ▶ | use Scalar::Util qw(blessed reftype refaddr); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 35 | ▶ | use RDF::Query::Error qw(:try); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | ###################################################################### | ||||||||||
| 35 | |||||||||||
| 36 | 35 | our ($VERSION); | |||||||||
| 37 | 35 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 35 | $VERSION = '2.919'; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | ###################################################################### | ||||||||||
| 42 | |||||||||||
| 43 | =item C<< ordered_triples ( $context, @triples ) >> | ||||||||||
| 44 | |||||||||||
| 45 | Returns a list of triples, ordered so as to optimize a left-deep join plan based | ||||||||||
| 46 | on the frequency counts provided by the underlying model. | ||||||||||
| 47 | |||||||||||
| 48 | =cut | ||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | sub ordered_triples { | ||||||||
|
Subroutine
ordered_triples: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||
| 51 | 0 | my $self = shift; | |||||||||
| 52 | 0 | my $context = shift; | |||||||||
| 53 | 0 | my @triples = @_; | |||||||||
| 54 | |||||||||||
| 55 | 0 | my $model = $context->model; | |||||||||
| 56 | |||||||||||
| 57 | 0 | my %vars; | |||||||||
| 58 | my %seen; | ||||||||||
| 59 | my @weighted = map { | ||||||||||
| 60 | 0 | my $triple = RDF::Query::Plan::Triple->new( $_->nodes ); | |||||||||
| 61 | 0 | [ $_, $self->_cost( $triple, $context ) ] | |||||||||
| 62 | } @triples; | ||||||||||
| 63 | 0 | my %triples = map { refaddr($_->[0]) => $_ } @weighted; | |||||||||
| 64 | 0 | my @ordered = sort { $a->[1] <=> $b->[1] } @weighted; | |||||||||
| 65 | |||||||||||
| 66 | 0 | foreach my $t (@triples) { | |||||||||
| 67 | 0 | my @vars = $self->_triple_vars( $t ); | |||||||||
| 68 | 0 | foreach my $name (@vars) { | |||||||||
| 69 | 0 | ▶ | push( @{ $vars{ $name } }, $t ) unless ($seen{ $name }{ refaddr($t) }++); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | my %edges; | |||||||||
| 74 | 0 | foreach my $name (keys %vars) { | |||||||||
| 75 | 0 | my @triples = @{ $vars{ $name } }; | |||||||||
| 76 | 0 | foreach my $t (@triples) { | |||||||||
| 77 | 0 | my $ta = refaddr($t); | |||||||||
| 78 | 0 | foreach my $u (@triples) { | |||||||||
| 79 | 0 | my $ua = refaddr($u); | |||||||||
| 80 | 0 | ▶ | next if ($ta == $ua); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | $edges{ $ta }{ $ua } = $u; | |||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | |||||||||||
| 87 | 0 | my @final; | |||||||||
| 88 | my %used; | ||||||||||
| 89 | 0 | my $start = shift(@ordered); | |||||||||
| 90 | 0 | $used{ refaddr($start) }++; | |||||||||
| 91 | 0 | push(@final, $start); | |||||||||
| 92 | |||||||||||
| 93 | 0 | my @seen = refaddr($start->[0]); | |||||||||
| 94 | 0 | my $count = 0; | |||||||||
| 95 | 0 | ▶ | while (@ordered) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | ▶ | if (++$count > scalar(@triples)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | die "loop in BGPOptimizer (?)"; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | my @frontier = grep { not($used{refaddr($_)}) } map { $triples{ $_ } } map { keys(%{ $edges{ $_ } }) } @seen; | |||||||||
| 101 | 0 | my @orderedf = sort { $a->[1] <=> $b->[1] } @frontier; | |||||||||
| 102 | 0 | ▶ | if (@orderedf) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | my $next = shift(@orderedf); | |||||||||
| 104 | 0 | my $addr = refaddr($next); | |||||||||
| 105 | 0 | $used{ $addr }++; | |||||||||
| 106 | 0 | push(@final, $next); | |||||||||
| 107 | 0 | push(@seen, refaddr($next->[0])); | |||||||||
| 108 | 0 | @ordered = grep { refaddr($_) != $addr } @ordered; | |||||||||
| 109 | } else { | ||||||||||
| 110 | 0 | my $next = shift(@ordered); | |||||||||
| 111 | 0 | my $addr = refaddr($next); | |||||||||
| 112 | 0 | $used{ $addr }++; | |||||||||
| 113 | 0 | push(@final, $next); | |||||||||
| 114 | 0 | push(@seen, refaddr($next->[0])); | |||||||||
| 115 | } | ||||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 0 | return map { $_->[0] } @final; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 0 | ▶ | sub _cost { | ||||||||
|
Subroutine
_cost: not called CC=1 SCAR=6.9
| |||||||||||
| 122 | 0 | my $self = shift; | |||||||||
| 123 | 0 | my $pattern = shift; | |||||||||
| 124 | 0 | my $context = shift; | |||||||||
| 125 | 0 | my $l = Log::Log4perl->get_logger("rdf.query.bgpoptimizer"); | |||||||||
| 126 | 0 | my $bf = $pattern->bf( $context ); | |||||||||
| 127 | 0 | my $f = ($bf =~ tr/f//); | |||||||||
| 128 | 0 | my $r = $f / 3; | |||||||||
| 129 | 0 | $l->debug( "Pattern has bf representation '$bf'" ); | |||||||||
| 130 | 0 | $l->debug( "There are $f of 3 free variables" ); | |||||||||
| 131 | 0 | $l->debug( 'Estimated cardinality of triple is : ' . $r ); | |||||||||
| 132 | |||||||||||
| 133 | # round the cardinality to an integer | ||||||||||
| 134 | 0 | return int($r + .5 * ($r <=> 0)); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 0 | ▶ | sub _triple_vars { | ||||||||
|
Subroutine
_triple_vars: not called CC=4 SCAR=30.0
| |||||||||||
| 138 | 0 | my $self = shift; | |||||||||
| 139 | 0 | my $t = shift; | |||||||||
| 140 | 0 | my @nodes = $t->nodes; | |||||||||
| 141 | 0 | my (@vars, %seen); | |||||||||
| 142 | 0 | foreach my $n (@nodes) { | |||||||||
| 143 | 0 | ▶ | if ($n->isa('RDF::Trine::Node::Variable')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 0 | my $name = $n->name; | |||||||||
| 145 | 0 | ▶ | push(@vars, $name) unless ($seen{ $name }++); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | } | ||||||||||
| 147 | } | ||||||||||
| 148 | 0 | return @vars; | |||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | 35 | 1; | |||||||||
| 152 | |||||||||||
| 153 | __END__ | ||||||||||