File tree Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -59,33 +59,39 @@ def expandTriples(terms: ParseResults) -> List[Any]:
59
59
Expand ; and , syntax for repeat predicates, subjects
60
60
"""
61
61
# import pdb; pdb.set_trace()
62
+ last_subject , last_predicate = None , None # Used for ; and ,
62
63
try :
63
64
res : List [Any ] = []
64
65
if DEBUG :
65
66
print ("Terms" , terms )
66
67
l_ = len (terms )
67
68
for i , t in enumerate (terms ):
68
69
if t == "," :
69
- res .extend ([res [ - 3 ], res [ - 2 ] ])
70
+ res .extend ([last_subject , last_predicate ])
70
71
elif t == ";" :
71
72
if i + 1 == len (terms ) or terms [i + 1 ] == ";" or terms [i + 1 ] == "." :
72
73
continue # this semicolon is spurious
73
- res .append (res [ 0 ] )
74
+ res .append (last_subject )
74
75
elif isinstance (t , list ):
75
76
# BlankNodePropertyList
76
77
# is this bnode the object of previous triples?
77
78
if (len (res ) % 3 ) == 2 :
78
79
res .append (t [0 ])
79
80
# is this a single [] ?
80
81
if len (t ) > 1 :
81
- res += t
82
+ res += t # Don't update last_subject/last_predicate
82
83
# is this bnode the subject of more triples?
83
84
if i + 1 < l_ and terms [i + 1 ] not in ".,;" :
85
+ last_subject , last_predicate = t [0 ], None
84
86
res .append (t [0 ])
85
87
elif isinstance (t , ParseResults ):
86
88
res += t .asList ()
87
89
elif t != "." :
88
90
res .append (t )
91
+ if (len (res ) % 3 ) == 1 :
92
+ last_subject = t
93
+ elif (len (res ) % 3 ) == 2 :
94
+ last_predicate = t
89
95
if DEBUG :
90
96
print (len (res ), t )
91
97
if DEBUG :
Original file line number Diff line number Diff line change 12
12
13
13
import rdflib .plugins .sparql .algebra as algebra
14
14
import rdflib .plugins .sparql .parser as parser
15
- from rdflib import Graph , Literal , URIRef
15
+ from rdflib import Graph , Literal , URIRef , Variable
16
16
from rdflib .plugins .sparql .algebra import translateAlgebra
17
17
from test .data import TEST_DATA_DIR
18
18
@@ -329,3 +329,20 @@ def test_sparql_group_concat():
329
329
g = Graph ()
330
330
q = dict (g .query (query ))
331
331
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"
You can’t perform that action at this time.
0 commit comments