Search in sources :

Example 91 with IFrameTupleReference

use of org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference in project asterixdb by apache.

the class StringJoinDescriptor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final ListAccessor listAccessor = new ListAccessor();

                private final DataOutput out = resultStorage.getDataOutput();

                private final IScalarEvaluatorFactory listEvalFactory = args[0];

                private final IScalarEvaluatorFactory sepEvalFactory = args[1];

                private final IPointable inputArgList = new VoidPointable();

                private final IPointable inputArgSep = new VoidPointable();

                private final IScalarEvaluator evalList = listEvalFactory.createScalarEvaluator(ctx);

                private final IScalarEvaluator evalSep = sepEvalFactory.createScalarEvaluator(ctx);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ANull> nullSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL);

                private ISerializerDeserializer<AMissing> missingSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AMISSING);

                private final byte[] tempLengthArray = new byte[5];

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evalList.evaluate(tuple, inputArgList);
                    evalSep.evaluate(tuple, inputArgSep);
                    byte[] listBytes = inputArgList.getByteArray();
                    int listOffset = inputArgList.getStartOffset();
                    if (listBytes[listOffset] != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG && listBytes[listOffset] != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, listBytes[listOffset], ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG, ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG);
                    }
                    byte[] sepBytes = inputArgSep.getByteArray();
                    int sepOffset = inputArgSep.getStartOffset();
                    if (sepBytes[sepOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 1, sepBytes[sepOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    }
                    int sepLen = UTF8StringUtil.getUTFLength(sepBytes, sepOffset + 1);
                    int sepMetaLen = UTF8StringUtil.getNumBytesToStoreLength(sepLen);
                    listAccessor.reset(listBytes, listOffset);
                    try {
                        // calculate length first
                        int utf8Len = 0;
                        int size = listAccessor.size();
                        for (int i = 0; i < size; i++) {
                            int itemOffset = listAccessor.getItemOffset(i);
                            ATypeTag itemType = listAccessor.getItemType(itemOffset);
                            // since the item itself has a typetag.
                            if (listAccessor.itemsAreSelfDescribing()) {
                                itemOffset += 1;
                            }
                            if (itemType != ATypeTag.STRING) {
                                if (itemType == ATypeTag.NULL) {
                                    nullSerde.serialize(ANull.NULL, out);
                                    result.set(resultStorage);
                                    return;
                                }
                                if (itemType == ATypeTag.MISSING) {
                                    missingSerde.serialize(AMissing.MISSING, out);
                                    result.set(resultStorage);
                                    return;
                                }
                                throw new UnsupportedItemTypeException(getIdentifier(), itemType.serialize());
                            }
                            int currentSize = UTF8StringUtil.getUTFLength(listBytes, itemOffset);
                            if (i != size - 1 && currentSize != 0) {
                                utf8Len += sepLen;
                            }
                            utf8Len += currentSize;
                        }
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        int cbytes = UTF8StringUtil.encodeUTF8Length(utf8Len, tempLengthArray, 0);
                        out.write(tempLengthArray, 0, cbytes);
                        for (int i = 0; i < listAccessor.size(); i++) {
                            int itemOffset = listAccessor.getItemOffset(i);
                            if (listAccessor.itemsAreSelfDescribing()) {
                                itemOffset += 1;
                            }
                            utf8Len = UTF8StringUtil.getUTFLength(listBytes, itemOffset);
                            out.write(listBytes, UTF8StringUtil.getNumBytesToStoreLength(utf8Len) + itemOffset, utf8Len);
                            for (int j = 0; j < sepLen; j++) {
                                out.writeByte(sepBytes[sepOffset + 1 + sepMetaLen + j]);
                            }
                        }
                    } catch (IOException | AsterixException ex) {
                        throw new HyracksDataException(ex);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ListAccessor(org.apache.asterix.runtime.evaluators.common.ListAccessor) UnsupportedItemTypeException(org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) 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)

Example 92 with IFrameTupleReference

use of org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference in project asterixdb by apache.

the class SleepDescriptor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private IPointable argTime = new VoidPointable();

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

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

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    evalValue.evaluate(tuple, result);
                    evalTime.evaluate(tuple, argTime);
                    final byte[] bytes = argTime.getByteArray();
                    final int offset = argTime.getStartOffset();
                    final long time = ATypeHierarchy.getLongValue(getIdentifier().getName(), 1, bytes, offset);
                    try {
                        if (LOGGER.isLoggable(Level.INFO)) {
                            LOGGER.log(Level.INFO, ctx.getTaskAttemptId() + " sleeping for " + time + " ms");
                        }
                        Thread.sleep(time);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    } finally {
                        if (LOGGER.isLoggable(Level.INFO)) {
                            LOGGER.log(Level.INFO, ctx.getTaskAttemptId() + " done sleeping for " + time + " ms");
                        }
                    }
                }
            };
        }
    };
}
Also used : 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) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 93 with IFrameTupleReference

