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

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

Overview

The SPARQL coalesce function.

[121] BuiltInCall ::= … | ‘COALESCE’ ExpressionList

Examples:

SPARQL Grammar

PREFIX :        <http://example/>
PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>

SELECT ?X (SAMPLE(?v) AS ?S)
{
  ?s :p ?v .
  OPTIONAL { ?s :q ?w }
}
GROUP BY (COALESCE(?w, "1605-11-05"^^xsd:date) AS ?X) 

SSE

(prefix
 ((: <http://example/>) (xsd: <http://www.w3.org/2001/XMLSchema#>))
 (project (?X ?S)
  (extend ((?S ??.0))
   (group
    ((?X (coalesce ?w "1605-11-05"^^xsd:date)))
    ((??.0 (sample ?v)))
    (leftjoin
     (bgp (triple ?s :p ?v))
     (bgp (triple ?s :q ?w)))))))

See Also:

Constant Summary collapse

NAME =
:coalesce

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 Evaluatable

#apply, #memoize, #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, #initialize, #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?, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, 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})

    ({}) options passed from query

Returns:

Raises:

  • (TypeError)

    if none of the operands succeeds



57
58
59
60
61
62
63
64
65
# File 'lib/sparql/algebra/operator/coalesce.rb', line 57

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

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:

  • (String)


72
73
74
# File 'lib/sparql/algebra/operator/coalesce.rb', line 72

def to_sparql(**options)
  "COALESCE(#{operands.to_sparql(delimiter: ', ', **options)})"
end