}
}
columnIndices = newColumnIndices;
columnIndicesReference.set(columnIndices);
DataTable result = new DataTable();
result.addColumns(newColumnDescriptions);
// Calculate the values in the data table rows.
for (TableRow sourceRow : table.getRows()) {
TableRow newRow = new TableRow();
for (AbstractColumn col : selectedColumns) {
boolean wasFound = false;
Set<List<Value>> pivotValuesSet = columnLookups.keySet();
for (List<Value> values : pivotValuesSet) {
// If the current column-lookup contains the current column and it is
// either a column that contains aggregations or a column that
// contains only group-by columns and was not yet found, get its value
// in the current row. Otherwise continue. If the column contains
// only group-by columns it should appear only once, even though
// it may appear in many column lookups.
if (columnLookups.get(values).containsColumn(col)
&& ((col.getAllAggregationColumns().size() != 0) || !wasFound)) {
wasFound = true;
newRow.addCell(sourceRow.getCell(columnLookups.get(values).getColumnIndex(col)));
}
}
// If the column was not found in any of the column lookups
// calculate its value (e.g., scalar function column that was not
// calculated in a previous stage).
if (!wasFound) {
DataTableColumnLookup lookup = new DataTableColumnLookup(table);
newRow.addCell(col.getCell(lookup, sourceRow));
}
}
result.addRow(newRow);
}
return result;
}