简体   繁体   中英

ُEXISTS and join with distinct values in two SPARQL queries return different results while they should do the same thing?

When running the following two queries on DBpedia the result is different. First query gives 68 while the second gives 42. The only difference is the line

filter(exists {[] <http://dbpedia.org/ontology/nationality> ?o.}) 

replaced by join to ensure that the object of dbpo:country is in dbpo:nationality

{select distinct ?o { [] <http://dbpedia.org/ontology/nationality> ?o.}} 

First Query:

select count(*){
 {select distinct ?s ?o
 { ?o1 <http://dbpedia.org/ontology/successor> ?s . 
 ?o1 <http://dbpedia.org/ontology/governor> ?o2 . 
 ?o2 <http://dbpedia.org/ontology/country> ?o  
filter(exists {[] <http://dbpedia.org/ontology/nationality> ?o.})
filter(exists {?s <http://dbpedia.org/ontology/nationality> []})
}}.
}

Second Query:

select count(*){
 {select distinct ?s ?o
 { ?o1 <http://dbpedia.org/ontology/successor> ?s . 
 ?o1 <http://dbpedia.org/ontology/governor> ?o2 . 
 ?o2 <http://dbpedia.org/ontology/country> ?o  
{select distinct ?o { [] <http://dbpedia.org/ontology/nationality> ?o.}} 
filter(exists {?s <http://dbpedia.org/ontology/nationality> []})
}}.
}

The result of the first query seems to be the correct one.

You've got a DISTINCT in the subquery within the second full query, which is causing some results not to be carried through to the final result set.

Note the result of this query, which drops that keyword from the subquery, matches your first, ie, 68 --

select count(*)
  { { select distinct ?s ?o
        { ?o1 <http://dbpedia.org/ontology/successor> ?s . 
          ?o1 <http://dbpedia.org/ontology/governor> ?o2 . 
          ?o2 <http://dbpedia.org/ontology/country> ?o  
          { select ?o { [] <http://dbpedia.org/ontology/nationality> ?o. } } 
          filter ( exists { ?s <http://dbpedia.org/ontology/nationality> [] } )
  } } }

I can't spare the time to investigate which result rows from the first and third queries are not found in the second, but I imagine that if you dig further into the descriptions of all these ?s and ?o , you will be able to find the answer.

A key hint — SPARQL queries are evaluated from inside-out (also described as from bottom-up, but this is confusing because it's not the literal bottom, but the lowest sub-query). That means that select?o { [] <http://dbpedia.org/ontology/nationality>?o. } select?o { [] <http://dbpedia.org/ontology/nationality>?o. } (or select distinct?o { [] <http://dbpedia.org/ontology/nationality>?o. } ) is evaluated before the rest of the query -- while the filter clauses are evaluated after the main select .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM