The Knowledge Graph Conference Icon
The Knowledge Graph Conference
  • 馃彔Home
  • 馃搮Events
  • 馃懁Members
  • 馃數Announcements
  • 馃數Ask
  • 馃數Ask The Ontologists
  • 馃數Events
  • 馃數Jobs
  • 馃數Promotions
  • 馃數Share
Powered by Tightknit
Ask
Ask

Understanding Variable Scoping in SPARQL Graph Patterns

Avatar of Jeffrey T.Jeffrey T.
路Oct 15, 2020 11:27 PM

SPARQL question: It appears that variables can't cross graph pattern boundaries. This doesn't work (Error 400: Bad Request):

WHERE {
	?s :p1 ?o1 ;
	   :p2 ?o2 .
	?o1 :p3 ?o3 .
	OPTIONAL {
		?o2 :p4 "some literal"
	}
}

i.e., ?o2 seemingly can't cross the OPTIONAL boundary. This does work:

WHERE {
	?s :p1 ?o1 .
	?o1 :p3 ?o3 .
	OPTIONAL {
		?s :p1 ?o2 . 
		?o2 :p4 "some literal" .
	}
}

I had to both remove any reference to ?o2 in the main body of the WHERE clause and repeat the s :p1 part in the OPTIONAL clause. Is this because the basic graph pattern and optional graph pattern are completely separate (as far as variable scoping)?

馃憤1

7 comments

路 Sorted by Oldest
  • Avatar of Matthias S.
    Matthias S.
    路

    I am not sure but I think in your first request you forgot to close the graph parttern, i.e. a dot (.) is missing at the end of your optional triple. Let me know if that helps

  • Avatar of Jeffrey T.
    Jeffrey T.
    路

    Thanks for your reply, Matthias S.. It's supposed to be the case that the period at the end of a single/the final triple is optional. I'll give it a try, though, and report back....

  • Avatar of Matthias S.
    Matthias S.
    路

    "and in a query pattern with multiple triple patterns, the period at the end of final triple is also optional." I did not know about this. I knew that for a single triple pattern, the period is optional. In your case the optional close is part of the entire pattern and not a single pattern isn't it ?

  • Avatar of Jeffrey T.
    Jeffrey T.
    路

    Good question--they seem like distinct triple patterns, but that might be a subtlety that escaped me. I'll let you know.

  • Avatar of Matthias S.
    Matthias S.
    路

    Please do let me know your findings, I am curious. Thank you 馃檪

  • Avatar of Mike Deagen
    Mike Deagen
    路

    Hi Jeff, can you confirm whether the working query uses :p2 instead of :p1 in the optional clause? That is the only logical difference I can see between the two query patterns... (In either case, not sure why the 400 error is returned for the first query, instead of just "0 results" if there are no matching triples)

  • Avatar of Boris Pelakh
    Boris Pelakh
    路

    I don't think the issue here is crossing OPTIONAL boundaries and more a syntax error in the original queries - 400 Error would indicate a malformed query that doesn't parse. Note that in your working query, ?s crosses the boundary, resulting in a join between the mandatory and optional BGPs. Do you get a more detailed message back with your Error 400? Perhaps if you run your query with a curl -v and capture the full output, you might get a hint as to what is actually happening

    馃憤1