Flatten

Description

Flatten removes a level of nesting in a record. The result of applying Flatten to a bag is that each item in the bag will appear in a record of its own. If you flatten two bags in the same data set, they will be crossed join (i.e. you will get records for each item in the first bag with each of the items in the second bag).

Syntax

Flatten(bag)

Examples

TOKENIZE('a,b,c',',') returns a bag with 3 items: {(a),(b),(c)}

Flatten(TOKENIZE('a,b,c',',')) returns 3 records:

a
b
c

Notes

Since Flatten works at the record level rather than the field level, it can't be used in the x-console.

Using Flatten on an empty bag could result in the record being excluded. As a safeguard, use it with an expression as Flatten(COALESCE(CASE WHEN SIZE(bag) > 0 THEN bag END, TOBAG('')))

Return value datatype

Depends on input datatype.

Impact of null value

If input is null, returns null.