Search in sources :

Example 1 with SingleAggregateFunction

use of org.apache.phoenix.expression.function.SingleAggregateFunction in project phoenix by apache.

the class Aggregators method toString.

@Override
public String toString() {
    StringBuilder buf = new StringBuilder(this.getClass().getName() + " [" + functions.length + "]:");
    for (int i = 0; i < functions.length; i++) {
        SingleAggregateFunction function = functions[i];
        buf.append("\t" + i + ") " + function);
    }
    return buf.toString();
}
Also used : SingleAggregateFunction(org.apache.phoenix.expression.function.SingleAggregateFunction)

Example 2 with SingleAggregateFunction

use of org.apache.phoenix.expression.function.SingleAggregateFunction in project phoenix by apache.

the class TestUtil method getSingleSumAggregator.

public static ClientAggregators getSingleSumAggregator(String url, Properties props) throws SQLException {
    try (PhoenixConnection pconn = DriverManager.getConnection(url, props).unwrap(PhoenixConnection.class)) {
        PhoenixStatement statement = new PhoenixStatement(pconn);
        StatementContext context = new StatementContext(statement, null, new Scan(), new SequenceManager(statement));
        AggregationManager aggregationManager = context.getAggregationManager();
        SumAggregateFunction func = new SumAggregateFunction(Arrays.<Expression>asList(new KeyValueColumnExpression(new PLongColumn() {

            @Override
            public PName getName() {
                return SINGLE_COLUMN_NAME;
            }

            @Override
            public PName getFamilyName() {
                return SINGLE_COLUMN_FAMILY_NAME;
            }

            @Override
            public int getPosition() {
                return 0;
            }

            @Override
            public SortOrder getSortOrder() {
                return SortOrder.getDefault();
            }

            @Override
            public Integer getArraySize() {
                return 0;
            }

            @Override
            public byte[] getViewConstant() {
                return null;
            }

            @Override
            public boolean isViewReferenced() {
                return false;
            }

            @Override
            public String getExpressionStr() {
                return null;
            }

            @Override
            public boolean isRowTimestamp() {
                return false;
            }

            @Override
            public boolean isDynamic() {
                return false;
            }

            @Override
            public byte[] getColumnQualifierBytes() {
                return SINGLE_COLUMN_NAME.getBytes();
            }
        })), null);
        aggregationManager.setAggregators(new ClientAggregators(Collections.<SingleAggregateFunction>singletonList(func), 1));
        ClientAggregators aggregators = aggregationManager.getAggregators();
        return aggregators;
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ClientAggregators(org.apache.phoenix.expression.aggregator.ClientAggregators) SortOrder(org.apache.phoenix.schema.SortOrder) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) SequenceManager(org.apache.phoenix.compile.SequenceManager) StatementContext(org.apache.phoenix.compile.StatementContext) AggregationManager(org.apache.phoenix.compile.AggregationManager) PName(org.apache.phoenix.schema.PName) SumAggregateFunction(org.apache.phoenix.expression.function.SumAggregateFunction) Scan(org.apache.hadoop.hbase.client.Scan) PLongColumn(org.apache.phoenix.schema.PLongColumn) SingleAggregateFunction(org.apache.phoenix.expression.function.SingleAggregateFunction) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression)

Example 3 with SingleAggregateFunction

use of org.apache.phoenix.expression.function.SingleAggregateFunction in project phoenix by apache.

the class SpillManager method getAggregators.

