Sha256: ba1532264f257af880036a5e720c70eaa0ce4090a830356de23b26b2a886077d

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module SPARQL; module Algebra
  class Operator
    ##
    # The SPARQL `max` set function.
    #
    # @example
    #    (prefix ((: <http://www.example.org/>))
    #      (project (?max)
    #        (extend ((?max ??.0))
    #          (group () ((??.0 (max ?o)))
    #            (bgp (triple ?s ?p ?o))))))
    #
    # @see https://www.w3.org/TR/sparql11-query/#defn_aggMax
    class Max < Operator
      include Aggregate

      NAME = :max

      ##
      # Max is a SPARQL set function that return the maximum value from a group respectively.
      #
      # It makes use of the SPARQL ORDER BY ordering definition, to allow ordering over arbitrarily typed expressions.
      #
      # @param  [Enumerable<Array<RDF::Term>>] enum
      #   enum of evaluated operand
      # @return [RDF::Literal] The maximum value of the terms
      def apply(enum)
        # FIXME: we don't actually do anything with distinct
        operands.shift if distinct = (operands.first == :distinct)
        if enum.empty?
          raise TypeError, "Maximum of an empty multiset"
        elsif enum.flatten.all? {|n| n.literal?}
          RDF::Literal(enum.flatten.max)
        else
          raise TypeError, "Maximum of non-literals: #{enum.flatten}"
        end
      end
    end # Max
  end # Operator
end; end # SPARQL::Algebra

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sparql-3.1.4 lib/sparql/algebra/operator/max.rb
sparql-3.1.3 lib/sparql/algebra/operator/max.rb
sparql-3.1.2 lib/sparql/algebra/operator/max.rb