Compute aggregation which is equivalent to SQL aggregation statement like
"SELECT a, b, sum(c), max(d) FROM e GROUP BY a, b"

@param fields a string includes aggregated fields and functions, e.g "a, b, sum(c), max(d)"
@return
@throws DDFException
543397
public AggregationResult aggregate(String fields) throws DDFException {
    return this.getAggregationHandler().aggregate(AggregateField.fromSqlFieldSpecs(fields));
  }
168113
@Override
  public void aggregate()
  {
    final IndexedInts keys = keySelector.getRow();
    for (int i = 0; i < valueSelectors.length; i++) {
      values[i] = valueSelectors[i].getDouble();
    }
    synchronized (this) {
      for (int i = 0, keysSize = keys.size(); i < keysSize; i++) {
        final String key = keySelector.lookupName(keys.get(i));
        sketch.update(key, values);
      }
    }
  }
395968
public Query groupby(final Collection<? extends Expression> groupbyColumns) {
        if (groupbyColumns == null) {
            return this;
        }

        this.groupbyColumns.addAll(groupbyColumns);

        return this;
    }
1326025
public function sum($field)
    {
        // prepare new query
        $q = $this->dsql()->del('fields')->del('order');

        // put field in array if it's not already
        if (!is_array($field)) {
            $field = array($field);
        }

        // add all fields to query
        foreach ($field as $f) {
            if (!is_object($f)) {
                $f = $this->getElement($f);
            }
            $q->field($q->sum($f), $f->short_name);
        }

        // return query
        return $q;
    }
59233
public function sum($field)
    {
        $field = $field instanceof Raw ? $field->toString() : "`{$field}`";
        return intval($this->select(new Raw("SUM({$field}) as `aggregation`"))->limit(1)->fetch()->aggregation);
    }
1298679
public static List<JBBPAbstractField> fieldsAsList(final JBBPAbstractField... fields) {
    final List<JBBPAbstractField> result = new ArrayList<>();
    Collections.addAll(result, fields);
    return result;
  }
543847
private AggregationBuilder buildSelectAggregations(AggregationBuilder aggregationBuilder,
            SelectStatement selectStatement, EntityMetadata entityMetadata)
    {
        Expression expression = ((SelectClause) selectStatement.getSelectClause()).getSelectExpression();

        if (expression instanceof CollectionExpression)
        {
            aggregationBuilder = appendAggregation((CollectionExpression) expression, entityMetadata,
                    aggregationBuilder);
        }
        else
        {
            if (isAggregationExpression(expression) && checkIfKeyExists(expression.toParsedText()))
                aggregationBuilder.subAggregation(getMetricsAggregation(expression, entityMetadata));
        }
        return aggregationBuilder;
    }
1369075
@TimerJ
    public TableMetadataBuilder withPartitionKey(String... fields) {
        for (String field : fields) {
            partitionKey.add(new ColumnName(tableName, field));

        }

        return this;
    }
1361823
public function aggregate(string $fn, string $field = '*', string $as = null): self
    {
        static $fns = ['count', 'sum', 'avg', 'min', 'max'];

        $fn = strtolower($fn);
        if (!in_array($fn, $fns)) {
            throw new BuilderException(sprintf('Invalid function %s given, %s are supported only!',
                $fn, join(',', $fns)));
        }

        $field = $field ?: '*';

        // if as not provided
        if ($as == null) {
            // aggregate('count', 'x') count_x
            // aggregate('count', 'u.x') count_ux
            $as = ($field && $field != '*') ? preg_replace('~[^\w]~', '', $fn .'_'. $field) : $fn;
        }

        return $this->push('select', sprintf('%s(%s) AS %s', $fn, $field, $this->agent->quoteField($as)));
    }
743810
public function aggregate($aggregate, $field, $force = false)
    {
        $this->parseOptions();

        $result = $this->connection->aggregate($this, $aggregate, $field);

        if (!empty($this->options['fetch_sql'])) {
            return $result;
        } elseif ($force) {
            $result = (float) $result;
        }

        return $result;
    }
277632
public static <Key, Value> Aggregation<Key, Value, Long> count() {
        return new AggregationAdapter(new CountAggregation<Key, Value>());
    }
