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
19 changes: 19 additions & 0 deletions test/test_issues/test_issue1278.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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")}
}
}"""
assert results.bindings[0].get(Variable("o")) == Literal("c")
aucampia marked this conversation as resolved.
Show resolved Hide resolved