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

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Query
Defined in:
vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/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 collapse

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=, #bind, #boolean, #constant?, #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_sxp, #to_sxp_bin, #variable?, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, 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:

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
94
95
96
97
98
99
100
101
102
103
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/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, depth: options[:depth].to_i + 1, **options)

  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,
                                                      queryable: queryable,
                                                      depth: options[:depth].to_i + 1,
                                                      **options)
        else
          # Form is variable
          soln[operand] = operand.evaluate(solution, queryable: queryable,
                                                     depth: options[:depth].to_i + 1,
                                                     **options)
        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)

  # If there exprlist is empty, make sure that's at least an empty solution
  if @solutions.empty? && exprlist.empty?
    soln = RDF::Query::Solution.new
    aggregates.each do |(var, aggregate)|
      begin
        soln[var] = aggregate.aggregate([], **options)
      rescue TypeError
        # Ignored in output
      end
    end
    @solutions << soln
  end

  debug(options) {"=>(solutions) #{@solutions.inspect}"}
  @solutions.each(&block) if block_given?
  @solutions
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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/algebra/operator/group.rb', line 108

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