Package com.google.visualization.datasource.datatable

Examples of com.google.visualization.datasource.datatable.DataTable


    Query q = new Query();
    QuerySelection sel = new QuerySelection();
    sel.addColumn(new SimpleColumn("weight"));
    sel.addColumn(new SimpleColumn("isPig"));
    q.setSelection(sel);
    DataTable res = QueryEngine.executeQuery(q, input, ULocale.US);
    List<ColumnDescription> cols = res.getColumnDescriptions();

    assertEquals(2, cols.size());
    assertEquals("weight", cols.get(0).getId());
    assertEquals("isPig", cols.get(1).getId());

    assertEquals(3, res.getRows().size());
    assertEquals(2, res.getRows().get(0).getCells().size());
   
    // Verify that formatted values survive the selection.
    assertEquals("T", res.getCell(0, 1).getFormattedValue());
  }
View Full Code Here


    q.setSort(sort);

    QuerySelection selection = new QuerySelection();
    selection.addColumn(new SimpleColumn("name"));
    q.setSelection(selection);
    DataTable res = QueryEngine.executeQuery(q, input, ULocale.US);
    List<ColumnDescription> cols = res.getColumnDescriptions();

    assertEquals(1, cols.size());
    assertEquals("name", cols.get(0).getId());

    assertEquals(3, res.getRows().size());
    assertEquals("ccc", res.getRows().get(0).getCells().get(0).toString());
    assertEquals("bbb", res.getRows().get(1).getCells().get(0).toString());
    assertEquals("aaa", res.getRows().get(2).getCells().get(0).toString());
  }
