Java Code Examples for io.prestosql.spi.type.Type#getBaseName()

The following examples show how to use io.prestosql.spi.type.Type#getBaseName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeCoercion.java    From presto with Apache License 2.0 6 votes vote down vote up
private TypeCompatibility typeCompatibilityForCovariantParametrizedType(Type fromType, Type toType)
{
    checkState(fromType.getClass().equals(toType.getClass()));
    ImmutableList.Builder<TypeSignatureParameter> commonParameterTypes = ImmutableList.builder();
    List<Type> fromTypeParameters = fromType.getTypeParameters();
    List<Type> toTypeParameters = toType.getTypeParameters();

    if (fromTypeParameters.size() != toTypeParameters.size()) {
        return TypeCompatibility.incompatible();
    }

    boolean coercible = true;
    for (int i = 0; i < fromTypeParameters.size(); i++) {
        TypeCompatibility compatibility = compatibility(fromTypeParameters.get(i), toTypeParameters.get(i));
        if (!compatibility.isCompatible()) {
            return TypeCompatibility.incompatible();
        }
        coercible &= compatibility.isCoercible();
        commonParameterTypes.add(TypeSignatureParameter.typeParameter(compatibility.getCommonSuperType().getTypeSignature()));
    }
    String typeBase = fromType.getBaseName();
    return TypeCompatibility.compatible(lookupType.apply(new TypeSignature(typeBase, commonParameterTypes.build())), coercible);
}
 
Example 2
Source File: SqlTypeBytecodeExpression.java    From presto with Apache License 2.0 5 votes vote down vote up
private static String generateName(Type type)
{
    String name = type.getTypeSignature().toString();
    if (name.length() > 20) {
        // Use type base to reduce the identifier size in generated code
        name = type.getBaseName();
    }
    return name.replaceAll("\\W+", "_");
}
 
Example 3
Source File: TypeCoercion.java    From presto with Apache License 2.0 5 votes vote down vote up
public boolean isTypeOnlyCoercion(Type source, Type result)
{
    if (source.equals(result)) {
        return true;
    }

    if (!canCoerce(source, result)) {
        return false;
    }

    if (source instanceof VarcharType && result instanceof VarcharType) {
        return true;
    }

    if (source instanceof DecimalType && result instanceof DecimalType) {
        DecimalType sourceDecimal = (DecimalType) source;
        DecimalType resultDecimal = (DecimalType) result;
        boolean sameDecimalSubtype = (sourceDecimal.isShort() && resultDecimal.isShort())
                || (!sourceDecimal.isShort() && !resultDecimal.isShort());
        boolean sameScale = sourceDecimal.getScale() == resultDecimal.getScale();
        boolean sourcePrecisionIsLessOrEqualToResultPrecision = sourceDecimal.getPrecision() <= resultDecimal.getPrecision();
        return sameDecimalSubtype && sameScale && sourcePrecisionIsLessOrEqualToResultPrecision;
    }

    String sourceTypeBase = source.getBaseName();
    String resultTypeBase = result.getBaseName();

    if (sourceTypeBase.equals(resultTypeBase) && isCovariantParametrizedType(source)) {
        List<Type> sourceTypeParameters = source.getTypeParameters();
        List<Type> resultTypeParameters = result.getTypeParameters();
        checkState(sourceTypeParameters.size() == resultTypeParameters.size());
        for (int i = 0; i < sourceTypeParameters.size(); i++) {
            if (!isTypeOnlyCoercion(sourceTypeParameters.get(i), resultTypeParameters.get(i))) {
                return false;
            }
        }
        return true;
    }
    return false;
}