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