Search in sources :

Example 1 with SingleAggregateFunctionVisitor

use of org.apache.phoenix.expression.visitor.SingleAggregateFunctionVisitor in project phoenix by apache.

the class AggregationManager method compile.

/**
     * Compiles projection by:
     * 1) Adding RowCount aggregate function if not present when limiting rows. We need this
     *    to track how many rows have been scanned.
     * 2) Reordering aggregation functions (by putting fixed length aggregates first) to
     *    optimize the positional access of the aggregated value.
     */
public void compile(StatementContext context, GroupByCompiler.GroupBy groupBy) throws SQLException {
    final Set<SingleAggregateFunction> aggFuncSet = Sets.newHashSetWithExpectedSize(context.getExpressionManager().getExpressionCount());
    Iterator<Expression> expressions = context.getExpressionManager().getExpressions();
    while (expressions.hasNext()) {
        Expression expression = expressions.next();
        expression.accept(new SingleAggregateFunctionVisitor() {

            @Override
            public Iterator<Expression> visitEnter(SingleAggregateFunction function) {
                aggFuncSet.add(function);
                return Iterators.emptyIterator();
            }
        });
    }
    if (aggFuncSet.isEmpty() && groupBy.isEmpty()) {
        return;
    }
    List<SingleAggregateFunction> aggFuncs = new ArrayList<SingleAggregateFunction>(aggFuncSet);
    Collections.sort(aggFuncs, SingleAggregateFunction.SCHEMA_COMPARATOR);
    int minNullableIndex = getMinNullableIndex(aggFuncs, groupBy.isEmpty());
    context.getScan().setAttribute(BaseScannerRegionObserver.AGGREGATORS, ServerAggregators.serialize(aggFuncs, minNullableIndex));
    ClientAggregators clientAggregators = new ClientAggregators(aggFuncs, minNullableIndex);
    context.getAggregationManager().setAggregators(clientAggregators);
}
Also used : ClientAggregators(org.apache.phoenix.expression.aggregator.ClientAggregators) Expression(org.apache.phoenix.expression.Expression) SingleAggregateFunctionVisitor(org.apache.phoenix.expression.visitor.SingleAggregateFunctionVisitor) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) SingleAggregateFunction(org.apache.phoenix.expression.function.SingleAggregateFunction)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Expression (org.apache.phoenix.expression.Expression)1 ClientAggregators (org.apache.phoenix.expression.aggregator.ClientAggregators)1 SingleAggregateFunction (org.apache.phoenix.expression.function.SingleAggregateFunction)1 SingleAggregateFunctionVisitor (org.apache.phoenix.expression.visitor.SingleAggregateFunctionVisitor)1