Skip to content

Commit

Permalink
Document reduce_agg aggregate function
Browse files Browse the repository at this point in the history
Extracted from prestodb/presto#12195
  • Loading branch information
electrum committed Jan 22, 2019
1 parent fd34a3c commit 82b7fff
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions presto-docs/src/main/sphinx/functions/aggregate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,42 @@ Statistical Aggregate Functions
.. function:: var_samp(x) -> double

Returns the sample variance of all input values.

Lambda Aggregate Functions
--------------------------

.. function:: reduce_agg(inputValue T, initialState S, inputFunction(S, T, S), combineFunction(S, S, S)) -> S

Reduces all input values into a single value. ``inputFunction`` will be invoked
for each non-null input value. In addition to taking the input value, ``inputFunction``
takes the current state, initially ``initialState``, and returns the new state.
``combineFunction`` will be invoked to combine two states into a new state.
The final state is returned::

SELECT id, reduce_agg(value, 0, (a, b) -> a + b, (a, b) -> a + b)
FROM (
VALUES
(1, 3),
(1, 4),
(1, 5),
(2, 6),
(2, 7)
) AS t(id, value)
GROUP BY id;
-- (1, 12)
-- (2, 13)

SELECT id, reduce_agg(value, 1, (a, b) -> a * b, (a, b) -> a * b)
FROM (
VALUES
(1, 3),
(1, 4),
(1, 5),
(2, 6),
(2, 7)
) AS t(id, value)
GROUP BY id;
-- (1, 60)
-- (2, 42)

The state type must be a boolean, integer, floating-point, or date/time/interval.

0 comments on commit 82b7fff

Please sign in to comment.