Skip to content

Fix recursion error when parsing nested SERVICE calls#3408

Open
vemonet wants to merge 1 commit intoRDFLib:mainfrom
vemonet:fix-nested-service
Open

Fix recursion error when parsing nested SERVICE calls#3408
vemonet wants to merge 1 commit intoRDFLib:mainfrom
vemonet:fix-nested-service

Conversation

@vemonet
Copy link
Contributor

@vemonet vemonet commented Mar 3, 2026

Summary of changes

This PR fixes #2136 where RDFLib faces infinite recursion loop when a SERVICE call is used in another SERVICE call, which is not uncommon (Wikidata, one of the most popular public triplestore uses SERVICE call to resolve label, so if someone tries to do a service call to wikidata with labels they would face error at parsing time when executing it with rdflib), e.g.

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?item ?pic WHERE {
    SERVICE <https://query.wikidata.org/sparql> {
        ?item wdt:P31 wd:Q146 .
		?item wdt:P18 ?pic
        SERVICE wikibase:label {
            bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
        }
    }
}
  • update the Comp(TokenConverter) class to properly track and parse SERVICE calls instead of re-parsing the entire input which caused RecursionError with nested services
  • add a test for parsing nested SERVICE calls in test_sparql_parser.py
  • in existing test_translate_algebra.py update the xfail for test_other__service1.txt as it now fails due an issue with parsing nested OPTIONAL/SERVICE blocks instead of triggering a RecursionError (it is a different issue that already existed but was never noticed as the recursion error was triggered first)
  • added .poetry to black skip as this was causing error when running black with precommit hook, preventing to commit
error: cannot format rdflib/.poetry/plugins/poetry_plugin_export/exporter.py: Cannot parse for target version Python 3.9: 328:18:             match dependency:

Note fixing nested OPTIONAL/SERVICE blocks would require small changes to plugins/sparql/algebra.py to properly handle OptionalGraphPattern nodes in _AlgebraTranslator class. I can add it to this PR if you want, or create a new PR later

@nicholascar @aucampia

Checklist

  • Checked that there aren't other open pull requests for
    the same change.
  • Checked that all tests and type checking passes.
  • If the change has a potential impact on users of this project:
    • Added or updated tests that fail without the change.
    • Updated relevant documentation to avoid inaccuracies.
    • Considered adding additional documentation.
  • Considered granting push permissions to the PR branch,
    so maintainers can fix minor issues and keep your PR up to date.

* update the `Comp(TokenConverter)` class to properly track and parse SERVICE calls instead of re-parsing the entire input which caused RecursionError with nested SERVICE
* add a test for parsing nested SERVICE call in `test_sparql_parser.py`
* in existing `test_translate_algebra.py` update the xfail for `test_other__service1.txt` as it now fails due an issue with parsing nested OPTIONAL/SERVICE blocks instead of triggering a RecursionError (it is a different issue that already existed)
* added `.poetry` to black skip as this was causing error when running black with precommit hook

fixes RDFLib#2136
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Maximum recursion depth exceeded when parsing a nested SERVICE query

1 participant