Search in sources :

Example 1 with NamedTypeSignature

use of com.facebook.presto.spi.type.NamedTypeSignature in project presto by prestodb.

the class QueryResults method fixValue.

/**
     * Force values coming from Jackson to have the expected object type.
     */
private static Object fixValue(TypeSignature signature, Object value) {
    if (value == null) {
        return null;
    }
    if (signature.getBase().equals(ARRAY)) {
        List<Object> fixedValue = new ArrayList<>();
        for (Object object : List.class.cast(value)) {
            fixedValue.add(fixValue(signature.getTypeParametersAsTypeSignatures().get(0), object));
        }
        return fixedValue;
    }
    if (signature.getBase().equals(MAP)) {
        TypeSignature keySignature = signature.getTypeParametersAsTypeSignatures().get(0);
        TypeSignature valueSignature = signature.getTypeParametersAsTypeSignatures().get(1);
        Map<Object, Object> fixedValue = new HashMap<>();
        for (Map.Entry<?, ?> entry : (Set<Map.Entry<?, ?>>) Map.class.cast(value).entrySet()) {
            fixedValue.put(fixValue(keySignature, entry.getKey()), fixValue(valueSignature, entry.getValue()));
        }
        return fixedValue;
    }
    if (signature.getBase().equals(ROW)) {
        Map<String, Object> fixedValue = new LinkedHashMap<>();
        List<Object> listValue = List.class.cast(value);
        checkArgument(listValue.size() == signature.getParameters().size(), "Mismatched data values and row type");
        for (int i = 0; i < listValue.size(); i++) {
            TypeSignatureParameter parameter = signature.getParameters().get(i);
            checkArgument(parameter.getKind() == ParameterKind.NAMED_TYPE, "Unexpected parameter [%s] for row type", parameter);
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            String key = namedTypeSignature.getName();
            fixedValue.put(key, fixValue(namedTypeSignature.getTypeSignature(), listValue.get(i)));
        }
        return fixedValue;
    }
    switch(signature.getBase()) {
        case BIGINT:
            if (value instanceof String) {
                return Long.parseLong((String) value);
            }
            return ((Number) value).longValue();
        case INTEGER:
            if (value instanceof String) {
                return Integer.parseInt((String) value);
            }
            return ((Number) value).intValue();
        case SMALLINT:
            if (value instanceof String) {
                return Short.parseShort((String) value);
            }
            return ((Number) value).shortValue();
        case TINYINT:
            if (value instanceof String) {
                return Byte.parseByte((String) value);
            }
            return ((Number) value).byteValue();
        case DOUBLE:
            if (value instanceof String) {
                return Double.parseDouble((String) value);
            }
            return ((Number) value).doubleValue();
        case REAL:
            if (value instanceof String) {
                return Float.parseFloat((String) value);
            }
            return ((Number) value).floatValue();
        case BOOLEAN:
            if (value instanceof String) {
                return Boolean.parseBoolean((String) value);
            }
            return Boolean.class.cast(value);
        case VARCHAR:
        case JSON:
        case TIME:
        case TIME_WITH_TIME_ZONE:
        case TIMESTAMP:
        case TIMESTAMP_WITH_TIME_ZONE:
        case DATE:
        case INTERVAL_YEAR_TO_MONTH:
        case INTERVAL_DAY_TO_SECOND:
        case DECIMAL:
        case CHAR:
            return String.class.cast(value);
        default:
            // as a plain text and everything else is base64 encoded binary
            if (value instanceof String) {
                return Base64.getDecoder().decode((String) value);
            }
            return value;
    }
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) LinkedHashMap(java.util.LinkedHashMap) TypeSignature(com.facebook.presto.spi.type.TypeSignature) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with NamedTypeSignature

use of com.facebook.presto.spi.type.NamedTypeSignature in project presto by prestodb.

the class ParquetReader method readStruct.

private Block readStruct(Type type, List<String> path, IntList elementOffsets) throws IOException {
    List<TypeSignatureParameter> parameters = type.getTypeSignature().getParameters();
    Block[] blocks = new Block[parameters.size()];
    for (int i = 0; i < parameters.size(); i++) {
        NamedTypeSignature namedTypeSignature = parameters.get(i).getNamedTypeSignature();
        Type fieldType = typeManager.getType(namedTypeSignature.getTypeSignature());
        String name = namedTypeSignature.getName();
        blocks[i] = readBlock(name, fieldType, path, new IntArrayList());
    }
    InterleavedBlock interleavedBlock = new InterleavedBlock(blocks);
    int blockSize = blocks[0].getPositionCount();
    int[] offsets = new int[blockSize + 1];
    for (int i = 1; i < offsets.length; i++) {
        elementOffsets.add(parameters.size());
        offsets[i] = i * parameters.size();
    }
    return new ArrayBlock(blockSize, new boolean[blockSize], offsets, interleavedBlock);
}
Also used : Type(com.facebook.presto.spi.type.Type) MessageType(parquet.schema.MessageType) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) Block(com.facebook.presto.spi.block.Block) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) ArrayBlock(com.facebook.presto.spi.block.ArrayBlock) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock)

Example 3 with NamedTypeSignature

use of com.facebook.presto.spi.type.NamedTypeSignature in project presto by prestodb.

the class HiveTypeTranslator method translate.

@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;
        int varcharLength = varcharType.getLength();
        if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharLength);
        } else if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
            return HIVE_STRING.getTypeInfo();
        } else {
            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();
            fieldNames.add(namedTypeSignature.getName());
        }
        return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(this::translate).collect(toList()));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(com.facebook.presto.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) PrestoException(com.facebook.presto.spi.PrestoException) TypeInfoFactory.getCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo) TypeInfoFactory.getStructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getStructTypeInfo) TypeInfoFactory.getVarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) DecimalType(com.facebook.presto.spi.type.DecimalType) CharType(com.facebook.presto.spi.type.CharType)

Aggregations

NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)3 TypeSignatureParameter (com.facebook.presto.spi.type.TypeSignatureParameter)3 PrestoException (com.facebook.presto.spi.PrestoException)1 ArrayBlock (com.facebook.presto.spi.block.ArrayBlock)1 Block (com.facebook.presto.spi.block.Block)1 InterleavedBlock (com.facebook.presto.spi.block.InterleavedBlock)1 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)1 CharType (com.facebook.presto.spi.type.CharType)1 DecimalType (com.facebook.presto.spi.type.DecimalType)1 Type (com.facebook.presto.spi.type.Type)1 TypeSignature (com.facebook.presto.spi.type.TypeSignature)1 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)1 VarcharType (com.facebook.presto.spi.type.VarcharType)1 ImmutableList (com.google.common.collect.ImmutableList)1 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1