Class: SPARQL::Algebra::Operator::Project

Inherits:
Binary show all
Includes:
Query
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/project.rb

Overview

The SPARQL GraphPattern project operator.

[9] SelectClause ::= 'SELECT' ( 'DISTINCT' | 'REDUCED' )? ( ( Var | ( '(' Expression 'AS' Var ')' ) )+ | '*' )

Basic Projection

Examples:

SPARQL Grammar

PREFIX : <http://example/>
SELECT ?v  { 
  ?s :p ?v . 
  FILTER (?v = 2)
}

SSE

(prefix ((: <http://example/>))
 (project (?v)
  (filter (= ?v 2)
   (bgp (triple ?s :p ?v)))))

SPARQL Grammar (Sub select)

SELECT (1 AS ?X ) {
  SELECT (2 AS ?Y ) {}
}

SSE (Sub select)

(project (?X)
 (extend ((?X 1))
  (project (?Y)
   (extend ((?Y 2))
    (bgp)))))

SPARQL Grammar (filter projection)

PREFIX : <http://www.example.org/>
ASK {
  {SELECT (GROUP_CONCAT(?o) AS ?g) WHERE {
   :a :p1 ?o
  }}
  FILTER(?g = "1 22" || ?g = "22 1")
}

SSE (filter projection)

(prefix ((: <http://www.example.org/>))
 (ask
  (filter
   (|| (= ?g "1 22") (= ?g "22 1"))
   (project (?g)
    (extend ((?g ??.0))
     (group () ((??.0 (group_concat ?o)))
      (bgp (triple :a :p1 ?o)))))) ))

See Also:

Constant Summary collapse

NAME =
[:project]

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

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

Methods included from Expression

cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #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 this query on the given queryable graph or repository. Reduces the result set to the variables listed in the first operand

If the first operand is empty, this indicates a SPARQL *, and all in-scope variables are projected.

Parameters:

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



103
104
105
106
107
108
109
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/project.rb', line 103

def execute(queryable, **options, &block)
  @solutions = queryable.query(operands.last, **options.merge(depth: options[:depth].to_i + 1))
  @solutions.variable_names = self.variables.keys
  @solutions = @solutions.project(*(operands.first)) unless operands.first.empty?
  @solutions.each(&block) if block_given?
  @solutions
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Extracts projections

If there are already extensions or filters, then this is a sub-select.

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/project.rb', line 132

def to_sparql(**options)
  vars = operands[0].empty? ? [:*] : operands[0]
  if options[:extensions] || options[:filter_ops] || options[:project]
    # Any of these options indicates we're in a sub-select
    opts = options.dup.delete_if {|k,v| %I{extensions filter_ops project}.include?(k)}
    content = operands.last.to_sparql(project: vars, **opts)
    content = "{#{content}}" unless content.start_with?('{') && content.end_with?('}')
    Operator.to_sparql(content, **options)
  else
    operands.last.to_sparql(project: vars, **options)
  end
end

#validate!Operator

Can only project in-scope variables.

Returns:

  • (Operator)

    a new instance of Operator



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/project.rb', line 64

def validate!
  if (group = descendants.detect {|o| o.is_a?(Group)})
    raise ArgumentError, "project * on group is illegal" if operands.first.empty?
    query_vars = operands.last.variables
    variables.keys.each do |v|
      raise ArgumentError,
        "projecting #{v.to_sse} not projected from group" unless
        query_vars.key?(v.to_sym)
    end
  end

  super
end

#variablesHash{Symbol => RDF::Query::Variable}

In-scope variables for a select are limited to those projected.



82
83
84
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/project.rb', line 82

def variables
  operands(1).inject({}) {|hash, o| hash.merge(o.variables)}
end