@Test
  public void testGroupByQuery() throws Exception {
    Table table = dataContext.getDefaultSchema().getTableByName(peopleIndexType);

    Query q = new Query();
    q.from(table);
    q.groupBy(table.getColumnByName("gender"));
    q.select(
        new SelectItem(table.getColumnByName("gender")),
        new SelectItem(FunctionType.MAX, table.getColumnByName("age")),
        new SelectItem(FunctionType.MIN, table.getColumnByName("age")),
        new SelectItem(FunctionType.COUNT, "*", "total"),
        new SelectItem(FunctionType.MIN, table.getColumnByName("id")).setAlias("firstId"));
    q.orderBy("gender");
    DataSet data = dataContext.executeQuery(q);
    assertEquals(
        "[peopletype.gender, MAX(peopletype.age), MIN(peopletype.age), COUNT(*) AS total, MIN(peopletype.id) AS firstId]",
        Arrays.toString(data.getSelectItems()));

    assertTrue(data.next());
    assertEquals("Row[values=[female, 20, 17, 5, 5]]", data.getRow().toString());
    assertTrue(data.next());
    assertEquals("Row[values=[male, 19, 17, 4, 1]]", data.getRow().toString());
    assertFalse(data.next());
  }
  public void testSplitSubQuery() throws Exception {
    Connection con = getTestDbConnection();
    DataContext dc = new JdbcDataContext(con);
    Schema schema = dc.getDefaultSchema();
    Table employeesTable = schema.getTableByName("EMPLOYEES");
    Table customersTable = schema.getTableByName("CUSTOMERS");
    Query sq = new Query().from(employeesTable, "e").from(customersTable, "c");
    SelectItem empSelectItem = new SelectItem(employeesTable.getColumns()[0]);
    SelectItem custSelectItem = new SelectItem(customersTable.getColumns()[0]);
    sq.select(empSelectItem, custSelectItem);
    assertEquals(
        "SELECT e._EMPLOYEENUMBER_, c._CUSTOMERNUMBER_ FROM PUBLIC._EMPLOYEES_ e, PUBLIC._CUSTOMERS_ c",
        sq.toString().replace('\"', '_'));
    Query q = new Query();
    FromItem sqItem = new FromItem(sq).setAlias("sq");

    custSelectItem.setAlias("c_num");
    empSelectItem.setAlias("e_num");

    q.from(sqItem);
    q.select(new SelectItem(custSelectItem, sqItem), new SelectItem(empSelectItem, sqItem));
    assertEquals(
        "SELECT sq.c_num, sq.e_num FROM (SELECT e._EMPLOYEENUMBER_ AS e_num, c._CUSTOMERNUMBER_ AS c_num FROM PUBLIC._EMPLOYEES_ e, PUBLIC._CUSTOMERS_ c) sq",
        q.toString().replace('\"', '_'));

    QuerySplitter qs = new QuerySplitter(dc, q);
    assertEquals(2806, qs.getRowCount());

    List<Query> splitQueries = qs.setMaxRows(1000).splitQuery();
    assertSameCount(dc, qs, splitQueries);
    assertEquals(5, splitQueries.size());
    assertEquals("[793, 610, 366, 714, 323]", Arrays.toString(getCounts(dc, splitQueries)));

    splitQueries = qs.setMaxRows(2000).splitQuery();
    assertSameCount(dc, qs, splitQueries);
    assertEquals(2, splitQueries.size());
    assertEquals("[1403, 1403]", Arrays.toString(getCounts(dc, splitQueries)));

    DataSet data = qs.executeQueries();
    int count = 0;
    while (data.next()) {
      if (count == 2) {
        assertEquals("Row[values=[114, 1002]]", data.getRow().toString());
      }
      count++;
    }
    data.close();
    assertEquals(2806, count);
  }