Search code examples
aggregateblazegraph

Aggregate query causes error on Blazegraph but not Sesame


I'm just working on moving an application from Sesame to Blazegraph and have a problem with the following query. It works OK on Sesame, but Blazegraph reports an error:

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

The Blazegraph error is:

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

This is an Ubuntu install of Blazegraph:

Build Version=2.0.0  
Build Git Commit=516e5a7014af1fbe378772c02d51ba1046f53e08

How can I fix this problem?


Solution

  • According to 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.

    It might be, that Sesame implementation extends SPARQL 1.1 specification to allow variables in projection clause, which are not mentioned in group by.

    But generally you need to specify all variables both in select and group by clauses:

    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
    

    or use sample aggregate:

    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