# Aggregate

Aggregate functions compute a single result from the elements in a bag.

Basic aggregate functions have one of their arguments declared as a bag and return a single result:

`aggfn(Bag of Type1 x) -> Type2`

Aggregate functions with a single argument can be used in group by queries.

## Functions

`aggregate(Stream s,Object e0,Function f)->Stream`

Stream of values aggregated over stream `s`

by function `f`

with
start value `e0`

`aggregate(Vector v,Object e0,Function f)->Object`

The aggregated value of vector `v`

by function `f`

with start value `e0`

`aggregate(Bag b,Object e0,Function f)->Object`

The aggregated value of bag `b`

by function `f`

with start value `e0`

`aggregate(Bag b,Object e0,Charstring fn)->Object`

The aggregated value of bag `b`

by function named `fn`

with start value `e0`

`aggregate(Stream s,Object e0,Charstring fn)->Stream`

Stream of values aggregated over stream `s`

by function named `fn`

with start value `e0`

`aggregate(Vector v,Object e0,Charstring fn)->Object`

The aggregated value of vector `v`

by function named `fn`

with start value `e0`

`aggv(Bag of Vector bv,Function fn)->Vector of Number`

Apply aggregate function `fn`

on each position of the vectors in `bv`

`concatagg(Bag b)->Charstring`

Concatenate stringified elements in bag `b`

`count(Bag b)->Integer`

The number of elements in bag `b`

`fnoagg(Stream s,Function init,Function agg,Function emitfn)->Stream`

Cumulative aggregation over stream `s`

:
The aggregation is initialized using `init`

For each incoming tuple in `s`

, the aggregation is updated using `agg`

For each incoming tuple in `s`

, the aggregation is emitted using `emitfn`

`fnoagg(Stream s,Function init,Function agg,Function emitfn,Integer size,`

Integer stride)->Stream

Window aggregation over stream `s`

:
Windows of `size`

and `stride`

are formed over `s`

.
For each window,

- the aggregation is initialized using
`init`

- for each incoming tuple in
`s`

, the aggregation is updated using`agg`

- when the window is complete, its aggregation is emitted using
`emitfn`

`forall(Bag b)->Boolean`

Iterate through `b`

`histogram(Bag of Vector b,Matrix limits)->Vector of Integer`

Calculate a histogram over a bag of tuple vectors `b`

where each row
$l_i$ in the `limits`

matrix specifies the ranges and number of bins in
tuple position $i$ as $l_i = [min_i,max_i, bins_i]$

`histogram(Bag of Number b,Vector limits)->Vector of Integer`

Calculate a histogram over a bag `b`

where the `limits`

vector specifies
the ranges and number of bins as `[min,max,number of bins]`

`histogram(Bag of Number b,Number min,Number max,Number bins)->Vector of Integer`

Calculate a histogram over a bag `b`

, with `min`

, `max`

, and `bins`

the range for the histogram is always $[min,max)$

`init_avg()->(Integer,Real)`

`init_avgv()->Vector of Number`

`inject(Bag b,Object o)->Bag`

Inject 'o' between elements in bag 'b'

`max(Bag b)->Object`

The largest element in bag `b`

`mean(Bag b)->Real a`

Average of numbers in bag `b`

`median(Bag of Number b)->Number`

The median of numbers in bag `b`

`median(Vector v)->Number`

The median of numbers in vector `v`

`min(Bag b)->Object`

The smallest element in bag `b`

`notany(Bag b)->Boolean`

Is bag `b`

empty?

`nxt_avg(Integer cnt,Real sum,Real this)->(Integer,Real)`

`nxt_avgv(Vector of Number v,Real this)->Vector of Number`

`some(Bag b)->Boolean`

Is there any element in bag `b`

?

`stdev(Bag b)->Real s`

Standard deviation of numbers in bag `b`

`sum(Bag b)->Number`

The sum of the numbers in bag `b`

`this_avg(Integer cnt,Real sum)->Vector of Real`

`this_avgv(Vector of Number v)->Vector of Real`

`vectorize(Bag b)->Vector v`

`winstatagg(Stream s,Integer size,Integer stride)->Stream`

Statistics window aggregation over stream `s`

:
Windows of `size`

and `stride`

are formed over `s`

.
For each window,

- statistics emitted when the window is complete