You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Martynas Jusevičius <ma...@atomgraph.com> on 2020/11/24 13:27:33 UTC

Help with OPTIONAL

Hi,

despite using SPARQL for years, cases where different implementations
return different results leave me scratching my head. Can someone help
me out with this?

I have a rather simple query:

PREFIX schema: <https://schema.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX sioc: <http://rdfs.org/sioc/ns#>

SELECT * WHERE {
  GRAPH ?docGraph {
    ?doc sioc:has_container <https://localhost:4443/employees/>;
      foaf:primaryTopic ?employee.
    OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
  }
}
ORDER BY ?doc

Dydra returns 9 results:

employee,reportsToEmployee
https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/2/#this,
https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this

Fuseki 3.16.0 returns 9 results:

employee,reportsToEmployee
https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/2/#this,
https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this

So far so good -- the results are identical. Now I append an OPTIONAL
to the end of the query:

PREFIX schema: <https://schema.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX sioc: <http://rdfs.org/sioc/ns#>

SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
  GRAPH ?docGraph {
    ?doc sioc:has_container <https://localhost:4443/employees/>;
      foaf:primaryTopic ?employee.
    OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
  }
  OPTIONAL {
    GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
<http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
  }
}
ORDER BY ?doc

Dydra returns 9 results:

employee,reportsToEmployee,reportsToEmployeeLabel
https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
https://localhost:4443/employees/2/#this,,
https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan

Fuseki 3.16.0 returns 2400+ results:

employee,reportsToEmployee,reportsToEmployeeLabel
https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
Products
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
Products
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
....

Dydra's result is the one I'm going after, and is based on my
understanding of OPTIONAL. But is it actually correct?

And if Dydra is correct, what is Fuseki doing here? I would have
expected the ?reportsToEmployee bindings from the appended OPTIONAL to
be joined against the first result (without OPTIONAL), but that is not
the case?

Thanks.

Martynas

Re: Help with OPTIONAL

Posted by Andy Seaborne <an...@apache.org>.

On 24/11/2020 17:51, Martynas Jusevičius wrote:
> PREFIX  schema: <https://schema.org/>
> PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX  sioc: <http://rdfs.org/sioc/ns#>
> 
> SELECT DISTINCT  ?employee ?reportsToEmployee ?reportsToEmployeeLabel
> WHERE
>    { GRAPH ?docGraph
>        { ?doc  sioc:has_container  <https://localhost:4443/employees/> ;
>                foaf:primaryTopic   ?employee
>          OPTIONAL
>            { ?employee  schema:sponsor  ?reportsToEmployee }
>        }
>      OPTIONAL
>        { GRAPH ?reportsToEmployeeLabelGraph
>            { ?reportsToEmployee
>                        <http://purl.org/dc/terms/title>  ?reportsToEmployeeLabel
>            }
>        }
>    }
> ORDER BY ?doc
> 
> Fuseki --engine=ref: 2417 results
> Fuseki --optimize=off: 2417 results
> Dydra: 9 results
> RDF4J: 2417 results
> 
> 
> PREFIX  schema: <https://schema.org/>
> PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX  sioc: <http://rdfs.org/sioc/ns#>
> 
> SELECT  DISTINCT  ?employee ?reportsToEmployee ?reportsToEmployeeLabel
> WHERE
>    { GRAPH ?docGraph
>        { ?doc  sioc:has_container  <https://localhost:4443/employees/> ;
>                foaf:primaryTopic   ?employee
>          OPTIONAL
>            { ?employee  schema:sponsor  ?reportsToEmployee }
>        }
>      OPTIONAL
[A]

>        { { SELECT  DISTINCT ?reportsToEmployee ?reportsToEmployeeLabel
>            WHERE
>              {   { ?reportsToEmployee <http://purl.org/dc/terms/title>
> ?reportsToEmployeeLabel }

^^^^^^ Default graph.

>                UNION
>                  { GRAPH ?reportsToEmployeeLabelGraph
>                      { ?reportsToEmployee


?reportsToEmployee blocks an optimization of leftjoin strategy of [A].


> <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel }
>                  }
>              }
>          }
>        }
>    }
> ORDER BY ?doc
> 
> Fuseki --engine=ref: 2417 results
> Fuseki --optimize=off: 2417 results
> Dydra: 9 results
> RDF4J: 9 results

The treatment of the default graph is different between Jena and RDf4J.

Try to find out what the cardinalities of the partial patterns are.

	Andy

