Search in sources :

Example 1 with AMutableDouble

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

the class Operation method doTimeArithmetic.

public static int doTimeArithmetic(int op, Value v1, Value v2, Value result, ClassAdObjectPool objectPool) {
    ClassAdTime asecs1 = objectPool.classAdTimePool.get();
    ClassAdTime asecs2 = objectPool.classAdTimePool.get();
    ValueType vt1 = v1.getType();
    ValueType vt2 = v2.getType();
    // addition
    if (op == OpKind_ADDITION_OP) {
        if (vt1 == ValueType.ABSOLUTE_TIME_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE) {
            v1.isAbsoluteTimeValue(asecs1);
            v2.isRelativeTimeValue(asecs2);
            asecs1.setValue(asecs1.getTimeInMillis() + asecs2.getTimeInMillis());
            result.setAbsoluteTimeValue(asecs1);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.ABSOLUTE_TIME_VALUE) {
            v1.isRelativeTimeValue(asecs1);
            v2.isAbsoluteTimeValue(asecs2);
            asecs2.setValue(asecs1.getTimeInMillis() + asecs2.getTimeInMillis());
            result.setAbsoluteTimeValue(asecs2);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE) {
            v1.isRelativeTimeValue(asecs1);
            v2.isRelativeTimeValue(asecs2);
            result.setRelativeTimeValue(asecs1.plus(asecs2.getRelativeTime(), false));
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
    }
    if (op == OpKind_SUBTRACTION_OP) {
        if (vt1 == ValueType.ABSOLUTE_TIME_VALUE && vt2 == ValueType.ABSOLUTE_TIME_VALUE) {
            v1.isAbsoluteTimeValue(asecs1);
            v2.isAbsoluteTimeValue(asecs2);
            result.setRelativeTimeValue(asecs1.subtract(asecs2, false));
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.ABSOLUTE_TIME_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE) {
            v1.isAbsoluteTimeValue(asecs1);
            v2.isRelativeTimeValue(asecs2);
            asecs1.setValue(asecs1.getTimeInMillis() - asecs2.getRelativeTime());
            result.setAbsoluteTimeValue(asecs1);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE) {
            v1.isRelativeTimeValue(asecs1);
            v2.isRelativeTimeValue(asecs2);
            result.setRelativeTimeValue(asecs1.subtract(asecs2));
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
    }
    if (op == OpKind_MULTIPLICATION_OP || op == OpKind_DIVISION_OP) {
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.INTEGER_VALUE) {
            AMutableInt64 num = objectPool.int64Pool.get();
            ClassAdTime msecs = objectPool.classAdTimePool.get();
            v1.isRelativeTimeValue(asecs1);
            v2.isIntegerValue(num);
            if (op == OpKind_MULTIPLICATION_OP) {
                msecs.setValue(asecs1.multiply(num.getLongValue(), false));
            } else {
                msecs.setValue(asecs1.divide(num.getLongValue(), false));
            }
            result.setRelativeTimeValue(msecs);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.REAL_VALUE) {
            AMutableDouble num = objectPool.doublePool.get();
            AMutableDouble msecs = objectPool.doublePool.get();
            v1.isRelativeTimeValue(asecs1);
            v2.isRealValue(num);
            if (op == OpKind_MULTIPLICATION_OP) {
                msecs.setValue(asecs1.getRelativeTime() * num.getDoubleValue());
            } else {
                msecs.setValue(asecs1.getRelativeTime() * num.getDoubleValue());
            }
            ClassAdTime time = objectPool.classAdTimePool.get();
            time.setRelativeTime(1000L * ((long) msecs.getDoubleValue()));
            result.setRelativeTimeValue(time);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.INTEGER_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE && op == OpKind_MULTIPLICATION_OP) {
            AMutableInt64 num = objectPool.int64Pool.get();
            v1.isIntegerValue(num);
            v2.isRelativeTimeValue(asecs1);
            ClassAdTime time = objectPool.classAdTimePool.get();
            time.setRelativeTime(num.getLongValue() * asecs1.getRelativeTime());
            result.setRelativeTimeValue(time);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt2 == ValueType.RELATIVE_TIME_VALUE && vt1 == ValueType.REAL_VALUE && op == OpKind_MULTIPLICATION_OP) {
            AMutableDouble num = objectPool.doublePool.get();
            v1.isRelativeTimeValue(asecs1);
            v2.isRealValue(num);
            ClassAdTime time = objectPool.classAdTimePool.get();
            time.setRelativeTime((long) (asecs1.getRelativeTime() * num.getDoubleValue()));
            result.setRelativeTimeValue(time);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
    }
    // no other operations are supported on times
    result.setErrorValue();
    return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
}
Also used : ValueType(org.apache.asterix.external.classad.Value.ValueType) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 2 with AMutableDouble

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

the class Operation method coerceToNumber.

// This function performs type promotions so that both v1 and v2 are of the
// same numerical type: (v1 and v2 are not ERROR or UNDEFINED)
//  + if both v1 and v2 are Numbers and of the same type, return type
//  + if v1 is an int and v2 is a real, convert v1 to real; return REAL_VALUE
//  + if v1 is a real and v2 is an int, convert v2 to real; return REAL_VALUE
public static ValueType coerceToNumber(Value v1, Value v2, ClassAdObjectPool objectPool) {
    AMutableInt64 i = objectPool.int64Pool.get();
    AMutableDouble r = objectPool.doublePool.get();
    MutableBoolean b = objectPool.boolPool.get();
    // either of v1, v2 not numerical?
    if (v1.isClassAdValue() || v2.isClassAdValue()) {
        return ValueType.CLASSAD_VALUE;
    }
    if (v1.isListValue() || v2.isListValue()) {
        return ValueType.LIST_VALUE;
    }
    if (v1.isStringValue() || v2.isStringValue()) {
        return ValueType.STRING_VALUE;
    }
    if (v1.isUndefinedValue() || v2.isUndefinedValue()) {
        return ValueType.UNDEFINED_VALUE;
    }
    if (v1.isErrorValue() || v2.isErrorValue()) {
        return ValueType.ERROR_VALUE;
    }
    if (v1.isAbsoluteTimeValue() || v2.isAbsoluteTimeValue()) {
        return ValueType.ABSOLUTE_TIME_VALUE;
    }
    if (v1.isRelativeTimeValue() || v2.isRelativeTimeValue()) {
        return ValueType.RELATIVE_TIME_VALUE;
    }
    // promote booleans to integers
    if (v1.isBooleanValue(b)) {
        if (b.booleanValue()) {
            v1.setIntegerValue(1);
        } else {
            v1.setIntegerValue(0);
        }
    }
    if (v2.isBooleanValue(b)) {
        if (b.booleanValue()) {
            v2.setIntegerValue(1);
        } else {
            v2.setIntegerValue(0);
        }
    }
    // both v1 and v2 of same numerical type
    if (v1.isIntegerValue(i) && v2.isIntegerValue(i)) {
        return ValueType.INTEGER_VALUE;
    }
    if (v1.isRealValue(r) && v2.isRealValue(r)) {
        return ValueType.REAL_VALUE;
    }
    // type promotions required
    if (v1.isIntegerValue(i) && v2.isRealValue(r)) {
        v1.setRealValue(i.getLongValue());
    } else if (v1.isRealValue(r) && v2.isIntegerValue(i)) {
        v2.setRealValue(i.getLongValue());
    }
    return ValueType.REAL_VALUE;
}
Also used : AMutableDouble(org.apache.asterix.om.base.AMutableDouble) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 3 with AMutableDouble

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

the class BuiltinClassAdFunctions method doSplitTime.

public static boolean doSplitTime(Value time, ClassAd splitClassAd, ClassAdObjectPool objectPool) throws HyracksDataException {
    boolean did_conversion;
    AMutableInt64 integer = objectPool.int64Pool.get();
    integer.setValue(0);
    AMutableDouble real = objectPool.doublePool.get();
    real.setValue(0);
    ClassAdTime asecs = objectPool.classAdTimePool.get();
    ClassAdTime rsecs = objectPool.classAdTimePool.get();
    ClassAd classad = objectPool.classAdPool.get();
    did_conversion = true;
    if (time.isIntegerValue(integer)) {
        asecs.setValue(integer.getLongValue());
        asecs.makeLocalAbsolute();
        absTimeToClassAd(asecs, splitClassAd);
    } else if (time.isRealValue(real)) {
        asecs.setValue((long) real.getDoubleValue());
        asecs.makeAbsolute(true);
        absTimeToClassAd(asecs, splitClassAd);
    } else if (time.isAbsoluteTimeValue(asecs)) {
        absTimeToClassAd(asecs, splitClassAd);
    } else if (time.isRelativeTimeValue(rsecs)) {
        relTimeToClassAd((rsecs.getRelativeTime() / 1000.0), splitClassAd);
    } else if (time.isClassAdValue(classad)) {
        splitClassAd = objectPool.classAdPool.get();
        splitClassAd.copyFrom(classad);
    } else {
        did_conversion = false;
    }
    return did_conversion;
}
Also used : AMutableDouble(org.apache.asterix.om.base.AMutableDouble) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 4 with AMutableDouble

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

the class SimilarityDescriptor 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 evalLen1 = args[0].createScalarEvaluator(ctx);

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

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

                private final IScalarEvaluator evalTokens2 = args[3].createScalarEvaluator(ctx);

                private final IScalarEvaluator evalTokenPrefix = args[4].createScalarEvaluator(ctx);

                private final IScalarEvaluator evalSimilarity = args[5].createScalarEvaluator(ctx);

                private final IScalarEvaluator evalThreshold = args[6].createScalarEvaluator(ctx);

                private final SimilarityFiltersCache similarityFiltersCache = new SimilarityFiltersCache();

                private final IntArray tokens1 = new IntArray();

                private final IntArray tokens2 = new IntArray();

                private final PartialIntersect parInter = new PartialIntersect();

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

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<ADouble> doubleSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    // similarity threshold
                    evalThreshold.evaluate(tuple, inputVal);
                    byte[] data = inputVal.getByteArray();
                    int offset = inputVal.getStartOffset();
                    if (data[offset] != ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, data[offset], ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
                    }
                    float similarityThreshold = (float) ADoubleSerializerDeserializer.getDouble(data, offset + 1);
                    // similarity name
                    evalSimilarity.evaluate(tuple, inputVal);
                    data = inputVal.getByteArray();
                    offset = inputVal.getStartOffset();
                    int len = inputVal.getLength();
                    if (data[offset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 1, data[offset], ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
                    }
                    SimilarityFilters similarityFilters = similarityFiltersCache.get(similarityThreshold, data, offset, len);
                    evalLen1.evaluate(tuple, inputVal);
                    data = inputVal.getByteArray();
                    offset = inputVal.getStartOffset();
                    if (data[offset] != ATypeTag.SERIALIZED_INT32_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 2, data[offset], ATypeTag.SERIALIZED_INT32_TYPE_TAG);
                    }
                    int length1 = IntegerPointable.getInteger(data, offset + 1);
                    evalLen2.evaluate(tuple, inputVal);
                    data = inputVal.getByteArray();
                    offset = inputVal.getStartOffset();
                    if (data[offset] != ATypeTag.SERIALIZED_INT32_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 3, data[offset], ATypeTag.SERIALIZED_INT32_TYPE_TAG);
                    }
                    int length2 = IntegerPointable.getInteger(data, offset + 1);
                    float sim = 0;
                    //
                    if (similarityFilters.passLengthFilter(length1, length2)) {
                        // -- - tokens1 - --
                        int i;
                        tokens1.reset();
                        evalTokens1.evaluate(tuple, inputVal);
                        byte[] serList = inputVal.getByteArray();
                        offset = inputVal.getStartOffset();
                        if (serList[offset] != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG && serList[offset] != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 4, data[offset], ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG, ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG);
                        }
                        int lengthTokens1;
                        if (serList[offset] == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
                            lengthTokens1 = AOrderedListSerializerDeserializer.getNumberOfItems(serList, offset);
                            // read tokens
                            for (i = 0; i < lengthTokens1; i++) {
                                int itemOffset;
                                try {
                                    itemOffset = AOrderedListSerializerDeserializer.getItemOffset(serList, offset, i);
                                } catch (AsterixException e) {
                                    throw new HyracksDataException(e);
                                }
                                tokens1.add(IntegerPointable.getInteger(serList, itemOffset));
                            }
                        } else {
                            lengthTokens1 = AUnorderedListSerializerDeserializer.getNumberOfItems(serList, offset);
                            // read tokens
                            for (i = 0; i < lengthTokens1; i++) {
                                int itemOffset;
                                try {
                                    itemOffset = AUnorderedListSerializerDeserializer.getItemOffset(serList, offset, i);
                                } catch (AsterixException e) {
                                    throw new HyracksDataException(e);
                                }
                                tokens1.add(IntegerPointable.getInteger(serList, itemOffset));
                            }
                        }
                        // pad tokens
                        for (; i < length1; i++) {
                            tokens1.add(Integer.MAX_VALUE);
                        }
                        // -- - tokens2 - --
                        tokens2.reset();
                        evalTokens2.evaluate(tuple, inputVal);
                        serList = inputVal.getByteArray();
                        offset = inputVal.getStartOffset();
                        if (serList[offset] != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG && serList[offset] != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 5, data[offset], ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG, ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG);
                        }
                        int lengthTokens2;
                        if (serList[0] == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
                            lengthTokens2 = AOrderedListSerializerDeserializer.getNumberOfItems(serList, offset);
                            // read tokens
                            for (i = 0; i < lengthTokens2; i++) {
                                int itemOffset;
                                try {
                                    itemOffset = AOrderedListSerializerDeserializer.getItemOffset(serList, offset, i);
                                } catch (AsterixException e) {
                                    throw new HyracksDataException(e);
                                }
                                tokens2.add(IntegerPointable.getInteger(serList, itemOffset));
                            }
                        } else {
                            lengthTokens2 = AUnorderedListSerializerDeserializer.getNumberOfItems(serList, offset);
                            // read tokens
                            for (i = 0; i < lengthTokens2; i++) {
                                int itemOffset;
                                try {
                                    itemOffset = AUnorderedListSerializerDeserializer.getItemOffset(serList, offset, i);
                                } catch (AsterixException e) {
                                    throw new HyracksDataException(e);
                                }
                                tokens2.add(IntegerPointable.getInteger(serList, itemOffset));
                            }
                        }
                        // pad tokens
                        for (; i < length2; i++) {
                            tokens2.add(Integer.MAX_VALUE);
                        }
                        // -- - token prefix - --
                        evalTokenPrefix.evaluate(tuple, inputVal);
                        int tokenPrefix = IntegerPointable.getInteger(inputVal.getByteArray(), inputVal.getStartOffset() + 1);
                        //
                        // -- - position filter - --
                        //
                        SimilarityMetric.getPartialIntersectSize(tokens1.get(), 0, tokens1.length(), tokens2.get(), 0, tokens2.length(), tokenPrefix, parInter);
                        if (similarityFilters.passPositionFilter(parInter.intersectSize, parInter.posXStop, length1, parInter.posYStop, length2)) {
                            //
                            if (similarityFilters.passSuffixFilter(tokens1.get(), 0, tokens1.length(), parInter.posXStart, tokens2.get(), 0, tokens2.length(), parInter.posYStart)) {
                                sim = similarityFilters.passSimilarityFilter(tokens1.get(), 0, tokens1.length(), parInter.posXStop + 1, tokens2.get(), 0, tokens2.length(), parInter.posYStop + 1, parInter.intersectSize);
                            }
                        }
                    }
                    res.setValue(sim);
                    try {
                        doubleSerde.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) SimilarityFiltersCache(org.apache.asterix.runtime.evaluators.common.SimilarityFiltersCache) SimilarityFilters(org.apache.asterix.fuzzyjoin.similarity.SimilarityFilters) 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) AsterixException(org.apache.asterix.common.exceptions.AsterixException) IntArray(org.apache.asterix.fuzzyjoin.IntArray) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) PartialIntersect(org.apache.asterix.fuzzyjoin.similarity.PartialIntersect) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)

Example 5 with AMutableDouble

use of org.apache.asterix.om.base.AMutableDouble 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)

Aggregations

AMutableDouble (org.apache.asterix.om.base.AMutableDouble)24 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)16 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)12 DataOutput (java.io.DataOutput)11 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)11 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)11 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)11 IPointable (org.apache.hyracks.data.std.api.IPointable)11 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)11 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)11 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)11 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)10 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)9 AMutableInt32 (org.apache.asterix.om.base.AMutableInt32)7 AMutableFloat (org.apache.asterix.om.base.AMutableFloat)6 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)6 IOException (java.io.IOException)5 AMutableInt16 (org.apache.asterix.om.base.AMutableInt16)5 AMutableInt8 (org.apache.asterix.om.base.AMutableInt8)5 AMutableCharArrayString (org.apache.asterix.external.classad.AMutableCharArrayString)2