use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator in project asterixdb by apache.
the class IntegerAddEvalFactory method createScalarEvaluator.
@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
return new IScalarEvaluator() {
private IPointable p = VoidPointable.FACTORY.createPointable();
private ArrayBackedValueStorage argOut = new ArrayBackedValueStorage();
private IScalarEvaluator evalLeft = evalLeftFactory.createScalarEvaluator(ctx);
private IScalarEvaluator evalRight = evalRightFactory.createScalarEvaluator(ctx);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
evalLeft.evaluate(tuple, p);
int v1 = IntegerPointable.getInteger(p.getByteArray(), p.getStartOffset());
evalRight.evaluate(tuple, p);
int v2 = IntegerPointable.getInteger(p.getByteArray(), p.getStartOffset());
try {
argOut.reset();
argOut.getDataOutput().writeInt(v1 + v2);
result.set(argOut);
} catch (IOException e) {
throw new HyracksDataException(e);
}
}
};
}
use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator in project asterixdb by apache.
the class StreamLimitRuntimeFactory method createOneOutputPushRuntime.
@Override
public AbstractOneInputOneOutputOneFramePushRuntime createOneOutputPushRuntime(final IHyracksTaskContext ctx) {
final IBinaryIntegerInspector bii = binaryIntegerInspectorFactory.createBinaryIntegerInspector(ctx);
return new AbstractOneInputOneOutputOneFramePushRuntime() {
private final IPointable p = VoidPointable.FACTORY.createPointable();
private IScalarEvaluator evalMaxObjects;
private IScalarEvaluator evalOffset = null;
// how many tuples still to write
private int toWrite = 0;
// how many tuples still to skip
private int toSkip = 0;
private boolean firstTuple = true;
private boolean afterLastTuple = false;
@Override
public void open() throws HyracksDataException {
writer.open();
if (evalMaxObjects == null) {
initAccessAppendRef(ctx);
evalMaxObjects = maxObjectsEvalFactory.createScalarEvaluator(ctx);
if (offsetEvalFactory != null) {
evalOffset = offsetEvalFactory.createScalarEvaluator(ctx);
}
}
afterLastTuple = false;
}
@Override
public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
if (afterLastTuple) {
return;
}
tAccess.reset(buffer);
int nTuple = tAccess.getTupleCount();
int start = 0;
if (nTuple <= toSkip) {
toSkip -= nTuple;
return;
} else if (toSkip > 0) {
start = toSkip;
toSkip = 0;
}
for (int t = start; t < nTuple; t++) {
if (firstTuple) {
firstTuple = false;
toWrite = evaluateInteger(evalMaxObjects, t);
if (evalOffset != null) {
toSkip = evaluateInteger(evalOffset, t);
}
}
if (toSkip > 0) {
toSkip--;
} else if (toWrite > 0) {
toWrite--;
if (projectionList != null) {
appendProjectionToFrame(t, projectionList);
} else {
appendTupleToFrame(t);
}
} else {
afterLastTuple = true;
break;
}
}
}
@Override
public void close() throws HyracksDataException {
// how many tuples still to write
toWrite = 0;
// how many tuples still to skip
toSkip = 0;
firstTuple = true;
afterLastTuple = false;
super.close();
}
private int evaluateInteger(IScalarEvaluator eval, int tIdx) throws HyracksDataException {
tRef.reset(tAccess, tIdx);
eval.evaluate(tRef, p);
int lim = bii.getIntegerValue(p.getByteArray(), p.getStartOffset(), p.getLength());
return lim;
}
@Override
public void flush() throws HyracksDataException {
appender.flush(writer);
}
};
}
use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator in project asterixdb by apache.
the class ExceptionIT method testFunction.
private void testFunction(IFunctionDescriptorFactory funcFactory) throws Exception {
AbstractScalarFunctionDynamicDescriptor funcDesc = (AbstractScalarFunctionDynamicDescriptor) funcFactory.createFunctionDescriptor();
int inputArity = funcDesc.getIdentifier().getArity();
Iterator<IScalarEvaluatorFactory[]> argEvalFactoryIterator = getArgCombinations(inputArity);
while (argEvalFactoryIterator.hasNext()) {
IScalarEvaluatorFactory evalFactory = funcDesc.createEvaluatorFactory(argEvalFactoryIterator.next());
IHyracksTaskContext ctx = mock(IHyracksTaskContext.class);
IScalarEvaluator evaluator = evalFactory.createScalarEvaluator(ctx);
IPointable resultPointable = new VoidPointable();
try {
evaluator.evaluate(null, resultPointable);
} catch (Throwable e) {
String msg = e.getMessage();
if (msg == null) {
continue;
}
if (msg.startsWith("ASX")) {
// Verifies the error code.
int errorCode = Integer.parseInt(msg.substring(3, 7));
Assert.assertTrue(errorCode >= 0 && errorCode < 1000);
continue;
} else {
// Any root-level data exceptions thrown from runtime functions should have an error code.
Assert.assertTrue(!(e instanceof HyracksDataException) || (e.getCause() != null));
}
}
}
}
use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator in project asterixdb by apache.
the class NullMissingTest method testFunction.
private void testFunction(IFunctionDescriptorFactory funcFactory) throws Exception {
IFunctionDescriptor functionDescriptor = funcFactory.createFunctionDescriptor();
if (!(functionDescriptor instanceof AbstractScalarFunctionDynamicDescriptor)) {
return;
}
AbstractScalarFunctionDynamicDescriptor funcDesc = (AbstractScalarFunctionDynamicDescriptor) functionDescriptor;
int inputArity = funcDesc.getIdentifier().getArity();
Iterator<IScalarEvaluatorFactory[]> argEvalFactoryIterator = getArgCombinations(inputArity);
int index = 0;
while (argEvalFactoryIterator.hasNext()) {
IScalarEvaluatorFactory evalFactory = funcDesc.createEvaluatorFactory(argEvalFactoryIterator.next());
IHyracksTaskContext ctx = mock(IHyracksTaskContext.class);
IScalarEvaluator evaluator = evalFactory.createScalarEvaluator(ctx);
IPointable resultPointable = new VoidPointable();
evaluator.evaluate(null, resultPointable);
if (index != 0) {
Assert.assertTrue(resultPointable.getByteArray()[resultPointable.getStartOffset()] == ATypeTag.SERIALIZED_MISSING_TYPE_TAG);
} else {
Assert.assertTrue(resultPointable.getByteArray()[resultPointable.getStartOffset()] == ATypeTag.SERIALIZED_NULL_TYPE_TAG);
}
++index;
}
}
use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator in project asterixdb by apache.
the class SpatialAreaDescriptor 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 DataOutput out = resultStorage.getDataOutput();
private final IPointable argPtr = new VoidPointable();
private final IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
@Override
public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
resultStorage.reset();
eval.evaluate(tuple, argPtr);
try {
byte[] bytes = argPtr.getByteArray();
int offset = argPtr.getStartOffset();
ATypeTag tag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes[offset]);
double area = 0.0;
switch(tag) {
case POLYGON:
int numOfPoints = AInt16SerializerDeserializer.getShort(bytes, offset + 1);
if (numOfPoints < 3) {
throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_POLYGON_TYPE_TAG);
}
area = Math.abs(SpatialUtils.polygonArea(bytes, offset, numOfPoints));
out.writeByte(ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
out.writeDouble(area);
break;
case CIRCLE:
double radius = ADoubleSerializerDeserializer.getDouble(bytes, offset + ACircleSerializerDeserializer.getRadiusOffset());
area = SpatialUtils.pi() * radius * radius;
out.writeByte(ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
out.writeDouble(area);
break;
case RECTANGLE:
double x1 = ADoubleSerializerDeserializer.getDouble(bytes, offset + ARectangleSerializerDeserializer.getBottomLeftCoordinateOffset(Coordinate.X));
double y1 = ADoubleSerializerDeserializer.getDouble(bytes, offset + ARectangleSerializerDeserializer.getBottomLeftCoordinateOffset(Coordinate.Y));
double x2 = ADoubleSerializerDeserializer.getDouble(bytes, offset + ARectangleSerializerDeserializer.getUpperRightCoordinateOffset(Coordinate.X));
double y2 = ADoubleSerializerDeserializer.getDouble(bytes, offset + ARectangleSerializerDeserializer.getUpperRightCoordinateOffset(Coordinate.Y));
area = (x2 - x1) * (y2 - y1);
out.writeByte(ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG);
out.writeDouble(area);
break;
default:
throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_POLYGON_TYPE_TAG, ATypeTag.SERIALIZED_CIRCLE_TYPE_TAG, ATypeTag.SERIALIZED_RECTANGLE_TYPE_TAG);
}
} catch (IOException e) {
throw new HyracksDataException(e);
}
result.set(resultStorage);
}
};
}
};
}
Aggregations