Class: SPARQL::Algebra::Operator::LeftJoin

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Query
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/left_join.rb

Overview

The SPARQL GraphPattern leftjoin operator.

Examples:

(prefix ((: <http://example/>))
  (leftjoin
    (bgp (triple ?x :p ?v))
    (bgp (triple ?y :q ?w))
    (= ?v 2)))

See Also:

Constant Summary collapse

NAME =
[:leftjoin]

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Instance Attribute Summary

Attributes included from Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Query

#each_solution, #empty?, #failed?, #graph_name=, #matched?, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #unshift, #variables

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, #variable?, #vars

Methods included from Expression

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

Constructor Details

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

Instance Method Details

#execute(queryable, options = {}) {|solution| ... } ⇒ RDF::Query::Solutions

Executes each operand with queryable and performs the leftjoin operation by adding every solution from the left, merging compatible solutions from the right that match an optional filter.

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

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

    any additional keyword options

Yields:

  • (solution)

    each matching solution

Yield Parameters:

  • solution (RDF::Query::Solution)

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/left_join.rb', line 37

def execute(queryable, options = {}, &block)
  filter = operand(2)

  debug(options) {"LeftJoin"}
  left = queryable.query(operand(0), options.merge(depth: options[:depth].to_i + 1))
  debug(options) {"=>(leftjoin left) #{left.inspect}"}

  right = queryable.query(operand(1), options.merge(depth: options[:depth].to_i + 1))
  debug(options) {"=>(leftjoin right) #{right.inspect}"}

  # LeftJoin(Ω1, Ω2, expr) =
  @solutions = RDF::Query::Solutions()
  left.each do |s1|
    load_left = true
    right.each do |s2|
      s = s2.merge(s1)
      expr = filter ? boolean(filter.evaluate(s)).true? : true rescue false
      debug(options) {"===>(evaluate) #{s.inspect}"} if filter

      if expr && s1.compatible?(s2)
        # { merge(μ1, μ2) | μ1 in Ω1 and μ2 in Ω2, and μ1 and μ2 are compatible and expr(merge(μ1, μ2)) is true }
        debug(options) {"=>(merge s1 s2) #{s.inspect}"}
        @solutions << s
        load_left = false   # Left solution added one or more times due to merge
      end
    end
    if load_left
      debug(options) {"=>(add) #{s1.inspect}"}
      @solutions << s1
    end
  end
  
  debug(options) {"=> #{@solutions.inspect}"}
  @solutions.each(&block) if block_given?
  @solutions
end

#optimizeUnion, RDF::Query

Returns an optimized version of this query.

If optimize operands, and if the first two operands are both Queries, replace with the unique sum of the query elements

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/left_join.rb', line 92

def optimize
  ops = operands.map {|o| o.optimize }.select {|o| o.respond_to?(:empty?) && !o.empty?}
  expr = ops.pop unless ops.last.executable?
  expr = nil if expr.respond_to?(:true?) && expr.true?
  
  # ops now is one or two executable operators
  # expr is a filter expression, which may have been optimized to 'true'
  case ops.length
  when 0
    RDF::Query.new  # Empty query, expr doesn't matter
  when 1
    expr ? Filter.new(expr, ops.first) : ops.first
  else
    expr ? LeftJoin(ops[0], ops[1], expr) : LeftJoin(ops[0], ops[1])
  end
end

#validate!Object

The same blank node label cannot be used in two different basic graph patterns in the same query



75
76
77
78
79
80
81
82
83
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/left_join.rb', line 75

def validate!
  left_nodes, right_nodes = operand(0).ndvars, operand(1).ndvars

  unless (left_nodes.compact & right_nodes.compact).empty?
    raise ArgumentError,
         "sub-operands share non-distinguished variables: #{(left_nodes.compact & right_nodes.compact).to_sse}"
  end
  super
end