W3C home > Mailing lists > Public > public-rdf-dawg@w3.org > April to June 2012

Nested Aggregate Expressions

From: Andy Seaborne <andy.seaborne@epimorphics.com>
Date: Sun, 03 Jun 2012 16:03:55 +0100
Message-ID: <4FCB7CDB.4060205@epimorphics.com>
To: SPARQL Working Group <public-rdf-dawg@w3.org>
Can aggregate expressions contain aggregates?

This comes from a recent email [1] and while these may well not actually 
address the application goal (a subquery was meant - see thread), the 
SPARQL 1.1 Query spec does permit these unusual queries.


PREFIX ex: <http://example.org/meals#>
SELECT (AVG(?mealPrice * (1.0 + MAX( ?mealTip / ?mealPrice)))
            AS ?avgCostWithBestTip)
   ?description ex:mealPrice ?mealPrice .
   ?description ex:mealTip ?mealTip .
} GROUP BY ?description

i.e.an aggregate inside an aggregate:  AVG(?x * MAX (?y) )

One line of argument is that the expression inside the aggregate is 
applied to each row, so only row variables should be considered 
in-scope.  The aggregate AVG(max(?x)+1) is violating that as max(?x) is 
not a per-row expression.

What ARQ does is to calculate the aggregates of a group as the group 
streams past; it does not wait until the end of evaluation of the whole 
block when all the elements of all the groups are known.

Related to this is the interaction with select expressions:

SELECT (max(?x) As ?M) (avg(?M+1) AS ?A)

because the select expression rules say you can use ?M inside AVG().

If we wish to forbid this, we can do it quite easily by having a parser 
rule that aggregates can't appear in expression for the aggregate, which 
is a simple static check.


[1] http://s.apache.org/Yy6
Received on Sunday, 3 June 2012 15:04:32 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 15:01:06 UTC