> 
> On Tue, Nov 24, 2020 at 4:43 PM Andy Seaborne <an...@apache.org> wrote:
>>
>> Try the reference query engine.
>>
>> sparql --engine=ref ...
>>
>> and also without optimization on the the general engine  --optimize=off
>>
>>       Andy
>>
>> On 24/11/2020 14:19, Martynas Jusevičius wrote:
>>> I've just tried a third implementation (RDF4J 3.4.4) which produces
>>> the same result as Fuseki (2419 rows):
>>>
>>> employee,reportsToEmployee,reportsToEmployeeLabel
>>> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
>>> https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
>>> ....
>>>
>>> Which would suggest that Dydra is the outlier here.
>>>
>>> On Tue, Nov 24, 2020 at 2:27 PM Martynas Jusevičius
>>> <ma...@atomgraph.com> wrote:
>>>>
>>>> Hi,
>>>>
>>>> despite using SPARQL for years, cases where different implementations
>>>> return different results leave me scratching my head. Can someone help
>>>> me out with this?
>>>>
>>>> I have a rather simple query:
>>>>
>>>> PREFIX schema: <https://schema.org/>
>>>> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
>>>> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>>>>
>>>> SELECT * WHERE {
>>>>     GRAPH ?docGraph {
>>>>       ?doc sioc:has_container <https://localhost:4443/employees/>;
>>>>         foaf:primaryTopic ?employee.
>>>>       OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>>>>     }
>>>> }
>>>> ORDER BY ?doc
>>>>
>>>> Dydra returns 9 results:
>>>>
>>>> employee,reportsToEmployee
>>>> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/2/#this,
>>>> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
>>>> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
>>>> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>>>>
>>>> Fuseki 3.16.0 returns 9 results:
>>>>
>>>> employee,reportsToEmployee
>>>> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/2/#this,
>>>> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
>>>> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
>>>> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
>>>> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>>>>
>>>> So far so good -- the results are identical. Now I append an OPTIONAL
>>>> to the end of the query:
>>>>
>>>> PREFIX schema: <https://schema.org/>
>>>> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
>>>> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>>>>
>>>> SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
>>>>     GRAPH ?docGraph {
>>>>       ?doc sioc:has_container <https://localhost:4443/employees/>;
>>>>         foaf:primaryTopic ?employee.
>>>>       OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>>>>     }
>>>>     OPTIONAL {
>>>>       GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
>>>> <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
>>>>     }
>>>> }
>>>> ORDER BY ?doc
>>>>
>>>> Dydra returns 9 results:
>>>>
>>>> employee,reportsToEmployee,reportsToEmployeeLabel
>>>> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
>>>> https://localhost:4443/employees/2/#this,,
>>>> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
>>>> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
>>>> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
>>>> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
>>>> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
>>>> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
>>>> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan
>>>>
>>>> Fuseki 3.16.0 returns 2400+ results:
>>>>
>>>> employee,reportsToEmployee,reportsToEmployeeLabel
>>>> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
>>>> Products
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
>>>> Products
>>>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
>>>> ....
>>>>
>>>> Dydra's result is the one I'm going after, and is based on my
>>>> understanding of OPTIONAL. But is it actually correct?
>>>>
>>>> And if Dydra is correct, what is Fuseki doing here? I would have
>>>> expected the ?reportsToEmployee bindings from the appended OPTIONAL to
>>>> be joined against the first result (without OPTIONAL), but that is not
>>>> the case?
>>>>
>>>> Thanks.
>>>>
>>>> Martynas

Re: Help with OPTIONAL

Posted by Martynas Jusevičius <ma...@atomgraph.com>.
PREFIX  schema: <https://schema.org/>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  sioc: <http://rdfs.org/sioc/ns#>

SELECT DISTINCT  ?employee ?reportsToEmployee ?reportsToEmployeeLabel
WHERE
  { GRAPH ?docGraph
      { ?doc  sioc:has_container  <https://localhost:4443/employees/> ;
              foaf:primaryTopic   ?employee
        OPTIONAL
          { ?employee  schema:sponsor  ?reportsToEmployee }
      }
    OPTIONAL
      { GRAPH ?reportsToEmployeeLabelGraph
          { ?reportsToEmployee
                      <http://purl.org/dc/terms/title>  ?reportsToEmployeeLabel
          }
      }
  }
ORDER BY ?doc

Fuseki --engine=ref: 2417 results
Fuseki --optimize=off: 2417 results
Dydra: 9 results
RDF4J: 2417 results


PREFIX  schema: <https://schema.org/>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  sioc: <http://rdfs.org/sioc/ns#>

