use of org.apache.hyracks.data.std.util.UTF8StringCharacterIterator 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);
}
}
};
}
};
}
Aggregations