Search in sources :

Example 6 with FunctionDependencies

use of io.trino.metadata.FunctionDependencies 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 7 with FunctionDependencies

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

Example 8 with FunctionDependencies

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

the class TestAnnotationEngineForScalars method testSingleImplementationScalarParse.

@Test
public void testSingleImplementationScalarParse() {
    Signature expectedSignature = new Signature("single_implementation_parametric_scalar", DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(SingleImplementationScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    FunctionMetadata functionMetadata = scalar.getFunctionMetadata();
    assertEquals(functionMetadata.getSignature(), expectedSignature);
    assertTrue(functionMetadata.isDeterministic());
    assertFalse(functionMetadata.isHidden());
    assertEquals(functionMetadata.getDescription(), "Simple scalar with single implementation based on class");
    assertFalse(functionMetadata.getFunctionNullability().isArgumentNullable(0));
    assertImplementationCount(scalar, 1, 0, 0);
    BoundSignature boundSignature = new BoundSignature(expectedSignature.getName(), DOUBLE, ImmutableList.of(DOUBLE));
    ChoicesScalarFunctionImplementation specialized = (ChoicesScalarFunctionImplementation) scalar.specialize(boundSignature, new FunctionDependencies(FUNCTION_MANAGER::getScalarFunctionInvoker, ImmutableMap.of(), ImmutableSet.of()));
    assertFalse(specialized.getChoices().get(0).getInstanceFactory().isPresent());
}
Also used : FunctionMetadata(io.trino.metadata.FunctionMetadata) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricScalar(io.trino.operator.scalar.ParametricScalar) ChoicesScalarFunctionImplementation(io.trino.operator.scalar.ChoicesScalarFunctionImplementation) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) FunctionDependencies(io.trino.metadata.FunctionDependencies) Test(org.testng.annotations.Test)

Aggregations

FunctionDependencies (io.trino.metadata.FunctionDependencies)8 FunctionMetadata (io.trino.metadata.FunctionMetadata)8 BoundSignature (io.trino.metadata.BoundSignature)7 Signature (io.trino.metadata.Signature)7 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)6 TypeSignature (io.trino.spi.type.TypeSignature)6 ImmutableList (com.google.common.collect.ImmutableList)4 FunctionDependencyDeclaration (io.trino.metadata.FunctionDependencyDeclaration)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 FunctionDependencyDeclarationBuilder (io.trino.metadata.FunctionDependencyDeclaration.FunctionDependencyDeclarationBuilder)3 SCALAR (io.trino.metadata.FunctionKind.SCALAR)3 FunctionNullability (io.trino.metadata.FunctionNullability)3 TrinoException (io.trino.spi.TrinoException)3 NEVER_NULL (io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL)3 FAIL_ON_NULL (io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL)3 InvocationConvention.simpleConvention (io.trino.spi.function.InvocationConvention.simpleConvention)3 MethodHandle (java.lang.invoke.MethodHandle)3 List (java.util.List)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Streams.mapWithIndex (com.google.common.collect.Streams.mapWithIndex)2