| 1 | # RDF::Query::Plan::Union | ||||||||||
| 2 | # ----------------------------------------------------------------------------- | ||||||||||
| 3 | |||||||||||
| 4 | =head1 NAME | ||||||||||
| 5 | |||||||||||
| 6 | RDF::Query::Plan::Union - Executable query plan for unions. | ||||||||||
| 7 | |||||||||||
| 8 | =head1 VERSION | ||||||||||
| 9 | |||||||||||
| 10 | This document describes RDF::Query::Plan::Union version 2.919. | ||||||||||
| 11 | |||||||||||
| 12 | =head1 METHODS | ||||||||||
| 13 | |||||||||||
| 14 | Beyond the methods documented below, this class inherits methods from the | ||||||||||
| 15 | L<RDF::Query::Plan> class. | ||||||||||
| 16 | |||||||||||
| 17 | =over 4 | ||||||||||
| 18 | |||||||||||
| 19 | =cut | ||||||||||
| 20 | |||||||||||
| 21 | package RDF::Query::Plan::Union; | ||||||||||
| 22 | |||||||||||
| 23 | 35 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 35 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 35 | ▶ | use base qw(RDF::Query::Plan); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 35 | ▶ | use Scalar::Util qw(blessed refaddr); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 35 | ▶ | use RDF::Query::ExecutionContext; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | ###################################################################### | ||||||||||
| 32 | |||||||||||
| 33 | 35 | our ($VERSION); | |||||||||
| 34 | 35 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 35 | $VERSION = '2.919'; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | ###################################################################### | ||||||||||
| 39 | |||||||||||
| 40 | =item C<< new ( $lhs, $rhs ) >> | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 45 | 2 | my $class = shift; | |||||||||
| 46 | 2 | my ($lhs, $rhs) = @_; | |||||||||
| 47 | 2 | my $self = $class->SUPER::new( [ $lhs, $rhs ] ); | |||||||||
| 48 | 2 | my %vars; | |||||||||
| 49 | 2 | foreach my $v ($lhs->referenced_variables, $rhs->referenced_variables) { | |||||||||
| 50 | 8 | $vars{ $v }++; | |||||||||
| 51 | } | ||||||||||
| 52 | 2 | $self->[0]{referenced_variables} = [ keys %vars ]; | |||||||||
| 53 | 2 | return $self; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | =item C<< execute ( $execution_context ) >> | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | 2 | ▶ | sub execute ($) { | ||||||||
|
Subroutine
execute: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 61 | 2 | my $self = shift; | |||||||||
| 62 | 2 | my $context = shift; | |||||||||
| 63 | 2 | $self->[0]{delegate} = $context->delegate; | |||||||||
| 64 | 2 | ▶ | if ($self->state == $self->OPEN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | throw RDF::Query::Error::ExecutionError -text => "BGP plan can't be executed while already open"; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 2 | my $iter = $self->[1][0]; | |||||||||
| 69 | 2 | $iter->execute( $context ); | |||||||||
| 70 | |||||||||||
| 71 | 2 | ▶ | if ($iter->state == $self->OPEN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 2 | $self->[0]{iter} = $iter; | |||||||||
| 73 | 2 | $self->[0]{idx} = 0; | |||||||||
| 74 | 2 | $self->[0]{context} = $context; | |||||||||
| 75 | 2 | $self->state( $self->OPEN ); | |||||||||
| 76 | } else { | ||||||||||
| 77 | 0 | warn "no iterator in execute()"; | |||||||||
| 78 | } | ||||||||||
| 79 | 2 | $self; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | =item C<< next >> | ||||||||||
| 83 | |||||||||||
| 84 | =cut | ||||||||||
| 85 | |||||||||||
| 86 | 16 | ▶ | sub next { | ||||||||
|
Subroutine
next: called CC=8 SCAR=21.4
Pod
documented
| |||||||||||
| 87 | 16 | my $self = shift; | |||||||||
| 88 | 16 | my $l = Log::Log4perl->get_logger("rdf.query.plan.union"); | |||||||||
| 89 | 16 | ▶ | unless ($self->state == $self->OPEN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open BGP"; | |||||||||
| 91 | } | ||||||||||
| 92 | 16 | my $iter = $self->[0]{iter}; | |||||||||
| 93 | 16 | ▶ | return undef unless ($iter); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 16 | my $row = $iter->next; | |||||||||
| 95 | 16 | ▶ | if (defined($row)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 12 | $l->trace( "union row: $row" ); | |||||||||
| 97 | 12 | ▶ | if (my $d = $self->delegate) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | $d->log_result( $self, $row ); | |||||||||
| 99 | } | ||||||||||
| 100 | 12 | return $row; | |||||||||
| 101 | } else { | ||||||||||
| 102 | 4 | $self->[0]{iter} = undef; | |||||||||
| 103 | 4 | ▶ | if ($self->[0]{idx} < $#{ $self->[1] }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 2 | $iter->close(); | |||||||||
| 105 | 2 | $self->[0]{idx}++; | |||||||||
| 106 | 2 | my $index = $self->[0]{idx}; | |||||||||
| 107 | 2 | my $iter = $self->[1][ $index ]; | |||||||||
| 108 | 2 | $iter->execute( $self->[0]{context} ); | |||||||||
| 109 | 2 | ▶ | if ($iter->state == $self->OPEN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 2 | $l->trace( "union moving to next branch" ); | |||||||||
| 111 | 2 | $self->[0]{iter} = $iter; | |||||||||
| 112 | 2 | my $bindings = $self->next; | |||||||||
| 113 | 2 | ▶ | if (my $d = $self->delegate) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | $d->log_result( $self, $bindings ); | |||||||||
| 115 | } | ||||||||||
| 116 | 2 | return $bindings; | |||||||||
| 117 | } else { | ||||||||||
| 118 | 0 | throw RDF::Query::Error::ExecutionError -text => "execute() on RHS of UNION failed during next()"; | |||||||||
| 119 | } | ||||||||||
| 120 | } else { | ||||||||||
| 121 | 2 | $l->trace( "union reached end of last branch" ); | |||||||||
| 122 | 2 | $iter->close(); | |||||||||
| 123 | 2 | delete $self->[0]{iter}; | |||||||||
| 124 | 2 | return undef; | |||||||||
| 125 | } | ||||||||||
| 126 | } | ||||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | =item C<< close >> | ||||||||||
| 130 | |||||||||||
| 131 | =cut | ||||||||||
| 132 | |||||||||||
| 133 | 2 | ▶ | sub close { | ||||||||
|
Subroutine
close: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||
| 134 | 2 | my $self = shift; | |||||||||
| 135 | 2 | ▶ | unless ($self->state == $self->OPEN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open BGP"; | |||||||||
| 137 | } | ||||||||||
| 138 | 2 | ▶ | if (my $iter = $self->[0]{iter}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | $iter->close(); | |||||||||
| 140 | 0 | delete $self->[0]{iter}; | |||||||||
| 141 | 0 | delete $self->[0]{idx}; | |||||||||
| 142 | } | ||||||||||
| 143 | 2 | $self->SUPER::close(); | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | =item C<< lhs >> | ||||||||||
| 147 | |||||||||||
| 148 | Returns the left-hand-side plan to the union. | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | 0 | ▶ | sub lhs { | ||||||||
|
Subroutine
lhs: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 153 | 0 | my $self = shift; | |||||||||
| 154 | 0 | return $self->[1][0]; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | =item C<< rhs >> | ||||||||||
| 158 | |||||||||||
| 159 | Returns the right-hand-side plan to the union. | ||||||||||
| 160 | |||||||||||
| 161 | =cut | ||||||||||
| 162 | |||||||||||
| 163 | 0 | ▶ | sub rhs { | ||||||||
|
Subroutine
rhs: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 164 | 0 | my $self = shift; | |||||||||
| 165 | 0 | return $self->[1][1]; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | =item C<< distinct >> | ||||||||||
| 169 | |||||||||||
| 170 | Returns true if the pattern is guaranteed to return distinct results. | ||||||||||
| 171 | |||||||||||
| 172 | =cut | ||||||||||
| 173 | |||||||||||
| 174 | 1 | ▶ | sub distinct { | ||||||||
|
Subroutine
distinct: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 175 | 1 | return 0; | |||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | =item C<< ordered >> | ||||||||||
| 179 | |||||||||||
| 180 | Returns true if the pattern is guaranteed to return ordered results. | ||||||||||
| 181 | |||||||||||
| 182 | =cut | ||||||||||
| 183 | |||||||||||
| 184 | 2 | ▶ | sub ordered { | ||||||||
|
Subroutine
ordered: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 185 | 2 | return []; | |||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | =item C<< plan_node_name >> | ||||||||||
| 189 | |||||||||||
| 190 | Returns the string name of this plan node, suitable for use in serialization. | ||||||||||
| 191 | |||||||||||
| 192 | =cut | ||||||||||
| 193 | |||||||||||
| 194 | 0 | ▶ | sub plan_node_name { | ||||||||
|
Subroutine
plan_node_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 195 | 0 | return 'union'; | |||||||||
| 196 | } | ||||||||||
| 197 | |||||||||||
| 198 | =item C<< plan_prototype >> | ||||||||||
| 199 | |||||||||||
| 200 | Returns a list of scalar identifiers for the type of the content (children) | ||||||||||
| 201 | nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable | ||||||||||
| 202 | identifiers. | ||||||||||
| 203 | |||||||||||
| 204 | =cut | ||||||||||
| 205 | |||||||||||
| 206 | 0 | ▶ | sub plan_prototype { | ||||||||
|
Subroutine
plan_prototype: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 207 | 0 | my $self = shift; | |||||||||
| 208 | 0 | return qw(P P); | |||||||||
| 209 | } | ||||||||||
| 210 | |||||||||||
| 211 | =item C<< plan_node_data >> | ||||||||||
| 212 | |||||||||||
| 213 | Returns the data for this plan node that corresponds to the values described by | ||||||||||
| 214 | the signature returned by C<< plan_prototype >>. | ||||||||||
| 215 | |||||||||||
| 216 | =cut | ||||||||||
| 217 | |||||||||||
| 218 | 0 | ▶ | sub plan_node_data { | ||||||||
|
Subroutine
plan_node_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 219 | 0 | my $self = shift; | |||||||||
| 220 | 0 | my $expr = $self->[2]; | |||||||||
| 221 | 0 | return ($self->lhs, $self->rhs); | |||||||||
| 222 | } | ||||||||||
| 223 | |||||||||||
| 224 | =item C<< graph ( $g ) >> | ||||||||||
| 225 | |||||||||||
| 226 | =cut | ||||||||||
| 227 | |||||||||||
| 228 | 0 | ▶ | sub graph { | ||||||||
|
Subroutine
graph: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 229 | 0 | my $self = shift; | |||||||||
| 230 | 0 | my $g = shift; | |||||||||
| 231 | 0 | my ($l, $r) = map { $_->graph( $g ) } ($self->lhs, $self->rhs); | |||||||||
| 232 | 0 | $g->add_node( "$self", label => "Union" . $self->graph_labels ); | |||||||||
| 233 | 0 | $g->add_edge( "$self", $l ); | |||||||||
| 234 | 0 | $g->add_edge( "$self", $r ); | |||||||||
| 235 | 0 | return "$self"; | |||||||||
| 236 | } | ||||||||||
| 237 | |||||||||||
| 238 | 35 | 1; | |||||||||
| 239 | |||||||||||
| 240 | __END__ | ||||||||||