In their simplest manifestation, aggregates can be described as the end result of a calculation over every record of a table. In a more advanced form, aggregates return partial results during the computation. In other words an aggregate is calculated each time a record of the table is processed, and it reaches a final value once all the records have been processed.
We associate a state with an aggregate. Initially the aggregate has some state
S depending on the first record of the table. Then with each record
processed
the state of the aggregate changes depending on the value of the fetched
record. Finally after all the records of the table are processed, the
aggregate has state S
. The result is then produced based on all the
states the aggregate takes.
In simple aggregates like sum, max, count etc. the result is produced based on
only S
. Whereas in online aggregates results are produced every i
record and hence depend on all the intermediate states S
of the aggregate.
The process of the calculation of an aggregate can be written in pseudo-code as:
We now bring out the notion of single, multi and
produce. The calculation of the aggregates can be summarized by just these
three functions. Single is the action to be performed to calculate
S from the first record. Multi is the action to be
performed to compute state new S
, given the next record and state
S
. Produce is the action to be performed to produce the result
depending on any intermediate state S
or state S
. We can rewrite
the computation of the aggregate in terms of single, multi and
produce as:
A generic tool to generate code for an aggregate thus will accept the definition of single, multi and produce and use it to generate the code for that aggregate.