SELECT  DISTINCT  ?employee ?reportsToEmployee ?reportsToEmployeeLabel
WHERE
  { GRAPH ?docGraph
      { ?doc  sioc:has_container  <https://localhost:4443/employees/> ;
              foaf:primaryTopic   ?employee
        OPTIONAL
          { ?employee  schema:sponsor  ?reportsToEmployee }
      }
    OPTIONAL
      { { SELECT  DISTINCT ?reportsToEmployee ?reportsToEmployeeLabel
          WHERE
            {   { ?reportsToEmployee <http://purl.org/dc/terms/title>
?reportsToEmployeeLabel }
              UNION
                { GRAPH ?reportsToEmployeeLabelGraph
                    { ?reportsToEmployee
<http://purl.org/dc/terms/title> ?reportsToEmployeeLabel }
                }
            }
        }
      }
  }
ORDER BY ?doc

Fuseki --engine=ref: 2417 results
Fuseki --optimize=off: 2417 results
Dydra: 9 results
RDF4J: 9 results

On Tue, Nov 24, 2020 at 4:43 PM Andy Seaborne <an...@apache.org> wrote:
>
> Try the reference query engine.
>
> sparql --engine=ref ...
>
> and also without optimization on the the general engine  --optimize=off
>
>      Andy
>
> On 24/11/2020 14:19, Martynas Jusevičius wrote:
> > I've just tried a third implementation (RDF4J 3.4.4) which produces
> > the same result as Fuseki (2419 rows):
> >
> > employee,reportsToEmployee,reportsToEmployeeLabel
> > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
> > ....
> >
> > Which would suggest that Dydra is the outlier here.
> >
> > On Tue, Nov 24, 2020 at 2:27 PM Martynas Jusevičius
> > <ma...@atomgraph.com> wrote:
> >>
> >> Hi,
> >>
> >> despite using SPARQL for years, cases where different implementations
> >> return different results leave me scratching my head. Can someone help
> >> me out with this?
> >>
> >> I have a rather simple query:
> >>
> >> PREFIX schema: <https://schema.org/>
> >> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> >> PREFIX sioc: <http://rdfs.org/sioc/ns#>
> >>
> >> SELECT * WHERE {
> >>    GRAPH ?docGraph {
> >>      ?doc sioc:has_container <https://localhost:4443/employees/>;
> >>        foaf:primaryTopic ?employee.
> >>      OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
> >>    }
> >> }
> >> ORDER BY ?doc
> >>
> >> Dydra returns 9 results:
> >>
> >> employee,reportsToEmployee
> >> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/2/#this,
> >> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> >> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> >> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
> >>
> >> Fuseki 3.16.0 returns 9 results:
> >>
> >> employee,reportsToEmployee
> >> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/2/#this,
> >> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> >> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> >> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> >> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
> >>
> >> So far so good -- the results are identical. Now I append an OPTIONAL
> >> to the end of the query:
> >>
> >> PREFIX schema: <https://schema.org/>
> >> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> >> PREFIX sioc: <http://rdfs.org/sioc/ns#>
> >>
> >> SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
> >>    GRAPH ?docGraph {
> >>      ?doc sioc:has_container <https://localhost:4443/employees/>;
> >>        foaf:primaryTopic ?employee.
> >>      OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
> >>    }
> >>    OPTIONAL {
> >>      GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
> >> <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
> >>    }
> >> }
> >> ORDER BY ?doc
> >>
> >> Dydra returns 9 results:
> >>
> >> employee,reportsToEmployee,reportsToEmployeeLabel
> >> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> >> https://localhost:4443/employees/2/#this,,
> >> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
> >> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
> >> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
> >> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
> >> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
> >> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
> >> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan
> >>
> >> Fuseki 3.16.0 returns 2400+ results:
> >>
> >> employee,reportsToEmployee,reportsToEmployeeLabel
> >> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
> >> Products
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
> >> Products
> >> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
> >> ....
> >>
> >> Dydra's result is the one I'm going after, and is based on my
> >> understanding of OPTIONAL. But is it actually correct?
> >>
> >> And if Dydra is correct, what is Fuseki doing here? I would have
> >> expected the ?reportsToEmployee bindings from the appended OPTIONAL to
> >> be joined against the first result (without OPTIONAL), but that is not
> >> the case?
> >>
> >> Thanks.
> >>
> >> Martynas

Re: Help with OPTIONAL

Posted by Andy Seaborne <an...@apache.org>.
Try the reference query engine.

sparql --engine=ref ...

and also without optimization on the the general engine  --optimize=off

     Andy

