问题
Please refer to Factforge Endpoint to execute this query. The subquery doesn't return any results. ?myVar
will be projected out to the containing query, and then joined with the triple pattern ?myVar ?p ?o.
. But as there are no results from the inner select, the join should result in nothing. However, this is not the case when executing the query. Isn't this a bug?
SELECT
?myVar ?p ?o
WHERE
{
{
SELECT ?myVar
WHERE {
?myVar <http://www.example.com/arbitraryNonExistent> ?xx.
}
GROUP BY ?myVar
}
?myVar ?p ?o.
}
LIMIT 10
回答1:
It is the expected behaviour. According to https://www.w3.org/TR/sparql11-query/#aggregateAlgebra if there is a GROUP BY:
Group(exprlist, Ω) = { ... | μ in Ω }
and we have no matches, then Ω is empty, so:
Group(exprlist, {}) = {}
The effect is that the subquery returns a single solution where ?myVar is unbound and the join with the next statement pattern matches everything for ?myVar. At the end you are getting a lot of solutions for the whole query.
There is even a W3C SPARQL conformance testcase covering the exact scenario:
https://www.w3.org/2009/sparql/docs/tests/data-sparql11/aggregates/agg-empty-group.rq
https://www.w3.org/2009/sparql/docs/tests/data-sparql11/aggregates/agg-empty-group.srx
And also an old discussion at http://answers.semanticweb.com/questions/17410/semantics-of-sparql-aggregates.
来源:https://stackoverflow.com/questions/50116255/wrong-query-evaluation-with-aggregation-subquery