Search in sources :

Example 21 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class TestSignatureBinder method testBindUnknownToDecimal.

@Test
public void testBindUnknownToDecimal() {
    Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(new TypeSignature("decimal", TypeSignatureParameter.typeVariable("p"), TypeSignatureParameter.typeVariable("s"))).build();
    assertThat(function).boundTo(UNKNOWN).withCoercion().produces(new BoundVariables().setLongVariable("p", 1L).setLongVariable("s", 0L));
}
Also used : TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 22 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class TestSignatureBinder method testBindLiteralForRepeatedVarchar.

@Test
public void testBindLiteralForRepeatedVarchar() {
    TypeSignature leftType = new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x"));
    TypeSignature rightType = new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x"));
    TypeSignature returnType = new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x"));
    Signature function = functionSignature().returnType(returnType).argumentTypes(leftType, rightType).build();
    assertThat(function).withCoercion().boundTo(ImmutableList.of(createVarcharType(3), createVarcharType(5)), createVarcharType(5)).produces(new BoundVariables().setLongVariable("x", 5L));
    assertThat(function).withCoercion().boundTo(ImmutableList.of(createVarcharType(3), createVarcharType(5)), createVarcharType(6)).produces(new BoundVariables().setLongVariable("x", 6L));
}
Also used : TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 23 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class TestSignatureBinder method testCanCoerceTo.

@Test
public void testCanCoerceTo() {
    Signature arrayJoin = functionSignature().returnType(VARCHAR.getTypeSignature()).argumentTypes(arrayType(new TypeSignature("E"))).typeVariableConstraints(castableToTypeParameter("E", VARCHAR.getTypeSignature())).build();
    assertThat(arrayJoin).boundTo(new ArrayType(INTEGER)).produces(new BoundVariables().setTypeVariable("E", INTEGER));
    assertThat(arrayJoin).boundTo(new ArrayType(VARBINARY)).fails();
    Signature castArray = functionSignature().returnType(arrayType(new TypeSignature("T"))).argumentTypes(arrayType(new TypeSignature("F"))).typeVariableConstraints(typeVariable("T"), castableToTypeParameter("F", new TypeSignature("T"))).build();
    assertThat(castArray).boundTo(ImmutableList.of(new ArrayType(INTEGER)), new ArrayType(VARCHAR)).produces(new BoundVariables().setTypeVariable("F", INTEGER).setTypeVariable("T", VARCHAR));
    assertThat(castArray).boundTo(new ArrayType(INTEGER), new ArrayType(TIMESTAMP_MILLIS)).fails();
    Signature multiCast = functionSignature().returnType(VARCHAR.getTypeSignature()).argumentTypes(arrayType(new TypeSignature("E"))).typeVariableConstraints(castableToTypeParameter("E", VARCHAR.getTypeSignature(), INTEGER.getTypeSignature())).build();
    assertThat(multiCast).boundTo(new ArrayType(TINYINT)).produces(new BoundVariables().setTypeVariable("E", TINYINT));
    assertThat(multiCast).boundTo(new ArrayType(TIMESTAMP_MILLIS)).fails();
}
Also used : ArrayType(io.trino.spi.type.ArrayType) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 24 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class TestSignatureBinder method testMap.

@Test
public void testMap() {
    Signature getValueFunction = functionSignature().returnType(new TypeSignature("V")).argumentTypes(mapType(new TypeSignature("K"), new TypeSignature("V")), new TypeSignature("K")).typeVariableConstraints(ImmutableList.of(typeVariable("K"), typeVariable("V"))).build();
    assertThat(getValueFunction).boundTo(type(mapType(BIGINT.getTypeSignature(), VARCHAR.getTypeSignature())), BIGINT).produces(new BoundVariables().setTypeVariable("K", BIGINT).setTypeVariable("V", VARCHAR));
    assertThat(getValueFunction).boundTo(type(mapType(BIGINT.getTypeSignature(), VARCHAR.getTypeSignature())), VARCHAR).withCoercion().fails();
}
Also used : TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 25 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class TestSignatureBinder method testNoVariableReuseAcrossTypes.

@Test
public void testNoVariableReuseAcrossTypes() {
    TypeSignature leftType = new TypeSignature("decimal", TypeSignatureParameter.typeVariable("p1"), TypeSignatureParameter.typeVariable("s"));
    TypeSignature rightType = new TypeSignature("decimal", TypeSignatureParameter.typeVariable("p2"), TypeSignatureParameter.typeVariable("s"));
    Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(leftType, rightType).build();
    assertThatThrownBy(() -> assertThat(function).boundTo(createDecimalType(2, 1), createDecimalType(3, 1)).produces(NO_BOUND_VARIABLES)).isInstanceOf(UnsupportedOperationException.class).hasMessage("Literal parameters may not be shared across different types");
}
Also used : TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Aggregations

TypeSignature (io.trino.spi.type.TypeSignature)78 Test (org.testng.annotations.Test)49 TypeSignatureTranslator.parseTypeSignature (io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature)35 ImmutableList (com.google.common.collect.ImmutableList)19 Signature (io.trino.metadata.Signature)17 NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)17 Type (io.trino.spi.type.Type)15 ArrayType (io.trino.spi.type.ArrayType)14 List (java.util.List)13 BoundSignature (io.trino.metadata.BoundSignature)11 Optional (java.util.Optional)10 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 ImmutableSet (com.google.common.collect.ImmutableSet)9 TrinoException (io.trino.spi.TrinoException)9 DecimalType (io.trino.spi.type.DecimalType)9 TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)9 Objects.requireNonNull (java.util.Objects.requireNonNull)9 BIGINT (io.trino.spi.type.BigintType.BIGINT)8 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)7 ADD (io.trino.spi.function.OperatorType.ADD)7