Search in sources :

Example 51 with TypeSignature

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

the class TestSignatureBinder method testBindLiteralForVarchar.

@Test
public void testBindLiteralForVarchar() {
    TypeSignature leftType = new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x"));
    TypeSignature rightType = new TypeSignature("varchar", TypeSignatureParameter.typeVariable("y"));
    Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(leftType, rightType).build();
    assertThat(function).boundTo(createVarcharType(42), createVarcharType(44)).produces(new BoundVariables().setLongVariable("x", 42L).setLongVariable("y", 44L));
    assertThat(function).boundTo(UNKNOWN, createVarcharType(44)).withCoercion().produces(new BoundVariables().setLongVariable("x", 0L).setLongVariable("y", 44L));
}
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 52 with TypeSignature

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

the class TestSignatureBinder method testRow.

@Test
public void testRow() {
    Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(rowType(anonymousField(INTEGER.getTypeSignature()))).build();
    assertThat(function).boundTo(RowType.anonymous(ImmutableList.of(TINYINT))).withCoercion().produces(NO_BOUND_VARIABLES);
    assertThat(function).boundTo(RowType.anonymous(ImmutableList.of(INTEGER))).withCoercion().produces(NO_BOUND_VARIABLES);
    assertThat(function).boundTo(RowType.anonymous(ImmutableList.of(BIGINT))).withCoercion().fails();
    Signature biFunction = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(rowType(anonymousField(new TypeSignature("T"))), rowType(anonymousField(new TypeSignature("T")))).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).build();
    assertThat(biFunction).boundTo(RowType.anonymous(ImmutableList.of(INTEGER)), RowType.anonymous(ImmutableList.of(BIGINT))).withCoercion().produces(new BoundVariables().setTypeVariable("T", BIGINT));
    assertThat(biFunction).boundTo(RowType.anonymous(ImmutableList.of(INTEGER)), RowType.anonymous(ImmutableList.of(BIGINT))).withCoercion().produces(new BoundVariables().setTypeVariable("T", BIGINT));
}
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 53 with TypeSignature

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

the class TestSignatureBinder method testBindVarcharTemplateStyle.

@Test
public void testBindVarcharTemplateStyle() {
    Signature function = functionSignature().returnType(new TypeSignature("T2")).argumentTypes(new TypeSignature("T1")).typeVariableConstraints(ImmutableList.of(new TypeVariableConstraint("T1", true, false, null, ImmutableSet.of(), ImmutableSet.of()), new TypeVariableConstraint("T2", true, false, null, ImmutableSet.of(), ImmutableSet.of()))).build();
    assertThat(function).boundTo(ImmutableList.of(createVarcharType(42)), createVarcharType(1)).produces(new BoundVariables().setTypeVariable("T1", createVarcharType(42)).setTypeVariable("T2", createVarcharType(1)));
}
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 54 with TypeSignature

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

the class TestSignatureBinder method testCanCoerceFrom.

@Test
public void testCanCoerceFrom() {
    Signature arrayJoin = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(arrayType(new TypeSignature("E")), JSON.getTypeSignature()).typeVariableConstraints(castableFromTypeParameter("E", JSON.getTypeSignature())).build();
    assertThat(arrayJoin).boundTo(new ArrayType(INTEGER), JSON).produces(new BoundVariables().setTypeVariable("E", INTEGER));
    assertThat(arrayJoin).boundTo(new ArrayType(VARBINARY)).fails();
    Signature multiCast = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(arrayType(new TypeSignature("E")), JSON.getTypeSignature()).typeVariableConstraints(castableFromTypeParameter("E", VARCHAR.getTypeSignature(), JSON.getTypeSignature())).build();
    assertThat(multiCast).boundTo(new ArrayType(TINYINT), JSON).produces(new BoundVariables().setTypeVariable("E", TINYINT));
    assertThat(multiCast).boundTo(new ArrayType(TIMESTAMP_MILLIS), JSON).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 55 with TypeSignature

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

the class TestSignatureBinder method testVarArgs.

@Test
public void testVarArgs() {
    Signature variableArityFunction = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(new TypeSignature("T")).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).setVariableArity(true).build();
    assertThat(variableArityFunction).boundTo(BIGINT).produces(new BoundVariables().setTypeVariable("T", BIGINT));
    assertThat(variableArityFunction).boundTo(VARCHAR).produces(new BoundVariables().setTypeVariable("T", VARCHAR));
    assertThat(variableArityFunction).boundTo(BIGINT, BIGINT).produces(new BoundVariables().setTypeVariable("T", BIGINT));
    assertThat(variableArityFunction).boundTo(BIGINT, 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)

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