Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix simple literals returned as NULL using SERVICE (issue #1278) #1894

Merged
merged 14 commits into from
May 15, 2022
20 changes: 12 additions & 8 deletions rdflib/plugins/sparql/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,18 +381,22 @@ def _yieldBindingsFromServiceCallResult(ctx: QueryContext, r, variables):
res_dict: Dict[Variable, Identifier] = {}
for var in variables:
if var in r and r[var]:
if r[var]["type"] == "uri":
res_dict[Variable(var)] = URIRef(r[var]["value"])
elif r[var]["type"] == "bnode":
res_dict[Variable(var)] = BNode(r[var]["value"])
elif r[var]["type"] == "literal" and "datatype" in r[var]:
d = r[var]
t = d["type"]
if t == "uri":
res_dict[Variable(var)] = URIRef(d["value"])
elif t == "literal":
res_dict[Variable(var)] = Literal(
r[var]["value"], datatype=r[var]["datatype"]
d["value"], datatype=d.get("datatype"), lang=d.get("xml:lang")
)
elif r[var]["type"] == "literal" and "xml:lang" in r[var]:
elif t == "typed-literal":
aucampia marked this conversation as resolved.
Show resolved Hide resolved
res_dict[Variable(var)] = Literal(
r[var]["value"], lang=r[var]["xml:lang"]
d["value"], datatype=URIRef(d["datatype"])
)
elif t == "bnode":
res_dict[Variable(var)] = BNode(d["value"])
else:
raise NotImplementedError("json term type %r" % t)
aucampia marked this conversation as resolved.
Show resolved Hide resolved
yield FrozenBindings(ctx, res_dict)


Expand Down
20 changes: 20 additions & 0 deletions test/test_issues/test_issue1278.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from test.utils import helper

from rdflib import Graph, Literal, Variable


def test():
"""Test service returns simple literals not as NULL.

Issue: https://github.com/RDFLib/rdflib/issues/1278
"""

g = Graph()
q = """SELECT ?s ?p ?o
WHERE {
SERVICE <https://DBpedia.org/sparql> {
VALUES (?s ?p ?o) {(<http://example.org/a> <http://example.org/b> "c")}
}
}"""
results = helper.query_with_retry(g, q)
assert results.bindings[0].get(Variable("o")) == Literal("c")
27 changes: 27 additions & 0 deletions test/test_sparql/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ def test_service_with_implicit_select_and_allcaps():
assert len(results) == 3


def test_service_node_types():
"""Test if SERVICE properly returns different types of nodes:
- URI;
- Simple Literal;
- Literal with datatype ;
- Literal with language tag .
"""

g = Graph()
q = """
SELECT ?o
WHERE {
SERVICE <https://dbpedia.org/sparql> {
VALUES (?s ?p ?o) {
(<http://example.org/a> <http://example.org/uri> <http://example.org/URI>)
(<http://example.org/a> <http://example.org/simpleLiteral> "Simple Literal")
(<http://example.org/a> <http://example.org/dataType> "String Literal"^^xsd:string)
(<http://example.org/a> <http://example.org/language> "String Language"@en)
}
}
FILTER( ?o IN (<http://example.org/URI>, "Simple Literal", "String Literal"^^xsd:string, "String Language"@en) )
}"""
results = helper.query_with_retry(g, q)
assert len(results) == 4


# def test_with_fixture(httpserver):
# httpserver.expect_request("/sparql/?query=SELECT * WHERE ?s ?p ?o").respond_with_json({"vars": ["s","p","o"], "bindings":[]})
# test_server = httpserver.url_for('/sparql')
Expand All @@ -151,3 +177,4 @@ def test_service_with_implicit_select_and_allcaps():
test_service_with_implicit_select()
test_service_with_implicit_select_and_prefix()
test_service_with_implicit_select_and_base()
test_service_node_types()