Search in sources :

Example 46 with TypeSignature

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);
}
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) Test(org.testng.annotations.Test)

Example 47 with TypeSignature

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

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

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));
}
Also used : TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) ArrayType(io.trino.spi.type.ArrayType) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) UNKNOWN(io.trino.type.UnknownType.UNKNOWN) Test(org.testng.annotations.Test) TypeSignatureParameter.anonymousField(io.trino.spi.type.TypeSignatureParameter.anonymousField) FunctionType(io.trino.type.FunctionType) TEST_SESSION(io.trino.SessionTestUtils.TEST_SESSION) Signature.castableFromTypeParameter(io.trino.metadata.Signature.castableFromTypeParameter) INTEGER(io.trino.spi.type.IntegerType.INTEGER) Assert.assertFalse(org.testng.Assert.assertFalse) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) TypeSignature(io.trino.spi.type.TypeSignature) RowType(io.trino.spi.type.RowType) ImmutableSet(com.google.common.collect.ImmutableSet) Signature.castableToTypeParameter(io.trino.metadata.Signature.castableToTypeParameter) ArrayType(io.trino.spi.type.ArrayType) Assert.assertNotNull(org.testng.Assert.assertNotNull) String.format(java.lang.String.format) Signature.orderableTypeParameter(io.trino.metadata.Signature.orderableTypeParameter) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) Signature.typeVariable(io.trino.metadata.Signature.typeVariable) Optional(java.util.Optional) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) Signature.comparableTypeParameter(io.trino.metadata.Signature.comparableTypeParameter) TIMESTAMP_MILLIS(io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS) Type(io.trino.spi.type.Type) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) Assert.assertEquals(org.testng.Assert.assertEquals) HYPER_LOG_LOG(io.trino.spi.type.HyperLogLogType.HYPER_LOG_LOG) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Objects.requireNonNull(java.util.Objects.requireNonNull) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) TypeSignature.functionType(io.trino.spi.type.TypeSignature.functionType) TypeSignatureParameter.numericParameter(io.trino.spi.type.TypeSignatureParameter.numericParameter) TypeSignature.arrayType(io.trino.spi.type.TypeSignature.arrayType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) Assert.fail(org.testng.Assert.fail) Signature.withVariadicBound(io.trino.metadata.Signature.withVariadicBound) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) TypeSignature.rowType(io.trino.spi.type.TypeSignature.rowType) PLANNER_CONTEXT(io.trino.sql.planner.TestingPlannerContext.PLANNER_CONTEXT) TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) Assert.assertTrue(org.testng.Assert.assertTrue) TINYINT(io.trino.spi.type.TinyintType.TINYINT) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) JSON(io.trino.type.JsonType.JSON) 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) FunctionType(io.trino.type.FunctionType) Test(org.testng.annotations.Test)

Example 50 with TypeSignature

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