Search in sources :

Example 1 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class OrcTester method rowType.

public static Type rowType(Type... fieldTypes) {
    ImmutableList.Builder<TypeSignatureParameter> typeSignatureParameters = ImmutableList.builder();
    for (int i = 0; i < fieldTypes.length; i++) {
        String filedName = "field_" + i;
        Type fieldType = fieldTypes[i];
        typeSignatureParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(filedName, false)), fieldType.getTypeSignature())));
    }
    return FUNCTION_AND_TYPE_MANAGER.getParameterizedType(StandardTypes.ROW, typeSignatureParameters.build());
}
Also used : DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) RowType(com.facebook.presto.common.type.RowType) VarcharType(com.facebook.presto.common.type.VarcharType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature)

Example 2 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class HiveTypeTranslator method translate.

@Override
public TypeInfo translate(Type type, Optional<HiveType> defaultHiveType) {
    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 EnumType<?>) {
        return translate(((EnumType<?>) type).getValueType());
    }
    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 (type instanceof TypeWithName) {
        return translate(((TypeWithName) type).getType());
    }
    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), defaultHiveType);
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = translate(type.getTypeParameters().get(0), defaultHiveType);
        TypeInfo valueType = translate(type.getTypeParameters().get(1), defaultHiveType);
        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().isPresent()) {
                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(t -> translate(t, defaultHiveType)).collect(toList()));
    }
    return defaultHiveType.orElseThrow(() -> new PrestoException(NOT_SUPPORTED, format("No default Hive type provided for unsupported Hive type: %s", type))).getTypeInfo();
}
Also used : TypeWithName(com.facebook.presto.common.type.TypeWithName) VarcharType(com.facebook.presto.common.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) NamedTypeSignature(com.facebook.presto.common.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.common.type.TypeSignatureParameter) EnumType(com.facebook.presto.common.type.EnumType) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

Example 3 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class BuiltInTypeAndFunctionNamespaceManager method instantiateParametricType.

private Type instantiateParametricType(TypeSignature signature) {
    List<TypeParameter> parameters = new ArrayList<>();
    for (TypeSignatureParameter parameter : signature.getParameters()) {
        TypeParameter typeParameter = TypeParameter.of(parameter, functionAndTypeManager);
        parameters.add(typeParameter);
    }
    ParametricType parametricType = parametricTypes.get(signature.getBase().toLowerCase(Locale.ENGLISH));
    if (parametricType == null) {
        throw new IllegalArgumentException("Unknown type " + signature);
    }
    if (parametricType instanceof MapParametricType) {
        return ((MapParametricType) parametricType).createType(functionAndTypeManager, parameters);
    }
    Type instantiatedType = parametricType.createType(parameters);
    // checkState(instantiatedType.equalsSignature(signature), "Instantiated parametric type name (%s) does not match expected name (%s)", instantiatedType, signature);
    return instantiatedType;
}
Also used : TypeParameter(com.facebook.presto.common.type.TypeParameter) DecimalParametricType(com.facebook.presto.type.DecimalParametricType) UserDefinedType(com.facebook.presto.common.type.UserDefinedType) OperatorType.tryGetOperatorType(com.facebook.presto.common.function.OperatorType.tryGetOperatorType) VarcharParametricType(com.facebook.presto.type.VarcharParametricType) ParametricType(com.facebook.presto.common.type.ParametricType) CharParametricType(com.facebook.presto.type.CharParametricType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) MapParametricType(com.facebook.presto.type.MapParametricType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ArrayList(java.util.ArrayList) MapParametricType(com.facebook.presto.type.MapParametricType) DecimalParametricType(com.facebook.presto.type.DecimalParametricType) VarcharParametricType(com.facebook.presto.type.VarcharParametricType) ParametricType(com.facebook.presto.common.type.ParametricType) CharParametricType(com.facebook.presto.type.CharParametricType) MapParametricType(com.facebook.presto.type.MapParametricType)

Example 4 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class MongoSession method guessFieldType.

private Optional<TypeSignature> guessFieldType(Object value) {
    if (value == null) {
        return Optional.empty();
    }
    TypeSignature typeSignature = null;
    if (value instanceof String) {
        typeSignature = createUnboundedVarcharType().getTypeSignature();
    } else if (value instanceof Integer || value instanceof Long) {
        typeSignature = BIGINT.getTypeSignature();
    } else if (value instanceof Boolean) {
        typeSignature = BOOLEAN.getTypeSignature();
    } else if (value instanceof Float || value instanceof Double) {
        typeSignature = DOUBLE.getTypeSignature();
    } else if (value instanceof Date) {
        typeSignature = TIMESTAMP.getTypeSignature();
    } else if (value instanceof ObjectId) {
        typeSignature = OBJECT_ID.getTypeSignature();
    } else if (value instanceof List) {
        List<Optional<TypeSignature>> subTypes = ((List<?>) value).stream().map(this::guessFieldType).collect(toList());
        if (subTypes.isEmpty() || subTypes.stream().anyMatch(t -> !t.isPresent())) {
            return Optional.empty();
        }
        Set<TypeSignature> signatures = subTypes.stream().map(Optional::get).collect(toSet());
        if (signatures.size() == 1) {
            typeSignature = new TypeSignature(StandardTypes.ARRAY, signatures.stream().map(TypeSignatureParameter::of).collect(Collectors.toList()));
        } else {
            // TODO: presto cli doesn't handle empty field name row type yet
            typeSignature = new TypeSignature(StandardTypes.ROW, IntStream.range(0, subTypes.size()).mapToObj(idx -> TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(String.format("%s%d", implicitPrefix, idx + 1), false)), subTypes.get(idx).get()))).collect(toList()));
        }
    } else if (value instanceof Document) {
        List<TypeSignatureParameter> parameters = new ArrayList<>();
        for (String key : ((Document) value).keySet()) {
            Optional<TypeSignature> fieldType = guessFieldType(((Document) value).get(key));
            if (!fieldType.isPresent()) {
                return Optional.empty();
            }
            parameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(key, false)), fieldType.get())));
        }
        typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
    }
    return Optional.ofNullable(typeSignature);
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) LoadingCache(com.google.common.cache.LoadingCache) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) TypeSignature(com.facebook.presto.common.type.TypeSignature) SchemaTableName(com.facebook.presto.spi.SchemaTableName) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Map(java.util.Map) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Set(java.util.Set) Collectors(java.util.stream.Collectors) Range(com.facebook.presto.common.predicate.Range) Preconditions.checkState(com.google.common.base.Preconditions.checkState) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) IntStream(java.util.stream.IntStream) Logger(com.facebook.airlift.log.Logger) StandardTypes(com.facebook.presto.common.type.StandardTypes) MongoCollection(com.mongodb.client.MongoCollection) Slice(io.airlift.slice.Slice) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) OBJECT_ID(com.facebook.presto.mongodb.ObjectIdType.OBJECT_ID) TIMESTAMP(com.facebook.presto.common.type.TimestampType.TIMESTAMP) MINUTES(java.util.concurrent.TimeUnit.MINUTES) PrestoException(com.facebook.presto.spi.PrestoException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) MongoCursor(com.mongodb.client.MongoCursor) Verify.verify(com.google.common.base.Verify.verify) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TypeManager(com.facebook.presto.common.type.TypeManager) Objects.requireNonNull(java.util.Objects.requireNonNull) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) Type(com.facebook.presto.common.type.Type) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) IndexOptions(com.mongodb.client.model.IndexOptions) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) Domain(com.facebook.presto.common.predicate.Domain) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) ColumnHandle(com.facebook.presto.spi.ColumnHandle) MongoClient(com.mongodb.MongoClient) DeleteResult(com.mongodb.client.result.DeleteResult) ObjectId(org.bson.types.ObjectId) HOURS(java.util.concurrent.TimeUnit.HOURS) RowFieldName(com.facebook.presto.common.type.RowFieldName) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Optional(java.util.Optional) ObjectId(org.bson.types.ObjectId) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Document(org.bson.Document) Date(java.util.Date) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList)