View Full Code Here

    assertEquals("aaa", res.getRows().get(2).getCells().get(0).toString());
  }

  public void testGrouping() throws Exception {

    DataTable res = MockDataSource.getData(1);

    // select max(songs), min(songs), year, avg(songs), sum(sales)
    // group by year, band
    Query q = new Query();
    QueryGroup group = new QueryGroup();
    group.addColumn(new SimpleColumn("Year"));
    group.addColumn(new SimpleColumn("Band"));
    q.setGroup(group);
    QuerySelection selection = new QuerySelection();
    selection.addColumn(new AggregationColumn(new SimpleColumn("Songs"),
        AggregationType.MAX));
    selection.addColumn(new AggregationColumn(new SimpleColumn("Songs"),
        AggregationType.MIN));
    selection.addColumn(new SimpleColumn("Year"));
    selection.addColumn(new AggregationColumn(new SimpleColumn("Songs"),
        AggregationType.AVG));
    selection.addColumn(new AggregationColumn(new SimpleColumn("Sales"),
        AggregationType.SUM));
    q.setSelection(selection);
    q.validate();

    DataTable result = QueryEngine.executeQuery(q, res, ULocale.US);
    // Test column description
    List<ColumnDescription> cols = result.getColumnDescriptions();

    assertEquals(5, cols.size());
    assertEquals("max Number of new songs", cols.get(0).getLabel());
    assertEquals("min Number of new songs", cols.get(1).getLabel());
    assertEquals("Just year", cols.get(2).getLabel());
View Full Code Here

    assertStringArraysEqual(new String[]{"2.0", "2.0", "2003",
        "2.0", "67.0"}, resultStrings[4]);
  }

  public void testGroupingWithPivoting() throws Exception {
    DataTable res = MockDataSource.getData(1);
    // select max(sales), year, min(sales), avg(fans)
    // group by year
    // pivot by band, songs
    Query q = new Query();
    QueryGroup group = new QueryGroup();
    group.addColumn(new SimpleColumn("Year"));
    q.setGroup(group);
    QueryPivot pivot = new QueryPivot();
    pivot.addColumn(new SimpleColumn("Band"));
    pivot.addColumn(new SimpleColumn("Songs"));
    q.setPivot(pivot);
    QuerySelection selection = new QuerySelection();
    selection.addColumn(new AggregationColumn(new SimpleColumn("Sales"),
        AggregationType.MAX));
    selection.addColumn(new SimpleColumn("Year"));
    selection.addColumn(new AggregationColumn(new SimpleColumn("Sales"),
        AggregationType.MIN));
    selection.addColumn(new AggregationColumn(new SimpleColumn("Fans"),
        AggregationType.AVG));
    q.setSelection(selection);
    q.validate();

    DataTable result = QueryEngine.executeQuery(q, res, ULocale.US);

    // Test column description
    List<ColumnDescription> cols = result.getColumnDescriptions();

    assertEquals(16, cols.size());
    assertEquals("Collection,2.0 max-Sales", cols.get(0).getId());
    assertEquals("Contraband,2.0 max-Sales", cols.get(1).getId());
    assertEquals("Contraband,4.0 max-Sales", cols.get(2).getId());
View Full Code Here

        "2003", "4.0", "null", "null", "null", "null", "2249.733333333333",
        "null", "null", "null", "null"}, resultStrings[2]);
  }

  public void testGroupingByScalarFunction() throws Exception {
    DataTable res = MockDataSource.getData(3);

    // select sum(age), count(dept), hour(lunchTime) group by hour(lunchtime)
    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);

    q.validate();

    DataTable result = QueryEngine.executeQuery(q, res.clone(), ULocale.US);

    // Test column description
    List<ColumnDescription> cols =
        result.getColumnDescriptions();

    assertEquals(3, cols.size());
    assertEquals("sum-age", cols.get(0).getId());
    assertEquals("count-dept", cols.get(1).getId());
    assertEquals("hour_lunchTime", cols.get(2).getId());

    String[][] resultStrings = MockDataSource.queryResultToStringMatrix(result);
    assertEquals(2, resultStrings.length);

    assertStringArraysEqual(new String[]{"119.0", "4.0", "12.0"},
        resultStrings[0]);
    assertStringArraysEqual(new String[]{"54.0", "2.0", "13.0"},
        resultStrings[1]);

    // Select sum(age), count(name), datediff(hireDate, seniorityStartTime),
    // dept group by datediff(hireDate, seniorityStartTime), dept.
    q = new Query();

    // Add selection.
    selection = new QuerySelection();
    selection.addColumn(new AggregationColumn(new SimpleColumn("age"),
        AggregationType.SUM));
    selection.addColumn(new AggregationColumn(new SimpleColumn("name"),
        AggregationType.COUNT));
    selection.addColumn(new ScalarFunctionColumn(
        Lists.<AbstractColumn>newArrayList(
            new SimpleColumn("seniorityStartTime"),
            new SimpleColumn("hireDate")), DateDiff.getInstance()));
    selection.addColumn(new SimpleColumn("dept"));
    q.setSelection(selection);

    // Add group.
    group = new QueryGroup();
    group.addColumn(new ScalarFunctionColumn(
        Lists.<AbstractColumn>newArrayList(
            new SimpleColumn("seniorityStartTime"),
            new SimpleColumn("hireDate")), DateDiff.getInstance()));
    group.addColumn(new SimpleColumn("dept"));
    q.setGroup(group);

    q.validate();

    result = QueryEngine.executeQuery(q, res.clone(), ULocale.US);

    // Test column description
    cols = result.getColumnDescriptions();

    assertEquals(4, cols.size());
    assertEquals("sum-age", cols.get(0).getId());
    assertEquals("count-name", cols.get(1).getId());
    assertEquals("dateDiff_seniorityStartTime,hireDate", cols.get(2).getId());
