Search in sources :

Example 1 with FunctionTemplate

use of org.apache.drill.exec.expr.annotations.FunctionTemplate in project drill by apache.

the class TestClassPathScanner method test.

@Test
public void test() throws Exception {
    ScanResult result = ClassPathScanner.fromPrescan(DrillConfig.create());
    // if the build has run properly. BuildTimeScan.REGISTRY_FILE was created with a prescan
    //    assertListEqualsUnordered(result.getPrescannedPackages(),
    //      "org.apache.drill.common.logical",
    //      "org.apache.drill.exec.expr",
    //      "org.apache.drill.exec.physical.base",
    //      "org.apache.drill.exec.expr.fn.impl",
    //      "org.apache.drill.exec.physical.impl",
    //      "org.apache.drill.exec.rpc.user.security",
    //      "org.apache.drill.exec.store",
    //      "org.apache.drill.exec.store.mock",
    //      "org.apache.drill.exec.physical.config",
    //      "org.apache.drill.storage"
    //    );
    //    // this is added in the unit test folder that was not scanned so far
    //    assertListEqualsUnordered(result.getScannedPackages(),
    //      "org.apache.drill.exec.testing",
    //      "org.apache.drill.exec.fn.impl.testing",
    //      "org.apache.drill.exec.rpc.user.security.testing"
    //    );
    List<AnnotatedClassDescriptor> functions = result.getAnnotatedClasses();
    Set<String> scanned = new HashSet<>();
    AnnotatedClassDescriptor functionRandomBigIntGauss = null;
    for (AnnotatedClassDescriptor function : functions) {
        assertTrue(function.getClassName() + " scanned twice", scanned.add(function.getClassName()));
        if (function.getClassName().equals(RandomBigIntGauss.class.getName())) {
            functionRandomBigIntGauss = function;
        }
    }
    if (functionRandomBigIntGauss == null) {
        Assert.fail("functionRandomBigIntGauss not found");
    }
    // TODO: use Andrew's randomized test framework to verify a subset of the functions
    for (AnnotatedClassDescriptor function : functions) {
        Class<?> c = Class.forName(function.getClassName(), false, this.getClass().getClassLoader());
        Field[] fields = c.getDeclaredFields();
        assertEquals("fields count for " + function, fields.length, function.getFields().size());
        for (int i = 0; i < fields.length; i++) {
            FieldDescriptor fieldDescriptor = function.getFields().get(i);
            Field field = fields[i];
            assertEquals("Class fields:\n" + Arrays.toString(fields) + "\n != \nDescriptor fields:\n" + function.getFields(), field.getName(), fieldDescriptor.getName());
            verifyAnnotations(field.getDeclaredAnnotations(), fieldDescriptor.getAnnotations());
            assertEquals(field.getType(), fieldDescriptor.getFieldClass());
        }
        Annotation[] annotations = c.getDeclaredAnnotations();
        List<AnnotationDescriptor> scannedAnnotations = function.getAnnotations();
        verifyAnnotations(annotations, scannedAnnotations);
        FunctionTemplate bytecodeAnnotation = function.getAnnotationProxy(FunctionTemplate.class);
        FunctionTemplate reflectionAnnotation = c.getAnnotation(FunctionTemplate.class);
        assertEquals(reflectionAnnotation.name(), bytecodeAnnotation.name());
        assertArrayEquals(reflectionAnnotation.names(), bytecodeAnnotation.names());
        assertEquals(reflectionAnnotation.scope(), bytecodeAnnotation.scope());
        assertEquals(reflectionAnnotation.nulls(), bytecodeAnnotation.nulls());
        assertEquals(reflectionAnnotation.isBinaryCommutative(), bytecodeAnnotation.isBinaryCommutative());
        assertEquals(reflectionAnnotation.desc(), bytecodeAnnotation.desc());
        assertEquals(reflectionAnnotation.costCategory(), bytecodeAnnotation.costCategory());
    }
    for (String baseType : result.getScannedClasses()) {
        validateType(result, baseType);
    }
    assertTrue(result.getImplementations(PhysicalOperator.class).size() > 0);
    assertTrue(result.getImplementations(DrillFunc.class).size() > 0);
}
Also used : AnnotationDescriptor(org.apache.drill.common.scanner.persistence.AnnotationDescriptor) ScanResult(org.apache.drill.common.scanner.persistence.ScanResult) Annotation(java.lang.annotation.Annotation) FieldDescriptor(org.apache.drill.common.scanner.persistence.FieldDescriptor) AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) Field(java.lang.reflect.Field) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) RandomBigIntGauss(org.apache.drill.exec.fn.impl.testing.GeneratorFunctions.RandomBigIntGauss) HashSet(java.util.HashSet) DrillFunc(org.apache.drill.exec.expr.DrillFunc) Test(org.junit.Test)

