Search in sources :

Example 76 with TypeSignature

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

the class RaptorStorageManager method toOrcFileType.

static Type toOrcFileType(Type raptorType, TypeManager typeManager) {
    // TIMESTAMPS are stored as BIGINT to void the poor encoding in ORC
    if (raptorType.equals(TIMESTAMP_MILLIS)) {
        return BIGINT;
    }
    if (raptorType instanceof ArrayType) {
        Type elementType = toOrcFileType(((ArrayType) raptorType).getElementType(), typeManager);
        return new ArrayType(elementType);
    }
    if (raptorType instanceof MapType) {
        TypeSignature keyType = toOrcFileType(((MapType) raptorType).getKeyType(), typeManager).getTypeSignature();
        TypeSignature valueType = toOrcFileType(((MapType) raptorType).getValueType(), typeManager).getTypeSignature();
        return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.typeParameter(keyType), TypeSignatureParameter.typeParameter(valueType)));
    }
    if (raptorType instanceof RowType) {
        List<Field> fields = ((RowType) raptorType).getFields().stream().map(field -> new Field(field.getName(), toOrcFileType(field.getType(), typeManager))).collect(toImmutableList());
        return RowType.from(fields);
    }
    return raptorType;
}
Also used : ArrayType(io.trino.spi.type.ArrayType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) MoreFutures.allAsList(io.airlift.concurrent.MoreFutures.allAsList) OrcPredicate(io.trino.orc.OrcPredicate) Maps.uniqueIndex(com.google.common.collect.Maps.uniqueIndex) CharType.createCharType(io.trino.spi.type.CharType.createCharType) RAPTOR_RECOVERY_TIMEOUT(io.trino.plugin.raptor.legacy.RaptorErrorCode.RAPTOR_RECOVERY_TIMEOUT) Future(java.util.concurrent.Future) ShardDelta(io.trino.plugin.raptor.legacy.metadata.ShardDelta) Slices(io.airlift.slice.Slices) Map(java.util.Map) RaptorColumnHandle.isShardUuidColumn(io.trino.plugin.raptor.legacy.RaptorColumnHandle.isShardUuidColumn) FileOrcDataSource(io.trino.orc.FileOrcDataSource) RAPTOR_ERROR(io.trino.plugin.raptor.legacy.RaptorErrorCode.RAPTOR_ERROR) ConnectorPageSource(io.trino.spi.connector.ConnectorPageSource) BackupStore(io.trino.plugin.raptor.legacy.backup.BackupStore) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) Slice(io.airlift.slice.Slice) OrcFileInfo(io.trino.plugin.raptor.legacy.storage.OrcFileRewriter.OrcFileInfo) TIMESTAMP_MILLIS(io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS) Page(io.trino.spi.Page) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) ShardRecorder(io.trino.plugin.raptor.legacy.metadata.ShardRecorder) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) ShardStats.computeColumnStats(io.trino.plugin.raptor.legacy.storage.ShardStats.computeColumnStats) TupleDomainOrcPredicateBuilder(io.trino.orc.TupleDomainOrcPredicate.TupleDomainOrcPredicateBuilder) AggregatedMemoryContext(io.trino.memory.context.AggregatedMemoryContext) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) OrcType(io.trino.orc.metadata.OrcType) ATOMIC_MOVE(java.nio.file.StandardCopyOption.ATOMIC_MOVE) Files(java.nio.file.Files) MapType(io.trino.spi.type.MapType) StandardTypes(io.trino.spi.type.StandardTypes) IOException(java.io.IOException) MoreFutures.getFutureValue(io.airlift.concurrent.MoreFutures.getFutureValue) UTC(org.joda.time.DateTimeZone.UTC) CatalogName(io.trino.plugin.base.CatalogName) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) File(java.io.File) RAPTOR_RECOVERY_ERROR(io.trino.plugin.raptor.legacy.RaptorErrorCode.RAPTOR_RECOVERY_ERROR) ExecutionException(java.util.concurrent.ExecutionException) JsonCodec.jsonCodec(io.airlift.json.JsonCodec.jsonCodec) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) ShardInfo(io.trino.plugin.raptor.legacy.metadata.ShardInfo) OrcColumn(io.trino.orc.OrcColumn) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) OrcColumnId(io.trino.orc.metadata.OrcColumnId) NodeManager(io.trino.spi.NodeManager) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) TimeoutException(java.util.concurrent.TimeoutException) BackupManager(io.trino.plugin.raptor.legacy.backup.BackupManager) Duration(io.airlift.units.Duration) ColumnInfo(io.trino.plugin.raptor.legacy.metadata.ColumnInfo) PreDestroy(javax.annotation.PreDestroy) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) OrcRecordReader(io.trino.orc.OrcRecordReader) OrcDataSource(io.trino.orc.OrcDataSource) ROOT_COLUMN(io.trino.orc.metadata.OrcColumnId.ROOT_COLUMN) TypeSignature(io.trino.spi.type.TypeSignature) RowType(io.trino.spi.type.RowType) ImmutableSet(com.google.common.collect.ImmutableSet) Field(io.trino.spi.type.RowType.Field) Collection(java.util.Collection) INITIAL_BATCH_SIZE(io.trino.orc.OrcReader.INITIAL_BATCH_SIZE) TrinoException(io.trino.spi.TrinoException) ArrayType(io.trino.spi.type.ArrayType) UUID(java.util.UUID) Math.min(java.lang.Math.min) RaptorColumnHandle(io.trino.plugin.raptor.legacy.RaptorColumnHandle) FileNotFoundException(java.io.FileNotFoundException) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DataSize(io.airlift.units.DataSize) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) Optional(java.util.Optional) DecimalType(io.trino.spi.type.DecimalType) JsonCodec(io.airlift.json.JsonCodec) RaptorColumnHandle.isHiddenColumn(io.trino.plugin.raptor.legacy.RaptorColumnHandle.isHiddenColumn) AggregatedMemoryContext.newSimpleAggregatedMemoryContext(io.trino.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) CompletableFuture(java.util.concurrent.CompletableFuture) TupleDomainOrcPredicate(io.trino.orc.TupleDomainOrcPredicate) OptionalInt(java.util.OptionalInt) ColumnAdaptation(io.trino.plugin.raptor.legacy.storage.RaptorPageSource.ColumnAdaptation) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) Threads.daemonThreadsNamed(io.airlift.concurrent.Threads.daemonThreadsNamed) OrcReaderOptions(io.trino.orc.OrcReaderOptions) Objects.requireNonNull(java.util.Objects.requireNonNull) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) RowFieldName(io.trino.spi.type.RowFieldName) ExecutorService(java.util.concurrent.ExecutorService) OrcReader(io.trino.orc.OrcReader) RAPTOR_LOCAL_DISK_FULL(io.trino.plugin.raptor.legacy.RaptorErrorCode.RAPTOR_LOCAL_DISK_FULL) PETABYTE(io.airlift.units.DataSize.Unit.PETABYTE) XxHash64(io.airlift.slice.XxHash64) ColumnMetadata(io.trino.orc.metadata.ColumnMetadata) FileInputStream(java.io.FileInputStream) TupleDomain(io.trino.spi.predicate.TupleDomain) Executors.newFixedThreadPool(java.util.concurrent.Executors.newFixedThreadPool) TimeUnit(java.util.concurrent.TimeUnit) RaptorColumnHandle.isShardRowIdColumn(io.trino.plugin.raptor.legacy.RaptorColumnHandle.isShardRowIdColumn) RaptorColumnHandle.isBucketNumberColumn(io.trino.plugin.raptor.legacy.RaptorColumnHandle.isBucketNumberColumn) Collectors.toList(java.util.stream.Collectors.toList) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) ColumnStats(io.trino.plugin.raptor.legacy.metadata.ColumnStats) Closeable(java.io.Closeable) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BitSet(java.util.BitSet) TypeManager(io.trino.spi.type.TypeManager) InputStream(java.io.InputStream) Field(io.trino.spi.type.RowType.Field) CharType.createCharType(io.trino.spi.type.CharType.createCharType) OrcType(io.trino.orc.metadata.OrcType) MapType(io.trino.spi.type.MapType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType)

