Search in sources :

Example 1 with DataTypeParameter

use of io.trino.sql.tree.DataTypeParameter in project trino by trinodb.

the class TypeSignatureTranslator method toTypeSignature.

private static TypeSignature toTypeSignature(GenericDataType type, Set<String> typeVariables) {
    ImmutableList.Builder<TypeSignatureParameter> parameters = ImmutableList.builder();
    if (type.getName().getValue().equalsIgnoreCase(StandardTypes.VARCHAR) && type.getArguments().isEmpty()) {
        // TODO: Eventually, we should split the types into VARCHAR and VARCHAR(n)
        return VarcharType.VARCHAR.getTypeSignature();
    }
    checkArgument(!typeVariables.contains(type.getName().getValue()), "Base type name cannot be a type variable");
    for (DataTypeParameter parameter : type.getArguments()) {
        if (parameter instanceof NumericParameter) {
            String value = ((NumericParameter) parameter).getValue();
            try {
                parameters.add(numericParameter(Long.parseLong(value)));
            } catch (NumberFormatException e) {
                throw semanticException(TYPE_MISMATCH, parameter, "Invalid type parameter: %s", value);
            }
        } else if (parameter instanceof TypeParameter) {
            DataType value = ((TypeParameter) parameter).getValue();
            if (value instanceof GenericDataType && ((GenericDataType) value).getArguments().isEmpty() && typeVariables.contains(((GenericDataType) value).getName().getValue())) {
                parameters.add(typeVariable(((GenericDataType) value).getName().getValue()));
            } else {
                parameters.add(typeParameter(toTypeSignature(value, typeVariables)));
            }
        } else {
            throw new UnsupportedOperationException("Unsupported type parameter kind: " + parameter.getClass().getName());
        }
    }
    return new TypeSignature(canonicalize(type.getName()), parameters.build());
}
Also used : DataTypeParameter(io.trino.sql.tree.DataTypeParameter) TypeSignatureParameter.namedTypeParameter(io.trino.spi.type.TypeSignatureParameter.namedTypeParameter) TypeParameter(io.trino.sql.tree.TypeParameter) DataTypeParameter(io.trino.sql.tree.DataTypeParameter) GenericDataType(io.trino.sql.tree.GenericDataType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) NumericParameter(io.trino.sql.tree.NumericParameter) DateTimeDataType(io.trino.sql.tree.DateTimeDataType) RowDataType(io.trino.sql.tree.RowDataType) GenericDataType(io.trino.sql.tree.GenericDataType) DataType(io.trino.sql.tree.DataType) IntervalDayTimeDataType(io.trino.sql.tree.IntervalDayTimeDataType)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)1 TypeSignature (io.trino.spi.type.TypeSignature)1 TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)1 TypeSignatureParameter.namedTypeParameter (io.trino.spi.type.TypeSignatureParameter.namedTypeParameter)1 DataType (io.trino.sql.tree.DataType)1 DataTypeParameter (io.trino.sql.tree.DataTypeParameter)1 DateTimeDataType (io.trino.sql.tree.DateTimeDataType)1 GenericDataType (io.trino.sql.tree.GenericDataType)1 IntervalDayTimeDataType (io.trino.sql.tree.IntervalDayTimeDataType)1 NumericParameter (io.trino.sql.tree.NumericParameter)1 RowDataType (io.trino.sql.tree.RowDataType)1 TypeParameter (io.trino.sql.tree.TypeParameter)1