Reduce array elements

Supported in: Batch, Streaming

Reduces array elements using an expression.

Expression categories: Array

Declared arguments

  • Array - Array to reduce.
    Expression<Array<Array<Boolean | Byte | Date | Double | Float | Integer | Long | Map<AnyType, AnyType> | Short | String | Timestamp> | Boolean | Byte | Date | Double | Float | Integer | Long | Map<AnyType, AnyType> | Short | String | Timestamp>>
  • Expression to reduce - The expression to apply once per element of the array.
    Expression<T>
  • Initial value - This is the start value used to initialize the accumulator, if the array has a length of 0, this value will be returned.
    Expression<T>

Type variable bounds: T accepts Array<Boolean | Byte | Date | Double | Float | Integer | Long | Map<AnyType, AnyType> | Short | String | Timestamp> | Boolean | Byte | Date | Double | Float | Integer | Long | Map<AnyType, AnyType> | Short | String | Timestamp

Output type: T

Examples

Example 1: Base case

Argument values:

  • Array: miles
  • Expression to reduce:
    add(
     expressions: [accumulator, element],
    )
  • Initial value: 0
milesOutput
[ 12300, 12342 ]24642

Example 2: Base case

Description: Return the first non null within the array. Argument values:

  • Array: miles
  • Expression to reduce:
    firstNonNull(
     expressions: [accumulator, element],
    )
  • Initial value: init
milesinitOutput
[ null, null, 12300, 12111 ]null12300

Example 3: Base case

Argument values:

  • Array: miles
  • Expression to reduce:
    concatStrings(
     expressions: [accumulator,
    cast(
     expression: element,
     type: String,
    )],
     separator: -,
    )
  • Initial value: empty string
milesOutput
[ 12300, 12342 ]-12300-12342

Example 4: Null case

Description: Null arrays will return null outputs. Argument values:

  • Array: miles
  • Expression to reduce:
    firstNonNull(
     expressions: [accumulator, element],
    )
  • Initial value: init
milesinitOutput
nullnullnull

Example 5: Edge case

Description: Empty array will return the initial value. Argument values:

  • Array: miles
  • Expression to reduce:
    add(
     expressions: [accumulator, element],
    )
  • Initial value: 0
milesOutput
[ ]0