Skip to content

Commit

Permalink
Bovlb patch 1 (#2931)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
bovlb and pre-commit-ci[bot] authored Oct 16, 2024
1 parent 9c469b5 commit 7956712
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
12 changes: 9 additions & 3 deletions rdflib/plugins/sparql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,39 @@ def expandTriples(terms: ParseResults) -> List[Any]:
Expand ; and , syntax for repeat predicates, subjects
"""
# import pdb; pdb.set_trace()
last_subject, last_predicate = None, None # Used for ; and ,
try:
res: List[Any] = []
if DEBUG:
print("Terms", terms)
l_ = len(terms)
for i, t in enumerate(terms):
if t == ",":
res.extend([res[-3], res[-2]])
res.extend([last_subject, last_predicate])
elif t == ";":
if i + 1 == len(terms) or terms[i + 1] == ";" or terms[i + 1] == ".":
continue # this semicolon is spurious
res.append(res[0])
res.append(last_subject)
elif isinstance(t, list):
# BlankNodePropertyList
# is this bnode the object of previous triples?
if (len(res) % 3) == 2:
res.append(t[0])
# is this a single [] ?
if len(t) > 1:
res += t
res += t # Don't update last_subject/last_predicate
# is this bnode the subject of more triples?
if i + 1 < l_ and terms[i + 1] not in ".,;":
last_subject, last_predicate = t[0], None
res.append(t[0])
elif isinstance(t, ParseResults):
res += t.asList()
elif t != ".":
res.append(t)
if (len(res) % 3) == 1:
last_subject = t
elif (len(res) % 3) == 2:
last_predicate = t
if DEBUG:
print(len(res), t)
if DEBUG:
Expand Down
19 changes: 18 additions & 1 deletion test/test_sparql/test_translate_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import rdflib.plugins.sparql.algebra as algebra
import rdflib.plugins.sparql.parser as parser
from rdflib import Graph, Literal, URIRef
from rdflib import Graph, Literal, URIRef, Variable
from rdflib.plugins.sparql.algebra import translateAlgebra
from test.data import TEST_DATA_DIR

Expand Down Expand Up @@ -329,3 +329,20 @@ def test_sparql_group_concat():
g = Graph()
q = dict(g.query(query))
assert q[URIRef("http://example.org/pred")] == Literal("abc")


def test_sparql_blank_node_comma():
"""Tests if blank nodes separated by commas are correctly parsed"""

query = """
PREFIX : <http://example.org/>
SELECT ?s WHERE {
?s :hasIngredient [:name "chicken"], [:name "butter"] .
} LIMIT 10
"""

parse_results = parser.parseQuery(query)
triples = parse_results[1]["where"].part[0].triples[0]
s_count = sum(1 for i in range(0, len(triples), 3) if triples[i] == Variable("s"))
assert s_count == 2, f"Found ?s as subject {s_count} times, expected 2"

0 comments on commit 7956712

Please sign in to comment.