Example 2 with FunctionTemplate

use of org.apache.drill.exec.expr.annotations.FunctionTemplate in project drill by axbaretto.

the class TestClassPathScanner method verifyAnnotations.

private void verifyAnnotations(Annotation[] annotations, List<AnnotationDescriptor> scannedAnnotations) throws Exception {
    assertEquals(Arrays.toString(annotations) + " expected but got " + scannedAnnotations, annotations.length, scannedAnnotations.size());
    for (int i = 0; i < annotations.length; i++) {
        Annotation annotation = annotations[i];
        AnnotationDescriptor scannedAnnotation = scannedAnnotations.get(i);
        Class<? extends Annotation> annotationType = annotation.annotationType();
        assertEquals(annotationType.getName(), scannedAnnotation.getAnnotationType());
        if (annotation instanceof FunctionTemplate) {
            FunctionTemplate ft = (FunctionTemplate) annotation;
            if (ft.name() != null && !ft.name().equals("")) {
                assertEquals(ft.name(), scannedAnnotation.getSingleValue("name"));
            }
        }
        // generally verify all properties
        Annotation proxy = scannedAnnotation.getProxy(annotationType);
        Method[] declaredMethods = annotationType.getDeclaredMethods();
        for (Method method : declaredMethods) {
            if (method.getParameterTypes().length == 0) {
                Object reflectValue = method.invoke(annotation);
                Object byteCodeValue = method.invoke(proxy);
                String message = annotationType.getSimpleName() + "." + method.getName();
                if (method.getReturnType().isArray()) {
                    assertArrayEquals(message, (Object[]) reflectValue, (Object[]) byteCodeValue);
                } else {
                    assertEquals(message, reflectValue, byteCodeValue);
                }
            }
        }
    }
}
Also used : AnnotationDescriptor(org.apache.drill.common.scanner.persistence.AnnotationDescriptor) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 3 with FunctionTemplate

use of org.apache.drill.exec.expr.annotations.FunctionTemplate in project drill by axbaretto.

the class TestClassPathScanner method test.

@Test
public void test() throws Exception {
    ScanResult result = ClassPathScanner.fromPrescan(DrillConfig.create());
    List<AnnotatedClassDescriptor> functions = result.getAnnotatedClasses();
    Set<String> scanned = new HashSet<>();
    AnnotatedClassDescriptor functionRandomBigIntGauss = null;
    for (AnnotatedClassDescriptor function : functions) {
        assertTrue(function.getClassName() + " scanned twice", scanned.add(function.getClassName()));
        if (function.getClassName().equals(RandomBigIntGauss.class.getName())) {
            functionRandomBigIntGauss = function;
        }
    }
    if (functionRandomBigIntGauss == null) {
        Assert.fail("functionRandomBigIntGauss not found");
    }
    // TODO: use Andrew's randomized test framework to verify a subset of the functions
    for (AnnotatedClassDescriptor function : functions) {
        Class<?> c = Class.forName(function.getClassName(), false, this.getClass().getClassLoader());
        Field[] fields = c.getDeclaredFields();
        assertEquals("fields count for " + function, fields.length, function.getFields().size());
        for (int i = 0; i < fields.length; i++) {
            FieldDescriptor fieldDescriptor = function.getFields().get(i);
            Field field = fields[i];
            assertEquals("Class fields:\n" + Arrays.toString(fields) + "\n != \nDescriptor fields:\n" + function.getFields(), field.getName(), fieldDescriptor.getName());
            verifyAnnotations(field.getDeclaredAnnotations(), fieldDescriptor.getAnnotations());
            assertEquals(field.getType(), fieldDescriptor.getFieldClass());
        }
        Annotation[] annotations = c.getDeclaredAnnotations();
        List<AnnotationDescriptor> scannedAnnotations = function.getAnnotations();
        verifyAnnotations(annotations, scannedAnnotations);
        FunctionTemplate bytecodeAnnotation = function.getAnnotationProxy(FunctionTemplate.class);
        FunctionTemplate reflectionAnnotation = c.getAnnotation(FunctionTemplate.class);
        assertEquals(reflectionAnnotation.name(), bytecodeAnnotation.name());
        assertArrayEquals(reflectionAnnotation.names(), bytecodeAnnotation.names());
        assertEquals(reflectionAnnotation.scope(), bytecodeAnnotation.scope());
        assertEquals(reflectionAnnotation.nulls(), bytecodeAnnotation.nulls());
        assertEquals(reflectionAnnotation.isBinaryCommutative(), bytecodeAnnotation.isBinaryCommutative());
        assertEquals(reflectionAnnotation.desc(), bytecodeAnnotation.desc());
        assertEquals(reflectionAnnotation.costCategory(), bytecodeAnnotation.costCategory());
    }
    for (String baseType : result.getScannedClasses()) {
        validateType(result, baseType);
    }
    assertTrue(result.getImplementations(PhysicalOperator.class).size() > 0);
    assertTrue(result.getImplementations(DrillFunc.class).size() > 0);
}
Also used : AnnotationDescriptor(org.apache.drill.common.scanner.persistence.AnnotationDescriptor) ScanResult(org.apache.drill.common.scanner.persistence.ScanResult) Annotation(java.lang.annotation.Annotation) FieldDescriptor(org.apache.drill.common.scanner.persistence.FieldDescriptor) AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) Field(java.lang.reflect.Field) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) RandomBigIntGauss(org.apache.drill.exec.fn.impl.testing.GeneratorFunctions.RandomBigIntGauss) HashSet(java.util.HashSet) DrillFunc(org.apache.drill.exec.expr.DrillFunc) SlowTest(org.apache.drill.categories.SlowTest) Test(org.junit.Test)

