Search in sources :

Example 1 with GregorianCalendarSystem

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

the class PrintTools method printYearMonthDurationString.

public static void printYearMonthDurationString(byte[] b, int s, int l, PrintStream ps) throws HyracksDataException {
    final GregorianCalendarSystem gCalInstance = GregorianCalendarSystem.getInstance();
    boolean positive = true;
    int months = AInt32SerializerDeserializer.getInt(b, s + 1);
    // set the negative flag. "||" is necessary in case that months field is not there (so it is 0)
    if (months < 0) {
        months *= -1;
        positive = false;
    }
    int month = gCalInstance.getDurationMonth(months);
    int year = gCalInstance.getDurationYear(months);
    if (!positive) {
        ps.print("-");
    }
    try {
        ps.print("P");
        if (year != 0) {
            WriteValueTools.writeInt(year, ps);
            ps.print("Y");
        }
        if (month != 0) {
            WriteValueTools.writeInt(month, ps);
            ps.print("M");
        }
    } catch (IOException e) {
        throw new HyracksDataException(e);
    }
}
Also used : GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 2 with GregorianCalendarSystem

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

the class CalendarDurationFromDateTimeDescriptor 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_DATETIME_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATETIME_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 = ADateTimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                    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)

Example 3 with GregorianCalendarSystem

use of org.apache.asterix.om.base.temporal.GregorianCalendarSystem 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 4 with GregorianCalendarSystem

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

the class TemporalYearAccessor 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 final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                private final IPointable argPtr = new VoidPointable();

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

                private final GregorianCalendarSystem calSystem = GregorianCalendarSystem.getInstance();

                private final UTF8StringPointable strExprPtr = new UTF8StringPointable();

                private final UTF8StringCharacterIterator strIter = new UTF8StringCharacterIterator();

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

                private final AMutableInt64 aMutableInt64 = new AMutableInt64(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    eval.evaluate(tuple, argPtr);
                    byte[] bytes = argPtr.getByteArray();
                    int startOffset = argPtr.getStartOffset();
                    int len = argPtr.getLength();
                    resultStorage.reset();
                    try {
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
                            aMutableInt64.setValue(calSystem.getDurationYear(ADurationSerializerDeserializer.getYearMonth(bytes, startOffset + 1)));
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        }
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG) {
                            aMutableInt64.setValue(calSystem.getDurationYear(AYearMonthDurationSerializerDeserializer.getYearMonth(bytes, startOffset + 1)));
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        }
                        long chrononTimeInMs = 0;
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                            chrononTimeInMs = AInt32SerializerDeserializer.getInt(bytes, startOffset + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                        } else if (bytes[startOffset] == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                            chrononTimeInMs = AInt64SerializerDeserializer.getLong(bytes, startOffset + 1);
                        } else if (bytes[startOffset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            int year;
                            strExprPtr.set(bytes, startOffset + 1, len - 1);
                            strIter.reset(strExprPtr);
                            char firstChar = strIter.next();
                            if (firstChar == '-') {
                                // in case of a negative year
                                year = -1 * ((strIter.next() - '0') * 1000 + (strIter.next() - '0') * 100 + (strIter.next() - '0') * 10 + (strIter.next() - '0'));
                            } else {
                                year = (firstChar - '0') * 1000 + (strIter.next() - '0') * 100 + (strIter.next() - '0') * 10 + (strIter.next() - '0');
                            }
                            aMutableInt64.setValue(year);
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG, ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        int year = calSystem.getYear(chrononTimeInMs);
                        aMutableInt64.setValue(year);
                        intSerde.serialize(aMutableInt64, out);
                        result.set(resultStorage);
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                }
            };
        }
    };
}
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) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) UTF8StringCharacterIterator(org.apache.hyracks.data.std.util.UTF8StringCharacterIterator) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 5 with GregorianCalendarSystem

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

the class TemporalHourAccessor method createEvaluatorFactory.

/* (non-Javadoc)
     * @see org.apache.asterix.om.function.IFunctionDescriptor#createEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory[])
     */
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IScalarEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

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

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                private final IPointable argPtr = new VoidPointable();

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

                private final GregorianCalendarSystem calSystem = GregorianCalendarSystem.getInstance();

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

                private final AMutableInt64 aMutableInt64 = new AMutableInt64(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    eval.evaluate(tuple, argPtr);
                    byte[] bytes = argPtr.getByteArray();
                    int startOffset = argPtr.getStartOffset();
                    resultStorage.reset();
                    try {
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
                            aMutableInt64.setValue(calSystem.getDurationHour(ADurationSerializerDeserializer.getDayTime(bytes, startOffset + 1)));
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        }
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_DAY_TIME_DURATION_TYPE_TAG) {
                            aMutableInt64.setValue(calSystem.getDurationHour(ADayTimeDurationSerializerDeserializer.getDayTime(bytes, startOffset + 1)));
                            intSerde.serialize(aMutableInt64, out);
                            result.set(resultStorage);
                            return;
                        }
                        long chrononTimeInMs = 0;
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
                            chrononTimeInMs = AInt32SerializerDeserializer.getInt(bytes, startOffset + 1);
                        } else if (bytes[startOffset] == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                            chrononTimeInMs = AInt64SerializerDeserializer.getLong(bytes, startOffset + 1);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_DAY_TIME_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                        }
                        int hour = calSystem.getHourOfDay(chrononTimeInMs);
                        aMutableInt64.setValue(hour);
                        intSerde.serialize(aMutableInt64, out);
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Aggregations

GregorianCalendarSystem (org.apache.asterix.om.base.temporal.GregorianCalendarSystem)14 DataOutput (java.io.DataOutput)13 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)13 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)13 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)13 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)13 IPointable (org.apache.hyracks.data.std.api.IPointable)13 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)13 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)13 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)13 IOException (java.io.IOException)11 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)11 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)11 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)7 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)4 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)3 AMutableDuration (org.apache.asterix.om.base.AMutableDuration)2 AMutableInterval (org.apache.asterix.om.base.AMutableInterval)2 ATypeTag (org.apache.asterix.om.types.ATypeTag)2 IncompatibleTypeException (org.apache.asterix.runtime.exceptions.IncompatibleTypeException)2