On 24/11/2020 14:19, Martynas Jusevičius wrote:
> I've just tried a third implementation (RDF4J 3.4.4) which produces
> the same result as Fuseki (2419 rows):
> 
> employee,reportsToEmployee,reportsToEmployeeLabel
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
> ....
> 
> Which would suggest that Dydra is the outlier here.
> 
> On Tue, Nov 24, 2020 at 2:27 PM Martynas Jusevičius
> <ma...@atomgraph.com> wrote:
>>
>> Hi,
>>
>> despite using SPARQL for years, cases where different implementations
>> return different results leave me scratching my head. Can someone help
>> me out with this?
>>
>> I have a rather simple query:
>>
>> PREFIX schema: <https://schema.org/>
>> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
>> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>>
>> SELECT * WHERE {
>>    GRAPH ?docGraph {
>>      ?doc sioc:has_container <https://localhost:4443/employees/>;
>>        foaf:primaryTopic ?employee.
>>      OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>>    }
>> }
>> ORDER BY ?doc
>>
>> Dydra returns 9 results:
>>
>> employee,reportsToEmployee
>> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/2/#this,
>> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
>> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
>> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>>
>> Fuseki 3.16.0 returns 9 results:
>>
>> employee,reportsToEmployee
>> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/2/#this,
>> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
>> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
>> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
>> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>>
>> So far so good -- the results are identical. Now I append an OPTIONAL
>> to the end of the query:
>>
>> PREFIX schema: <https://schema.org/>
>> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
>> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>>
>> SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
>>    GRAPH ?docGraph {
>>      ?doc sioc:has_container <https://localhost:4443/employees/>;
>>        foaf:primaryTopic ?employee.
>>      OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>>    }
>>    OPTIONAL {
>>      GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
>> <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
>>    }
>> }
>> ORDER BY ?doc
>>
>> Dydra returns 9 results:
>>
>> employee,reportsToEmployee,reportsToEmployeeLabel
>> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
>> https://localhost:4443/employees/2/#this,,
>> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
>> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
>> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
>> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
>> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
>> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
>> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan
>>
>> Fuseki 3.16.0 returns 2400+ results:
>>
>> employee,reportsToEmployee,reportsToEmployeeLabel
>> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
>> https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
>> Products
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
>> Products
>> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
>> ....
>>
>> Dydra's result is the one I'm going after, and is based on my
>> understanding of OPTIONAL. But is it actually correct?
>>
>> And if Dydra is correct, what is Fuseki doing here? I would have
>> expected the ?reportsToEmployee bindings from the appended OPTIONAL to
>> be joined against the first result (without OPTIONAL), but that is not
>> the case?
>>
>> Thanks.
>>
>> Martynas

Re: Help with OPTIONAL

Posted by Martynas Jusevičius <ma...@atomgraph.com>.
I've just tried a third implementation (RDF4J 3.4.4) which produces
the same result as Fuseki (2419 rows):

employee,reportsToEmployee,reportsToEmployeeLabel
https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
....

Which would suggest that Dydra is the outlier here.

On Tue, Nov 24, 2020 at 2:27 PM Martynas Jusevičius
<ma...@atomgraph.com> wrote:
>
> Hi,
>
> despite using SPARQL for years, cases where different implementations
> return different results leave me scratching my head. Can someone help
> me out with this?
>
> I have a rather simple query:
>
> PREFIX schema: <https://schema.org/>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>
> SELECT * WHERE {
>   GRAPH ?docGraph {
>     ?doc sioc:has_container <https://localhost:4443/employees/>;
>       foaf:primaryTopic ?employee.
>     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>   }
> }
> ORDER BY ?doc
>
> Dydra returns 9 results:
>
> employee,reportsToEmployee
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/2/#this,
> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>
> Fuseki 3.16.0 returns 9 results:
>
> employee,reportsToEmployee
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/2/#this,
> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>
> So far so good -- the results are identical. Now I append an OPTIONAL
> to the end of the query:
>
> PREFIX schema: <https://schema.org/>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>
> SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
>   GRAPH ?docGraph {
>     ?doc sioc:has_container <https://localhost:4443/employees/>;
>       foaf:primaryTopic ?employee.
>     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>   }
>   OPTIONAL {
>     GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
> <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
>   }
> }
> ORDER BY ?doc
>
> Dydra returns 9 results:
>
> employee,reportsToEmployee,reportsToEmployeeLabel
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/2/#this,,
> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan
>
> Fuseki 3.16.0 returns 2400+ results:
>
> employee,reportsToEmployee,reportsToEmployeeLabel
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
> Products
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
> Products
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
> ....
>
> Dydra's result is the one I'm going after, and is based on my
> understanding of OPTIONAL. But is it actually correct?
>
> And if Dydra is correct, what is Fuseki doing here? I would have
> expected the ?reportsToEmployee bindings from the appended OPTIONAL to
> be joined against the first result (without OPTIONAL), but that is not
> the case?
>
> Thanks.
>
> Martynas

Re: Help with OPTIONAL

Posted by Martynas Jusevičius <ma...@atomgraph.com>.
I removed the graph name projection from the OPTIONAL:

PREFIX  schema: <https://schema.org/>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  sioc: <http://rdfs.org/sioc/ns#>

