use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestSignatureBinder method testBindLiteralForDecimal.
@Test
public void testBindLiteralForDecimal() {
TypeSignature leftType = new TypeSignature("decimal", TypeSignatureParameter.typeVariable("p1"), TypeSignatureParameter.typeVariable("s1"));
TypeSignature rightType = new TypeSignature("decimal", TypeSignatureParameter.typeVariable("p2"), TypeSignatureParameter.typeVariable("s2"));
Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(leftType, rightType).build();
assertThat(function).boundTo(createDecimalType(2, 1), createDecimalType(1, 0)).produces(new BoundVariables().setLongVariable("p1", 2L).setLongVariable("s1", 1L).setLongVariable("p2", 1L).setLongVariable("s2", 0L));
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestSignatureBinder method testBindPartialDecimal.
@Test
public void testBindPartialDecimal() {
Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(new TypeSignature("decimal", numericParameter(4), TypeSignatureParameter.typeVariable("s"))).build();
assertThat(function).boundTo(createDecimalType(2, 1)).withCoercion().produces(new BoundVariables().setLongVariable("s", 1L));
function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(new TypeSignature("decimal", TypeSignatureParameter.typeVariable("p"), numericParameter(1))).build();
assertThat(function).boundTo(createDecimalType(2, 0)).withCoercion().produces(new BoundVariables().setLongVariable("p", 3L));
assertThat(function).boundTo(createDecimalType(2, 1)).produces(new BoundVariables().setLongVariable("p", 2L));
assertThat(function).boundTo(BIGINT).withCoercion().produces(new BoundVariables().setLongVariable("p", 20L));
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestSignatureBinder method testArray.
@Test
public void testArray() {
Signature getFunction = functionSignature().returnType(new TypeSignature("T")).argumentTypes(arrayType(new TypeSignature("T"))).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).build();
assertThat(getFunction).boundTo(new ArrayType(BIGINT)).produces(new BoundVariables().setTypeVariable("T", BIGINT));
assertThat(getFunction).boundTo(BIGINT).withCoercion().fails();
assertThat(getFunction).boundTo(RowType.anonymous(ImmutableList.of(BIGINT))).withCoercion().fails();
Signature containsFunction = functionSignature().returnType(new TypeSignature("T")).argumentTypes(arrayType(new TypeSignature("T")), new TypeSignature("T")).typeVariableConstraints(ImmutableList.of(comparableTypeParameter("T"))).build();
assertThat(containsFunction).boundTo(new ArrayType(BIGINT), BIGINT).produces(new BoundVariables().setTypeVariable("T", BIGINT));
assertThat(containsFunction).boundTo(new ArrayType(BIGINT), VARCHAR).withCoercion().fails();
assertThat(containsFunction).boundTo(new ArrayType(HYPER_LOG_LOG), HYPER_LOG_LOG).withCoercion().fails();
Signature castFunction = functionSignature().returnType(arrayType(new TypeSignature("T2"))).argumentTypes(arrayType(new TypeSignature("T1")), arrayType(new TypeSignature("T2"))).typeVariableConstraints(ImmutableList.of(typeVariable("T1"), typeVariable("T2"))).build();
assertThat(castFunction).boundTo(new ArrayType(UNKNOWN), new ArrayType(createDecimalType(2, 1))).withCoercion().produces(new BoundVariables().setTypeVariable("T1", UNKNOWN).setTypeVariable("T2", createDecimalType(2, 1)));
Signature fooFunction = functionSignature().returnType(new TypeSignature("T")).argumentTypes(arrayType(new TypeSignature("T")), arrayType(new TypeSignature("T"))).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).build();
assertThat(fooFunction).boundTo(new ArrayType(BIGINT), new ArrayType(BIGINT)).produces(new BoundVariables().setTypeVariable("T", BIGINT));
assertThat(fooFunction).boundTo(new ArrayType(BIGINT), new ArrayType(VARCHAR)).withCoercion().fails();
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestSignatureBinder method testBindLiteralForRepeatedVarcharWithReturn.
@Test
public void testBindLiteralForRepeatedVarcharWithReturn() {
TypeSignature leftType = new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x"));
TypeSignature rightType = new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x"));
Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(leftType, rightType).build();
assertThat(function).boundTo(createVarcharType(44), createVarcharType(44)).produces(new BoundVariables().setLongVariable("x", 44L));
assertThat(function).boundTo(createVarcharType(44), createVarcharType(42)).withCoercion().produces(new BoundVariables().setLongVariable("x", 44L));
assertThat(function).boundTo(createVarcharType(42), createVarcharType(44)).withCoercion().produces(new BoundVariables().setLongVariable("x", 44L));
assertThat(function).boundTo(UNKNOWN, createVarcharType(44)).withCoercion().produces(new BoundVariables().setLongVariable("x", 44L));
}
use of io.trino.spi.type.TypeSignature 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);
}
Aggregations