use of org.apache.asterix.om.base.AMutableTime in project asterixdb by apache.
the class ParseTimeDescriptor 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<ATime> timeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ATIME);
private AMutableTime aTime = new AMutableTime(0);
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();
if (bytes0[offset0] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
if (bytes1[offset1] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
utf8Ptr.set(bytes0, offset0 + 1, len0 - 1);
int start0 = utf8Ptr.getCharStartOffset();
int length0 = utf8Ptr.getUTF8Length();
utf8Ptr.set(bytes1, offset1 + 1, len1 - 1);
int start1 = utf8Ptr.getCharStartOffset();
int length1 = utf8Ptr.getUTF8Length();
long chronon = 0;
int formatStart = start1;
int formatLength;
boolean processSuccessfully = false;
while (!processSuccessfully && formatStart < start1 + length1) {
// search for "|"
formatLength = 0;
for (; formatStart + formatLength < start1 + length1; formatLength++) {
if (bytes1[formatStart + formatLength] == '|') {
break;
}
}
try {
chronon = DT_UTILS.parseDateTime(bytes0, start0, length0, bytes1, formatStart, formatLength, DateTimeParseMode.TIME_ONLY);
} catch (AsterixTemporalTypeParseException ex) {
formatStart += formatLength + 1;
continue;
}
processSuccessfully = true;
}
if (!processSuccessfully) {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_TIME_TYPE_TAG);
}
aTime.setValue((int) chronon);
timeSerde.serialize(aTime, out);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.om.base.AMutableTime in project asterixdb by apache.
the class TimeFromDatetimeDescriptor method createEvaluatorFactory.
/* (non-Javadoc)
* @see org.apache.asterix.runtime.base.IScalarFunctionDynamicDescriptor#createEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.ICopyEvaluatorFactory[])
*/
@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 argPtr = new VoidPointable();
private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
// possible returning types
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ATime> timeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ATIME);
private AMutableTime aTime = new AMutableTime(0);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval.evaluate(tuple, argPtr);
byte[] bytes = argPtr.getByteArray();
int offset = argPtr.getStartOffset();
if (bytes[offset] != ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
}
long datetimeChronon = ADateTimeSerializerDeserializer.getChronon(bytes, offset + 1);
int timeChronon = (int) (datetimeChronon % GregorianCalendarSystem.CHRONON_OF_DAY);
if (timeChronon < 0) {
timeChronon += GregorianCalendarSystem.CHRONON_OF_DAY;
}
aTime.setValue(timeChronon);
timeSerde.serialize(aTime, out);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.om.base.AMutableTime in project asterixdb by apache.
the class ADMDataParserTest method test.
@Test
public void test() throws IOException {
String[] dates = { "-9537-08-04", "9656-06-03", "-9537-04-04", "9656-06-04", "-9537-10-04", "9626-09-05" };
AMutableDate[] parsedDates = new AMutableDate[] { new AMutableDate(-4202630), new AMutableDate(2807408), new AMutableDate(-4202752), new AMutableDate(2807409), new AMutableDate(-4202569), new AMutableDate(2796544) };
String[] times = { "12:04:45.689Z", "12:41:59.002Z", "12:10:45.169Z", "15:37:48.736Z", "04:16:42.321Z", "12:22:56.816Z" };
AMutableTime[] parsedTimes = new AMutableTime[] { new AMutableTime(43485689), new AMutableTime(45719002), new AMutableTime(43845169), new AMutableTime(56268736), new AMutableTime(15402321), new AMutableTime(44576816) };
String[] dateTimes = { "-2640-10-11T17:32:15.675Z", "4104-02-01T05:59:11.902Z", "0534-12-08T08:20:31.487Z", "6778-02-16T22:40:21.653Z", "2129-12-12T13:18:35.758Z", "8647-07-01T13:10:19.691Z" };
AMutableDateTime[] parsedDateTimes = new AMutableDateTime[] { new AMutableDateTime(-145452954464325L), new AMutableDateTime(67345192751902L), new AMutableDateTime(-45286270768513L), new AMutableDateTime(151729886421653L), new AMutableDateTime(5047449515758L), new AMutableDateTime(210721439419691L) };
Thread[] threads = new Thread[16];
AtomicInteger errorCount = new AtomicInteger(0);
for (int i = 0; i < threads.length; ++i) {
threads[i] = new Thread(new Runnable() {
ADMDataParser parser = new ADMDataParser(null, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutput dos = new DataOutputStream(bos);
@Override
public void run() {
try {
int round = 0;
while (round++ < 10000) {
// Test parseDate.
for (int index = 0; index < dates.length; ++index) {
PA.invokeMethod(parser, "parseDate(java.lang.String, java.io.DataOutput)", dates[index], dos);
AMutableDate aDate = (AMutableDate) PA.getValue(parser, "aDate");
Assert.assertTrue(aDate.equals(parsedDates[index]));
}
// Tests parseTime.
for (int index = 0; index < times.length; ++index) {
PA.invokeMethod(parser, "parseTime(java.lang.String, java.io.DataOutput)", times[index], dos);
AMutableTime aTime = (AMutableTime) PA.getValue(parser, "aTime");
Assert.assertTrue(aTime.equals(parsedTimes[index]));
}
// Tests parseDateTime.
for (int index = 0; index < dateTimes.length; ++index) {
PA.invokeMethod(parser, "parseDateTime(java.lang.String, java.io.DataOutput)", dateTimes[index], dos);
AMutableDateTime aDateTime = (AMutableDateTime) PA.getValue(parser, "aDateTime");
Assert.assertTrue(aDateTime.equals(parsedDateTimes[index]));
}
}
} catch (Exception e) {
errorCount.incrementAndGet();
e.printStackTrace();
}
}
});
// Kicks off test threads.
threads[i].start();
}
// Joins all the threads.
try {
for (int i = 0; i < threads.length; ++i) {
threads[i].join();
}
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
// Asserts no failure.
Assert.assertTrue(errorCount.get() == 0);
}
use of org.apache.asterix.om.base.AMutableTime in project asterixdb by apache.
the class TemporalIntervalEndAccessor 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);
// possible output
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<ADate> dateSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);
private final AMutableDate aDate = new AMutableDate(0);
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<ADateTime> datetimeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);
private final AMutableDateTime aDateTime = new AMutableDateTime(0);
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<ATime> timeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ATIME);
private final AMutableTime aTime = new AMutableTime(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_INTERVAL_TYPE_TAG) {
byte timeType = AIntervalSerializerDeserializer.getIntervalTimeType(bytes, startOffset + 1);
long endTime = AIntervalSerializerDeserializer.getIntervalEnd(bytes, startOffset + 1);
if (timeType == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
aDate.setValue((int) (endTime));
dateSerde.serialize(aDate, out);
} else if (timeType == ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
aTime.setValue((int) (endTime));
timeSerde.serialize(aTime, out);
} else if (timeType == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
aDateTime.setValue(endTime);
datetimeSerde.serialize(aDateTime, out);
} else {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
} else {
throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
} catch (IOException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.om.base.AMutableTime in project asterixdb by apache.
the class TemporalIntervalStartAccessor 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);
// possible output
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<ADate> dateSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);
private final AMutableDate aDate = new AMutableDate(0);
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<ADateTime> datetimeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);
private final AMutableDateTime aDateTime = new AMutableDateTime(0);
@SuppressWarnings("unchecked")
private final ISerializerDeserializer<ATime> timeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ATIME);
private final AMutableTime aTime = new AMutableTime(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_INTERVAL_TYPE_TAG) {
byte timeType = AIntervalSerializerDeserializer.getIntervalTimeType(bytes, startOffset + 1);
long startTime = AIntervalSerializerDeserializer.getIntervalStart(bytes, startOffset + 1);
if (timeType == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
aDate.setValue((int) (startTime));
dateSerde.serialize(aDate, out);
} else if (timeType == ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
aTime.setValue((int) (startTime));
timeSerde.serialize(aTime, out);
} else if (timeType == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
aDateTime.setValue(startTime);
datetimeSerde.serialize(aDateTime, out);
} else {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
} else {
throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
} catch (IOException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
}
};
}
};
}
Aggregations