Skip to content

SPARQL query parsing fails when the query has nested SERVICE calls #3082

@MathiasVDA

Description

@MathiasVDA

Hello all,

I'm trying to create a SPARQL syntax checker to be used as github action. I'm running in some trouble for more advanced queries. Below I use a reproducible simple example.

Considering this SPARQL query:

PREFIX ex: <http://example.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

CONSTRUCT {
    ?subject ex:hasInfo ?info .
    ?subject ex:hasDetail ?detail .
}
WHERE {
    SERVICE <http://example.org/endpoint1> {
        ?subject rdfs:label ?label .
        ?subject ex:hasIdentifier ?id .
        
        SERVICE <http://example.org/endpoint2> {
            ?subject ex:info ?info .
            OPTIONAL {
                ?subject ex:detail ?detail .
            }
        }
    }
}

And this python script:

import os
import sys
from pathlib import Path
from rdflib.plugins.sparql import parser

def validate_sparql(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        query = f.read()
    
    try:
        # Parse the query to check syntax
        parser.parseQuery(query)
        print(f"✅ {file_path} is valid")
        return True
    except Exception as e:
        print(f"❌ {file_path} has syntax error: {str(e)}")
        print(f"::error file={file_path}::SPARQL syntax error: {str(e)}")
        return False

def main():
    directory = Path('.')
    
    if not directory.exists():
        print(f"Error: Directory {directory} does not exist")
        return 1
    
    all_valid = True
    
    for file_path in directory.glob('*.rq'):
        print(f"Validating {file_path}")
        is_valid = validate_sparql(file_path)
        if not is_valid:
            all_valid = False
    
    return 0 if all_valid else 1

if __name__ == "__main__":
    sys.exit(main())

The script will report a syntax error "maximum recursion depth exceeded" although there is no error. I believe there is an issue with the parser.parseQuery(query) line not being able to handle the nested SERVICE calls.

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