Search in sources :

Example 1 with TypeParameter

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

the class TestRowParametricType method testTypeSignatureRoundTrip.

@Test
public void testTypeSignatureRoundTrip() {
    TypeSignature typeSignature = new TypeSignature(ROW, TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName("col1")), BIGINT.getTypeSignature())), TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName("col2")), DOUBLE.getTypeSignature())));
    List<TypeParameter> parameters = typeSignature.getParameters().stream().map(parameter -> TypeParameter.of(parameter, TESTING_TYPE_MANAGER)).collect(Collectors.toList());
    Type rowType = RowParametricType.ROW.createType(TESTING_TYPE_MANAGER, parameters);
    assertEquals(rowType.getTypeSignature(), typeSignature);
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) ROW(io.trino.spi.type.StandardTypes.ROW) Type(io.trino.spi.type.Type) TESTING_TYPE_MANAGER(io.trino.type.InternalTypeManager.TESTING_TYPE_MANAGER) Test(org.testng.annotations.Test) TypeParameter(io.trino.spi.type.TypeParameter) Collectors(java.util.stream.Collectors) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) RowFieldName(io.trino.spi.type.RowFieldName) Optional(java.util.Optional) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) Assert.assertEquals(io.trino.testing.assertions.Assert.assertEquals) TypeSignature(io.trino.spi.type.TypeSignature) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeParameter(io.trino.spi.type.TypeParameter) Type(io.trino.spi.type.Type) RowFieldName(io.trino.spi.type.RowFieldName) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) Test(org.testng.annotations.Test)

Example 2 with TypeParameter

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

the class VarcharParametricType method createType.

@Override
public Type createType(TypeManager typeManager, List<TypeParameter> parameters) {
    if (parameters.isEmpty()) {
        return createUnboundedVarcharType();
    }
    if (parameters.size() != 1) {
        throw new IllegalArgumentException("Expected exactly one parameter for VARCHAR");
    }
    TypeParameter parameter = parameters.get(0);
    if (!parameter.isLongLiteral()) {
        throw new IllegalArgumentException("VARCHAR length must be a number");
    }
    long length = parameter.getLongLiteral();
    if (length == VarcharType.UNBOUNDED_LENGTH) {
        return VarcharType.createUnboundedVarcharType();
    }
    if (length < 0 || length > VarcharType.MAX_LENGTH) {
        throw new IllegalArgumentException("Invalid VARCHAR length " + length);
    }
    return VarcharType.createVarcharType((int) length);
}
Also used : TypeParameter(io.trino.spi.type.TypeParameter)

Example 3 with TypeParameter

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

the class TypeRegistry method instantiateParametricType.

private Type instantiateParametricType(TypeSignature signature) {
    List<TypeParameter> parameters = new ArrayList<>();
    for (TypeSignatureParameter parameter : signature.getParameters()) {
        TypeParameter typeParameter = TypeParameter.of(parameter, typeManager);
        parameters.add(typeParameter);
    }
    ParametricType parametricType = parametricTypes.get(signature.getBase().toLowerCase(Locale.ENGLISH));
    if (parametricType == null) {
        throw new TypeNotFoundException(signature);
    }
    Type instantiatedType;
    try {
        instantiatedType = parametricType.createType(typeManager, parameters);
    } catch (IllegalArgumentException e) {
        throw new TypeNotFoundException(signature, e);
    }
    // checkState(instantiatedType.equalsSignature(signature), "Instantiated parametric type name (%s) does not match expected name (%s)", instantiatedType, signature);
    return instantiatedType;
}
Also used : TypeParameter(io.trino.spi.type.TypeParameter) DecimalParametricType(io.trino.type.DecimalParametricType) VarcharParametricType(io.trino.type.VarcharParametricType) Re2JRegexpType(io.trino.type.Re2JRegexpType) ParametricType(io.trino.spi.type.ParametricType) Type(io.trino.spi.type.Type) CharParametricType(io.trino.type.CharParametricType) OperatorType(io.trino.spi.function.OperatorType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TypeNotFoundException(io.trino.spi.type.TypeNotFoundException) ArrayList(java.util.ArrayList) DecimalParametricType(io.trino.type.DecimalParametricType) VarcharParametricType(io.trino.type.VarcharParametricType) ParametricType(io.trino.spi.type.ParametricType) CharParametricType(io.trino.type.CharParametricType)

Example 4 with TypeParameter

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

the class MapParametricType method createType.

@Override
public Type createType(TypeManager typeManager, List<TypeParameter> parameters) {
    checkArgument(parameters.size() == 2, "Expected two parameters, got %s", parameters);
    TypeParameter firstParameter = parameters.get(0);
    TypeParameter secondParameter = parameters.get(1);
    checkArgument(firstParameter.getKind() == ParameterKind.TYPE && secondParameter.getKind() == ParameterKind.TYPE, "Expected key and type to be types, got %s", parameters);
    return new MapType(firstParameter.getType(), secondParameter.getType(), typeManager.getTypeOperators());
}
Also used : TypeParameter(io.trino.spi.type.TypeParameter) MapType(io.trino.spi.type.MapType)

Example 5 with TypeParameter

use of io.trino.spi.type.TypeParameter 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)

Aggregations

TypeParameter (io.trino.spi.type.TypeParameter)5 Type (io.trino.spi.type.Type)3 TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)3 NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)2 ParametricType (io.trino.spi.type.ParametricType)2 RowFieldName (io.trino.spi.type.RowFieldName)2 TypeSignature (io.trino.spi.type.TypeSignature)2 List (java.util.List)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 OperatorType (io.trino.spi.function.OperatorType)1 BIGINT (io.trino.spi.type.BigintType.BIGINT)1 DOUBLE (io.trino.spi.type.DoubleType.DOUBLE)1 MapType (io.trino.spi.type.MapType)1 ParameterKind (io.trino.spi.type.ParameterKind)1 RowType (io.trino.spi.type.RowType)1 StandardTypes (io.trino.spi.type.StandardTypes)1 ROW (io.trino.spi.type.StandardTypes.ROW)1 TypeManager (io.trino.spi.type.TypeManager)1 TypeNotFoundException (io.trino.spi.type.TypeNotFoundException)1 Assert.assertEquals (io.trino.testing.assertions.Assert.assertEquals)1