SELECT  DISTINCT ?employee ?reportsToEmployee ?reportsToEmployeeLabel
WHERE
  { GRAPH ?docGraph
      { ?doc  sioc:has_container  <https://localhost:4443/employees/> ;
              foaf:primaryTopic   ?employee
        OPTIONAL
          { ?employee  schema:sponsor  ?reportsToEmployee }
      }
    OPTIONAL
      { { SELECT  ?reportsToEmployee ?reportsToEmployeeLabel
          WHERE
            {   { ?reportsToEmployee <http://purl.org/dc/terms/title>
?reportsToEmployeeLabel }
              UNION
                { GRAPH ?reportsToEmployeeLabelGraph
                    { ?reportsToEmployee
<http://purl.org/dc/terms/title> ?reportsToEmployeeLabel }
                }
            }
        }
      }
  }
ORDER BY ?doc

RDF4J now produces 9 results (same as Dydra), but Fuseki returns 2417 rows.

On Tue, Nov 24, 2020 at 3:33 PM Martynas Jusevičius
<ma...@atomgraph.com> wrote:
>
> Yes, it looks like this is due to the different treatment of the named graphs.
>
> PREFIX schema: <https://schema.org/>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>
> SELECT (COUNT(?docGraph) AS ?docGraphCount) WHERE {
>   GRAPH ?docGraph {
>     ?doc sioc:has_container <https://localhost:4443/employees/>;
>       foaf:primaryTopic ?employee.
>     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>   }
> }
>
> Dydra: ?docGraphCount = 9
> Fuseki: ?docGraphCount = 9
> RDF4J: ?docGraphCount = 9
>
>
> PREFIX schema: <https://schema.org/>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>
> SELECT (COUNT(?reportsToEmployeeLabelGraph) AS
> ?reportsToEmployeeLabelGraphCount) WHERE {
>   GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
> <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
> }
>
> Dydra: ?reportsToEmployeeLabelGraphCount = 2409
> Fuseki: ?reportsToEmployeeLabelGraphCount = 2409
> RDF4J: ?reportsToEmployeeLabelGraphCount = 2409
>
> On Tue, Nov 24, 2020 at 3:14 PM Lorenz Buehmann
> <bu...@informatik.uni-leipzig.de> wrote:
> >
> > It looks more like the cartesian happens on the graphs - how many graph
> > do you have matching the first and the second part?
> >
> > On 24.11.20 14:27, Martynas Jusevičius wrote:
> > > Hi,
> > >
> > > despite using SPARQL for years, cases where different implementations
> > > return different results leave me scratching my head. Can someone help
> > > me out with this?
> > >
> > > I have a rather simple query:
> > >
> > > PREFIX schema: <https://schema.org/>
> > > PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> > > PREFIX sioc: <http://rdfs.org/sioc/ns#>
> > >
> > > SELECT * WHERE {
> > >   GRAPH ?docGraph {
> > >     ?doc sioc:has_container <https://localhost:4443/employees/>;
> > >       foaf:primaryTopic ?employee.
> > >     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
> > >   }
> > > }
> > > ORDER BY ?doc
> > >
> > > Dydra returns 9 results:
> > >
> > > employee,reportsToEmployee
> > > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/2/#this,
> > > https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> > > https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> > > https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
> > >
> > > Fuseki 3.16.0 returns 9 results:
> > >
> > > employee,reportsToEmployee
> > > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/2/#this,
> > > https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> > > https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> > > https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> > > https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
> > >
> > > So far so good -- the results are identical. Now I append an OPTIONAL
> > > to the end of the query:
> > >
> > > PREFIX schema: <https://schema.org/>
> > > PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> > > PREFIX sioc: <http://rdfs.org/sioc/ns#>
> > >
> > > SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
> > >   GRAPH ?docGraph {
> > >     ?doc sioc:has_container <https://localhost:4443/employees/>;
> > >       foaf:primaryTopic ?employee.
> > >     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
> > >   }
> > >   OPTIONAL {
> > >     GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
> > > <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
> > >   }
> > > }
> > > ORDER BY ?doc
> > >
> > > Dydra returns 9 results:
> > >
> > > employee,reportsToEmployee,reportsToEmployeeLabel
> > > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> > > https://localhost:4443/employees/2/#this,,
> > > https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
> > > https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
> > > https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
> > > https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
> > > https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
> > > https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
> > > https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan
> > >
> > > Fuseki 3.16.0 returns 2400+ results:
> > >
> > > employee,reportsToEmployee,reportsToEmployeeLabel
> > > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
> > > Products
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
> > > Products
> > > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
> > > ....
> > >
> > > Dydra's result is the one I'm going after, and is based on my
> > > understanding of OPTIONAL. But is it actually correct?
> > >
> > > And if Dydra is correct, what is Fuseki doing here? I would have
> > > expected the ?reportsToEmployee bindings from the appended OPTIONAL to
> > > be joined against the first result (without OPTIONAL), but that is not
> > > the case?
> > >
> > > Thanks.
> > >
> > > Martynas

Re: Help with OPTIONAL

Posted by Martynas Jusevičius <ma...@atomgraph.com>.
Yes, it looks like this is due to the different treatment of the named graphs.

PREFIX schema: <https://schema.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX sioc: <http://rdfs.org/sioc/ns#>

SELECT (COUNT(?docGraph) AS ?docGraphCount) WHERE {
  GRAPH ?docGraph {
    ?doc sioc:has_container <https://localhost:4443/employees/>;
      foaf:primaryTopic ?employee.
    OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
  }
}