34486
public int getAggregationDistinctColumnIndex(final int derivedSumIndex) {
        for (Entry<Integer, Integer> entry : aggregationDistinctColumnIndexAndSumColumnIndexes.entrySet()) {
            if (entry.getValue().equals(derivedSumIndex)) {
                return entry.getKey();
            }
        }
        throw new ShardingException("Can not get aggregation distinct column index.");
    }
809185
@Override
	public boolean hasField(String fldname) {
		if (groupFlds.contains(fldname))
			return true;
		if (aggFns != null)
			for (AggregationFn fn : aggFns)
				if (fn.fieldName().equals(fldname))
					return true;
		return false;
	}
36978
private CouchDBQueryInterpreter onAggregatedQuery(EntityMetadata m, CouchDBQueryInterpreter interpreter,
            KunderaQuery kunderaQuery)
    {
        interpreter.setAggregation(true);
        SelectStatement selectStatement = kunderaQuery.getSelectStatement();
        Expression whereClause = selectStatement.getWhereClause();
        if (!NullExpression.class.isAssignableFrom(whereClause.getClass()))
        {
            throw new KunderaException("Aggregations with where clause are yet not supported in CouchDB");
        }
        SelectClause selectClause = (SelectClause) selectStatement.getSelectClause();
        Expression expression = selectClause.getSelectExpression();
        if (CountFunction.class.isAssignableFrom(expression.getClass()))
        {
            interpreter.setAggregationType(CouchDBConstants.COUNT);
            Expression exp = ((CountFunction) expression).getExpression();
            setAggregationColInInterpreter(m, interpreter, exp);
        }
        else if (MinFunction.class.isAssignableFrom(expression.getClass()))
        {
            interpreter.setAggregationType(CouchDBConstants.MIN);
            Expression exp = ((MinFunction) expression).getExpression();
            setAggregationColInInterpreter(m, interpreter, exp);
        }
        else if (MaxFunction.class.isAssignableFrom(expression.getClass()))
        {
            interpreter.setAggregationType(CouchDBConstants.MAX);
            Expression exp = ((MaxFunction) expression).getExpression();
            setAggregationColInInterpreter(m, interpreter, exp);
        }
        else if (AvgFunction.class.isAssignableFrom(expression.getClass()))
        {
            interpreter.setAggregationType(CouchDBConstants.AVG);
            Expression exp = ((AvgFunction) expression).getExpression();
            setAggregationColInInterpreter(m, interpreter, exp);
        }
        else if (SumFunction.class.isAssignableFrom(expression.getClass()))
        {
            interpreter.setAggregationType(CouchDBConstants.SUM);
            Expression exp = ((SumFunction) expression).getExpression();
            setAggregationColInInterpreter(m, interpreter, exp);
        }
        else
        {
            throw new KunderaException("This query is currently not supported in CouchDB");
        }
        return interpreter;
    }
299670
public Stream partitionBy(Fields fields) {
        projectionValidation(fields);
        return partition(Grouping.fields(fields.toList()));
    }
300452
protected B fields(List<F> fields) {
        if (instance.def.fields == null) {
            instance.def.fields = new ArrayList<F>(fields.size());
        }
        instance.def.fields.addAll(fields);
        return returnThis();
    }
1279570
@SuppressWarnings({ "unchecked", "rawtypes" })
    public Table aggregate(ListMultimap<String, AggregateFunction<?,?>> functions) {
        Preconditions.checkArgument(!getSlices().isEmpty());
        Table groupTable = summaryTableName(sourceTable);
        StringColumn groupColumn = StringColumn.create("Group");
        groupTable.addColumns(groupColumn);
        for (Map.Entry<String, Collection<AggregateFunction<?,?>>> entry : functions.asMap().entrySet()) {
            String columnName = entry.getKey();
            int functionCount = 0;
            for (AggregateFunction function : entry.getValue()) {
                String colName = aggregateColumnName(columnName, function.functionName());
                ColumnType type = function.returnType();
                Column resultColumn = type.create(colName);
                for (TableSlice subTable : getSlices()) {
                    Object result = function.summarize(subTable.column(columnName));
                    if (functionCount == 0) {
                        groupColumn.append(subTable.name());
                    }
                    if (result instanceof Number) {
                        Number number = (Number) result;
                        resultColumn.append(number.doubleValue());
                    } else {
                        resultColumn.append(result);
                    }
                }
                groupTable.addColumns(resultColumn);
                functionCount++;
            }
        }
        return splitGroupingColumn(groupTable);
    }