use of org.apache.hyracks.dataflow.std.group.AggregateState in project asterixdb by apache.
the class SimpleAlgebricksAccumulatingAggregatorFactory method createAggregator.
@Override
public IAggregatorDescriptor createAggregator(final IHyracksTaskContext ctx, RecordDescriptor inRecordDesc, RecordDescriptor outRecordDescriptor, int[] aggKeys, int[] partialKeys) throws HyracksDataException {
return new IAggregatorDescriptor() {
private FrameTupleReference ftr = new FrameTupleReference();
private IPointable p = VoidPointable.FACTORY.createPointable();
@Override
public void init(ArrayTupleBuilder tupleBuilder, IFrameTupleAccessor accessor, int tIndex, AggregateState state) throws HyracksDataException {
IAggregateEvaluator[] agg = (IAggregateEvaluator[]) state.state;
// initialize aggregate functions
for (int i = 0; i < agg.length; i++) {
agg[i].init();
}
ftr.reset(accessor, tIndex);
for (int i = 0; i < agg.length; i++) {
agg[i].step(ftr);
}
}
@Override
public void aggregate(IFrameTupleAccessor accessor, int tIndex, IFrameTupleAccessor stateAccessor, int stateTupleIndex, AggregateState state) throws HyracksDataException {
IAggregateEvaluator[] agg = (IAggregateEvaluator[]) state.state;
ftr.reset(accessor, tIndex);
for (int i = 0; i < agg.length; i++) {
agg[i].step(ftr);
}
}
@Override
public boolean outputFinalResult(ArrayTupleBuilder tupleBuilder, IFrameTupleAccessor stateAccessor, int tIndex, AggregateState state) throws HyracksDataException {
IAggregateEvaluator[] agg = (IAggregateEvaluator[]) state.state;
for (int i = 0; i < agg.length; i++) {
agg[i].finish(p);
tupleBuilder.addField(p.getByteArray(), p.getStartOffset(), p.getLength());
}
return true;
}
@Override
public AggregateState createAggregateStates() throws HyracksDataException {
IAggregateEvaluator[] agg = new IAggregateEvaluator[aggFactories.length];
for (int i = 0; i < agg.length; i++) {
agg[i] = aggFactories[i].createAggregateEvaluator(ctx);
}
return new AggregateState(agg);
}
@Override
public void reset() {
}
@Override
public boolean outputPartialResult(ArrayTupleBuilder tupleBuilder, IFrameTupleAccessor stateAccessor, int tIndex, AggregateState state) throws HyracksDataException {
IAggregateEvaluator[] agg = (IAggregateEvaluator[]) state.state;
for (int i = 0; i < agg.length; i++) {
agg[i].finishPartial(p);
tupleBuilder.addField(p.getByteArray(), p.getStartOffset(), p.getLength());
}
return true;
}
@Override
public void close() {
}
};
}
Aggregations