Dydra: ?docGraphCount = 9
Fuseki: ?docGraphCount = 9
RDF4J: ?docGraphCount = 9


PREFIX schema: <https://schema.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX sioc: <http://rdfs.org/sioc/ns#>

SELECT (COUNT(?reportsToEmployeeLabelGraph) AS
?reportsToEmployeeLabelGraphCount) WHERE {
  GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
<http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
}

Dydra: ?reportsToEmployeeLabelGraphCount = 2409
Fuseki: ?reportsToEmployeeLabelGraphCount = 2409
RDF4J: ?reportsToEmployeeLabelGraphCount = 2409

On Tue, Nov 24, 2020 at 3:14 PM Lorenz Buehmann
<bu...@informatik.uni-leipzig.de> wrote:
>
> It looks more like the cartesian happens on the graphs - how many graph
> do you have matching the first and the second part?
>
> On 24.11.20 14:27, Martynas Jusevičius wrote:
> > Hi,
> >
> > despite using SPARQL for years, cases where different implementations
> > return different results leave me scratching my head. Can someone help
> > me out with this?
> >
> > I have a rather simple query:
> >
> > PREFIX schema: <https://schema.org/>
> > PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> > PREFIX sioc: <http://rdfs.org/sioc/ns#>
> >
> > SELECT * WHERE {
> >   GRAPH ?docGraph {
> >     ?doc sioc:has_container <https://localhost:4443/employees/>;
> >       foaf:primaryTopic ?employee.
> >     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
> >   }
> > }
> > ORDER BY ?doc
> >
> > Dydra returns 9 results:
> >
> > employee,reportsToEmployee
> > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/2/#this,
> > https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> > https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> > https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
> >
> > Fuseki 3.16.0 returns 9 results:
> >
> > employee,reportsToEmployee
> > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/2/#this,
> > https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> > https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> > https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
> >
> > So far so good -- the results are identical. Now I append an OPTIONAL
> > to the end of the query:
> >
> > PREFIX schema: <https://schema.org/>
> > PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> > PREFIX sioc: <http://rdfs.org/sioc/ns#>
> >
> > SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
> >   GRAPH ?docGraph {
> >     ?doc sioc:has_container <https://localhost:4443/employees/>;
> >       foaf:primaryTopic ?employee.
> >     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
> >   }
> >   OPTIONAL {
> >     GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
> > <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
> >   }
> > }
> > ORDER BY ?doc
> >
> > Dydra returns 9 results:
> >
> > employee,reportsToEmployee,reportsToEmployeeLabel
> > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/2/#this,,
> > https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
> > https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
> > https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan
> >
> > Fuseki 3.16.0 returns 2400+ results:
> >
> > employee,reportsToEmployee,reportsToEmployeeLabel
> > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
> > Products
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
> > Products
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
> > ....
> >
> > Dydra's result is the one I'm going after, and is based on my
> > understanding of OPTIONAL. But is it actually correct?
> >
> > And if Dydra is correct, what is Fuseki doing here? I would have
> > expected the ?reportsToEmployee bindings from the appended OPTIONAL to
> > be joined against the first result (without OPTIONAL), but that is not
> > the case?
> >
> > Thanks.
> >
> > Martynas

Re: Help with OPTIONAL

Posted by Lorenz Buehmann <bu...@informatik.uni-leipzig.de>.
It looks more like the cartesian happens on the graphs - how many graph
do you have matching the first and the second part?

On 24.11.20 14:27, Martynas Jusevičius wrote:
> Hi,
>
> despite using SPARQL for years, cases where different implementations
> return different results leave me scratching my head. Can someone help
> me out with this?
>
> I have a rather simple query:
>
> PREFIX schema: <https://schema.org/>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>
> SELECT * WHERE {
>   GRAPH ?docGraph {
>     ?doc sioc:has_container <https://localhost:4443/employees/>;
>       foaf:primaryTopic ?employee.
>     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>   }
> }
> ORDER BY ?doc
>
> Dydra returns 9 results:
>
> employee,reportsToEmployee
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/2/#this,
> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>
> Fuseki 3.16.0 returns 9 results:
>
> employee,reportsToEmployee
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/2/#this,
> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>
> So far so good -- the results are identical. Now I append an OPTIONAL
> to the end of the query:
>
> PREFIX schema: <https://schema.org/>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>
> SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
>   GRAPH ?docGraph {
>     ?doc sioc:has_container <https://localhost:4443/employees/>;
>       foaf:primaryTopic ?employee.
>     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>   }
>   OPTIONAL {
>     GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
> <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
>   }
> }
> ORDER BY ?doc
>
> Dydra returns 9 results:
>
> employee,reportsToEmployee,reportsToEmployeeLabel
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/2/#this,,
> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan
>
> Fuseki 3.16.0 returns 2400+ results:
>
> employee,reportsToEmployee,reportsToEmployeeLabel
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
> Products
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
> Products
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
> ....
>
> Dydra's result is the one I'm going after, and is based on my
> understanding of OPTIONAL. But is it actually correct?
>
> And if Dydra is correct, what is Fuseki doing here? I would have
> expected the ?reportsToEmployee bindings from the appended OPTIONAL to
> be joined against the first result (without OPTIONAL), but that is not
> the case?
>
> Thanks.
>
> Martynas

