Query/Algebra/Aggregate.pm

filter: Statementstmt 77.7% 91 / 117 Branchbran 50.0% 10 / 20 Conditioncond 47.0% 8 / 17 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 85.7% 18 / 21 Podpod 100% 12 / 12 totaltotal 71.4% 140 / 196 CC 21 SCAR 18.0
CC
21
Cov
71%
sse
11.8
new
11.0
potentially_bound
6.1
CRAP 6.1
« Query/Node.pm ↑ Summary Query/Parser/SPARQL.pm »
1 # RDF::Query::Algebra::Aggregate
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Algebra::Aggregate - Algebra class for aggregate patterns
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Algebra::Aggregate version 2.919.
11  
12 =cut
13  
14 package RDF::Query::Algebra::Aggregate;
15  
16 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 36 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 ▶use base qw(RDF::Query::Algebra);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 36 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 36 ▶use RDF::Trine::Iterator qw(smap);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 ######################################################################
27  
28 36 our ($VERSION);
29 36 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 36 $VERSION = '2.919';
31 }
32  
33 ######################################################################
34  
35 =head1 METHODS
36  
37 Beyond the methods documented below, this class inherits methods from the
38 L<RDF::Query::Algebra> class.
39  
40 =over 4
41  
42 =cut
43  
44 =item C<new ( $pattern, \@groupby, $alias => [$op => $col] )>
45  
46 =item C<new ( $pattern, \@groupby, expressions => [ $alias => [$op, \%options, @cols] ] )>
47  
48 Returns a new Aggregate structure. Groups by the named bindings in C<< @groupby >>,
49 and returns new bindings for the named C<< $alias >> for the operation C<< $op >>
50 on column C<< $col >>.
51  
52 C<< $op >> may be one of: COUNT, MIN, MAX, SUM.
53  
54 =cut
55  
56 17 ▶sub new {
Subroutine new: called CC=6 SCAR=24.0
Pod documented
57 17 my $class = shift;
58 17 my $pattern = shift;
59 17 my $groupby = shift;
60 17 my @ops;
61 17 ▶ if (scalar(@_) and ref($_[0]) and reftype($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ and ref $_[0] and builtin::reftype($_[0]) eq 'HASH') { } 17 0 17
Condition33%2/6
scalar @_ and ref $_[0] and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
0017
scalar @_ and ref $_[0]
!ll&&!rl&&r
0017
MC/DC0%0/3
scalar @_ and ref $_[0] and builtin::reftype($_[0]) eq "HASH"
scalar @_ ref $_[0] builtin::reftype($_[0]) eq "HASH"
Truth table25%1/4
A and B and C
A= scalar @_B= ref $_[0]C= builtin::reftype($_[0]) eq "HASH"
ABCresult
0XX0
10X0
1100
1111
62 17 my $hash = shift;
63 17 ▶ @ops = @{ $hash->{ 'expressions' } || [] };
Condition50%1/2
$hash->{'expressions'} || []
l!l
170
MC/DC0%0/1
$hash->{'expressions'} || []
$hash->{'expressions'}
Truth table50%1/2
A
A= $hash->{'expressions'}
Aresult
11
00
64 } else {
65 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
66 0 my ($alias, $data) = splice(@_,0,2,());
67 0 my $op = shift(@$data);
68 0 my @data = ($op, {}, @$data);
69 0 push(@ops, $alias, \@data);
70 }
71 0 @ops = @_;
72 }
73
74 17 return bless( [ $pattern, $groupby, \@ops ] );
75 }
76  
77 =item C<< construct_args >>
78  
79 Returns a list of arguments that, passed to this class' constructor,
80 will produce a clone of this algebra pattern.
81  
82 =cut
83  
84 84 ▶sub construct_args {
Subroutine construct_args: called CC=1 SCAR=0.0
Pod documented
85 84 my $self = shift;
86 84 my @ops = @{ $self->[2] };
87 84 return ($self->pattern, [ $self->groupby ], { expressions => \@ops });
88 }
89  
90 =item C<< pattern >>
91  
92 Returns the aggregates pattern.
93  
94 =cut
95  
96 156 ▶sub pattern {
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
97 156 my $self = shift;
98 156 return $self->[0];
99 }
100  
101 =item C<< groupby >>
102  
103 Returns the aggregate's GROUP BY binding names.
104  
105 =cut
106  
107 202 ▶sub groupby {
Subroutine groupby: called CC=1 SCAR=0.0
Pod documented
108 202 my $self = shift;
109 202 return @{ $self->[1] };
110 }
111  
112 =item C<< ops >>
113  
114 Returns a list of tuples as ARRAY refs containing C<< $alias, $op, @cols >>.
115  
116 =cut
117  
118 80 ▶sub ops {
Subroutine ops: called CC=2 SCAR=6.9
Pod documented
119 80 my $self = shift;
120 80 my @ops = @{ $self->[2] };
121 80 my @tuples;
122 80 ▶ while (@ops) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ops) 90 80 170
123 90 my $alias = shift(@ops);
124 90 my $data = shift(@ops);
125 90 my ($op, $opts, @col) = @$data;
126 90 push(@tuples, [$alias, $op, $opts, @col]);
127 }
128 80 return @tuples;
129 }
130  
131 =item C<< sse >>
132  
133 Returns the SSE string for this algebra expression.
134  
135 =cut
136  
137 53 ▶sub sse {
Subroutine sse: called CC=10 SCAR=24.7
Pod documented
138 53 my $self = shift;
139 53 my $context = shift;
140 53 ▶ my $prefix = shift || '';
Condition100%2/2
shift() || ''
l!l
3617
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
141 53 ▶ my $indent = ($context->{indent} ||= ' ');
Condition50%1/2
$context->{"indent"} ||= " "
l!l
530
MC/DC0%0/1
$context->{"indent"} ||= " "
$context->{"indent"}
Truth table50%1/2
A
A= $context->{"indent"}
Aresult
11
00
142
143 53 my @ops_sse;
144 53 my @ops = $self->ops;
145 53 foreach my $data (@ops) {
146 59 my ($alias, $op, $opts, @cols) = @$data;
147 59 ▶ my @col_strings = map { (not(blessed($_)) and $_ eq '*') ? '*' : $_->sse( $context, "${prefix}${indent}" ) } @cols;
Branch: 1 branch
BranchTrueFalseTotal
!builtin::blessed($_) && $_ eq '*' ? : 0 59 59
Condition33%1/3
not builtin::blessed($_) and $_ eq "*"
!ll&&!rl&&r
5900
MC/DC0%0/2
not builtin::blessed($_) and $_ eq "*"
not builtin::blessed($_) $_ eq "*"
Truth table33%1/3
not(A) and B
A= not builtin::blessed($_)B= $_ eq "*"
ABresult
0X0
100
111
148 59 my $col_string = join(' ', @col_strings);
149 59 ▶ if (@col_strings > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@col_strings > 1) 0 59 59
150 0 $col_string = '(' . $col_string . ')';
151 }
152 59 ▶ my %op_opts = %{ $opts || {} };
Condition50%1/2
$opts || {}
l!l
590
MC/DC0%0/1
$opts || {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
153 59 my @opts_keys = keys %op_opts;
154 59 ▶ if (@opts_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opts_keys) { } 0 59 59
155 0 my $opt_string = '(' . join(' ', map { $_, qq["$op_opts{$_}"] } @opts_keys) . ')';
156 0 push(@ops_sse, sprintf('(alias "%s" (%s %s %s))', $alias, $op, $col_string, $opt_string));
157 } else {
158 59 push(@ops_sse, sprintf('(alias "%s" (%s %s))', $alias, $op, $col_string));
159 }
160 }
161
162 53 my @group = $self->groupby;
163 53 ▶ my $group = (@group) ? '(' . join(', ', map {$_->sse($context, $prefix)} @group) . ')' : '';
Branch: 1 branch
BranchTrueFalseTotal
@group ? : 26 27 53
164 53 return sprintf(
165 "(aggregate\n${prefix}${indent}%s\n${prefix}${indent}(%s)\n${prefix}${indent}%s)",
166 $self->pattern->sse( $context, "${prefix}${indent}" ),
167 join(', ', @ops_sse),
168 $group,
169 );
170 }
171  
172 =item C<< as_sparql >>
173  
174 Returns the SPARQL string for this algebra expression.
175  
176 =cut
177  
178 1 ▶sub as_sparql {
Subroutine as_sparql: called CC=1 SCAR=0.0
Pod documented
179 1 my $self = shift;
180 1 my $context = shift;
181 1 my $indent = shift;
182 1 return $self->pattern->as_sparql($context, $indent);
183 }
184  
185 =item C<< as_hash >>
186  
187 Returns the query as a nested set of plain data structures (no objects).
188  
189 =cut
190  
191 0 ▶sub as_hash {
Subroutine as_hash: not called CC=2 SCAR=17.9
Pod documented
192 0 my $self = shift;
193 0 my $context = shift;
194
195 0 my @ops = $self->ops;
196 0 my @expressions;
197 0 foreach my $o (@ops) {
198 0 my ($alias, $op, $agg_options, @cols) = @$o;
199 0 push(@expressions, { alias => $alias, op => $op, scalarvals => $agg_options, columns => [ map { $_->as_hash } @cols ] });
200 }
201
202 return {
203 type => lc($self->type),
204 pattern => $self->pattern->as_hash,
205 0 groupby => [ map { $_->as_hash } $self->groupby ],
206 expressions => \@expressions,
207 };
208 }
209  
210 =item C<< type >>
211  
212 Returns the type of this algebra expression.
213  
214 =cut
215  
216 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
217 0 return 'AGGREGATE';
218 }
219  
220 =item C<< referenced_variables >>
221  
222 Returns a list of the variable names used in this algebra expression.
223  
224 =cut
225  
226 2 ▶sub referenced_variables {
Subroutine referenced_variables: called CC=1 SCAR=0.0
Pod documented
227 2 my $self = shift;
228 2 my @aliases = map { $_->[0] } $self->ops;
229 2 return RDF::Query::_uniq( @aliases, $self->pattern->referenced_variables );
230 }
231  
232 =item C<< potentially_bound >>
233  
234 Returns a list of the variable names used in this algebra expression that will
235 bind values during execution.
236  
237 =cut
238  
239 16 ▶sub potentially_bound {
Subroutine potentially_bound: called CC=5 SCAR=18.1
Pod documented
240 16 my $self = shift;
241 16 my @vars;
242 # push(@vars, map { $_->[0] } $self->ops);
243 16 foreach my $g ($self->groupby) {
244 7 ▶ if (blessed($g)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($g)) 7 0 7
245 7 ▶ if ($g->isa('RDF::Query::Node::Variable')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($g->isa('RDF::Query::Node::Variable')) { } 7 0 7
elsif ($g->isa('RDF::Query::Expression::Alias')) { } 0 0 0
246 7 push(@vars, $g->name);
247 } elsif ($g->isa('RDF::Query::Expression::Alias')) {
248 0 push(@vars, $g->name);
249 }
250 }
251 }
252 16 return RDF::Query::_uniq(@vars);
253 # return RDF::Query::_uniq( @aliases, $self->pattern->referenced_variables );
254 }
255  
256 =item C<< definite_variables >>
257  
258 Returns a list of the variable names that will be bound after evaluating this algebra expression.
259  
260 =cut
261  
262 0 ▶sub definite_variables {
Subroutine definite_variables: not called CC=1 SCAR=6.9
Pod documented
263 0 my $self = shift;
264 0 my @aliases = map { $_->[0] } $self->ops;
265 0 return @aliases;
266 }
267  
268 36 1;
269  
270 __END__
« Query/Node.pm ↑ Summary Query/Parser/SPARQL.pm »