Example 4 with FunctionTemplate

use of org.apache.drill.exec.expr.annotations.FunctionTemplate in project drill by axbaretto.

the class FunctionConverter method getHolder.

public <T extends DrillFunc> DrillFuncHolder getHolder(AnnotatedClassDescriptor func, ClassLoader classLoader) {
    FunctionTemplate template = func.getAnnotationProxy(FunctionTemplate.class);
    if (template == null) {
        return failure("Class does not declare FunctionTemplate annotation.", func);
    }
    String name = template.name();
    List<String> names = Arrays.asList(template.names());
    if (name.isEmpty() && names.isEmpty()) {
        // none set
        return failure("Must define 'name' or 'names'", func);
    }
    if (!name.isEmpty() && !names.isEmpty()) {
        // both are set
        return failure("Must use only one annotations 'name' or 'names', not both", func);
    }
    // start by getting field information.
    List<ValueReference> params = Lists.newArrayList();
    List<WorkspaceReference> workspaceFields = Lists.newArrayList();
    ValueReference outputField = null;
    for (FieldDescriptor field : func.getFields()) {
        Param param = field.getAnnotationProxy(Param.class);
        Output output = field.getAnnotationProxy(Output.class);
        Workspace workspace = field.getAnnotationProxy(Workspace.class);
        Inject inject = field.getAnnotationProxy(Inject.class);
        Annotation[] annotations = { param, output, workspace, inject };
        int annotationCount = 0;
        for (Annotation annotationDescriptor : annotations) {
            if (annotationDescriptor != null) {
                annotationCount += 1;
            }
        }
        if (annotationCount == 0) {
            return failure("The field must be either a @Param, @Output, @Inject or @Workspace field.", func, field);
        } else if (annotationCount > 1) {
            return failure("The field must be only one of @Param, @Output, @Inject or @Workspace. It currently has more than one of these annotations.", func, field);
        }
        // TODO(Julien): verify there are a few of those and we can load them
        Class<?> fieldClass = field.getFieldClass();
        if (param != null || output != null) {
            // Special processing for @Param FieldReader
            if (param != null && FieldReader.class.isAssignableFrom(fieldClass)) {
                params.add(ValueReference.createFieldReaderRef(field.getName()));
                continue;
            }
            // Special processing for @Output ComplexWriter
            if (output != null && ComplexWriter.class.isAssignableFrom(fieldClass)) {
                if (outputField != null) {
                    return failure("You've declared more than one @Output field.  You must declare one and only @Output field per Function class.", func, field);
                } else {
                    outputField = ValueReference.createComplexWriterRef(field.getName());
                }
                continue;
            }
            // check that param and output are value holders.
            if (!ValueHolder.class.isAssignableFrom(fieldClass)) {
                return failure(String.format("The field doesn't holds value of type %s which does not implement the ValueHolder interface.  All fields of type @Param or @Output must extend this interface..", fieldClass), func, field);
            }
            // get the type field from the value holder.
            MajorType type = null;
            try {
                type = getStaticFieldValue("TYPE", fieldClass, MajorType.class);
            } catch (Exception e) {
                return failure("Failure while trying to access the ValueHolder's TYPE static variable.  All ValueHolders must contain a static TYPE variable that defines their MajorType.", e, func, field);
            }
            ValueReference p = new ValueReference(type, field.getName());
            if (param != null) {
                p.setConstant(param.constant());
                params.add(p);
            } else {
                if (outputField != null) {
                    return failure("You've declared more than one @Output field.  You must declare one and only @Output field per Function class.", func, field);
                } else {
                    outputField = p;
                }
            }
        } else {
            // workspace work.
            boolean isInject = inject != null;
            if (isInject && UdfUtilities.INJECTABLE_GETTER_METHODS.get(fieldClass) == null) {
                return failure(String.format("A %s cannot be injected into a %s," + " available injectable classes are: %s.", fieldClass, DrillFunc.class.getSimpleName(), Joiner.on(",").join(UdfUtilities.INJECTABLE_GETTER_METHODS.keySet())), func, field);
            }
            WorkspaceReference wsReference = new WorkspaceReference(fieldClass, field.getName(), isInject);
            if (!isInject && template.scope() == FunctionScope.POINT_AGGREGATE && !ValueHolder.class.isAssignableFrom(fieldClass)) {
                return failure(String.format("Aggregate function '%s' workspace variable '%s' is of type '%s'. Please change it to Holder type.", func.getClassName(), field.getName(), fieldClass), func, field);
            }
            // If the workspace var is of Holder type, get its MajorType and assign to WorkspaceReference.
            if (ValueHolder.class.isAssignableFrom(fieldClass)) {
                MajorType majorType = null;
                try {
                    majorType = getStaticFieldValue("TYPE", fieldClass, MajorType.class);
                } catch (Exception e) {
                    return failure("Failure while trying to access the ValueHolder's TYPE static variable.  All ValueHolders must contain a static TYPE variable that defines their MajorType.", e, func, field);
                }
                wsReference.setMajorType(majorType);
            }
            workspaceFields.add(wsReference);
        }
    }
    if (outputField == null) {
        return failure("This function declares zero output fields.  A function must declare one output field.", func);
    }
    FunctionInitializer initializer = new FunctionInitializer(func.getClassName(), classLoader);
    try {
        // return holder
        ValueReference[] ps = params.toArray(new ValueReference[params.size()]);
        WorkspaceReference[] works = workspaceFields.toArray(new WorkspaceReference[workspaceFields.size()]);
        FunctionAttributes functionAttributes = new FunctionAttributes(template, ps, outputField, works);
        switch(template.scope()) {
            case POINT_AGGREGATE:
                return new DrillAggFuncHolder(functionAttributes, initializer);
            case SIMPLE:
                return outputField.isComplexWriter() ? new DrillComplexWriterFuncHolder(functionAttributes, initializer) : new DrillSimpleFuncHolder(functionAttributes, initializer);
            case HOLISTIC_AGGREGATE:
            case RANGE_AGGREGATE:
            default:
                return failure("Unsupported Function Type.", func);
        }
    } catch (Exception | NoSuchFieldError | AbstractMethodError ex) {
        return failure("Failure while creating function holder.", ex, func);
    }
}
Also used : ComplexWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ComplexWriter) FieldDescriptor(org.apache.drill.common.scanner.persistence.FieldDescriptor) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) Output(org.apache.drill.exec.expr.annotations.Output) Inject(javax.inject.Inject) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) ValueHolder(org.apache.drill.exec.expr.holders.ValueHolder) Annotation(java.lang.annotation.Annotation) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) Param(org.apache.drill.exec.expr.annotations.Param) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) Workspace(org.apache.drill.exec.expr.annotations.Workspace)

