Class: SPARQL::Algebra::Operator::Coalesce

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/coalesce.rb

Overview

The SPARQL coalesce function.

Examples:

(prefix ((: <http://example.org/>)
         (xsd: <http://www.w3.org/2001/XMLSchema#>))
  (project (?cx ?div ?def ?err)
    (extend ((?cx (coalesce ?x -1))
             (?div (coalesce (/ ?o ?x) -2))
             (?def (coalesce ?z -3))
             (?err (coalesce ?z)))
      (leftjoin
        (bgp (triple ?s :p ?o))
        (bgp (triple ?s :q ?x))))))

See Also:

Constant Summary

NAME =
:coalesce

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Evaluatable

#apply, #memoize, #replace_aggregate!, #replace_vars!

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, base_uri, #base_uri, base_uri=, #boolean, #constant?, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from Expression

cast, #constant?, extension, extensions, for, #invalid?, new, #node?, open, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

This class inherits a constructor from SPARQL::Algebra::Operator

Instance Method Details

#evaluate(bindings, options = {}) ⇒ RDF::Term

The COALESCE function form returns the RDF term value of the first expression that evaluates without error. In SPARQL, evaluating an unbound variable raises an error.

If none of the arguments evaluates to an RDF term, an error is raised. If no expressions are evaluated without error, an error is raised.

Examples:

Suppose ?x = 2 and ?y is not bound in some query solution:

  COALESCE(?x, 1/0) #=> 2, the value of x
  COALESCE(1/0, ?x) #=> 2
  COALESCE(5, ?x) #=> 5
  COALESCE(?y, 3) #=> 3
  COALESCE(?y) #=> raises an error because y is not bound.

Parameters:

  • bindings (RDF::Query::Solution)

    a query solution containing zero or more variable bindings

  • options (Hash{Symbol => Object}) (defaults to: {})

    ({}) options passed from query

Returns:

Raises:

  • (TypeError)

    if none of the operands succeeds



44
45
46
47
48
49
50
51
52
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/coalesce.rb', line 44

def evaluate(bindings, options = {})
  operands.each do |op|
    begin
      return op.evaluate(bindings, options.merge(depth: options[:depth].to_i + 1))
    rescue
    end
  end
  raise TypeError, "None of the operands evaluated"
end

#optimizeUnion, RDF::Query

Returns an optimized version of this query.

Return optimized query

Returns:



60
61
62
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/coalesce.rb', line 60

def optimize
  operands = operands.map(&:optimize)
end