Search in sources :

Example 31 with AMutableInt64

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

the class TemporalMonthAccessor method createEvaluatorFactory.

/* (non-Javadoc)
     * @see org.apache.asterix.runtime.base.IScalarFunctionDynamicDescriptor#createEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.ICopyEvaluatorFactory[])
     */
@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 final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                private final IPointable argPtr = new VoidPointable();

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

                private final GregorianCalendarSystem calSystem = GregorianCalendarSystem.getInstance();

                // for output: type integer
                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<AInt64> intSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);

                private final AMutableInt64 aMutableInt64 = new AMutableInt64(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    eval.evaluate(tuple, argPtr);
                    byte[] bytes = argPtr.getByteArray();
                    int startOffset = argPtr.getStartOffset();
                    resultStorage.reset();
                    try {
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
                            aMutableInt64.setValue(calSystem.getDurationMonth(ADurationSerializerDeserializer.getYearMonth(bytes, startOffset + 1)));
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        }
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG) {
                            aMutableInt64.setValue(calSystem.getDurationMonth(AYearMonthDurationSerializerDeserializer.getYearMonth(bytes, startOffset + 1)));
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        }
                        long chrononTimeInMs = 0;
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                            chrononTimeInMs = AInt32SerializerDeserializer.getInt(bytes, startOffset + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                        } else if (bytes[startOffset] == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                            chrononTimeInMs = AInt64SerializerDeserializer.getLong(bytes, startOffset + 1);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                        }
                        int year = calSystem.getYear(chrononTimeInMs);
                        int month = calSystem.getMonthOfYear(chrononTimeInMs, year);
                        aMutableInt64.setValue(month);
                        intSerde.serialize(aMutableInt64, out);
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) 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 32 with AMutableInt64

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

the class Value method convertValueToRealValue.

public static boolean convertValueToRealValue(Value value, Value realValue, ClassAdObjectPool objectPool) throws HyracksDataException {
    boolean could_convert;
    AMutableCharArrayString buf = objectPool.strPool.get();
    int endIndex;
    char end;
    AMutableInt64 ivalue = objectPool.int64Pool.get();
    ClassAdTime atvalue = objectPool.classAdTimePool.get();
    MutableBoolean bvalue = objectPool.boolPool.get();
    double rvalue;
    NumberFactor nf = NumberFactor.NO_FACTOR;
    switch(value.getType()) {
        case UNDEFINED_VALUE:
            realValue.setUndefinedValue();
            could_convert = false;
            break;
        case ERROR_VALUE:
        case CLASSAD_VALUE:
        case LIST_VALUE:
        case SLIST_VALUE:
            realValue.setErrorValue();
            could_convert = false;
            break;
        case STRING_VALUE:
            could_convert = true;
            value.isStringValue(buf);
            endIndex = buf.fistNonDoubleDigitChar();
            if (endIndex < 0) {
                // no non digit
                String buffString = buf.toString();
                if (buffString.contains("INF")) {
                    buffString = buffString.replace("INF", "Infinity");
                }
                rvalue = Double.parseDouble(buffString);
                nf = NumberFactor.NO_FACTOR;
            } else {
                rvalue = Double.parseDouble(buf.substr(0, endIndex));
                end = buf.charAt(endIndex);
                switch(Character.toUpperCase(end)) {
                    case 'B':
                        nf = NumberFactor.B_FACTOR;
                        break;
                    case 'K':
                        nf = NumberFactor.K_FACTOR;
                        break;
                    case 'M':
                        nf = NumberFactor.M_FACTOR;
                        break;
                    case 'G':
                        nf = NumberFactor.G_FACTOR;
                        break;
                    case 'T':
                        nf = NumberFactor.T_FACTOR;
                        break;
                    case '\0':
                        nf = NumberFactor.NO_FACTOR;
                        break;
                    default:
                        nf = NumberFactor.NO_FACTOR;
                        break;
                }
            }
            if (could_convert) {
                realValue.setRealValue(rvalue * Value.ScaleFactor[nf.ordinal()]);
            }
            break;
        case BOOLEAN_VALUE:
            value.isBooleanValue(bvalue);
            realValue.setRealValue(bvalue.booleanValue() ? 1.0 : 0.0);
            could_convert = true;
            break;
        case INTEGER_VALUE:
            value.isIntegerValue(ivalue);
            realValue.setRealValue(ivalue.getLongValue());
            could_convert = true;
            break;
        case REAL_VALUE:
            realValue.setValue(value);
            could_convert = true;
            break;
        case ABSOLUTE_TIME_VALUE:
            value.isAbsoluteTimeValue(atvalue);
            realValue.setRealValue(atvalue.getTimeInMillis() / 1000.0);
            could_convert = true;
            break;
        case RELATIVE_TIME_VALUE:
            value.isRelativeTimeValue(atvalue);
            realValue.setRealValue(atvalue.getRelativeTime() / 1000.0);
            could_convert = true;
            break;
        default:
            // Make gcc's -Wuninitalized happy
            could_convert = false;
            throw new HyracksDataException("Should not reach here");
    }
    return could_convert;
}
Also used : MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 33 with AMutableInt64

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

the class Value method convertValueToIntegerValue.

