use of org.apache.hyracks.api.comm.IFrameTupleAccessor 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.api.comm.IFrameTupleAccessor in project asterixdb by apache.
the class ResultPrinter method print.
public void print(ResultReader resultReader) throws HyracksDataException {
printPrefix();
final IFrameTupleAccessor fta = resultReader.getFrameTupleAccessor();
final IFrame frame = new VSizeFrame(resultDisplayFrameMgr);
while (resultReader.read(frame) > 0) {
final ByteBuffer frameBuffer = frame.getBuffer();
final byte[] frameBytes = frameBuffer.array();
fta.reset(frameBuffer);
final int last = fta.getTupleCount();
for (int tIndex = 0; tIndex < last; tIndex++) {
final int start = fta.getTupleStartOffset(tIndex);
int length = fta.getTupleEndOffset(tIndex) - start;
if (conf.fmt() == SessionConfig.OutputFormat.CSV && ((length > 0) && (frameBytes[start + length - 1] == '\n'))) {
length--;
}
String result = new String(frameBytes, start, length, UTF_8);
if (wrapArray && notFirst) {
output.out().print(", ");
}
notFirst = true;
displayRecord(result);
}
frameBuffer.clear();
}
printPostfix();
}
use of org.apache.hyracks.api.comm.IFrameTupleAccessor in project asterixdb by apache.
the class FieldHashPartitionComputerFactory method createPartitioner.
@Override
public ITuplePartitionComputer createPartitioner() {
final IBinaryHashFunction[] hashFunctions = new IBinaryHashFunction[hashFunctionFactories.length];
for (int i = 0; i < hashFunctionFactories.length; ++i) {
hashFunctions[i] = hashFunctionFactories[i].createBinaryHashFunction();
}
return new ITuplePartitionComputer() {
@Override
public int partition(IFrameTupleAccessor accessor, int tIndex, int nParts) throws HyracksDataException {
if (nParts == 1) {
return 0;
}
int h = 0;
int startOffset = accessor.getTupleStartOffset(tIndex);
int slotLength = accessor.getFieldSlotsLength();
for (int j = 0; j < hashFields.length; ++j) {
int fIdx = hashFields[j];
IBinaryHashFunction hashFn = hashFunctions[j];
int fStart = accessor.getFieldStartOffset(tIndex, fIdx);
int fEnd = accessor.getFieldEndOffset(tIndex, fIdx);
int fh = hashFn.hash(accessor.getBuffer().array(), startOffset + slotLength + fStart, fEnd - fStart);
h = h * 31 + fh;
}
if (h < 0) {
h = -(h + 1);
}
return h % nParts;
}
};
}
use of org.apache.hyracks.api.comm.IFrameTupleAccessor in project asterixdb by apache.
the class AssignRuntimeFactory method createOneOutputPushRuntime.
@Override
public AbstractOneInputOneOutputOneFramePushRuntime createOneOutputPushRuntime(final IHyracksTaskContext ctx) throws HyracksDataException {
final int[] projectionToOutColumns = new int[projectionList.length];
for (int j = 0; j < projectionList.length; j++) {
projectionToOutColumns[j] = Arrays.binarySearch(outColumns, projectionList[j]);
}
return new AbstractOneInputOneOutputOneFramePushRuntime() {
private IPointable result = VoidPointable.FACTORY.createPointable();
private IScalarEvaluator[] eval = new IScalarEvaluator[evalFactories.length];
private ArrayTupleBuilder tupleBuilder = new ArrayTupleBuilder(projectionList.length);
private boolean first = true;
private boolean isOpen = false;
private int tupleIndex = 0;
@Override
public void open() throws HyracksDataException {
if (first) {
initAccessAppendRef(ctx);
first = false;
int n = evalFactories.length;
for (int i = 0; i < n; i++) {
eval[i] = evalFactories[i].createScalarEvaluator(ctx);
}
}
isOpen = true;
writer.open();
}
@Override
public void close() throws HyracksDataException {
if (isOpen) {
super.close();
}
}
@Override
public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
// what if nTuple is 0?
tAccess.reset(buffer);
int nTuple = tAccess.getTupleCount();
if (nTuple < 1) {
if (nTuple < 0) {
throw new HyracksDataException("Negative number of tuples in the frame: " + nTuple);
}
appender.flush(writer);
} else {
if (nTuple > 1) {
for (; tupleIndex < nTuple - 1; tupleIndex++) {
tRef.reset(tAccess, tupleIndex);
produceTuple(tupleBuilder, tAccess, tupleIndex, tRef);
appendToFrameFromTupleBuilder(tupleBuilder);
}
}
if (tupleIndex < nTuple) {
tRef.reset(tAccess, tupleIndex);
produceTuple(tupleBuilder, tAccess, tupleIndex, tRef);
if (flushFramesRapidly) {
// Whenever all the tuples in the incoming frame have been consumed, the assign operator
// will push its frame to the next operator; i.e., it won't wait until the frame gets full.
appendToFrameFromTupleBuilder(tupleBuilder, true);
} else {
appendToFrameFromTupleBuilder(tupleBuilder);
}
} else {
if (flushFramesRapidly) {
flushAndReset();
}
}
}
tupleIndex = 0;
}
private void produceTuple(ArrayTupleBuilder tb, IFrameTupleAccessor accessor, int tIndex, FrameTupleReference tupleRef) throws HyracksDataException {
try {
tb.reset();
for (int f = 0; f < projectionList.length; f++) {
int k = projectionToOutColumns[f];
if (k >= 0) {
eval[k].evaluate(tupleRef, result);
tb.addField(result.getByteArray(), result.getStartOffset(), result.getLength());
} else {
tb.addField(accessor, tIndex, projectionList[f]);
}
}
} catch (HyracksDataException e) {
throw HyracksDataException.create(ErrorCode.ERROR_PROCESSING_TUPLE, e, tupleIndex);
}
}
@Override
public void fail() throws HyracksDataException {
if (isOpen) {
super.fail();
}
}
@Override
public void flush() throws HyracksDataException {
appender.flush(writer);
}
};
}
use of org.apache.hyracks.api.comm.IFrameTupleAccessor in project asterixdb by apache.
the class RunningAggregateRuntimeFactory method createOneOutputPushRuntime.
@Override
public AbstractOneInputOneOutputOneFramePushRuntime createOneOutputPushRuntime(final IHyracksTaskContext ctx) throws HyracksDataException {
final int[] projectionToOutColumns = new int[projectionList.length];
for (int j = 0; j < projectionList.length; j++) {
projectionToOutColumns[j] = Arrays.binarySearch(outColumns, projectionList[j]);
}
return new AbstractOneInputOneOutputOneFramePushRuntime() {
private final IPointable p = VoidPointable.FACTORY.createPointable();
private final IRunningAggregateEvaluator[] raggs = new IRunningAggregateEvaluator[runningAggregates.length];
private final ArrayTupleBuilder tupleBuilder = new ArrayTupleBuilder(projectionList.length);
private boolean first = true;
private boolean isOpen = false;
@Override
public void open() throws HyracksDataException {
initAccessAppendRef(ctx);
if (first) {
first = false;
int n = runningAggregates.length;
for (int i = 0; i < n; i++) {
raggs[i] = runningAggregates[i].createRunningAggregateEvaluator(ctx);
}
}
for (int i = 0; i < runningAggregates.length; i++) {
raggs[i].init();
}
isOpen = true;
writer.open();
}
@Override
public void close() throws HyracksDataException {
if (isOpen) {
super.close();
}
}
@Override
public void fail() throws HyracksDataException {
if (isOpen) {
super.fail();
}
}
@Override
public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
tAccess.reset(buffer);
int nTuple = tAccess.getTupleCount();
for (int t = 0; t < nTuple; t++) {
tRef.reset(tAccess, t);
produceTuple(tupleBuilder, tAccess, t, tRef);
appendToFrameFromTupleBuilder(tupleBuilder);
}
}
private void produceTuple(ArrayTupleBuilder tb, IFrameTupleAccessor accessor, int tIndex, FrameTupleReference tupleRef) throws HyracksDataException {
tb.reset();
for (int f = 0; f < projectionList.length; f++) {
int k = projectionToOutColumns[f];
if (k >= 0) {
raggs[k].step(tupleRef, p);
tb.addField(p.getByteArray(), p.getStartOffset(), p.getLength());
} else {
tb.addField(accessor, tIndex, projectionList[f]);
}
}
}
@Override
public void flush() throws HyracksDataException {
appender.flush(writer);
}
};
}
Aggregations