use of org.apache.asterix.om.base.AMutableDuration 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);
}
};
}
};
}
use of org.apache.asterix.om.base.AMutableDuration in project asterixdb by apache.
the class DurationFromMillisecondsDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private IPointable argPtr0 = new VoidPointable();
private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ADuration> durationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADURATION);
AMutableDuration aDuration = new AMutableDuration(0, 0);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval0.evaluate(tuple, argPtr0);
byte[] bytes = argPtr0.getByteArray();
int offset = argPtr0.getStartOffset();
ATypeTag argPtrTypeTag = ATypeTag.VALUE_TYPE_MAPPING[bytes[offset]];
switch(argPtrTypeTag) {
case TINYINT:
aDuration.setValue(0, AInt8SerializerDeserializer.getByte(bytes, offset + 1));
break;
case SMALLINT:
aDuration.setValue(0, AInt16SerializerDeserializer.getShort(bytes, offset + 1));
break;
case INTEGER:
aDuration.setValue(0, AInt32SerializerDeserializer.getInt(bytes, offset + 1));
break;
case BIGINT:
aDuration.setValue(0, AInt64SerializerDeserializer.getLong(bytes, offset + 1));
break;
default:
throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG);
}
durationSerde.serialize(aDuration, out);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.om.base.AMutableDuration in project asterixdb by apache.
the class DurationFromMonthsDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private IPointable argPtr0 = new VoidPointable();
private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ADuration> durationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADURATION);
AMutableDuration aDuration = new AMutableDuration(0, 0);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval0.evaluate(tuple, argPtr0);
byte[] bytes = argPtr0.getByteArray();
int offset = argPtr0.getStartOffset();
aDuration.setValue(ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 0, bytes, offset), 0);
durationSerde.serialize(aDuration, out);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.om.base.AMutableDuration in project asterixdb by apache.
the class ADurationParserFactory method parseDuration.
public static void parseDuration(final Object durationString, final int start, int length, IAObject mutableObject, ADurationParseOption parseOption) throws HyracksDataException {
int offset = 0;
int value = 0, hour = 0, minute = 0, second = 0, millisecond = 0, year = 0, month = 0, day = 0;
State state = State.NOTHING_READ;
IStringAccessor charAccessor;
if (durationString instanceof char[]) {
charAccessor = new IStringAccessor() {
@Override
public char getCharAt(int index) {
return ((char[]) durationString)[start + index];
}
};
} else if (durationString instanceof byte[]) {
charAccessor = new IStringAccessor() {
@Override
public char getCharAt(int index) {
return (char) (((byte[]) durationString)[start + index]);
}
};
} else if (durationString instanceof String) {
charAccessor = new IStringAccessor() {
@Override
public char getCharAt(int index) {
return ((String) durationString).charAt(start + index);
}
};
} else {
throw new HyracksDataException(durationErrorMessage);
}
short sign = 1;
if (charAccessor.getCharAt(offset) == '-') {
offset++;
sign = -1;
}
if (charAccessor.getCharAt(offset) != 'P') {
throw new HyracksDataException(durationErrorMessage + ": Missing leading 'P'.");
}
offset++;
for (; offset < length; offset++) {
if (charAccessor.getCharAt(offset) >= '0' && charAccessor.getCharAt(offset) <= '9') {
// accumulate the digit fields
value = value * DECIMAL_UNIT + charAccessor.getCharAt(offset) - '0';
} else {
switch(charAccessor.getCharAt(offset)) {
case 'Y':
if (state.compareTo(State.YEAR) < 0) {
if (parseOption == ADurationParseOption.DAY_TIME) {
throw new HyracksDataException(onlyDayTimeErrorMessage);
}
year = value;
state = State.YEAR;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong YEAR feild.");
}
break;
case 'M':
if (state.compareTo(State.TIME) < 0) {
if (state.compareTo(State.MONTH) < 0) {
if (parseOption == ADurationParseOption.DAY_TIME) {
throw new HyracksDataException(onlyDayTimeErrorMessage);
}
month = value;
state = State.MONTH;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong MONTH field.");
}
} else if (state.compareTo(State.MIN) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
minute = value;
state = State.MIN;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong MIN field.");
}
break;
case 'D':
if (state.compareTo(State.DAY) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
day = value;
state = State.DAY;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong DAY field");
}
break;
case 'T':
if (state.compareTo(State.TIME) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
state = State.TIME;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong TIME field.");
}
break;
case 'H':
if (state.compareTo(State.HOUR) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
hour = value;
state = State.HOUR;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong HOUR field.");
}
break;
case '.':
if (state.compareTo(State.MILLISEC) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
int i = 1;
for (; offset + i < length; i++) {
if (charAccessor.getCharAt(offset + i) >= '0' && charAccessor.getCharAt(offset + i) <= '9') {
if (i < 4) {
millisecond = millisecond * DECIMAL_UNIT + (charAccessor.getCharAt(offset + i) - '0');
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong MILLISECOND field.");
}
} else {
break;
}
}
offset += i;
state = State.MILLISEC;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong MILLISECOND field.");
}
case 'S':
if (state.compareTo(State.SEC) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
second = value;
state = State.SEC;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong SECOND field.");
}
break;
default:
throw new HyracksDataException(durationErrorMessage + ": wrong format for duration.");
}
value = 0;
}
}
if (state.compareTo(State.TIME) == 0) {
throw new HyracksDataException(durationErrorMessage + ": no time fields after time separator.");
}
int totalMonths = sign * (year * 12 + month);
long totalMilliseconds = sign * (day * GregorianCalendarSystem.CHRONON_OF_DAY + hour * GregorianCalendarSystem.CHRONON_OF_HOUR + minute * GregorianCalendarSystem.CHRONON_OF_MINUTE + second * GregorianCalendarSystem.CHRONON_OF_SECOND + millisecond);
if (sign > 0) {
if (totalMonths < 0) {
throw new HyracksDataException(durationErrorMessage + ": total number of months is beyond its max value (-2147483647 to 2147483647).");
}
if (totalMilliseconds < 0) {
throw new HyracksDataException(durationErrorMessage + ": total number of milliseconds is beyond its max value (-9223372036854775808 to 9223372036854775807).");
}
}
if (mutableObject instanceof AMutableDuration) {
((AMutableDuration) mutableObject).setValue(totalMonths, totalMilliseconds);
} else if (mutableObject instanceof AMutableYearMonthDuration) {
((AMutableYearMonthDuration) mutableObject).setMonths(totalMonths);
} else if (mutableObject instanceof AMutableDayTimeDuration) {
((AMutableDayTimeDuration) mutableObject).setMilliseconds(totalMilliseconds);
}
}
use of org.apache.asterix.om.base.AMutableDuration in project asterixdb by apache.
the class AIntervalStartFromDateConstructorDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private IPointable argPtr0 = new VoidPointable();
private IPointable argPtr1 = new VoidPointable();
private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
private IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
private AMutableInterval aInterval = new AMutableInterval(0L, 0L, (byte) 0);
private AMutableDuration aDuration = new AMutableDuration(0, 0L);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<AInterval> intervalSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINTERVAL);
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();
int len0 = argPtr0.getLength();
byte[] bytes1 = argPtr1.getByteArray();
int offset1 = argPtr1.getStartOffset();
int len1 = argPtr1.getLength();
try {
long intervalStart = 0, intervalEnd = 0;
if (bytes0[offset0] == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
intervalStart = ADateSerializerDeserializer.getChronon(bytes0, offset0 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
} else if (bytes0[offset0] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
utf8Ptr.set(bytes0, offset0 + 1, len0 - 1);
// start date
int stringLength = utf8Ptr.getUTF8Length();
int startOffset = utf8Ptr.getCharStartOffset();
intervalStart = ADateParserFactory.parseDatePart(bytes0, startOffset, stringLength);
} else {
throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
if (bytes1[offset1] == ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
intervalEnd = DurationArithmeticOperations.addDuration(intervalStart, ADurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1), ADurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1), false);
} else if (bytes1[offset1] == ATypeTag.SERIALIZED_DAY_TIME_DURATION_TYPE_TAG) {
intervalEnd = DurationArithmeticOperations.addDuration(intervalStart, 0, ADayTimeDurationSerializerDeserializer.getDayTime(bytes1, offset1 + 1), false);
} else if (bytes1[offset1] == ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG) {
intervalEnd = DurationArithmeticOperations.addDuration(intervalStart, AYearMonthDurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1), 0, false);
} else if (bytes1[offset1] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
// duration
utf8Ptr.set(bytes1, offset1 + 1, len1 - 1);
int stringLength = utf8Ptr.getUTF8Length();
ADurationParserFactory.parseDuration(bytes1, utf8Ptr.getCharStartOffset(), stringLength, aDuration, ADurationParseOption.All);
intervalEnd = DurationArithmeticOperations.addDuration(intervalStart, aDuration.getMonths(), aDuration.getMilliseconds(), false);
} else {
throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
intervalStart = GregorianCalendarSystem.getChrononInDays(intervalStart);
intervalEnd = GregorianCalendarSystem.getChrononInDays(intervalEnd);
if (intervalEnd < intervalStart) {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
aInterval.setValue(intervalStart, intervalEnd, ATypeTag.SERIALIZED_DATE_TYPE_TAG);
intervalSerde.serialize(aInterval, out);
} catch (IOException e) {
throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
result.set(resultStorage);
}
};
}
};
}
Aggregations