Skip to content

PreparedSparqlQuery.substitute_variable() when variable is in WHERE #1837

Description

@ajnelson-nist

Hello,

I have a SPARQL query where I want to substitute a value in its WHERE clause. I tried following the documentation for substitute_variable (rendered docs here for today's release-state), and got a runtime error while unwrap-ing, NotExistingSubstitutedVariable.

I found three enum variants when searching for that symbol (query results here), and they each indicate this:

If a variable present in the given initial substitution is not present in the SELECT part of the query

I admit I'm not familiar with the use case where variable substitution would happen on something in the SELECT clause of a SPARQL query. However, I am comfortable running substitutions on variables in the WHERE clause with other frameworks.

Is there another mechanism available that would let me do so? And, is this the intended design & constraint of substitute_variable? The doc string reads:

Substitute a variable with a given RDF term in the SPARQL query.

From that, I'd guess that anywhere in the query would be a fair target for substitution.

Activity

  1. Tpt commented on Aug 3, 2026

    @Tpt
    Collaborator

    @ajnelson-nist Sorry for the answer lag.

    This is a great question. For now substitution works following https://github.com/w3c-cg/sparql-dev/blob/main/SEP/SEP-0007/sep-0007.md i.e. it crosses SELECT boundaries only when the variable is in the SELECT.

    I have opened #1848 that clarifies it a bit

  2. ajnelson-nist commented on Aug 3, 2026

    @ajnelson-nist
    ContributorAuthor

    Oh! This is a more nuanced issue than I thought.

    It makes sense to me now that substituting in the WHERE clause only works in a subset of all possible SPARQL queries. Thanks for linking that issue.

    The update in 1848 still just takes a by-fiat documentation approach; I don't really understand why I can't touch a variable in the WHERE clause. Is it worth linking to that sparql-dev note file for further explanation?

    I also had to mull over whether I could do a binding at all, but after reading your note I realized I can just "tag" the variable in the SELECT clause. I filed #1850 on top of #1848 to help others understand this. Feedback or revisions are welcome.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions