Class: SPARQL::Algebra::Operator::Sum
- Inherits:
-
SPARQL::Algebra::Operator
- Object
- SPARQL::Algebra::Operator
- 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
Constants inherited from SPARQL::Algebra::Operator
Constants included from Expression
Instance Attribute Summary
Attributes inherited from SPARQL::Algebra::Operator
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!
Methods inherited from SPARQL::Algebra::Operator
#aggregate?, arity, #base_uri, base_uri, base_uri=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #validate!, #variable?, #variables, #vars
Methods included from Expression
cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?
Constructor Details
This class inherits a constructor from SPARQL::Algebra::Operator
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 |