reduce() function

The reduce() function aggregates records in each table according to the reducer, fn, providing a way to create custom aggregations. The output for each table is the group key of the table with columns corresponding to each field in the reducer record.

*Function type: Transformation*

  1. reduce(
  2. fn: (r, accumulator) => ({ sum: r._value + accumulator.sum }),
  3. identity: {sum: 0.0}
  4. )

If the reducer record contains a column with the same name as a group key column, the group key column’s value is overwritten, and the outgoing group key is changed. However, if two reduced tables write to the same destination group key, the function will error.

Parameters

Make sure fn parameter names match each specified parameter. To learn why, see Match parameter names.

fn

Function to apply to each record with a reducer record (identity).

*Data type: Function*

fn syntax
  1. // Pattern
  2. fn: (r, accumulator) => ({ identityKey: r.column + accumulator.identityKey })
  3. // Example
  4. fn: (r, accumulator) => ({ sum: r._value + accumulator.sum })

Matching output record keys and types

The output record from fn must have the same key names and value types as the identity. After operating on a record, the output record is given back to fn as the input accumulator. If the output record keys and value types do not match the identity keys and value types, it will return a type error.

r

Record representing each row or record.

accumulator

Reducer record defined by identity.

identity

Defines the reducer record and provides initial values to use when creating a reducer. May be used more than once in asynchronous processing use cases. The data type of values in the identity record determine the data type of output values.

*Data type: Record*

identity record syntax
  1. // Pattern
  2. identity: {identityKey1: value1, identityKey2: value2}
  3. // Example
  4. identity: {sum: 0.0, count: 0.0}

Important notes

Dropped columns

By default, reduce() drops any columns that:

  1. Are not part of the input table’s group key.
  2. Are not explicitly mapped in the reduce() function.

Examples

Compute the sum of the value column
  1. from(bucket:"example-bucket")
  2. |> filter(fn: (r) =>
  3. r._measurement == "cpu" and
  4. r._field == "usage_system" and
  5. r.service == "app-server"
  6. )
  7. |> range(start:-12h)
  8. |> reduce(
  9. fn: (r, accumulator) => ({
  10. sum: r._value + accumulator.sum
  11. }),
  12. identity: {sum: 0.0}
  13. )
Compute the sum and count in a single reducer
  1. from(bucket:"example-bucket")
  2. |> filter(fn: (r) =>
  3. r._measurement == "cpu" and
  4. r._field == "usage_system" and
  5. r.service == "app-server"
  6. )
  7. |> range(start:-12h)
  8. |> reduce(
  9. fn: (r, accumulator) => ({
  10. sum: r._value + accumulator.sum,
  11. count: accumulator.count + 1.0
  12. }),
  13. identity: {sum: 0.0, count: 0.0}
  14. )
Compute the product of all values
  1. from(bucket:"example-bucket")
  2. |> filter(fn: (r) =>
  3. r._measurement == "cpu" and
  4. r._field == "usage_system" and
  5. r.service == "app-server")
  6. |> range(start:-12h)
  7. |> reduce(
  8. fn: (r, accumulator) => ({
  9. prod: r._value * accumulator.prod
  10. }),
  11. identity: {prod: 1.0}
  12. )
Calculate the average
  1. from(bucket: "example-bucket")
  2. |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  3. |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
  4. |> window(every: 5m)
  5. |> reduce(fn: (r, accumulator) => ({
  6. count: accumulator.count + 1,
  7. total: accumulator.total + r._value,
  8. avg: (accumulator.total + r._value) / float(v: accumulator.count)
  9. }),
  10. identity: {count: 1, total: 0.0, avg: 0.0}
  11. )

Related articles

exists