Search in sources :

Example 1 with IUnnestingEvaluator

use of org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator in project asterixdb by apache.

the class RangeDescriptor method createUnnestingEvaluatorFactory.

@Override
public IUnnestingEvaluatorFactory createUnnestingEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IUnnestingEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

        @Override
        public IUnnestingEvaluator createUnnestingEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new IUnnestingEvaluator() {

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                @SuppressWarnings("rawtypes")
                private ISerializerDeserializer serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);

                private IPointable inputVal = new VoidPointable();

                private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);

                private IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);

                private AMutableInt64 aInt64 = new AMutableInt64(0);

                private long current;

                private long max;

                @Override
                public void init(IFrameTupleReference tuple) throws HyracksDataException {
                    eval0.evaluate(tuple, inputVal);
                    current = ATypeHierarchy.getLongValue(getIdentifier().getName(), 0, inputVal.getByteArray(), inputVal.getStartOffset());
                    eval1.evaluate(tuple, inputVal);
                    max = ATypeHierarchy.getLongValue(getIdentifier().getName(), 1, inputVal.getByteArray(), inputVal.getStartOffset());
                }

                @SuppressWarnings("unchecked")
                @Override
                public boolean step(IPointable result) throws HyracksDataException {
                    if (current > max) {
                        return false;
                    }
                    aInt64.setValue(current);
                    resultStorage.reset();
                    serde.serialize(aInt64, resultStorage.getDataOutput());
                    result.set(resultStorage);
                    current++;
                    return true;
                }
            };
        }
    };
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IUnnestingEvaluator(org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) IPointable(org.apache.hyracks.data.std.api.IPointable) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) IUnnestingEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluatorFactory) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)

Example 2 with IUnnestingEvaluator

use of org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator in project asterixdb by apache.

the class SubsetCollectionDescriptor method createUnnestingEvaluatorFactory.

@Override
public IUnnestingEvaluatorFactory createUnnestingEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IUnnestingEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

        @Override
        public IUnnestingEvaluator createUnnestingEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
            return new IUnnestingEvaluator() {

                private IPointable inputVal = new VoidPointable();

                private IScalarEvaluator evalList = args[0].createScalarEvaluator(ctx);

                private IScalarEvaluator evalStart = args[1].createScalarEvaluator(ctx);

                private IScalarEvaluator evalLen = args[2].createScalarEvaluator(ctx);

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private int numItems;

                private int numItemsMax;

                private int posStart;

                private int posCrt;

                private ATypeTag itemTag;

                private boolean selfDescList = false;

                private boolean metUnknown = false;

                @Override
                public void init(IFrameTupleReference tuple) throws HyracksDataException {
                    try {
                        evalStart.evaluate(tuple, inputVal);
                        posStart = ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 0, inputVal.getByteArray(), inputVal.getStartOffset());
                        evalLen.evaluate(tuple, inputVal);
                        numItems = ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 1, inputVal.getByteArray(), inputVal.getStartOffset());
                        evalList.evaluate(tuple, inputVal);
                        byte[] serList = inputVal.getByteArray();
                        int offset = inputVal.getStartOffset();
                        metUnknown = false;
                        byte typeTag = serList[offset];
                        if (typeTag == ATypeTag.SERIALIZED_MISSING_TYPE_TAG || typeTag == ATypeTag.SERIALIZED_NULL_TYPE_TAG) {
                            metUnknown = true;
                            return;
                        }
                        if (typeTag != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG && typeTag != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
                            throw new RuntimeDataException(ErrorCode.COERCION, getIdentifier());
                        }
                        if (typeTag == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
                            numItemsMax = AOrderedListSerializerDeserializer.getNumberOfItems(serList, offset);
                        } else {
                            numItemsMax = AUnorderedListSerializerDeserializer.getNumberOfItems(serList, offset);
                        }
                        itemTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serList[offset + 1]);
                        if (itemTag == ATypeTag.ANY) {
                            selfDescList = true;
                        }
                        posCrt = posStart;
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                }

                @Override
                public boolean step(IPointable result) throws HyracksDataException {
                    if (!metUnknown && posCrt < posStart + numItems && posCrt < numItemsMax) {
                        resultStorage.reset();
                        byte[] serList = inputVal.getByteArray();
                        int offset = inputVal.getStartOffset();
                        int itemLength = 0;
                        try {
                            int itemOffset = AOrderedListSerializerDeserializer.getItemOffset(serList, offset, posCrt);
                            if (selfDescList) {
                                itemTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serList[itemOffset]);
                            }
                            itemLength = NonTaggedFormatUtil.getFieldValueLength(serList, itemOffset, itemTag, selfDescList);
                            if (!selfDescList) {
                                resultStorage.getDataOutput().writeByte(itemTag.serialize());
                            }
                            resultStorage.getDataOutput().write(serList, itemOffset, itemLength + (!selfDescList ? 0 : 1));
                        } catch (IOException e) {
                            throw new HyracksDataException(e);
                        } catch (AsterixException e) {
                            throw new HyracksDataException(e);
                        }
                        result.set(resultStorage);
                        ++posCrt;
                        return true;
                    }
                    return false;
                }
            };
        }
    };
}
Also used : IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IUnnestingEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluatorFactory) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) IUnnestingEvaluator(org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator) ATypeTag(org.apache.asterix.om.types.ATypeTag) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 3 with IUnnestingEvaluator

