Search in sources :

Example 66 with TypeSignature

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

the class TestTypeSignature method assertRowSignature.

private static void assertRowSignature(String typeName, Set<String> literalParameters, TypeSignature expectedSignature) {
    TypeSignature signature = parseTypeSignature(typeName, literalParameters);
    assertEquals(signature, expectedSignature);
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature)

Example 67 with TypeSignature

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

the class TestTypeSignature method parseSignatureWithLiterals.

@Test
public void parseSignatureWithLiterals() {
    TypeSignature result = new TypeSignature("decimal", typeVariable("X"), numericParameter(42));
    assertEquals(result.getParameters().size(), 2);
    assertEquals(result.getParameters().get(0).isVariable(), true);
    assertEquals(result.getParameters().get(1).isLongLiteral(), true);
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 68 with TypeSignature

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

the class DecimalOperators method modulusSignatureBuilder.

public static SignatureBuilder modulusSignatureBuilder() {
    TypeSignature decimalLeftSignature = new TypeSignature("decimal", typeVariable("a_precision"), typeVariable("a_scale"));
    TypeSignature decimalRightSignature = new TypeSignature("decimal", typeVariable("b_precision"), typeVariable("b_scale"));
    TypeSignature decimalResultSignature = new TypeSignature("decimal", typeVariable("r_precision"), typeVariable("r_scale"));
    return Signature.builder().longVariableConstraints(longVariableExpression("r_precision", "min(b_precision - b_scale, a_precision - a_scale) + max(a_scale, b_scale)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature);
}
Also used : TypeSignature(io.trino.spi.type.TypeSignature)

Example 69 with TypeSignature

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

the class RowParametricType method createType.

@Override
public Type createType(TypeManager typeManager, List<TypeParameter> parameters) {
    checkArgument(!parameters.isEmpty(), "Row type must have at least one parameter");
    checkArgument(parameters.stream().allMatch(parameter -> parameter.getKind() == ParameterKind.NAMED_TYPE), "Expected only named types as a parameters, got %s", parameters);
    List<TypeSignatureParameter> typeSignatureParameters = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(parameter.getName(), parameter.getType().getTypeSignature()))).collect(toList());
    List<RowType.Field> fields = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> new RowType.Field(parameter.getName().map(RowFieldName::getName), parameter.getType())).collect(toList());
    return RowType.createWithTypeSignature(new TypeSignature(StandardTypes.ROW, typeSignatureParameters), fields);
}
Also used : RowType(io.trino.spi.type.RowType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) Type(io.trino.spi.type.Type) StandardTypes(io.trino.spi.type.StandardTypes) TypeParameter(io.trino.spi.type.TypeParameter) ParameterKind(io.trino.spi.type.ParameterKind) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Collectors.toList(java.util.stream.Collectors.toList) ParametricType(io.trino.spi.type.ParametricType) RowFieldName(io.trino.spi.type.RowFieldName) TypeManager(io.trino.spi.type.TypeManager) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TypeSignature(io.trino.spi.type.TypeSignature) TypeParameter(io.trino.spi.type.TypeParameter) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature)

Example 70 with TypeSignature

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

the class TestFixJsonDataUtils method testFixData.

@Test
public void testFixData() {
    assertQueryResult(BIGINT.getTypeSignature(), 1000, 1000L);
    assertQueryResult(INTEGER.getTypeSignature(), 100, 100);
    assertQueryResult(SMALLINT.getTypeSignature(), 10, (short) 10);
    assertQueryResult(TINYINT.getTypeSignature(), 1, (byte) 1);
    assertQueryResult(BOOLEAN.getTypeSignature(), true, true);
    assertQueryResult(DATE.getTypeSignature(), "2017-07-01", "2017-07-01");
    assertQueryResult(createDecimalType().getTypeSignature(), "2.15", "2.15");
    assertQueryResult(REAL.getTypeSignature(), 100.23456, (float) 100.23456);
    assertQueryResult(DOUBLE.getTypeSignature(), 100.23456D, 100.23456);
    assertQueryResult(new TypeSignature(INTERVAL_DAY_TO_SECOND), "INTERVAL '2' DAY", "INTERVAL '2' DAY");
    assertQueryResult(new TypeSignature(INTERVAL_YEAR_TO_MONTH), "INTERVAL '3' MONTH", "INTERVAL '3' MONTH");
    assertQueryResult(TIMESTAMP_MILLIS.getTypeSignature(), "2001-08-22 03:04:05.321", "2001-08-22 03:04:05.321");
    assertQueryResult(TIMESTAMP_WITH_TIME_ZONE.getTypeSignature(), "2001-08-22 03:04:05.321 America/Los_Angeles", "2001-08-22 03:04:05.321 America/Los_Angeles");
    assertQueryResult(TIME.getTypeSignature(), "01:02:03.456", "01:02:03.456");
    assertQueryResult(TIMESTAMP_WITH_TIME_ZONE.getTypeSignature(), "01:02:03.456 America/Los_Angeles", "01:02:03.456 America/Los_Angeles");
    assertQueryResult(VARBINARY.getTypeSignature(), "garbage", Base64.getDecoder().decode("garbage"));
    assertQueryResult(VARCHAR.getTypeSignature(), "teststring", "teststring");
    assertQueryResult(createCharType(3).getTypeSignature(), "abc", "abc");
    assertQueryResult(RowType.from(ImmutableList.of(field("foo", BIGINT), field("bar", BIGINT))).getTypeSignature(), ImmutableList.of(1, 2), Row.builder().addField("foo", 1L).addField("bar", 2L).build());
    assertQueryResult(arrayType(BIGINT.getTypeSignature()), ImmutableList.of(1, 2, 4), ImmutableList.of(1L, 2L, 4L));
    assertQueryResult(mapType(BIGINT.getTypeSignature(), BIGINT.getTypeSignature()), ImmutableMap.of(1, 3, 2, 4), ImmutableMap.of(1L, 3L, 2L, 4L));
    assertQueryResult(new TypeSignature(JSON), "{\"json\": {\"a\": 1}}", "{\"json\": {\"a\": 1}}");
    assertQueryResult(new TypeSignature(IPADDRESS), "1.2.3.4", "1.2.3.4");
    assertQueryResult(new TypeSignature(UUID), "0397e63b-2b78-4b7b-9c87-e085fa225dd8", "0397e63b-2b78-4b7b-9c87-e085fa225dd8");
    assertQueryResult(new TypeSignature("Geometry"), "POINT (1.2 3.4)", "POINT (1.2 3.4)");
    assertQueryResult(mapType(new TypeSignature("BingTile"), BIGINT.getTypeSignature()), ImmutableMap.of("BingTile{x=1, y=2, zoom_level=10}", 1), ImmutableMap.of("BingTile{x=1, y=2, zoom_level=10}", 1L));
}
Also used : 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