use of org.apache.asterix.runtime.exceptions.TypeMismatchException 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.runtime.exceptions.TypeMismatchException in project asterixdb by apache.
the class DatetimeFromUnixTimeInSecsDescriptor 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 output types
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ADateTime> datetimeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);
private AMutableDateTime aDatetime = new AMutableDateTime(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();
ATypeTag argPtrTypeTag = ATypeTag.VALUE_TYPE_MAPPING[bytes[offset]];
switch(argPtrTypeTag) {
case TINYINT:
aDatetime.setValue(AInt8SerializerDeserializer.getByte(bytes, offset + 1) * 1000l);
break;
case SMALLINT:
aDatetime.setValue(AInt16SerializerDeserializer.getShort(bytes, offset + 1) * 1000l);
break;
case INTEGER:
aDatetime.setValue(AInt32SerializerDeserializer.getInt(bytes, offset + 1) * 1000l);
break;
case BIGINT:
aDatetime.setValue(AInt64SerializerDeserializer.getLong(bytes, offset + 1) * 1000l);
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);
}
datetimeSerde.serialize(aDatetime, out);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.
the class DayOfWeekDescriptor 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 argPtr = new VoidPointable();
private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
// possible returning types
@SuppressWarnings("unchecked")
private ISerializerDeserializer<AInt64> int64Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
private AMutableInt64 aInt64 = new AMutableInt64(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();
int daysSinceAnchor;
int reminder = 0;
if (bytes[offset] == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
daysSinceAnchor = (int) (ADateTimeSerializerDeserializer.getChronon(bytes, offset + 1) / GregorianCalendarSystem.CHRONON_OF_DAY);
reminder = (int) (ADateTimeSerializerDeserializer.getChronon(bytes, offset + 1) % GregorianCalendarSystem.CHRONON_OF_DAY);
} else if (bytes[offset] == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
daysSinceAnchor = ADateSerializerDeserializer.getChronon(bytes, offset + 1);
} else {
throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_DATETIME_TYPE_TAG, ATypeTag.SERIALIZED_DATE_TYPE_TAG);
}
// adjust the day before 1970-01-01
if (daysSinceAnchor < 0 && reminder != 0) {
daysSinceAnchor -= 1;
}
// compute the weekday (0-based, and 0 = Sunday). Adjustment is needed as
// the anchor day is Thursday.
int weekday = (daysSinceAnchor + ANCHOR_WEEKDAY) % 7;
// handle the negative weekday
if (weekday < 0) {
weekday += 7;
}
// convert from 0-based to 1-based (so 7 = Sunday)
if (weekday == 0) {
weekday = 7;
}
aInt64.setValue(weekday);
int64Serde.serialize(aInt64, out);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.runtime.exceptions.TypeMismatchException 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);
}
};
}
};
}
use of org.apache.asterix.runtime.exceptions.TypeMismatchException in project asterixdb by apache.
the class AIntervalStartFromTimeConstructorDescriptor 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_TIME_TYPE_TAG) {
intervalStart = ATimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
} else if (bytes0[offset0] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
utf8Ptr.set(bytes0, offset0 + 1, len0 - 1);
int stringLength = utf8Ptr.getUTF8Length();
intervalStart = ATimeParserFactory.parseTimePart(bytes0, utf8Ptr.getCharStartOffset(), stringLength);
} else {
throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
if (intervalStart < 0) {
intervalStart += GregorianCalendarSystem.CHRONON_OF_DAY;
}
if (bytes1[offset1] == ATypeTag.SERIALIZED_DURATION_TYPE_TAG) {
if (ADurationSerializerDeserializer.getYearMonth(bytes1, offset1 + 1) != 0) {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
intervalEnd = DurationArithmeticOperations.addDuration(intervalStart, 0, 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_STRING_TYPE_TAG) {
// duration
utf8Ptr.set(bytes1, offset1 + 1, len1 - 1);
int stringLength = utf8Ptr.getUTF8Length();
ADurationParserFactory.parseDuration(bytes1, utf8Ptr.getCharStartOffset(), stringLength, aDuration, ADurationParseOption.All);
if (aDuration.getMonths() != 0) {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
intervalEnd = DurationArithmeticOperations.addDuration(intervalStart, 0, aDuration.getMilliseconds(), false);
} else {
throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_DAY_TIME_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_STRING_TYPE_TAG);
}
if (intervalEnd > GregorianCalendarSystem.CHRONON_OF_DAY) {
intervalEnd = intervalEnd % (int) (GregorianCalendarSystem.CHRONON_OF_DAY);
}
if (intervalEnd < intervalStart) {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
aInterval.setValue(intervalStart, intervalEnd, ATypeTag.SERIALIZED_TIME_TYPE_TAG);
intervalSerde.serialize(aInterval, out);
} catch (IOException e) {
throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
}
result.set(resultStorage);
}
};
}
};
}
Aggregations