Skip to content

Commit

Permalink
Document reduce_agg aggregate function
Browse files Browse the repository at this point in the history
  • Loading branch information
wenleix committed Jan 22, 2019
1 parent 93f7571 commit d4de05b
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions presto-docs/src/main/sphinx/functions/aggregate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,44 @@ General Aggregate Functions

Returns ``n`` smallest values of all input values of ``x``.

.. 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 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, 2)
(1, 3),
(1, 4),
(2, 20),
(2, 30),
(2, 40)
) AS t(id, value)
GROUP BY id;
-- (1, 9)
-- (2, 90)

SELECT id, reduce_agg(value, 1, (a, b) -> a * b, (a, b) -> a * b)
FROM (
VALUES
(1, 2),
(1, 3),
(1, 4),
(2, 20),
(2, 30),
(2, 40)
) AS t(id, value)
GROUP BY id;
-- (1, 24)
-- (2, 24000)

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

.. function:: sum(x) -> [same as input]

Returns the sum of all input values.
Expand Down

0 comments on commit d4de05b

Please sign in to comment.