Search in sources :

Example 1 with OverflowException

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

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

                private final IPointable argPtr1 = new VoidPointable();

                private final IPointable argPtr2 = new VoidPointable();

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

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

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

                // for output
                private OrderedListBuilder listBuilder = new OrderedListBuilder();

                private ArrayBackedValueStorage listStorage = new ArrayBackedValueStorage();

                protected final AOrderedListType intListType = new AOrderedListType(BuiltinType.AINTERVAL, null);

                private final AMutableInterval aInterval = new AMutableInterval(0, 0, (byte) -1);

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<AInterval> intervalSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINTERVAL);

                private final GregorianCalendarSystem gregCalSys = GregorianCalendarSystem.getInstance();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    eval2.evaluate(tuple, argPtr2);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    ATypeTag type0 = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes0[offset0]);
                    long intervalStart;
                    long intervalEnd;
                    byte intervalTypeTag;
                    if (type0 == ATypeTag.INTERVAL) {
                        intervalStart = AIntervalSerializerDeserializer.getIntervalStart(bytes0, offset0 + 1);
                        intervalEnd = AIntervalSerializerDeserializer.getIntervalEnd(bytes0, offset0 + 1);
                        intervalTypeTag = AIntervalSerializerDeserializer.getIntervalTimeType(bytes0, offset0 + 1);
                        if (intervalTypeTag == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                            intervalStart = intervalStart * GregorianCalendarSystem.CHRONON_OF_DAY;
                        }
                    } else {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                    }
                    // get the anchor instance time
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    ATypeTag type1 = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes1[offset1]);
                    if (intervalTypeTag != bytes1[offset1]) {
                        throw new IncompatibleTypeException(getIdentifier(), intervalTypeTag, bytes1[offset1]);
                    }
                    long anchorTime;
                    switch(type1) {
                        case DATE:
                            anchorTime = ADateSerializerDeserializer.getChronon(bytes1, offset1 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                            break;
                        case TIME:
                            anchorTime = ATimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                            break;
                        case DATETIME:
                            anchorTime = ADateTimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                            break;
                        default:
                            throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                    }
                    byte[] bytes2 = argPtr2.getByteArray();
                    int offset2 = argPtr2.getStartOffset();
                    ATypeTag type2 = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes2[offset2]);
                    int yearMonth = 0;
                    long dayTime = 0;
                    long firstBinIndex;
                    switch(type2) {
                        case YEARMONTHDURATION:
                            yearMonth = AYearMonthDurationSerializerDeserializer.getYearMonth(bytes2, offset2 + 1);
                            int yearStart = gregCalSys.getYear(anchorTime);
                            int monthStart = gregCalSys.getMonthOfYear(anchorTime, yearStart);
                            int yearToBin = gregCalSys.getYear(intervalStart);
                            int monthToBin = gregCalSys.getMonthOfYear(intervalStart, yearToBin);
                            int totalMonths = (yearToBin - yearStart) * 12 + (monthToBin - monthStart);
                            firstBinIndex = totalMonths / yearMonth + ((totalMonths < 0 && totalMonths % yearMonth != 0) ? -1 : 0);
                            if (firstBinIndex > Integer.MAX_VALUE) {
                                throw new OverflowException(getIdentifier());
                            }
                            if (firstBinIndex < Integer.MIN_VALUE) {
                                throw new UnderflowException(getIdentifier());
                            }
                            break;
                        case DAYTIMEDURATION:
                            dayTime = ADayTimeDurationSerializerDeserializer.getDayTime(bytes2, offset2 + 1);
                            long totalChronon = intervalStart - anchorTime;
                            firstBinIndex = totalChronon / dayTime + ((totalChronon < 0 && totalChronon % dayTime != 0) ? -1 : 0);
                            break;
                        default:
                            throw new TypeMismatchException(getIdentifier(), 2, bytes2[offset2], ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_DAY_TIME_DURATION_TYPE_TAG);
                    }
                    long binStartChronon;
                    long binEndChronon;
                    int binOffset;
                    listBuilder.reset(intListType);
                    try {
                        if (intervalTypeTag == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                            binOffset = 0;
                            do {
                                binStartChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * (int) (firstBinIndex + binOffset), dayTime * (firstBinIndex + binOffset), false);
                                binEndChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * ((int) (firstBinIndex + binOffset) + 1), dayTime * ((firstBinIndex + binOffset) + 1), false);
                                binStartChronon = binStartChronon / GregorianCalendarSystem.CHRONON_OF_DAY + ((binStartChronon < 0 && binStartChronon % GregorianCalendarSystem.CHRONON_OF_DAY != 0) ? -1 : 0);
                                binEndChronon = binEndChronon / GregorianCalendarSystem.CHRONON_OF_DAY + ((binEndChronon < 0 && binEndChronon % GregorianCalendarSystem.CHRONON_OF_DAY != 0) ? -1 : 0);
                                aInterval.setValue(binStartChronon, binEndChronon, intervalTypeTag);
                                listStorage.reset();
                                intervalSerde.serialize(aInterval, listStorage.getDataOutput());
                                listBuilder.addItem(listStorage);
                                binOffset++;
                            } while (binEndChronon < intervalEnd);
                        } else if (intervalTypeTag == ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
                            if (yearMonth != 0) {
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                            }
                            binOffset = 0;
                            binStartChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * (int) (firstBinIndex + binOffset), dayTime * (firstBinIndex + binOffset), true);
                            binEndChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * ((int) (firstBinIndex + binOffset) + 1), dayTime * ((firstBinIndex + binOffset) + 1), true);
                            if (binStartChronon < 0 || binStartChronon >= GregorianCalendarSystem.CHRONON_OF_DAY) {
                                // avoid the case where a time bin is before 00:00:00 or no early than 24:00:00
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                            }
                            while (!((binStartChronon < intervalStart && binEndChronon <= intervalStart) || (binStartChronon >= intervalEnd && binEndChronon > intervalEnd))) {
                                aInterval.setValue(binStartChronon, binEndChronon, intervalTypeTag);
                                listStorage.reset();
                                intervalSerde.serialize(aInterval, listStorage.getDataOutput());
                                listBuilder.addItem(listStorage);
                                binOffset++;
                                binStartChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * (int) (firstBinIndex + binOffset), dayTime * (firstBinIndex + binOffset), true);
                                binEndChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * ((int) (firstBinIndex + binOffset) + 1), dayTime * ((firstBinIndex + binOffset) + 1), true);
                                if (binStartChronon == GregorianCalendarSystem.CHRONON_OF_DAY) {
                                    break;
                                }
                                if (binEndChronon < binStartChronon) {
                                    throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                                }
                            }
                        } else if (intervalTypeTag == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                            binOffset = 0;
                            do {
                                binStartChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * (int) (firstBinIndex + binOffset), dayTime * (firstBinIndex + binOffset), false);
                                binEndChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * ((int) (firstBinIndex + binOffset) + 1), dayTime * ((firstBinIndex + binOffset) + 1), false);
                                aInterval.setValue(binStartChronon, binEndChronon, intervalTypeTag);
                                listStorage.reset();
                                intervalSerde.serialize(aInterval, listStorage.getDataOutput());
                                listBuilder.addItem(listStorage);
                                binOffset++;
                            } while (binEndChronon < intervalEnd);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                        }
                        listBuilder.write(out, true);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IncompatibleTypeException(org.apache.asterix.runtime.exceptions.IncompatibleTypeException) GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) IOException(java.io.IOException) 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) ATypeTag(org.apache.asterix.om.types.ATypeTag) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) UnderflowException(org.apache.asterix.runtime.exceptions.UnderflowException) AMutableInterval(org.apache.asterix.om.base.AMutableInterval) OverflowException(org.apache.asterix.runtime.exceptions.OverflowException)

