Query/Plan/Aggregate.pm

filter: Statementstmt 86.9% 259 / 298 Branchbran 67.4% 89 / 132 Conditioncond 32.9% 26 / 79 MC/DCmcdc 7.8% 3 / 38 Subroutinesub 81.8% 18 / 22 Podpod 100% 11 / 11 totaltotal 70.0% 406 / 580 CC 103 SCAR 43.3
CC
103
Cov
73%
__ANON__
135.6
execute
50.0
_node_type
10.5
CRAP 76.0
« Query/Functions/SPARQL.pm ↑ Summary Query/BGPOptimizer.pm »
1 # RDF::Query::Plan::Aggregate
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Plan::Aggregate - Executable query plan for Aggregates.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Plan::Aggregate 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::Aggregate;
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 35 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 35 ▶use RDF::Query::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 35 ▶use RDF::Query::Node qw(literal);
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 ( $pattern, \@group_by, expressions => [ [ $alias, $op, \%options, @attributes ], ... ] ) >>
41  
42 =cut
43  
44 16 ▶sub new {
Subroutine new: called CC=4 SCAR=14.5
Pod documented
45 16 my $class = shift;
46 16 my $plan = shift;
47 16 my $groupby = shift;
48 16 my %args = @_;
49 16 ▶ my @ops = @{ $args{ 'expressions' } || [] };
Condition50%1/2
$args{'expressions'} || []
l!l
160
MC/DC0%0/1
$args{'expressions'} || []
$args{'expressions'}
Truth table50%1/2
A
A= $args{'expressions'}
Aresult
11
00
50 16 my $self = $class->SUPER::new( $plan, $groupby, \@ops );
51 $self->[0]{referenced_variables} = [
52 RDF::Query::_uniq(
53 $plan->referenced_variables,
54 map {
55 16 ▶ ($_->isa('RDF::Query::Node::Variable'))
Branch: 2 branches
BranchTrueFalseTotal
$_->isa('RDF::Query::Node::Variable') ? : 7 0 7
$_->isa('RDF::Query::Node') ? : 0 0 0
56 ? $_->name
57 : $_->isa('RDF::Query::Node')
58 ? ()
59 : $_->referenced_variables
60 } @$groupby)
61 ];
62 16 return $self;
63 }
64  
65 =item C<< execute ( $execution_context ) >>
66  
67 =cut
68  
69 16 ▶sub execute ($) {
Subroutine execute: called CC=31 SCAR=39.1
Pod documented
70 16 my $self = shift;
71 16 my $context = shift;
72 16 $self->[0]{delegate} = $context->delegate;
73 16 ▶ if ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state == $self->OPEN) 0 16 16
74 0 throw RDF::Query::Error::ExecutionError -text => "AGGREGATE plan can't be executed while already open";
75 }
76 16 my $plan = $self->[1];
77 16 $plan->execute( $context );
78
79 16 my $l = Log::Log4perl->get_logger("rdf.query.plan.aggregate");
80 16 ▶ if ($plan->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->state == $self->OPEN) { } 16 0 16
81 16 my $query = $context->query;
82 16 my $bridge = $context->model;
83
84 16 my %seen;
85 my %groups;
86 16 my %group_data;
87 16 my @groupby = $self->groupby;
88 16 my @ops = @{ $self->[3] };
89 16 local($RDF::Query::Node::Literal::LAZY_COMPARISONS) = 1;
90
91 16 ▶ ROW: while (my $row = $plan->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $plan->next) 75 16 91
92 75 $l->debug("aggregate on $row");
93 75 my @group;
94 75 foreach my $g (@groupby) {
95 27 my $v = $query->var_or_expr_value( $row, $g, $context );
96 27 ▶ if ($g->isa('RDF::Query::Expression::Alias')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->isa("RDF::Query::Expression::Alias")) 0 27 27
97 0 $row->{ $g->name } = $v;
98 }
99 27 push(@group, $v);
100 }
101
102 # my @group = map { $query->var_or_expr_value( $row, $_ ) } @groupby;
103 75 ▶ my $group = join('<<<', map { blessed($_) ? $_->as_string : '' } map { blessed($_) ? $query->var_or_expr_value( $row, $_, $context ) : '' } @group);
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($_) ? : 27 0 27
builtin::blessed($_) ? : 27 0 27
104
105 75 push( @{ $group_data{ 'rows' }{ $group } }, $row );
106 75 $group_data{ 'groups' }{ $group } = \@group;
107 75 foreach my $i (0 .. $#groupby) {
108 27 my $g = $groupby[$i];
109 27 $group_data{ 'groupby_sample' }{ $group } = $row;
110 }
111 }
112
113 16 my @groups = values %{ $group_data{'groups'} };
114 16 ▶ if (scalar(@groups) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @groups == 0) 0 16 16
115 0 $group_data{'rows'}{''} = [];
116 0 $group_data{'groups'}{''} = [];
117 }
118
119 16 my @rows;
120 16 GROUP: foreach my $group (keys %{ $group_data{ 'rows' } }) {
121 27 $l->debug( "group: $group" );
122 27 my %options;
123 my %aggregates;
124 27 my %passthrough_data;
125 27 my @group = @{ $group_data{ 'groups' }{ $group } };
126
127 27 my $row_sample = $group_data{ 'groupby_sample' }{ $group };
128 27 foreach my $g (@groupby) {
129 18 ▶ if ($g->isa('RDF::Query::Expression::Alias') or $g->isa('RDF::Query::Node::Variable')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($g->isa('RDF::Query::Expression::Alias') or $g->isa('RDF::Query::Node::Variable')) { } 18 0 18
elsif ($g->isa('RDF::Query::Expression')) { } 0 0 0
Condition33%1/3
$g->isa("RDF::Query::Expression::Alias") or $g->isa("RDF::Query::Node::Variable")
l!l&&r!l&&!r
0180
MC/DC0%0/2
$g->isa("RDF::Query::Expression::Alias") or $g->isa("RDF::Query::Node::Variable")
$g->isa("RDF::Query::Expression::Alias") $g->isa("RDF::Query::Node::Variable")
Truth table33%1/3
A or B
A= $g->isa("RDF::Query::Expression::Alias")B= $g->isa("RDF::Query::Node::Variable")
ABresult
1X1
011
000
130 18 my $name = $g->name;
131 18 $passthrough_data{ $name } = $row_sample->{ $name };
132 } elsif ($g->isa('RDF::Query::Expression')) {
133 0 my @names = $g->referenced_variables;
134 0 foreach my $name (@names) {
135 0 $passthrough_data{ $name } = $row_sample->{ $name };
136 }
137 } else {
138 0 my $name = $g->sse;
139 0 $passthrough_data{ $name } = $row_sample->{ $name };
140 }
141 }
142
143 27 my @operation_data = (map { [ @{ $_ }, \%aggregates ] } @ops);
144 27 foreach my $data (@operation_data) {
145 29 my $aggregate_data = pop(@$data);
146 29 my ($alias, $op, $opts, @cols) = @$data;
147 29 $options{ $alias } = $opts;
148 29 my $distinct = ($op =~ /^(.*)-DISTINCT$/);
149 29 $op =~ s/-DISTINCT$//;
150 29 my $col = $cols[0];
151 29 my %agg_group_seen;
152 29 ▶ try {
Subroutine __ANON__: called CC=57 SCAR=49.1
153 29 foreach my $row (@{ $group_data{ 'rows' }{ $group } }) {
154 88 ▶ my @proj_rows = map { (blessed($col)) ? $query->var_or_expr_value( $row, $col, $context ) : '*' } @cols;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($col) ? : 88 0 88
155 88 ▶ if ($distinct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distinct) 4 84 88
156 4 ▶ next if ($agg_group_seen{ join('<<<', @proj_rows) }++);
Branch: 1 branch
BranchTrueFalseTotal
if $agg_group_seen{join "<<<", @proj_rows}++ 2 2 4
157 }
158
159 86 $l->debug( "- row: $row" );
160 # $groups{ $group } ||= { map { $_ => $row->{ $_ } } @groupby };
161 86 ▶ if ($op eq 'COUNT') {
Branch: 7 branches
BranchTrueFalseTotal
if ($op eq 'COUNT') { } 14 72 86
elsif ($op eq 'SUM') { } 8 64 72
elsif ($op eq 'MAX') { } 25 39 64
elsif ($op eq 'MIN') { } 25 14 39
elsif ($op eq 'SAMPLE') { } 5 9 14
elsif ($op eq 'AVG') { } 4 5 9
elsif ($op eq 'GROUP_CONCAT') { } 5 0 5
162 14 $l->debug("- aggregate op: COUNT");
163 14 my $should_inc = 0;
164 14 ▶ if (not(blessed($col)) and $col eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($col) and $col eq '*') { } 0 14 14
Condition33%1/3
not builtin::blessed($col) and $col eq "*"
!ll&&!rl&&r
1400
MC/DC0%0/2
not builtin::blessed($col) and $col eq "*"
not builtin::blessed($col) $col eq "*"
Truth table33%1/3
not(A) and B
A= not builtin::blessed($col)B= $col eq "*"
ABresult
0X0
100
111
165 0 $should_inc = 1;
166 } else {
167 14 my $value = $query->var_or_expr_value( $row, $col, $context );
168 14 ▶ $should_inc = (defined $value) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 12 2 14
169 }
170
171 14 $aggregate_data->{ $alias }{ $group }[0] = $op;
172 14 $aggregate_data->{ $alias }{ $group }[1] += $should_inc;
173 } elsif ($op eq 'SUM') {
174 8 $l->debug("- aggregate op: SUM");
175 8 my $value = $query->var_or_expr_value( $row, $col, $context );
176 8 my $type = _node_type( $value );
177 8 $aggregate_data->{ $alias }{ $group }[0] = $op;
178
179 8 my $strict = 1;
180 8 ▶ unless ($value->isa('RDF::Query::Node::Literal') and $value->is_numeric_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type) 0 8 8
Condition33%1/3
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type
!ll&&!rl&&r
008
MC/DC0%0/2
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type
$value->isa("RDF::Query::Node::Literal") $value->is_numeric_type
Truth table33%1/3
A and B
A= $value->isa("RDF::Query::Node::Literal")B= $value->is_numeric_type
ABresult
0X0
100
111
181 0 throw RDF::Query::Error::TypeError -text => "Cannot compute SUM aggregate with a non-numeric term: " . $value->as_ntriples;
182 }
183
184 # if ($value->isa('RDF::Query::Node::Literal')) {
185 8 my $v = $value->numeric_value;
186 8 ▶ if (scalar( @{ $aggregate_data->{ $alias }{ $group } } ) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$aggregate_data->{$alias}{$group};} > 1) { } 4 4 8
187 4 ▶ if ($type ne $aggregate_data->{ $alias }{ $group }[2] and not($value->isa('RDF::Query::Node::Literal') and $value->is_numeric_type and blessed($aggregate_data->{ $alias }{ $group }[1]) and $aggregate_data->{ $alias }{ $group }[1]->isa('RDF::Query::Node::Literal') and $aggregate_data->{ $alias }{ $group }[1]->is_numeric_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne $aggregate_data->{$alias}{$group}[2] and not $value->isa("RDF::Query::Node::Literal") && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") && $aggregate_data->{$alias}{$group}[1]->is_numeric_type) 0 4 4
Condition6%1/15
$type ne $aggregate_data->{$alias}{$group}[2] and not $value->isa("RDF::Query::Node::Literal") && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") && $aggregate_data->{$alias}{$group}[1]->is_numeric_type
!ll&&!rl&&r
400
$value->isa('RDF::Query::Node::Literal') && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa('RDF::Query::Node::Literal') && $aggregate_data->{$alias}{$group}[1]->is_numeric_type
!ll&&!rl&&r
000
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type and builtin::blessed($aggregate_data->{$alias}{$group}[1]) and $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal")
!ll&&!rl&&r
000
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type and builtin::blessed($aggregate_data->{$alias}{$group}[1])
!ll&&!rl&&r
000
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type
!ll&&!rl&&r
000
MC/DC0%0/6
$type ne $aggregate_data->{$alias}{$group}[2] and not $value->isa("RDF::Query::Node::Literal") && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") && $aggregate_data->{$alias}{$group}[1]->is_numeric_type
$type ne $aggregate_data->{$alias}{$group}[2] $value->isa("RDF::Query::Node::Literal") $value->is_numeric_type builtin::blessed($aggregate_data->{$alias}{$group}[1]) $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") $aggregate_data->{$alias}{$group}[1]->is_numeric_type
Truth table14%1/7
A and not(B and C and D and E && F)
A= $type ne $aggregate_data->{$alias}{$group}[2]B= $value->isa("RDF::Query::Node::Literal")C= $value->is_numeric_typeD= builtin::blessed($aggregate_data->{$alias}{$group}[1])E= $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal")F= $aggregate_data->{$alias}{$group}[1]->is_numeric_type
ABCDEFresult
0XXXXX0
1111110
10XXXX1
110XXX1
1110XX1
11110X1
1111101
188 0 ▶ if ($context->strict_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->strict_errors) { } 0 0 0
189 0 delete $aggregate_data->{ $alias }{ $group };
190 0 throw RDF::Query::Error::ComparisonError -text => "Cannot compute SUM aggregate over nodes of multiple, non-numeric types";
191 } else {
192 0 $strict = 0;
193 }
194 }
195
196 4 $aggregate_data->{ $alias }{ $group }[1] += $v;
197 4 $aggregate_data->{ $alias }{ $group }[2] = RDF::Query::Expression::Binary->promote_type('+', $type, $aggregate_data->{ $alias }{ $group }[2]);
198 } else {
199 4 $aggregate_data->{ $alias }{ $group }[1] = $v;
200 4 $aggregate_data->{ $alias }{ $group }[2] = $type;
201 }
202 # }
203 } elsif ($op eq 'MAX') {
204 25 $l->debug("- aggregate op: MAX");
205 25 my $value = $query->var_or_expr_value( $row, $col, $context );
206 25 my $type = _node_type( $value );
207 25 $aggregate_data->{ $alias }{ $group }[0] = $op;
208
209 25 my $strict = 1;
210 25 ▶ if (scalar( @{ $aggregate_data->{ $alias }{ $group } } ) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$aggregate_data->{$alias}{$group};} > 1) { } 14 11 25
211 14 ▶ if ($type ne $aggregate_data->{ $alias }{ $group }[2] and not($value->isa('RDF::Query::Node::Literal') and $value->is_numeric_type and blessed($aggregate_data->{ $alias }{ $group }[1]) and $aggregate_data->{ $alias }{ $group }[1]->isa('RDF::Query::Node::Literal') and $aggregate_data->{ $alias }{ $group }[1]->is_numeric_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne $aggregate_data->{$alias}{$group}[2] and not $value->isa("RDF::Query::Node::Literal") && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") && $aggregate_data->{$alias}{$group}[1]->is_numeric_type) 2 12 14
Condition40%6/15
$type ne $aggregate_data->{$alias}{$group}[2] and not $value->isa("RDF::Query::Node::Literal") && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") && $aggregate_data->{$alias}{$group}[1]->is_numeric_type
!ll&&!rl&&r
1202
$value->isa('RDF::Query::Node::Literal') && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa('RDF::Query::Node::Literal') && $aggregate_data->{$alias}{$group}[1]->is_numeric_type
!ll&&!rl&&r
200
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type and builtin::blessed($aggregate_data->{$alias}{$group}[1]) and $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal")
!ll&&!rl&&r
200
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type and builtin::blessed($aggregate_data->{$alias}{$group}[1])
!ll&&!rl&&r
200
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type
!ll&&!rl&&r
020
MC/DC16%1/6
$type ne $aggregate_data->{$alias}{$group}[2] and not $value->isa("RDF::Query::Node::Literal") && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") && $aggregate_data->{$alias}{$group}[1]->is_numeric_type
$type ne $aggregate_data->{$alias}{$group}[2] $value->isa("RDF::Query::Node::Literal") $value->is_numeric_type builtin::blessed($aggregate_data->{$alias}{$group}[1]) $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") $aggregate_data->{$alias}{$group}[1]->is_numeric_type
Truth table28%2/7
A and not(B and C and D and E && F)
A= $type ne $aggregate_data->{$alias}{$group}[2]B= $value->isa("RDF::Query::Node::Literal")C= $value->is_numeric_typeD= builtin::blessed($aggregate_data->{$alias}{$group}[1])E= $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal")F= $aggregate_data->{$alias}{$group}[1]->is_numeric_type
ABCDEFresult
0XXXXX0
1111110
10XXXX1
110XXX1
1110XX1
11110X1
1111101
212 2 ▶ if ($context->strict_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->strict_errors) { } 0 2 2
213 0 delete $aggregate_data->{ $alias }{ $group };
214 0 throw RDF::Query::Error::ComparisonError -text => "Cannot compute MAX aggregate over nodes of multiple, non-numeric types";
215 } else {
216 2 $strict = 0;
217 }
218 }
219
220 14 ▶ if ($strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict) { } 12 2 14
221 12 ▶ if ($value > $aggregate_data->{ $alias }{ $group }[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $aggregate_data->{$alias}{$group}[1]) 4 8 12
222 4 $aggregate_data->{ $alias }{ $group }[1] = $value;
223 4 $aggregate_data->{ $alias }{ $group }[2] = $type;
224 }
225 } else {
226 2 ▶ if ("$value" gt "$aggregate_data->{ $alias }{ $group }[1]") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$value" gt "$aggregate_data->{$alias}{$group}[1]") 2 0 2
227 2 $aggregate_data->{ $alias }{ $group }[1] = $value;
228 2 $aggregate_data->{ $alias }{ $group }[2] = $type;
229 }
230 }
231 } else {
232 11 $aggregate_data->{ $alias }{ $group }[1] = $value;
233 11 $aggregate_data->{ $alias }{ $group }[2] = $type;
234 }
235 } elsif ($op eq 'MIN') {
236 25 $l->debug("- aggregate op: MIN");
237 25 my $value = $query->var_or_expr_value( $row, $col, $context );
238 25 my $type = _node_type( $value );
239 25 $aggregate_data->{ $alias }{ $group }[0] = $op;
240
241 25 my $strict = 1;
242 25 ▶ if (scalar( @{ $aggregate_data->{ $alias }{ $group } } ) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$aggregate_data->{$alias}{$group};} > 1) { } 19 6 25
243 19 ▶ if ($type ne $aggregate_data->{ $alias }{ $group }[2] and not($value->isa('RDF::Query::Node::Literal') and $value->is_numeric_type and blessed($aggregate_data->{ $alias }{ $group }[1]) and $aggregate_data->{ $alias }{ $group }[1]->isa('RDF::Query::Node::Literal') and $aggregate_data->{ $alias }{ $group }[1]->is_numeric_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne $aggregate_data->{$alias}{$group}[2] and not $value->isa("RDF::Query::Node::Literal") && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") && $aggregate_data->{$alias}{$group}[1]->is_numeric_type) 3 16 19
Condition40%6/15
$type ne $aggregate_data->{$alias}{$group}[2] and not $value->isa("RDF::Query::Node::Literal") && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") && $aggregate_data->{$alias}{$group}[1]->is_numeric_type
!ll&&!rl&&r
1603
$value->isa('RDF::Query::Node::Literal') && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa('RDF::Query::Node::Literal') && $aggregate_data->{$alias}{$group}[1]->is_numeric_type
!ll&&!rl&&r
300
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type and builtin::blessed($aggregate_data->{$alias}{$group}[1]) and $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal")
!ll&&!rl&&r
300
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type and builtin::blessed($aggregate_data->{$alias}{$group}[1])
!ll&&!rl&&r
300
$value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type
!ll&&!rl&&r
030
MC/DC16%1/6
$type ne $aggregate_data->{$alias}{$group}[2] and not $value->isa("RDF::Query::Node::Literal") && $value->is_numeric_type && builtin::blessed($aggregate_data->{$alias}{$group}[1]) && $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") && $aggregate_data->{$alias}{$group}[1]->is_numeric_type
$type ne $aggregate_data->{$alias}{$group}[2] $value->isa("RDF::Query::Node::Literal") $value->is_numeric_type builtin::blessed($aggregate_data->{$alias}{$group}[1]) $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal") $aggregate_data->{$alias}{$group}[1]->is_numeric_type
Truth table28%2/7
A and not(B and C and D and E && F)
A= $type ne $aggregate_data->{$alias}{$group}[2]B= $value->isa("RDF::Query::Node::Literal")C= $value->is_numeric_typeD= builtin::blessed($aggregate_data->{$alias}{$group}[1])E= $aggregate_data->{$alias}{$group}[1]->isa("RDF::Query::Node::Literal")F= $aggregate_data->{$alias}{$group}[1]->is_numeric_type
ABCDEFresult
0XXXXX0
1111110
10XXXX1
110XXX1
1110XX1
11110X1
1111101
244 3 ▶ if ($context->strict_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->strict_errors) { } 0 3 3
245 0 delete $aggregate_data->{ $alias }{ $group };
246 0 throw RDF::Query::Error::ComparisonError -text => "Cannot compute MIN aggregate over nodes of multiple, non-numeric types";
247 } else {
248 3 $strict = 0;
249 }
250 }
251
252 19 ▶ if ($strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict) { } 16 3 19
253 16 ▶ if ($value < $aggregate_data->{ $alias }{ $group }[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < $aggregate_data->{$alias}{$group}[1]) 7 9 16
254 7 $aggregate_data->{ $alias }{ $group }[1] = $value;
255 7 $aggregate_data->{ $alias }{ $group }[2] = $type;
256 }
257 } else {
258 3 ▶ if ("$value" lt "$aggregate_data->{ $alias }{ $group }[1]") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$value" lt "$aggregate_data->{$alias}{$group}[1]") 1 2 3
259 1 $aggregate_data->{ $alias }{ $group }[1] = $value;
260 1 $aggregate_data->{ $alias }{ $group }[2] = $type;
261 }
262 }
263 } else {
264 6 $aggregate_data->{ $alias }{ $group }[1] = $value;
265 6 $aggregate_data->{ $alias }{ $group }[2] = $type;
266 }
267 } elsif ($op eq 'SAMPLE') {
268 ### this is just the MIN code from above, without the strict comparison checking
269 5 $l->debug("- aggregate op: SAMPLE");
270 5 my $value = $query->var_or_expr_value( $row, $col, $context );
271 5 my $type = _node_type( $value );
272 5 $aggregate_data->{ $alias }{ $group }[0] = $op;
273
274 5 ▶ if (scalar( @{ $aggregate_data->{ $alias }{ $group } } ) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$aggregate_data->{$alias}{$group};} > 1) { } 4 1 5
275 4 ▶ if ("$value" lt "$aggregate_data->{ $alias }{ $group }[1]") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$value" lt "$aggregate_data->{$alias}{$group}[1]") 1 3 4
276 1 $aggregate_data->{ $alias }{ $group }[1] = $value;
277 1 $aggregate_data->{ $alias }{ $group }[2] = $type;
278 }
279 } else {
280 1 $aggregate_data->{ $alias }{ $group }[1] = $value;
281 1 $aggregate_data->{ $alias }{ $group }[2] = $type;
282 }
283 } elsif ($op eq 'AVG') {
284 4 $l->debug("- aggregate op: AVG");
285 4 my $value = $query->var_or_expr_value( $row, $col, $context );
286 4 my $type = _node_type( $value );
287 # warn "AVG\t$group\t" . $value->as_string . "\n";
288 4 $aggregate_data->{ $alias }{ $group }[0] = $op;
289
290 4 ▶ unless (blessed($value) and $value->isa('RDF::Query::Node::Literal') and $value->is_numeric_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($value) and $value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type) 0 4 4
Condition33%2/6
builtin::blessed($value) and $value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type
!ll&&!rl&&r
004
builtin::blessed($value) and $value->isa("RDF::Query::Node::Literal")
!ll&&!rl&&r
004
MC/DC0%0/3
builtin::blessed($value) and $value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type
builtin::blessed($value) $value->isa("RDF::Query::Node::Literal") $value->is_numeric_type
Truth table25%1/4
A and B and C
A= builtin::blessed($value)B= $value->isa("RDF::Query::Node::Literal")C= $value->is_numeric_type
ABCresult
0XX0
10X0
1100
1111
291 0 delete $aggregate_data->{ $alias }{ $group };
292 0 throw RDF::Query::Error::ComparisonError -text => "Cannot compute AVG aggregate over non-numeric nodes";
293 }
294
295 4 ▶ if (blessed($value) and $value->isa('RDF::Query::Node::Literal') and $value->is_numeric_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type) 4 0 4
Condition33%2/6
builtin::blessed($value) and $value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type
!ll&&!rl&&r
004
builtin::blessed($value) and $value->isa("RDF::Query::Node::Literal")
!ll&&!rl&&r
004
MC/DC0%0/3
builtin::blessed($value) and $value->isa("RDF::Query::Node::Literal") and $value->is_numeric_type
builtin::blessed($value) $value->isa("RDF::Query::Node::Literal") $value->is_numeric_type
Truth table25%1/4
A and B and C
A= builtin::blessed($value)B= $value->isa("RDF::Query::Node::Literal")C= $value->is_numeric_type
ABCresult
0XX0
10X0
1100
1111
296 4 $aggregate_data->{ $alias }{ $group }[1]++;
297 4 $aggregate_data->{ $alias }{ $group }[2] += $value->numeric_value;
298 4 ▶ if ($aggregate_data->{ $alias }{ $group }[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aggregate_data->{$alias}{$group}[3]) { } 3 1 4
299 3 $aggregate_data->{ $alias }{ $group }[3] = RDF::Query::Expression::Binary->promote_type('+', $type, $aggregate_data->{ $alias }{ $group }[3]);
300 } else {
301 1 $aggregate_data->{ $alias }{ $group }[3] = $type;
302 }
303 }
304 } elsif ($op eq 'GROUP_CONCAT') {
305 5 $l->debug("- aggregate op: GROUP_CONCAT");
306 5 $aggregate_data->{ $alias }{ $group }[0] = $op;
307
308 5 my $str = RDF::Query::Node::Resource->new('sparql:str');
309
310 my @values = map {
311 5 my $expr = RDF::Query::Expression::Function->new( $str, $query->var_or_expr_value( $row, $_, $context ) );
312 5 my $val = $expr->evaluate( $context->query, $row );
313 5 ▶ blessed($val) ? $val->literal_value : '';
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($val) ? : 5 0 5
314 } @cols;
315
316 # warn "adding '$string' to group_concat aggregate";
317 5 push( @{ $aggregate_data->{ $alias }{ $group }[1] }, @values );
318 } else {
319 0 throw RDF::Query::Error -text => "Unknown aggregate operator $op";
320 }
321 }
322 0 ▶ } catch RDF::Query::Error::ComparisonError with {
Subroutine __ANON__: not called CC=1 SCAR=6.9
323 0 delete $aggregate_data->{ $alias }{ $group };
324 0 ▶ } catch RDF::Query::Error::TypeError with {
Subroutine __ANON__: not called CC=1 SCAR=6.9
325 0 delete $aggregate_data->{ $alias }{ $group };
326 }
327 29 }
328
329 27 my %row = %passthrough_data;
330 27 foreach my $agg (keys %aggregates) {
331 29 ▶ if (defined($aggregates{$agg}{$group})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aggregates{$agg}{$group}) 29 0 29
332 29 my $op = $aggregates{ $agg }{ $group }[0];
333 29 ▶ if ($op eq 'AVG') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'AVG') { } 1 28 29
elsif ($op eq 'GROUP_CONCAT') { } 1 27 28
elsif ($op =~ /COUNT/) { } 5 22 27
334 1 my $value = ($aggregates{ $agg }{ $group }[2] / $aggregates{ $agg }{ $group }[1]);
335 1 my $type = $aggregates{ $agg }{ $group }[3];
336 1 ▶ if ($type eq 'http://www.w3.org/2001/XMLSchema#integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "http://www.w3.org/2001/XMLSchema#integer") 0 1 1
337 0 $type = 'http://www.w3.org/2001/XMLSchema#decimal';
338 }
339 1 ▶ $row{ $agg } = (blessed($value) and $value->isa('RDF::Trine::Node')) ? $value : RDF::Trine::Node::Literal->new( $value, undef, $type, 1 );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('RDF::Trine::Node') ? : 0 1 1
Condition33%1/3
builtin::blessed($value) and $value->isa("RDF::Trine::Node")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($value) and $value->isa("RDF::Trine::Node")
builtin::blessed($value) $value->isa("RDF::Trine::Node")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("RDF::Trine::Node")
ABresult
0X0
100
111
340 } elsif ($op eq 'GROUP_CONCAT') {
341 1 ▶ my $j = (exists $options{$agg}{seperator}) ? $options{$agg}{seperator} : ' ';
Branch: 1 branch
BranchTrueFalseTotal
exists $options{$agg}{'seperator'} ? : 0 1 1
342 1 $row{ $agg } = RDF::Query::Node::Literal->new( join($j, @{ $aggregates{ $agg }{ $group }[1] }) );
343 } elsif ($op =~ /COUNT/) {
344 5 my $value = $aggregates{ $agg }{ $group }[1];
345 5 ▶ $row{ $agg } = (blessed($value) and $value->isa('RDF::Trine::Node')) ? $value : RDF::Trine::Node::Literal->new( $value, undef, 'http://www.w3.org/2001/XMLSchema#integer', 1 );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('RDF::Trine::Node') ? : 0 5 5
Condition33%1/3
builtin::blessed($value) and $value->isa("RDF::Trine::Node")
!ll&&!rl&&r
500
MC/DC0%0/2
builtin::blessed($value) and $value->isa("RDF::Trine::Node")
builtin::blessed($value) $value->isa("RDF::Trine::Node")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("RDF::Trine::Node")
ABresult
0X0
100
111
346 } else {
347 22 ▶ if (defined($aggregates{$agg}{$group})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aggregates{$agg}{$group}) 22 0 22
348 22 my $value = $aggregates{ $agg }{ $group }[1];
349 22 ▶ $row{ $agg } = (blessed($value) and $value->isa('RDF::Trine::Node')) ? $value : RDF::Trine::Node::Literal->new( $value, undef, $aggregates{ $agg }{ $group }[2], 1 );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('RDF::Trine::Node') ? : 18 4 22
Condition66%2/3
builtin::blessed($value) and $value->isa("RDF::Trine::Node")
!ll&&!rl&&r
4018
MC/DC50%1/2
builtin::blessed($value) and $value->isa("RDF::Trine::Node")
builtin::blessed($value) $value->isa("RDF::Trine::Node")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("RDF::Trine::Node")
ABresult
0X0
100
111
350 }
351 }
352 }
353 }
354
355 27 my $vars = RDF::Query::VariableBindings->new( \%row );
356 27 $l->debug("aggregate row: $vars");
357 27 push(@rows, $vars);
358 }
359
360 16 $self->[0]{rows} = \@rows;
361 16 $self->state( $self->OPEN );
362 } else {
363 0 warn "could not execute plan in distinct";
364 }
365 16 $self;
366 }
367  
368 =item C<< next >>
369  
370 =cut
371  
372 43 ▶sub next {
Subroutine next: called CC=3 SCAR=12.0
Pod documented
373 43 my $self = shift;
374 43 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 43 43
375 0 throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open AGGREGATE";
376 }
377 43 my $bindings = shift(@{ $self->[0]{rows} });
378 43 ▶ if (my $d = $self->delegate) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $self->delegate) 0 43 43
379 0 $d->log_result( $self, $bindings );
380 }
381 43 return $bindings;
382 }
383  
384 =item C<< close >>
385  
386 =cut
387  
388 16 ▶sub close {
Subroutine close: called CC=3 SCAR=11.6
Pod documented
389 16 my $self = shift;
390 16 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 16 16
391 0 throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open AGGREGATE";
392 }
393 16 delete $self->[0]{rows};
394 16 ▶ if (defined($self->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[1]) 16 0 16
395 16 $self->[1]->close();
396 }
397 16 $self->SUPER::close();
398 }
399  
400 =item C<< pattern >>
401  
402 Returns the query plan that will be used to produce the aggregated data.
403  
404 =cut
405  
406 31 ▶sub pattern {
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
407 31 my $self = shift;
408 31 return $self->[1];
409 }
410  
411 =item C<< groupby >>
412  
413 Returns the grouping arguments that will be used to produce the aggregated data.
414  
415 =cut
416  
417 45 ▶sub groupby {
Subroutine groupby: called CC=2 SCAR=7.1
Pod documented
418 45 my $self = shift;
419 45 ▶ return @{ $self->[2] || [] };
Condition50%1/2
$self->[2] || []
l!l
450
MC/DC0%0/1
$self->[2] || []
$self->[2]
Truth table50%1/2
A
A= $self->[2]
Aresult
11
00
420 }
421  
422 =item C<< plan_node_name >>
423  
424 Returns the string name of this plan node, suitable for use in serialization.
425  
426 =cut
427  
428 0 ▶sub plan_node_name {
Subroutine plan_node_name: not called CC=1 SCAR=6.9
Pod documented
429 0 return 'aggregate';
430 }
431  
432 =item C<< plan_node_data >>
433  
434 Returns the data for this plan node that corresponds to the values described by
435 the signature returned by C<< plan_prototype >>.
436  
437 =cut
438  
439 0 ▶sub plan_node_data {
Subroutine plan_node_data: not called CC=1 SCAR=6.9
Pod documented
440 0 my $self = shift;
441 0 return ($self->pattern, $self->groupby);
442 }
443  
444 =item C<< sse ( $context, $indent ) >>
445  
446 =cut
447  
448 15 ▶sub sse {
Subroutine sse: called CC=4 SCAR=14.1
Pod documented
449 15 my $self = shift;
450 15 my $context = shift;
451 15 my $indent = shift;
452 15 my $more = ' ';
453 15 my $psse = $self->pattern->sse( $context, "${indent}${more}" );
454 15 my @group = map { $_->sse($context, "${indent}${more}") } $self->groupby;
455 15 my $gsse = join(' ', @group);
456 15 my @ops;
457 15 foreach my $p (@{ $self->[3] }) {
458 17 my ($alias, $op, $options, @cols) = @$p;
459 17 ▶ my $cols = '(' . join(' ', map { ref($_) ? $_->sse($context, "${indent}${more}") : '*' } @cols) . ')';
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 17 0 17
460 17 my @opts_keys = keys %$options;
461 17 ▶ if (@opts_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opts_keys) { } 0 17 17
462 0 my $opt_string = '(' . join(' ', map { $_, qq["$options->{$_}"] } @opts_keys) . ')';
463 0 push(@ops, qq[("$alias" "$op" $cols $opt_string)]);
464 } else {
465 17 push(@ops, qq[("$alias" "$op" $cols)]);
466 }
467 }
468 15 my $osse = join(' ', @ops);
469 15 return sprintf(
470 "(aggregate\n${indent}${more}%s\n${indent}${more}(%s)\n${indent}${more}(%s))",
471 $psse,
472 $gsse,
473 $osse,
474 );
475 }
476  
477 # =item C<< plan_prototype >>
478 #
479 # Returns a list of scalar identifiers for the type of the content (children)
480 # nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable
481 # identifiers.
482 #
483 # =cut
484 #
485 # sub plan_prototype {
486 # my $self = shift;
487 # return qw(P \E *\ssW);
488 # }
489 #
490 # =item C<< plan_node_data >>
491 #
492 # Returns the data for this plan node that corresponds to the values described by
493 # the signature returned by C<< plan_prototype >>.
494 #
495 # =cut
496 #
497 # sub plan_node_data {
498 # my $self = shift;
499 # my @group = $self->groupby;
500 # my @ops = @{ $self->[3] };
501 # return ($self->pattern, \@group, map { [@$_] } @ops);
502 # }
503  
504 =item C<< distinct >>
505  
506 Returns true if the pattern is guaranteed to return distinct results.
507  
508 =cut
509  
510 16 ▶sub distinct {
Subroutine distinct: called CC=1 SCAR=0.0
Pod documented
511 16 my $self = shift;
512 16 return $self->pattern->distinct;
513 }
514  
515 =item C<< ordered >>
516  
517 Returns true if the pattern is guaranteed to return ordered results.
518  
519 =cut
520  
521 14 ▶sub ordered {
Subroutine ordered: called CC=1 SCAR=0.0
Pod documented
522 14 my $self = shift;
523 14 my $sort = [ $self->groupby ];
524 14 return []; # XXX aggregates are actually sorted, so figure out what should go here...
525 }
526  
527 67 ▶sub _node_type {
Subroutine _node_type: called CC=6 SCAR=23.5
528 67 my $node = shift;
529 67 ▶ if (blessed($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($node)) { } 67 0 67
530 67 ▶ if ($node->isa('RDF::Query::Node::Literal')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->isa('RDF::Query::Node::Literal')) { } 67 0 67
elsif ($node->isa('RDF::Query::Node::Resource')) { } 0 0 0
elsif ($node->isa('RDF::Query::Node::Blank')) { } 0 0 0
531 67 ▶ if (my $type = $node->literal_datatype) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $node->literal_datatype) { } 34 33 67
532 34 return $type;
533 } else {
534 33 return 'literal';
535 }
536 } elsif ($node->isa('RDF::Query::Node::Resource')) {
537 0 return 'resource';
538 } elsif ($node->isa('RDF::Query::Node::Blank')) {
539 0 return 'blank';
540 } else {
541 0 return '';
542 }
543 } else {
544 0 return '';
545 }
546 }
547  
548 35 1;
549  
550 __END__
« Query/Functions/SPARQL.pm ↑ Summary Query/BGPOptimizer.pm »