Page MenuHomePhabricator

unexpected SPARQL query result
Open, MediumPublic

Description

I found a cursed query:

My initial query finding 129112 results (where only 1 row was expected):

SELECT (COUNT(?item) AS ?count) ?type WHERE {VALUES (?item) { (wd:Q1018063) (wd:Q1026869) } .?item wdt:P31 ?type .}GROUP BY ?type

Try it!

The simplified demonstration of the problem suggested by @Lucas_Werkmeister_WMDE:

SELECT ?type (COUNT(*) AS ?count) WHERE {
  VALUES ?item { <a> <b> }
  ?item wdt:P1622 ?type.
}
GROUP BY ?type

Try it! Current results:

typecount
wd:Q145651990
wd:Q145651990
wd:Q131967500
wd:Q131967500

The problem doesn’t occur with only one value in the VALUES clause (wild guess: an optimizer turns that into a BIND(), avoiding the bug), but as soon as there is more than one value (note that <a> and <b> aren’t values we actually have any triples for), the query service seems to behave as if it first takes all the possible values of ?type (independent of ?item) and then counts the ?item wdt:P1622 ?type triples for each of of those ?types and each ?item from the VALUES clause. Compare:

SELECT ?type (COUNT(*) AS ?count) WHERE {
  VALUES ?item { wd:Q183 wd:Q142 wd:Q145 } # Germany, France, United Kingdom
  ?item wdt:P1622 ?type. # driving side
}
GROUP BY ?type

Try it! Current results:

typecount
wd:Q145651991
wd:Q145651991
wd:Q145651990
wd:Q131967500
wd:Q131967500
wd:Q131967501

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript
Gehel triaged this task as Medium priority.Sep 15 2020, 7:50 AM

Hypothesis: This bug looks related but a bit different from what I described in T321855. Looking at the query plan we see FastRangeCountOp and DistinctTermScanOp, this indicates that the Blazegraph optimizer is incorrectly trying to apply the Group by and count optimization. I described how it works in the query optimization guide. This should only fire when there is a single BGP in the body of the query. The optimizer fails to pick up on that there is supposed to be a join happening, and so this specific optimization should not be applied.

For a workaround we simply need to prevent the optimization from firing, we can do this by adding an always-true filter, or an equivalent NOOP instruction:

SELECT (COUNT(?item) AS ?count) ?type WHERE {
  VALUES ?item {
    wd:Q1018063
    wd:Q1026869
  }
  ?item wdt:P31 ?type.
  filter("true"^^xsd:boolean = "true"^^xsd:boolean)
}
GROUP BY ?type