diff options
| author | Veyndan Stuart <veyndan@gmail.com> | 2022-08-21 17:58:49 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-21 17:58:49 +0200 |
| commit | a70a9c82649f9345c3691d69df6f1108e9e05871 (patch) | |
| tree | 197f2d338300f5d21b8468755c194250de8f2388 | |
| parent | a39d1436e00affc3cba9e903b22700029d8e8163 (diff) | |
| download | rdflib-a70a9c82649f9345c3691d69df6f1108e9e05871.tar.gz | |
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.
| -rw-r--r-- | CHANGELOG.md | 18 | ||||
| -rw-r--r-- | rdflib/plugins/sparql/evaluate.py | 2 | ||||
| -rw-r--r-- | test/test_sparql/test_service.py | 36 |
3 files changed, 55 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d006172c..bede2f68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -267,6 +267,24 @@ and will be removed for release. <!-- --> <!-- --> + +<!-- --> +<!-- --> +<!-- CHANGE BARRIER: START PR #2079 --> +<!-- --> +<!-- --> + +- Fixed the generation of VALUES block for federated queries. + The values block was including non-variable values like BNodes which resulted + in invalid queries. + [PR #2079](https://github.com/RDFLib/rdflib/pull/2079). + +<!-- --> +<!-- --> +<!-- CHANGE BARRIER: END PR #2079 --> +<!-- --> +<!-- --> + <!-- --> <!-- --> <!-- CHANGE BARRIER: START --> diff --git a/rdflib/plugins/sparql/evaluate.py b/rdflib/plugins/sparql/evaluate.py index 7b0f6e50..706b7673 100644 --- a/rdflib/plugins/sparql/evaluate.py +++ b/rdflib/plugins/sparql/evaluate.py @@ -386,7 +386,7 @@ def _buildQueryStringForServiceCall(ctx: QueryContext, match: re.Match) -> str: base = ctx.prologue.base # type: ignore[union-attr] if base is not None and len(base) > 0: service_query = "BASE <" + base + "> " + service_query - sol = ctx.solution() + sol = [v for v in ctx.solution() if isinstance(v, Variable)] if len(sol) > 0: variables = " ".join([v.n3() for v in sol]) variables_bound = " ".join([ctx.get(v).n3() for v in sol]) diff --git a/test/test_sparql/test_service.py b/test/test_sparql/test_service.py index 8ed09cc8..53019771 100644 --- a/test/test_sparql/test_service.py +++ b/test/test_sparql/test_service.py @@ -70,6 +70,41 @@ def test_service_with_bind(): assert len(r) == 3 +def test_service_with_bound_solutions(): + g = Graph() + g.update( + """ + INSERT DATA { + [] + <http://www.w3.org/2002/07/owl#sameAs> <http://de.dbpedia.org/resource/John_Lilburne> ; + <http://purl.org/dc/terms/subject> <http://dbpedia.org/resource/Category:1614_births> . + } + """ + ) + q = """ + SELECT ?sameAs ?dbpComment ?subject WHERE { + [] + <http://www.w3.org/2002/07/owl#sameAs> ?sameAs ; + <http://purl.org/dc/terms/subject> ?subject . + + SERVICE <http://DBpedia.org/sparql> { + SELECT ?sameAs ?dbpComment ?subject WHERE { + <http://dbpedia.org/resource/John_Lilburne> + <http://www.w3.org/2002/07/owl#sameAs> ?sameAs ; + <http://www.w3.org/2000/01/rdf-schema#comment> ?dbpComment ; + <http://purl.org/dc/terms/subject> ?subject . + } + } + } + LIMIT 2 + """ + results = helper.query_with_retry(g, q) + assert len(results) == 2 + + for r in results: + assert len(r) == 3 + + def test_service_with_values(): g = Graph() q = """select ?sameAs ?dbpComment ?subject @@ -326,6 +361,7 @@ def test_with_mock( if __name__ == "__main__": test_service() test_service_with_bind() + test_service_with_bound_solutions() test_service_with_values() test_service_with_implicit_select() test_service_with_implicit_select_and_prefix() |
