Class: SPARQL::Algebra::Operator::Union

Inherits:
Binary show all
Includes:
Query
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/operator/union.rb

Overview

The SPARQL GraphPattern union operator.

[67] GroupOrUnionGraphPattern::= GroupGraphPattern ( 'UNION' GroupGraphPattern )*

Examples:

SPARQL Grammar

SELECT * {
  { ?s ?p ?o }
  UNION
  { GRAPH ?g { ?s ?p ?o } }}

SSE

(union
 (bgp (triple ?s ?p ?o))
 (graph ?g
  (bgp (triple ?s ?p ?o))))

See Also:

Constant Summary collapse

NAME =
[:union]

Constants inherited from Binary

Binary::ARITY

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 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 Binary

#initialize

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, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #parent, #parent=, prefixes, #prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #variable?, #variables, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, parse, register_extension, #to_sxp_bin, #valid?, #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

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

Instance Method Details

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

Executes each operand with queryable and performs the union operation by creating a new solution set consiting of all solutions from both operands.

Parameters:

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



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

def execute(queryable, **options, &block)
  debug(options) {"Union"}
  @solutions = RDF::Query::Solutions(operands.inject([]) do |memo, op|
    solns = op.execute(queryable, **options.merge(depth: options[:depth].to_i + 1))
    debug(options) {"=> (op) #{solns.inspect}"}
    memo + solns
  end)
  debug(options) {"=> #{@solutions.inspect}"}
  @solutions.each(&block) if block_given?
  @solutions
end

#optimize!(**options) ⇒ self

Optimizes this query.

Optimize operands and remove any which are empty.

Returns:

  • (self)

See Also:



71
72
73
74
75
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/operator/union.rb', line 71

def optimize!(**options)
  ops = operands.map {|o| o.optimize(**options) }.select {|o| o.respond_to?(:empty?) && !o.empty?}
  @operands = ops
  self
end

#to_sparql(top_level: true, **options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Parameters:

  • top_level (Boolean) (defaults to: true)

    (true) Treat this as a top-level, generating SELECT ... WHERE {}

Returns:



84
85
86
87
88
89
90
91
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/operator/union.rb', line 84

def to_sparql(top_level: true, **options)
  str = "{\n"
  str << operands[0].to_sparql(top_level: false, **options)
  str << "\n} UNION {\n"
  str << operands[1].to_sparql(top_level: false, **options)
  str << "\n}"
  top_level ? Operator.to_sparql(str, **options) : str
end

#validate!Object

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



54
55
56
57
58
59
60
61
62
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/operator/union.rb', line 54

def validate!
  left_nodes, right_nodes = operand(0).ndvars.map(&:name), operand(1).ndvars.map(&:name)

  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