Class: SPARQL::Algebra::Operator::GroupConcat

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Aggregate
Defined in:
lib/sparql/algebra/operator/group_concat.rb

Overview

The SPARQL groupconcat set function.

GroupConcat is a set function which performs a string concatenation across the values of an expression with a group. The order of the strings is not specified. The separator character used in the concatenation may be given with the scalar argument SEPARATOR.

[127] Aggregate::= ... | 'GROUP_CONCAT' '(' 'DISTINCT'? Expression ( ';' 'SEPARATOR' '=' String )? ')'

Examples:

SPARQL Grammar

SELECT (GROUP_CONCAT(?x) AS ?y) {}

SSE

(project (?y)
 (extend ((?y ??.0))
  (group () ((??.0 (group_concat ?x)))
   (bgp))))

SPARQL Grammar (DISTINCT)

SELECT (GROUP_CONCAT(DISTINCT ?x) AS ?y) {}

SSE (DISTINCT)

(project (?y)
 (extend ((?y ??.0))
  (group () ((??.0 (group_concat distinct ?x)))
   (bgp))))

SPARQL Grammar (SEPARATOR)

SELECT (GROUP_CONCAT(?x; SEPARATOR=';') AS ?y) {}

SSE (SEPARATOR)

(project (?y)
 (extend ((?y ??.0))
  (group () ((??.0 (group_concat (separator ";") ?x)))
   (bgp))))

See Also:

Constant Summary collapse

NAME =
:group_concat

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Aggregate

#replace_aggregate!, #replace_vars!

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

Constructor Details

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

Instance Method Details

#aggregate(solutions = [], **options) ⇒ RDF::Term

This method is abstract.

One, two or three operands, the first may be distinct, the last operand, if it exists, is a separator, defaulting to ' '.

Parameters:

  • solutions (Enumerable<RDF::Query::Solution>) (defaults to: [])

    ([]) an enumerable set of query solutions

  • options (Hash{Symbol => Object})

    ({}) options passed from query

Returns:

Raises:

  • (TypeError)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sparql/algebra/operator/group_concat.rb', line 53

def aggregate(solutions = [], **options)
  operands.shift if distinct = (operands.first == :distinct)
  sep = operands.length == 2 ? operand(0).last : RDF::Literal(' ')
  args_enum = solutions.map do |solution|
    begin
      operands.last.evaluate(solution, **options.merge(depth: options[:depth].to_i + 1))
    rescue TypeError
      # Ignore errors
      nil
    end
  end
  apply(distinct ? args_enum.uniq : args_enum, sep)
end

#apply(enum, separator, **options) ⇒ RDF::Term

GroupConcat is a set function which performs a string concatenation across the values of an expression with a group. The order of the strings is not specified. The separator character used in the concatenation may be given with the scalar argument SEPARATOR.

Parameters:

Returns:

Raises:

  • (TypeError)

    If enum is empty



74
75
76
# File 'lib/sparql/algebra/operator/group_concat.rb', line 74

def apply(enum, separator, **options)
  RDF::Literal(enum.flatten.map(&:to_s).join(separator.to_s))
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:

  • (String)


83
84
85
86
87
88
89
90
91
# File 'lib/sparql/algebra/operator/group_concat.rb', line 83

def to_sparql(**options)
  distinct = operands.first == :distinct
  args = distinct ? operands[1..-1] : operands
  separator = args.first.last if args.first.is_a?(Array) && args.first.first == :separator
  args = args[1..-1] if separator
  str = "GROUP_CONCAT(#{'DISTINCT ' if distinct}#{args.to_sparql(delimiter: ', ', **options)}"
  str << "; SEPARATOR=#{separator.to_sparql}" if separator
  str << ")"
end