Class: SPARQL::Algebra::Operator::Min

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Aggregate
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/min.rb

Overview

The SPARQL min set function.

[127] Aggregate::= ... | 'MIN' '(' 'DISTINCT'? Expression ')'

Examples:

SPARQL Grammar

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

SSE

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

See Also:

Constant Summary collapse

NAME =
:min

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

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

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

Returns a new instance of Min.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/min.rb', line 26

def initialize(*operands, **options)
  raise ArgumentError,
    "min 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

Min is a SPARQL set function that return the minimum value from a group respectively.

It makes use of the SPARQL ORDER BY ordering definition, to allow ordering over arbitrarily typed expressions.

Parameters:

Returns:



41
42
43
44
45
46
47
48
49
50
51
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/min.rb', line 41

def apply(enum, **options)
  # FIXME: we don't actually do anything with distinct
  operands.shift if distinct = (operands.first == :distinct)
  if enum.empty?
    raise TypeError, "Minumuim of an empty multiset"
  elsif enum.flatten.all? {|n| n.literal?}
    RDF::Literal(enum.flatten.min)
  else
    raise TypeError, "Minumuim of non-literals: #{enum.flatten}"
  end
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:



58
59
60
61
62
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/min.rb', line 58

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