View Full Code Here

    assertStringArraysEqual(new String[]{"35.0", "1.0", "1180.0", "Eng"},
        resultStrings[4]);
  }

  public void testPivotingByScalarFunction() throws Exception {
    DataTable res = MockDataSource.getData(3);

    // select sum(age), count(dept) pivot hour(lunchtime)
    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));
    q.setSelection(selection);

    // Add pivot.
    QueryPivot pivot = new QueryPivot();
    pivot.addColumn(new ScalarFunctionColumn(
        Lists.newArrayList((AbstractColumn) new SimpleColumn("lunchTime")),
        TimeComponentExtractor.getInstance(
            TimeComponentExtractor.TimeComponent.HOUR)));
    q.setPivot(pivot);

    q.validate();
    DataTable result = QueryEngine.executeQuery(q, res.clone(), ULocale.US);
    // Test column description
    List<ColumnDescription> cols = result.getColumnDescriptions();

    assertEquals(4, cols.size());
    assertEquals("12.0 sum-age", cols.get(0).getId());
    assertEquals("13.0 sum-age", cols.get(1).getId());
    assertEquals("12.0 count-dept", cols.get(2).getId());
    assertEquals("13.0 count-dept", cols.get(3).getId());

    String[][] resultStrings = MockDataSource.queryResultToStringMatrix(result);
    assertEquals(1, resultStrings.length);

    assertStringArraysEqual(new String[]{"119.0", "54.0", "4.0", "2.0"},
        resultStrings[0]);

    // select count(dept) pivot hour(lunchtime), datediff(hireDate,
    // seniorityStartTime)
    Query q1 = new Query();

    // Add selection.
    QuerySelection selection1 = new QuerySelection();
    selection1.addColumn(new AggregationColumn(new SimpleColumn("dept"),
        AggregationType.COUNT));
    q1.setSelection(selection1);

    // Add pivot.
    QueryPivot pivot1 = new QueryPivot();
    pivot1.addColumn(new ScalarFunctionColumn(
        Lists.newArrayList((AbstractColumn) new SimpleColumn("lunchTime")),
        TimeComponentExtractor.getInstance(
            TimeComponentExtractor.TimeComponent.HOUR)));
    pivot1.addColumn(new ScalarFunctionColumn(
        Lists.<AbstractColumn>newArrayList(
            new SimpleColumn("seniorityStartTime"),
            new SimpleColumn("hireDate")), DateDiff.getInstance()));
    q1.setPivot(pivot1);

    q1.validate();

    DataTable result1 = QueryEngine.executeQuery(q1, res.clone(), ULocale.US);

    // Test column description
    List<ColumnDescription> cols1 = result1.getColumnDescriptions();

    assertEquals(5, cols1.size());
    assertEquals("12.0,null count-dept", cols1.get(0).getId());
    assertEquals("12.0,891.0 count-dept", cols1.get(1).getId());
    assertEquals("12.0,1180.0 count-dept", cols1.get(2).getId());
View Full Code Here

    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());
View Full Code Here

    assertStringArraysEqual(new String[]{"null", "1.0", "1180.0"},
        resultStrings[3]);
  }

  public void testPivotingByScalarFunctionWithGrouping() throws Exception {
    DataTable res = MockDataSource.getData(3);

    // select sum(age), count(dept) group by isSenior pivot hour(lunchtime)
    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 SimpleColumn("isSenior"));
    q.setSelection(selection);

    // Add Group.
    QueryGroup group = new QueryGroup();
    group.addColumn(new SimpleColumn("isSenior"));
    q.setGroup(group);

    // Add pivot.
    QueryPivot pivot = new QueryPivot();
    pivot.addColumn(new ScalarFunctionColumn(
        Lists.newArrayList((AbstractColumn) new SimpleColumn("lunchTime")),
        TimeComponentExtractor.getInstance(
            TimeComponentExtractor.TimeComponent.HOUR)));
    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("12.0 sum-age", cols.get(0).getId());
    assertEquals("13.0 sum-age", cols.get(1).getId());
    assertEquals("12.0 count-dept", cols.get(2).getId());
    assertEquals("13.0 count-dept", cols.get(3).getId());
    assertEquals("isSenior", cols.get(4).getId());

    String[][] resultStrings = MockDataSource.queryResultToStringMatrix(result);
    assertEquals(2, resultStrings.length);

    assertStringArraysEqual(new String[]{"52.0", "30.0", "2.0", "1.0",
        "false"}, resultStrings[0]);
    assertStringArraysEqual(new String[]{"67.0", "24.0", "2.0", "1.0",
        "true"}, resultStrings[1]);

    // selectcount(dept) group by isSenior pivot datediff(hireDate,
    // seniorityStartTime)
    q = new Query();
    res = MockDataSource.getData(3);

    // Add selection.
    selection = new QuerySelection();
    selection.addColumn(new AggregationColumn(new SimpleColumn("dept"),
        AggregationType.COUNT));
    selection.addColumn(new SimpleColumn("isSenior"));
    q.setSelection(selection);

    // Add Group.
    group = new QueryGroup();
    group.addColumn(new SimpleColumn("isSenior"));
    q.setGroup(group);

    // Add pivot.
    pivot = new QueryPivot();
    pivot.addColumn(new ScalarFunctionColumn(
        Lists.<AbstractColumn>newArrayList(
            new SimpleColumn("seniorityStartTime"),
            new SimpleColumn("hireDate")), DateDiff.getInstance()));
    q.setPivot(pivot);

    q.validate();

    result = QueryEngine.executeQuery(q, res, ULocale.US);

    // Test column description
    cols = result.getColumnDescriptions();

    assertEquals(5, cols.size());
    assertEquals("null count-dept", cols.get(0).getId());
    assertEquals("891.0 count-dept", cols.get(1).getId());
    assertEquals("1084.0 count-dept", cols.get(2).getId());
