Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bovlb patch 1 #2931

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading