Search in sources :

Example 1 with AMutableDayTimeDuration

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

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

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

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ADayTimeDuration> dayTimeDurationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADAYTIMEDURATION);

                AMutableDayTimeDuration aDayTimeDuration = new AMutableDayTimeDuration(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    byte[] bytes = argPtr0.getByteArray();
                    int offset = argPtr0.getStartOffset();
                    if (bytes[offset] != ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_DURATION_TYPE_TAG);
                    }
                    aDayTimeDuration.setMilliseconds(ADurationSerializerDeserializer.getDayTime(bytes, offset + 1));
                    dayTimeDurationSerde.serialize(aDayTimeDuration, out);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) 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) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableDayTimeDuration(org.apache.asterix.om.base.AMutableDayTimeDuration)

Example 2 with AMutableDayTimeDuration

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

the class ADurationParserFactory method parseDuration.

public static void parseDuration(final Object durationString, final int start, int length, IAObject mutableObject, ADurationParseOption parseOption) throws HyracksDataException {
    int offset = 0;
    int value = 0, hour = 0, minute = 0, second = 0, millisecond = 0, year = 0, month = 0, day = 0;
    State state = State.NOTHING_READ;
    IStringAccessor charAccessor;
    if (durationString instanceof char[]) {
        charAccessor = new IStringAccessor() {

            @Override
            public char getCharAt(int index) {
                return ((char[]) durationString)[start + index];
            }
        };
    } else if (durationString instanceof byte[]) {
        charAccessor = new IStringAccessor() {

            @Override
            public char getCharAt(int index) {
                return (char) (((byte[]) durationString)[start + index]);
            }
        };
    } else if (durationString instanceof String) {
        charAccessor = new IStringAccessor() {

            @Override
            public char getCharAt(int index) {
                return ((String) durationString).charAt(start + index);
            }
        };
    } else {
        throw new HyracksDataException(durationErrorMessage);
    }
    short sign = 1;
    if (charAccessor.getCharAt(offset) == '-') {
        offset++;
        sign = -1;
    }
    if (charAccessor.getCharAt(offset) != 'P') {
        throw new HyracksDataException(durationErrorMessage + ": Missing leading 'P'.");
    }
    offset++;
    for (; offset < length; offset++) {
        if (charAccessor.getCharAt(offset) >= '0' && charAccessor.getCharAt(offset) <= '9') {
            // accumulate the digit fields
            value = value * DECIMAL_UNIT + charAccessor.getCharAt(offset) - '0';
        } else {
            switch(charAccessor.getCharAt(offset)) {
                case 'Y':
                    if (state.compareTo(State.YEAR) < 0) {
                        if (parseOption == ADurationParseOption.DAY_TIME) {
                            throw new HyracksDataException(onlyDayTimeErrorMessage);
                        }
                        year = value;
                        state = State.YEAR;
                    } else {
                        throw new HyracksDataException(durationErrorMessage + ": wrong YEAR feild.");
                    }
                    break;
                case 'M':
                    if (state.compareTo(State.TIME) < 0) {
                        if (state.compareTo(State.MONTH) < 0) {
                            if (parseOption == ADurationParseOption.DAY_TIME) {
                                throw new HyracksDataException(onlyDayTimeErrorMessage);
                            }
                            month = value;
                            state = State.MONTH;
                        } else {
                            throw new HyracksDataException(durationErrorMessage + ": wrong MONTH field.");
                        }
                    } else if (state.compareTo(State.MIN) < 0) {
                        if (parseOption == ADurationParseOption.YEAR_MONTH) {
                            throw new HyracksDataException(onlyYearMonthErrorMessage);
                        }
                        minute = value;
                        state = State.MIN;
                    } else {
                        throw new HyracksDataException(durationErrorMessage + ": wrong MIN field.");
                    }
                    break;
                case 'D':
                    if (state.compareTo(State.DAY) < 0) {
                        if (parseOption == ADurationParseOption.YEAR_MONTH) {
                            throw new HyracksDataException(onlyYearMonthErrorMessage);
                        }
                        day = value;
                        state = State.DAY;
                    } else {
                        throw new HyracksDataException(durationErrorMessage + ": wrong DAY field");
                    }
                    break;
                case 'T':
                    if (state.compareTo(State.TIME) < 0) {
                        if (parseOption == ADurationParseOption.YEAR_MONTH) {
                            throw new HyracksDataException(onlyYearMonthErrorMessage);
                        }
                        state = State.TIME;
                    } else {
                        throw new HyracksDataException(durationErrorMessage + ": wrong TIME field.");
                    }
                    break;
                case 'H':
                    if (state.compareTo(State.HOUR) < 0) {
                        if (parseOption == ADurationParseOption.YEAR_MONTH) {
                            throw new HyracksDataException(onlyYearMonthErrorMessage);
                        }
                        hour = value;
                        state = State.HOUR;
                    } else {
                        throw new HyracksDataException(durationErrorMessage + ": wrong HOUR field.");
                    }
                    break;
                case '.':
                    if (state.compareTo(State.MILLISEC) < 0) {
                        if (parseOption == ADurationParseOption.YEAR_MONTH) {
                            throw new HyracksDataException(onlyYearMonthErrorMessage);
                        }
                        int i = 1;
                        for (; offset + i < length; i++) {
                            if (charAccessor.getCharAt(offset + i) >= '0' && charAccessor.getCharAt(offset + i) <= '9') {
                                if (i < 4) {
                                    millisecond = millisecond * DECIMAL_UNIT + (charAccessor.getCharAt(offset + i) - '0');
                                } else {
                                    throw new HyracksDataException(durationErrorMessage + ": wrong MILLISECOND field.");
                                }
                            } else {
                                break;
                            }
                        }
                        offset += i;
                        state = State.MILLISEC;
                    } else {
                        throw new HyracksDataException(durationErrorMessage + ": wrong MILLISECOND field.");
                    }
                case 'S':
                    if (state.compareTo(State.SEC) < 0) {
                        if (parseOption == ADurationParseOption.YEAR_MONTH) {
                            throw new HyracksDataException(onlyYearMonthErrorMessage);
                        }
                        second = value;
                        state = State.SEC;
                    } else {
                        throw new HyracksDataException(durationErrorMessage + ": wrong SECOND field.");
                    }
                    break;
                default:
                    throw new HyracksDataException(durationErrorMessage + ": wrong format for duration.");
            }
            value = 0;
        }
    }
    if (state.compareTo(State.TIME) == 0) {
        throw new HyracksDataException(durationErrorMessage + ": no time fields after time separator.");
    }
    int totalMonths = sign * (year * 12 + month);
    long totalMilliseconds = sign * (day * GregorianCalendarSystem.CHRONON_OF_DAY + hour * GregorianCalendarSystem.CHRONON_OF_HOUR + minute * GregorianCalendarSystem.CHRONON_OF_MINUTE + second * GregorianCalendarSystem.CHRONON_OF_SECOND + millisecond);
    if (sign > 0) {
        if (totalMonths < 0) {
            throw new HyracksDataException(durationErrorMessage + ": total number of months is beyond its max value (-2147483647 to 2147483647).");
        }
        if (totalMilliseconds < 0) {
            throw new HyracksDataException(durationErrorMessage + ": total number of milliseconds is beyond its max value (-9223372036854775808 to 9223372036854775807).");
        }
    }
    if (mutableObject instanceof AMutableDuration) {
        ((AMutableDuration) mutableObject).setValue(totalMonths, totalMilliseconds);
    } else if (mutableObject instanceof AMutableYearMonthDuration) {
        ((AMutableYearMonthDuration) mutableObject).setMonths(totalMonths);
    } else if (mutableObject instanceof AMutableDayTimeDuration) {
        ((AMutableDayTimeDuration) mutableObject).setMilliseconds(totalMilliseconds);
    }
}
Also used : AMutableYearMonthDuration(org.apache.asterix.om.base.AMutableYearMonthDuration) AMutableDuration(org.apache.asterix.om.base.AMutableDuration) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) AMutableDayTimeDuration(org.apache.asterix.om.base.AMutableDayTimeDuration)

