The DAWG test query two-nested-opt.rq
is like this:
PREFIX : <http://example/>
SELECT *
{
:x1 :p ?v .
OPTIONAL
{
:x3 :q ?w .
OPTIONAL { :x2 :p ?v }
}
}
The test data is:
@prefix : <http://example/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:x1 :p "1"^^xsd:integer .
:x2 :p "2"^^xsd:integer .
:x3 :q "3"^^xsd:integer .
:x3 :q "4"^^xsd:integer .
If I run the query, the expected results is just one record with ?v=1 (for short). Don't really understand it since commenting the second optional, the result is two records:
Some possible explanations I found mention the second optional is a no-op since the ?v
bindings don't match between second optional and the main bgp. But don't get how that explains it. Shouldn't the results of first optional be always included in the solution whatever the result of second optional is?
This test case is about evaluation being functional (also called bottom-up) and how early (inner-most) setting of ?v affects the outcome. In the test, the inner most setting of ?v then blocks the first OPTIONAL and the ?w = 3 and ?w = 4 are not results.
It is the case of the general situation of having an outer ?v (left side of the OPTIONAL), then an OPTIONAL part that does not mention ?v which itself has an OPTIONAL using ?v.
If the query is thought of top-down, the answers would be different.
Evaluation is:
:x3 :q ?w leftjoin :x2 :p ?v
Two rows:
?w = 3; ?v = 2
?w = 4; ?v = 2
:x1 :p ?v
?v = 1
Now left join "?v = 1" with "?w = 3; ?v = 2" -- The OPTIONAL does not join so the result is one row, "?v = 1" and no ?w binding.
If the ":x2 :p ?v" is omitted, the first
:x3 :q ?w
Two rows:
?w = 3
?w = 4
both of which join with ?v = 1, giving two rows of ?v and ?w.