Search in sources :

Example 31 with TypeSignature

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));
}
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 32 with TypeSignature

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));
}
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 33 with TypeSignature

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();
}
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 34 with TypeSignature

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));
}
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 35 with TypeSignature

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);
}
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)

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