维基数据的 Sparql 请求中的错误聚合

Bad aggregate in Sparql request for Wikidata

我试图让所有的人(因为在 wikidata 中引用而著名)活着和他们的职业(串联),但是查询引擎 query editor 否认我无法调试的“语法”问题。你能帮我重新安排请求以使其生效吗?

SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions)
WHERE {
  ?person wdt:P106 ?occupation.
  ?person wdt:P27 wd:Q142. 
  FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
GROUP BY ?person

要解决此问题,您可以将所有 non-aggregated 变量添加到 GROUP BY 子句:

SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions)
WHERE {
  ?person wdt:P106 ?occupation.
  ?person wdt:P27 wd:Q142. 
  FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
GROUP BY ?person ?personLabel ?personDescription

但是,我无法在没有超时的情况下将其发送到 运行。使用查询提示关闭查询优化器并将其四处移动允许查询到 运行:

SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions)
WHERE {
  hint:Query hint:optimizer "None".
  {
    ?person wdt:P27 wd:Q142. 
    FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
  }
  ?person wdt:P106 ?occupation.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
GROUP BY ?person ?personLabel ?personDescription

此时,我们可以看到 ?positions 变量没有任何数据。 这向我暗示维基数据的 Label 变量(例如 ?occupationLabel)的神奇数量可能不适用于聚合。 将聚合拉出到一个新的 top-level 查询中,并在子查询中进行匹配允许正确分配变量,但我仍然无法让它不超时:

SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions) WHERE {
  SELECT ?person ?personLabel ?personDescription ?occupationLabel
  WHERE {
    hint:Query hint:optimizer "None".
    {
      ?person wdt:P27 wd:Q142. 
      FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
    }
    ?person wdt:P106 ?occupation.
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
  }
}
GROUP BY ?person ?personLabel ?personDescription

可能需要进行一些仔细的优化(或部分执行)才能使其完全执行。例如,限制为 1000 (non-deterministic) person-occupation 对将 运行 而不会超时:

SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions) WHERE {
  SELECT ?person ?personLabel ?personDescription ?occupationLabel
  WHERE {
    hint:Query hint:optimizer "None".
    {
      ?person wdt:P27 wd:Q142. 
      FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
    }
    ?person wdt:P106 ?occupation.
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
  }
  LIMIT 1000
}
GROUP BY ?person ?personLabel ?personDescription