Explode array with position

Supported in: Batch, Streaming

Explode array into a row per value as a struct containing the element's relative position in the array and the element itself.

Expression categories: Array

Declared arguments

  • Array - Array of values to explode.
    Expression<Array<T>>
  • optional Keep empty / null arrays - If true, empty arrays and nulls will be kept as nulls in the output, otherwise they will be filtered.
    Literal<Boolean>

Type variable bounds: T accepts AnyType

Output type: Struct<Optional[position], Optional[element]>

Examples

Example 1: Base case

Argument values:

  • Array: array
  • Keep empty / null arrays: null

Given input table:

array
[ one, two, three ]
[ four, five ]

Expected output table: | array | | ----- | | {
 element -> one,
 position -> 1,
} | | {
 element -> two,
 position -> 2,
} | | {
 element -> three,
 position -> 3,
} | | {
 element -> four,
 position -> 1,
} | | {
 element -> five,
 position -> 2,
} |


Example 2: Edge case

Argument values:

  • Array: array
  • Keep empty / null arrays: false

Given input table:

array
[ one, two, three ]
[ ]
[ four, five ]
[ ]

Expected output table: | array | | ----- | | {
 element -> one,
 position -> 1,
} | | {
 element -> two,
 position -> 2,
} | | {
 element -> three,
 position -> 3,
} | | {
 element -> four,
 position -> 1,
} | | {
 element -> five,
 position -> 2,
} |


Example 3: Edge case

Argument values:

  • Array: array
  • Keep empty / null arrays: true

Given input table:

array
[ one, two, three ]
[ ]
[ four, five ]
[ ]

Expected output table: | array | | ----- | | {
 element -> one,
 position -> 1,
} | | {
 element -> two,
 position -> 2,
} | | {
 element -> three,
 position -> 3,
} | | {
 element -> null,
 position -> null,
} | | {
 element -> four,
 position -> 1,
} | | {
 element -> five,
 position -> 2,
} | | {
 element -> null,
 position -> null,
} |