Search in sources :

Example 1 with FunctionBinding

use of io.trino.metadata.FunctionBinding 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)

Example 2 with FunctionBinding

use of io.trino.metadata.FunctionBinding in project trino by trinodb.

the class ParametricAggregation method specialize.

@Override
public AggregationMetadata specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
    // Find implementation matching arguments
    AggregationImplementation concreteImplementation = findMatchingImplementation(boundSignature);
    // Build state factory and serializer
    AccumulatorStateDescriptor<?> accumulatorStateDescriptor = generateAccumulatorStateDescriptor(stateClass);
    // Bind provided dependencies to aggregation method handlers
    FunctionMetadata metadata = getFunctionMetadata();
    FunctionBinding functionBinding = SignatureBinder.bindFunction(metadata.getFunctionId(), metadata.getSignature(), boundSignature);
    MethodHandle inputHandle = bindDependencies(concreteImplementation.getInputFunction(), concreteImplementation.getInputDependencies(), functionBinding, functionDependencies);
    Optional<MethodHandle> removeInputHandle = concreteImplementation.getRemoveInputFunction().map(removeInputFunction -> bindDependencies(removeInputFunction, concreteImplementation.getRemoveInputDependencies(), functionBinding, functionDependencies));
    Optional<MethodHandle> combineHandle = concreteImplementation.getCombineFunction();
    if (getAggregationMetadata().isDecomposable()) {
        checkArgument(combineHandle.isPresent(), "Decomposable method %s does not have a combine method", boundSignature.getName());
        combineHandle = combineHandle.map(combineFunction -> bindDependencies(combineFunction, concreteImplementation.getCombineDependencies(), functionBinding, functionDependencies));
    } else {
        checkArgument(concreteImplementation.getCombineFunction().isEmpty(), "Decomposable method %s does not have a combine method", boundSignature.getName());
    }
    MethodHandle outputHandle = bindDependencies(concreteImplementation.getOutputFunction(), concreteImplementation.getOutputDependencies(), functionBinding, functionDependencies);
    List<AggregationParameterKind> inputParameterKinds = concreteImplementation.getInputParameterKinds();
    inputHandle = normalizeInputMethod(inputHandle, boundSignature, inputParameterKinds);
    removeInputHandle = removeInputHandle.map(function -> normalizeInputMethod(function, boundSignature, inputParameterKinds));
    return new AggregationMetadata(inputHandle, removeInputHandle, combineHandle, outputHandle, ImmutableList.of(accumulatorStateDescriptor));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) FunctionDependencies(io.trino.metadata.FunctionDependencies) FunctionDependencyDeclarationBuilder(io.trino.metadata.FunctionDependencyDeclaration.FunctionDependencyDeclarationBuilder) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) ImplementationDependency(io.trino.operator.annotations.ImplementationDependency) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature(io.trino.metadata.Signature) FunctionMetadata(io.trino.metadata.FunctionMetadata) StateCompiler.getSerializedType(io.trino.operator.aggregation.state.StateCompiler.getSerializedType) AggregationFunctionAdapter.normalizeInputMethod(io.trino.operator.aggregation.AggregationFunctionAdapter.normalizeInputMethod) FunctionDependencyDeclaration(io.trino.metadata.FunctionDependencyDeclaration) ParametricImplementationsGroup(io.trino.operator.ParametricImplementationsGroup) ParametricFunctionHelpers.bindDependencies(io.trino.operator.ParametricFunctionHelpers.bindDependencies) Collection(java.util.Collection) TrinoException(io.trino.spi.TrinoException) StateCompiler.generateStateFactory(io.trino.operator.aggregation.state.StateCompiler.generateStateFactory) FunctionBinding(io.trino.metadata.FunctionBinding) StateCompiler.generateStateSerializer(io.trino.operator.aggregation.state.StateCompiler.generateStateSerializer) AGGREGATE(io.trino.metadata.FunctionKind.AGGREGATE) AccumulatorState(io.trino.spi.function.AccumulatorState) String.format(java.lang.String.format) AggregationParameterKind(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind) AMBIGUOUS_FUNCTION_CALL(io.trino.spi.StandardErrorCode.AMBIGUOUS_FUNCTION_CALL) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) List(java.util.List) SignatureBinder(io.trino.metadata.SignatureBinder) BoundSignature(io.trino.metadata.BoundSignature) StringJoiner(java.util.StringJoiner) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FUNCTION_IMPLEMENTATION_MISSING(io.trino.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_MISSING) SqlAggregationFunction(io.trino.metadata.SqlAggregationFunction) FunctionMetadata(io.trino.metadata.FunctionMetadata) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) FunctionBinding(io.trino.metadata.FunctionBinding) AggregationParameterKind(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind) MethodHandle(java.lang.invoke.MethodHandle)

