Search in sources :

Example 1 with AMutableYearMonthDuration

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

the class GetYearMonthDurationDescriptor 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<AYearMonthDuration> yearMonthDurationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AYEARMONTHDURATION);

                AMutableYearMonthDuration aYearMonthDuration = new AMutableYearMonthDuration(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    if (bytes0[offset0] != ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DURATION_TYPE_TAG);
                    }
                    aYearMonthDuration.setMonths(ADurationSerializerDeserializer.getYearMonth(bytes0, offset0 + 1));
                    yearMonthDurationSerde.serialize(aYearMonthDuration, 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) AMutableYearMonthDuration(org.apache.asterix.om.base.AMutableYearMonthDuration) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)

Example 2 with AMutableYearMonthDuration

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

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

the class AYearMonthDurationConstructorDescriptor 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 AMutableYearMonthDuration aYearMonthDuration = new AMutableYearMonthDuration(0);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<AYearMonthDuration> yearMonthDurationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AYEARMONTHDURATION);

                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();
                            ADurationParserFactory.parseDuration(serString, utf8Ptr.getCharStartOffset(), stringLength, aYearMonthDuration, ADurationParseOption.YEAR_MONTH);
                            yearMonthDurationSerde.serialize(aYearMonthDuration, 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_YEAR_MONTH_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) AMutableYearMonthDuration(org.apache.asterix.om.base.AMutableYearMonthDuration) 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)

Aggregations

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