sparqlrdfdbpedialinked-data

What is the logic of the SPARQL CONSTRUCT LIMIT?


What is the logic of LIMIT when using CONSTRUCT queries in SPARQL?

When I use CONSTRUCT to query DBpedia, I find that the results sometimes don't match the LIMIT

Example

SPARQL:

CONSTRUCT {
  ?s a ?sType .
  ?s ?p ?o .
  ?o a ?oType .
}
WHERE {
  GRAPH <http://dbpedia.org> {
    ?s a ?sType .
    ?s ?p ?o .
    ?o a ?oType .
  }
}
LIMIT XXX

When limit = 3, result is incorrect:

<http://dbpedia.org/resource/Cabrini_Medical_Center>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://dbpedia.org/ontology/building> .
<http://dbpedia.org/resource/Cabrini_Medical_Center>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing> .
<http://dbpedia.org/resource/Cabrini_Medical_Center>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/2002/07/owl#Thing> .
<http://dbpedia.org/ontology/building>  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .

When limit = 10, result is incorrect:

<http://dbpedia.org/ontology/locationCountry>   <http://www.w3.org/2000/01/rdf-schema#subPropertyOf>    <http://dbpedia.org/ontology/location> .
<http://dbpedia.org/ontology/locationCountry>   <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://dbpedia.org/resource/Cabrini_Medical_Center>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://dbpedia.org/ontology/building> .
<http://dbpedia.org/ontology/location>  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://dbpedia.org/ontology/locationCity>  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://dbpedia.org/ontology/locationCity>  <http://www.w3.org/2000/01/rdf-schema#subPropertyOf>    <http://dbpedia.org/ontology/location> .
<http://dbpedia.org/resource/Cabrini_Medical_Center>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing> .
<http://dbpedia.org/resource/Cabrini_Medical_Center>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://dbpedia.org/class/yago/Artifact100021939> .
<http://dbpedia.org/ontology/locationCity>  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://dbpedia.org/resource/Cabrini_Medical_Center>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/2002/07/owl#Thing> .
<http://dbpedia.org/ontology/locationCountry>   <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://dbpedia.org/resource/Cabrini_Medical_Center>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://dbpedia.org/class/yago/Building102913152> .
<http://dbpedia.org/ontology/building>  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://dbpedia.org/resource/Cabrini_Medical_Center>    <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>   <http://dbpedia.org/class/yago/Hospital103540595> .

Solution

  • The LIMIT clause applies to the WHERE clause.

    With LIMIT 3, you will get at most 3 results from the WHERE clause. The CONSTRUCT clause will then work with only these 3 results, and produce as many triples as requested in the template.

    Note that a result is not the same as a triple.

    Example

    The WHERE clause has these 3 results, thanks to your LIMIT 3:

    s o sType p oType
    dbr:Cabrini_Medical_Center dbo:building owl:Thing rdf:type rdf:Property
    dbr:Cabrini_Medical_Center dbo:building dbo:building rdf:type rdf:Property
    dbr:Cabrini_Medical_Center dbo:building geo:SpatialThing rdf:type rdf:Property

    These 3 results are derived from 4 triples:

    # ?s a ?sType . / ?s ?p ?o .
    dbr:Cabrini_Medical_Center rdf:type dbo:building .
    dbr:Cabrini_Medical_Center rdf:type owl:Thing .
    dbr:Cabrini_Medical_Center rdf:type geo:SpatialThing .
    
    # ?o a ?oType .
    dbo:building rdf:type rdf:Property .
    

    As your CONSTRUCT template is identical to the patterns in WHERE, these are also the produced triples.