Example 3 with FunctionBinding

use of io.trino.metadata.FunctionBinding in project trino by trinodb.

the class TestAnnotationEngineForAggregates method specializeAggregationFunction.

private static void specializeAggregationFunction(BoundSignature boundSignature, SqlAggregationFunction aggregation) {
    FunctionMetadata functionMetadata = aggregation.getFunctionMetadata();
    FunctionBinding functionBinding = MetadataManager.toFunctionBinding(functionMetadata.getFunctionId(), boundSignature, functionMetadata.getSignature());
    AggregationFunctionMetadata aggregationMetadata = aggregation.getAggregationMetadata();
    assertFalse(aggregationMetadata.isOrderSensitive());
    assertFalse(aggregationMetadata.getIntermediateTypes().isEmpty());
    ResolvedFunction resolvedFunction = METADATA.resolve(TEST_SESSION, functionBinding, functionMetadata, aggregation.getFunctionDependencies(boundSignature));
    FunctionDependencies functionDependencies = new FunctionDependencies(FUNCTION_MANAGER::getScalarFunctionInvoker, resolvedFunction.getTypeDependencies(), resolvedFunction.getFunctionDependencies());
    aggregation.specialize(boundSignature, functionDependencies);
}
Also used : FunctionMetadata(io.trino.metadata.FunctionMetadata) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) FunctionBinding(io.trino.metadata.FunctionBinding) ResolvedFunction(io.trino.metadata.ResolvedFunction) FunctionDependencies(io.trino.metadata.FunctionDependencies) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata)

Aggregations

FunctionBinding (io.trino.metadata.FunctionBinding)3 FunctionMetadata (io.trino.metadata.FunctionMetadata)3 AggregationFunctionMetadata (io.trino.metadata.AggregationFunctionMetadata)2 FunctionDependencies (io.trino.metadata.FunctionDependencies)2 TrinoException (io.trino.spi.TrinoException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ImmutableList (com.google.common.collect.ImmutableList)1 BoundSignature (io.trino.metadata.BoundSignature)1 FunctionDependencyDeclaration (io.trino.metadata.FunctionDependencyDeclaration)1 FunctionDependencyDeclarationBuilder (io.trino.metadata.FunctionDependencyDeclaration.FunctionDependencyDeclarationBuilder)1 AGGREGATE (io.trino.metadata.FunctionKind.AGGREGATE)1 ResolvedFunction (io.trino.metadata.ResolvedFunction)1 Signature (io.trino.metadata.Signature)1 SignatureBinder (io.trino.metadata.SignatureBinder)1 SqlAggregationFunction (io.trino.metadata.SqlAggregationFunction)1 ParametricFunctionHelpers.bindDependencies (io.trino.operator.ParametricFunctionHelpers.bindDependencies)1 ParametricImplementationsGroup (io.trino.operator.ParametricImplementationsGroup)1 AggregationParameterKind (io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind)1 AggregationFunctionAdapter.normalizeInputMethod (io.trino.operator.aggregation.AggregationFunctionAdapter.normalizeInputMethod)1