Skip to content

Commit a70a9c8

Browse files
authored
fix: generate VALUES block for federated queries with variables only (#2084)
Fixed the generation of VALUES block for federated queries. The values block was including non-variable values like BNodes which resulted in invalid queries. Closes #2079.
1 parent a39d143 commit a70a9c8

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ and will be removed for release.
267267
<!-- -->
268268
<!-- -->
269269

270+
271+
<!-- -->
272+
<!-- -->
273+
<!-- CHANGE BARRIER: START PR #2079 -->
274+
<!-- -->
275+
<!-- -->
276+
277+
- Fixed the generation of VALUES block for federated queries.
278+
The values block was including non-variable values like BNodes which resulted
279+
in invalid queries.
280+
[PR #2079](https://github.com/RDFLib/rdflib/pull/2079).
281+
282+
<!-- -->
283+
<!-- -->
284+
<!-- CHANGE BARRIER: END PR #2079 -->
285+
<!-- -->
286+
<!-- -->
287+
270288
<!-- -->
271289
<!-- -->
272290
<!-- CHANGE BARRIER: START -->

rdflib/plugins/sparql/evaluate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def _buildQueryStringForServiceCall(ctx: QueryContext, match: re.Match) -> str:
386386
base = ctx.prologue.base # type: ignore[union-attr]
387387
if base is not None and len(base) > 0:
388388
service_query = "BASE <" + base + "> " + service_query
389-
sol = ctx.solution()
389+
sol = [v for v in ctx.solution() if isinstance(v, Variable)]
390390
if len(sol) > 0:
391391
variables = " ".join([v.n3() for v in sol])
392392
variables_bound = " ".join([ctx.get(v).n3() for v in sol])

test/test_sparql/test_service.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,41 @@ def test_service_with_bind():
7070
assert len(r) == 3
7171

7272

73+
def test_service_with_bound_solutions():
74+
g = Graph()
75+
g.update(
76+
"""
77+
INSERT DATA {
78+
[]
79+
<http://www.w3.org/2002/07/owl#sameAs> <http://de.dbpedia.org/resource/John_Lilburne> ;
80+
<http://purl.org/dc/terms/subject> <http://dbpedia.org/resource/Category:1614_births> .
81+
}
82+
"""
83+
)
84+
q = """
85+
SELECT ?sameAs ?dbpComment ?subject WHERE {
86+
[]
87+
<http://www.w3.org/2002/07/owl#sameAs> ?sameAs ;
88+
<http://purl.org/dc/terms/subject> ?subject .
89+
90+
SERVICE <http://DBpedia.org/sparql> {
91+
SELECT ?sameAs ?dbpComment ?subject WHERE {
92+
<http://dbpedia.org/resource/John_Lilburne>
93+
<http://www.w3.org/2002/07/owl#sameAs> ?sameAs ;
94+
<http://www.w3.org/2000/01/rdf-schema#comment> ?dbpComment ;
95+
<http://purl.org/dc/terms/subject> ?subject .
96+
}
97+
}
98+
}
99+
LIMIT 2
100+
"""
101+
results = helper.query_with_retry(g, q)
102+
assert len(results) == 2
103+
104+
for r in results:
105+
assert len(r) == 3
106+
107+
73108
def test_service_with_values():
74109
g = Graph()
75110
q = """select ?sameAs ?dbpComment ?subject
@@ -326,6 +361,7 @@ def test_with_mock(
326361
if __name__ == "__main__":
327362
test_service()
328363
test_service_with_bind()
364+
test_service_with_bound_solutions()
329365
test_service_with_values()
330366
test_service_with_implicit_select()
331367
test_service_with_implicit_select_and_prefix()

0 commit comments

Comments
 (0)