Java Code Examples for io.prestosql.spi.type.VarcharType#getBoundedLength()

The following examples show how to use io.prestosql.spi.type.VarcharType#getBoundedLength() . 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: SliceColumnReader.java    From presto with Apache License 2.0 5 votes vote down vote up
private static int getMaxCodePointCount(Type type)
{
    if (isVarcharType(type)) {
        VarcharType varcharType = (VarcharType) type;
        return varcharType.isUnbounded() ? -1 : varcharType.getBoundedLength();
    }
    if (isCharType(type)) {
        return ((CharType) type).getLength();
    }
    if (isVarbinaryType(type)) {
        return -1;
    }
    throw new IllegalArgumentException("Unsupported encoding " + type.getDisplayName());
}
 
Example 2
Source File: HiveUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Slice varcharPartitionKey(String value, String name, Type columnType)
{
    Slice partitionKey = Slices.utf8Slice(value);
    VarcharType varcharType = (VarcharType) columnType;
    if (!varcharType.isUnbounded() && SliceUtf8.countCodePoints(partitionKey) > varcharType.getBoundedLength()) {
        throw new PrestoException(HIVE_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", value, columnType.toString(), name));
    }
    return partitionKey;
}
 
Example 3
Source File: IcebergPageSource.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Object deserializePartitionValue(Type type, String valueString, String name, TimeZoneKey timeZoneKey)
{
    if (valueString == null) {
        return null;
    }

    try {
        if (type.equals(BOOLEAN)) {
            if (valueString.equalsIgnoreCase("true")) {
                return true;
            }
            if (valueString.equalsIgnoreCase("false")) {
                return false;
            }
            throw new IllegalArgumentException();
        }
        if (type.equals(INTEGER)) {
            return parseLong(valueString);
        }
        if (type.equals(BIGINT)) {
            return parseLong(valueString);
        }
        if (type.equals(REAL)) {
            return (long) floatToRawIntBits(parseFloat(valueString));
        }
        if (type.equals(DOUBLE)) {
            return parseDouble(valueString);
        }
        if (type.equals(DATE)) {
            return parseLong(valueString);
        }
        if (type.equals(TIME)) {
            return parseLong(valueString);
        }
        if (type.equals(TIMESTAMP)) {
            return MICROSECONDS.toMillis(parseLong(valueString));
        }
        if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
            return packDateTimeWithZone(parseLong(valueString), timeZoneKey);
        }
        if (isVarcharType(type)) {
            Slice value = utf8Slice(valueString);
            VarcharType varcharType = (VarcharType) type;
            if (!varcharType.isUnbounded() && SliceUtf8.countCodePoints(value) > varcharType.getBoundedLength()) {
                throw new IllegalArgumentException();
            }
            return value;
        }
        if (type.equals(VarbinaryType.VARBINARY)) {
            return utf8Slice(valueString);
        }
        if (isShortDecimal(type) || isLongDecimal(type)) {
            DecimalType decimalType = (DecimalType) type;
            BigDecimal decimal = new BigDecimal(valueString);
            decimal = decimal.setScale(decimalType.getScale(), BigDecimal.ROUND_UNNECESSARY);
            if (decimal.precision() > decimalType.getPrecision()) {
                throw new IllegalArgumentException();
            }
            BigInteger unscaledValue = decimal.unscaledValue();
            return isShortDecimal(type) ? unscaledValue.longValue() : Decimals.encodeUnscaledValue(unscaledValue);
        }
    }
    catch (IllegalArgumentException e) {
        throw new PrestoException(ICEBERG_INVALID_PARTITION_VALUE, format(
                "Invalid partition value '%s' for %s partition key: %s",
                valueString,
                type.getDisplayName(),
                name));
    }
    // Iceberg tables don't partition by non-primitive-type columns.
    throw new PrestoException(GENERIC_INTERNAL_ERROR, "Invalid partition type " + type.toString());
}
 