// Instantiate Aggregators from a serialized byte array
private Aggregator[] getAggregators(byte[] data) throws IOException {
    DataInputStream input = null;
    try {
        input = new DataInputStream(new ByteArrayInputStream(data));
        // key length
        int keyLength = WritableUtils.readVInt(input);
        int vIntKeyLength = WritableUtils.getVIntSize(keyLength);
        ImmutableBytesPtr ptr = new ImmutableBytesPtr(data, vIntKeyLength, keyLength);
        // value length
        input.skip(keyLength);
        int valueLength = WritableUtils.readVInt(input);
        int vIntValLength = WritableUtils.getVIntSize(keyLength);
        KeyValue keyValue = KeyValueUtil.newKeyValue(ptr.get(), ptr.getOffset(), ptr.getLength(), QueryConstants.SINGLE_COLUMN_FAMILY, QueryConstants.SINGLE_COLUMN, QueryConstants.AGG_TIMESTAMP, data, vIntKeyLength + keyLength + vIntValLength, valueLength);
        Tuple result = new SingleKeyValueTuple(keyValue);
        TupleUtil.getAggregateValue(result, ptr);
        KeyValueSchema schema = aggregators.getValueSchema();
        ValueBitSet tempValueSet = ValueBitSet.newInstance(schema);
        tempValueSet.clear();
        tempValueSet.or(ptr);
        int i = 0, maxOffset = ptr.getOffset() + ptr.getLength();
        SingleAggregateFunction[] funcArray = aggregators.getFunctions();
        Aggregator[] sAggs = new Aggregator[funcArray.length];
        Boolean hasValue;
        schema.iterator(ptr);
        while ((hasValue = schema.next(ptr, i, maxOffset, tempValueSet)) != null) {
            SingleAggregateFunction func = funcArray[i];
            sAggs[i++] = hasValue ? func.newServerAggregator(conf, ptr) : func.newServerAggregator(conf);
        }
        return sAggs;
    } finally {
        Closeables.closeQuietly(input);
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) ValueBitSet(org.apache.phoenix.schema.ValueBitSet) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) Aggregator(org.apache.phoenix.expression.aggregator.Aggregator) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SingleAggregateFunction(org.apache.phoenix.expression.function.SingleAggregateFunction) KeyValueSchema(org.apache.phoenix.schema.KeyValueSchema) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple)

Example 4 with SingleAggregateFunction

use of org.apache.phoenix.expression.function.SingleAggregateFunction 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 Collections.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)

Example 5 with SingleAggregateFunction

use of org.apache.phoenix.expression.function.SingleAggregateFunction in project phoenix by apache.

the class ServerAggregators method deserialize.

/**
 * Deserialize aggregators from the serialized byte array representation
 * @param b byte array representation of a list of Aggregators
 * @param conf Server side configuration used by HBase
 * @return newly instantiated Aggregators instance
 */
public static ServerAggregators deserialize(byte[] b, Configuration conf) {
    if (b == null) {
        return ServerAggregators.EMPTY_AGGREGATORS;
    }
    ByteArrayInputStream stream = new ByteArrayInputStream(b);
    try {
        DataInputStream input = new DataInputStream(stream);
        int minNullableIndex = WritableUtils.readVInt(input);
        int len = WritableUtils.readVInt(input);
        Aggregator[] aggregators = new Aggregator[len];
        Expression[] expressions = new Expression[len];
        SingleAggregateFunction[] functions = new SingleAggregateFunction[len];
        for (int i = 0; i < aggregators.length; i++) {
            SingleAggregateFunction aggFunc = (SingleAggregateFunction) ExpressionType.values()[WritableUtils.readVInt(input)].newInstance();
            aggFunc.readFields(input, conf);
            functions[i] = aggFunc;
            aggregators[i] = aggFunc.getAggregator();
            expressions[i] = aggFunc.getAggregatorExpression();
        }
        return new ServerAggregators(functions, aggregators, expressions, minNullableIndex);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Expression(org.apache.phoenix.expression.Expression) SingleAggregateFunction(org.apache.phoenix.expression.function.SingleAggregateFunction) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Aggregations

SingleAggregateFunction (org.apache.phoenix.expression.function.SingleAggregateFunction)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 IOException (java.io.IOException)2 Expression (org.apache.phoenix.expression.Expression)2 ClientAggregators (org.apache.phoenix.expression.aggregator.ClientAggregators)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 KeyValue (org.apache.hadoop.hbase.KeyValue)1 Scan (org.apache.hadoop.hbase.client.Scan)1 AggregationManager (org.apache.phoenix.compile.AggregationManager)1 SequenceManager (org.apache.phoenix.compile.SequenceManager)1 StatementContext (org.apache.phoenix.compile.StatementContext)1 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)1 Aggregator (org.apache.phoenix.expression.aggregator.Aggregator)1 SumAggregateFunction (org.apache.phoenix.expression.function.SumAggregateFunction)1 SingleAggregateFunctionVisitor (org.apache.phoenix.expression.visitor.SingleAggregateFunctionVisitor)1 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)1