Example 5 with FunctionTemplate

use of org.apache.drill.exec.expr.annotations.FunctionTemplate in project drill by apache.

the class TestClassPathScanner method testFunctionTemplates.

@Test
public void testFunctionTemplates() throws Exception {
    List<AnnotatedClassDescriptor> functions = result.getAnnotatedClasses(FunctionTemplate.class.getName());
    Set<String> scanned = new HashSet<>();
    AnnotatedClassDescriptor functionRandomBigIntGauss = null;
    for (AnnotatedClassDescriptor function : functions) {
        assertTrue(function.getClassName() + " scanned twice", scanned.add(function.getClassName()));
        if (function.getClassName().equals(RandomBigIntGauss.class.getName())) {
            functionRandomBigIntGauss = function;
        }
    }
    if (functionRandomBigIntGauss == null) {
        Assert.fail("functionRandomBigIntGauss not found");
    }
    // TODO: use Andrew's randomized test framework to verify a subset of the functions
    for (AnnotatedClassDescriptor function : functions) {
        Class<?> c = Class.forName(function.getClassName(), false, this.getClass().getClassLoader());
        Field[] fields = c.getDeclaredFields();
        assertEquals("fields count for " + function, fields.length, function.getFields().size());
        for (int i = 0; i < fields.length; i++) {
            FieldDescriptor fieldDescriptor = function.getFields().get(i);
            Field field = fields[i];
            assertEquals("Class fields:\n" + Arrays.toString(fields) + "\n != \nDescriptor fields:\n" + function.getFields(), field.getName(), fieldDescriptor.getName());
            verifyAnnotations(field.getDeclaredAnnotations(), fieldDescriptor.getAnnotations());
            assertEquals(field.getType(), fieldDescriptor.getFieldClass());
        }
        Annotation[] annotations = c.getDeclaredAnnotations();
        List<AnnotationDescriptor> scannedAnnotations = function.getAnnotations();
        verifyAnnotations(annotations, scannedAnnotations);
        FunctionTemplate bytecodeAnnotation = function.getAnnotationProxy(FunctionTemplate.class);
        assertNotNull(bytecodeAnnotation);
        FunctionTemplate reflectionAnnotation = c.getAnnotation(FunctionTemplate.class);
        assertEquals(reflectionAnnotation.name(), bytecodeAnnotation.name());
        assertArrayEquals(reflectionAnnotation.names(), bytecodeAnnotation.names());
        assertEquals(reflectionAnnotation.scope(), bytecodeAnnotation.scope());
        assertEquals(reflectionAnnotation.nulls(), bytecodeAnnotation.nulls());
        assertEquals(reflectionAnnotation.isBinaryCommutative(), bytecodeAnnotation.isBinaryCommutative());
        assertEquals(reflectionAnnotation.desc(), bytecodeAnnotation.desc());
        assertEquals(reflectionAnnotation.costCategory(), bytecodeAnnotation.costCategory());
    }
    for (String baseType : result.getScannedClasses()) {
        validateType(result, baseType);
    }
    assertTrue(result.getImplementations(PhysicalOperator.class).size() > 0);
    assertTrue(result.getImplementations(DrillFunc.class).size() > 0);
}
Also used : AnnotationDescriptor(org.apache.drill.common.scanner.persistence.AnnotationDescriptor) Annotation(java.lang.annotation.Annotation) FieldDescriptor(org.apache.drill.common.scanner.persistence.FieldDescriptor) AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) Field(java.lang.reflect.Field) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) RandomBigIntGauss(org.apache.drill.exec.fn.impl.testing.GeneratorFunctions.RandomBigIntGauss) HashSet(java.util.HashSet) DrillFunc(org.apache.drill.exec.expr.DrillFunc) BaseTest(org.apache.drill.test.BaseTest) SlowTest(org.apache.drill.categories.SlowTest) Test(org.junit.Test)

Aggregations

Annotation (java.lang.annotation.Annotation)7 FunctionTemplate (org.apache.drill.exec.expr.annotations.FunctionTemplate)7 AnnotationDescriptor (org.apache.drill.common.scanner.persistence.AnnotationDescriptor)5 FieldDescriptor (org.apache.drill.common.scanner.persistence.FieldDescriptor)5 Field (java.lang.reflect.Field)3 HashSet (java.util.HashSet)3 AnnotatedClassDescriptor (org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor)3 DrillFunc (org.apache.drill.exec.expr.DrillFunc)3 RandomBigIntGauss (org.apache.drill.exec.fn.impl.testing.GeneratorFunctions.RandomBigIntGauss)3 PhysicalOperator (org.apache.drill.exec.physical.base.PhysicalOperator)3 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 Inject (javax.inject.Inject)2 SlowTest (org.apache.drill.categories.SlowTest)2 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)2 ScanResult (org.apache.drill.common.scanner.persistence.ScanResult)2 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)2 Output (org.apache.drill.exec.expr.annotations.Output)2 Param (org.apache.drill.exec.expr.annotations.Param)2 Workspace (org.apache.drill.exec.expr.annotations.Workspace)2