Search in sources :

Example 11 with IFrameTupleAccessor

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;
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AggregateState(org.apache.hyracks.dataflow.std.group.AggregateState) IFrameTupleAccessor(org.apache.hyracks.api.comm.IFrameTupleAccessor) IOException(java.io.IOException) IFieldAggregateDescriptor(org.apache.hyracks.dataflow.std.group.IFieldAggregateDescriptor) ByteBuffer(java.nio.ByteBuffer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 12 with IFrameTupleAccessor

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();
}
Also used : IFrame(org.apache.hyracks.api.comm.IFrame) IFrameTupleAccessor(org.apache.hyracks.api.comm.IFrameTupleAccessor) ByteBuffer(java.nio.ByteBuffer) VSizeFrame(org.apache.hyracks.api.comm.VSizeFrame)

Example 13 with IFrameTupleAccessor

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;
        }
    };
}
Also used : IBinaryHashFunction(org.apache.hyracks.api.dataflow.value.IBinaryHashFunction) IFrameTupleAccessor(org.apache.hyracks.api.comm.IFrameTupleAccessor) ITuplePartitionComputer(org.apache.hyracks.api.dataflow.value.ITuplePartitionComputer)

Example 14 with IFrameTupleAccessor

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);
        }
    };
}
Also used : AbstractOneInputOneOutputOneFramePushRuntime(org.apache.hyracks.algebricks.runtime.operators.base.AbstractOneInputOneOutputOneFramePushRuntime) IFrameTupleAccessor(org.apache.hyracks.api.comm.IFrameTupleAccessor) FrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.FrameTupleReference) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) IPointable(org.apache.hyracks.data.std.api.IPointable) ByteBuffer(java.nio.ByteBuffer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 15 with IFrameTupleAccessor

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);
        }
    };
}
Also used : AbstractOneInputOneOutputOneFramePushRuntime(org.apache.hyracks.algebricks.runtime.operators.base.AbstractOneInputOneOutputOneFramePushRuntime) IFrameTupleAccessor(org.apache.hyracks.api.comm.IFrameTupleAccessor) FrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.FrameTupleReference) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) IPointable(org.apache.hyracks.data.std.api.IPointable) ByteBuffer(java.nio.ByteBuffer)

Aggregations

IFrameTupleAccessor (org.apache.hyracks.api.comm.IFrameTupleAccessor)31 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)12 AggregateState (org.apache.hyracks.dataflow.std.group.AggregateState)11 DataOutput (java.io.DataOutput)10 ArrayTupleBuilder (org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder)10 IOException (java.io.IOException)8 ByteBuffer (java.nio.ByteBuffer)8 VSizeFrame (org.apache.hyracks.api.comm.VSizeFrame)7 IFieldAggregateDescriptor (org.apache.hyracks.dataflow.std.group.IFieldAggregateDescriptor)7 FrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.FrameTupleReference)6 IAggregatorDescriptor (org.apache.hyracks.dataflow.std.group.IAggregatorDescriptor)5 FrameTupleAccessor (org.apache.hyracks.dataflow.common.comm.io.FrameTupleAccessor)4 FrameTupleAppender (org.apache.hyracks.dataflow.common.comm.io.FrameTupleAppender)4 IFrame (org.apache.hyracks.api.comm.IFrame)3 ITuplePartitionComputer (org.apache.hyracks.api.dataflow.value.ITuplePartitionComputer)3 RecordDescriptor (org.apache.hyracks.api.dataflow.value.RecordDescriptor)3 FrameManager (org.apache.hyracks.control.nc.resources.memory.FrameManager)3 IPointable (org.apache.hyracks.data.std.api.IPointable)3 ByteBufferInputStream (org.apache.hyracks.dataflow.common.comm.util.ByteBufferInputStream)3 Test (org.junit.Test)3