use of org.apache.asterix.om.base.AMutableYearMonthDuration in project asterixdb by apache.
the class GetYearMonthDurationDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private IPointable argPtr0 = new VoidPointable();
private IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<AYearMonthDuration> yearMonthDurationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AYEARMONTHDURATION);
AMutableYearMonthDuration aYearMonthDuration = new AMutableYearMonthDuration(0);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval0.evaluate(tuple, argPtr0);
byte[] bytes0 = argPtr0.getByteArray();
int offset0 = argPtr0.getStartOffset();
if (bytes0[offset0] != ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DURATION_TYPE_TAG);
}
aYearMonthDuration.setMonths(ADurationSerializerDeserializer.getYearMonth(bytes0, offset0 + 1));
yearMonthDurationSerde.serialize(aYearMonthDuration, out);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.om.base.AMutableYearMonthDuration in project asterixdb by apache.
the class ADurationParserFactory method parseDuration.
public static void parseDuration(final Object durationString, final int start, int length, IAObject mutableObject, ADurationParseOption parseOption) throws HyracksDataException {
int offset = 0;
int value = 0, hour = 0, minute = 0, second = 0, millisecond = 0, year = 0, month = 0, day = 0;
State state = State.NOTHING_READ;
IStringAccessor charAccessor;
if (durationString instanceof char[]) {
charAccessor = new IStringAccessor() {
@Override
public char getCharAt(int index) {
return ((char[]) durationString)[start + index];
}
};
} else if (durationString instanceof byte[]) {
charAccessor = new IStringAccessor() {
@Override
public char getCharAt(int index) {
return (char) (((byte[]) durationString)[start + index]);
}
};
} else if (durationString instanceof String) {
charAccessor = new IStringAccessor() {
@Override
public char getCharAt(int index) {
return ((String) durationString).charAt(start + index);
}
};
} else {
throw new HyracksDataException(durationErrorMessage);
}
short sign = 1;
if (charAccessor.getCharAt(offset) == '-') {
offset++;
sign = -1;
}
if (charAccessor.getCharAt(offset) != 'P') {
throw new HyracksDataException(durationErrorMessage + ": Missing leading 'P'.");
}
offset++;
for (; offset < length; offset++) {
if (charAccessor.getCharAt(offset) >= '0' && charAccessor.getCharAt(offset) <= '9') {
// accumulate the digit fields
value = value * DECIMAL_UNIT + charAccessor.getCharAt(offset) - '0';
} else {
switch(charAccessor.getCharAt(offset)) {
case 'Y':
if (state.compareTo(State.YEAR) < 0) {
if (parseOption == ADurationParseOption.DAY_TIME) {
throw new HyracksDataException(onlyDayTimeErrorMessage);
}
year = value;
state = State.YEAR;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong YEAR feild.");
}
break;
case 'M':
if (state.compareTo(State.TIME) < 0) {
if (state.compareTo(State.MONTH) < 0) {
if (parseOption == ADurationParseOption.DAY_TIME) {
throw new HyracksDataException(onlyDayTimeErrorMessage);
}
month = value;
state = State.MONTH;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong MONTH field.");
}
} else if (state.compareTo(State.MIN) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
minute = value;
state = State.MIN;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong MIN field.");
}
break;
case 'D':
if (state.compareTo(State.DAY) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
day = value;
state = State.DAY;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong DAY field");
}
break;
case 'T':
if (state.compareTo(State.TIME) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
state = State.TIME;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong TIME field.");
}
break;
case 'H':
if (state.compareTo(State.HOUR) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
hour = value;
state = State.HOUR;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong HOUR field.");
}
break;
case '.':
if (state.compareTo(State.MILLISEC) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
int i = 1;
for (; offset + i < length; i++) {
if (charAccessor.getCharAt(offset + i) >= '0' && charAccessor.getCharAt(offset + i) <= '9') {
if (i < 4) {
millisecond = millisecond * DECIMAL_UNIT + (charAccessor.getCharAt(offset + i) - '0');
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong MILLISECOND field.");
}
} else {
break;
}
}
offset += i;
state = State.MILLISEC;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong MILLISECOND field.");
}
case 'S':
if (state.compareTo(State.SEC) < 0) {
if (parseOption == ADurationParseOption.YEAR_MONTH) {
throw new HyracksDataException(onlyYearMonthErrorMessage);
}
second = value;
state = State.SEC;
} else {
throw new HyracksDataException(durationErrorMessage + ": wrong SECOND field.");
}
break;
default:
throw new HyracksDataException(durationErrorMessage + ": wrong format for duration.");
}
value = 0;
}
}
if (state.compareTo(State.TIME) == 0) {
throw new HyracksDataException(durationErrorMessage + ": no time fields after time separator.");
}
int totalMonths = sign * (year * 12 + month);
long totalMilliseconds = sign * (day * GregorianCalendarSystem.CHRONON_OF_DAY + hour * GregorianCalendarSystem.CHRONON_OF_HOUR + minute * GregorianCalendarSystem.CHRONON_OF_MINUTE + second * GregorianCalendarSystem.CHRONON_OF_SECOND + millisecond);
if (sign > 0) {
if (totalMonths < 0) {
throw new HyracksDataException(durationErrorMessage + ": total number of months is beyond its max value (-2147483647 to 2147483647).");
}
if (totalMilliseconds < 0) {
throw new HyracksDataException(durationErrorMessage + ": total number of milliseconds is beyond its max value (-9223372036854775808 to 9223372036854775807).");
}
}
if (mutableObject instanceof AMutableDuration) {
((AMutableDuration) mutableObject).setValue(totalMonths, totalMilliseconds);
} else if (mutableObject instanceof AMutableYearMonthDuration) {
((AMutableYearMonthDuration) mutableObject).setMonths(totalMonths);
} else if (mutableObject instanceof AMutableDayTimeDuration) {
((AMutableDayTimeDuration) mutableObject).setMilliseconds(totalMilliseconds);
}
}
use of org.apache.asterix.om.base.AMutableYearMonthDuration in project asterixdb by apache.
the class AYearMonthDurationConstructorDescriptor method createEvaluatorFactory.
@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
return new IScalarEvaluatorFactory() {
private static final long serialVersionUID = 1L;
@Override
public IScalarEvaluator createScalarEvaluator(IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput out = resultStorage.getDataOutput();
private IPointable inputArg = new VoidPointable();
private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
private AMutableYearMonthDuration aYearMonthDuration = new AMutableYearMonthDuration(0);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<AYearMonthDuration> yearMonthDurationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AYEARMONTHDURATION);
private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
try {
resultStorage.reset();
eval.evaluate(tuple, inputArg);
byte[] serString = inputArg.getByteArray();
int offset = inputArg.getStartOffset();
int len = inputArg.getLength();
if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
utf8Ptr.set(serString, offset + 1, len - 1);
int stringLength = utf8Ptr.getUTF8Length();
ADurationParserFactory.parseDuration(serString, utf8Ptr.getCharStartOffset(), stringLength, aYearMonthDuration, ADurationParseOption.YEAR_MONTH);
yearMonthDurationSerde.serialize(aYearMonthDuration, out);
} else {
throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
result.set(resultStorage);
} catch (Exception e) {
throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG);
}
}
};
}
};
}
Aggregations