Example 77 with TypeSignature

use of io.trino.spi.type.TypeSignature 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)

Example 78 with TypeSignature

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

the class MongoSession method guessTableFields.

private List<Document> guessTableFields(String schemaName, String tableName) {
    MongoDatabase db = client.getDatabase(schemaName);
    Document doc = db.getCollection(tableName).find().first();
    if (doc == null) {
        // no records at the collection
        return ImmutableList.of();
    }
    ImmutableList.Builder<Document> builder = ImmutableList.builder();
    for (String key : doc.keySet()) {
        Object value = doc.get(key);
        Optional<TypeSignature> fieldType = guessFieldType(value);
        if (fieldType.isPresent()) {
            Document metadata = new Document();
            metadata.append(FIELDS_NAME_KEY, key);
            metadata.append(FIELDS_TYPE_KEY, fieldType.get().toString());
            metadata.append(FIELDS_HIDDEN_KEY, key.equals("_id") && fieldType.get().equals(OBJECT_ID.getTypeSignature()));
            builder.add(metadata);
        } else {
            log.debug("Unable to guess field type from %s : %s", value == null ? "null" : value.getClass().getName(), value);
        }
    }
    return builder.build();
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

TypeSignature (io.trino.spi.type.TypeSignature)78 Test (org.testng.annotations.Test)49 TypeSignatureTranslator.parseTypeSignature (io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature)35 ImmutableList (com.google.common.collect.ImmutableList)19 Signature (io.trino.metadata.Signature)17 NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)17 Type (io.trino.spi.type.Type)15 ArrayType (io.trino.spi.type.ArrayType)14 List (java.util.List)13 BoundSignature (io.trino.metadata.BoundSignature)11 Optional (java.util.Optional)10 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 ImmutableSet (com.google.common.collect.ImmutableSet)9 TrinoException (io.trino.spi.TrinoException)9 DecimalType (io.trino.spi.type.DecimalType)9 TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)9 Objects.requireNonNull (java.util.Objects.requireNonNull)9 BIGINT (io.trino.spi.type.BigintType.BIGINT)8 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)7 ADD (io.trino.spi.function.OperatorType.ADD)7