openlink / virtuoso-opensource

Virtuoso is a high-performance and scalable Multi-Model RDBMS, Data Integration Middleware, Linked Data Deployment, and HTTP Application Server Platform
https://vos.openlinksw.com
Other
867 stars 210 forks source link

CONSTRUCT WHERE executes even if invalid #657

Open jakubklimek opened 7 years ago

jakubklimek commented 7 years ago

This query is invalid, but Virtuoso executes it:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dcterms: <http://purl.org/dc/terms/>

CONSTRUCT WHERE {
   <https://nkod.opendata.cz/zdroj/datová-sada/66268584/11> ?p ?o .
OPTIONAL {
   <https://nkod.opendata.cz/zdroj/datová-sada/66268584/11> dcterms:format ?format .
   ?format skos:prefLabel ?formatLabel . 
}
}

It is invalid because it is syntactic sugar for

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dcterms: <http://purl.org/dc/terms/>

CONSTRUCT {
   <https://nkod.opendata.cz/zdroj/datová-sada/66268584/11> ?p ?o .
OPTIONAL {
   <https://nkod.opendata.cz/zdroj/datová-sada/66268584/11> dcterms:format ?format .
   ?format skos:prefLabel ?formatLabel . 
}
WHERE 
{
   <https://nkod.opendata.cz/zdroj/datová-sada/66268584/11> ?p ?o .
OPTIONAL {
   <https://nkod.opendata.cz/zdroj/datová-sada/66268584/11> dcterms:format ?format .
   ?format skos:prefLabel ?formatLabel . 
}
}

and it is invalid to have OPTIONAL in CONSTRUCT clause.

IvanMikhailov commented 7 years ago

Yes it executes even if invalid in SPARQL 1.1, using triples gathered from WHERE clause but ignoring structures like extra {..} and OPTIONAL. I can't promise that this behavior will become valid in SPARQL 1.2 but if enough people will like it then chances are high.

One of popular purposes of CONSTRUCT WHERE is to get most of source data of a simple SELECT query, e.g., to reproduce the case on other box, by replacing WITH SELECT xxx WHERE with WITH CONSTRUCT WHERE. If OPTIONAL {} adds no bindings to some solution then construct templates from OPTIONAL get NULL fields in that solution so no missing "optional" triples appear in the output, quite straightforward.