use of org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference in project asterixdb by apache.

the class Substring2Descriptor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable argString = new VoidPointable();

                private IPointable argStart = new VoidPointable();

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

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

                private final GrowableArray array = new GrowableArray();

                private final UTF8StringBuilder builder = new UTF8StringBuilder();

                private final UTF8StringPointable string = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evalString.evaluate(tuple, argString);
                    evalStart.evaluate(tuple, argStart);
                    byte[] bytes = argStart.getByteArray();
                    int offset = argStart.getStartOffset();
                    int start = ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 1, bytes, offset) - 1;
                    bytes = argString.getByteArray();
                    offset = argString.getStartOffset();
                    int len = argString.getLength();
                    if (bytes[offset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    }
                    string.set(bytes, offset + 1, len - 1);
                    array.reset();
                    try {
                        UTF8StringPointable.substr(string, start, Integer.MAX_VALUE, builder, array);
                    } catch (StringIndexOutOfBoundsException e) {
                        throw new RuntimeDataException(ErrorCode.OUT_OF_BOUND, getIdentifier(), 1, start);
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    try {
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        out.write(array.getByteArray(), 0, array.getLength());
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) GrowableArray(org.apache.hyracks.data.std.util.GrowableArray) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) UTF8StringBuilder(org.apache.hyracks.data.std.util.UTF8StringBuilder) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) 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 94 with IFrameTupleReference

use of org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference in project asterixdb by apache.

the class LenDescriptor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                private final IPointable inputVal = new VoidPointable();

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

                // result
                private final AMutableInt64 res = new AMutableInt64(0);

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<AInt64> int64Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evalList.evaluate(tuple, inputVal);
                    byte[] serList = inputVal.getByteArray();
                    int offset = inputVal.getStartOffset();
                    if (serList[offset] != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG && serList[offset] != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, serList[offset], ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG, ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG);
                    }
                    int numberOfitems = 0;
                    if (serList[offset] == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
                        numberOfitems = AOrderedListSerializerDeserializer.getNumberOfItems(serList, offset);
                    } else {
                        numberOfitems = AUnorderedListSerializerDeserializer.getNumberOfItems(serList, offset);
                    }
                    res.setValue(numberOfitems);
                    try {
                        int64Serde.serialize(res, out);
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 95 with IFrameTupleReference

use of org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference in project asterixdb by apache.

the class InjectFailureDescriptor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            final IPointable argPtr = new VoidPointable();
            final IScalarEvaluator[] evals = new IScalarEvaluator[args.length];
            evals[0] = args[0].createScalarEvaluator(ctx);
            evals[1] = args[1].createScalarEvaluator(ctx);
            return new IScalarEvaluator() {

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    evals[1].evaluate(tuple, argPtr);
                    ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(argPtr.getByteArray()[argPtr.getStartOffset()]);
                    if (typeTag == ATypeTag.BOOLEAN) {
                        boolean argResult = ABooleanSerializerDeserializer.getBoolean(argPtr.getByteArray(), argPtr.getStartOffset() + 1);
                        if (argResult) {
                            LOGGER.log(Level.SEVERE, ctx.getTaskAttemptId() + " injecting failure");
                            throw new RuntimeDataException(ErrorCode.INJECTED_FAILURE, getIdentifier());
                        }
                    }
                    // evaluate the real evaluator
                    evals[0].evaluate(tuple, argPtr);
                    result.set(argPtr.getByteArray(), argPtr.getStartOffset(), argPtr.getLength());
                }
            };
        }
    };
}
Also used : 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) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Aggregations

IPointable (org.apache.hyracks.data.std.api.IPointable)153 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)153 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)144 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)143 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)139 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)134 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)134 DataOutput (java.io.DataOutput)133 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)117 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)112 IOException (java.io.IOException)90 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)70 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)48 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)37 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)26 ATypeTag (org.apache.asterix.om.types.ATypeTag)25 AsterixException (org.apache.asterix.common.exceptions.AsterixException)15 GregorianCalendarSystem (org.apache.asterix.om.base.temporal.GregorianCalendarSystem)13 AMutableDateTime (org.apache.asterix.om.base.AMutableDateTime)11 AMutableDouble (org.apache.asterix.om.base.AMutableDouble)11