Skip to content

[Query] Enforce SPARQL 1.1 blank node scope validation across graph patterns #571

Description

@remiceres

Summary

Enforce SPARQL 1.1 blank node scope validation during parsing and semantic analysis. According to W3C SPARQL 1.1 (§5.1.1 & §4.1.4), a blank node label (e.g. _:a) is scoped strictly to a single Basic Graph Pattern (BGP). Reusing the same label across disjoint BGPs (e.g. across UNION, OPTIONAL, GRAPH, or separate pattern blocks) is a syntax/scope violation and must be rejected.

Target Branch

  • Branch: feature/corese-next

Specification & Semantics

# VALID: same label reused within the same BGP
SELECT * WHERE {
    _:a :p ?x .
    _:a :q ?y .
}

# INVALID: same label reused across distinct BGPs
SELECT * WHERE {
    { _:a :p ?x }
    UNION
    { _:a :q ?y }
}

Technical Context & Implementation Notes

  1. BGP Tracking:
    • SparqlAstBuilder.java already defines enterBgp() / exitBgp().
    • In termFromBlankNode(SparqlParser.BlankNodeContext ctx), blank node labels are currently collected globally in blankNodeLabels without tracking their declaring BGP.
  2. Scope Validation:
    • Implement validation (either during AST construction or via a BlankNodeScopeValidationRule in fr.inria.corese.core.next.query.impl.sparql.parser.semantic.rule).
    • Reject with QuerySyntaxException / QueryValidationException when a label appears in more than one distinct BGP.

Targeted W3C SPARQL 1.0 / 1.1 Test Cases

  • syn-bad-UNION-breaks-BGP
  • syn-bad-OPT-breaks-BGP
  • syn-bad-GRAPH-breaks-BGP
  • syn-blabel-cross-union-bad
  • syn-blabel-cross-optional-bad
  • syn-blabel-cross-graph-bad
  • syn-bad-34.rq through syn-bad-38.rq

Verification

  • Unit tests (corese-core):
    ./gradlew test --tests "fr.inria.corese.core.next.query.impl.sparql.parser.SparqlParserValidationTest"
  • W3C conformance (corese-w3c):
    ./gradlew test --tests "fr.inria.corese.w3c.sparql10.Sparql10DynamicTest"

Acceptance Criteria

  • Reusing a blank node label within the same BGP is accepted.
  • Reusing a blank node label across UNION, OPTIONAL, GRAPH, or disjoint BGPs is rejected.
  • The 11 targeted syn-bad-* and syn-blabel-* tests pass in Sparql10DynamicTest.
  • 0 SonarLint issues and cognitive complexity < 15.

Activity

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

Metadata

Metadata

Assignees

Labels

featureNew feature or capabilityw3c-conformanceW3C standards compliance (SPARQL 1.1, Turtle, RDF 1.1)

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions