From 6bbfcd51bacab58e008bbf864733301abad26893 Mon Sep 17 00:00:00 2001 From: PritishWadhwa Date: Mon, 9 May 2022 14:17:54 +0530 Subject: [PATCH 1/6] Issue 166 in SPARQLWrapper solved --- rdflib/plugins/sparql/algebra.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/rdflib/plugins/sparql/algebra.py b/rdflib/plugins/sparql/algebra.py index 5d7dac330..332d72797 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 not in seenVars: + res.append(x) + seenVars.add(x) 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 From 15d9736fb429a78192ab0138ebe4f5327e35e6ae Mon Sep 17 00:00:00 2001 From: Pritish Wadhwa <55271875+PritishWadhwa@users.noreply.github.com> Date: Mon, 9 May 2022 14:29:56 +0530 Subject: [PATCH 2/6] Update algebra.py --- rdflib/plugins/sparql/algebra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/plugins/sparql/algebra.py b/rdflib/plugins/sparql/algebra.py index 332d72797..6f44c4286 100644 --- a/rdflib/plugins/sparql/algebra.py +++ b/rdflib/plugins/sparql/algebra.py @@ -557,7 +557,7 @@ def translate(q): q.where = traverse(q.where, visitPost=translatePath) # TODO: Var scope test - VS = list() + VS = [] traverse(q.where, functools.partial(_findVars, res=VS)) # all query types have a where part From f92a0432c86760c2f25eb659a60f83ea1f39d4df Mon Sep 17 00:00:00 2001 From: Pritish Wadhwa <55271875+PritishWadhwa@users.noreply.github.com> Date: Mon, 9 May 2022 14:35:06 +0530 Subject: [PATCH 3/6] Update algebra.py --- rdflib/plugins/sparql/algebra.py | 42 +++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/rdflib/plugins/sparql/algebra.py b/rdflib/plugins/sparql/algebra.py index 6f44c4286..cbefb4e46 100644 --- a/rdflib/plugins/sparql/algebra.py +++ b/rdflib/plugins/sparql/algebra.py @@ -417,30 +417,44 @@ def _aggs(e, A): e["res"] = aggvar return aggvar - def _findVars(x, res): """ Find all variables in a tree """ - seenVars = set() if isinstance(x, Variable): - if x not in seenVars: - res.append(x) - seenVars.add(x) + res.add(x) if isinstance(x, CompValue): if x.name == "Bind": - if x not in seenVars: - res.append(x) - seenVars.add(x) + res.add(x.var) return x # stop recursion and finding vars in the expr elif x.name == "SubSelect": if 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) + res.update(v.var or v.evar for v in x.projection) return x + +# def _findVars(x, res): +# """ +# Find all variables in a tree +# """ +# seenVars = set() +# if isinstance(x, Variable): +# if x not in seenVars: +# res.append(x) +# seenVars.add(x) +# if isinstance(x, CompValue): +# if x.name == "Bind": +# if x not in seenVars: +# res.append(x) +# seenVars.add(x) +# return x # stop recursion and finding vars in the expr +# elif x.name == "SubSelect": +# if 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 def _addVars(x, children): @@ -557,7 +571,7 @@ def translate(q): q.where = traverse(q.where, visitPost=translatePath) # TODO: Var scope test - VS = [] + VS = set() traverse(q.where, functools.partial(_findVars, res=VS)) # all query types have a where part From 0a1f04a596412ae0a654d90647772269b2561d1e Mon Sep 17 00:00:00 2001 From: Pritish Wadhwa <55271875+PritishWadhwa@users.noreply.github.com> Date: Mon, 9 May 2022 18:21:36 +0530 Subject: [PATCH 4/6] Update algebra.py --- rdflib/plugins/sparql/algebra.py | 44 ++++++++++---------------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/rdflib/plugins/sparql/algebra.py b/rdflib/plugins/sparql/algebra.py index cbefb4e46..31e3be2c5 100644 --- a/rdflib/plugins/sparql/algebra.py +++ b/rdflib/plugins/sparql/algebra.py @@ -416,46 +416,30 @@ def _aggs(e, A): aggvar = Variable("__agg_%d__" % len(A)) e["res"] = aggvar return aggvar - + 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 not in seenVars: + res.append(x) + seenVars.add(x) 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 - -# def _findVars(x, res): -# """ -# Find all variables in a tree -# """ -# seenVars = set() -# if isinstance(x, Variable): -# if x not in seenVars: -# res.append(x) -# seenVars.add(x) -# if isinstance(x, CompValue): -# if x.name == "Bind": -# if x not in seenVars: -# res.append(x) -# seenVars.add(x) -# return x # stop recursion and finding vars in the expr -# elif x.name == "SubSelect": -# if 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 - def _addVars(x, children): """ @@ -571,7 +555,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 From 288fc47a925a95a4f72110fc445b0e69fe124706 Mon Sep 17 00:00:00 2001 From: PritishWadhwa Date: Tue, 10 May 2022 11:25:31 +0530 Subject: [PATCH 5/6] Issue 166 in SPARQLWrapper solved --- CONTRIBUTORS | 1 + rdflib/plugins/sparql/algebra.py | 6 +++--- test/test_issues/test_issue166.py | 10 ++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 test/test_issues/test_issue166.py 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 332d72797..b83282e75 100644 --- a/rdflib/plugins/sparql/algebra.py +++ b/rdflib/plugins/sparql/algebra.py @@ -429,9 +429,9 @@ def _findVars(x, res): seenVars.add(x) if isinstance(x, CompValue): if x.name == "Bind": - if x not in seenVars: - res.append(x) - seenVars.add(x) + 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: diff --git a/test/test_issues/test_issue166.py b/test/test_issues/test_issue166.py new file mode 100644 index 000000000..bdb8ac9ca --- /dev/null +++ b/test/test_issues/test_issue166.py @@ -0,0 +1,10 @@ +import pytest +from rdflib import Graph +import rdflib + + +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')]) \ No newline at end of file From d2add040f0571508f2ebc169e74dcebd0ae067fa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 May 2022 14:23:40 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- rdflib/plugins/sparql/algebra.py | 6 ++++-- test/test_issues/test_issue166.py | 13 +++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/rdflib/plugins/sparql/algebra.py b/rdflib/plugins/sparql/algebra.py index f33272a73..bbcd886bb 100644 --- a/rdflib/plugins/sparql/algebra.py +++ b/rdflib/plugins/sparql/algebra.py @@ -416,7 +416,8 @@ def _aggs(e, A): aggvar = Variable("__agg_%d__" % len(A)) e["res"] = aggvar return aggvar - + + def _findVars(x, res): """ Find all variables in a tree @@ -425,7 +426,7 @@ def _findVars(x, res): if isinstance(x, Variable): if x not in seenVars: res.append(x) - seenVars.add(x) + seenVars.add(x) if isinstance(x, CompValue): if x.name == "Bind": if x.var not in seenVars: @@ -441,6 +442,7 @@ def _findVars(x, res): seenVars.add(a) return x + def _addVars(x, children): """ find which variables may be bound by this part of the query diff --git a/test/test_issues/test_issue166.py b/test/test_issues/test_issue166.py index bdb8ac9ca..87b860d6d 100644 --- a/test/test_issues/test_issue166.py +++ b/test/test_issues/test_issue166.py @@ -1,10 +1,15 @@ import pytest -from rdflib import Graph + 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')]) \ No newline at end of file + 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'), + ]