Search in sources :

Example 81 with TypeMismatchException

use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.

the class FieldAccessByNameEvalFactory method createScalarEvaluator.

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

        private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

        private DataOutput out = resultStorage.getDataOutput();

        private IPointable inputArg0 = new VoidPointable();

        private IPointable inputArg1 = new VoidPointable();

        private IScalarEvaluator eval0 = recordEvalFactory.createScalarEvaluator(ctx);

        private IScalarEvaluator eval1 = fldNameEvalFactory.createScalarEvaluator(ctx);

        private int fieldValueOffset;

        private int fieldValueLength;

        private ATypeTag fieldValueTypeTag;

        @Override
        public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
            try {
                resultStorage.reset();
                eval0.evaluate(tuple, inputArg0);
                eval1.evaluate(tuple, inputArg1);
                byte[] serRecord = inputArg0.getByteArray();
                int serRecordOffset = inputArg0.getStartOffset();
                int serRecordLen = inputArg0.getLength();
                if (serRecord[serRecordOffset] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                    throw new TypeMismatchException(BuiltinFunctions.FIELD_ACCESS_BY_NAME, 0, serRecord[serRecordOffset], ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
                }
                byte[] serFldName = inputArg1.getByteArray();
                int serFldNameOffset = inputArg1.getStartOffset();
                fieldValueOffset = ARecordSerializerDeserializer.getFieldOffsetByName(serRecord, serRecordOffset, serRecordLen, serFldName, serFldNameOffset);
                if (fieldValueOffset < 0) {
                    out.writeByte(ATypeTag.SERIALIZED_MISSING_TYPE_TAG);
                    result.set(resultStorage);
                    return;
                }
                fieldValueTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serRecord[fieldValueOffset]);
                fieldValueLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, fieldValueOffset, fieldValueTypeTag, true) + 1;
                result.set(serRecord, fieldValueOffset, fieldValueLength);
            } catch (IOException e) {
                throw new HyracksDataException(e);
            } catch (AsterixException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) 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) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ATypeTag(org.apache.asterix.om.types.ATypeTag) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)

Example 82 with TypeMismatchException

use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.

