use of org.apache.hyracks.dataflow.std.group.IFieldAggregateDescriptor in project asterixdb by apache.
the class AvgFieldGroupAggregatorFactory method createAggregator.
/*
* (non-Javadoc)
*
* @see org.apache.hyracks.dataflow.std.aggregations.
* IFieldAggregateDescriptorFactory
* #createAggregator(org.apache.hyracks.api.context.IHyracksTaskContext,
* org.apache.hyracks.api.dataflow.value.RecordDescriptor,
* org.apache.hyracks.api.dataflow.value.RecordDescriptor)
*/
@Override
public IFieldAggregateDescriptor createAggregator(IHyracksTaskContext ctx, RecordDescriptor inRecordDescriptor, RecordDescriptor outRecordDescriptor) throws HyracksDataException {
return new IFieldAggregateDescriptor() {
@Override
public void reset() {
}
@Override
public void outputPartialResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int sum, count;
if (!useObjectState) {
sum = IntegerPointable.getInteger(data, offset);
count = IntegerPointable.getInteger(data, offset + 4);
} else {
Integer[] fields = (Integer[]) state.state;
sum = fields[0];
count = fields[1];
}
try {
fieldOutput.writeInt(sum);
fieldOutput.writeInt(count);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing aggregation to the output buffer.");
}
}
@Override
public void outputFinalResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int sum, count;
if (!useObjectState) {
sum = IntegerPointable.getInteger(data, offset);
count = IntegerPointable.getInteger(data, offset + 4);
} else {
Integer[] fields = (Integer[]) state.state;
sum = fields[0];
count = fields[1];
}
try {
fieldOutput.writeFloat((float) sum / count);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing aggregation to the output buffer.");
}
}
@Override
public void init(IFrameTupleAccessor accessor, int tIndex, DataOutput fieldOutput, AggregateState state) throws HyracksDataException {
int sum = 0;
int count = 0;
int tupleOffset = accessor.getTupleStartOffset(tIndex);
int fieldStart = accessor.getFieldStartOffset(tIndex, aggField);
sum += IntegerPointable.getInteger(accessor.getBuffer().array(), tupleOffset + accessor.getFieldSlotsLength() + fieldStart);
count += 1;
if (!useObjectState) {
try {
fieldOutput.writeInt(sum);
fieldOutput.writeInt(count);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when initializing the aggregator.");
}
} else {
state.state = new Integer[] { sum, count };
}
}
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public void aggregate(IFrameTupleAccessor accessor, int tIndex, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int sum = 0, count = 0;
int tupleOffset = accessor.getTupleStartOffset(tIndex);
int fieldStart = accessor.getFieldStartOffset(tIndex, aggField);
sum += IntegerPointable.getInteger(accessor.getBuffer().array(), tupleOffset + accessor.getFieldSlotsLength() + fieldStart);
count += 1;
if (!useObjectState) {
ByteBuffer buf = ByteBuffer.wrap(data);
sum += buf.getInt(offset);
count += buf.getInt(offset + 4);
buf.putInt(offset, sum);
buf.putInt(offset + 4, count);
} else {
Integer[] fields = (Integer[]) state.state;
sum += fields[0];
count += fields[1];
state.state = new Integer[] { sum, count };
}
}
@Override
public boolean needsObjectState() {
return useObjectState;
}
@Override
public boolean needsBinaryState() {
return !useObjectState;
}
@Override
public AggregateState createState() {
return new AggregateState(new Integer[] { 0, 0 });
}
};
}
use of org.apache.hyracks.dataflow.std.group.IFieldAggregateDescriptor in project asterixdb by apache.
the class CountFieldAggregatorFactory method createAggregator.
/*
* (non-Javadoc)
*
* @see org.apache.hyracks.dataflow.std.aggregations.
* IFieldAggregateDescriptorFactory
* #createAggregator(org.apache.hyracks.api.context.IHyracksTaskContext,
* org.apache.hyracks.api.dataflow.value.RecordDescriptor,
* org.apache.hyracks.api.dataflow.value.RecordDescriptor)
*/
@Override
public IFieldAggregateDescriptor createAggregator(IHyracksTaskContext ctx, RecordDescriptor inRecordDescriptor, RecordDescriptor outRecordDescriptor) throws HyracksDataException {
return new IFieldAggregateDescriptor() {
@Override
public void reset() {
}
@Override
public void outputPartialResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int count;
if (!useObjectState) {
count = IntegerPointable.getInteger(data, offset);
} else {
count = (Integer) state.state;
}
try {
fieldOutput.writeInt(count);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing aggregation to the output buffer.");
}
}
@Override
public void outputFinalResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int count;
if (!useObjectState) {
count = IntegerPointable.getInteger(data, offset);
} else {
count = (Integer) state.state;
}
try {
fieldOutput.writeInt(count);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing aggregation to the output buffer.");
}
}
@Override
public void init(IFrameTupleAccessor accessor, int tIndex, DataOutput fieldOutput, AggregateState state) throws HyracksDataException {
int count = 1;
if (!useObjectState) {
try {
fieldOutput.writeInt(count);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when initializing the aggregator.");
}
} else {
state.state = count;
}
}
public boolean needsObjectState() {
return useObjectState;
}
public boolean needsBinaryState() {
return !useObjectState;
}
public AggregateState createState() {
return new AggregateState(new Integer(0));
}
@Override
public void close() {
}
@Override
public void aggregate(IFrameTupleAccessor accessor, int tIndex, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int count = 1;
if (!useObjectState) {
ByteBuffer buf = ByteBuffer.wrap(data);
count += buf.getInt(offset);
buf.putInt(offset, count);
} else {
count += (Integer) state.state;
state.state = count;
}
}
};
}
use of org.apache.hyracks.dataflow.std.group.IFieldAggregateDescriptor in project asterixdb by apache.
the class FloatSumFieldAggregatorFactory method createAggregator.
/* (non-Javadoc)
* @see org.apache.hyracks.dataflow.std.group.IFieldAggregateDescriptorFactory#createAggregator(org.apache.hyracks.api.context.IHyracksTaskContext, org.apache.hyracks.api.dataflow.value.RecordDescriptor, org.apache.hyracks.api.dataflow.value.RecordDescriptor)
*/
@Override
public IFieldAggregateDescriptor createAggregator(IHyracksTaskContext ctx, RecordDescriptor inRecordDescriptor, RecordDescriptor outRecordDescriptor) throws HyracksDataException {
return new IFieldAggregateDescriptor() {
@Override
public void reset() {
}
@Override
public void outputPartialResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
float sum;
if (!useObjectState) {
sum = FloatPointable.getFloat(data, offset);
} else {
sum = (Float) state.state;
}
try {
fieldOutput.writeFloat(sum);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing aggregation to the output buffer.");
}
}
@Override
public void outputFinalResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
float sum;
if (!useObjectState) {
sum = FloatPointable.getFloat(data, offset);
} else {
sum = (Float) state.state;
}
try {
fieldOutput.writeFloat(sum);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing aggregation to the output buffer.");
}
}
@Override
public boolean needsObjectState() {
return useObjectState;
}
@Override
public boolean needsBinaryState() {
return !useObjectState;
}
@Override
public void init(IFrameTupleAccessor accessor, int tIndex, DataOutput fieldOutput, AggregateState state) throws HyracksDataException {
float sum = 0;
int tupleOffset = accessor.getTupleStartOffset(tIndex);
int fieldStart = accessor.getFieldStartOffset(tIndex, aggField);
sum += FloatPointable.getFloat(accessor.getBuffer().array(), tupleOffset + accessor.getFieldSlotsLength() + fieldStart);
if (!useObjectState) {
try {
fieldOutput.writeFloat(sum);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when initializing the aggregator.");
}
} else {
state.state = sum;
}
}
@Override
public AggregateState createState() {
return new AggregateState(new Float(0.0));
}
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public void aggregate(IFrameTupleAccessor accessor, int tIndex, byte[] data, int offset, AggregateState state) throws HyracksDataException {
float sum = 0;
int tupleOffset = accessor.getTupleStartOffset(tIndex);
int fieldStart = accessor.getFieldStartOffset(tIndex, aggField);
sum += FloatPointable.getFloat(accessor.getBuffer().array(), tupleOffset + accessor.getFieldSlotsLength() + fieldStart);
if (!useObjectState) {
ByteBuffer buf = ByteBuffer.wrap(data);
sum += buf.getFloat(offset);
buf.putFloat(offset, sum);
} else {
sum += (Float) state.state;
state.state = sum;
}
}
};
}
use of org.apache.hyracks.dataflow.std.group.IFieldAggregateDescriptor in project asterixdb by apache.
the class IntSumFieldAggregatorFactory method createAggregator.
/*
* (non-Javadoc)
*
* @see org.apache.hyracks.dataflow.std.aggregations.
* IFieldAggregateDescriptorFactory
* #createAggregator(org.apache.hyracks.api.context.IHyracksTaskContext,
* org.apache.hyracks.api.dataflow.value.RecordDescriptor,
* org.apache.hyracks.api.dataflow.value.RecordDescriptor)
*/
@Override
public IFieldAggregateDescriptor createAggregator(IHyracksTaskContext ctx, RecordDescriptor inRecordDescriptor, RecordDescriptor outRecordDescriptor) throws HyracksDataException {
return new IFieldAggregateDescriptor() {
@Override
public void reset() {
}
@Override
public void outputPartialResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int sum;
if (!useObjectState) {
sum = IntegerPointable.getInteger(data, offset);
} else {
sum = (Integer) state.state;
}
try {
fieldOutput.writeInt(sum);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing aggregation to the output buffer.");
}
}
@Override
public void outputFinalResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int sum;
if (!useObjectState) {
sum = IntegerPointable.getInteger(data, offset);
} else {
sum = (Integer) state.state;
}
try {
fieldOutput.writeInt(sum);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing aggregation to the output buffer.");
}
}
@Override
public void init(IFrameTupleAccessor accessor, int tIndex, DataOutput fieldOutput, AggregateState state) throws HyracksDataException {
int sum = 0;
int tupleOffset = accessor.getTupleStartOffset(tIndex);
int fieldStart = accessor.getFieldStartOffset(tIndex, aggField);
sum += IntegerPointable.getInteger(accessor.getBuffer().array(), tupleOffset + accessor.getFieldSlotsLength() + fieldStart);
if (!useObjectState) {
try {
fieldOutput.writeInt(sum);
} catch (IOException e) {
throw new HyracksDataException("I/O exception when initializing the aggregator.");
}
} else {
state.state = sum;
}
}
public boolean needsObjectState() {
return useObjectState;
}
public boolean needsBinaryState() {
return !useObjectState;
}
public AggregateState createState() {
return new AggregateState(new Integer(0));
}
@Override
public void close() {
}
@Override
public void aggregate(IFrameTupleAccessor accessor, int tIndex, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int sum = 0;
int tupleOffset = accessor.getTupleStartOffset(tIndex);
int fieldStart = accessor.getFieldStartOffset(tIndex, aggField);
sum += IntegerPointable.getInteger(accessor.getBuffer().array(), tupleOffset + accessor.getFieldSlotsLength() + fieldStart);
if (!useObjectState) {
ByteBuffer buf = ByteBuffer.wrap(data);
sum += buf.getInt(offset);
buf.putInt(offset, sum);
} else {
sum += (Integer) state.state;
state.state = sum;
}
}
};
}
use of org.apache.hyracks.dataflow.std.group.IFieldAggregateDescriptor in project asterixdb by apache.
the class MinMaxStringFieldAggregatorFactory method createAggregator.
/*
* (non-Javadoc)
*
* @see
* org.apache.hyracks.dataflow.std.aggregators.IAggregatorDescriptorFactory
* #createAggregator(org.apache.hyracks.api.context.IHyracksTaskContext,
* org.apache.hyracks.api.dataflow.value.RecordDescriptor,
* org.apache.hyracks.api.dataflow.value.RecordDescriptor, int[])
*/
@Override
public IFieldAggregateDescriptor createAggregator(IHyracksTaskContext ctx, RecordDescriptor inRecordDescriptor, RecordDescriptor outRecordDescriptor) throws HyracksDataException {
return new IFieldAggregateDescriptor() {
UTF8StringSerializerDeserializer utf8SerializerDeserializer = new UTF8StringSerializerDeserializer();
@Override
public void reset() {
}
@Override
public void outputPartialResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
try {
if (hasBinaryState) {
int stateIdx = IntegerPointable.getInteger(data, offset);
Object[] storedState = (Object[]) state.state;
fieldOutput.writeUTF((String) storedState[stateIdx]);
} else {
fieldOutput.writeUTF((String) state.state);
}
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing a string to the output writer in MinMaxStringAggregatorFactory.");
}
}
@Override
public void outputFinalResult(DataOutput fieldOutput, byte[] data, int offset, AggregateState state) throws HyracksDataException {
try {
if (hasBinaryState) {
int stateIdx = IntegerPointable.getInteger(data, offset);
Object[] storedState = (Object[]) state.state;
fieldOutput.writeUTF((String) storedState[stateIdx]);
} else {
fieldOutput.writeUTF((String) state.state);
}
} catch (IOException e) {
throw new HyracksDataException("I/O exception when writing a string to the output writer in MinMaxStringAggregatorFactory.");
}
}
@Override
public void init(IFrameTupleAccessor accessor, int tIndex, DataOutput fieldOutput, AggregateState state) throws HyracksDataException {
int tupleOffset = accessor.getTupleStartOffset(tIndex);
int fieldStart = accessor.getFieldStartOffset(tIndex, aggField);
int fieldLength = accessor.getFieldLength(tIndex, aggField);
String strField = utf8SerializerDeserializer.deserialize(new DataInputStream(new ByteArrayInputStream(accessor.getBuffer().array(), tupleOffset + accessor.getFieldSlotsLength() + fieldStart, fieldLength)));
if (hasBinaryState) {
// Object-binary-state
Object[] storedState;
if (state.state == null) {
storedState = new Object[8];
storedState[0] = new Integer(0);
state.state = storedState;
} else {
storedState = (Object[]) state.state;
}
int stateCount = (Integer) (storedState[0]);
if (stateCount + 1 >= storedState.length) {
storedState = Arrays.copyOf(storedState, storedState.length * 2);
state.state = storedState;
}
stateCount++;
storedState[0] = stateCount;
storedState[stateCount] = strField;
try {
fieldOutput.writeInt(stateCount);
} catch (IOException e) {
throw new HyracksDataException(e.fillInStackTrace());
}
} else {
// Only object-state
state.state = strField;
}
}
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public void aggregate(IFrameTupleAccessor accessor, int tIndex, byte[] data, int offset, AggregateState state) throws HyracksDataException {
int tupleOffset = accessor.getTupleStartOffset(tIndex);
int fieldStart = accessor.getFieldStartOffset(tIndex, aggField);
int fieldLength = accessor.getFieldLength(tIndex, aggField);
String strField = utf8SerializerDeserializer.deserialize(new DataInputStream(new ByteArrayInputStream(accessor.getBuffer().array(), tupleOffset + accessor.getFieldSlotsLength() + fieldStart, fieldLength)));
if (hasBinaryState) {
int stateIdx = IntegerPointable.getInteger(data, offset);
Object[] storedState = (Object[]) state.state;
if (isMax) {
if (strField.length() > ((String) (storedState[stateIdx])).length()) {
storedState[stateIdx] = strField;
}
} else {
if (strField.length() < ((String) (storedState[stateIdx])).length()) {
storedState[stateIdx] = strField;
}
}
} else {
if (isMax) {
if (strField.length() > ((String) (state.state)).length()) {
state.state = strField;
}
} else {
if (strField.length() < ((String) (state.state)).length()) {
state.state = strField;
}
}
}
}
public boolean needsObjectState() {
return true;
}
public boolean needsBinaryState() {
return hasBinaryState;
}
public AggregateState createState() {
return new AggregateState();
}
};
}
Aggregations