| 1 | # RDF::Query::Plan::Sort | ||||||||||||
| 2 | # ----------------------------------------------------------------------------- | ||||||||||||
| 3 | |||||||||||||
| 4 | =head1 NAME | ||||||||||||
| 5 | |||||||||||||
| 6 | RDF::Query::Plan::Sort - Executable query plan for Sorts. | ||||||||||||
| 7 | |||||||||||||
| 8 | =head1 VERSION | ||||||||||||
| 9 | |||||||||||||
| 10 | This document describes RDF::Query::Plan::Sort 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::Sort; | ||||||||||||
| 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 Scalar::Util qw(refaddr); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 35 | ▶ | use base qw(RDF::Query::Plan); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | ###################################################################### | ||||||||||||
| 29 | |||||||||||||
| 30 | 35 | our ($VERSION); | |||||||||||
| 31 | 35 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | 35 | $VERSION = '2.919'; | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | ###################################################################### | ||||||||||||
| 36 | |||||||||||||
| 37 | |||||||||||||
| 38 | =item C<< new ( $pattern, [ $expr1, $rev1 ], ... ) >> | ||||||||||||
| 39 | |||||||||||||
| 40 | =cut | ||||||||||||
| 41 | |||||||||||||
| 42 | 12 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 43 | 12 | my $class = shift; | |||||||||||
| 44 | 12 | my $plan = shift; | |||||||||||
| 45 | 12 | my @exprs = @_; | |||||||||||
| 46 | 12 | foreach my $e (@exprs) { | |||||||||||
| 47 | 12 | ▶ | $e->[1] ||= 0; | ||||||||||
|
Condition100%2/2
$e->[1] ||= 0
MC/DC100%1/1
$e->[1] ||= 0
$e->[1]
Truth table100%2/2
A
A= $e->[1]
| |||||||||||||
| 48 | } | ||||||||||||
| 49 | 12 | my $self = $class->SUPER::new( $plan, \@exprs ); | |||||||||||
| 50 | 12 | $self->[0]{referenced_variables} = [ $plan->referenced_variables ]; | |||||||||||
| 51 | 12 | return $self; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | =item C<< execute ( $execution_context ) >> | ||||||||||||
| 55 | |||||||||||||
| 56 | =cut | ||||||||||||
| 57 | |||||||||||||
| 58 | 12 | ▶ | sub execute ($) { | ||||||||||
|
Subroutine
execute: called CC=7 SCAR=20.9
Pod
documented
| |||||||||||||
| 59 | 12 | my $self = shift; | |||||||||||
| 60 | 12 | my $context = shift; | |||||||||||
| 61 | 12 | $self->[0]{delegate} = $context->delegate; | |||||||||||
| 62 | 12 | ▶ | if ($self->state == $self->OPEN) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 0 | throw RDF::Query::Error::ExecutionError -text => "SORT plan can't be executed while already open"; | |||||||||||
| 64 | } | ||||||||||||
| 65 | 12 | my $plan = $self->[1]; | |||||||||||
| 66 | 12 | $plan->execute( $context ); | |||||||||||
| 67 | |||||||||||||
| 68 | 12 | my $l = Log::Log4perl->get_logger("rdf.query.plan.sort"); | |||||||||||
| 69 | 12 | $l->trace("executing sort"); | |||||||||||
| 70 | 12 | ▶ | if ($plan->state == $self->OPEN) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 12 | my $exprs = $self->[2]; | |||||||||||
| 72 | 12 | my @rows = $plan->get_all; | |||||||||||
| 73 | 12 | ▶ | if ($l->is_trace) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 0 | $l->trace("sorting result list:"); | |||||||||||
| 75 | 0 | $l->trace("- $_") foreach (@rows); | |||||||||||
| 76 | } | ||||||||||||
| 77 | 12 | my $query = $context->query; | |||||||||||
| 78 | |||||||||||||
| 79 | 35 | ▶ | use sort 'stable'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 80 | 12 | my @sorted = sort { _cmp_rows( $context, $exprs, $a, $b ) } @rows; | |||||||||||
| 81 | 12 | ▶ | if ($l->is_trace) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 0 | $l->trace("sorted list:"); | |||||||||||
| 83 | 0 | $l->trace("- $_") foreach (@sorted); | |||||||||||
| 84 | } | ||||||||||||
| 85 | 12 | $self->[0]{rows} = \@sorted; | |||||||||||
| 86 | 12 | $self->state( $self->OPEN ); | |||||||||||
| 87 | } else { | ||||||||||||
| 88 | 0 | warn "could not execute plan in distinct"; | |||||||||||
| 89 | } | ||||||||||||
| 90 | 12 | $self; | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | =item C<< next >> | ||||||||||||
| 94 | |||||||||||||
| 95 | =cut | ||||||||||||
| 96 | |||||||||||||
| 97 | 32 | ▶ | sub next { | ||||||||||
|
Subroutine
next: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||
| 98 | 32 | my $self = shift; | |||||||||||
| 99 | 32 | ▶ | unless ($self->state == $self->OPEN) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 0 | throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open SORT"; | |||||||||||
| 101 | } | ||||||||||||
| 102 | 32 | my $bindings = shift(@{ $self->[0]{rows} }); | |||||||||||
| 103 | 32 | ▶ | if (my $d = $self->delegate) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 0 | $d->log_result( $self, $bindings ); | |||||||||||
| 105 | } | ||||||||||||
| 106 | 32 | return $bindings; | |||||||||||
| 107 | } | ||||||||||||
| 108 | |||||||||||||
| 109 | =item C<< close >> | ||||||||||||
| 110 | |||||||||||||
| 111 | =cut | ||||||||||||
| 112 | |||||||||||||
| 113 | 12 | ▶ | sub close { | ||||||||||
|
Subroutine
close: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||
| 114 | 12 | my $self = shift; | |||||||||||
| 115 | 12 | ▶ | unless ($self->state == $self->OPEN) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 116 | 0 | throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open SORT"; | |||||||||||
| 117 | } | ||||||||||||
| 118 | 12 | delete $self->[0]{rows}; | |||||||||||
| 119 | 12 | $self->[1]->close(); | |||||||||||
| 120 | 12 | $self->SUPER::close(); | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 30 | ▶ | sub _cmp_rows { | ||||||||||
|
Subroutine
_cmp_rows: called CC=5 SCAR=16.1
| |||||||||||||
| 124 | 30 | my $context = shift; | |||||||||||
| 125 | 30 | my $exprs = shift; | |||||||||||
| 126 | 30 | my $a = shift; | |||||||||||
| 127 | 30 | my $b = shift; | |||||||||||
| 128 | |||||||||||||
| 129 | 30 | my $l = Log::Log4perl->get_logger("rdf.query.plan.sort"); | |||||||||||
| 130 | 30 | ▶ | my $query = $context->query || 'RDF::Query'; | ||||||||||
|
Condition50%1/2
$context->query || 'RDF::Query'
MC/DC0%0/1
$context->query || 'RDF::Query'
$context->query
Truth table50%1/2
A
A= $context->query
| |||||||||||||
| 131 | 30 | my $bridge = $context->model; | |||||||||||
| 132 | |||||||||||||
| 133 | 35 | ▶ | no warnings 'numeric'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 134 | 35 | ▶ | no warnings 'uninitialized'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 135 | 30 | foreach my $data (@$exprs) { | |||||||||||
| 136 | 30 | my ($expr, $rev) = @$data; | |||||||||||
| 137 | 30 | my $a_val = $query->var_or_expr_value( $a, $expr, $context ); | |||||||||||
| 138 | 30 | my $b_val = $query->var_or_expr_value( $b, $expr, $context ); | |||||||||||
| 139 | 30 | local($RDF::Query::Node::Literal::LAZY_COMPARISONS) = 1; | |||||||||||
| 140 | 30 | $l->trace("comparing $a_val <=> $b_val"); | |||||||||||
| 141 | 30 | my $cmp = $a_val <=> $b_val; | |||||||||||
| 142 | 30 | ▶ | if ($cmp != 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 20 | ▶ | if ($rev) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | 6 | $cmp *= -1; | |||||||||||
| 145 | } | ||||||||||||
| 146 | 20 | $l->trace("==> $cmp"); | |||||||||||
| 147 | 20 | return $cmp; | |||||||||||
| 148 | } else { | ||||||||||||
| 149 | } | ||||||||||||
| 150 | } | ||||||||||||
| 151 | 10 | $l->trace("==> 0"); | |||||||||||
| 152 | 10 | return 0; | |||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | =item C<< pattern >> | ||||||||||||
| 156 | |||||||||||||
| 157 | Returns the query plan that will be used to produce the data to be sorted. | ||||||||||||
| 158 | |||||||||||||
| 159 | =cut | ||||||||||||
| 160 | |||||||||||||
| 161 | 7 | ▶ | sub pattern { | ||||||||||
|
Subroutine
pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 162 | 7 | my $self = shift; | |||||||||||
| 163 | 7 | return $self->[1]; | |||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | =item C<< distinct >> | ||||||||||||
| 167 | |||||||||||||
| 168 | Returns true if the pattern is guaranteed to return distinct results. | ||||||||||||
| 169 | |||||||||||||
| 170 | =cut | ||||||||||||
| 171 | |||||||||||||
| 172 | 7 | ▶ | sub distinct { | ||||||||||
|
Subroutine
distinct: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 173 | 7 | my $self = shift; | |||||||||||
| 174 | 7 | return $self->pattern->distinct; | |||||||||||
| 175 | } | ||||||||||||
| 176 | |||||||||||||
| 177 | =item C<< ordered >> | ||||||||||||
| 178 | |||||||||||||
| 179 | Returns true if the pattern is guaranteed to return ordered results. | ||||||||||||
| 180 | |||||||||||||
| 181 | =cut | ||||||||||||
| 182 | |||||||||||||
| 183 | 12 | ▶ | sub ordered { | ||||||||||
|
Subroutine
ordered: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 184 | 12 | my $self = shift; | |||||||||||
| 185 | 12 | my $sort = $self->[2]; | |||||||||||
| 186 | |||||||||||||
| 187 | 12 | ▶ | return [ map { [ $_->[0], ($_->[1] ? 'DESC' : 'ASC') ] } @$sort ]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | } | ||||||||||||
| 189 | |||||||||||||
| 190 | =item C<< plan_node_name >> | ||||||||||||
| 191 | |||||||||||||
| 192 | Returns the string name of this plan node, suitable for use in serialization. | ||||||||||||
| 193 | |||||||||||||
| 194 | =cut | ||||||||||||
| 195 | |||||||||||||
| 196 | 0 | ▶ | sub plan_node_name { | ||||||||||
|
Subroutine
plan_node_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 197 | 0 | return 'order'; | |||||||||||
| 198 | } | ||||||||||||
| 199 | |||||||||||||
| 200 | =item C<< plan_prototype >> | ||||||||||||
| 201 | |||||||||||||
| 202 | Returns a list of scalar identifiers for the type of the content (children) | ||||||||||||
| 203 | nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable | ||||||||||||
| 204 | identifiers. | ||||||||||||
| 205 | |||||||||||||
| 206 | =cut | ||||||||||||
| 207 | |||||||||||||
| 208 | 0 | ▶ | sub plan_prototype { | ||||||||||
|
Subroutine
plan_prototype: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 209 | 0 | my $self = shift; | |||||||||||
| 210 | 0 | return qw(P *\wE); | |||||||||||
| 211 | } | ||||||||||||
| 212 | |||||||||||||
| 213 | =item C<< plan_node_data >> | ||||||||||||
| 214 | |||||||||||||
| 215 | Returns the data for this plan node that corresponds to the values described by | ||||||||||||
| 216 | the signature returned by C<< plan_prototype >>. | ||||||||||||
| 217 | |||||||||||||
| 218 | =cut | ||||||||||||
| 219 | |||||||||||||
| 220 | 0 | ▶ | sub plan_node_data { | ||||||||||
|
Subroutine
plan_node_data: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 221 | 0 | my $self = shift; | |||||||||||
| 222 | 0 | my $exprs = $self->[2]; | |||||||||||
| 223 | 0 | ▶ | return ($self->pattern, map { [ ($_->[1] == 0 ? 'asc' : 'desc'), $_->[0] ] } @$exprs); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 224 | } | ||||||||||||
| 225 | |||||||||||||
| 226 | =item C<< graph ( $g ) >> | ||||||||||||
| 227 | |||||||||||||
| 228 | =cut | ||||||||||||
| 229 | |||||||||||||
| 230 | 0 | ▶ | sub graph { | ||||||||||
|
Subroutine
graph: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 231 | 0 | my $self = shift; | |||||||||||
| 232 | 0 | my $g = shift; | |||||||||||
| 233 | 0 | my $c = $self->pattern->graph( $g ); | |||||||||||
| 234 | 0 | my $expr = join(' ', map { $_->sse( {}, "" ) } @{ $self->[2] }); | |||||||||||
| 235 | 0 | $g->add_node( "$self", label => "Sort ($expr)" . $self->graph_labels ); | |||||||||||
| 236 | 0 | $g->add_edge( "$self", $c ); | |||||||||||
| 237 | 0 | return "$self"; | |||||||||||
| 238 | } | ||||||||||||
| 239 | |||||||||||||
| 240 | =item C<< explain >> | ||||||||||||
| 241 | |||||||||||||
| 242 | Returns a string serialization of the plan appropriate for display on the | ||||||||||||
| 243 | command line. | ||||||||||||
| 244 | |||||||||||||
| 245 | =cut | ||||||||||||
| 246 | |||||||||||||
| 247 | 0 | ▶ | sub explain { | ||||||||||
|
Subroutine
explain: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 248 | 0 | my $self = shift; | |||||||||||
| 249 | 0 | my $s = shift; | |||||||||||
| 250 | 0 | my $count = shift; | |||||||||||
| 251 | 0 | my $indent = $s x $count; | |||||||||||
| 252 | 0 | my $type = $self->plan_node_name; | |||||||||||
| 253 | 0 | my $string = sprintf("%s%s (0x%x)\n", $indent, $type, refaddr($self)); | |||||||||||
| 254 | 0 | $string .= "${indent}${s}sory by:\n"; | |||||||||||
| 255 | 0 | my $exprs = $self->[2]; | |||||||||||
| 256 | 0 | foreach my $e (@$exprs) { | |||||||||||
| 257 | 0 | ▶ | my $dir = ($e->[1] == 0 ? 'asc ' : 'desc '); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 258 | 0 | $string .= "${indent}${s}${s}${dir}" . $e->[0] . "\n"; | |||||||||||
| 259 | } | ||||||||||||
| 260 | 0 | $string .= $self->pattern->explain( $s, $count+1 ); | |||||||||||
| 261 | 0 | return $string; | |||||||||||
| 262 | } | ||||||||||||
| 263 | |||||||||||||
| 264 | |||||||||||||
| 265 | 35 | 1; | |||||||||||
| 266 | |||||||||||||
| 267 | __END__ | ||||||||||||