fix: GROUP_CONCAT handling of empty separator (issue) (#2474) · RDFLib/rdflib@e94c252 (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -245,11 +245,16 @@ def get_value(self) -> None:
245 245
246 246
247 247 class GroupConcat(Accumulator):
248 -def __init__(self, aggregation):
248 +value: List[Literal]
249 +
250 +def __init__(self, aggregation: CompValue):
249 251 super(GroupConcat, self).__init__(aggregation)
250 252 # only GROUPCONCAT needs to have a list as accumulator
251 253 self.value = []
252 -self.separator = aggregation.separator or " "
254 +if aggregation.separator is None:
255 +self.separator = " "
256 +else:
257 +self.separator = aggregation.separator
253 258
254 259 def update(self, row: FrozenBindings, aggregator: "Aggregator") -> None:
255 260 try:
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
11 11
12 12 import rdflib.plugins.sparql.algebra as algebra
13 13 import rdflib.plugins.sparql.parser as parser
14 +from rdflib import Graph, Literal, URIRef
14 15 from rdflib.plugins.sparql.algebra import translateAlgebra
15 16
16 17
@@ -304,3 +305,25 @@ def test_roundtrip(test_spec: AlgebraTest, data_path: Path) -> None:
304 305 # TODO: Execute the raw query (query_text) and the reconstituted query
305 306 # (query_from_query_from_algebra) against a well defined graph and ensure
306 307 # they yield the same result.
308 +
309 +
310 +def test_sparql_group_concat():
311 +"""Tests if GROUP_CONCAT correctly uses the separator keyword"""
312 +query = """
313 + PREFIX : http://example.org/
314 +
315 + SELECT ?subject (GROUP_CONCAT(?object; separator="")
316 + AS ?concatenatedObjects)
317 + WHERE {
318 + VALUES (?subject ?object) {
319 + (:pred "a")
320 + (:pred "b")
321 + (:pred "c")
322 + }
323 + }
324 + GROUP BY ?subject
325 + """
326 +
327 +g = Graph()
328 +q = dict(g.query(query))
329 +assert q[URIRef("http://example.org/pred")\] == Literal("abc")