public static boolean convertValueToIntegerValue(Value value, Value integerValue, ClassAdObjectPool objectPool) throws HyracksDataException {
    boolean could_convert;
    AMutableCharArrayString buf = objectPool.strPool.get();
    char end;
    AMutableInt64 ivalue = objectPool.int64Pool.get();
    AMutableDouble rtvalue = objectPool.doublePool.get();
    ClassAdTime atvalue = objectPool.classAdTimePool.get();
    MutableBoolean bvalue = objectPool.boolPool.get();
    NumberFactor nf;
    switch(value.getType()) {
        case UNDEFINED_VALUE:
            integerValue.setUndefinedValue();
            could_convert = false;
            break;
        case ERROR_VALUE:
        case CLASSAD_VALUE:
        case LIST_VALUE:
        case SLIST_VALUE:
            integerValue.setErrorValue();
            could_convert = false;
            break;
        case STRING_VALUE:
            could_convert = true;
            value.isStringValue(buf);
            int endIndex = buf.firstNonDigitChar();
            if (endIndex < 0) {
                // no non digit
                ivalue.setValue(Long.parseLong(buf.toString()));
                nf = NumberFactor.NO_FACTOR;
                break;
            } else {
                ivalue.setValue(Long.parseLong(buf.substr(0, endIndex)));
                end = buf.charAt(endIndex);
                switch(Character.toUpperCase(end)) {
                    case 'B':
                        nf = NumberFactor.B_FACTOR;
                        break;
                    case 'K':
                        nf = NumberFactor.K_FACTOR;
                        break;
                    case 'M':
                        nf = NumberFactor.M_FACTOR;
                        break;
                    case 'G':
                        nf = NumberFactor.G_FACTOR;
                        break;
                    case 'T':
                        nf = NumberFactor.T_FACTOR;
                        break;
                    case '\0':
                        nf = NumberFactor.NO_FACTOR;
                        break;
                    default:
                        nf = NumberFactor.NO_FACTOR;
                        break;
                }
                if (could_convert) {
                    integerValue.setIntegerValue((long) ((ivalue.getLongValue() * Value.ScaleFactor[nf.ordinal()])));
                }
            }
            break;
        case BOOLEAN_VALUE:
            value.isBooleanValue(bvalue);
            integerValue.setIntegerValue(bvalue.booleanValue() ? 1 : 0);
            could_convert = true;
            break;
        case INTEGER_VALUE:
            integerValue.setValue(value);
            could_convert = true;
            break;
        case REAL_VALUE:
            value.isRealValue(rtvalue);
            integerValue.setIntegerValue((long) rtvalue.getDoubleValue());
            could_convert = true;
            break;
        case ABSOLUTE_TIME_VALUE:
            value.isAbsoluteTimeValue(atvalue);
            integerValue.setIntegerValue(atvalue.getTimeInMillis() / 1000L);
            could_convert = true;
            break;
        case RELATIVE_TIME_VALUE:
            value.isRelativeTimeValue(atvalue);
            integerValue.setIntegerValue((atvalue.getTime() / 1000L));
            could_convert = true;
            break;
        default:
            // Make gcc's -Wuninitalized happy
            could_convert = false;
            throw new HyracksDataException("Should not reach here");
    }
    return could_convert;
}
Also used : AMutableDouble(org.apache.asterix.om.base.AMutableDouble) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 34 with AMutableInt64

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

the class Literal method privateEvaluate.

@Override
public boolean privateEvaluate(EvalState eval, Value val) throws HyracksDataException {
    AMutableInt64 i = objectPool.int64Pool.get();
    AMutableDouble r = objectPool.doublePool.get();
    val.setValue(value);
    // if integer or real, multiply by the factor
    if (val.isIntegerValue(i)) {
        if (factor != NumberFactor.NO_FACTOR) {
            val.setRealValue((i.getLongValue()) * Value.ScaleFactor[factor.ordinal()]);
        } else {
            val.setIntegerValue(i.getLongValue());
        }
    } else if (val.isRealValue(r)) {
        val.setRealValue(r.getDoubleValue() * Value.ScaleFactor[factor.ordinal()]);
    }
    return true;
}
Also used : AMutableDouble(org.apache.asterix.om.base.AMutableDouble) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 35 with AMutableInt64

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

the class Literal method GetValue.

public void GetValue(Value val) throws HyracksDataException {
    AMutableInt64 i = objectPool.int64Pool.get();
    AMutableDouble r = objectPool.doublePool.get();
    val.setValue(value);
    // if integer or real, multiply by the factor
    if (val.isIntegerValue(i)) {
        if (factor != NumberFactor.NO_FACTOR) {
            val.setRealValue((i.getLongValue()) * Value.ScaleFactor[factor.ordinal()]);
        }
    } else if (val.isRealValue(r)) {
        if (factor != NumberFactor.NO_FACTOR) {
            val.setRealValue(r.getDoubleValue() * Value.ScaleFactor[factor.ordinal()]);
        }
    }
}
Also used : AMutableDouble(org.apache.asterix.om.base.AMutableDouble) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Aggregations

AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)41 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)27 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)26 IPointable (org.apache.hyracks.data.std.api.IPointable)26 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)26 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)25 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)24 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)24 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)24 DataOutput (java.io.DataOutput)23 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)22 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)17 AMutableDouble (org.apache.asterix.om.base.AMutableDouble)16 IOException (java.io.IOException)11 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)8 AMutableInt32 (org.apache.asterix.om.base.AMutableInt32)7 GregorianCalendarSystem (org.apache.asterix.om.base.temporal.GregorianCalendarSystem)7 AMutableFloat (org.apache.asterix.om.base.AMutableFloat)6 AMutableInt16 (org.apache.asterix.om.base.AMutableInt16)5 AMutableInt8 (org.apache.asterix.om.base.AMutableInt8)5