Search in sources :

Example 1 with ParametricScalarImplementation

use of io.trino.operator.scalar.annotations.ParametricScalarImplementation in project trino by trinodb.

the class ParametricScalar method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
    FunctionMetadata metadata = getFunctionMetadata();
    FunctionBinding functionBinding = SignatureBinder.bindFunction(metadata.getFunctionId(), metadata.getSignature(), boundSignature);
    ParametricScalarImplementation exactImplementation = implementations.getExactImplementations().get(boundSignature.toSignature());
    if (exactImplementation != null) {
        Optional<ScalarFunctionImplementation> scalarFunctionImplementation = exactImplementation.specialize(functionBinding, functionDependencies);
        checkCondition(scalarFunctionImplementation.isPresent(), FUNCTION_IMPLEMENTATION_ERROR, format("Exact implementation of %s do not match expected java types.", boundSignature.getName()));
        return scalarFunctionImplementation.get();
    }
    ScalarFunctionImplementation selectedImplementation = null;
    for (ParametricScalarImplementation implementation : implementations.getSpecializedImplementations()) {
        Optional<ScalarFunctionImplementation> scalarFunctionImplementation = implementation.specialize(functionBinding, functionDependencies);
        if (scalarFunctionImplementation.isPresent()) {
            checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", metadata.getSignature(), boundSignature);
            selectedImplementation = scalarFunctionImplementation.get();
        }
    }
    if (selectedImplementation != null) {
        return selectedImplementation;
    }
    for (ParametricScalarImplementation implementation : implementations.getGenericImplementations()) {
        Optional<ScalarFunctionImplementation> scalarFunctionImplementation = implementation.specialize(functionBinding, functionDependencies);
        if (scalarFunctionImplementation.isPresent()) {
            checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", metadata.getSignature(), boundSignature);
            selectedImplementation = scalarFunctionImplementation.get();
        }
    }
    if (selectedImplementation != null) {
        return selectedImplementation;
    }
    throw new TrinoException(FUNCTION_IMPLEMENTATION_MISSING, format("Unsupported binding %s for signature %s", boundSignature, getFunctionMetadata().getSignature()));
}
Also used : FunctionMetadata(io.trino.metadata.FunctionMetadata) FunctionBinding(io.trino.metadata.FunctionBinding) ParametricScalarImplementation(io.trino.operator.scalar.annotations.ParametricScalarImplementation) TrinoException(io.trino.spi.TrinoException)

Aggregations

FunctionBinding (io.trino.metadata.FunctionBinding)1 FunctionMetadata (io.trino.metadata.FunctionMetadata)1 ParametricScalarImplementation (io.trino.operator.scalar.annotations.ParametricScalarImplementation)1 TrinoException (io.trino.spi.TrinoException)1