Example 3 with AMutableDayTimeDuration

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

the class ADayTimeDurationConstructorDescriptor 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 AMutableDayTimeDuration aDayTimeDuration = new AMutableDayTimeDuration(0);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ADayTimeDuration> dayTimeDurationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADAYTIMEDURATION);

                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 offset = inputArg.getStartOffset();
                        int len = inputArg.getLength();
                        if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            utf8Ptr.set(serString, offset + 1, len - 1);
                            int stringLength = utf8Ptr.getUTF8Length();
                            int startOffset = utf8Ptr.getCharStartOffset();
                            ADurationParserFactory.parseDuration(serString, startOffset, stringLength, aDayTimeDuration, ADurationParseOption.DAY_TIME);
                            dayTimeDurationSerde.serialize(aDayTimeDuration, out);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        result.set(resultStorage);
                    } catch (Exception e) {
                        throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_DAY_TIME_DURATION_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) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) 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) AMutableDayTimeDuration(org.apache.asterix.om.base.AMutableDayTimeDuration)

Example 4 with AMutableDayTimeDuration

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

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

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ADayTimeDuration> dayTimeDurationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADAYTIMEDURATION);

                private AMutableDayTimeDuration aDayTimeDuration = new AMutableDayTimeDuration(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval.evaluate(tuple, argPtr);
                    byte[] bytes = argPtr.getByteArray();
                    int offset = argPtr.getStartOffset();
                    if (bytes[offset] != ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                    }
                    long chrononStart = AIntervalSerializerDeserializer.getIntervalStart(bytes, offset + 1);
                    long chrononEnd = AIntervalSerializerDeserializer.getIntervalEnd(bytes, offset + 1);
                    byte intervalTypeTag = AIntervalSerializerDeserializer.getIntervalTimeType(bytes, offset + 1);
                    if (intervalTypeTag == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                        chrononStart *= GregorianCalendarSystem.CHRONON_OF_DAY;
                        chrononEnd *= GregorianCalendarSystem.CHRONON_OF_DAY;
                    }
                    aDayTimeDuration.setMilliseconds(chrononEnd - chrononStart);
                    dayTimeDurationSerde.serialize(aDayTimeDuration, out);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) 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) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableDayTimeDuration(org.apache.asterix.om.base.AMutableDayTimeDuration)

Aggregations

AMutableDayTimeDuration (org.apache.asterix.om.base.AMutableDayTimeDuration)4 DataOutput (java.io.DataOutput)3 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)3 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)3 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)3 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)3 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)3 IPointable (org.apache.hyracks.data.std.api.IPointable)3 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)3 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)3 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)3 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)2 AMutableDuration (org.apache.asterix.om.base.AMutableDuration)1 AMutableYearMonthDuration (org.apache.asterix.om.base.AMutableYearMonthDuration)1 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)1 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)1