the class FieldAccessNestedEvalFactory method createScalarEvaluator.

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

        private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

        private final DataOutput out = resultStorage.getDataOutput();

        private final ByteArrayAccessibleOutputStream subRecordTmpStream = new ByteArrayAccessibleOutputStream();

        private final IPointable inputArg0 = new VoidPointable();

        private final IScalarEvaluator eval0 = recordEvalFactory.createScalarEvaluator(ctx);

        private final IPointable[] fieldPointables = new VoidPointable[fieldPath.size()];

        private final RuntimeRecordTypeInfo[] recTypeInfos = new RuntimeRecordTypeInfo[fieldPath.size()];

        @SuppressWarnings("unchecked")
        private final ISerializerDeserializer<ANull> nullSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL);

        @SuppressWarnings("unchecked")
        private final ISerializerDeserializer<AMissing> missingSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AMISSING);

        {
            generateFieldsPointables();
            for (int index = 0; index < fieldPath.size(); ++index) {
                recTypeInfos[index] = new RuntimeRecordTypeInfo();
            }
        }

        @SuppressWarnings("unchecked")
        private void generateFieldsPointables() throws HyracksDataException {
            for (int i = 0; i < fieldPath.size(); i++) {
                ArrayBackedValueStorage storage = new ArrayBackedValueStorage();
                DataOutput out = storage.getDataOutput();
                AString as = new AString(fieldPath.get(i));
                SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(as.getType()).serialize(as, out);
                fieldPointables[i] = new VoidPointable();
                fieldPointables[i].set(storage);
            }
        }

        @Override
        public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
            try {
                resultStorage.reset();
                eval0.evaluate(tuple, inputArg0);
                byte[] serRecord = inputArg0.getByteArray();
                int offset = inputArg0.getStartOffset();
                int start = offset;
                int len = inputArg0.getLength();
                if (serRecord[start] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                    throw new TypeMismatchException(BuiltinFunctions.FIELD_ACCESS_NESTED, 0, serRecord[start], ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
                }
                int subFieldIndex = -1;
                int subFieldOffset = -1;
                int subFieldLength = -1;
                int nullBitmapSize = -1;
                IAType subType = recordType;
                recTypeInfos[0].reset(recordType);
                ATypeTag subTypeTag = ATypeTag.MISSING;
                boolean openField = false;
                int pathIndex = 0;
                // Moving through closed fields first.
                for (; pathIndex < fieldPointables.length; pathIndex++) {
                    if (subType.getTypeTag().equals(ATypeTag.UNION)) {
                        //enforced SubType
                        subType = ((AUnionType) subType).getActualType();
                        byte serializedTypeTag = subType.getTypeTag().serialize();
                        if (serializedTypeTag != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                            throw new UnsupportedTypeException(BuiltinFunctions.FIELD_ACCESS_NESTED.getName(), serializedTypeTag);
                        }
                        if (subType.getTypeTag() == ATypeTag.OBJECT) {
                            recTypeInfos[pathIndex].reset((ARecordType) subType);
                        }
                    }
                    subFieldIndex = recTypeInfos[pathIndex].getFieldIndex(fieldPointables[pathIndex].getByteArray(), fieldPointables[pathIndex].getStartOffset() + 1, fieldPointables[pathIndex].getLength() - 1);
                    if (subFieldIndex == -1) {
                        break;
                    }
                    nullBitmapSize = RecordUtil.computeNullBitmapSize((ARecordType) subType);
                    subFieldOffset = ARecordSerializerDeserializer.getFieldOffsetById(serRecord, start, subFieldIndex, nullBitmapSize, ((ARecordType) subType).isOpen());
                    if (subFieldOffset == 0) {
                        // the field is null, we checked the null bit map
                        // any path after null will return null.
                        nullSerde.serialize(ANull.NULL, out);
                        result.set(resultStorage);
                        return;
                    }
                    if (subFieldOffset < 0) {
                        // the field is missing, we checked the missing bit map
                        // any path after missing will return null.
                        missingSerde.serialize(AMissing.MISSING, out);
                        result.set(resultStorage);
                        return;
                    }
                    subType = ((ARecordType) subType).getFieldTypes()[subFieldIndex];
                    if (subType.getTypeTag() == ATypeTag.OBJECT && pathIndex + 1 < fieldPointables.length) {
                        // Move to the next Depth
                        recTypeInfos[pathIndex + 1].reset((ARecordType) subType);
                    }
                    if (subType.getTypeTag().equals(ATypeTag.UNION)) {
                        subTypeTag = ((AUnionType) subType).getActualType().getTypeTag();
                        subFieldLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, subFieldOffset, subTypeTag, false);
                    } else {
                        subTypeTag = subType.getTypeTag();
                        subFieldLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, subFieldOffset, subTypeTag, false);
                    }
                    if (pathIndex < fieldPointables.length - 1) {
                        //setup next iteration
                        subRecordTmpStream.reset();
                        subRecordTmpStream.write(subTypeTag.serialize());
                        subRecordTmpStream.write(serRecord, subFieldOffset, subFieldLength);
                        serRecord = subRecordTmpStream.getByteArray();
                        start = 0;
                    }
                    // type check
                    if (pathIndex < fieldPointables.length - 1 && serRecord[start] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                        throw new UnsupportedTypeException(BuiltinFunctions.FIELD_ACCESS_NESTED, serRecord[start]);
                    }
                }
                // Moving through open fields after we hit the first open field.
                for (; pathIndex < fieldPointables.length; pathIndex++) {
                    openField = true;
                    subFieldOffset = ARecordSerializerDeserializer.getFieldOffsetByName(serRecord, start, len, fieldPointables[pathIndex].getByteArray(), fieldPointables[pathIndex].getStartOffset());
                    if (subFieldOffset < 0) {
                        out.writeByte(ATypeTag.SERIALIZED_MISSING_TYPE_TAG);
                        result.set(resultStorage);
                        return;
                    }
                    subTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serRecord[subFieldOffset]);
                    subFieldLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, subFieldOffset, subTypeTag, true) + 1;
                    if (pathIndex >= fieldPointables.length - 1) {
                        continue;
                    }
                    //setup next iteration
                    start = subFieldOffset;
                    len = subFieldLength;
                    // type check
                    if (serRecord[start] == ATypeTag.SERIALIZED_MISSING_TYPE_TAG) {
                        missingSerde.serialize(AMissing.MISSING, out);
                        result.set(resultStorage);
                        return;
                    }
                    if (serRecord[start] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                        throw new UnsupportedTypeException(BuiltinFunctions.FIELD_ACCESS_NESTED.getName(), serRecord[start]);
                    }
                }
                // emit the final result.
                if (openField) {
                    result.set(serRecord, subFieldOffset, subFieldLength);
                } else {
                    out.writeByte(subTypeTag.serialize());
                    out.write(serRecord, subFieldOffset, subFieldLength);
                    result.set(resultStorage);
                }
            } catch (IOException | AsterixException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AUnionType(org.apache.asterix.om.types.AUnionType) ByteArrayAccessibleOutputStream(org.apache.hyracks.data.std.util.ByteArrayAccessibleOutputStream) 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) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ATypeTag(org.apache.asterix.om.types.ATypeTag) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) UnsupportedTypeException(org.apache.asterix.runtime.exceptions.UnsupportedTypeException) RuntimeRecordTypeInfo(org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo) AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 83 with TypeMismatchException