Re: Help with OPTIONAL

Posted by Martynas Jusevičius <ma...@atomgraph.com>.
Hi Mikael,

in the actual use case, we use a UNION inside the OPTIONAL:

SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
  GRAPH ?docGraph {
    ?doc sioc:has_container <https://localhost:4443/employees/>;
      foaf:primaryTopic ?employee.
    OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
  }
  OPTIONAL {
    { ?reportsToEmployee <http://purl.org/dc/terms/title>
?reportsToEmployeeLabel. }
    UNION
    { GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
<http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. } }
  }
}
ORDER BY ?doc

which would not be equivalent to

SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
  GRAPH ?docGraph {
    ?doc sioc:has_container <https://localhost:4443/employees/>;
      foaf:primaryTopic ?employee.
    OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
    OPTIONAL {
      { ?reportsToEmployee <http://purl.org/dc/terms/title>
?reportsToEmployeeLabel. }
      UNION
      { GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
<http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. } }
    }
  }
}
ORDER BY ?doc

And before using a workaround, I'd like to understand first which of
the implementations is correct.

On Tue, Nov 24, 2020 at 2:57 PM Mikael Pesonen
<mi...@lingsoft.fi> wrote:
>
>
> Hi Martynas,
>
> I don't recall if nested graphs are allowed, but if so, this could work
> better
>
> SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
>    GRAPH ?docGraph {
>      ?doc sioc:has_container<https://localhost:4443/employees/>;
>        foaf:primaryTopic ?employee.
>      OPTIONAL { ?employee schema:sponsor ?reportsToEmployee.
>        OPTIONAL {
>           GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
> <http://purl.org/dc/terms/title>  ?reportsToEmployeeLabel. }
>          }
>       }
>    }
>
> }
> ORDER BY ?doc
>
>
> So second OPTIONAL inside the first one. That makes sure second OPTIONAL
> isn't left unbinded.
>
>
>
>
> On 24/11/2020 15.27, Martynas Jusevičius wrote:
> > Hi,
> >
> > despite using SPARQL for years, cases where different implementations
> > return different results leave me scratching my head. Can someone help
> > me out with this?
> >
> > I have a rather simple query:
> >
> > PREFIX schema: <https://schema.org/>
> > PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> > PREFIX sioc: <http://rdfs.org/sioc/ns#>
> >
> > SELECT * WHERE {
> >    GRAPH ?docGraph {
> >      ?doc sioc:has_container <https://localhost:4443/employees/>;
> >        foaf:primaryTopic ?employee.
> >      OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
> >    }
> > }
> > ORDER BY ?doc
> >
> > Dydra returns 9 results:
> >
> > employee,reportsToEmployee
> > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/2/#this,
> > https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> > https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> > https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
> >
> > Fuseki 3.16.0 returns 9 results:
> >
> > employee,reportsToEmployee
> > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/2/#this,
> > https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> > https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> > https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> > https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
> >
> > So far so good -- the results are identical. Now I append an OPTIONAL
> > to the end of the query:
> >
> > PREFIX schema: <https://schema.org/>
> > PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> > PREFIX sioc: <http://rdfs.org/sioc/ns#>
> >
> > SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
> >    GRAPH ?docGraph {
> >      ?doc sioc:has_container <https://localhost:4443/employees/>;
> >        foaf:primaryTopic ?employee.
> >      OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
> >    }
> >    OPTIONAL {
> >      GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
> > <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
> >    }
> > }
> > ORDER BY ?doc
> >
> > Dydra returns 9 results:
> >
> > employee,reportsToEmployee,reportsToEmployeeLabel
> > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/2/#this,,
> > https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
> > https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
> > https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan
> >
> > Fuseki 3.16.0 returns 2400+ results:
> >
> > employee,reportsToEmployee,reportsToEmployeeLabel
> > https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> > https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
> > Products
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
> > Products
> > https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
> > ....
> >
> > Dydra's result is the one I'm going after, and is based on my
> > understanding of OPTIONAL. But is it actually correct?
> >
> > And if Dydra is correct, what is Fuseki doing here? I would have
> > expected the ?reportsToEmployee bindings from the appended OPTIONAL to
> > be joined against the first result (without OPTIONAL), but that is not
> > the case?
> >
> > Thanks.
> >
> > Martynas
>
> --
> Lingsoft - 30 years of Leading Language Management
>
> www.lingsoft.fi
>
> Speech Applications - Language Management - Translation - Reader's and Writer's Tools - Text Tools - E-books and M-books
>
> Mikael Pesonen
> System Engineer
>
> e-mail: mikael.pesonen@lingsoft.fi
> Tel. +358 2 279 3300
>
> Time zone: GMT+2
>
> Helsinki Office
> Eteläranta 10
> FI-00130 Helsinki
> FINLAND
>
> Turku Office
> Kauppiaskatu 5 A
> FI-20100 Turku
> FINLAND
>

