-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing use of `SELECT *` in sub-select within `SELECT *` parent query as discovered in #1722. Now when an instance of `SELECT *` is encountered, the query tree/plan builder now correctly considers the projected variables of any sub-select statements when deciding which variables should be projected out. Fixes <#1722>.
- Loading branch information
1 parent
fbb8279
commit c5c16df
Showing
3 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
from rdflib import RDFS, Graph, Literal, URIRef | ||
|
||
_graph_with_label = Graph() | ||
_graph_with_label.add( | ||
(URIRef("http://example.com/something"), RDFS.label, Literal("Some label")) | ||
) | ||
|
||
|
||
def test_select_star_sub_select(): | ||
""" | ||
This tests the fix for a bug which returned no results when using `SELECT *` in the | ||
parent of a sub-select using `SELECT *`. | ||
""" | ||
results = list( | ||
_graph_with_label.query( | ||
""" | ||
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
SELECT * | ||
WHERE { | ||
{ | ||
SELECT * | ||
WHERE { | ||
[] rdfs:label ?label. | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
) | ||
|
||
assert len(results) == 1 | ||
assert results[0].asdict() == {"label": Literal("Some label")} | ||
|
||
|
||
def test_select_star_multiple_sub_select_star(): | ||
""" | ||
Ensure that we can define select * in multiple sub-selects and still select * (all) | ||
of the variables out in the parent. | ||
""" | ||
results = list( | ||
_graph_with_label.query( | ||
""" | ||
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
SELECT * | ||
WHERE { | ||
{ | ||
SELECT * | ||
WHERE { | ||
[] rdfs:label ?label. | ||
} | ||
} | ||
{ | ||
SELECT * | ||
WHERE { | ||
[] rdfs:label ?label2. | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
) | ||
|
||
assert len(results) == 1 | ||
assert results[0].asdict() == { | ||
"label": Literal("Some label"), | ||
"label2": Literal("Some label"), | ||
} | ||
|
||
|
||
def test_select_star_multiple_sub_select_mixed_projections(): | ||
""" | ||
Ensure that we can define select * from one sub-select and define | ||
projected variables on another sub-select and still select * out of the parent. | ||
""" | ||
results = list( | ||
_graph_with_label.query( | ||
""" | ||
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
SELECT * | ||
WHERE { | ||
{ | ||
SELECT * | ||
WHERE { | ||
[] rdfs:label ?label. | ||
} | ||
} | ||
{ | ||
SELECT ?label2 | ||
WHERE { | ||
[] rdfs:label ?label2. | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
) | ||
|
||
assert len(results) == 1 | ||
assert results[0].asdict() == { | ||
"label": Literal("Some label"), | ||
"label2": Literal("Some label"), | ||
} | ||
|
||
|
||
def test_select_star_multiple_sub_select_defined_projections(): | ||
""" | ||
Ensure that we can define select * from multiple sub-selects which define | ||
projected variables. | ||
""" | ||
results = list( | ||
_graph_with_label.query( | ||
""" | ||
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
SELECT * | ||
WHERE { | ||
{ | ||
SELECT ?label | ||
WHERE { | ||
[] rdfs:label ?label. | ||
} | ||
} | ||
{ | ||
SELECT ?label2 | ||
WHERE { | ||
[] rdfs:label ?label2. | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
) | ||
|
||
assert len(results) == 1 | ||
assert results[0].asdict() == { | ||
"label": Literal("Some label"), | ||
"label2": Literal("Some label"), | ||
} |