Example 5 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class OrcStorageManager method getType.

private Type getType(List<OrcType> types, int index) {
    OrcType type = types.get(index);
    switch(type.getOrcTypeKind()) {
        case BOOLEAN:
            return BOOLEAN;
        case LONG:
            return BIGINT;
        case DOUBLE:
            return DOUBLE;
        case STRING:
            return createUnboundedVarcharType();
        case VARCHAR:
            return createVarcharType(type.getLength().get());
        case CHAR:
            return createCharType(type.getLength().get());
        case BINARY:
            return VARBINARY;
        case DECIMAL:
            return DecimalType.createDecimalType(type.getPrecision().get(), type.getScale().get());
        case LIST:
            TypeSignature elementType = getType(types, type.getFieldTypeIndex(0)).getTypeSignature();
            return typeManager.getParameterizedType(StandardTypes.ARRAY, ImmutableList.of(TypeSignatureParameter.of(elementType)));
        case MAP:
            TypeSignature keyType = getType(types, type.getFieldTypeIndex(0)).getTypeSignature();
            TypeSignature valueType = getType(types, type.getFieldTypeIndex(1)).getTypeSignature();
            return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
        case STRUCT:
            List<String> fieldNames = type.getFieldNames();
            ImmutableList.Builder<TypeSignatureParameter> fieldTypes = ImmutableList.builder();
            for (int i = 0; i < type.getFieldCount(); i++) {
                fieldTypes.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(fieldNames.get(i), false)), getType(types, type.getFieldTypeIndex(i)).getTypeSignature())));
            }
            return typeManager.getParameterizedType(StandardTypes.ROW, fieldTypes.build());
    }
    throw new PrestoException(RAPTOR_ERROR, "Unhandled ORC type: " + type);
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) OrcType(com.facebook.presto.orc.metadata.OrcType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) PrestoException(com.facebook.presto.spi.PrestoException)

Aggregations

TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)21 NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)15 ImmutableList (com.google.common.collect.ImmutableList)13 TypeSignature (com.facebook.presto.common.type.TypeSignature)11 Type (com.facebook.presto.common.type.Type)9 RowFieldName (com.facebook.presto.common.type.RowFieldName)8 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 PrestoException (com.facebook.presto.spi.PrestoException)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Optional (java.util.Optional)5 CharType (com.facebook.presto.common.type.CharType)4 DecimalType (com.facebook.presto.common.type.DecimalType)4 VarcharType (com.facebook.presto.common.type.VarcharType)4 Map (java.util.Map)4 Collectors.toList (java.util.stream.Collectors.toList)4 RowType (com.facebook.presto.common.type.RowType)3 StandardTypes (com.facebook.presto.common.type.StandardTypes)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Set (java.util.Set)3