Search in sources :

Example 86 with IScalarEvaluatorFactory

use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory in project asterixdb by apache.

the class ExceptionIT method getArgCombinations.

private Iterator<IScalarEvaluatorFactory[]> getArgCombinations(final int inputArity) {
    final int argSize = inputArity >= 0 ? inputArity : 3;
    final int numCombinations = (int) Math.pow(ATypeTag.values().length, argSize);
    return new Iterator<IScalarEvaluatorFactory[]>() {

        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < numCombinations;
        }

        @Override
        public IScalarEvaluatorFactory[] next() {
            IScalarEvaluatorFactory[] scalarEvaluatorFactories = new IScalarEvaluatorFactory[argSize];
            for (int j = 0; j < argSize; ++j) {
                int base = (int) Math.pow(ATypeTag.values().length, j);
                // Enumerates through all possible type tags.
                byte serializedTypeTag = (byte) ((index / base) % ATypeTag.values().length);
                scalarEvaluatorFactories[j] = new ConstantEvalFactory(new byte[] { serializedTypeTag });
            }
            ++index;
            return scalarEvaluatorFactories;
        }
    };
}
Also used : ConstantEvalFactory(org.apache.hyracks.algebricks.runtime.evaluators.ConstantEvalFactory) Iterator(java.util.Iterator) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 87 with IScalarEvaluatorFactory

use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory 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));
            }
        }
    }
}
Also used : IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) AbstractScalarFunctionDynamicDescriptor(org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 88 with IScalarEvaluatorFactory

use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory 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;
    }
}
Also used : IFunctionDescriptor(org.apache.asterix.om.functions.IFunctionDescriptor) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) AbstractScalarFunctionDynamicDescriptor(org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 89 with IScalarEvaluatorFactory

use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory in project asterixdb by apache.

the class NullMissingTest method getArgCombinations.

private Iterator<IScalarEvaluatorFactory[]> getArgCombinations(int inputArity) {
    int argSize = inputArity >= 0 ? inputArity : 3;
    final int numCombinations = 1 << argSize;
    return new Iterator<IScalarEvaluatorFactory[]>() {

        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < numCombinations;
        }

        @Override
        public IScalarEvaluatorFactory[] next() {
            IScalarEvaluatorFactory[] scalarEvaluatorFactories = new IScalarEvaluatorFactory[argSize];
            for (int j = 0; j < argSize; ++j) {
                byte serializedTypeTag = (index & (1 << j)) != 0 ? ATypeTag.SERIALIZED_MISSING_TYPE_TAG : ATypeTag.SERIALIZED_NULL_TYPE_TAG;
                scalarEvaluatorFactories[j] = new ConstantEvalFactory(new byte[] { serializedTypeTag });
            }
            ++index;
            return scalarEvaluatorFactories;
        }
    };
}
Also used : ConstantEvalFactory(org.apache.hyracks.algebricks.runtime.evaluators.ConstantEvalFactory) Iterator(java.util.Iterator) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 90 with IScalarEvaluatorFactory

use of org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory 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);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) ATypeTag(org.apache.asterix.om.types.ATypeTag) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)

Aggregations

IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)167 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)146 IPointable (org.apache.hyracks.data.std.api.IPointable)136 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)134 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)132 DataOutput (java.io.DataOutput)129 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)129 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)124 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)110 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)107 IOException (java.io.IOException)77 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)61 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)48 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)40 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)24 ATypeTag (org.apache.asterix.om.types.ATypeTag)19 GregorianCalendarSystem (org.apache.asterix.om.base.temporal.GregorianCalendarSystem)13 AMutableDateTime (org.apache.asterix.om.base.AMutableDateTime)11 AMutableDouble (org.apache.asterix.om.base.AMutableDouble)11 AMutablePoint (org.apache.asterix.om.base.AMutablePoint)11