Search code examples
sparqlwikidata

Path matching inside a VALUES clause


I'm trying to perform path matching inside a VALUES clause in sparql in order to match all instances and subclasses of both battles and sieges in wikidata. The following request repeatedly times out.

SELECT DISTINCT ?battle ?battleLabel WHERE {
  {
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    VALUES ?type {wd:Q178561 wd:Q188055} ?battle (wdt:P31/wdt:P279*) ?type .
    ?battle rdfs:label ?queryByTitle.
    FILTER(REGEX(?queryByTitle, "saratoga", "i"))
  }
}

Solution

  • It seems that VALUES, esp. in conjunction with /, confuses the Blazegraph's query optimizer in that case.

    Use UNION instead of VALUES:

    SELECT DISTINCT ?battle ?battleLabel WHERE {
        { ?battle wdt:P31/wdt:P279* wd:Q178561 }
        UNION
        { ?battle wdt:P31/wdt:P279* wd:Q188055 }
        ?battle rdfs:label ?queryByTitle.
        FILTER(REGEX(?queryByTitle, "saratoga", "i"))
        SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
    }
    

    Alternatively, disable the optimizer and specify explicit order:

    SELECT DISTINCT ?battle ?battleLabel WHERE {
        hint:Query hint:optimizer "None" .
        VALUES ?type {wd:Q178561 wd:Q188055}
        ?subtype wdt:P279* ?type .
        ?battle wdt:P31 ?subtype .
        ?battle rdfs:label ?queryByTitle.
        FILTER(REGEX(?queryByTitle, "saratoga", "i"))
        SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
    }