Example 2 with OverflowException

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

the class AbstractNumericArithmeticEval method createEvaluatorFactory.

@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) throws AlgebricksException {
    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 argPtr0 = new VoidPointable();

                private IPointable argPtr1 = new VoidPointable();

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

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

                private double[] operandsFloating = new double[args.length];

                private long[] operandsInteger = new long[args.length];

                private int resultType;

                protected static final int typeInt8 = 1;

                protected static final int typeInt16 = 2;

                protected static final int typeInt32 = 3;

                protected static final int typeInt64 = 4;

                protected static final int typeFloat = 5;

                protected static final int typeDouble = 6;

                protected AMutableFloat aFloat = new AMutableFloat(0);

                protected AMutableDouble aDouble = new AMutableDouble(0);

                protected AMutableInt64 aInt64 = new AMutableInt64(0);

                protected AMutableInt32 aInt32 = new AMutableInt32(0);

                protected AMutableInt16 aInt16 = new AMutableInt16((short) 0);

                protected AMutableInt8 aInt8 = new AMutableInt8((byte) 0);

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

                protected AMutableDate aDate = new AMutableDate(0);

                protected AMutableTime aTime = new AMutableTime(0);

                protected AMutableDateTime aDatetime = new AMutableDateTime(0);

                private ATypeTag typeTag;

                @SuppressWarnings("rawtypes")
                private ISerializerDeserializer serde;

                @SuppressWarnings("unchecked")
                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    resultType = 0;
                    int currentType;
                    evalLeft.evaluate(tuple, argPtr0);
                    evalRight.evaluate(tuple, argPtr1);
                    for (int i = 0; i < args.length; i++) {
                        IPointable argPtr = i == 0 ? argPtr0 : argPtr1;
                        byte[] bytes = argPtr.getByteArray();
                        int offset = argPtr.getStartOffset();
                        typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes[offset]);
                        switch(typeTag) {
                            case TINYINT:
                                currentType = typeInt8;
                                operandsInteger[i] = AInt8SerializerDeserializer.getByte(bytes, offset + 1);
                                operandsFloating[i] = operandsInteger[i];
                                break;
                            case SMALLINT:
                                currentType = typeInt16;
                                operandsInteger[i] = AInt16SerializerDeserializer.getShort(bytes, offset + 1);
                                operandsFloating[i] = operandsInteger[i];
                                break;
                            case INTEGER:
                                currentType = typeInt32;
                                operandsInteger[i] = AInt32SerializerDeserializer.getInt(bytes, offset + 1);
                                operandsFloating[i] = operandsInteger[i];
                                break;
                            case BIGINT:
                                currentType = typeInt64;
                                operandsInteger[i] = AInt64SerializerDeserializer.getLong(bytes, offset + 1);
                                operandsFloating[i] = operandsInteger[i];
                                break;
                            case FLOAT:
                                currentType = typeFloat;
                                operandsFloating[i] = AFloatSerializerDeserializer.getFloat(bytes, offset + 1);
                                break;
                            case DOUBLE:
                                currentType = typeDouble;
                                operandsFloating[i] = ADoubleSerializerDeserializer.getDouble(bytes, offset + 1);
                                break;
                            case DATE:
                            case TIME:
                            case DATETIME:
                            case DURATION:
                            case YEARMONTHDURATION:
                            case DAYTIMEDURATION:
                                evaluateTemporalArthmeticOperation(typeTag);
                                result.set(resultStorage);
                                return;
                            default:
                                throw new TypeMismatchException(getIdentifier(), i, bytes[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, ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG, ATypeTag.SERIALIZED_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_DAY_TIME_DURATION_TYPE_TAG);
                        }
                        if (resultType < currentType) {
                            resultType = currentType;
                        }
                    }
                    long lres;
                    double dres;
                    switch(resultType) {
                        case typeInt8:
                            serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT8);
                            lres = evaluateInteger(operandsInteger[0], operandsInteger[1]);
                            if (lres > Byte.MAX_VALUE) {
                                throw new OverflowException(getIdentifier());
                            }
                            if (lres < Byte.MIN_VALUE) {
                                throw new UnderflowException(getIdentifier());
                            }
                            aInt8.setValue((byte) lres);
                            serde.serialize(aInt8, out);
                            break;
                        case typeInt16:
                            serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT16);
                            lres = evaluateInteger(operandsInteger[0], operandsInteger[1]);
                            if (lres > Short.MAX_VALUE) {
                                throw new OverflowException(getIdentifier());
                            }
                            if (lres < Short.MIN_VALUE) {
                                throw new UnderflowException(getIdentifier());
                            }
                            aInt16.setValue((short) lres);
                            serde.serialize(aInt16, out);
                            break;
                        case typeInt32:
                            serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
                            lres = evaluateInteger(operandsInteger[0], operandsInteger[1]);
                            if (lres > Integer.MAX_VALUE) {
                                throw new OverflowException(getIdentifier());
                            }
                            if (lres < Integer.MIN_VALUE) {
                                throw new UnderflowException(getIdentifier());
                            }
                            aInt32.setValue((int) lres);
                            serde.serialize(aInt32, out);
                            break;
                        case typeInt64:
                            serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
                            lres = evaluateInteger(operandsInteger[0], operandsInteger[1]);
                            aInt64.setValue(lres);
                            serde.serialize(aInt64, out);
                            break;
                        case typeFloat:
                            serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);
                            dres = evaluateDouble(operandsFloating[0], operandsFloating[1]);
                            if (dres > Float.MAX_VALUE) {
                                throw new OverflowException(getIdentifier());
                            }
                            if (dres < -Float.MAX_VALUE) {
                                throw new UnderflowException(getIdentifier());
                            }
                            aFloat.setValue((float) dres);
                            serde.serialize(aFloat, out);
                            break;
                        case typeDouble:
                            serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
                            aDouble.setValue(evaluateDouble(operandsFloating[0], operandsFloating[1]));
                            serde.serialize(aDouble, out);
                            break;
                    }
                    result.set(resultStorage);
                }

                @SuppressWarnings("unchecked")
                private void evaluateTemporalArthmeticOperation(ATypeTag leftType) throws HyracksDataException {
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    ATypeTag rightType = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes1[offset1]);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    if (rightType == leftType) {
                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADURATION);
                        long leftChronon = 0, rightChronon = 0, dayTime = 0;
                        int yearMonth = 0;
                        switch(leftType) {
                            case DATE:
                                leftChronon = ADateSerializerDeserializer.getChronon(bytes0, offset0 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                                rightChronon = ADateSerializerDeserializer.getChronon(bytes1, offset1 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                                break;
                            case TIME:
                                leftChronon = ATimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                                rightChronon = ATimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                                break;
                            case DATETIME:
                                leftChronon = ADateTimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                                rightChronon = ADateTimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                                break;
                            case YEARMONTHDURATION:
                                yearMonth = (int) evaluateTimeInstanceArithmetic(AYearMonthDurationSerializerDeserializer.getYearMonth(bytes0, offset0 + 1), AYearMonthDurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1));
                                break;
                            case DAYTIMEDURATION:
                                leftChronon = ADayTimeDurationSerializerDeserializer.getDayTime(bytes0, offset0 + 1);
                                rightChronon = ADayTimeDurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1);
                                break;
                            default:
                                throw new UnsupportedTypeException(getIdentifier(), bytes1[offset1]);
                        }
                        dayTime = evaluateTimeInstanceArithmetic(leftChronon, rightChronon);
                        aDuration.setValue(yearMonth, dayTime);
                        serde.serialize(aDuration, out);
                    } else {
                        long chronon = 0, dayTime = 0;
                        int yearMonth = 0;
                        ATypeTag resultType = null;
                        boolean isTimeOnly = false;
                        switch(leftType) {
                            case TIME:
                                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ATIME);
                                chronon = ATimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                                isTimeOnly = true;
                                resultType = ATypeTag.TIME;
                                switch(rightType) {
                                    case DAYTIMEDURATION:
                                        dayTime = ADayTimeDurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1);
                                        break;
                                    case DURATION:
                                        dayTime = ADurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1);
                                        yearMonth = ADurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1);
                                        break;
                                    default:
                                        throw new IncompatibleTypeException(getIdentifier(), bytes0[offset0], bytes1[offset1]);
                                }
                                break;
                            case DATE:
                                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);
                                resultType = ATypeTag.DATE;
                                chronon = ADateSerializerDeserializer.getChronon(bytes0, offset0 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                            case DATETIME:
                                if (leftType == ATypeTag.DATETIME) {
                                    serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);
                                    resultType = ATypeTag.DATETIME;
                                    chronon = ADateTimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                                }
                                switch(rightType) {
                                    case DURATION:
                                        yearMonth = ADurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1);
                                        dayTime = ADurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1);
                                        break;
                                    case YEARMONTHDURATION:
                                        yearMonth = AYearMonthDurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1);
                                        break;
                                    case DAYTIMEDURATION:
                                        dayTime = ADayTimeDurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1);
                                        break;
                                    default:
                                        throw new IncompatibleTypeException(getIdentifier(), bytes0[offset0], bytes1[offset1]);
                                }
                                break;
                            case YEARMONTHDURATION:
                                yearMonth = AYearMonthDurationSerializerDeserializer.getYearMonth(bytes0, offset0 + 1);
                                switch(rightType) {
                                    case DATETIME:
                                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);
                                        resultType = ATypeTag.DATETIME;
                                        chronon = ADateTimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                                        break;
                                    case DATE:
                                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);
                                        resultType = ATypeTag.DATE;
                                        chronon = ADateSerializerDeserializer.getChronon(bytes1, offset1 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                                        break;
                                    default:
                                        throw new IncompatibleTypeException(getIdentifier(), bytes0[offset0], bytes1[offset1]);
                                }
                                break;
                            case DURATION:
                                yearMonth = ADurationSerializerDeserializer.getYearMonth(bytes0, offset0 + 1);
                                dayTime = ADurationSerializerDeserializer.getDayTime(bytes0, offset0 + 1);
                            case DAYTIMEDURATION:
                                if (leftType == ATypeTag.DAYTIMEDURATION) {
                                    dayTime = ADayTimeDurationSerializerDeserializer.getDayTime(bytes0, offset0 + 1);
                                }
                                switch(rightType) {
                                    case DATETIME:
                                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);
                                        resultType = ATypeTag.DATETIME;
                                        chronon = ADateTimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                                        break;
                                    case DATE:
                                        serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);
                                        resultType = ATypeTag.DATE;
                                        chronon = ADateSerializerDeserializer.getChronon(bytes1, offset1 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                                        break;
                                    case TIME:
                                        if (yearMonth == 0) {
                                            serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ATIME);
                                            resultType = ATypeTag.TIME;
                                            chronon = ATimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                                            isTimeOnly = true;
                                            break;
                                        }
                                    default:
                                        throw new IncompatibleTypeException(getIdentifier(), bytes0[offset0], bytes1[offset1]);
                                }
                                break;
                            default:
                                throw new IncompatibleTypeException(getIdentifier(), bytes0[offset0], bytes1[offset1]);
                        }
                        chronon = evaluateTimeDurationArithmetic(chronon, yearMonth, dayTime, isTimeOnly);
                        switch(resultType) {
                            case DATE:
                                if (chronon < 0 && chronon % GregorianCalendarSystem.CHRONON_OF_DAY != 0) {
                                    chronon = chronon / GregorianCalendarSystem.CHRONON_OF_DAY - 1;
                                } else {
                                    chronon = chronon / GregorianCalendarSystem.CHRONON_OF_DAY;
                                }
                                aDate.setValue((int) chronon);
                                serde.serialize(aDate, out);
                                break;
                            case TIME:
                                aTime.setValue((int) chronon);
                                serde.serialize(aTime, out);
                                break;
                            case DATETIME:
                                aDatetime.setValue(chronon);
                                serde.serialize(aDatetime, out);
                                break;
                            default:
                                throw new IncompatibleTypeException(getIdentifier(), bytes0[offset0], bytes1[offset1]);
                        }
                    }
                }
            };
        }
    };
}
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) AMutableDate(org.apache.asterix.om.base.AMutableDate) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) IncompatibleTypeException(org.apache.asterix.runtime.exceptions.IncompatibleTypeException) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) AMutableFloat(org.apache.asterix.om.base.AMutableFloat) AMutableDuration(org.apache.asterix.om.base.AMutableDuration) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) AMutableDateTime(org.apache.asterix.om.base.AMutableDateTime) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) ATypeTag(org.apache.asterix.om.types.ATypeTag) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) UnsupportedTypeException(org.apache.asterix.runtime.exceptions.UnsupportedTypeException) AMutableInt16(org.apache.asterix.om.base.AMutableInt16) UnderflowException(org.apache.asterix.runtime.exceptions.UnderflowException) AMutableTime(org.apache.asterix.om.base.AMutableTime) OverflowException(org.apache.asterix.runtime.exceptions.OverflowException) AMutableInt32(org.apache.asterix.om.base.AMutableInt32)

