use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestAnnotationEngineForAggregates method testPartiallyFixedTypeParameterInjectionAggregateFunctionParse.
@Test
public void testPartiallyFixedTypeParameterInjectionAggregateFunctionParse() {
Signature expectedSignature = new Signature("partially_fixed_type_parameter_injection", ImmutableList.of(typeVariable("T1"), typeVariable("T2")), ImmutableList.of(), DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(new TypeSignature("T1"), new TypeSignature("T2")), false);
ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(PartiallyFixedTypeParameterInjectionAggregateFunction.class));
assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with fixed parameter type injected");
assertTrue(aggregation.getFunctionMetadata().isDeterministic());
assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
assertEquals(aggregation.getStateClass(), NullableDoubleState.class);
ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
assertImplementationCount(implementations, 0, 0, 1);
AggregationImplementation implementationDouble = getOnlyElement(implementations.getGenericImplementations());
assertEquals(implementationDouble.getDefinitionClass(), PartiallyFixedTypeParameterInjectionAggregateFunction.class);
assertDependencyCount(implementationDouble, 1, 1, 1);
assertFalse(implementationDouble.hasSpecializedTypeParameters());
assertEquals(implementationDouble.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL, INPUT_CHANNEL));
BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(DoubleType.DOUBLE, DoubleType.DOUBLE));
specializeAggregationFunction(boundSignature, aggregation);
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestSignatureBinder method testBindUnknown.
@Test
public void testBindUnknown() {
Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x"))).build();
assertThat(function).boundTo(UNKNOWN).fails();
assertThat(function).boundTo(UNKNOWN).withCoercion().succeeds();
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestSignatureBinder method testBindTypeVariablesBasedOnTheSecondArgument.
@Test
public void testBindTypeVariablesBasedOnTheSecondArgument() {
Signature function = functionSignature().returnType(new TypeSignature("T")).argumentTypes(arrayType(new TypeSignature("T")), new TypeSignature("T")).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).build();
assertThat(function).boundTo(UNKNOWN, createDecimalType(2, 1)).withCoercion().produces(new BoundVariables().setTypeVariable("T", createDecimalType(2, 1)));
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestSignatureBinder method testFunction.
@Test
public void testFunction() {
Signature simple = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(functionType(INTEGER.getTypeSignature(), INTEGER.getTypeSignature())).build();
assertThat(simple).boundTo(INTEGER).fails();
assertThat(simple).boundTo(new FunctionType(ImmutableList.of(INTEGER), INTEGER)).succeeds();
// TODO: Support coercion of return type of lambda
assertThat(simple).boundTo(new FunctionType(ImmutableList.of(INTEGER), SMALLINT)).withCoercion().fails();
assertThat(simple).boundTo(new FunctionType(ImmutableList.of(INTEGER), BIGINT)).withCoercion().fails();
Signature applyTwice = functionSignature().returnType(new TypeSignature("V")).argumentTypes(new TypeSignature("T"), functionType(new TypeSignature("T"), new TypeSignature("U")), functionType(new TypeSignature("U"), new TypeSignature("V"))).typeVariableConstraints(typeVariable("T"), typeVariable("U"), typeVariable("V")).build();
assertThat(applyTwice).boundTo(INTEGER, INTEGER, INTEGER).fails();
assertThat(applyTwice).boundTo(INTEGER, new FunctionType(ImmutableList.of(INTEGER), VARCHAR), new FunctionType(ImmutableList.of(VARCHAR), DOUBLE)).produces(new BoundVariables().setTypeVariable("T", INTEGER).setTypeVariable("U", VARCHAR).setTypeVariable("V", DOUBLE));
assertThat(applyTwice).boundTo(INTEGER, new TypeSignatureProvider(functionArgumentTypes -> new FunctionType(ImmutableList.of(INTEGER), VARCHAR).getTypeSignature()), new TypeSignatureProvider(functionArgumentTypes -> new FunctionType(ImmutableList.of(VARCHAR), DOUBLE).getTypeSignature())).produces(new BoundVariables().setTypeVariable("T", INTEGER).setTypeVariable("U", VARCHAR).setTypeVariable("V", DOUBLE));
assertThat(applyTwice).boundTo(// pass function argument to non-function position of a function
new TypeSignatureProvider(functionArgumentTypes -> new FunctionType(ImmutableList.of(INTEGER), VARCHAR).getTypeSignature()), new TypeSignatureProvider(functionArgumentTypes -> new FunctionType(ImmutableList.of(INTEGER), VARCHAR).getTypeSignature()), new TypeSignatureProvider(functionArgumentTypes -> new FunctionType(ImmutableList.of(VARCHAR), DOUBLE).getTypeSignature())).fails();
assertThat(applyTwice).boundTo(new TypeSignatureProvider(functionArgumentTypes -> new FunctionType(ImmutableList.of(INTEGER), VARCHAR).getTypeSignature()), // pass non-function argument to function position of a function
INTEGER, new TypeSignatureProvider(functionArgumentTypes -> new FunctionType(ImmutableList.of(VARCHAR), DOUBLE).getTypeSignature())).fails();
Signature flatMap = functionSignature().returnType(arrayType(new TypeSignature("T"))).argumentTypes(arrayType(new TypeSignature("T")), functionType(new TypeSignature("T"), arrayType(new TypeSignature("T")))).typeVariableConstraints(typeVariable("T")).build();
assertThat(flatMap).boundTo(new ArrayType(INTEGER), new FunctionType(ImmutableList.of(INTEGER), new ArrayType(INTEGER))).produces(new BoundVariables().setTypeVariable("T", INTEGER));
Signature varargApply = functionSignature().returnType(new TypeSignature("T")).argumentTypes(new TypeSignature("T"), functionType(new TypeSignature("T"), new TypeSignature("T"))).typeVariableConstraints(typeVariable("T")).setVariableArity(true).build();
assertThat(varargApply).boundTo(INTEGER, new FunctionType(ImmutableList.of(INTEGER), INTEGER), new FunctionType(ImmutableList.of(INTEGER), INTEGER), new FunctionType(ImmutableList.of(INTEGER), INTEGER)).produces(new BoundVariables().setTypeVariable("T", INTEGER));
assertThat(varargApply).boundTo(INTEGER, new FunctionType(ImmutableList.of(INTEGER), INTEGER), new FunctionType(ImmutableList.of(INTEGER), DOUBLE), new FunctionType(ImmutableList.of(DOUBLE), DOUBLE)).fails();
Signature loop = functionSignature().returnType(new TypeSignature("T")).argumentTypes(new TypeSignature("T"), functionType(new TypeSignature("T"), new TypeSignature("T"))).typeVariableConstraints(typeVariable("T")).build();
assertThat(loop).boundTo(INTEGER, new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, BIGINT).getTypeSignature())).fails();
assertThat(loop).boundTo(INTEGER, new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, BIGINT).getTypeSignature())).withCoercion().produces(new BoundVariables().setTypeVariable("T", BIGINT));
// TODO: Support coercion of return type of lambda
assertThat(loop).withCoercion().boundTo(INTEGER, new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, SMALLINT).getTypeSignature())).fails();
// TODO: Support coercion of return type of lambda
// Without coercion support for return type of lambda, the return type of lambda must be `varchar(x)` to avoid need for coercions.
Signature varcharApply = functionSignature().returnType(VARCHAR.getTypeSignature()).argumentTypes(VARCHAR.getTypeSignature(), functionType(VARCHAR.getTypeSignature(), new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x")))).build();
assertThat(varcharApply).withCoercion().boundTo(createVarcharType(10), new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, createVarcharType(1)).getTypeSignature())).succeeds();
Signature sortByKey = functionSignature().returnType(arrayType(new TypeSignature("T"))).argumentTypes(arrayType(new TypeSignature("T")), functionType(new TypeSignature("T"), new TypeSignature("E"))).typeVariableConstraints(typeVariable("T"), orderableTypeParameter("E")).build();
assertThat(sortByKey).boundTo(new ArrayType(INTEGER), new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, VARCHAR).getTypeSignature())).produces(new BoundVariables().setTypeVariable("T", INTEGER).setTypeVariable("E", VARCHAR));
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestSignatureBinder method testUnknownCoercion.
@Test
public void testUnknownCoercion() {
Signature foo = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(new TypeSignature("T"), new TypeSignature("T")).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).build();
assertThat(foo).boundTo(UNKNOWN, UNKNOWN).produces(new BoundVariables().setTypeVariable("T", UNKNOWN));
assertThat(foo).boundTo(UNKNOWN, BIGINT).withCoercion().produces(new BoundVariables().setTypeVariable("T", BIGINT));
assertThat(foo).boundTo(VARCHAR, BIGINT).withCoercion().fails();
Signature bar = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(new TypeSignature("T"), new TypeSignature("T")).typeVariableConstraints(ImmutableList.of(comparableTypeParameter("T"))).build();
assertThat(bar).boundTo(UNKNOWN, BIGINT).withCoercion().produces(new BoundVariables().setTypeVariable("T", BIGINT));
assertThat(bar).boundTo(VARCHAR, BIGINT).withCoercion().fails();
assertThat(bar).boundTo(HYPER_LOG_LOG, HYPER_LOG_LOG).withCoercion().fails();
}
Aggregations