use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.

the class AdjustDateTimeForTimeZoneDescriptor 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(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable argPtr0 = new VoidPointable();

                private IPointable argPtr1 = new VoidPointable();

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

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

                // possible output types
                private GregorianCalendarSystem calInstance = GregorianCalendarSystem.getInstance();

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                private final UTF8StringWriter utf8Writer = new UTF8StringWriter();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    int len1 = argPtr1.getLength();
                    try {
                        if (bytes0[offset0] != ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                        }
                        if (bytes1[offset1] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        utf8Ptr.set(bytes1, offset1 + 1, len1 - 1);
                        int timezone = ATimeParserFactory.parseTimezonePart(utf8Ptr.getByteArray(), utf8Ptr.getCharStartOffset());
                        if (!calInstance.validateTimeZone(timezone)) {
                            throw new InvalidDataFormatException(getIdentifier(), "timezone");
                        }
                        long chronon = ADateTimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                        chronon = calInstance.adjustChrononByTimezone(chronon, timezone);
                        StringBuilder sbder = new StringBuilder();
                        calInstance.getExtendStringRepUntilField(chronon, timezone, sbder, Fields.YEAR, Fields.MILLISECOND, true);
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        utf8Writer.writeUTF8(sbder.toString(), out);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) 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) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) 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) UTF8StringWriter(org.apache.hyracks.util.string.UTF8StringWriter)

Example 84 with TypeMismatchException

use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.

the class AdjustTimeForTimeZoneDescriptor 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(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable argPtr0 = new VoidPointable();

                private IPointable argPtr1 = new VoidPointable();

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

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

                private GregorianCalendarSystem calInstance = GregorianCalendarSystem.getInstance();

                private final UTF8StringWriter writer = new UTF8StringWriter();

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    int len1 = argPtr1.getLength();
                    try {
                        if (bytes0[offset0] != ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_TIME_TYPE_TAG);
                        }
                        if (bytes1[offset1] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        utf8Ptr.set(argPtr1.getByteArray(), offset1 + 1, len1 - 1);
                        int timezone = ATimeParserFactory.parseTimezonePart(bytes1, utf8Ptr.getCharStartOffset());
                        if (!calInstance.validateTimeZone(timezone)) {
                            throw new InvalidDataFormatException(getIdentifier(), "timezone");
                        }
                        int chronon = ATimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                        chronon = (int) calInstance.adjustChrononByTimezone(chronon, timezone);
                        StringBuilder sbder = new StringBuilder();
                        calInstance.getExtendStringRepUntilField(chronon, timezone, sbder, Fields.HOUR, Fields.MILLISECOND, true);
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        writer.writeUTF8(sbder.toString(), out);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) 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) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) 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) UTF8StringWriter(org.apache.hyracks.util.string.UTF8StringWriter)

Example 85 with TypeMismatchException

use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.

