Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Niklas Lindström
Pierre-Antoine Champin
Phil Dawes
Phillip Pearson
Pritish Wadhwa
Ron Alford
Remi Chateauneu
Sidnei da Silva
Expand Down
17 changes: 13 additions & 4 deletions rdflib/plugins/sparql/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,24 @@ def _findVars(x, res):
"""
Find all variables in a tree
"""
seenVars = set()
if isinstance(x, Variable):
res.add(x)
if x not in seenVars:
res.append(x)
seenVars.add(x)
if isinstance(x, CompValue):
if x.name == "Bind":
res.add(x.var)
if x.var not in seenVars:
res.append(x.var)
seenVars.add(x.var)
return x # stop recursion and finding vars in the expr
elif x.name == "SubSelect":
if x.projection:
res.update(v.var or v.evar for v in x.projection)
tempList = [v.var or v.evar for v in x.projection]
for a in tempList:
if a not in seenVars:
res.append(a)
seenVars.add(a)
return x


Expand Down Expand Up @@ -548,7 +557,7 @@ def translate(q):
q.where = traverse(q.where, visitPost=translatePath)

# TODO: Var scope test
VS = set()
VS = list()
traverse(q.where, functools.partial(_findVars, res=VS))

# all query types have a where part
Expand Down
15 changes: 15 additions & 0 deletions test/test_issues/test_issue166.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

import rdflib
from rdflib import Graph


def test_issue_166() -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just briefly, I would much rather prefer this goes into

pytest.param(
"""
SELECT * WHERE {
BIND(
NOT EXISTS {
rdfs:Class rdfs:label "Class"
}
AS ?bound
)
}
""",
[{Variable('bound'): Literal(False)}],
id="select-bind-notexists-const-true",
),
],
)
def test_queries(
query_string: str,
expected_bindings: Sequence[Mapping["Variable", "Identifier"]],
rdfs_graph: Graph,
) -> None:
"""
Results of queries against the rdfs.ttl return the expected values.
"""
query_tree = parseQuery(query_string)
logging.debug("query_tree = %s", prettify_parsetree(query_tree))
logging.debug("query_tree = %s", query_tree)
query = translateQuery(query_tree)
logging.debug("query = %s", query)
query._original_args = (query_string, {}, None)
result = rdfs_graph.query(query)
logging.debug("result = %s", result)
assert expected_bindings == result.bindings

g = Graph()
query = "SELECT * { ?a ?b ?c } LIMIT 10"
qres = g.query(query)
assert qres.vars == [
rdflib.term.Variable('a'),
rdflib.term.Variable('b'),
rdflib.term.Variable('c'),
]