// select sum(age), dept, hour(min(lunchTime)) group by dept
Query q = new Query();
// Add selection.
QuerySelection selection = new QuerySelection();
selection.addColumn(new AggregationColumn(new SimpleColumn("age"),
AggregationType.SUM));
selection.addColumn(new SimpleColumn("dept"));
selection.addColumn(new ScalarFunctionColumn(
Lists.newArrayList((AbstractColumn) new AggregationColumn(
new SimpleColumn("lunchTime"), AggregationType.MIN)),
TimeComponentExtractor.getInstance(
TimeComponentExtractor.TimeComponent.HOUR)));
q.setSelection(selection);
// Add group.
QueryGroup group = new QueryGroup();
group.addColumn(new SimpleColumn("dept"));
q.setGroup(group);
q.validate();
DataTable result = QueryEngine.executeQuery(q, res, ULocale.US);
// Test column description
List<ColumnDescription> cols = result.getColumnDescriptions();
assertEquals(3, cols.size());
assertEquals("sum-age", cols.get(0).getId());
assertEquals("dept", cols.get(1).getId());
assertEquals("hour_min-lunchTime", cols.get(2).getId());
String[][] resultStrings = MockDataSource.queryResultToStringMatrix(result);
assertEquals(3, resultStrings.length);
assertStringArraysEqual(new String[]{"92.0", "Eng", "12.0"},
resultStrings[0]);
assertStringArraysEqual(new String[]{"24.0", "Marketing", "13.0"},
resultStrings[1]);
assertStringArraysEqual(new String[]{"57.0", "Sales", "12.0"},
resultStrings[2]);
// Test group-by column with scalar function in the selection:
// select count(name), hour(lunchTime) group by lunchtime
q = new Query();
// Add selection
QuerySelection selection1 = new QuerySelection();
selection1.addColumn(new AggregationColumn(new SimpleColumn("name"),
AggregationType.COUNT));
selection1.addColumn(new ScalarFunctionColumn(
Lists.newArrayList((AbstractColumn) new SimpleColumn("lunchTime")),
TimeComponentExtractor.getInstance(
TimeComponentExtractor.TimeComponent.HOUR)));
q.setSelection(selection1);