the class CalendarDuartionFromDateDescriptor 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 IPointable argPtr1 = new VoidPointable();

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

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

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ADuration> durationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADURATION);

                private AMutableDuration aDuration = new AMutableDuration(0, 0);

                private GregorianCalendarSystem calInstanct = GregorianCalendarSystem.getInstance();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    if (bytes0[offset0] != ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATE_TYPE_TAG);
                    }
                    if (bytes1[offset1] != ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_DURATION_TYPE_TAG);
                    }
                    int yearMonthDurationInMonths = ADurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1);
                    long dayTimeDurationInMs = ADurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1);
                    long startingTimePoint = ADateSerializerDeserializer.getChronon(bytes0, offset0 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                    long endingTimePoint = DurationArithmeticOperations.addDuration(startingTimePoint, yearMonthDurationInMonths, dayTimeDurationInMs, false);
                    if (startingTimePoint == endingTimePoint) {
                        aDuration.setValue(0, 0);
                    } else {
                        boolean negative = false;
                        if (endingTimePoint < startingTimePoint) {
                            negative = true;
                            // swap the starting and ending time, so that ending time is always larger than the
                            // starting time.
                            long tmpTime = endingTimePoint;
                            endingTimePoint = startingTimePoint;
                            startingTimePoint = tmpTime;
                        }
                        int year0 = calInstanct.getYear(startingTimePoint);
                        int month0 = calInstanct.getMonthOfYear(startingTimePoint, year0);
                        int year1 = calInstanct.getYear(endingTimePoint);
                        int month1 = calInstanct.getMonthOfYear(endingTimePoint, year1);
                        int year = year1 - year0;
                        int month = month1 - month0;
                        int day = calInstanct.getDayOfMonthYear(endingTimePoint, year1, month1) - calInstanct.getDayOfMonthYear(startingTimePoint, year0, month0);
                        int hour = calInstanct.getHourOfDay(endingTimePoint) - calInstanct.getHourOfDay(startingTimePoint);
                        int min = calInstanct.getMinOfHour(endingTimePoint) - calInstanct.getMinOfHour(startingTimePoint);
                        int sec = calInstanct.getSecOfMin(endingTimePoint) - calInstanct.getSecOfMin(startingTimePoint);
                        int ms = calInstanct.getMillisOfSec(endingTimePoint) - calInstanct.getMillisOfSec(startingTimePoint);
                        if (ms < 0) {
                            ms += GregorianCalendarSystem.CHRONON_OF_SECOND;
                            sec -= 1;
                        }
                        if (sec < 0) {
                            sec += GregorianCalendarSystem.CHRONON_OF_MINUTE / GregorianCalendarSystem.CHRONON_OF_SECOND;
                            min -= 1;
                        }
                        if (min < 0) {
                            min += GregorianCalendarSystem.CHRONON_OF_HOUR / GregorianCalendarSystem.CHRONON_OF_MINUTE;
                            hour -= 1;
                        }
                        if (hour < 0) {
                            hour += GregorianCalendarSystem.CHRONON_OF_DAY / GregorianCalendarSystem.CHRONON_OF_HOUR;
                            day -= 1;
                        }
                        if (day < 0) {
                            boolean isLeapYear = calInstanct.isLeapYear(year1);
                            // need to "borrow" the days in previous month to make the day positive; when month is
                            // 1 (Jan), Dec will be borrowed
                            day += isLeapYear ? (GregorianCalendarSystem.DAYS_OF_MONTH_LEAP[(12 + month1 - 2) % 12]) : (GregorianCalendarSystem.DAYS_OF_MONTH_ORDI[(12 + month1 - 2) % 12]);
                            month -= 1;
                        }
                        if (month < 0) {
                            month += GregorianCalendarSystem.MONTHS_IN_A_YEAR;
                            year -= 1;
                        }
                        if (negative) {
                            aDuration.setValue(-1 * (year * GregorianCalendarSystem.MONTHS_IN_A_YEAR + month), -1 * (day * GregorianCalendarSystem.CHRONON_OF_DAY + hour * GregorianCalendarSystem.CHRONON_OF_HOUR + min * GregorianCalendarSystem.CHRONON_OF_MINUTE + sec * GregorianCalendarSystem.CHRONON_OF_SECOND + ms));
                        } else {
                            aDuration.setValue(year * GregorianCalendarSystem.MONTHS_IN_A_YEAR + month, day * GregorianCalendarSystem.CHRONON_OF_DAY + hour * GregorianCalendarSystem.CHRONON_OF_HOUR + min * GregorianCalendarSystem.CHRONON_OF_MINUTE + sec * GregorianCalendarSystem.CHRONON_OF_SECOND + ms);
                        }
                    }
                    durationSerde.serialize(aDuration, out);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AMutableDuration(org.apache.asterix.om.base.AMutableDuration) GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) 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)

Aggregations

TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)127 IPointable (org.apache.hyracks.data.std.api.IPointable)117 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)117 DataOutput (java.io.DataOutput)116 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)116 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)116 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)114 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)110 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)110 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)93 IOException (java.io.IOException)84 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)67 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)44 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)33 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)22 ATypeTag (org.apache.asterix.om.types.ATypeTag)18 AsterixException (org.apache.asterix.common.exceptions.AsterixException)14 GregorianCalendarSystem (org.apache.asterix.om.base.temporal.GregorianCalendarSystem)13 AMutablePoint (org.apache.asterix.om.base.AMutablePoint)11 AMutableDateTime (org.apache.asterix.om.base.AMutableDateTime)10