Skip to content

Runtime error querying a dataset using multiple graph variables #3102

@jaw111

Description

@jaw111

I run into RuntimeError: Set changed size during iteration when iterating over results from a query and was able to reduce the problem down to the following example code that reproduces the error:

import rdflib

# create a dataset
dataset = rdflib.Dataset()

# create a graph
g = dataset.graph(identifier="urn:example:graph")

sparql_query = """
SELECT *
WHERE {
  GRAPH ?g_1 { }
  GRAPH ?g_2 { }
}"""

results = dataset.query(sparql_query)

print(len(results))

The traceback is:

Traceback (most recent call last):
  File "C:\Users\XXXX\query_test.py", line 19, in <module>
    print(len(results))
          ^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\rdflib\query.py", line 347, in __len__
    return len(self.bindings)
               ^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\rdflib\query.py", line 242, in bindings
    self._bindings += list(self._genbindings)
                      ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\rdflib\plugins\sparql\evaluate.py", line 562, in <genexpr>
    return (row.project(project.PV) for row in res)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\rdflib\plugins\sparql\evaluate.py", line 142, in evalLazyJoin
    for a in evalPart(ctx, join.p1):
  File "C:\Python312\Lib\site-packages\rdflib\plugins\sparql\evaluate.py", line 225, in evalGraph
    for graph in ctx.dataset.contexts():
  File "C:\Python312\Lib\site-packages\rdflib\graph.py", line 2529, in contexts
    for c in super(Dataset, self).contexts(triple):
  File "C:\Python312\Lib\site-packages\rdflib\graph.py", line 2154, in contexts
    for context in self.store.contexts(triple):
  File "C:\Python312\Lib\site-packages\rdflib\plugins\stores\memory.py", line 565, in <genexpr>
    return (context for context in self.__all_contexts)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Set changed size during iteration

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions