Class: SPARQL::Algebra::Operator::Group

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

Overview

The SPARQL group operator.

group takes either two or three operands. The first operand is an array of grouped variables. The last operand is the query to be grouped. If three operands are provided, the second is an array of temporary bindings.

Examples:

(prefix ((: <http://example/>))
  (project (?w ?S)
    (extend ((?S ?.0))
      (group (?w) ((?.0 (sample ?v)))
        (leftjoin
          (bgp (triple ?s :p ?v))
          (bgp (triple ?s :q ?w)))))))

See Also:

Constant Summary

NAME =
[:group]

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 query with queryable and groups results based on the first operand.

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:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/group.rb', line 41

def execute(queryable, options = {}, &block)
  debug(options) {"Group"}
  exprlist = operands.first
  query = operands.last
  aggregates = operands.length == 3 ? operand(1) : []
  solutions = queryable.query(query, options.merge(depth: options[:depth].to_i + 1))

  groups = solutions.group_by do |solution|
    # Evaluate each exprlist operand to get groups where each key is a new solution
    # ListEval((expr1, ..., exprn), μ) returns a list (e1, ..., en), where ei = expri(μ) or error.
    soln = RDF::Query::Solution.new
    exprlist.each do |operand|
      begin
        if operand.is_a?(Array)
          # Form is [variable, expression]
          soln[operand.first] = operand.last.evaluate(solution,
                                                      options.merge(
                                                       queryable: queryable,
                                                       depth: options[:depth].to_i + 1))
        else
          # Form is variable
          soln[operand] = operand.evaluate(solution, options.merge(
                                                      queryable: queryable,
                                                      depth: options[:depth].to_i + 1))
        end
      rescue TypeError
        # Ignore expression
      end
    end
    soln
  end

  debug(options) {"=>(groups) #{groups.inspect}"}

  # Aggregate solutions in each group using aggregates to get solutions
  @solutions = RDF::Query::Solutions(groups.map do |group_soln, solns|
    aggregates.each do |(var, aggregate)|
      begin
        group_soln[var] = aggregate.aggregate(solns, options)
      rescue TypeError
        # Ignored in output
      end
    end
    group_soln
  end)

  # Make sure that's at least an empty solution
  @solutions << RDF::Query::Solution.new if @solutions.empty?

  debug(options) {"=>(solutions) #{@solutions.inspect}"}
  @solutions.each(&block) if block_given?
  @solutions
end

#optimizeGroup

Returns an optimized version of this query.

TODO

Returns:



122
123
124
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/group.rb', line 122

def optimize
  self
end

#validate!Object

It is an error for aggregates to project variables with a name already used in other aggregate projections, or in the WHERE clause.

It is also an error to project ungrouped variables



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/group.rb', line 98

def validate!
  group_vars = operand(0).map {|v| Array(v).first}
  ext = first_ancestor(Extend)
  extend_vars = ext ? ext.operand(0).map(&:first).select {|v| v.is_a?(RDF::Query::Variable)} : []
  project = first_ancestor(Project)
  # If not projecting, were are effectively projecting all variables in the query
  project_vars = project ? project.operand(0) : operands.last.vars

  available_vars = (extend_vars + group_vars).compact

  # All variables must either be grouped or extended
  unless (project_vars - available_vars).empty?
    raise ArgumentError,
         "projecting ungrouped/extended variables: #{(project_vars.compact - available_vars.compact).to_sse}"
  end
  super
end