Skip to content

Commit

Permalink
fix: GROUP_CONCAT handling of empty separator (issue) (#2474)
Browse files Browse the repository at this point in the history
`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>
  • Loading branch information
WhiteGobo and WhiteGobo authored Jul 8, 2023
1 parent 0ea6ca5 commit e94c252
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
9 changes: 7 additions & 2 deletions rdflib/plugins/sparql/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,16 @@ def get_value(self) -> None:


class GroupConcat(Accumulator):
def __init__(self, aggregation):
value: List[Literal]

def __init__(self, aggregation: CompValue):
super(GroupConcat, self).__init__(aggregation)
# only GROUPCONCAT needs to have a list as accumulator
self.value = []
self.separator = aggregation.separator or " "
if aggregation.separator is None:
self.separator = " "
else:
self.separator = aggregation.separator

def update(self, row: FrozenBindings, aggregator: "Aggregator") -> None:
try:
Expand Down
23 changes: 23 additions & 0 deletions test/test_sparql/test_translate_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import rdflib.plugins.sparql.algebra as algebra
import rdflib.plugins.sparql.parser as parser
from rdflib import Graph, Literal, URIRef
from rdflib.plugins.sparql.algebra import translateAlgebra


Expand Down Expand Up @@ -304,3 +305,25 @@ def test_roundtrip(test_spec: AlgebraTest, data_path: Path) -> None:
# TODO: Execute the raw query (query_text) and the reconstituted query
# (query_from_query_from_algebra) against a well defined graph and ensure
# they yield the same result.


def test_sparql_group_concat():
"""Tests if GROUP_CONCAT correctly uses the separator keyword"""
query = """
PREFIX : <http://example.org/>
SELECT ?subject (GROUP_CONCAT(?object; separator="")
AS ?concatenatedObjects)
WHERE {
VALUES (?subject ?object) {
(:pred "a")
(:pred "b")
(:pred "c")
}
}
GROUP BY ?subject
"""

g = Graph()
q = dict(g.query(query))
assert q[URIRef("http://example.org/pred")] == Literal("abc")

0 comments on commit e94c252

Please sign in to comment.