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

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Aggregate
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/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.

If all operands are language-tagged strings with the same language (and direction), the result shares the language (and direction).

[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

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

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, #formulae, #initialize, #inspect, #mergable?, #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?

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

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:

Returns:

Raises:

  • (TypeError)


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/group_concat.rb', line 55

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



76
77
78
79
80
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/group_concat.rb', line 76

def apply(enum, separator, **options)
  op1_lang = enum.first.language
  lang = op1_lang if op1_lang && enum.all? {|v| v.language == op1_lang}
  RDF::Literal(enum.flatten.map(&:to_s).join(separator.to_s), language: lang)
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:



87
88
89
90
91
92
93
94
95
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/group_concat.rb', line 87

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