Search in sources :

Example 1 with AMutableInt16

use of org.apache.asterix.om.base.AMutableInt16 in project asterixdb by apache.

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

                private IPointable argPrecision = new VoidPointable();

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

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

                private AMutableDouble aDouble = new AMutableDouble(0);

                private AMutableFloat aFloat = new AMutableFloat(0);

                private AMutableInt64 aInt64 = new AMutableInt64(0);

                private AMutableInt32 aInt32 = new AMutableInt32(0);

                private AMutableInt16 aInt16 = new AMutableInt16((short) 0);

                private AMutableInt8 aInt8 = new AMutableInt8((byte) 0);

                @SuppressWarnings("rawtypes")
                private ISerializerDeserializer serde;

                private int getPrecision() throws HyracksDataException {
                    byte[] bytes = argPrecision.getByteArray();
                    int offset = argPrecision.getStartOffset();
                    if (bytes[offset] == ATypeTag.SERIALIZED_INT8_TYPE_TAG) {
                        return AInt8SerializerDeserializer.getByte(bytes, offset + 1);
                    } else if (bytes[offset] == ATypeTag.SERIALIZED_INT16_TYPE_TAG) {
                        return AInt16SerializerDeserializer.getShort(bytes, offset + 1);
                    } else if (bytes[offset] == ATypeTag.SERIALIZED_INT32_TYPE_TAG) {
                        return AInt32SerializerDeserializer.getInt(bytes, offset + 1);
                    } else if (bytes[offset] == ATypeTag.SERIALIZED_INT64_TYPE_TAG) {
                        return (int) AInt64SerializerDeserializer.getLong(bytes, offset + 1);
                    } else {
                        throw new TypeMismatchException(getIdentifier(), 1, bytes[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG);
                    }
                }

                @SuppressWarnings("unchecked")
                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval.evaluate(tuple, argValue);
                    precision.evaluate(tuple, argPrecision);
                    byte[] data = argValue.getByteArray();
                    int offset = argValue.getStartOffset();
                    if (data[offset] == ATypeTag.SERIALIZED_INT8_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT8);
                        byte val = AInt8SerializerDeserializer.getByte(data, offset + 1);
                        aInt8.setValue(val);
                        serde.serialize(aInt8, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_INT16_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT16);
                        short val = AInt16SerializerDeserializer.getShort(data, offset + 1);
                        aInt16.setValue(val);
                        serde.serialize(aInt16, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_INT32_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
                        int val = AInt32SerializerDeserializer.getInt(data, offset + 1);
                        aInt32.setValue(val);
                        serde.serialize(aInt32, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_INT64_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
                        long val = AInt64SerializerDeserializer.getLong(data, offset + 1);
                        aInt64.setValue(val);
                        serde.serialize(aInt64, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_FLOAT_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);
                        float val = AFloatSerializerDeserializer.getFloat(data, offset + 1);
                        if (Float.isNaN(val) || Float.isInfinite(val) || Float.compare(val, -0.0F) == 0 || Float.compare(val, 0.0F) == 0) {
                            aFloat.setValue(val);
                            serde.serialize(aFloat, out);
                        } else {
                            BigDecimal r = new BigDecimal(Float.toString(val));
                            aFloat.setValue(r.setScale(getPrecision(), BigDecimal.ROUND_HALF_EVEN).floatValue());
                            serde.serialize(aFloat, out);
                        }
                    } else if (data[offset] == ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
                        double val = ADoubleSerializerDeserializer.getDouble(data, offset + 1);
                        if (Double.isNaN(val) || Double.isInfinite(val) || Double.compare(val, -0.0D) == 0 || Double.compare(val, 0.0D) == 0) {
                            aDouble.setValue(val);
                            serde.serialize(aDouble, out);
                        } else {
                            BigDecimal r = new BigDecimal(Double.toString(val));
                            aDouble.setValue(r.setScale(getPrecision(), BigDecimal.ROUND_HALF_EVEN).doubleValue());
                            serde.serialize(aDouble, out);
                        }
                    } else {
                        throw new TypeMismatchException(getIdentifier(), 0, data[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG, ATypeTag.SERIALIZED_FLOAT_TYPE_TAG, ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AMutableInt8(org.apache.asterix.om.base.AMutableInt8) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) BigDecimal(java.math.BigDecimal) 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) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt16(org.apache.asterix.om.base.AMutableInt16) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) AMutableInt32(org.apache.asterix.om.base.AMutableInt32) AMutableFloat(org.apache.asterix.om.base.AMutableFloat)

Example 2 with AMutableInt16

use of org.apache.asterix.om.base.AMutableInt16 in project asterixdb by apache.

the class AInt16ConstructorDescriptor method createEvaluatorFactory.

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

        private static final long serialVersionUID = 1L;

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

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable inputArg = new VoidPointable();

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

                private short value;

                private int offset;

                private boolean positive;

                private AMutableInt16 aInt16 = new AMutableInt16((short) 0);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<AInt16> int16Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT16);

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        eval.evaluate(tuple, inputArg);
                        byte[] serString = inputArg.getByteArray();
                        int startOffset = inputArg.getStartOffset();
                        int len = inputArg.getLength();
                        if (serString[startOffset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            utf8Ptr.set(serString, startOffset + 1, len - 1);
                            offset = utf8Ptr.getCharStartOffset();
                            //accumulating value in negative domain
                            //otherwise Short.MIN_VALUE = -(Short.MAX_VALUE + 1) would have caused overflow
                            value = 0;
                            positive = true;
                            short limit = -Short.MAX_VALUE;
                            if (serString[offset] == '+') {
                                offset++;
                            } else if (serString[offset] == '-') {
                                offset++;
                                positive = false;
                                limit = Short.MIN_VALUE;
                            }
                            int end = startOffset + len;
                            for (; offset < end; offset++) {
                                int digit;
                                if (serString[offset] >= '0' && serString[offset] <= '9') {
                                    value = (short) (value * 10);
                                    digit = serString[offset] - '0';
                                } else if (serString[offset] == 'i' && serString[offset + 1] == '1' && serString[offset + 2] == '6' && offset + 3 == end) {
                                    break;
                                } else {
                                    throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT16_TYPE_TAG);
                                }
                                if (value < limit + digit) {
                                    throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT16_TYPE_TAG);
                                }
                                value = (short) (value - digit);
                            }
                            if (value > 0) {
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT16_TYPE_TAG);
                            }
                            if (value < 0 && positive) {
                                value *= -1;
                            }
                            aInt16.setValue(value);
                            int16Serde.serialize(aInt16, out);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        result.set(resultStorage);
                    } catch (IOException e) {
                        throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_INT16_TYPE_TAG);
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) 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) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) 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) AMutableInt16(org.apache.asterix.om.base.AMutableInt16)

