diff --git a/CONTRIBUTORS b/CONTRIBUTORS index acd2ccb01..624dd9155 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -45,6 +45,7 @@ Niklas Lindström Pierre-Antoine Champin Phil Dawes Phillip Pearson +Pritish Wadhwa Ron Alford Remi Chateauneu Sidnei da Silva diff --git a/rdflib/plugins/sparql/algebra.py b/rdflib/plugins/sparql/algebra.py index 5d7dac330..bbcd886bb 100644 --- a/rdflib/plugins/sparql/algebra.py +++ b/rdflib/plugins/sparql/algebra.py @@ -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 @@ -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 diff --git a/test/test_issues/test_issue166.py b/test/test_issues/test_issue166.py new file mode 100644 index 000000000..87b860d6d --- /dev/null +++ b/test/test_issues/test_issue166.py @@ -0,0 +1,15 @@ +import pytest + +import rdflib +from rdflib import Graph + + +def test_issue_166() -> None: + 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'), + ]