聚合查询会导致 Blazegraph 出错,但不会导致 Sesame 出错

Aggregate query causes error on Blazegraph but not Sesame

我正在将应用程序从 Sesame 迁移到 Blazegraph,但遇到以下查询问题。在 Sesame 上运行正常,但 Blazegraph 报错:

PREFIX schema: <http://schema.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?otherperson ?name (count(?name) as ?count) WHERE {
    ?article schema:mentions <http://trove.alveo.edu.au/name/8e0fd54e145f0d0643fec64731d488fa> .
    ?article schema:mentions ?otherperson .
    ?article dcterms:title ?articletitle .
    ?otherperson foaf:name ?name .
  filter (<http://trove.alveo.edu.au/name/8e0fd54e145f0d0643fec64731d488fa> != ?otherperson)
} group by ?name
order by desc(?count)
    LIMIT 50

Blazegraph 错误是:

java.util.concurrent.ExecutionException: org.openrdf.query.MalformedQueryException: Bad aggregate

这是 Blazegraph 的 Ubuntu 安装:

Build Version=2.0.0  
Build Git Commit=516e5a7014af1fbe378772c02d51ba1046f53e08

我该如何解决这个问题?

我可以在 Blazegraph 上重现错误。这显然是 group by 的问题。适用于:

group by ?name ?otherperson

根据SPARQL 1.1 specification

In aggregate queries and sub-queries, variables that appear in the query pattern, but are not in the GROUP BY clause, can only be projected or used in select expressions if they are aggregated.

可能是,Sesame 实现扩展了 SPARQL 1.1 规范以允许投影子句中的变量,这些变量在 group by 中没有提到。

但通常您需要在 select 和 group by 子句中指定所有变量:

PREFIX schema: <http://schema.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?otherperson ?name (count(?name) as ?count) WHERE {
    ?article schema:mentions <http://trove.alveo.edu.au/name/8e0fd54e145f0d0643fec64731d488fa> .
    ?article schema:mentions ?otherperson .
    ?article dcterms:title ?articletitle .
    ?otherperson foaf:name ?name .
    filter (<http://trove.alveo.edu.au/name/8e0fd54e145f0d0643fec64731d488fa> != ?otherperson)
}
group by ?otherperson ?name
order by desc(?count)
LIMIT 50

或使用示例聚合:

PREFIX schema: <http://schema.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT (sample(?otherperson) as ?otherperson) ?name (count(?name) as ?count) WHERE {
    ?article schema:mentions <http://trove.alveo.edu.au/name/8e0fd54e145f0d0643fec64731d488fa> .
    ?article schema:mentions ?otherperson .
    ?article dcterms:title ?articletitle .
    ?otherperson foaf:name ?name .
    filter (<http://trove.alveo.edu.au/name/8e0fd54e145f0d0643fec64731d488fa> != ?otherperson)
}
group by ?name
order by desc(?count)
LIMIT 50