Class: SPARQL::Algebra::Operator::GroupConcat
- 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.
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 )? ')'
Constant Summary collapse
- NAME =
:group_concat
Instance Method Summary collapse
-
#aggregate(solutions = [], **options) ⇒ RDF::Term
abstract
One, two or three operands, the first may be
distinct, the last operand, if it exists, is a separator, defaulting to ' '. -
#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.
-
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
Methods included from Aggregate
#replace_aggregate!, #replace_vars!
Instance Method Details
#aggregate(solutions = [], **options) ⇒ RDF::Term
One, two or three operands, the first may be distinct, the last operand, if it exists, is a separator, defaulting to ' '.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sparql/algebra/operator/group_concat.rb', line 55 def aggregate(solutions = [], **) 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, **.merge(depth: [: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.
76 77 78 79 80 |
# File 'lib/sparql/algebra/operator/group_concat.rb', line 76 def apply(enum, separator, **) 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.
87 88 89 90 91 92 93 94 95 |
# File 'lib/sparql/algebra/operator/group_concat.rb', line 87 def to_sparql(**) 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: ', ', **)}" str << "; SEPARATOR=#{separator.to_sparql}" if separator str << ")" end |