use of org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator in project asterixdb by apache.

the class IntArrayUnnester method createUnnestingEvaluator.

@Override
public IUnnestingEvaluator createUnnestingEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
    final ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
    return new IUnnestingEvaluator() {

        private int pos;

        @Override
        public void init(IFrameTupleReference tuple) throws HyracksDataException {
            pos = 0;
        }

        @Override
        public boolean step(IPointable result) throws HyracksDataException {
            try {
                if (pos < x.length) {
                    // Writes one byte to distinguish between null
                    // values and end of sequence.
                    abvs.reset();
                    abvs.getDataOutput().writeInt(x[pos]);
                    result.set(abvs);
                    ++pos;
                    return true;
                } else {
                    return false;
                }
            } catch (IOException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IUnnestingEvaluator(org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 4 with IUnnestingEvaluator

use of org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator in project asterixdb by apache.

the class UnnestRuntimeFactory method createOneOutputPushRuntime.

@Override
public AbstractOneInputOneOutputOneFramePushRuntime createOneOutputPushRuntime(final IHyracksTaskContext ctx) throws HyracksDataException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutput output = new DataOutputStream(bos);
    if (missingWriterFactory != null) {
        IMissingWriter missingWriter = missingWriterFactory.createMissingWriter();
        missingWriter.writeMissing(output);
    }
    byte[] missingBytes = bos.toByteArray();
    int missingBytesLen = bos.size();
    return new AbstractOneInputOneOutputOneFramePushRuntime() {

        private IPointable p = VoidPointable.FACTORY.createPointable();

        private ArrayTupleBuilder tupleBuilder = new ArrayTupleBuilder(projectionList.length);

        private IUnnestingEvaluator unnest = unnestingFactory.createUnnestingEvaluator(ctx);

        @Override
        public void open() throws HyracksDataException {
            writer.open();
            if (tRef == null) {
                initAccessAppendRef(ctx);
            }
        }

        @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);
                try {
                    unnest.init(tRef);
                    unnesting(t);
                } catch (IOException ae) {
                    throw new HyracksDataException(ae);
                }
            }
        }

        private void unnesting(int t) throws IOException {
            // Assumes that when unnesting the tuple, each step() call for each element
            // in the tuple will increase the positionIndex, and the positionIndex will
            // be reset when a new tuple is to be processed.
            int positionIndex = 1;
            boolean emitted = false;
            do {
                if (!unnest.step(p)) {
                    break;
                }
                writeOutput(t, positionIndex++, false);
                emitted = true;
            } while (true);
            if (leftOuter && !emitted) {
                writeOutput(t, -1, true);
            }
        }

        private void writeOutput(int t, int positionIndex, boolean missing) throws HyracksDataException, IOException {
            if (!unnestColIsProjected && positionWriter == null) {
                appendProjectionToFrame(t, projectionList);
                appendToFrameFromTupleBuilder(tupleBuilder);
                return;
            }
            tupleBuilder.reset();
            for (int f = 0; f < outColPos; f++) {
                tupleBuilder.addField(tAccess, t, f);
            }
            if (unnestColIsProjected) {
                if (missing) {
                    tupleBuilder.addField(missingBytes, 0, missingBytesLen);
                } else {
                    tupleBuilder.addField(p.getByteArray(), p.getStartOffset(), p.getLength());
                }
            }
            for (int f = unnestColIsProjected ? outColPos + 1 : outColPos; f < (positionWriter != null ? projectionList.length - 1 : projectionList.length); f++) {
                tupleBuilder.addField(tAccess, t, f);
            }
            if (positionWriter != null) {
                // Write the positional variable
                if (missing) {
                    tupleBuilder.addField(missingBytes, 0, missingBytesLen);
                } else {
                    positionWriter.write(tupleBuilder.getDataOutput(), positionIndex);
                    tupleBuilder.addFieldEndOffset();
                }
            }
            appendToFrameFromTupleBuilder(tupleBuilder);
        }

        @Override
        public void flush() throws HyracksDataException {
            appender.flush(writer);
        }
    };
}
Also used : DataOutput(java.io.DataOutput) DataOutputStream(java.io.DataOutputStream) AbstractOneInputOneOutputOneFramePushRuntime(org.apache.hyracks.algebricks.runtime.operators.base.AbstractOneInputOneOutputOneFramePushRuntime) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IMissingWriter(org.apache.hyracks.api.dataflow.value.IMissingWriter) IUnnestingEvaluator(org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator)

Aggregations

IUnnestingEvaluator (org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluator)4 IPointable (org.apache.hyracks.data.std.api.IPointable)4 IOException (java.io.IOException)3 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)3 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)3 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)3 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)2 IUnnestingEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IUnnestingEvaluatorFactory)2 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)2 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutput (java.io.DataOutput)1 DataOutputStream (java.io.DataOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 AsterixException (org.apache.asterix.common.exceptions.AsterixException)1 RuntimeDataException (org.apache.asterix.common.exceptions.RuntimeDataException)1 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)1 ATypeTag (org.apache.asterix.om.types.ATypeTag)1 AbstractOneInputOneOutputOneFramePushRuntime (org.apache.hyracks.algebricks.runtime.operators.base.AbstractOneInputOneOutputOneFramePushRuntime)1 IMissingWriter (org.apache.hyracks.api.dataflow.value.IMissingWriter)1