-
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.
Fix SPARQL update parsing to handle arbitrary amounts of triples in i…
…nserts pyparsing handling of recursive rules is limited by python's recursion limit and the rule that should handle multiple triples in data inserts is recursive. The consequence of this is that the amount of triples that can be inserted becomes limited by python's recursion limit. To address this the rule has been rewritten to not be recursive. @rchateauneu thanks for the help with rule rewriting.
- Loading branch information
Showing
2 changed files
with
62 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from rdflib import Graph, Literal | ||
from rdflib.term import Node | ||
from rdflib.namespace import Namespace | ||
from rdflib.plugins.sparql.processor import processUpdate | ||
import unittest | ||
import sys | ||
import math | ||
from typing import Set, Tuple | ||
|
||
|
||
def triple_set(graph: Graph) -> Set[Tuple[Node, Node, Node]]: | ||
return set(graph.triples((None, None, None))) | ||
|
||
|
||
class SPARQLParserTests(unittest.TestCase): | ||
def test_insert_recursionlimit(self) -> None: | ||
# These values are experimentally determined | ||
# to cause the RecursionError reported in | ||
# https://github.com/RDFLib/rdflib/issues/1336 | ||
resource_count = math.ceil(sys.getrecursionlimit() / (33 - 3)) | ||
self.do_insert(resource_count) | ||
|
||
def test_insert_large(self) -> None: | ||
self.do_insert(200) | ||
|
||
def do_insert(self, resource_count: int) -> None: | ||
EGV = Namespace("http://example.org/vocab#") | ||
EGI = Namespace("http://example.org/instance#") | ||
prop0, prop1, prop2 = EGV["prop0"], EGV["prop1"], EGV["prop2"] | ||
g0 = Graph() | ||
for index in range(resource_count): | ||
resource = EGI[f"resource{index}"] | ||
g0.add((resource, prop0, Literal(index))) | ||
g0.add((resource, prop1, Literal("example resource"))) | ||
g0.add((resource, prop2, Literal(f"resource #{index}"))) | ||
|
||
g0ntriples = g0.serialize(format="ntriples") | ||
g1 = Graph() | ||
|
||
self.assertNotEqual(triple_set(g0), triple_set(g1)) | ||
|
||
try: | ||
processUpdate(g1, f"INSERT DATA {{ {g0ntriples!s} }}") | ||
except BaseException: | ||
# logging.error("caught", exc_info=True) | ||
raise | ||
|
||
self.assertEqual(triple_set(g0), triple_set(g1)) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
unittest.main() |