Search in sources :

Example 16 with TypeSignatureParameter

use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.

the class RaptorStorageManager method getType.

private Type getType(ColumnMetadata<OrcType> types, OrcColumnId columnId) {
    OrcType type = types.get(columnId);
    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.typeParameter(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.typeParameter(keyType), TypeSignatureParameter.typeParameter(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.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(fieldNames.get(i))), getType(types, type.getFieldTypeIndex(i)).getTypeSignature())));
            }
            return typeManager.getParameterizedType(StandardTypes.ROW, fieldTypes.build());
        default:
            throw new TrinoException(RAPTOR_ERROR, "Unhandled ORC type: " + type);
    }
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) OrcType(io.trino.orc.metadata.OrcType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(io.trino.spi.type.RowFieldName) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TrinoException(io.trino.spi.TrinoException)

Example 17 with TypeSignatureParameter

use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.

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();
    }
    if (value instanceof Binary) {
        typeSignature = VARBINARY.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_MILLIS.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(Optional::isEmpty)) {
            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::typeParameter).collect(Collectors.toList()));
        } else {
            // TODO: client doesn't handle empty field name row type yet
            typeSignature = new TypeSignature(StandardTypes.ROW, IntStream.range(0, subTypes.size()).mapToObj(idx -> TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(format("%s%d", implicitPrefix, idx + 1))), 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()) {
                parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(key)), fieldType.get())));
            }
        }
        if (!parameters.isEmpty()) {
            typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
        }
    } else if (value instanceof DBRef) {
        List<TypeSignatureParameter> parameters = new ArrayList<>();
        TypeSignature idFieldType = guessFieldType(((DBRef) value).getId()).orElseThrow(() -> new UnsupportedOperationException("Unable to guess $id field type of DBRef from: " + ((DBRef) value).getId()));
        parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(DATABASE_NAME)), VARCHAR.getTypeSignature())));
        parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(COLLECTION_NAME)), VARCHAR.getTypeSignature())));
        parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(ID)), idFieldType)));
        typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
    }
    return Optional.ofNullable(typeSignature);
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) SchemaNotFoundException(io.trino.spi.connector.SchemaNotFoundException) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) EvictableCacheBuilder(io.trino.collect.cache.EvictableCacheBuilder) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Map(java.util.Map) ENGLISH(java.util.Locale.ENGLISH) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) Collectors.toSet(java.util.stream.Collectors.toSet) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Range(io.trino.spi.predicate.Range) Domain(io.trino.spi.predicate.Domain) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) Collectors(java.util.stream.Collectors) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) FindIterable(com.mongodb.client.FindIterable) OBJECT_ID(io.trino.plugin.mongodb.ObjectIdType.OBJECT_ID) Optional(java.util.Optional) HostAddress(io.trino.spi.HostAddress) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) IntStream(java.util.stream.IntStream) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) Slice(io.airlift.slice.Slice) Logger(io.airlift.log.Logger) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TIMESTAMP_MILLIS(io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS) Type(io.trino.spi.type.Type) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) Shorts(com.google.common.primitives.Shorts) MINUTES(java.util.concurrent.TimeUnit.MINUTES) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) VarcharType(io.trino.spi.type.VarcharType) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) 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) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnHandle(io.trino.spi.connector.ColumnHandle) RowFieldName(io.trino.spi.type.RowFieldName) HostAddress.fromParts(io.trino.spi.HostAddress.fromParts) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) Math.toIntExact(java.lang.Math.toIntExact) Binary(org.bson.types.Binary) IntegerType(io.trino.spi.type.IntegerType) SignedBytes(com.google.common.primitives.SignedBytes) StandardTypes(io.trino.spi.type.StandardTypes) IndexOptions(com.mongodb.client.model.IndexOptions) TupleDomain(io.trino.spi.predicate.TupleDomain) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) Primitives(com.google.common.primitives.Primitives) ExecutionException(java.util.concurrent.ExecutionException) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) Collectors.toList(java.util.stream.Collectors.toList) DBRef(com.mongodb.DBRef) DeleteResult(com.mongodb.client.result.DeleteResult) ObjectId(org.bson.types.ObjectId) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TINYINT(io.trino.spi.type.TinyintType.TINYINT) Cache(com.google.common.cache.Cache) TypeManager(io.trino.spi.type.TypeManager) Optional(java.util.Optional) ObjectId(org.bson.types.ObjectId) RowFieldName(io.trino.spi.type.RowFieldName) DBRef(com.mongodb.DBRef) ArrayList(java.util.ArrayList) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) Document(org.bson.Document) Date(java.util.Date) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) Binary(org.bson.types.Binary)

Aggregations

TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)17 NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 Type (io.trino.spi.type.Type)9 ImmutableList (com.google.common.collect.ImmutableList)8 TrinoException (io.trino.spi.TrinoException)8 TypeSignature (io.trino.spi.type.TypeSignature)8 RowType (io.trino.spi.type.RowType)6 VarcharType (io.trino.spi.type.VarcharType)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 CharType (io.trino.spi.type.CharType)4 DecimalType (io.trino.spi.type.DecimalType)4 RowFieldName (io.trino.spi.type.RowFieldName)4 TimestampType (io.trino.spi.type.TimestampType)3 Collectors.toList (java.util.stream.Collectors.toList)3 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)3 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)3 TypeInfoFactory.getCharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo)3 TypeInfoFactory.getListTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo)3