Package com.google.visualization.datasource.query

Examples of com.google.visualization.datasource.query.QueryGroup


        AggregationType.SUM));
    selection.addColumn(col3);
    q.setSelection(selection);

    // Add group
    QueryGroup group = new QueryGroup();
    group.addColumn(col3);
    q.setGroup(group);

    q.validate();

    DataTable result = QueryEngine.executeQuery(q, res, ULocale.US);
View Full Code Here


        new Constant(new NumberValue(7))));
    AbstractColumn selectedColumn = new ScalarFunctionColumn(columns, Quotient.getInstance());
    selection.addColumn(selectedColumn);

    query.setSelection(selection);
    QueryGroup group = new QueryGroup();
    group.addColumn(new SimpleColumn("Year"));
    query.setGroup(group);
    QueryPivot pivot = new QueryPivot();
    pivot.addColumn(new SimpleColumn("Band"));
    pivot.addColumn(new SimpleColumn("Songs"));
    query.setPivot(pivot);
View Full Code Here

  public void testGroupingAndPivoting() throws Exception {
    Query query = QueryBuilder.getInstance().parseQuery(
        " SELECT a, min(c1), b, avg(c2) group by a,b pivot  c,d");
    QuerySelection selection = query.getSelection();
    QueryGroup group = query.getGroup();
    QueryPivot pivot = query.getPivot();
    assertEquals(Lists.newArrayList(new SimpleColumn("a"),
        new AggregationColumn(new SimpleColumn("c1"), AggregationType.MIN),
        new SimpleColumn("b"),
        new AggregationColumn(new SimpleColumn("c2"), AggregationType.AVG)),
        selection.getColumns());
    assertEquals(Lists.newArrayList("a", "b"), group.getColumnIds());
    assertEquals(Lists.newArrayList("c", "d"), pivot.getColumnIds());
  }
View Full Code Here

  public void testStrangeAggregations() throws Exception {
    Query query = QueryBuilder.getInstance().parseQuery(
        " SELECT `a, b`, avg, min(min), max(max) group by `a, b`, avg "
            + " pivot count");
    QuerySelection selection = query.getSelection();
    QueryGroup group = query.getGroup();
    QueryPivot pivot = query.getPivot();
    assertEquals(Lists.newArrayList(new SimpleColumn("a, b"),
        new SimpleColumn("avg"),
        new AggregationColumn(new SimpleColumn("min"), AggregationType.MIN),
        new AggregationColumn(new SimpleColumn("max"), AggregationType.MAX)),
        selection.getColumns());
    assertEquals(Lists.newArrayList("a, b", "avg"), group.getColumnIds());
    assertEquals(Lists.newArrayList("count"), pivot.getColumnIds());
  }
