Class: SPARQL::Algebra::Operator::Sum
- Inherits:
-
SPARQL::Algebra::Operator
- Object
- SPARQL::Algebra::Operator
- SPARQL::Algebra::Operator::Sum
- Includes:
- Aggregate
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/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
Constants included from RDF::Util::Logger
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, #formulae, #initialize, #inspect, #mergable?, #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?
Methods included from RDF::Util::Logger
#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger
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 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/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 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/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 |