QuerySelection completionSelection = new QuerySelection();
List<AbstractColumn> originalSelectedColumns =
query.getSelection().getColumns();
for (int i = 0; i < originalSelectedColumns.size(); i++) {
AbstractColumn column = originalSelectedColumns.get(i);
if (query.getGroup().getColumns().contains(column)) {
completionSelection.addColumn(column);
} else { // Must be an aggregation column if doesn't appear in the grouping.
// The id here is the id generated by the data source for the column containing
// the aggregated data, e.g., max-B.
String id = column.getId();
// MIN is chosen arbitrarily, because there will be exactly one.
completionSelection.addColumn(
new AggregationColumn(new SimpleColumn(id), AggregationType.MIN));
}
}
completionQuery.setSelection(completionSelection);
} else {
// When there is no pivoting, sql does everything (except skipping, options, labels, format).
dataSourceQuery.copyFrom(query);
dataSourceQuery.setOptions(null);
completionQuery.setOptions(query.getOptions());
try {
// If there is skipping pagination should be done in the completion query
if (query.hasRowSkipping()) {
dataSourceQuery.setRowSkipping(0);
dataSourceQuery.setRowLimit(-1);
dataSourceQuery.setRowOffset(0);
completionQuery.copyRowSkipping(query);
completionQuery.copyRowLimit(query);
completionQuery.copyRowOffset(query);
}
if (query.hasLabels()) {
dataSourceQuery.setLabels(null);
QueryLabels labels = query.getLabels();
QueryLabels newLabels = new QueryLabels();
for (AbstractColumn column : labels.getColumns()) {
newLabels.addLabel(new SimpleColumn(column.getId()), labels.getLabel(column));
}
completionQuery.setLabels(newLabels);
}
if (query.hasUserFormatOptions()) {
dataSourceQuery.setUserFormatOptions(null);
QueryFormat formats = query.getUserFormatOptions();
QueryFormat newFormats = new QueryFormat();
for (AbstractColumn column : formats.getColumns()) {
newFormats.addPattern(new SimpleColumn(column.getId()), formats.getPattern(column));
}
completionQuery.setUserFormatOptions(newFormats);
}
} catch (InvalidQueryException e) {
// Should not happen.