// columns and the aggregation columns.
columnIndices.clear();
int columnIndex = 0;
if (group != null) {
List<Value> empytListOfValues = Lists.newArrayList();
columnLookups.put(empytListOfValues, new GenericColumnLookup());
for (AbstractColumn column : group.getColumns()) {
columnIndices.put(column, columnIndex);
if (!(column instanceof ScalarFunctionColumn)) {
((GenericColumnLookup) columnLookups.get(empytListOfValues)).put(column, columnIndex);
for (List<Value> columnValues : pivotValuesSet) {
if (!columnLookups.containsKey(columnValues)) {
columnLookups.put(columnValues, new GenericColumnLookup());
}
((GenericColumnLookup) columnLookups.get(columnValues)).put(column, columnIndex);
}
}
columnIndex++;
}
}
for (ColumnTitle title : columnTitles) {
columnIndices.put(title.aggregation, columnIndex);
List<Value> values = title.getValues();
if (!columnLookups.containsKey(values)) {
columnLookups.put(values, new GenericColumnLookup());
}
((GenericColumnLookup) columnLookups.get(values)).put(title.aggregation, columnIndex);
columnIndex++;
}
// Dump the data from the metaTable to the result DataTable.
for (RowTitle rowTitle : rowTitles) {
TableRow curRow = new TableRow();
// Add the group-by columns cells.
for (Value v : rowTitle.values) {
curRow.addCell(new TableCell(v));
}
Map<ColumnTitle, TableCell> rowData = metaTable.getRow(rowTitle);
int i = 0;
// Add the aggregation columns cells.
for (ColumnTitle colTitle : columnTitles) {
TableCell cell = rowData.get(colTitle);
curRow.addCell((cell != null) ? cell : new TableCell(
Value.getNullValueFromValueType(colDescs.get(i + rowTitle.values.size()).getType())));
i++;
}
// Add the scalar function columns cells.
for (ScalarFunctionColumnTitle columnTitle : scalarFunctionColumnTitles) {
curRow.addCell(new TableCell(columnTitle.scalarFunctionColumn.
getValue(columnLookups.get(columnTitle.getValues()), curRow)));
}
result.addRow(curRow);
}
// Fill the columnIndices and columnLookups parameters for the scalar
// function column titles. This must be done after the calculation of the values
// in the scalar function column cells, or else the scalar function columns
// will not calculate their value recursively, but return the current value.
// See the logic of the getValue() method in ScalarFunctionColumn.
for (ScalarFunctionColumnTitle scalarFunctionColumnTitle
: scalarFunctionColumnTitles) {
columnIndices.put(scalarFunctionColumnTitle.scalarFunctionColumn,
columnIndex);
List<Value> values = scalarFunctionColumnTitle.getValues();
if (!columnLookups.containsKey(values)) {
columnLookups.put(values, new GenericColumnLookup());
}
((GenericColumnLookup) columnLookups.get(values)).put(
scalarFunctionColumnTitle.scalarFunctionColumn, columnIndex);
columnIndex++;
}