Skip to content

Commit 796d3be

Browse files
committed
Group Concat adds language if all operands have the same language.
1 parent b65a633 commit 796d3be

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

lib/sparql/algebra/operator/group_concat.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class Operator
55
#
66
# 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.
77
#
8+
# If all operands are language-tagged strings with the same language (and direction), the result shares the language (and direction).
9+
#
810
# [127] Aggregate::= ... | 'GROUP_CONCAT' '(' 'DISTINCT'? Expression ( ';' 'SEPARATOR' '=' String )? ')'
911
#
1012
# @example SPARQL Grammar
@@ -72,7 +74,9 @@ def aggregate(solutions = [], **options)
7274
# @return [RDF::Term] An arbitrary term
7375
# @raise [TypeError] If enum is empty
7476
def apply(enum, separator, **options)
75-
RDF::Literal(enum.flatten.map(&:to_s).join(separator.to_s))
77+
op1_lang = enum.first.language
78+
lang = op1_lang if op1_lang && enum.all? {|v| v.language == op1_lang}
79+
RDF::Literal(enum.flatten.map(&:to_s).join(separator.to_s), language: lang)
7680
end
7781

7882
##

script/tc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def run_tc(tc, **options)
8080
tc.expected.dump(:trig, standard_prefixes: true) :
8181
(tc.solutions.is_a?(RDF::Enumerable) ?
8282
tc.solutions.dump(:trig, standard_prefixes: true) :
83-
(tc.solutions ? "Vars: #{tc.solutions.variable_names}\n" + tc.solutions.to_sse : '')))
83+
(tc.solutions.respond_to?(:variable_names) ?
84+
("Vars: #{tc.solutions.variable_names}\n" + tc.solutions.to_sse) :
85+
(tc.solutions ? tc.solutions.to_sse : ''))))
8486
end
8587

8688
case tc.name
@@ -131,7 +133,9 @@ def run_tc(tc, **options)
131133
STDERR.puts "\nActual:\n" + (
132134
actual.is_a?(RDF::Enumerable) ?
133135
actual.dump(:trig, standard_prefixes: true) :
134-
"Vars: #{actual.variable_names}\n" + actual.to_sse)
136+
(actual.respond_to?(:variable_names) ?
137+
("Vars: #{actual.variable_names}\n" + actual.to_sse) :
138+
(actual ? actual.to_sse : '')))
135139
end
136140

137141
case tc.form
@@ -368,12 +372,12 @@ opts.each do |opt, arg|
368372
when '--optimize' then options[:optimize] = true
369373
when '--output' then options[:output] = File.open(arg, "w")
370374
when '--quiet'
371-
options[:quiet] = true
372-
logger.level = Logger::FATAL
375+
options[:quiet] = true
376+
logger.level = Logger::FATAL
373377
when '--sparql10' then options[:sparql10] = true
374378
when '--sparql11' then options[:sparql11] = true
375379
when '--sparql12' then options[:sparql12] = true
376-
when '--sparqldev' then options[:sparqldev] = true
380+
when '--sparqldev' then options[:sparqldev] = true
377381
when '--use11' then options[:use11] = true
378382
when '--validate' then options[:validate] = true
379383
when '--verbose' then options[:verbose] = true

spec/grammar/misc_spec.rb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,31 +140,31 @@
140140
(triple ?ev <http://example.org/b> ?b)))))
141141
}
142142
},
143-
"dawg-optional-filter-005-not-simplified" => {
144-
query: %(
145-
# Double curly braces do NOT get simplified to single curly braces early on, before filters are scoped
146-
PREFIX dc: <http://purl.org/dc/elements/1.1/>
147-
PREFIX x: <http://example.org/ns#>
148-
SELECT ?title ?price
149-
WHERE
150-
{ ?book dc:title ?title .
151-
OPTIONAL
152-
{
153-
{
154-
?book x:price ?price .
155-
FILTER (?title = "TITLE 2") .
156-
}
157-
} .
158-
}
159-
),
160-
sse: %{(prefix ((dc: <http://purl.org/dc/elements/1.1/>) (x: <http://example.org/ns#>))
161-
(project (?title ?price)
162-
(leftjoin
163-
(bgp (triple ?book dc:title ?title))
164-
(filter (= ?title "TITLE 2")
165-
(bgp (triple ?book x:price ?price))))))
166-
}
167-
}
143+
#"dawg-optional-filter-005-not-simplified" => {
144+
# query: %(
145+
# # Double curly braces do NOT get simplified to single curly braces early on, before filters are scoped
146+
# PREFIX dc: <http://purl.org/dc/elements/1.1/>
147+
# PREFIX x: <http://example.org/ns#>
148+
# SELECT ?title ?price
149+
# WHERE
150+
# { ?book dc:title ?title .
151+
# OPTIONAL
152+
# {
153+
# {
154+
# ?book x:price ?price .
155+
# FILTER (?title = "TITLE 2") .
156+
# }
157+
# } .
158+
# }
159+
# ),
160+
# sse: %{(prefix ((dc: <http://purl.org/dc/elements/1.1/>) (x: <http://example.org/ns#>))
161+
# (project (?title ?price)
162+
# (leftjoin
163+
# (bgp (triple ?book dc:title ?title))
164+
# (filter (= ?title "TITLE 2")
165+
# (bgp (triple ?book x:price ?price))))))
166+
# }
167+
#},
168168
}.each do |test, options|
169169
it "parses #{test}" do
170170
expect(options[:query]).to generate(options[:sse], logger: logger)

0 commit comments

Comments
 (0)