Search in sources :

Example 16 with Signature

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

the class TestAnnotationEngineForScalars method testWithNullablePrimitiveArgScalarParse.

@Test
public void testWithNullablePrimitiveArgScalarParse() {
    Signature expectedSignature = new Signature("scalar_with_nullable", DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature(), DOUBLE.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(WithNullablePrimitiveArgScalarFunction.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 nullable primitive");
    assertFalse(functionMetadata.getFunctionNullability().isArgumentNullable(0));
    assertTrue(functionMetadata.getFunctionNullability().isArgumentNullable(1));
    BoundSignature boundSignature = new BoundSignature(expectedSignature.getName(), DOUBLE, ImmutableList.of(DOUBLE, 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)

Example 17 with Signature

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

the class TestAnnotationEngineForScalars method testConstructorInjectionScalarParse.

@Test
public void testConstructorInjectionScalarParse() {
    Signature expectedSignature = new Signature("parametric_scalar_inject_constructor", ImmutableList.of(typeVariable("T")), ImmutableList.of(), BIGINT.getTypeSignature(), ImmutableList.of(arrayType(new TypeSignature("T"))), false);
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(ConstructorInjectionScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertImplementationCount(scalar, 2, 0, 1);
    List<ParametricScalarImplementationChoice> parametricScalarImplementationChoices = scalar.getImplementations().getGenericImplementations().get(0).getChoices();
    assertEquals(parametricScalarImplementationChoices.size(), 1);
    List<ImplementationDependency> dependencies = parametricScalarImplementationChoices.get(0).getDependencies();
    assertEquals(dependencies.size(), 0);
    List<ImplementationDependency> constructorDependencies = parametricScalarImplementationChoices.get(0).getConstructorDependencies();
    assertEquals(constructorDependencies.size(), 1);
    assertTrue(constructorDependencies.get(0) instanceof TypeImplementationDependency);
    FunctionMetadata functionMetadata = scalar.getFunctionMetadata();
    assertEquals(functionMetadata.getSignature(), expectedSignature);
    assertTrue(functionMetadata.isDeterministic());
    assertFalse(functionMetadata.isHidden());
    assertEquals(functionMetadata.getDescription(), "Parametric scalar with type injected though constructor");
}
Also used : FunctionMetadata(io.trino.metadata.FunctionMetadata) TypeSignature(io.trino.spi.type.TypeSignature) ImplementationDependency(io.trino.operator.annotations.ImplementationDependency) TypeImplementationDependency(io.trino.operator.annotations.TypeImplementationDependency) LiteralImplementationDependency(io.trino.operator.annotations.LiteralImplementationDependency) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricScalarImplementationChoice(io.trino.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice) ParametricScalar(io.trino.operator.scalar.ParametricScalar) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) TypeImplementationDependency(io.trino.operator.annotations.TypeImplementationDependency) Test(org.testng.annotations.Test)

Example 18 with Signature

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

the class TestAnnotationEngineForAggregates method testInjectTypeAggregateParse.

@Test
public void testInjectTypeAggregateParse() {
    Signature expectedSignature = new Signature("inject_type_aggregate", ImmutableList.of(typeVariable("T")), ImmutableList.of(), new TypeSignature("T"), ImmutableList.of(new TypeSignature("T")), false);
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(InjectTypeAggregateFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with type injected");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertEquals(implementations.getGenericImplementations().size(), 1);
    AggregationImplementation implementation = implementations.getGenericImplementations().get(0);
    assertEquals(implementation.getDefinitionClass(), InjectTypeAggregateFunction.class);
    assertDependencyCount(implementation, 1, 1, 1);
    assertTrue(implementation.getInputDependencies().get(0) instanceof TypeImplementationDependency);
    assertTrue(implementation.getCombineDependencies().get(0) instanceof TypeImplementationDependency);
    assertTrue(implementation.getOutputDependencies().get(0) instanceof TypeImplementationDependency);
    assertFalse(implementation.hasSpecializedTypeParameters());
    assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(DoubleType.DOUBLE));
    specializeAggregationFunction(boundSignature, aggregation);
}
Also used : AggregationImplementation(io.trino.operator.aggregation.AggregationImplementation) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricAggregation(io.trino.operator.aggregation.ParametricAggregation) TypeImplementationDependency(io.trino.operator.annotations.TypeImplementationDependency) Test(org.testng.annotations.Test)

Example 19 with Signature

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

the class ScalarFromAnnotationsParser method parseParametricScalar.

private static SqlScalarFunction parseParametricScalar(ScalarHeaderAndMethods scalar, Optional<Constructor<?>> constructor, boolean deprecated) {
    ScalarImplementationHeader header = scalar.getHeader();
    checkArgument(!header.getName().isEmpty());
    Map<SpecializedSignature, ParametricScalarImplementation.Builder> signatures = new HashMap<>();
    for (Method method : scalar.getMethods()) {
        ParametricScalarImplementation.Parser implementation = new ParametricScalarImplementation.Parser(header.getName(), method, constructor);
        if (!signatures.containsKey(implementation.getSpecializedSignature())) {
            ParametricScalarImplementation.Builder builder = new ParametricScalarImplementation.Builder(implementation.getSignature(), implementation.getArgumentNativeContainerTypes(), implementation.getSpecializedTypeParameters(), implementation.getReturnNativeContainerType());
            signatures.put(implementation.getSpecializedSignature(), builder);
            builder.addChoice(implementation.getChoice());
        } else {
            ParametricScalarImplementation.Builder builder = signatures.get(implementation.getSpecializedSignature());
            builder.addChoice(implementation.getChoice());
        }
    }
    ParametricImplementationsGroup.Builder<ParametricScalarImplementation> implementationsBuilder = ParametricImplementationsGroup.builder();
    for (ParametricScalarImplementation.Builder implementation : signatures.values()) {
        implementationsBuilder.addImplementation(implementation.build());
    }
    ParametricImplementationsGroup<ParametricScalarImplementation> implementations = implementationsBuilder.build();
    Signature scalarSignature = implementations.getSignature();
    header.getOperatorType().ifPresent(operatorType -> validateOperator(operatorType, scalarSignature.getReturnType(), scalarSignature.getArgumentTypes()));
    return new ParametricScalar(scalarSignature, header.getHeader(), implementations, deprecated);
}
Also used : SpecializedSignature(io.trino.operator.scalar.annotations.ParametricScalarImplementation.SpecializedSignature) HashMap(java.util.HashMap) ParametricImplementationsGroup(io.trino.operator.ParametricImplementationsGroup) Method(java.lang.reflect.Method) SpecializedSignature(io.trino.operator.scalar.annotations.ParametricScalarImplementation.SpecializedSignature) Signature(io.trino.metadata.Signature) ParametricScalar(io.trino.operator.scalar.ParametricScalar)

Example 20 with Signature

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

the class WindowAnnotationsParser method parse.

private static SqlWindowFunction parse(Class<? extends WindowFunction> clazz, WindowFunctionSignature window) {
    List<TypeVariableConstraint> typeVariables = ImmutableList.of();
    if (!window.typeVariable().isEmpty()) {
        typeVariables = ImmutableList.of(typeVariable(window.typeVariable()));
    }
    List<TypeSignature> argumentTypes = Stream.of(window.argumentTypes()).map(type -> parseTypeSignature(type, ImmutableSet.of())).collect(toImmutableList());
    Signature signature = new Signature(window.name(), typeVariables, ImmutableList.of(), parseTypeSignature(window.returnType(), ImmutableSet.of()), argumentTypes, false);
    Optional<String> description = Optional.ofNullable(clazz.getAnnotation(Description.class)).map(Description::value);
    boolean deprecated = clazz.getAnnotationsByType(Deprecated.class).length > 0;
    return new SqlWindowFunction(signature, description, deprecated, new ReflectionWindowFunctionSupplier(window.argumentTypes().length, clazz));
}
Also used : WindowFunctionSignature(io.trino.spi.function.WindowFunctionSignature) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) Description(io.trino.spi.function.Description) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) WindowFunction(io.trino.spi.function.WindowFunction) Signature.typeVariable(io.trino.metadata.Signature.typeVariable) Optional(java.util.Optional) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Description(io.trino.spi.function.Description) WindowFunctionSignature(io.trino.spi.function.WindowFunctionSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint)

Aggregations

Signature (io.trino.metadata.Signature)34 TypeSignature (io.trino.spi.type.TypeSignature)32 BoundSignature (io.trino.metadata.BoundSignature)27 Test (org.testng.annotations.Test)25 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)16 ParametricAggregation (io.trino.operator.aggregation.ParametricAggregation)14 AggregationImplementation (io.trino.operator.aggregation.AggregationImplementation)13 FunctionMetadata (io.trino.metadata.FunctionMetadata)12 ParametricScalar (io.trino.operator.scalar.ParametricScalar)12 AggregationFunctionMetadata (io.trino.metadata.AggregationFunctionMetadata)9 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 TrinoException (io.trino.spi.TrinoException)5 LiteralParameters (io.trino.spi.function.LiteralParameters)5 Objects.requireNonNull (java.util.Objects.requireNonNull)5 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)4 PolymorphicScalarFunctionBuilder (io.trino.metadata.PolymorphicScalarFunctionBuilder)4 SpecializeContext (io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext)4 Signature.longVariableExpression (io.trino.metadata.Signature.longVariableExpression)4 SignatureBuilder (io.trino.metadata.SignatureBuilder)4