Example 4
Source File: TypeConverter.java    From presto with Apache License 2.0 4 votes vote down vote up
private static TypeInfo toHiveTypeInfo(Type type)
{
    if (BOOLEAN.equals(type)) {
        return HIVE_BOOLEAN.getTypeInfo();
    }
    if (BIGINT.equals(type)) {
        return HIVE_LONG.getTypeInfo();
    }
    if (INTEGER.equals(type)) {
        return HIVE_INT.getTypeInfo();
    }
    if (SMALLINT.equals(type)) {
        return HIVE_SHORT.getTypeInfo();
    }
    if (TINYINT.equals(type)) {
        return HIVE_BYTE.getTypeInfo();
    }
    if (REAL.equals(type)) {
        return HIVE_FLOAT.getTypeInfo();
    }
    if (DOUBLE.equals(type)) {
        return HIVE_DOUBLE.getTypeInfo();
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return HIVE_STRING.getTypeInfo();
        }
        if (varcharType.getBoundedLength() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharType.getBoundedLength());
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
            return getCharTypeInfo(charLength);
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).",
                type, HiveChar.MAX_CHAR_LENGTH));
    }
    if (VARBINARY.equals(type)) {
        return HIVE_BINARY.getTypeInfo();
    }
    if (DATE.equals(type)) {
        return HIVE_DATE.getTypeInfo();
    }
    if (TIMESTAMP.equals(type)) {
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        // Hive does not have TIMESTAMP_WITH_TIME_ZONE, this is just a work around for iceberg.
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
    }
    if (isArrayType(type)) {
        TypeInfo elementType = toHiveTypeInfo(type.getTypeParameters().get(0));
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = toHiveTypeInfo(type.getTypeParameters().get(0));
        TypeInfo valueType = toHiveTypeInfo(type.getTypeParameters().get(1));
        return getMapTypeInfo(keyType, valueType);
    }
    if (isRowType(type)) {
        ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
        for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
            if (!parameter.isNamedTypeSignature()) {
                throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
            }
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            if (namedTypeSignature.getName().isEmpty()) {
                throw new PrestoException(NOT_SUPPORTED, format("Anonymous row type is not supported in Hive. Please give each field a name: %s", type));
            }
            fieldNames.add(namedTypeSignature.getName().get());
        }
        return getStructTypeInfo(
                fieldNames.build(),
                type.getTypeParameters().stream()
                        .map(TypeConverter::toHiveTypeInfo)
                        .collect(toList()));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
 
Example 5
Source File: MySqlClient.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type)
{
    if (REAL.equals(type)) {
        return WriteMapping.longMapping("float", realWriteFunction());
    }
    if (TIME_WITH_TIME_ZONE.equals(type) || TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        throw new PrestoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
    }
    if (TIMESTAMP.equals(type)) {
        // TODO use `timestampWriteFunction`
        return WriteMapping.longMapping("datetime", timestampWriteFunctionUsingSqlTimestamp(session));
    }
    if (VARBINARY.equals(type)) {
        return WriteMapping.sliceMapping("mediumblob", varbinaryWriteFunction());
    }
    if (isVarcharType(type)) {
        VarcharType varcharType = (VarcharType) type;
        String dataType;
        if (varcharType.isUnbounded()) {
            dataType = "longtext";
        }
        else if (varcharType.getBoundedLength() <= 255) {
            dataType = "tinytext";
        }
        else if (varcharType.getBoundedLength() <= 65535) {
            dataType = "text";
        }
        else if (varcharType.getBoundedLength() <= 16777215) {
            dataType = "mediumtext";
        }
        else {
            dataType = "longtext";
        }
        return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
    }
    if (type.equals(jsonType)) {
        return WriteMapping.sliceMapping("json", varcharWriteFunction());
    }

    return super.toWriteMapping(session, type);
}
 