View Full Code Here

    assertStringArraysEqual(new String[]{"null", "1.0", "1.0", "1.0",
        "true"}, resultStrings[1]);
  }

  public void testGroupingAndPivotingByScalarFunction() throws Exception {
    DataTable res = MockDataSource.getData(3);

    // select count(dept), year(seniorityStartTime) group by
    // year(seniorityStartTime) pivot hour(lunchtime)
    Query q = new Query();

    // Add selection.
    QuerySelection selection = new QuerySelection();
    selection.addColumn(new AggregationColumn(new SimpleColumn("dept"),
        AggregationType.COUNT));
    selection.addColumn(new ScalarFunctionColumn(
        Lists.newArrayList((AbstractColumn)
            new SimpleColumn("seniorityStartTime")), TimeComponentExtractor.
        getInstance(TimeComponentExtractor.TimeComponent.YEAR)));

    q.setSelection(selection);

    // Add Group.
    QueryGroup group = new QueryGroup();
    group.addColumn(new ScalarFunctionColumn(Lists.newArrayList((AbstractColumn)
        new SimpleColumn("seniorityStartTime")), TimeComponentExtractor.
        getInstance(TimeComponentExtractor.TimeComponent.YEAR)));
    q.setGroup(group);

    // Add pivot.
    QueryPivot pivot = new QueryPivot();
    pivot.addColumn(new ScalarFunctionColumn(Lists.newArrayList(
        (AbstractColumn) new SimpleColumn("lunchTime")),
        TimeComponentExtractor.getInstance(
            TimeComponentExtractor.TimeComponent.HOUR)));
    q.setPivot(pivot);

    q.validate();

    DataTable result = QueryEngine.executeQuery(q, res, ULocale.US);

    // Test column description
    List<ColumnDescription> cols = result.getColumnDescriptions();

    assertEquals(3, cols.size());
    assertEquals("12.0 count-dept", cols.get(0).getId());
    assertEquals("13.0 count-dept", cols.get(1).getId());
    assertEquals("year_seniorityStartTime", cols.get(2).getId());
View Full Code Here

    assertStringArraysEqual(new String[]{"1.0", "1.0", "2007.0"},
        resultStrings[2]);
  }

  public void testSelectionOfScalarFunction() throws Exception {
    DataTable res = MockDataSource.getData(3);

    // select name, dept, hour(lunchTime)
    Query q = new Query();
    QuerySelection selection = new QuerySelection();
    selection.addColumn(new SimpleColumn("name"));
    selection.addColumn(new SimpleColumn("dept"));
    selection.addColumn(new ScalarFunctionColumn(
        Lists.newArrayList((AbstractColumn) new SimpleColumn("lunchTime")),
        TimeComponentExtractor.getInstance(
            TimeComponentExtractor.TimeComponent.HOUR)));

    q.setSelection(selection);
    q.validate();
    DataTable result = QueryEngine.executeQuery(q, res, ULocale.US);

    // Test column description
    List<ColumnDescription> cols = result.getColumnDescriptions();

    assertEquals(3, cols.size());
    assertEquals("name", cols.get(0).getId());
    assertEquals("dept", cols.get(1).getId());
    assertEquals("hour_lunchTime", cols.get(2).getId());
View Full Code Here

TOP

Related Classes of com.google.visualization.datasource.datatable.DataTable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.