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

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Aggregate
Defined in:
lib/sparql/algebra/operator/sum.rb

Overview

The SPARQL sum set function.

[127] Aggregate::= … | ‘SUM’ ‘(’ ‘DISTINCT’? Expression ‘)’

Examples:

SPARQL Grammar

PREFIX : <http://www.example.org/>
SELECT (SUM(?o) AS ?sum)
WHERE { ?s :dec ?o }

SSE

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

See Also:

Constant Summary collapse

NAME =
:sum

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

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 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).

Parameters:

Returns:

  • (RDF::Literal::Numeric)

    The sum of the terms



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sparql/algebra/operator/sum.rb', line 32

def apply(enum, **options)
  # 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.

Returns:

  • (String)


49
50
51
52
53
# File 'lib/sparql/algebra/operator/sum.rb', line 49

def to_sparql(**options)
  distinct = operands.first == :distinct
  args = distinct ? operands[1..-1] : operands
  "SUM(#{'DISTINCT ' if distinct}#{args.to_sparql(**options)})"
end