Search in sources :

Example 16 with IFrameTupleAccessor

use of org.apache.hyracks.api.comm.IFrameTupleAccessor 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() {
        }
    };
}
Also used : AggregateState(org.apache.hyracks.dataflow.std.group.AggregateState) IFrameTupleAccessor(org.apache.hyracks.api.comm.IFrameTupleAccessor) FrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.FrameTupleReference) IAggregatorDescriptor(org.apache.hyracks.dataflow.std.group.IAggregatorDescriptor) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) IAggregateEvaluator(org.apache.hyracks.algebricks.runtime.base.IAggregateEvaluator) IPointable(org.apache.hyracks.data.std.api.IPointable)

Example 17 with IFrameTupleAccessor

use of org.apache.hyracks.api.comm.IFrameTupleAccessor in project asterixdb by apache.

the class SerializedDataWriterFactory method createWriter.

@Override
public IAWriter createWriter(final int[] fields, final PrintStream ps, IPrinterFactory[] printerFactories, final RecordDescriptor inputRecordDescriptor) {
    return new IAWriter() {

        @Override
        public void init() throws HyracksDataException {
            // dump the SerializerDeserializers to disk
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(inputRecordDescriptor);
                baos.writeTo(ps);
                oos.close();
            } catch (IOException e) {
                throw new HyracksDataException(e);
            }
        }

        @Override
        public void printTuple(IFrameTupleAccessor tAccess, int tIdx) throws HyracksDataException {
            for (int i = 0; i < fields.length; i++) {
                int fldStart = tAccess.getTupleStartOffset(tIdx) + tAccess.getFieldSlotsLength() + tAccess.getFieldStartOffset(tIdx, fields[i]);
                int fldLen = tAccess.getFieldLength(tIdx, fields[i]);
                ps.write(tAccess.getBuffer().array(), fldStart, fldLen);
            }
        }
    };
}
Also used : IAWriter(org.apache.hyracks.algebricks.data.IAWriter) IFrameTupleAccessor(org.apache.hyracks.api.comm.IFrameTupleAccessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 18 with IFrameTupleAccessor

use of org.apache.hyracks.api.comm.IFrameTupleAccessor 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;
            }
        }
    };
}
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 19 with IFrameTupleAccessor

use of org.apache.hyracks.api.comm.IFrameTupleAccessor 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;
            }
        }
    };
}
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 20 with IFrameTupleAccessor

use of org.apache.hyracks.api.comm.IFrameTupleAccessor 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();
        }
    };
}
Also used : DataOutput(java.io.DataOutput) IOException(java.io.IOException) IFieldAggregateDescriptor(org.apache.hyracks.dataflow.std.group.IFieldAggregateDescriptor) UTF8StringSerializerDeserializer(org.apache.hyracks.dataflow.common.data.marshalling.UTF8StringSerializerDeserializer) DataInputStream(java.io.DataInputStream) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) AggregateState(org.apache.hyracks.dataflow.std.group.AggregateState) ByteArrayInputStream(java.io.ByteArrayInputStream) IFrameTupleAccessor(org.apache.hyracks.api.comm.IFrameTupleAccessor)

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