Example 3 with AMutableInt16

use of org.apache.asterix.om.base.AMutableInt16 in project asterixdb by apache.

the class NumericRoundDescriptor 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 AMutableDouble aDouble = new AMutableDouble(0);

                private AMutableFloat aFloat = new AMutableFloat(0);

                private AMutableInt64 aInt64 = new AMutableInt64(0);

                private AMutableInt32 aInt32 = new AMutableInt32(0);

                private AMutableInt16 aInt16 = new AMutableInt16((short) 0);

                private AMutableInt8 aInt8 = new AMutableInt8((byte) 0);

                @SuppressWarnings("rawtypes")
                private ISerializerDeserializer serde;

                @SuppressWarnings("unchecked")
                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval.evaluate(tuple, argPtr);
                    byte[] data = argPtr.getByteArray();
                    int offset = argPtr.getStartOffset();
                    if (data[offset] == ATypeTag.SERIALIZED_INT8_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT8);
                        byte val = AInt8SerializerDeserializer.getByte(data, offset + 1);
                        aInt8.setValue(val);
                        serde.serialize(aInt8, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_INT16_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT16);
                        short val = AInt16SerializerDeserializer.getShort(data, offset + 1);
                        aInt16.setValue(val);
                        serde.serialize(aInt16, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_INT32_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
                        int val = AInt32SerializerDeserializer.getInt(data, offset + 1);
                        aInt32.setValue(val);
                        serde.serialize(aInt32, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_INT64_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
                        long val = AInt64SerializerDeserializer.getLong(data, offset + 1);
                        aInt64.setValue(val);
                        serde.serialize(aInt64, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_FLOAT_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);
                        float val = AFloatSerializerDeserializer.getFloat(data, offset + 1);
                        val = Math.round(val);
                        aFloat.setValue(val);
                        serde.serialize(aFloat, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
                        double val = ADoubleSerializerDeserializer.getDouble(data, offset + 1);
                        val = Math.round(val);
                        aDouble.setValue(val);
                        serde.serialize(aDouble, out);
                    } else {
                        throw new TypeMismatchException(getIdentifier(), 0, data[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG, ATypeTag.SERIALIZED_FLOAT_TYPE_TAG, ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AMutableInt8(org.apache.asterix.om.base.AMutableInt8) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) 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) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt16(org.apache.asterix.om.base.AMutableInt16) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) AMutableInt32(org.apache.asterix.om.base.AMutableInt32) AMutableFloat(org.apache.asterix.om.base.AMutableFloat)

Example 4 with AMutableInt16

use of org.apache.asterix.om.base.AMutableInt16 in project asterixdb by apache.

the class NumericRoundHalfToEvenDescriptor 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 AMutableDouble aDouble = new AMutableDouble(0);

                private AMutableFloat aFloat = new AMutableFloat(0);

                private AMutableInt64 aInt64 = new AMutableInt64(0);

                private AMutableInt32 aInt32 = new AMutableInt32(0);

                private AMutableInt16 aInt16 = new AMutableInt16((short) 0);

                private AMutableInt8 aInt8 = new AMutableInt8((byte) 0);

                @SuppressWarnings("rawtypes")
                private ISerializerDeserializer serde;

                @SuppressWarnings("unchecked")
                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval.evaluate(tuple, argPtr);
                    byte[] data = argPtr.getByteArray();
                    int offset = argPtr.getStartOffset();
                    if (data[offset] == ATypeTag.SERIALIZED_INT8_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT8);
                        byte val = AInt8SerializerDeserializer.getByte(data, offset + 1);
                        aInt8.setValue(val);
                        serde.serialize(aInt8, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_INT16_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT16);
                        short val = AInt16SerializerDeserializer.getShort(data, offset + 1);
                        aInt16.setValue(val);
                        serde.serialize(aInt16, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_INT32_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
                        int val = AInt32SerializerDeserializer.getInt(data, offset + 1);
                        aInt32.setValue(val);
                        serde.serialize(aInt32, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_INT64_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
                        long val = AInt64SerializerDeserializer.getLong(data, offset + 1);
                        aInt64.setValue(val);
                        serde.serialize(aInt64, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_FLOAT_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);
                        float val = AFloatSerializerDeserializer.getFloat(data, offset + 1);
                        aFloat.setValue((float) Math.rint(val));
                        serde.serialize(aFloat, out);
                    } else if (data[offset] == ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
                        double val = ADoubleSerializerDeserializer.getDouble(data, offset + 1);
                        aDouble.setValue(Math.rint(val));
                        serde.serialize(aDouble, out);
                    } else {
                        throw new TypeMismatchException(getIdentifier(), 0, data[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG, ATypeTag.SERIALIZED_FLOAT_TYPE_TAG, ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AMutableInt8(org.apache.asterix.om.base.AMutableInt8) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) 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) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt16(org.apache.asterix.om.base.AMutableInt16) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) AMutableInt32(org.apache.asterix.om.base.AMutableInt32) AMutableFloat(org.apache.asterix.om.base.AMutableFloat)

Example 5 with AMutableInt16

use of org.apache.asterix.om.base.AMutableInt16 in project asterixdb by apache.

the class NumericSubtractDescriptor 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();

                // one temp. buffer re-used by both children
                private IPointable argPtr = new VoidPointable();

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

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

                private double[] operands = new double[args.length];

                private boolean metInt8 = false, metInt16 = false, metInt32 = false, metInt64 = false, metFloat = false, metDouble = false;

                private ATypeTag typeTag;

                private AMutableDouble aDouble = new AMutableDouble(0);

                private AMutableFloat aFloat = new AMutableFloat(0);

                private AMutableInt64 aInt64 = new AMutableInt64(0);

                private AMutableInt32 aInt32 = new AMutableInt32(0);

                private AMutableInt16 aInt16 = new AMutableInt16((short) 0);

                private AMutableInt8 aInt8 = new AMutableInt8((byte) 0);

                @SuppressWarnings("rawtypes")
                private ISerializerDeserializer serde;

                @SuppressWarnings("unchecked")
                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    for (int i = 0; i < args.length; i++) {
                        if (i == 0) {
                            evalLeft.evaluate(tuple, argPtr);
                        } else {
                            evalRight.evaluate(tuple, argPtr);
                        }
                        byte[] data = argPtr.getByteArray();
                        int offset = argPtr.getStartOffset();
                        typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(data[offset]);
                        switch(typeTag) {
                            case TINYINT:
                                metInt8 = true;
                                operands[i] = AInt8SerializerDeserializer.getByte(data, offset + 1);
                                break;
                            case SMALLINT:
                                metInt16 = true;
                                operands[i] = AInt16SerializerDeserializer.getShort(data, offset + 1);
                                break;
                            case INTEGER:
                                metInt32 = true;
                                operands[i] = AInt32SerializerDeserializer.getInt(data, offset + 1);
                                break;
                            case BIGINT:
                                metInt64 = true;
                                operands[i] = AInt64SerializerDeserializer.getLong(data, offset + 1);
                                break;
                            case FLOAT:
                                metFloat = true;
                                operands[i] = AFloatSerializerDeserializer.getFloat(data, offset + 1);
                                break;
                            case DOUBLE:
                                metDouble = true;
                                operands[i] = ADoubleSerializerDeserializer.getDouble(data, offset + 1);
                                break;
                            default:
                                throw new TypeMismatchException(getIdentifier(), i, data[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG, ATypeTag.SERIALIZED_FLOAT_TYPE_TAG, ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
                        }
                    }
                    if (metDouble) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
                        aDouble.setValue(operands[0] - operands[1]);
                        serde.serialize(aDouble, out);
                    } else if (metFloat) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);
                        aFloat.setValue((float) (operands[0] - operands[1]));
                        serde.serialize(aFloat, out);
                    } else if (metInt64) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
                        aInt64.setValue((long) (operands[0] - operands[1]));
                        serde.serialize(aInt64, out);
                    } else if (metInt32) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
                        aInt32.setValue((int) (operands[0] - operands[1]));
                        serde.serialize(aInt32, out);
                    } else if (metInt16) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT16);
                        aInt16.setValue((short) (operands[0] - operands[1]));
                        serde.serialize(aInt16, out);
                    } else if (metInt8) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT8);
                        aInt8.setValue((byte) (operands[0] - operands[1]));
                        serde.serialize(aInt8, out);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AMutableInt8(org.apache.asterix.om.base.AMutableInt8) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) 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) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) ATypeTag(org.apache.asterix.om.types.ATypeTag) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt16(org.apache.asterix.om.base.AMutableInt16) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) AMutableInt32(org.apache.asterix.om.base.AMutableInt32) AMutableFloat(org.apache.asterix.om.base.AMutableFloat)

Aggregations

DataOutput (java.io.DataOutput)6 AMutableInt16 (org.apache.asterix.om.base.AMutableInt16)6 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)6 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)6 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)6 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)6 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)6 IPointable (org.apache.hyracks.data.std.api.IPointable)6 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)6 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)6 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)6 AMutableDouble (org.apache.asterix.om.base.AMutableDouble)5 AMutableFloat (org.apache.asterix.om.base.AMutableFloat)5 AMutableInt32 (org.apache.asterix.om.base.AMutableInt32)5 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)5 AMutableInt8 (org.apache.asterix.om.base.AMutableInt8)5 ATypeTag (org.apache.asterix.om.types.ATypeTag)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 AMutableDate (org.apache.asterix.om.base.AMutableDate)1