Search in sources :

Example 26 with TypeSignature

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

the class TestSignatureBinder method testBindParametricTypeParameterToUnknown.

@Test
public void testBindParametricTypeParameterToUnknown() {
    Signature function = functionSignature().returnType(new TypeSignature("T")).argumentTypes(arrayType(new TypeSignature("T"))).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).build();
    assertThat(function).boundTo(UNKNOWN).fails();
    assertThat(function).withCoercion().boundTo(UNKNOWN).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 27 with TypeSignature

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

the class TestSignatureBinder method testBindMixedLiteralAndTypeVariables.

@Test
public void testBindMixedLiteralAndTypeVariables() {
    Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).argumentTypes(arrayType(new TypeSignature("T")), arrayType(new TypeSignature("decimal", TypeSignatureParameter.typeVariable("p"), TypeSignatureParameter.typeVariable("s")))).build();
    assertThat(function).boundTo(new ArrayType(createDecimalType(2, 1)), new ArrayType(createDecimalType(3, 1))).withCoercion().produces(new BoundVariables().setTypeVariable("T", createDecimalType(2, 1)).setLongVariable("p", 3L).setLongVariable("s", 1L));
}
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 28 with TypeSignature

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

the class TestPolymorphicScalarFunction method testTypeParameters.

@Test
public void testTypeParameters() throws Throwable {
    Signature signature = Signature.builder().name("foo").typeVariableConstraints(comparableWithVariadicBound("V", "ROW")).returnType(new TypeSignature("V")).argumentTypes(new TypeSignature("V")).build();
    SqlScalarFunction function = new PolymorphicScalarFunctionBuilder(TestMethods.class).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("varcharToVarchar"))).build();
    BoundSignature boundSignature = new BoundSignature(signature.getName(), VARCHAR, ImmutableList.of(VARCHAR));
    ChoicesScalarFunctionImplementation functionImplementation = (ChoicesScalarFunctionImplementation) function.specialize(boundSignature, new FunctionDependencies(FUNCTION_MANAGER::getScalarFunctionInvoker, ImmutableMap.of(), ImmutableSet.of()));
    Slice slice = (Slice) functionImplementation.getChoices().get(0).getMethodHandle().invoke(INPUT_SLICE);
    assertEquals(slice, VARCHAR_TO_VARCHAR_RETURN_VALUE);
}
Also used : VARCHAR_TO_VARCHAR_RETURN_VALUE(io.trino.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE) BLOCK_POSITION(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.BLOCK_POSITION) Slice(io.airlift.slice.Slice) FAIL_ON_NULL(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) MAX_SHORT_PRECISION(io.trino.spi.type.Decimals.MAX_SHORT_PRECISION) TypeSignatureParameter.typeVariable(io.trino.spi.type.TypeSignatureParameter.typeVariable) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) Assert.assertEquals(org.testng.Assert.assertEquals) ChoicesScalarFunctionImplementation(io.trino.operator.scalar.ChoicesScalarFunctionImplementation) Test(org.testng.annotations.Test) IS_DISTINCT_FROM(io.trino.spi.function.OperatorType.IS_DISTINCT_FROM) FunctionManager.createTestingFunctionManager(io.trino.metadata.FunctionManager.createTestingFunctionManager) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) NULL_FLAG(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NULL_FLAG) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Block(io.trino.spi.block.Block) Arrays.asList(java.util.Arrays.asList) Slices(io.airlift.slice.Slices) LongArrayBlock(io.trino.spi.block.LongArrayBlock) Assert.assertFalse(org.testng.Assert.assertFalse) TypeSignature(io.trino.spi.type.TypeSignature) Int128(io.trino.spi.type.Int128) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Signature.comparableWithVariadicBound(io.trino.metadata.Signature.comparableWithVariadicBound) VARCHAR_TO_BIGINT_RETURN_VALUE(io.trino.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE) InvocationConvention(io.trino.spi.function.InvocationConvention) ADD(io.trino.spi.function.OperatorType.ADD) BIGINT(io.trino.spi.type.BigintType.BIGINT) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) DecimalType(io.trino.spi.type.DecimalType) TypeSignature(io.trino.spi.type.TypeSignature) Slice(io.airlift.slice.Slice) TypeSignature(io.trino.spi.type.TypeSignature) ChoicesScalarFunctionImplementation(io.trino.operator.scalar.ChoicesScalarFunctionImplementation) Test(org.testng.annotations.Test)

Example 29 with TypeSignature

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

the class TestSignatureBinder method testBasic.

@Test
public void testBasic() {
    Signature function = functionSignature().typeVariableConstraints(ImmutableList.of(typeVariable("T"))).returnType(new TypeSignature("T")).argumentTypes(new TypeSignature("T")).build();
    assertThat(function).boundTo(BIGINT).produces(new BoundVariables().setTypeVariable("T", BIGINT));
    assertThat(function).boundTo(VARCHAR).produces(new BoundVariables().setTypeVariable("T", VARCHAR));
    assertThat(function).boundTo(VARCHAR, BIGINT).fails();
    assertThat(function).boundTo(new ArrayType(BIGINT)).produces(new BoundVariables().setTypeVariable("T", new ArrayType(BIGINT)));
}
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 30 with TypeSignature

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

the class TestSignatureBinder method testVariadic.

@Test
public void testVariadic() {
    Signature rowVariadicBoundFunction = functionSignature().returnType(BIGINT.getTypeSignature()).argumentTypes(new TypeSignature("T")).typeVariableConstraints(ImmutableList.of(withVariadicBound("T", "row"))).build();
    assertThat(rowVariadicBoundFunction).boundTo(RowType.anonymous(ImmutableList.of(BIGINT, BIGINT))).produces(new BoundVariables().setTypeVariable("T", RowType.anonymous(ImmutableList.of(BIGINT, BIGINT))));
    assertThat(rowVariadicBoundFunction).boundTo(new ArrayType(BIGINT)).fails();
    assertThat(rowVariadicBoundFunction).boundTo(new ArrayType(BIGINT)).withCoercion().fails();
    assertThatThrownBy(() -> withVariadicBound("T", "array")).isInstanceOf(IllegalArgumentException.class).hasMessage("variadicBound must be row but is array");
    assertThatThrownBy(() -> withVariadicBound("T", "map")).isInstanceOf(IllegalArgumentException.class).hasMessage("variadicBound must be row but is map");
    assertThatThrownBy(() -> withVariadicBound("T", "decimal")).isInstanceOf(IllegalArgumentException.class).hasMessage("variadicBound must be row but is decimal");
}
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)

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