Search in sources :

Example 11 with IPointable

use of org.apache.hyracks.data.std.api.IPointable in project asterixdb by apache.

the class StringRepeatDescriptor 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() {

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

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

                // Argument pointers.
                private IPointable argString = new VoidPointable();

                private IPointable argNumber = new VoidPointable();

                // For outputting the result.
                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

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

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    // Calls argument evaluators.
                    evalString.evaluate(tuple, argString);
                    evalStart.evaluate(tuple, argNumber);
                    // Gets the repeating times.
                    byte[] bytes = argNumber.getByteArray();
                    int offset = argNumber.getStartOffset();
                    int repeatingTimes = ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 1, bytes, offset);
                    // Checks repeatingTimes. It should be a non-negative value.
                    if (repeatingTimes < 0) {
                        throw new RuntimeDataException(ErrorCode.NEGATIVE_VALUE, getIdentifier(), 1, repeatingTimes);
                    }
                    // Gets the input string.
                    bytes = argString.getByteArray();
                    offset = argString.getStartOffset();
                    // Checks the type of the string argument.
                    if (bytes[offset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    }
                    // Calculates the result string length.
                    int inputLen = UTF8StringUtil.getUTFLength(bytes, offset + 1);
                    // Can throw overflow exception.
                    int resultLen = Math.multiplyExact(inputLen, repeatingTimes);
                    int cbytes = UTF8StringUtil.encodeUTF8Length(resultLen, tempLengthArray, 0);
                    // Writes the output string.
                    int inputStringStart = offset + 1 + UTF8StringUtil.getNumBytesToStoreLength(inputLen);
                    try {
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        out.write(tempLengthArray, 0, cbytes);
                        for (int numRepeats = 0; numRepeats < repeatingTimes; ++numRepeats) {
                            out.write(bytes, inputStringStart, inputLen);
                        }
                    } 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) 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 12 with IPointable

use of org.apache.hyracks.data.std.api.IPointable in project asterixdb by apache.

the class SubstringAfterDescriptor 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 array0 = new VoidPointable();

                private IPointable array1 = new VoidPointable();

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

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

                private final GrowableArray array = new GrowableArray();

                private final UTF8StringBuilder builder = new UTF8StringBuilder();

                private final UTF8StringPointable stringPtr = new UTF8StringPointable();

                private final UTF8StringPointable patternPtr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evalString.evaluate(tuple, array0);
                    byte[] src = array0.getByteArray();
                    int srcOffset = array0.getStartOffset();
                    int srcLen = array0.getLength();
                    evalPattern.evaluate(tuple, array1);
                    byte[] pattern = array1.getByteArray();
                    int patternOffset = array1.getStartOffset();
                    int patternLen = array1.getLength();
                    if (src[srcOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, src[srcOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    }
                    if (pattern[patternOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 1, pattern[patternOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    }
                    try {
                        stringPtr.set(src, srcOffset + 1, srcLen - 1);
                        patternPtr.set(pattern, patternOffset + 1, patternLen - 1);
                        array.reset();
                        UTF8StringPointable.substrAfter(stringPtr, patternPtr, builder, array);
                        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)

Example 13 with IPointable

use of org.apache.hyracks.data.std.api.IPointable in project asterixdb by apache.

the class UUIDDescriptor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        private final ISerializerDeserializer<AUUID> uuidSerDe = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AUUID);

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

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput output = resultStorage.getDataOutput();

                private final AGeneratedUUID uuid = new AGeneratedUUID();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    uuid.nextUUID();
                    resultStorage.reset();
                    uuidSerDe.serialize(uuid, output);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AGeneratedUUID(org.apache.asterix.om.base.AGeneratedUUID) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) 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) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 14 with IPointable

use of org.apache.hyracks.data.std.api.IPointable in project asterixdb by apache.

the class DateFromUnixTimeInDaysDescriptor 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 argPtr = new VoidPointable();

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

                private AMutableDate aDate = new AMutableDate(0);

                // possible returning types
                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ADate> dateSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval.evaluate(tuple, argPtr);
                    aDate.setValue(ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 0, argPtr.getByteArray(), argPtr.getStartOffset()));
                    dateSerde.serialize(aDate, out);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AMutableDate(org.apache.asterix.om.base.AMutableDate) 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) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 15 with IPointable

use of org.apache.hyracks.data.std.api.IPointable in project asterixdb by apache.

the class FirstElementEvalFactory method createAggregateEvaluator.

@Override
public IAggregateEvaluator createAggregateEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
    return new IAggregateEvaluator() {

        private boolean first = true;

        // Needs to copy the bytes from inputVal to outputVal because the byte space of inputVal could be re-used
        // by consequent tuples.
        private ArrayBackedValueStorage outputVal = new ArrayBackedValueStorage();

        private IPointable inputVal = new VoidPointable();

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

        private final byte[] nullBytes = new byte[] { ATypeTag.SERIALIZED_NULL_TYPE_TAG };

        private final byte[] systemNullBytes = new byte[] { ATypeTag.SERIALIZED_SYSTEM_NULL_TYPE_TAG };

        @Override
        public void init() throws HyracksDataException {
            first = true;
        }

        @Override
        public void step(IFrameTupleReference tuple) throws HyracksDataException {
            if (!first) {
                return;
            }
            eval.evaluate(tuple, inputVal);
            byte typeTagByte = inputVal.getByteArray()[inputVal.getStartOffset()];
            if (typeTagByte == ATypeTag.SERIALIZED_SYSTEM_NULL_TYPE_TAG) {
                // Ignores SYSTEM_NULLs generated by local-first-element.
                return;
            }
            outputVal.assign(inputVal);
            first = false;
        }

        @Override
        public void finish(IPointable result) throws HyracksDataException {
            if (first) {
                result.set(isLocal ? systemNullBytes : nullBytes, 0, 1);
                return;
            }
            result.set(outputVal);
        }

        @Override
        public void finishPartial(IPointable result) throws HyracksDataException {
            finish(result);
        }
    };
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) IAggregateEvaluator(org.apache.hyracks.algebricks.runtime.base.IAggregateEvaluator) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)

Aggregations

IPointable (org.apache.hyracks.data.std.api.IPointable)165 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)153 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)147 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)144 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)141 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)136 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)136 DataOutput (java.io.DataOutput)135 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)117 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)112 IOException (java.io.IOException)91 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)73 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