Example 6
Source File: HiveTypeTranslator.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public TypeInfo translate(Type type)
{
    if (BOOLEAN.equals(type)) {
        return HIVE_BOOLEAN.getTypeInfo();
    }
    if (BIGINT.equals(type)) {
        return HIVE_LONG.getTypeInfo();
    }
    if (INTEGER.equals(type)) {
        return HIVE_INT.getTypeInfo();
    }
    if (SMALLINT.equals(type)) {
        return HIVE_SHORT.getTypeInfo();
    }
    if (TINYINT.equals(type)) {
        return HIVE_BYTE.getTypeInfo();
    }
    if (REAL.equals(type)) {
        return HIVE_FLOAT.getTypeInfo();
    }
    if (DOUBLE.equals(type)) {
        return HIVE_DOUBLE.getTypeInfo();
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return HIVE_STRING.getTypeInfo();
        }
        if (varcharType.getBoundedLength() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharType.getBoundedLength());
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
            return getCharTypeInfo(charLength);
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).",
                type, HiveChar.MAX_CHAR_LENGTH));
    }
    if (VARBINARY.equals(type)) {
        return HIVE_BINARY.getTypeInfo();
    }
    if (DATE.equals(type)) {
        return HIVE_DATE.getTypeInfo();
    }
    if (TIMESTAMP.equals(type)) {
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
    }
    if (isArrayType(type)) {
        TypeInfo elementType = translate(type.getTypeParameters().get(0));
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = translate(type.getTypeParameters().get(0));
        TypeInfo valueType = translate(type.getTypeParameters().get(1));
        return getMapTypeInfo(keyType, valueType);
    }
    if (isRowType(type)) {
        ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
        for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
            if (!parameter.isNamedTypeSignature()) {
                throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
            }
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            if (namedTypeSignature.getName().isEmpty()) {
                throw new PrestoException(NOT_SUPPORTED, format("Anonymous row type is not supported in Hive. Please give each field a name: %s", type));
            }
            fieldNames.add(namedTypeSignature.getName().get());
        }
        return getStructTypeInfo(
                fieldNames.build(),
                type.getTypeParameters().stream()
                        .map(this::translate)
                        .collect(toImmutableList()));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
 
Example 7
Source File: HiveWriteUtils.java    From presto with Apache License 2.0 4 votes vote down vote up
public static ObjectInspector getRowColumnInspector(Type type)
{
    if (type.equals(BooleanType.BOOLEAN)) {
        return writableBooleanObjectInspector;
    }

    if (type.equals(BigintType.BIGINT)) {
        return writableLongObjectInspector;
    }

    if (type.equals(IntegerType.INTEGER)) {
        return writableIntObjectInspector;
    }

    if (type.equals(SmallintType.SMALLINT)) {
        return writableShortObjectInspector;
    }

    if (type.equals(TinyintType.TINYINT)) {
        return writableByteObjectInspector;
    }

    if (type.equals(RealType.REAL)) {
        return writableFloatObjectInspector;
    }

    if (type.equals(DoubleType.DOUBLE)) {
        return writableDoubleObjectInspector;
    }

    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            // Unbounded VARCHAR is not supported by Hive.
            // Values for such columns must be stored as STRING in Hive
            return writableStringObjectInspector;
        }
        if (varcharType.getBoundedLength() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            // VARCHAR columns with the length less than or equal to 65535 are supported natively by Hive
            return getPrimitiveWritableObjectInspector(getVarcharTypeInfo(varcharType.getBoundedLength()));
        }
    }

    if (isCharType(type)) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        return getPrimitiveWritableObjectInspector(getCharTypeInfo(charLength));
    }

    if (type.equals(VarbinaryType.VARBINARY)) {
        return writableBinaryObjectInspector;
    }

    if (type.equals(DateType.DATE)) {
        return writableDateObjectInspector;
    }

    if (type.equals(TimestampType.TIMESTAMP)) {
        return writableTimestampObjectInspector;
    }

    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }

    if (isArrayType(type) || isMapType(type) || isRowType(type)) {
        return getJavaObjectInspector(type);
    }

    throw new IllegalArgumentException("unsupported type: " + type);
}