Class: SPARQL::Algebra::Operator::Sum
- Includes:
- Aggregate
- Defined in:
- lib/sparql/algebra/operator/sum.rb
Overview
The SPARQL sum set function.
[127] Aggregate::= ... | 'SUM' '(' 'DISTINCT'? Expression ')'
Constant Summary collapse
- NAME =
:sum
Instance Method Summary collapse
-
#apply(enum, **options) ⇒ RDF::Literal::Numeric
Sum is a SPARQL set function that will return the numeric value obtained by summing the values within the aggregate group.
-
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
Methods included from Aggregate
#aggregate, #replace_aggregate!, #replace_vars!
Instance Method Details
#apply(enum, **options) ⇒ RDF::Literal::Numeric
Sum is a SPARQL set function that will return the numeric value obtained by summing the values within the aggregate group. Type promotion happens as per the op:numeric-add function, applied transitively, (see definition below) so the value of SUM(?x), in an aggregate group where ?x has values 1 (integer), 2.0e0 (float), and 3.0 (decimal) will be 6.0 (float).
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sparql/algebra/operator/sum.rb', line 32 def apply(enum, **) # FIXME: we don't actually do anything with distinct operands.shift if distinct = (operands.first == :distinct) if enum.empty? RDF::Literal(0) elsif enum.flatten.all? {|n| n.is_a?(RDF::Literal::Numeric)} enum.flatten.reduce(:+) else raise TypeError, "Averaging non-numeric types: #{enum.flatten}" end end |
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
49 50 51 52 53 |
# File 'lib/sparql/algebra/operator/sum.rb', line 49 def to_sparql(**) distinct = operands.first == :distinct args = distinct ? operands[1..-1] : operands "SUM(#{'DISTINCT ' if distinct}#{args.to_sparql(**)})" end |