Search in sources :

Example 1 with FunctionValidationException

use of org.apache.drill.exec.exception.FunctionValidationException in project drill by axbaretto.

the class FunctionImplementationRegistry method validate.

/**
 * Using given local path to jar creates unique class loader for this jar.
 * Class loader is closed to release opened connection to jar when validation is finished.
 * Scan jar content to receive list of all scanned classes
 * and starts validation process against local function registry.
 * Checks if received list of validated function is not empty.
 *
 * @param path local path to jar we need to validate
 * @return list of validated function signatures
 */
public List<String> validate(Path path) throws IOException {
    URL url = path.toUri().toURL();
    URL[] urls = { url };
    try (URLClassLoader classLoader = new URLClassLoader(urls)) {
        ScanResult jarScanResult = scan(classLoader, path, urls);
        List<String> functions = localFunctionRegistry.validate(path.getName(), jarScanResult);
        if (functions.isEmpty()) {
            throw new FunctionValidationException(String.format("Jar %s does not contain functions", path.getName()));
        }
        return functions;
    }
}
Also used : FunctionValidationException(org.apache.drill.exec.exception.FunctionValidationException) ScanResult(org.apache.drill.common.scanner.persistence.ScanResult) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Example 2 with FunctionValidationException

use of org.apache.drill.exec.exception.FunctionValidationException in project drill by axbaretto.

the class LocalFunctionRegistry method validate.

/**
 * Validates all functions, present in jars.
 * Will throw {@link FunctionValidationException} if:
 * <ol>
 *  <li>Jar with the same name has been already registered.</li>
 *  <li>Conflicting function with the similar signature is found.</li>
 *  <li>Aggregating function is not deterministic.</li>
 *</ol>
 * @param jarName jar name to be validated
 * @param scanResult scan of all classes present in jar
 * @return list of validated function signatures
 */
public List<String> validate(String jarName, ScanResult scanResult) {
    List<String> functions = Lists.newArrayList();
    FunctionConverter converter = new FunctionConverter();
    List<AnnotatedClassDescriptor> providerClasses = scanResult.getAnnotatedClasses();
    if (registryHolder.containsJar(jarName)) {
        throw new JarValidationException(String.format("Jar with %s name has been already registered", jarName));
    }
    final ListMultimap<String, String> allFuncWithSignatures = registryHolder.getAllFunctionsWithSignatures();
    for (AnnotatedClassDescriptor func : providerClasses) {
        DrillFuncHolder holder = converter.getHolder(func, ClassLoader.getSystemClassLoader());
        if (holder != null) {
            String functionInput = holder.getInputParameters();
            String[] names = holder.getRegisteredNames();
            for (String name : names) {
                String functionName = name.toLowerCase();
                String functionSignature = String.format(functionSignaturePattern, functionName, functionInput);
                if (allFuncWithSignatures.get(functionName).contains(functionSignature)) {
                    throw new FunctionValidationException(String.format("Found duplicated function in %s: %s", registryHolder.getJarNameByFunctionSignature(functionName, functionSignature), functionSignature));
                } else if (holder.isAggregating() && !holder.isDeterministic()) {
                    throw new FunctionValidationException(String.format("Aggregate functions must be deterministic: %s", func.getClassName()));
                } else {
                    functions.add(functionSignature);
                    allFuncWithSignatures.put(functionName, functionSignature);
                }
            }
        } else {
            logger.warn("Unable to initialize function for class {}", func.getClassName());
        }
    }
    return functions;
}
Also used : AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) FunctionValidationException(org.apache.drill.exec.exception.FunctionValidationException) DrillFuncHolder(org.apache.drill.exec.expr.fn.DrillFuncHolder) FunctionConverter(org.apache.drill.exec.expr.fn.FunctionConverter) JarValidationException(org.apache.drill.exec.exception.JarValidationException)

Example 3 with FunctionValidationException

use of org.apache.drill.exec.exception.FunctionValidationException in project drill by apache.

the class LocalFunctionRegistry method validate.