Re: Help with OPTIONAL

Posted by Mikael Pesonen <mi...@lingsoft.fi>.
Hi Martynas,

I don't recall if nested graphs are allowed, but if so, this could work 
better

SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
   GRAPH ?docGraph {
     ?doc sioc:has_container<https://localhost:4443/employees/>;
       foaf:primaryTopic ?employee.
     OPTIONAL { ?employee schema:sponsor ?reportsToEmployee.
       OPTIONAL {
          GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
<http://purl.org/dc/terms/title>  ?reportsToEmployeeLabel. }
         }
      }
   }
   
}
ORDER BY ?doc


So second OPTIONAL inside the first one. That makes sure second OPTIONAL 
isn't left unbinded.




On 24/11/2020 15.27, Martynas Jusevičius wrote:
> Hi,
>
> despite using SPARQL for years, cases where different implementations
> return different results leave me scratching my head. Can someone help
> me out with this?
>
> I have a rather simple query:
>
> PREFIX schema: <https://schema.org/>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>
> SELECT * WHERE {
>    GRAPH ?docGraph {
>      ?doc sioc:has_container <https://localhost:4443/employees/>;
>        foaf:primaryTopic ?employee.
>      OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>    }
> }
> ORDER BY ?doc
>
> Dydra returns 9 results:
>
> employee,reportsToEmployee
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/2/#this,
> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>
> Fuseki 3.16.0 returns 9 results:
>
> employee,reportsToEmployee
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/2/#this,
> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this
> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this
> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this
>
> So far so good -- the results are identical. Now I append an OPTIONAL
> to the end of the query:
>
> PREFIX schema: <https://schema.org/>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX sioc: <http://rdfs.org/sioc/ns#>
>
> SELECT ?employee ?reportsToEmployee ?reportsToEmployeeLabel WHERE {
>    GRAPH ?docGraph {
>      ?doc sioc:has_container <https://localhost:4443/employees/>;
>        foaf:primaryTopic ?employee.
>      OPTIONAL { ?employee schema:sponsor ?reportsToEmployee. }
>    }
>    OPTIONAL {
>      GRAPH ?reportsToEmployeeLabelGraph { ?reportsToEmployee
> <http://purl.org/dc/terms/title> ?reportsToEmployeeLabel. }
>    }
> }
> ORDER BY ?doc
>
> Dydra returns 9 results:
>
> employee,reportsToEmployee,reportsToEmployeeLabel
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/2/#this,,
> https://localhost:4443/employees/3/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/4/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/5/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/6/#this,https://localhost:4443/employees/5/#this,Buchanan
> https://localhost:4443/employees/7/#this,https://localhost:4443/employees/5/#this,Buchanan
> https://localhost:4443/employees/8/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/9/#this,https://localhost:4443/employees/5/#this,Buchanan
>
> Fuseki 3.16.0 returns 2400+ results:
>
> employee,reportsToEmployee,reportsToEmployeeLabel
> https://localhost:4443/employees/1/#this,https://localhost:4443/employees/2/#this,Fuller
> https://localhost:4443/employees/2/#this,https://localhost:4443/,Root
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/,Categories
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/,Beverages
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/1/#this,Beverages
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/,Condiments
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/2/#this,Condiments
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/,Confections
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/3/#this,Confections
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/,Dairy
> Products
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/4/#this,Dairy
> Products
> https://localhost:4443/employees/2/#this,https://localhost:4443/categories/5/,Grains/Cereals
> ....
>
> Dydra's result is the one I'm going after, and is based on my
> understanding of OPTIONAL. But is it actually correct?
>
> And if Dydra is correct, what is Fuseki doing here? I would have
> expected the ?reportsToEmployee bindings from the appended OPTIONAL to
> be joined against the first result (without OPTIONAL), but that is not
> the case?
>
> Thanks.
>
> Martynas

-- 
Lingsoft - 30 years of Leading Language Management

www.lingsoft.fi

Speech Applications - Language Management - Translation - Reader's and Writer's Tools - Text Tools - E-books and M-books

Mikael Pesonen
System Engineer

e-mail: mikael.pesonen@lingsoft.fi
Tel. +358 2 279 3300

Time zone: GMT+2

Helsinki Office
Eteläranta 10
FI-00130 Helsinki
FINLAND

Turku Office
Kauppiaskatu 5 A
FI-20100 Turku
FINLAND