Search in sources :

Example 31 with RowType

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

the class CheckpointWriter method writeObjectMapAsFields.

private void writeObjectMapAsFields(BlockBuilder blockBuilder, RowType type, int fieldId, String fieldName, Optional<Map<String, Object>> values) {
    RowType.Field valuesField = validateAndGetField(type, fieldId, fieldName);
    RowType valuesFieldType = (RowType) valuesField.getType();
    BlockBuilder fieldBlockBuilder = blockBuilder.beginBlockEntry();
    if (values.isEmpty()) {
        blockBuilder.appendNull();
    } else {
        for (RowType.Field valueField : valuesFieldType.getFields()) {
            // anonymous row fields are not expected here
            Object value = values.get().get(valueField.getName().orElseThrow());
            if (valueField.getType() instanceof RowType) {
                Block rowBlock = (Block) value;
                checkState(rowBlock.getPositionCount() == 1, "Invalid RowType statistics for writing Delta Lake checkpoint");
                if (rowBlock.isNull(0)) {
                    fieldBlockBuilder.appendNull();
                } else {
                    valueField.getType().appendTo(rowBlock, 0, fieldBlockBuilder);
                }
            } else {
                writeNativeValue(valueField.getType(), fieldBlockBuilder, value);
            }
        }
    }
    blockBuilder.closeEntry();
}
Also used : RowType(io.trino.spi.type.RowType) Block(io.trino.spi.block.Block) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 32 with RowType

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

the class CheckpointWriter method writeStringList.

private void writeStringList(BlockBuilder blockBuilder, RowType type, int fieldId, String fieldName, @Nullable List<String> values) {
    RowType.Field field = validateAndGetField(type, fieldId, fieldName);
    checkArgument(field.getType() instanceof ArrayType, "Expected field %s/%s to by of ArrayType but got %s", fieldId, fieldName, field.getType());
    if (values == null) {
        blockBuilder.appendNull();
        return;
    }
    ArrayType arrayType = (ArrayType) field.getType();
    BlockBuilder mapBuilder = blockBuilder.beginBlockEntry();
    for (String value : values) {
        if (value == null) {
            mapBuilder.appendNull();
        } else {
            arrayType.getElementType().writeSlice(mapBuilder, utf8Slice(value));
        }
    }
    blockBuilder.closeEntry();
}
Also used : ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 33 with RowType

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

the class CheckpointWriter method write.

public void write(ConnectorSession session, CheckpointEntries entries, Path targetPath) {
    RowType metadataEntryType = checkpointSchemaManager.getMetadataEntryType();
    RowType protocolEntryType = checkpointSchemaManager.getProtocolEntryType();
    RowType txnEntryType = checkpointSchemaManager.getTxnEntryType();
    RowType addEntryType = checkpointSchemaManager.getAddEntryType(entries.getMetadataEntry());
    RowType removeEntryType = checkpointSchemaManager.getRemoveEntryType();
    List<String> columnNames = ImmutableList.of("metaData", "protocol", "txn", "add", "remove");
    List<Type> columnTypes = ImmutableList.of(metadataEntryType, protocolEntryType, txnEntryType, addEntryType, removeEntryType);
    Properties schema = buildSchemaProperties(columnNames, columnTypes);
    Configuration conf = hdfsEnvironment.getConfiguration(new HdfsEnvironment.HdfsContext(session), targetPath);
    configureCompression(conf, SNAPPY);
    JobConf jobConf = toJobConf(conf);
    RecordFileWriter writer = new RecordFileWriter(targetPath, columnNames, fromHiveStorageFormat(PARQUET), schema, PARQUET.getEstimatedWriterMemoryUsage(), jobConf, typeManager, DateTimeZone.UTC, session);
    PageBuilder pageBuilder = new PageBuilder(columnTypes);
    writeMetadataEntry(pageBuilder, metadataEntryType, entries.getMetadataEntry());
    writeProtocolEntry(pageBuilder, protocolEntryType, entries.getProtocolEntry());
    for (TransactionEntry transactionEntry : entries.getTransactionEntries()) {
        writeTransactionEntry(pageBuilder, txnEntryType, transactionEntry);
    }
    for (AddFileEntry addFileEntry : entries.getAddFileEntries()) {
        writeAddFileEntry(pageBuilder, addEntryType, addFileEntry);
    }
    for (RemoveFileEntry removeFileEntry : entries.getRemoveFileEntries()) {
        writeRemoveFileEntry(pageBuilder, removeEntryType, removeFileEntry);
    }
    // Not writing commit infos for now. DB does not keep them in the checkpoints by default
    writer.appendRows(pageBuilder.build());
    writer.commit();
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) RowType(io.trino.spi.type.RowType) PageBuilder(io.trino.spi.PageBuilder) Properties(java.util.Properties) HdfsEnvironment(io.trino.plugin.hive.HdfsEnvironment) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) HiveType(io.trino.plugin.hive.HiveType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) RecordFileWriter(io.trino.plugin.hive.RecordFileWriter) AddFileEntry(io.trino.plugin.deltalake.transactionlog.AddFileEntry) RemoveFileEntry(io.trino.plugin.deltalake.transactionlog.RemoveFileEntry) ConfigurationUtils.toJobConf(io.trino.plugin.hive.util.ConfigurationUtils.toJobConf) JobConf(org.apache.hadoop.mapred.JobConf) TransactionEntry(io.trino.plugin.deltalake.transactionlog.TransactionEntry)

