use of org.apache.asterix.om.base.AMutableDouble in project asterixdb by apache.
the class NumericATan2Descriptor 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() {
// For inputs.
private final IPointable leftPtr = new VoidPointable();
private final IPointable rightPtr = new VoidPointable();
private final IScalarEvaluator evalLeft = args[0].createScalarEvaluator(ctx);
private final IScalarEvaluator evalRight = args[1].createScalarEvaluator(ctx);
private final double[] operands = new double[args.length];
// For the output.
private final AMutableDouble aDouble = new AMutableDouble(0.0);
@SuppressWarnings("rawtypes")
private final ISerializerDeserializer outputSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
private final DataOutput out = resultStorage.getDataOutput();
@SuppressWarnings("unchecked")
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
evalLeft.evaluate(tuple, leftPtr);
evalRight.evaluate(tuple, rightPtr);
for (int i = 0; i < args.length; i++) {
IPointable argPtr = i == 0 ? leftPtr : rightPtr;
byte[] data = argPtr.getByteArray();
int offset = argPtr.getStartOffset();
operands[i] = ATypeHierarchy.getDoubleValue(getIdentifier().getName(), i, data, offset);
}
aDouble.setValue(Math.atan2(operands[0], operands[1]));
outputSerde.serialize(aDouble, out);
result.set(resultStorage);
}
};
}
};
}
use of org.apache.asterix.om.base.AMutableDouble in project asterixdb by apache.
the class ClassAdUnitTester method testClassad.
/*********************************************************************
* Function: test_classad
* Purpose: Test the ClassAd class.
* @param objectPool
*
* @throws IOException
*********************************************************************/
public static void testClassad(Parameters parameters, Results results, ClassAdObjectPool objectPool) throws IOException {
ClassAdParser parser = new ClassAdParser(objectPool);
boolean haveAttribute;
boolean success;
System.out.println("Testing the ClassAd class...");
String input_basic = "[ A = 3; B = 4.0; C = \"babyzilla\"; D = true; E = {1}; F = [ AA = 3; ]; G =\"deleteme\";]";
ClassAd basic = new ClassAd(objectPool);
AMutableInt64 i = new AMutableInt64(0);
MutableBoolean b = new MutableBoolean();
AMutableDouble r = new AMutableDouble(0);
AMutableCharArrayString s = new AMutableCharArrayString();
ClassAd c = new ClassAd(objectPool);
// ExprList *l;
basic = parser.parseClassAd(input_basic);
/* ----- Test EvaluateAttr* ----- */
haveAttribute = basic.evaluateAttrInt("A", i);
test("Have attribute A", (haveAttribute == true), "test_classad 1", results);
test("A is 3", (i.getLongValue() == 3), "test_classad 2", results);
haveAttribute = basic.evaluateAttrReal("B", r);
test("Have attribute B", (haveAttribute == true), "test_classad 3", results);
test("B is 4.0", (r.getDoubleValue() == 4.0), "test_classad 4", results);
haveAttribute = basic.evaluateAttrString("C", s);
test("Have attribute C", (haveAttribute == true), "test_classad 5", results);
test("C is 'babyzilla'", (s.compareTo("babyzilla") == 0), "test_classad 6", results);
haveAttribute = basic.evaluateAttrBool("D", b);
test("Have attribute D", (haveAttribute == true), "test_classad 7", results);
test("D is true", (b.booleanValue() == true), "test_classad 8", results);
/* ----- Test basic insert and delete ----- */
success = basic.insertAttr("new", 4);
test("InsertAttr claims to have worked", (success == true), "test_classad 9", results);
haveAttribute = basic.evaluateAttrInt("new", i);
test("Have new attribute", (haveAttribute == true), "test_classad 10", results);
test("new attribute is 4", i.getLongValue() == 4, "test_classad 11", results);
success = basic.delete("new");
test("Delete claims to have worked", (success == true), "test_classad 12", results);
haveAttribute = basic.evaluateAttrInt("new", i);
test("New attribute was deleted", (haveAttribute == false), "test_classad 13", results);
success = basic.delete("G");
test("DELETE claims to have worked", (success == true), "test_classad 14", results);
haveAttribute = basic.evaluateAttrString("G", s);
test("Attribute G was deleted", (haveAttribute == false), "test_classad 15", results);
basic = null;
/* ----- Test GetExternalReferences ----- */
String inputRef = "[ Rank=Member(\"LCG-2_1_0\",other.Environment) ? other.Time/seconds : other.Time/minutes; minutes=60; ]";
TreeSet<String> refs = new TreeSet<String>();
ExprTree rank;
c = parser.parseClassAd(inputRef);
test("Made classad_ref", (c != null), "Test GetExternalReferences 1", results);
if (c != null) {
rank = c.lookup("Rank");
test("Rank exists", (rank != null), "Test GetExternalReferences 2", results);
if (rank != null) {
boolean haveReferences;
if ((haveReferences = c.getExternalReferences(rank, refs, true))) {
test("have_references", (haveReferences == true), "Test GetExternalReferences 3", results);
if (haveReferences) {
boolean haveEnvironment;
boolean haveTime;
boolean haveSeconds;
boolean haveOther;
haveEnvironment = false;
haveTime = false;
haveSeconds = false;
haveOther = false;
for (String entry : refs) {
if (entry.compareTo("other.Environment") == 0) {
haveEnvironment = true;
} else if (entry.compareTo("other.Time") == 0) {
haveTime = true;
} else if (entry.compareTo("seconds") == 0) {
haveSeconds = true;
} else {
haveOther = true;
}
}
test("Have external reference to Environment", (haveEnvironment == true), "Test GetExternalReferences 4", results);
test("Have external reference to Time", (haveTime == true), "Test GetExternalReferences 5", results);
test("Have external reference to seconds", (haveSeconds == true), "Test GetExternalReferences 6", results);
test("Have no other external references", (haveOther != true), "Test GetExternalReferences 7", results);
}
}
}
c = null;
}
// This ClassAd may cause problems. Perhaps a memory leak.
// This test is only useful when run under valgrind.
String memoryProblemClassad = "[ Updates = [status = \"request_completed\"; timestamp = absTime(\"2004-12-16T18:10:59-0600]\")] ]";
c = parser.parseClassAd(memoryProblemClassad);
/* ----- Test Parsing multiple ClassAds ----- */
String twoClassads = "[ a = 3; ][ b = 4; ]";
ClassAd classad1 = new ClassAd(objectPool);
ClassAd classad2 = new ClassAd(objectPool);
AMutableInt32 offset = new AMutableInt32(0);
parser.parseClassAd(twoClassads, classad1, offset);
test("Have good offset #1", offset.getIntegerValue() == 10, "Test Parsing multiple ClassAds 1", results);
parser.parseClassAd(twoClassads, classad2, offset);
test("Have good offset #2", offset.getIntegerValue() == 20, "Test Parsing multiple ClassAds 2", results);
/* ----- Test chained ClassAds ----- */
// classad1 and classad2 from above test are used.
ClassAd classad3 = new ClassAd(objectPool);
classad1.chainToAd(classad2);
test("classad1's parent is classad2", classad1.getChainedParentAd().equals(classad2), "Test chained ClassAds 1", results);
haveAttribute = classad1.evaluateAttrInt("b", i);
test("chain has attribute b from parent", (haveAttribute == true), "Test chained ClassAds 2", results);
test("chain attribute b from parent is 4", (i.getLongValue() == 4), "Test chained ClassAds 3", results);
haveAttribute = classad1.evaluateAttrInt("a", i);
test("chain has attribute a from self", (haveAttribute == true), "Test chained ClassAds 4", results);
test("chain attribute a is 3", (i.getLongValue() == 3), "Test chained ClassAds 5", results);
// Now we modify classad2 (parent) to contain "a".
success = classad2.insertAttr("a", 7);
test("insert a into parent", (success == true), "Test chained ClassAds 6", results);
haveAttribute = classad1.evaluateAttrInt("a", i);
test("chain has attribute a from self (overriding parent)", (haveAttribute == true), "Test chained ClassAds 7", results);
test("chain attribute a is 3 (overriding parent)", (i.getLongValue() == 3), "Test chained ClassAds 8", results);
haveAttribute = classad2.evaluateAttrInt("a", i);
test("chain parent has attribute a", (haveAttribute == true), "Test chained ClassAds 9", results);
test("chain parent attribute a is 7", (i.getLongValue() == 7), "Test chained ClassAds 10", results);
success = classad3.copyFromChain(classad1);
test("copy from chain succeeded", (success == true), "Test chained ClassAds 11", results);
haveAttribute = classad3.evaluateAttrInt("b", i);
test("copy of chain has attribute b", (haveAttribute == true), "Test chained ClassAds 12", results);
test("copy of chain has attribute b==4", (i.getLongValue() == 4), "Test chained ClassAds 13", results);
success = classad3.insertAttr("c", 6);
test("insert into copy of chain succeeded", (success == true), "Test chained ClassAds 14", results);
classad3.copyFromChain(classad1);
haveAttribute = classad3.evaluateAttrInt("c", i);
test("copy of chain is clean", (haveAttribute == false), "Test chained ClassAds 15", results);
classad3.insertAttr("c", 6);
success = classad3.updateFromChain(classad1);
test("update from chain succeeded", (success == true), "Test chained ClassAds 16", results);
haveAttribute = classad3.evaluateAttrInt("c", i);
test("update from chain is merged", (haveAttribute == true), "Test chained ClassAds 17", results);
test("update from chain has attribute c==6", (i.getLongValue() == 6), "Test chained ClassAds 18", results);
}
use of org.apache.asterix.om.base.AMutableDouble in project asterixdb by apache.
the class ClassAdUnitTester method testValue.
/*********************************************************************
* Function: test_value
* Purpose: Test the Value class.
*
* @throws HyracksDataException
*********************************************************************/
public static void testValue(Parameters parameters, Results results, ClassAdObjectPool objectPool) throws HyracksDataException {
Value v = new Value(objectPool);
boolean isExpectedType;
System.out.println("Testing the Value class...");
test("New value is undefined", (v.isUndefinedValue()), "test_value 1", results);
test("New value isn't boolean", !(v.isBooleanValue()), "test_value 2", results);
test("GetType gives UNDEFINED_VALUE", (v.getType() == ValueType.UNDEFINED_VALUE), "test_value 3", results);
v.setErrorValue();
test("Is error value", (v.isErrorValue()), "test_value 4", results);
test("GetType gives ERROR_VALUE", (v.getType() == ValueType.ERROR_VALUE), "test_value 5", results);
MutableBoolean b = new MutableBoolean();
v.setBooleanValue(true);
isExpectedType = v.isBooleanValue(b);
test("Value is not undefined", !(v.isUndefinedValue()), "Value is not undefined", results);
test("Value is boolean", (v.isBooleanValue()), "Value is boolean", results);
test("Try 2: New value is boolean", (isExpectedType == true), "Try 2: New value is boolean", results);
test("Boolean is true", (b.booleanValue() == true), "Boolean is true", results);
test("GetType gives BOOLEAN_VALUE", (v.getType() == ValueType.BOOLEAN_VALUE), "GetType gives BOOLEAN_VALUE", results);
AMutableDouble r = new AMutableDouble(0.0);
v.setRealValue(1.0);
isExpectedType = v.isRealValue(r);
test("Value is real", isExpectedType, results);
test("Real is 1.0", (r.getDoubleValue() == 1.0), results);
test("GetType gives REAL_VALUE", (v.getType() == ValueType.REAL_VALUE), results);
test("Real is a number", v.isNumber(), results);
AMutableInt64 i = new AMutableInt64(0);
v.setIntegerValue(1);
isExpectedType = v.isIntegerValue(i);
test("Value is integer", isExpectedType, results);
test("Integer is 1", (i.getLongValue() == 1), results);
test("GetType gives INTEGER_VALUE", (v.getType() == ValueType.INTEGER_VALUE), results);
test("Integer is a number", v.isNumber(), results);
AMutableCharArrayString s = new AMutableCharArrayString();
v.setStringValue("Robert-Houdin");
isExpectedType = v.isStringValue(s);
test("Value is String", isExpectedType, results);
test("String is 'Robert-Houdin'", (0 == s.compareTo("Robert-Houdin")), results);
test("GetType gives STRING_VALUE", (v.getType() == ValueType.STRING_VALUE), results);
ClassAdTime at = new ClassAdTime(10, 36000000);
v.setAbsoluteTimeValue(at);
at.setValue(0);
at.setTimeZone(0);
isExpectedType = v.isAbsoluteTimeValue(at);
test("Value is absolute time", isExpectedType, results);
test("Absolute time is 10, 0", (10 == at.getTime() && 36000000 == at.getOffset()), results);
test("GetType gives ABSOLUTE_TIME_VALUE", (v.getType() == ValueType.ABSOLUTE_TIME_VALUE), results);
ClassAdTime rt = new ClassAdTime(10, false);
v.setRelativeTimeValue(10);
isExpectedType = v.isRelativeTimeValue(rt);
test("Value is relative time", isExpectedType, results);
test("Relative time is 10", (10 == rt.getRelativeTime()), results);
test("GetType gives RELATIVE_TIME_VALUE", (v.getType() == ValueType.RELATIVE_TIME_VALUE), results);
ExprList l = new ExprList(objectPool);
ExprList ll = new ExprList(objectPool);
v.setListValue(l);
isExpectedType = v.isListValue(ll);
test("Value is list value", isExpectedType, results);
test("List value is correct", l.equals(ll), results);
test("GetType gives LIST_VALUE", (v.getType() == ValueType.LIST_VALUE), results);
ExprList sl = new ExprList(true, objectPool);
ll = new ExprList(true, objectPool);
v.setListValue(sl);
isExpectedType = v.isListValue(ll);
test("Value is list value", isExpectedType, results);
test("List value is correct", sl.equals(ll), results);
test("GetType gives SLIST_VALUE", (v.getType() == ValueType.SLIST_VALUE), results);
ClassAd c = new ClassAd(objectPool);
c.insertAttr("test_int", 10);
ClassAd cc = new ClassAd(objectPool);
v.setClassAdValue(c);
isExpectedType = v.isClassAdValue(cc);
test("Value is ClassAd value", isExpectedType, results);
test("ClassAd value is correct", c.equals(cc), results);
test("GetType gives CLASSAD_VALUE", (v.getType() == ValueType.CLASSAD_VALUE), results);
return;
}
use of org.apache.asterix.om.base.AMutableDouble in project asterixdb by apache.
the class NumericRoundDescriptor 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);
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;
@SuppressWarnings("unchecked")
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval.evaluate(tuple, argPtr);
byte[] data = argPtr.getByteArray();
int offset = argPtr.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);
val = Math.round(val);
aFloat.setValue(val);
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);
val = Math.round(val);
aDouble.setValue(val);
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.asterix.om.base.AMutableDouble in project asterixdb by apache.
the class NumericRoundHalfToEvenDescriptor 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);
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;
@SuppressWarnings("unchecked")
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval.evaluate(tuple, argPtr);
byte[] data = argPtr.getByteArray();
int offset = argPtr.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);
aFloat.setValue((float) Math.rint(val));
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);
aDouble.setValue(Math.rint(val));
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);
}
};
}
};
}
Aggregations