Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
foreach
Syntax
1q = foreach q generate expression as alias[, expression as alias ...];The output column names are specified with the as keyword. The output data is ungrouped.
Using foreach with Ungrouped Data
When used with ungrouped data, the foreach statement maps the input rows to output rows. The number of rows remains the same.
Example
Using foreach with Grouped Data
When used with grouped data, the foreach statement behaves differently than it does with ungrouped data.
Fields can be directly accessed only when the value is the same for all group members. For example, the fields that were used as the grouping keys have the same value for all group members. Otherwise, use aggregate functions to access the members of a group. The type of the column determines which aggregate functions you can use. For example, if the column type is numeric, you can use the sum() function.
Example
Using foreach with a case Expression
Projected Field Names
Each field name in a projection must be unique and not have the name 'none'. Invalid field names throw an error.
1l = load "0Fabb000000002qCAA/0Fabb000000002WCAQ";
2r = load "0Fcyy000000002qCAA/0Fcyy000000002WCAQ";
3l = foreach l generate 'value'/'divisor' as 'value' , category as category;
4r = foreach r generate 'value'/'divisor' as 'value' , category as category;
5cg = cogroup l by category right, r by category;
6cg = foreach cg generate r.category as 'category', sum(r.value) as sumrval, sum(l.value) as sumrval;1q = load "Products";
2q = group q by all;
3q = foreach q generate count() as 'none';
4q = limit q 2000;Examples
For examples of projections, see Calculate Grand Totals and Subtotals with the rollup Modifier and grouping() Function.