Following on from the thread in October "Problem with MAX when no result expected" <https://s.apache.org/03Hz> here is an investigation into SPARQL Aggregation, both what the spec says and what ARQ does.

It would be interesting to get reports about other implementations for these two queries:

Q1: SELECT (COUNT(*) AS ?C1) { ?s ?p ?o FILTER(false) }
Q2: SELECT (COUNT(*) AS ?C2) { ?s ?p ?o FILTER(false) } GROUP BY ?s

It doesn't matter what the data is - the WHERE {} does not match anything. Or use Q1,Q2 below with an empty dataset.

== tl;dr

I think that ARQ follows the spec but the spec is not what you might expect from an SQL background.

==

The original question rose from the case of no matches to the WHERE clause.

* What happens when there is no GROUP BY
* What happens when there is a GROUP BY

Q1: SELECT (COUNT(*) AS ?C1) { ?s ?p ?o }
Q2: SELECT (COUNT(*) AS ?C2) { ?s ?p ?o } GROUP BY ?s

with no matches to the graph pattern  { ?s ?p ?o }.
(beware below! {} are being used in different ways!)

== The SPARQL 1.1 specification

In the definition of "Group",
https://www.w3.org/TR/sparql11-query/#defn_algGroup

If the pattern does not match, then Group(exprlist, Ω) is the empty set ∅.

ListEval(exprlist, μ) is the GROUP BY key
  μ is a row ("solution mapping" in the spec).

ListEval(exprlist, μ) → { μ' ... } is the grouping of rows by GROUP BY. For each group, there is a set of rows. It's a map from key to set of rows.

If there are no matches, so no μ, there are no entries in this map. When there are no matches to the pattern "WHERE { ?s ?p ?o }", so no μ, there are no entries in this map; { μ' ... } is the empty set ∅.

From here, the rest of the definitions simplify down to empty.

Aggregation(exprlist, func, scalarvals, ∅) is ∅.
AggregateJoin(∅) = ∅.
Flatten(∅) = ∅.

COUNT(∅) = 0 (xsd:integer zero)
SUM(∅) = 0
AVG, MIN, MAX, SAMPLE are errors
GROUP_CONCAT = ""

Throughout this, it does not matter if there is a GROUP BY clause or not because "Group" is the empty set ∅ in both cases.

This may be surprising if you are used to SQL because in SQL with GROUP BY you get no rows, and COUNT is never zero, whereas with no GROUP BY, you get one row and a COUNT of zero.

== Results from ARQ

Q1: no matches:
------
| C1 |
======
| 0  |
------
Q2: no matches:
------
| C2 |
======
| 0  |
------

Reply via email to