SELECT query using 'GROUP BY' returning unexpected results (application/sparql-results+json)

I am running a SELECT query which is expected to return no results. Using 'application/sparql-results+json' gives unexpected results.

SPARQL Query:

SELECT ?s ?g {
    VALUES ?s { <https://example.com/person:unknown> }
    GRAPH ?g {
        ?s ?p ?o .
    }
}
GROUP BY ?s ?g

Response (application/sparql-results+json):

{
    "head": {
        "vars": [
            "s",
            "g"
        ]
    },
    "results": {
        "bindings": [
            {}
        ]
    }
}
  • The property 'results.bindings' returns an array of a single empty object when an array of length 0 is expected.

If we remove the 'GROUP BY' statement, the query returns expected results.

SPARQL Query:

SELECT ?s ?g {
    VALUES ?s { <https://example.com/person:unknown> }
    GRAPH ?g {
        ?s ?p ?o .
    }
}

Response (application/sparql-results+json):

{
    "head": {
        "vars": [
            "s",
            "g"
        ]
    },
    "results": {
        "bindings": []
    }
}

Hi Neil,

You're right, but this isn't an issue with the JSON serialisation. The issue is that Stardog returns the empty row for a query with GROUP BY where the pattern in WHERE matches no results. It happens with other result formats too.

There's a bit of a confusion in the spec re: what the result should be in that case. For a long time the accepted interpretation was that the result should be the so-called "empty group". A W3C compliance test for that is still online: SPARQL 1.1 Evaluation Test Results

However, after some discussion it was decided that the right result is actually the empty set, you can read a bit more in the RDF4J tracker: SPARQL MAX aggregate should result in type error on empty set · Issue #1978 · eclipse/rdf4j · GitHub

We also have an open ticket to change it but for all practical purpose you can just add HAVING(bound(?s)) to your query to get rid of the empty group row.

Hope it helps,
Pavel