Documentation

reduce() function

Flux 0.23.0+

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. reduce() is an aggregate function.

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

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).

fn syntax
// Pattern
fn: (r, accumulator) => ({ identityKey: r.column + accumulator.identityKey })

// Example
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.

identity record syntax
// Pattern
identity: {identityKey1: value1, identityKey2: value2}

// Example
identity: {sum: 0.0, count: 0.0}

tables

Input data. Default is piped-forward data (<-).

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

The following examples use data provided by the sampledata package to show how reduce() transforms data.

Compute the sum of the value column

import "sampledata"

sampledata.int()
    |> reduce(
        fn: (r, accumulator) => ({
            sum: r._value + accumulator.sum
        }),
        identity: {sum: 0}
    )

View input and output

Compute the sum and count in a single reducer

import "sampledata"

sampledata.int()
    |> reduce(
        fn: (r, accumulator) => ({
          sum: r._value + accumulator.sum,
          count: accumulator.count + 1
        }),
        identity: {sum: 0, count: 0}
    )

View input and output

Compute the product of all values

import "sampledata"

sampledata.int()
    |> reduce(
        fn: (r, accumulator) => ({
            prod: r._value * accumulator.prod
        }),
        identity: {prod: 1}        
    )

View input and output

Calculate the average of all values

import "sampledata"

sampledata.int()
  |> reduce(fn: (r, accumulator) => ({
      count: accumulator.count + 1,
      total: accumulator.total + r._value,
      avg: float(v: (accumulator.total + r._value)) / float(v: accumulator.count)
    }),
    identity: {count: 1, total: 0, avg: 0.0}
  )

View input and output


Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.