Skip to content

Commit e94c252

Browse files
WhiteGoboWhiteGobo
andauthored
fix: GROUP_CONCAT handling of empty separator (issue) (#2474)
`GROUP_CONCAT` was handling an empty separator (i.e. `""`) incorrectly, it would handle it as if the separator were not set, so essentially it was treated as a single space (i.e. `" "`). This change fixes it so that an empty separator with `GROUP_CONCAT` results in a value with nothing between concatenated values. Fixes <#2473> --------- Co-authored-by: WhiteGobo <richardfechner@posteo.net>
1 parent 0ea6ca5 commit e94c252

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

rdflib/plugins/sparql/aggregates.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,16 @@ def get_value(self) -> None:
245245

246246

247247
class GroupConcat(Accumulator):
248-
def __init__(self, aggregation):
248+
value: List[Literal]
249+
250+
def __init__(self, aggregation: CompValue):
249251
super(GroupConcat, self).__init__(aggregation)
250252
# only GROUPCONCAT needs to have a list as accumulator
251253
self.value = []
252-
self.separator = aggregation.separator or " "
254+
if aggregation.separator is None:
255+
self.separator = " "
256+
else:
257+
self.separator = aggregation.separator
253258

254259
def update(self, row: FrozenBindings, aggregator: "Aggregator") -> None:
255260
try:

test/test_sparql/test_translate_algebra.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import rdflib.plugins.sparql.algebra as algebra
1313
import rdflib.plugins.sparql.parser as parser
14+
from rdflib import Graph, Literal, URIRef
1415
from rdflib.plugins.sparql.algebra import translateAlgebra
1516

1617

@@ -304,3 +305,25 @@ def test_roundtrip(test_spec: AlgebraTest, data_path: Path) -> None:
304305
# TODO: Execute the raw query (query_text) and the reconstituted query
305306
# (query_from_query_from_algebra) against a well defined graph and ensure
306307
# 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")

0 commit comments

Comments
 (0)