Hello Aldo,

> Isn't there any way that the compiler can detect which variables are
being referenced  in the select projections part?

It detects, but you write "select ?text", not "select ?s ?text" in subqueries.

Like in SQL, Datalog and other similar languages, variables inside
subexpressions are not in the surrounding scope, you should place ?s to
the inner SELECT result-list to refer to it at the upper level.
Similarly, ?text is usable not because it appears in { ?s
rdfs:label ?text } but because it is found after SELECT. It could be,
say,
{ select (?inner_s) as ?s (?inner_text) as ?text
where {?inner_s rdfs:label ?inner_text} }
to provide values for ?s and ?text even if there are no such variables
in subquery at all.

Next version of SPARQL compiler may contain special check for
"namesakes" in different scopes, to keep the author warned.

Best Regards,
Ivan.

On Thu, 2009-10-15 at 05:33 -0300, Aldo Bucchi wrote:
> Hi,
> 
> There is a general caveat when building SPARQL Unions of Subqueries in
> Virtuoso which I believe is unnecessary.
> 
> The following query works fine.
> 
> select distinct ?s ?text where {
>   { ?s rdfs:label ?text } union { ?s rdfs:comment ?text }
> } limit 10
> 
> But assume I want to complicate it a bit by using subqueries ( in this
> case it makes no sense, but there are definitely use cases for this,
> like combing results of complex queries )
> 
> select distinct ?s ?text where {
>   { select ?text where {?s rdfs:label ?text} } union { select ?text
> where {?s rdfs:comment ?text} }
> } limit 10
> 
> The latter query will yield an error:
> 
> 37000 Error SP031: SPARQL compiler: Variable 's' is used in the query
> result set but not assigned
> 
> So, it follows that I need to bind the variables at least once in the
> outer pattern.
> Fine, I can do that:
> 
> select distinct ?s ?text where {
>   ?s ?p ?text .
>   { select ?text where {?s rdfs:label ?text} } union { select ?text
> where {?s rdfs:comment ?text} }
> } limit 10
> 
> And it works ;)
> 
> But, here's the observation: Why do I HAVE to declare the variables outside?
> Isn't there any way that the compiler can detect which variables are
> being referenced  in the select projections part?
> 
> When dealing with complex queries I usually find myself writing dummy
> BGPs, for example:
> 
> ?interestingSubject ?xx1 ?xx2 .
> ?xx3 ?xx4 ?interestingObject .
> { subquery1... } union { subquery2...} union {subquery3...}
> 
> Will enabling this "automatically" introduce ambiguity?
> 
> Regards,
> A
> 
> 


Reply via email to