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();
}
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;
}
}
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);
}
}
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);
}
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);
}
}
}
Aggregations