/**
 * Validates all functions, present in jars.
 * Will throw {@link FunctionValidationException} if:
 * <ol>
 *  <li>Jar with the same name has been already registered.</li>
 *  <li>Conflicting function with the similar signature is found.</li>
 *  <li>Aggregating function is not deterministic.</li>
 *</ol>
 * @param jarName jar name to be validated
 * @param scanResult scan of all classes present in jar
 * @return list of validated function signatures
 */
public List<String> validate(String jarName, ScanResult scanResult) {
    List<String> functions = Lists.newArrayList();
    FunctionConverter converter = new FunctionConverter();
    List<AnnotatedClassDescriptor> providerClasses = scanResult.getAnnotatedClasses(FunctionTemplate.class.getName());
    if (registryHolder.containsJar(jarName)) {
        throw new JarValidationException(String.format("Jar with %s name has been already registered", jarName));
    }
    final ListMultimap<String, String> allFuncWithSignatures = registryHolder.getAllFunctionsWithSignatures();
    for (AnnotatedClassDescriptor func : providerClasses) {
        DrillFuncHolder holder = converter.getHolder(func, ClassLoader.getSystemClassLoader());
        if (holder != null) {
            String functionInput = holder.getInputParameters();
            String[] names = holder.getRegisteredNames();
            for (String name : names) {
                String functionName = name.toLowerCase();
                String functionSignature = String.format(functionSignaturePattern, functionName, functionInput);
                if (allFuncWithSignatures.get(functionName).contains(functionSignature)) {
                    throw new FunctionValidationException(String.format("Found duplicated function in %s: %s", registryHolder.getJarNameByFunctionSignature(functionName, functionSignature), functionSignature));
                } else if (holder.isAggregating() && !holder.isDeterministic()) {
                    throw new FunctionValidationException(String.format("Aggregate functions must be deterministic: %s", func.getClassName()));
                } else {
                    functions.add(functionSignature);
                    allFuncWithSignatures.put(functionName, functionSignature);
                }
            }
        } else {
            logger.warn("Unable to initialize function for class {}", func.getClassName());
        }
    }
    return functions;
}
Also used : AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) FunctionValidationException(org.apache.drill.exec.exception.FunctionValidationException) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) DrillFuncHolder(org.apache.drill.exec.expr.fn.DrillFuncHolder) FunctionConverter(org.apache.drill.exec.expr.fn.FunctionConverter) JarValidationException(org.apache.drill.exec.exception.JarValidationException)

Example 4 with FunctionValidationException

use of org.apache.drill.exec.exception.FunctionValidationException in project drill by apache.

the class FunctionImplementationRegistry method validate.

/**
 * Using given local path to jar creates unique class loader for this jar.
 * Class loader is closed to release opened connection to jar when validation is finished.
 * Scan jar content to receive list of all scanned classes
 * and starts validation process against local function registry.
 * Checks if received list of validated function is not empty.
 *
 * @param path local path to jar we need to validate
 * @return list of validated function signatures
 */
public List<String> validate(Path path) throws IOException {
    URL url = path.toUri().toURL();
    URL[] urls = { url };
    try (URLClassLoader classLoader = new URLClassLoader(urls)) {
        ScanResult jarScanResult = scan(classLoader, path, urls);
        List<String> functions = localFunctionRegistry.validate(path.getName(), jarScanResult);
        if (functions.isEmpty()) {
            throw new FunctionValidationException(String.format("Jar %s does not contain functions", path.getName()));
        }
        return functions;
    }
}
Also used : FunctionValidationException(org.apache.drill.exec.exception.FunctionValidationException) ScanResult(org.apache.drill.common.scanner.persistence.ScanResult) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Aggregations

FunctionValidationException (org.apache.drill.exec.exception.FunctionValidationException)4 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 AnnotatedClassDescriptor (org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor)2 ScanResult (org.apache.drill.common.scanner.persistence.ScanResult)2 JarValidationException (org.apache.drill.exec.exception.JarValidationException)2 DrillFuncHolder (org.apache.drill.exec.expr.fn.DrillFuncHolder)2 FunctionConverter (org.apache.drill.exec.expr.fn.FunctionConverter)2 FunctionTemplate (org.apache.drill.exec.expr.annotations.FunctionTemplate)1