Class: SPARQL::Algebra::Operator::Avg

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

Overview

The SPARQL avg set function.

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

Examples:

SPARQL Query

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

SSE

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

See Also:

Constant Summary collapse

NAME =
:avg

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, #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

#initialize(*operands, **options) ⇒ Avg

Returns a new instance of Avg.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
# File 'lib/sparql/algebra/operator/avg.rb', line 26

def initialize(*operands, **options)
  raise ArgumentError,
    "avg operator accepts at most one argument with an optional :distinct" if
    (operands - %i{distinct}).length != 1
  super
end

Instance Method Details

#apply(enum, **options) ⇒ RDF::Literal::Numeric

The Avg set function calculates the average value for an expression over a group. It is defined in terms of Sum and Count.

Parameters:

Returns:

  • (RDF::Literal::Numeric)

    The numeric average of the terms



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sparql/algebra/operator/avg.rb', line 39

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(:+) / RDF::Literal::Decimal.new(enum.length)
  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)


56
57
58
59
60
# File 'lib/sparql/algebra/operator/avg.rb', line 56

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