use of org.apache.hyracks.api.dataflow.value.ISerializerDeserializer in project asterixdb by apache.
the class NumericRoundHalfToEven2Descriptor 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 argValue = new VoidPointable();
private IPointable argPrecision = new VoidPointable();
private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
private IScalarEvaluator precision = args[1].createScalarEvaluator(ctx);
private AMutableDouble aDouble = new AMutableDouble(0);
private AMutableFloat aFloat = new AMutableFloat(0);
private AMutableInt64 aInt64 = new AMutableInt64(0);
private AMutableInt32 aInt32 = new AMutableInt32(0);
private AMutableInt16 aInt16 = new AMutableInt16((short) 0);
private AMutableInt8 aInt8 = new AMutableInt8((byte) 0);
@SuppressWarnings("rawtypes")
private ISerializerDeserializer serde;
private int getPrecision() throws HyracksDataException {
byte[] bytes = argPrecision.getByteArray();
int offset = argPrecision.getStartOffset();
if (bytes[offset] == ATypeTag.SERIALIZED_INT8_TYPE_TAG) {
return AInt8SerializerDeserializer.getByte(bytes, offset + 1);
} else if (bytes[offset] == ATypeTag.SERIALIZED_INT16_TYPE_TAG) {
return AInt16SerializerDeserializer.getShort(bytes, offset + 1);
} else if (bytes[offset] == ATypeTag.SERIALIZED_INT32_TYPE_TAG) {
return AInt32SerializerDeserializer.getInt(bytes, offset + 1);
} else if (bytes[offset] == ATypeTag.SERIALIZED_INT64_TYPE_TAG) {
return (int) AInt64SerializerDeserializer.getLong(bytes, offset + 1);
} else {
throw new TypeMismatchException(getIdentifier(), 1, bytes[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG);
}
}
@SuppressWarnings("unchecked")
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval.evaluate(tuple, argValue);
precision.evaluate(tuple, argPrecision);
byte[] data = argValue.getByteArray();
int offset = argValue.getStartOffset();
if (data[offset] == ATypeTag.SERIALIZED_INT8_TYPE_TAG) {
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT8);
byte val = AInt8SerializerDeserializer.getByte(data, offset + 1);
aInt8.setValue(val);
serde.serialize(aInt8, out);
} else if (data[offset] == ATypeTag.SERIALIZED_INT16_TYPE_TAG) {
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT16);
short val = AInt16SerializerDeserializer.getShort(data, offset + 1);
aInt16.setValue(val);
serde.serialize(aInt16, out);
} else if (data[offset] == ATypeTag.SERIALIZED_INT32_TYPE_TAG) {
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
int val = AInt32SerializerDeserializer.getInt(data, offset + 1);
aInt32.setValue(val);
serde.serialize(aInt32, out);
} else if (data[offset] == ATypeTag.SERIALIZED_INT64_TYPE_TAG) {
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
long val = AInt64SerializerDeserializer.getLong(data, offset + 1);
aInt64.setValue(val);
serde.serialize(aInt64, out);
} else if (data[offset] == ATypeTag.SERIALIZED_FLOAT_TYPE_TAG) {
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);
float val = AFloatSerializerDeserializer.getFloat(data, offset + 1);
if (Float.isNaN(val) || Float.isInfinite(val) || Float.compare(val, -0.0F) == 0 || Float.compare(val, 0.0F) == 0) {
aFloat.setValue(val);
serde.serialize(aFloat, out);
} else {
BigDecimal r = new BigDecimal(Float.toString(val));
aFloat.setValue(r.setScale(getPrecision(), BigDecimal.ROUND_HALF_EVEN).floatValue());
serde.serialize(aFloat, out);
}
} else if (data[offset] == ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG) {
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
double val = ADoubleSerializerDeserializer.getDouble(data, offset + 1);
if (Double.isNaN(val) || Double.isInfinite(val) || Double.compare(val, -0.0D) == 0 || Double.compare(val, 0.0D) == 0) {
aDouble.setValue(val);
serde.serialize(aDouble, out);
} else {
BigDecimal r = new BigDecimal(Double.toString(val));
aDouble.setValue(r.setScale(getPrecision(), BigDecimal.ROUND_HALF_EVEN).doubleValue());
serde.serialize(aDouble, out);
}
} else {
throw new TypeMismatchException(getIdentifier(), 0, data[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG, ATypeTag.SERIALIZED_FLOAT_TYPE_TAG, ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.hyracks.api.dataflow.value.ISerializerDeserializer in project asterixdb by apache.
the class OrDescriptor 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 {
final IPointable argPtr = new VoidPointable();
final IScalarEvaluator[] evals = new IScalarEvaluator[args.length];
for (int i = 0; i < evals.length; i++) {
evals[i] = args[i].createScalarEvaluator(ctx);
}
return new IScalarEvaluator() {
private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private DataOutput output = resultStorage.getDataOutput();
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ABoolean> booleanSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABOOLEAN);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ANull> nullSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<AMissing> missingSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AMISSING);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
int n = args.length;
boolean res = false;
boolean metNull = false;
boolean metMissing = false;
for (int i = 0; i < n; i++) {
evals[i].evaluate(tuple, argPtr);
byte[] data = argPtr.getByteArray();
int offset = argPtr.getStartOffset();
if (data[offset] == ATypeTag.SERIALIZED_MISSING_TYPE_TAG) {
metMissing = true;
continue;
}
if (data[offset] == ATypeTag.SERIALIZED_NULL_TYPE_TAG) {
metNull = true;
continue;
}
if (data[offset] != ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), i, data[offset], ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG);
}
boolean argResult = ABooleanSerializerDeserializer.getBoolean(data, offset + 1);
if (argResult) {
// anything OR TRUE = TRUE
booleanSerde.serialize(ABoolean.TRUE, output);
result.set(resultStorage);
return;
}
res |= argResult;
}
if (metNull) {
// NULL OR FALSE = NULL
// NULL OR MISSING = NULL
nullSerde.serialize(ANull.NULL, output);
} else if (metMissing) {
// MISSING OR FALSE = MISSING
missingSerde.serialize(AMissing.MISSING, output);
} else {
ABoolean aResult = res ? ABoolean.TRUE : ABoolean.FALSE;
booleanSerde.serialize(aResult, output);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.hyracks.api.dataflow.value.ISerializerDeserializer in project asterixdb by apache.
the class StringConcatDescriptor 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 final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private final ListAccessor listAccessor = new ListAccessor();
private final DataOutput out = resultStorage.getDataOutput();
private final IScalarEvaluatorFactory listEvalFactory = args[0];
private final IPointable inputArgList = new VoidPointable();
private final IScalarEvaluator evalList = listEvalFactory.createScalarEvaluator(ctx);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ANull> nullSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL);
private ISerializerDeserializer<AMissing> missingSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AMISSING);
private final byte[] tempLengthArray = new byte[5];
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
try {
evalList.evaluate(tuple, inputArgList);
byte[] listBytes = inputArgList.getByteArray();
int listOffset = inputArgList.getStartOffset();
if (listBytes[listOffset] != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG && listBytes[listOffset] != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), 0, listBytes[listOffset], ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG, ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG);
}
listAccessor.reset(listBytes, listOffset);
try {
// calculate length first
int utf8Len = 0;
for (int i = 0; i < listAccessor.size(); i++) {
int itemOffset = listAccessor.getItemOffset(i);
ATypeTag itemType = listAccessor.getItemType(itemOffset);
// since the item itself has a typetag.
if (listAccessor.itemsAreSelfDescribing()) {
itemOffset += 1;
}
if (itemType != ATypeTag.STRING) {
if (itemType == ATypeTag.NULL) {
nullSerde.serialize(ANull.NULL, out);
result.set(resultStorage);
return;
}
if (itemType == ATypeTag.MISSING) {
missingSerde.serialize(AMissing.MISSING, out);
result.set(resultStorage);
return;
}
throw new UnsupportedItemTypeException(getIdentifier(), itemType.serialize());
}
utf8Len += UTF8StringUtil.getUTFLength(listBytes, itemOffset);
}
out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
int cbytes = UTF8StringUtil.encodeUTF8Length(utf8Len, tempLengthArray, 0);
out.write(tempLengthArray, 0, cbytes);
for (int i = 0; i < listAccessor.size(); i++) {
int itemOffset = listAccessor.getItemOffset(i);
if (listAccessor.itemsAreSelfDescribing()) {
itemOffset += 1;
}
utf8Len = UTF8StringUtil.getUTFLength(listBytes, itemOffset);
out.write(listBytes, UTF8StringUtil.getNumBytesToStoreLength(utf8Len) + itemOffset, utf8Len);
}
} catch (AsterixException ex) {
throw new HyracksDataException(ex);
}
} catch (IOException e1) {
throw new HyracksDataException(e1);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.hyracks.api.dataflow.value.ISerializerDeserializer in project asterixdb by apache.
the class AndDescriptor 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 {
final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
final DataOutput out = resultStorage.getDataOutput();
final IPointable argPtr = new VoidPointable();
final IScalarEvaluator[] evals = new IScalarEvaluator[args.length];
for (int i = 0; i < evals.length; i++) {
evals[i] = args[i].createScalarEvaluator(ctx);
}
return new IScalarEvaluator() {
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ABoolean> booleanSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABOOLEAN);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ANull> nullSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<AMissing> missingSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AMISSING);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
int n = args.length;
boolean metNull = false;
boolean metMissing = false;
for (int i = 0; i < n; i++) {
evals[i].evaluate(tuple, argPtr);
byte[] bytes = argPtr.getByteArray();
int offset = argPtr.getStartOffset();
boolean isNull = false;
boolean isMissing = false;
if (bytes[offset] == ATypeTag.SERIALIZED_MISSING_TYPE_TAG) {
isMissing = true;
metMissing = true;
}
if (bytes[offset] == ATypeTag.SERIALIZED_NULL_TYPE_TAG) {
isNull = true;
metNull = true;
}
if (isMissing || isNull) {
continue;
}
if (bytes[offset] != ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG) {
throw new TypeMismatchException(getIdentifier(), i, bytes[offset], ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG);
}
boolean argResult = ABooleanSerializerDeserializer.getBoolean(bytes, offset + 1);
if (!argResult) {
// anything AND FALSE = FALSE
booleanSerde.serialize(ABoolean.FALSE, out);
result.set(resultStorage);
return;
}
}
if (metMissing) {
// MISSING AND NULL = MISSING
// MISSING AND TRUE = MISSING
missingSerde.serialize(AMissing.MISSING, out);
} else if (metNull) {
// NULL AND TRUE = NULL
nullSerde.serialize(ANull.NULL, out);
} else {
booleanSerde.serialize(ABoolean.TRUE, out);
}
result.set(resultStorage);
}
};
}
};
}
use of org.apache.hyracks.api.dataflow.value.ISerializerDeserializer in project asterixdb by apache.
the class NotDescriptor 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 argPtr = new VoidPointable();
private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
@SuppressWarnings("unchecked")
private ISerializerDeserializer<ABoolean> booleanSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABOOLEAN);
@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_BOOLEAN_TYPE_TAG) {
boolean argRes = ABooleanSerializerDeserializer.getBoolean(bytes, offset + 1);
ABoolean aResult = argRes ? ABoolean.FALSE : ABoolean.TRUE;
booleanSerde.serialize(aResult, out);
} else {
throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG);
}
result.set(resultStorage);
}
};
}
};
}
Aggregations