Example 3 with OverflowException

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

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

                private final IPointable argPtr1 = new VoidPointable();

                private final IPointable argPtr2 = new VoidPointable();

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

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

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

                private final AMutableInterval aInterval = new AMutableInterval(0, 0, (byte) -1);

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<AInterval> intervalSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINTERVAL);

                private final GregorianCalendarSystem GREG_CAL = GregorianCalendarSystem.getInstance();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    eval2.evaluate(tuple, argPtr2);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    ATypeTag type0 = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes0[offset0]);
                    long chrononToBin = 0;
                    switch(type0) {
                        case DATE:
                            chrononToBin = ADateSerializerDeserializer.getChronon(bytes0, offset0 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                            break;
                        case TIME:
                            chrononToBin = ATimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                            break;
                        case DATETIME:
                            chrononToBin = ADateTimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                            break;
                        default:
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                    }
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    ATypeTag type1 = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes1[offset1]);
                    if (type0 != type1) {
                        throw new IncompatibleTypeException(getIdentifier(), bytes0[offset0], bytes1[offset1]);
                    }
                    long chrononToStart = 0;
                    switch(type1) {
                        case DATE:
                            chrononToStart = ADateSerializerDeserializer.getChronon(bytes1, offset1 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                            break;
                        case TIME:
                            chrononToStart = ATimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                            break;
                        case DATETIME:
                            chrononToStart = ADateTimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                            break;
                        default:
                            throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                    }
                    byte[] bytes2 = argPtr2.getByteArray();
                    int offset2 = argPtr2.getStartOffset();
                    ATypeTag type2 = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes2[offset2]);
                    int yearMonth = 0;
                    long dayTime = 0;
                    long binIndex, binStartChronon, binEndChronon;
                    switch(type2) {
                        case YEARMONTHDURATION:
                            yearMonth = AYearMonthDurationSerializerDeserializer.getYearMonth(bytes2, offset2 + 1);
                            int yearStart = GREG_CAL.getYear(chrononToStart);
                            int monthStart = GREG_CAL.getMonthOfYear(chrononToStart, yearStart);
                            int yearToBin = GREG_CAL.getYear(chrononToBin);
                            int monthToBin = GREG_CAL.getMonthOfYear(chrononToBin, yearToBin);
                            int totalMonths = (yearToBin - yearStart) * 12 + (monthToBin - monthStart);
                            binIndex = totalMonths / yearMonth + ((totalMonths < 0 && totalMonths % yearMonth != 0) ? -1 : 0);
                            if (binIndex > Integer.MAX_VALUE) {
                                throw new OverflowException(getIdentifier());
                            }
                            if (binIndex < Integer.MIN_VALUE) {
                                throw new UnderflowException(getIdentifier());
                            }
                            break;
                        case DAYTIMEDURATION:
                            dayTime = ADayTimeDurationSerializerDeserializer.getDayTime(bytes2, offset2 + 1);
                            long totalChronon = chrononToBin - chrononToStart;
                            binIndex = totalChronon / dayTime + ((totalChronon < 0 && totalChronon % dayTime != 0) ? -1 : 0);
                            break;
                        default:
                            throw new TypeMismatchException(getIdentifier().getName(), 2, bytes2[offset2], ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_DAY_TIME_DURATION_TYPE_TAG);
                    }
                    switch(type0) {
                        case DATE:
                            binStartChronon = DurationArithmeticOperations.addDuration(chrononToStart, yearMonth * (int) binIndex, dayTime * binIndex, false);
                            binEndChronon = DurationArithmeticOperations.addDuration(chrononToStart, yearMonth * ((int) binIndex + 1), dayTime * (binIndex + 1), false);
                            binStartChronon = binStartChronon / GregorianCalendarSystem.CHRONON_OF_DAY + ((binStartChronon < 0 && binStartChronon % GregorianCalendarSystem.CHRONON_OF_DAY != 0) ? -1 : 0);
                            binEndChronon = binEndChronon / GregorianCalendarSystem.CHRONON_OF_DAY + ((binEndChronon < 0 && binEndChronon % GregorianCalendarSystem.CHRONON_OF_DAY != 0) ? -1 : 0);
                            break;
                        case TIME:
                            if (yearMonth != 0) {
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                            }
                            binStartChronon = DurationArithmeticOperations.addDuration(chrononToStart, yearMonth * (int) binIndex, dayTime * binIndex, true);
                            binEndChronon = DurationArithmeticOperations.addDuration(chrononToStart, yearMonth * ((int) binIndex + 1), dayTime * (binIndex + 1), true);
                            break;
                        case DATETIME:
                            binStartChronon = DurationArithmeticOperations.addDuration(chrononToStart, yearMonth * (int) binIndex, dayTime * binIndex, false);
                            binEndChronon = DurationArithmeticOperations.addDuration(chrononToStart, yearMonth * ((int) binIndex + 1), dayTime * (binIndex + 1), false);
                            break;
                        default:
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                    }
                    aInterval.setValue(binStartChronon, binEndChronon, type0.serialize());
                    intervalSerde.serialize(aInterval, out);
                    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) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) ATypeTag(org.apache.asterix.om.types.ATypeTag) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) IncompatibleTypeException(org.apache.asterix.runtime.exceptions.IncompatibleTypeException) UnderflowException(org.apache.asterix.runtime.exceptions.UnderflowException) AMutableInterval(org.apache.asterix.om.base.AMutableInterval) OverflowException(org.apache.asterix.runtime.exceptions.OverflowException)

Aggregations

DataOutput (java.io.DataOutput)3 ATypeTag (org.apache.asterix.om.types.ATypeTag)3 IncompatibleTypeException (org.apache.asterix.runtime.exceptions.IncompatibleTypeException)3 OverflowException (org.apache.asterix.runtime.exceptions.OverflowException)3 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)3 UnderflowException (org.apache.asterix.runtime.exceptions.UnderflowException)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 AMutableInterval (org.apache.asterix.om.base.AMutableInterval)2 GregorianCalendarSystem (org.apache.asterix.om.base.temporal.GregorianCalendarSystem)2 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)2 IOException (java.io.IOException)1 OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)1 AMutableDate (org.apache.asterix.om.base.AMutableDate)1