Class: SPARQL::Algebra::Operator::Sum

Inherits:
Unary show all
Includes:
Aggregate
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/sum.rb

Overview

The SPARQL sum set function.

Examples:

(prefix ((: <http://www.example.org/>))
  (project (?sum)
    (extend ((?sum ?.0))
      (group () ((?.0 (sum ?o)))
        (bgp (triple ?s :dec ?o))))))

See Also:

Constant Summary

NAME =
:sum

Constants inherited from Unary

Unary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Aggregate

#aggregate, #replace_aggregate!, #replace_vars!

Methods inherited from Unary

#initialize

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, base_uri, #base_uri, base_uri=, #boolean, #constant?, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #parent, #parent=, prefixes, #prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, #node?, open, #optimize, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

This class inherits a constructor from SPARQL::Algebra::Operator::Unary

Instance Method Details

#apply(enum) ⇒ 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).

Parameters:

Returns:



25
26
27
28
29
30
31
32
33
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/sum.rb', line 25

def apply(enum)
  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