Skip to content

Commit 7956712

Browse files
Bovlb patch 1 (#2931)
* Update parser.py Fix SPARQL parsing bug * Add test for bug fix * Check imports * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * style * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9c469b5 commit 7956712

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

rdflib/plugins/sparql/parser.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,39 @@ def expandTriples(terms: ParseResults) -> List[Any]:
5959
Expand ; and , syntax for repeat predicates, subjects
6060
"""
6161
# import pdb; pdb.set_trace()
62+
last_subject, last_predicate = None, None # Used for ; and ,
6263
try:
6364
res: List[Any] = []
6465
if DEBUG:
6566
print("Terms", terms)
6667
l_ = len(terms)
6768
for i, t in enumerate(terms):
6869
if t == ",":
69-
res.extend([res[-3], res[-2]])
70+
res.extend([last_subject, last_predicate])
7071
elif t == ";":
7172
if i + 1 == len(terms) or terms[i + 1] == ";" or terms[i + 1] == ".":
7273
continue # this semicolon is spurious
73-
res.append(res[0])
74+
res.append(last_subject)
7475
elif isinstance(t, list):
7576
# BlankNodePropertyList
7677
# is this bnode the object of previous triples?
7778
if (len(res) % 3) == 2:
7879
res.append(t[0])
7980
# is this a single [] ?
8081
if len(t) > 1:
81-
res += t
82+
res += t # Don't update last_subject/last_predicate
8283
# is this bnode the subject of more triples?
8384
if i + 1 < l_ and terms[i + 1] not in ".,;":
85+
last_subject, last_predicate = t[0], None
8486
res.append(t[0])
8587
elif isinstance(t, ParseResults):
8688
res += t.asList()
8789
elif t != ".":
8890
res.append(t)
91+
if (len(res) % 3) == 1:
92+
last_subject = t
93+
elif (len(res) % 3) == 2:
94+
last_predicate = t
8995
if DEBUG:
9096
print(len(res), t)
9197
if DEBUG:

test/test_sparql/test_translate_algebra.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import rdflib.plugins.sparql.algebra as algebra
1414
import rdflib.plugins.sparql.parser as parser
15-
from rdflib import Graph, Literal, URIRef
15+
from rdflib import Graph, Literal, URIRef, Variable
1616
from rdflib.plugins.sparql.algebra import translateAlgebra
1717
from test.data import TEST_DATA_DIR
1818

@@ -329,3 +329,20 @@ def test_sparql_group_concat():
329329
g = Graph()
330330
q = dict(g.query(query))
331331
assert q[URIRef("http://example.org/pred")] == Literal("abc")
332+
333+
334+
def test_sparql_blank_node_comma():
335+
"""Tests if blank nodes separated by commas are correctly parsed"""
336+
337+
query = """
338+
PREFIX : <http://example.org/>
339+
340+
SELECT ?s WHERE {
341+
?s :hasIngredient [:name "chicken"], [:name "butter"] .
342+
} LIMIT 10
343+
"""
344+
345+
parse_results = parser.parseQuery(query)
346+
triples = parse_results[1]["where"].part[0].triples[0]
347+
s_count = sum(1 for i in range(0, len(triples), 3) if triples[i] == Variable("s"))
348+
assert s_count == 2, f"Found ?s as subject {s_count} times, expected 2"

0 commit comments

Comments
 (0)