Search in sources :

Example 1 with MethodHandleAndConstructor

use of com.facebook.presto.operator.scalar.annotations.ScalarImplementation.MethodHandleAndConstructor in project presto by prestodb.

the class ParametricScalar method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    Signature boundSignature = applyBoundVariables(getSignature(), boundVariables, arity);
    if (implementations.getExactImplementations().containsKey(boundSignature)) {
        ScalarImplementation implementation = implementations.getExactImplementations().get(boundSignature);
        Optional<MethodHandleAndConstructor> methodHandleAndConstructor = implementation.specialize(boundSignature, boundVariables, typeManager, functionRegistry);
        checkCondition(methodHandleAndConstructor.isPresent(), FUNCTION_IMPLEMENTATION_ERROR, String.format("Exact implementation of %s do not match expected java types.", boundSignature.getName()));
        return new ScalarFunctionImplementation(implementation.isNullable(), implementation.getNullableArguments(), implementation.getNullFlags(), methodHandleAndConstructor.get().getMethodHandle(), methodHandleAndConstructor.get().getConstructor(), isDeterministic());
    }
    ScalarFunctionImplementation selectedImplementation = null;
    for (ScalarImplementation implementation : implementations.getSpecializedImplementations()) {
        Optional<MethodHandleAndConstructor> methodHandle = implementation.specialize(boundSignature, boundVariables, typeManager, functionRegistry);
        if (methodHandle.isPresent()) {
            checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", getSignature(), boundVariables.getTypeVariables());
            selectedImplementation = new ScalarFunctionImplementation(implementation.isNullable(), implementation.getNullableArguments(), implementation.getNullFlags(), methodHandle.get().getMethodHandle(), methodHandle.get().getConstructor(), isDeterministic());
        }
    }
    if (selectedImplementation != null) {
        return selectedImplementation;
    }
    for (ScalarImplementation implementation : implementations.getGenericImplementations()) {
        Optional<MethodHandleAndConstructor> methodHandle = implementation.specialize(boundSignature, boundVariables, typeManager, functionRegistry);
        if (methodHandle.isPresent()) {
            checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", getSignature(), boundVariables.getTypeVariables());
            selectedImplementation = new ScalarFunctionImplementation(implementation.isNullable(), implementation.getNullableArguments(), implementation.getNullFlags(), methodHandle.get().getMethodHandle(), methodHandle.get().getConstructor(), isDeterministic());
        }
    }
    if (selectedImplementation != null) {
        return selectedImplementation;
    }
    throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("Unsupported type parameters (%s) for %s", boundVariables, getSignature()));
}
Also used : MethodHandleAndConstructor(com.facebook.presto.operator.scalar.annotations.ScalarImplementation.MethodHandleAndConstructor) Signature(com.facebook.presto.metadata.Signature) PrestoException(com.facebook.presto.spi.PrestoException) ScalarImplementation(com.facebook.presto.operator.scalar.annotations.ScalarImplementation)

Aggregations

Signature (com.facebook.presto.metadata.Signature)1 ScalarImplementation (com.facebook.presto.operator.scalar.annotations.ScalarImplementation)1 MethodHandleAndConstructor (com.facebook.presto.operator.scalar.annotations.ScalarImplementation.MethodHandleAndConstructor)1 PrestoException (com.facebook.presto.spi.PrestoException)1