Example 34 with RowType

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

the class CheckpointWriter method writeMetadataEntry.

private void writeMetadataEntry(PageBuilder pageBuilder, RowType entryType, MetadataEntry metadataEntry) {
    pageBuilder.declarePosition();
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(METADATA_BLOCK_CHANNEL);
    BlockBuilder entryBlockBuilder = blockBuilder.beginBlockEntry();
    writeString(entryBlockBuilder, entryType, 0, "id", metadataEntry.getId());
    writeString(entryBlockBuilder, entryType, 1, "name", metadataEntry.getName());
    writeString(entryBlockBuilder, entryType, 2, "description", metadataEntry.getDescription());
    RowType formatType = getInternalRowType(entryType, 3, "format");
    BlockBuilder formatBlockBuilder = entryBlockBuilder.beginBlockEntry();
    writeString(formatBlockBuilder, formatType, 0, "provider", metadataEntry.getFormat().getProvider());
    writeStringMap(formatBlockBuilder, formatType, 1, "options", metadataEntry.getFormat().getOptions());
    entryBlockBuilder.closeEntry();
    writeString(entryBlockBuilder, entryType, 4, "schemaString", metadataEntry.getSchemaString());
    writeStringList(entryBlockBuilder, entryType, 5, "partitionColumns", metadataEntry.getOriginalPartitionColumns());
    writeStringMap(entryBlockBuilder, entryType, 6, "configuration", metadataEntry.getConfiguration());
    writeLong(entryBlockBuilder, entryType, 7, "createdTime", metadataEntry.getCreatedTime());
    blockBuilder.closeEntry();
    // null for others
    appendNullOtherBlocks(pageBuilder, METADATA_BLOCK_CHANNEL);
}
Also used : RowType(io.trino.spi.type.RowType) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 35 with RowType

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

the class CheckpointEntryIterator method readNullCount.

private Map<String, Object> readNullCount(Block block, int blockPosition, List<ColumnMetadata> columns) {
    if (block.isNull(blockPosition)) {
        // Statistics were not collected
        return ImmutableMap.of();
    }
    Block valuesBlock = block.getObject(blockPosition, Block.class);
    ImmutableMap.Builder<String, Object> values = ImmutableMap.builder();
    for (int i = 0; i < columns.size(); i++) {
        ColumnMetadata metadata = columns.get(i);
        if (valuesBlock.isNull(i)) {
            continue;
        }
        if (metadata.getType() instanceof RowType) {
            if (checkpointRowStatisticsWritingEnabled) {
                // RowType column statistics are not used for query planning, but need to be copied when writing out new Checkpoint files.
                values.put(metadata.getName(), valuesBlock.getSingleValueBlock(i));
            }
            continue;
        }
        values.put(metadata.getName(), getLong(valuesBlock, i));
    }
    return values.buildOrThrow();
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Block(io.trino.spi.block.Block) RowType(io.trino.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

RowType (io.trino.spi.type.RowType)90 ArrayType (io.trino.spi.type.ArrayType)51 MapType (io.trino.spi.type.MapType)42 Type (io.trino.spi.type.Type)42 ImmutableList (com.google.common.collect.ImmutableList)30 VarcharType (io.trino.spi.type.VarcharType)28 BlockBuilder (io.trino.spi.block.BlockBuilder)26 DecimalType (io.trino.spi.type.DecimalType)21 Test (org.testng.annotations.Test)21 ImmutableMap (com.google.common.collect.ImmutableMap)20 Block (io.trino.spi.block.Block)20 List (java.util.List)20 Map (java.util.Map)18 ArrayList (java.util.ArrayList)17 Optional (java.util.Optional)17 CharType (io.trino.spi.type.CharType)16 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)15 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)12 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)12 HashMap (java.util.HashMap)12