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: [email protected]
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

Reply via email to