assertStringArraysEqual(new String[]{"2.0", "1.0", "1.0", "1.0", "1.0"},
resultStrings[0]);
}
public void testGroupingByScalarFunctionWithPivoting() throws Exception {
DataTable res = MockDataSource.getData(3);
// select sum(age), count(dept), hour(lunchTime) group by hour(lunchtime)
// pivot isSenior.
Query q = new Query();
// Add selection.
QuerySelection selection = new QuerySelection();
selection.addColumn(new AggregationColumn(new SimpleColumn("age"),
AggregationType.SUM));
selection.addColumn(new AggregationColumn(new SimpleColumn("dept"),
AggregationType.COUNT));
selection.addColumn(new ScalarFunctionColumn(
Lists.newArrayList((AbstractColumn) new SimpleColumn("lunchTime")),
TimeComponentExtractor.getInstance(
TimeComponentExtractor.TimeComponent.HOUR)));
q.setSelection(selection);
// Add group.
QueryGroup group = new QueryGroup();
group.addColumn(new ScalarFunctionColumn(
Lists.newArrayList((AbstractColumn) new SimpleColumn("lunchTime")),
TimeComponentExtractor.getInstance(
TimeComponentExtractor.TimeComponent.HOUR)));
q.setGroup(group);
// Add pivot.
QueryPivot pivot = new QueryPivot();
pivot.addColumn(new SimpleColumn("isSenior"));
q.setPivot(pivot);
q.validate();
DataTable result = QueryEngine.executeQuery(q, res, ULocale.US);
// Test column description
List<ColumnDescription> cols = result.getColumnDescriptions();
assertEquals(5, cols.size());
assertEquals("false sum-age", cols.get(0).getId());
assertEquals("true sum-age", cols.get(1).getId());
assertEquals("false count-dept", cols.get(2).getId());
assertEquals("true count-dept", cols.get(3).getId());
assertEquals("hour_lunchTime", cols.get(4).getId());
String[][] resultStrings = MockDataSource.queryResultToStringMatrix(result);
assertEquals(2, resultStrings.length);
assertStringArraysEqual(new String[]{"52.0", "67.0", "2.0", "2.0", "12.0"},
resultStrings[0]);
assertStringArraysEqual(new String[]{"30.0", "24.0", "1.0", "1.0", "13.0"},
resultStrings[1]);
res = MockDataSource.getData(3);
// select count(dept), datediff(hireDate, seniorityStartTime) group by
// datediff(hireDate, seniorityStartTime) pivot isSenior.
q = new Query();
// Add selection.
selection = new QuerySelection();
selection.addColumn(new AggregationColumn(new SimpleColumn("dept"),
AggregationType.COUNT));
selection.addColumn(new ScalarFunctionColumn(
Lists.<AbstractColumn>newArrayList(
new SimpleColumn("seniorityStartTime"),
new SimpleColumn("hireDate")), DateDiff.getInstance()));
q.setSelection(selection);
// Add group.
group = new QueryGroup();
group.addColumn(new ScalarFunctionColumn(
Lists.<AbstractColumn>newArrayList(
new SimpleColumn("seniorityStartTime"),
new SimpleColumn("hireDate")), DateDiff.getInstance()));
q.setGroup(group);
// Add pivot.
pivot = new QueryPivot();
pivot.addColumn(new SimpleColumn("isSenior"));
q.setPivot(pivot);
q.validate();
result = QueryEngine.executeQuery(q, res, ULocale.US);
// Test column description
cols = result.getColumnDescriptions();
assertEquals(3, cols.size());
assertEquals("false count-dept", cols.get(0).getId());
assertEquals("true count-dept", cols.get(1).getId());
assertEquals("dateDiff_seniorityStartTime,hireDate", cols.get(2).getId());