View Full Code Here

  }

  public void testPivotAndGroupByArithmeticExpression() throws Exception {
    Query query = QueryBuilder.getInstance().parseQuery("select sum(c7) group "
        + "by ((c1+c2)*c3/c4) pivot `c5` -   `c6`");
    QueryGroup group = query.getGroup();
    QueryPivot pivot = query.getPivot();
    List<AbstractColumn> groupColumns = group.getColumns();
    assertEquals(1, groupColumns.size());
    List<AbstractColumn> pivotColumns = pivot.getColumns();
    assertEquals(1, pivotColumns.size());

    ScalarFunctionColumn col1 = new ScalarFunctionColumn(
View Full Code Here

      newGroupColumns.addAll(pivotColumns);
      if (dataSourceQuery.hasSelection()) {
        newSelectionColumns.addAll(dataSourceQuery.getSelection().getColumns());
      }
      newSelectionColumns.addAll(pivotColumns);
      QueryGroup group = new QueryGroup();
      for (AbstractColumn col : newGroupColumns) {
        group.addColumn(col);
      }
      dataSourceQuery.setGroup(group);
      QuerySelection selection = new QuerySelection();
      for (AbstractColumn col : newSelectionColumns) {
        selection.addColumn(col);
View Full Code Here

  static void appendGroupByClause(Query query, StrBuilder queryStringBuilder) {
    if (!query.hasGroup()) {
      return;
    }
    queryStringBuilder.append("GROUP BY ");
    QueryGroup queryGroup = query.getGroup();
    List<String> groupColumnIds = queryGroup.getColumnIds();
    List<String> newColumnIds = Lists.newArrayList();
    for (String groupColumnId : groupColumnIds) {
      newColumnIds.add('`' + groupColumnId + '`');
    }
    queryStringBuilder.appendWithSeparators(newColumnIds, ", ");
View Full Code Here

      ColumnIndices columnIndices, TreeMap<List<Value>, ColumnLookup> columnLookups)
      throws TypeMismatchException {
    if (!queryHasAggregation(query) || (table.getNumberOfRows() == 0)) {
      return table;
    }
    QueryGroup group = query.getGroup();
    QueryPivot pivot = query.getPivot();
    QuerySelection selection = query.getSelection();

    List<String> groupByIds = Lists.newArrayList();
    if (group != null) {
      groupByIds = group.getColumnIds();
    }

    List<String> pivotByIds = Lists.newArrayList();
    if (pivot != null) {
      pivotByIds = pivot.getColumnIds(); // contained in groupByIds
    }

    List<String> groupAndPivotIds = Lists.newArrayList(groupByIds);
    groupAndPivotIds.addAll(pivotByIds);

    List<AggregationColumn> tmpColumnAggregations = selection.getAggregationColumns();
    List<ScalarFunctionColumn> selectedScalarFunctionColumns = selection.getScalarFunctionColumns();
   
    // Remove duplicates from tmpColumnAggregations, creating columnAggregations:
    List<AggregationColumn> columnAggregations =
      Lists.newArrayListWithExpectedSize(tmpColumnAggregations.size());
    for (AggregationColumn aggCol : tmpColumnAggregations) {
      if (!columnAggregations.contains(aggCol)) {
        columnAggregations.add(aggCol);
      }
    }
   
    List<String> aggregationIds = Lists.newArrayList();
    for (AggregationColumn col : columnAggregations) {
      aggregationIds.add(col.getAggregatedColumn().getId());
    }

    List<ScalarFunctionColumn> groupAndPivotScalarFunctionColumns = Lists.newArrayList();
    if (group != null) {
      groupAndPivotScalarFunctionColumns.addAll(group.getScalarFunctionColumns());
    }
    if (pivot != null) {
      groupAndPivotScalarFunctionColumns.addAll(pivot.getScalarFunctionColumns());
    }

    List<ColumnDescription> newColumnDescriptions = Lists.newArrayList();
    newColumnDescriptions.addAll(table.getColumnDescriptions());

    // Add to the table description the scalar function columns included in the
    // group and pivot. The groups of rows are defined according to the
    // values of those columns, and so it is necessary to add them before the
    // calculations of the groups, pivots and aggregations.
    for (ScalarFunctionColumn column : groupAndPivotScalarFunctionColumns) {
      newColumnDescriptions.add(new ColumnDescription(column.getId(),
          column.getValueType(table),
          ScalarFunctionColumnTitle.getColumnDescriptionLabel(table, column)));
    }

    DataTable tempTable = new DataTable();
    tempTable.addColumns(newColumnDescriptions);

    // Calculate the values of the added scalar function columns in each row.
    DataTableColumnLookup lookup = new DataTableColumnLookup(table);
    for (TableRow sourceRow : table.getRows()) {
      TableRow newRow = new TableRow();
      for (TableCell sourceCell : sourceRow.getCells()) {
        newRow.addCell(sourceCell);
      }
      for (ScalarFunctionColumn column : groupAndPivotScalarFunctionColumns) {
        newRow.addCell(new TableCell(column.getValue(lookup, sourceRow)));
      }
      try {
        tempTable.addRow(newRow);
      } catch (TypeMismatchException e) {
        // Should not happen, given that the original table is OK.
      }
    }
    table = tempTable;

    // Calculate the aggregations.
    TableAggregator aggregator = new TableAggregator(groupAndPivotIds,
        Sets.newHashSet(aggregationIds), table);
    Set<AggregationPath> paths = aggregator.getPathsToLeaves();

    // These variables will hold the "titles" of the rows and columns.
    // They are TreeSets because their order matters.
    SortedSet<RowTitle> rowTitles =
        Sets.newTreeSet(GroupingComparators.ROW_TITLE_COMPARATOR);
    SortedSet<ColumnTitle> columnTitles = Sets.newTreeSet(
        GroupingComparators.getColumnTitleDynamicComparator(columnAggregations));

    // A tree set containing all pivot value lists (the set is for the
    // uniqueness and the tree for the order).
    TreeSet<List<Value>> pivotValuesSet =
        Sets.newTreeSet(GroupingComparators.VALUE_LIST_COMPARATOR);
    // This MetaTable holds all the data in the table, this data is then
    // dumped into the real table.
    MetaTable metaTable = new MetaTable();
    for (AggregationColumn columnAggregation : columnAggregations) {
      for (AggregationPath path : paths) {

        // A ColumnTitle is composed of all the values for the pivot-by
        // columns, and a ColumnAggregation. That is why it is necessary to iterate over all
        // ColumnAggregations and create a ColumnTitle for each one.
        List<Value> originalValues = path.getValues();

        // Separate originalValues into the rowValues and columnValues. The
        // rowValues are the values of the group-by columns and the columnValues
        // are the values of the pivot-by columns.
        List<Value> rowValues = originalValues.subList(0, groupByIds.size());
        RowTitle rowTitle = new RowTitle(rowValues);
        rowTitles.add(rowTitle);

        List<Value> columnValues = originalValues.subList(groupByIds.size(), originalValues.size());
        pivotValuesSet.add(columnValues);

        ColumnTitle columnTitle = new ColumnTitle(columnValues,
            columnAggregation, (columnAggregations.size() > 1));
        columnTitles.add(columnTitle);
        metaTable.put(rowTitle, columnTitle, new TableCell(aggregator.getAggregationValue(path,
            columnAggregation.getAggregatedColumn().getId(),
            columnAggregation.getAggregationType())));
      }
    }

    // Create the scalar function column titles for the scalar function columns
    // that contain aggregations.
    List<ScalarFunctionColumnTitle> scalarFunctionColumnTitles =
        Lists.newArrayList();
    for (ScalarFunctionColumn scalarFunctionColumn :
        selectedScalarFunctionColumns) {
      if (scalarFunctionColumn.getAllAggregationColumns().size() != 0) {
        for (List<Value> columnValues : pivotValuesSet) {
          scalarFunctionColumnTitles.add(new ScalarFunctionColumnTitle(columnValues,
              scalarFunctionColumn));
        }
      }
    }

    // Create the new table description.
    DataTable result = createDataTable(groupByIds, columnTitles, table, scalarFunctionColumnTitles);
    List<ColumnDescription> colDescs = result.getColumnDescriptions();

    // Fill the columnIndices and columnLookups parameters for the group-by
    // columns and the aggregation columns.
    columnIndices.clear();
    int columnIndex = 0;
    if (group != null) {
      List<Value> empytListOfValues = Lists.newArrayList();
      columnLookups.put(empytListOfValues, new GenericColumnLookup());
      for (AbstractColumn column : group.getColumns()) {
        columnIndices.put(column, columnIndex);
        if (!(column instanceof ScalarFunctionColumn)) {
          ((GenericColumnLookup) columnLookups.get(empytListOfValues)).put(column, columnIndex);
          for (List<Value> columnValues : pivotValuesSet) {
            if (!columnLookups.containsKey(columnValues)) {
View Full Code Here

    format.addPattern(new SimpleColumn("A"), "foo");
    q.setUserFormatOptions(format);
    QueryOptions options = new QueryOptions();
    options.setNoFormat(true);
    q.setOptions(options);
    QueryGroup group = new QueryGroup();
    group.addColumn(new SimpleColumn("A"));
    q.setGroup(group);
    QueryPivot pivot = new QueryPivot();
    pivot.addColumn(new SimpleColumn("C"));
    q.setPivot(pivot);
  }
View Full Code Here

  static void appendGroupByClause(Query query, StrBuilder queryStringBuilder) {
    if (!query.hasGroup()) {
      return;
    }
    queryStringBuilder.append("GROUP BY ");
    QueryGroup queryGroup = query.getGroup();
    List<String> groupColumnIds = queryGroup.getColumnIds();
    List<String> newColumnIds = Lists.newArrayList();
    for (String groupColumnId : groupColumnIds) {
      newColumnIds.add('`' + groupColumnId + '`');
    }
    queryStringBuilder.appendWithSeparators(newColumnIds, ", ");
View Full Code Here

TOP

Related Classes of com.google.visualization.datasource.query.QueryGroup

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.