Search in sources :

Example 6 with TableColumn

use of io.trino.plugin.raptor.legacy.metadata.TableColumn in project trino by trinodb.

the class ShardOrganizerUtil method getOrganizationEligibleShards.

public static Collection<ShardIndexInfo> getOrganizationEligibleShards(Jdbi dbi, MetadataDao metadataDao, Table tableInfo, Collection<ShardMetadata> shards, boolean includeSortColumns) {
    Map<Long, ShardMetadata> shardsById = uniqueIndex(shards, ShardMetadata::getShardId);
    long tableId = tableInfo.getTableId();
    ImmutableList.Builder<String> columnsBuilder = ImmutableList.builder();
    columnsBuilder.add("shard_id");
    // include temporal columns if present
    Optional<TableColumn> temporalColumn = Optional.empty();
    if (tableInfo.getTemporalColumnId().isPresent()) {
        long temporalColumnId = tableInfo.getTemporalColumnId().getAsLong();
        temporalColumn = Optional.of(metadataDao.getTableColumn(tableId, temporalColumnId));
        columnsBuilder.add(minColumn(temporalColumnId), maxColumn(temporalColumnId));
    }
    // include sort columns if needed
    Optional<List<TableColumn>> sortColumns = Optional.empty();
    if (includeSortColumns) {
        sortColumns = Optional.of(metadataDao.listSortColumns(tableId));
        for (TableColumn column : sortColumns.get()) {
            columnsBuilder.add(minColumn(column.getColumnId()), maxColumn(column.getColumnId()));
        }
    }
    String columnToSelect = Joiner.on(",\n").join(columnsBuilder.build());
    ImmutableList.Builder<ShardIndexInfo> indexInfoBuilder = ImmutableList.builder();
    try (Connection connection = dbi.open().getConnection()) {
        for (List<ShardMetadata> partitionedShards : partition(shards, 1000)) {
            String shardIds = Joiner.on(",").join(nCopies(partitionedShards.size(), "?"));
            String sql = format("" + "SELECT %s\n" + "FROM %s\n" + "WHERE shard_id IN (%s)", columnToSelect, shardIndexTable(tableId), shardIds);
            try (PreparedStatement statement = connection.prepareStatement(sql)) {
                for (int i = 0; i < partitionedShards.size(); i++) {
                    statement.setLong(i + 1, partitionedShards.get(i).getShardId());
                }
                try (ResultSet resultSet = statement.executeQuery()) {
                    while (resultSet.next()) {
                        long shardId = resultSet.getLong("shard_id");
                        Optional<ShardRange> sortRange = Optional.empty();
                        if (includeSortColumns) {
                            sortRange = getShardRange(sortColumns.get(), resultSet);
                            if (sortRange.isEmpty()) {
                                continue;
                            }
                        }
                        Optional<ShardRange> temporalRange = Optional.empty();
                        if (temporalColumn.isPresent()) {
                            temporalRange = getShardRange(ImmutableList.of(temporalColumn.get()), resultSet);
                            if (temporalRange.isEmpty()) {
                                continue;
                            }
                        }
                        ShardMetadata shardMetadata = shardsById.get(shardId);
                        indexInfoBuilder.add(toShardIndexInfo(shardMetadata, temporalRange, sortRange));
                    }
                }
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    return indexInfoBuilder.build();
}
Also used : SQLException(java.sql.SQLException) ImmutableList(com.google.common.collect.ImmutableList) ShardMetadata(io.trino.plugin.raptor.legacy.metadata.ShardMetadata) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TableColumn(io.trino.plugin.raptor.legacy.metadata.TableColumn) ResultSet(java.sql.ResultSet) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 7 with TableColumn

use of io.trino.plugin.raptor.legacy.metadata.TableColumn in project trino by trinodb.

the class ShardCompactionManager method filterAndCreateCompactionSets.

private Collection<OrganizationSet> filterAndCreateCompactionSets(long tableId, Collection<ShardMetadata> tableShards) {
    Table tableInfo = metadataDao.getTableInformation(tableId);
    OptionalLong temporalColumnId = tableInfo.getTemporalColumnId();
    if (temporalColumnId.isPresent()) {
        TableColumn tableColumn = metadataDao.getTableColumn(tableId, temporalColumnId.getAsLong());
        if (!isValidTemporalColumn(tableId, tableColumn.getDataType())) {
            return ImmutableSet.of();
        }
    }
    Set<ShardMetadata> filteredShards = tableShards.stream().filter(this::needsCompaction).filter(shard -> !organizer.inProgress(shard.getShardUuid())).collect(toSet());
    Collection<ShardIndexInfo> shardIndexInfos = getOrganizationEligibleShards(dbi, metadataDao, tableInfo, filteredShards, false);
    if (tableInfo.getTemporalColumnId().isPresent()) {
        Set<ShardIndexInfo> temporalShards = shardIndexInfos.stream().filter(shard -> shard.getTemporalRange().isPresent()).collect(toSet());
        return compactionSetCreator.createCompactionSets(tableInfo, temporalShards);
    }
    return compactionSetCreator.createCompactionSets(tableInfo, shardIndexInfos);
}
Also used : ShardOrganizerUtil.getOrganizationEligibleShards(io.trino.plugin.raptor.legacy.storage.organization.ShardOrganizerUtil.getOrganizationEligibleShards) ListMultimap(com.google.common.collect.ListMultimap) Logger(io.airlift.log.Logger) TIMESTAMP_MILLIS(io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS) NodeManager(io.trino.spi.NodeManager) Type(io.trino.spi.type.Type) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Multimaps(com.google.common.collect.Multimaps) Duration(io.airlift.units.Duration) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) OptionalLong(java.util.OptionalLong) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ForMetadata(io.trino.plugin.raptor.legacy.metadata.ForMetadata) Executors.newScheduledThreadPool(java.util.concurrent.Executors.newScheduledThreadPool) Threads.daemonThreadsNamed(io.airlift.concurrent.Threads.daemonThreadsNamed) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MetadataDao(io.trino.plugin.raptor.legacy.metadata.MetadataDao) Collectors.toSet(java.util.stream.Collectors.toSet) Jdbi(org.jdbi.v3.core.Jdbi) ImmutableSet(com.google.common.collect.ImmutableSet) StorageManagerConfig(io.trino.plugin.raptor.legacy.storage.StorageManagerConfig) Collection(java.util.Collection) ShardMetadata(io.trino.plugin.raptor.legacy.metadata.ShardMetadata) Set(java.util.Set) TableColumn(io.trino.plugin.raptor.legacy.metadata.TableColumn) DatabaseUtil.onDemandDao(io.trino.plugin.raptor.legacy.util.DatabaseUtil.onDemandDao) Table(io.trino.plugin.raptor.legacy.metadata.Table) TimeUnit(java.util.concurrent.TimeUnit) DataSize(io.airlift.units.DataSize) List(java.util.List) ShardManager(io.trino.plugin.raptor.legacy.metadata.ShardManager) PostConstruct(javax.annotation.PostConstruct) Entry(java.util.Map.Entry) DATE(io.trino.spi.type.DateType.DATE) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Table(io.trino.plugin.raptor.legacy.metadata.Table) ShardMetadata(io.trino.plugin.raptor.legacy.metadata.ShardMetadata) OptionalLong(java.util.OptionalLong) TableColumn(io.trino.plugin.raptor.legacy.metadata.TableColumn)

Example 8 with TableColumn

use of io.trino.plugin.raptor.legacy.metadata.TableColumn in project trino by trinodb.

the class RaptorMetadata method listTableColumns.

@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix) {
    requireNonNull(prefix, "prefix is null");
    ImmutableListMultimap.Builder<SchemaTableName, ColumnMetadata> columns = ImmutableListMultimap.builder();
    for (TableColumn tableColumn : dao.listTableColumns(prefix.getSchema().orElse(null), prefix.getTable().orElse(null))) {
        ColumnMetadata columnMetadata = new ColumnMetadata(tableColumn.getColumnName(), tableColumn.getDataType());
        columns.put(tableColumn.getTable(), columnMetadata);
    }
    return Multimaps.asMap(columns.build());
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) SchemaTableName(io.trino.spi.connector.SchemaTableName) TableColumn(io.trino.plugin.raptor.legacy.metadata.TableColumn)

Example 9 with TableColumn

use of io.trino.plugin.raptor.legacy.metadata.TableColumn in project trino by trinodb.

the class RaptorMetadata method getTableHandle.

private RaptorTableHandle getTableHandle(SchemaTableName tableName) {
    requireNonNull(tableName, "tableName is null");
    Table table = dao.getTableInformation(tableName.getSchemaName(), tableName.getTableName());
    if (table == null) {
        return null;
    }
    List<TableColumn> tableColumns = dao.listTableColumns(table.getTableId());
    checkArgument(!tableColumns.isEmpty(), "Table '%s' does not have any columns", tableName);
    return new RaptorTableHandle(tableName.getSchemaName(), tableName.getTableName(), table.getTableId(), table.getDistributionId(), table.getDistributionName(), table.getBucketCount(), table.isOrganized(), OptionalLong.empty(), TupleDomain.all(), table.getDistributionId().map(shardManager::getBucketAssignments), false);
}
Also used : ColumnRangesSystemTable(io.trino.plugin.raptor.legacy.systemtables.ColumnRangesSystemTable) Table(io.trino.plugin.raptor.legacy.metadata.Table) ColumnRangesSystemTable.getSourceTable(io.trino.plugin.raptor.legacy.systemtables.ColumnRangesSystemTable.getSourceTable) SystemTable(io.trino.spi.connector.SystemTable) TableColumn(io.trino.plugin.raptor.legacy.metadata.TableColumn)

Example 10 with TableColumn

use of io.trino.plugin.raptor.legacy.metadata.TableColumn in project trino by trinodb.

the class RaptorMetadata method addColumn.

@Override
public void addColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnMetadata column) {
    RaptorTableHandle table = (RaptorTableHandle) tableHandle;
    // Always add new columns to the end.
    List<TableColumn> existingColumns = dao.listTableColumns(table.getSchemaName(), table.getTableName());
    TableColumn lastColumn = existingColumns.get(existingColumns.size() - 1);
    long columnId = lastColumn.getColumnId() + 1;
    int ordinalPosition = lastColumn.getOrdinalPosition() + 1;
    String type = column.getType().getTypeId().getId();
    daoTransaction(dbi, MetadataDao.class, dao -> {
        dao.insertColumn(table.getTableId(), columnId, column.getName(), ordinalPosition, type, null, null);
        dao.updateTableVersion(table.getTableId(), session.getStart().toEpochMilli());
    });
    shardManager.addColumn(table.getTableId(), new ColumnInfo(columnId, column.getType()));
}
Also used : ColumnInfo(io.trino.plugin.raptor.legacy.metadata.ColumnInfo) TableColumn(io.trino.plugin.raptor.legacy.metadata.TableColumn) Constraint(io.trino.spi.connector.Constraint)

Aggregations

TableColumn (io.trino.plugin.raptor.legacy.metadata.TableColumn)11 ImmutableList (com.google.common.collect.ImmutableList)4 Table (io.trino.plugin.raptor.legacy.metadata.Table)4 SchemaTableName (io.trino.spi.connector.SchemaTableName)4 Type (io.trino.spi.type.Type)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ShardInfo (io.trino.plugin.raptor.legacy.metadata.ShardInfo)3 ShardMetadata (io.trino.plugin.raptor.legacy.metadata.ShardMetadata)3 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)3 List (java.util.List)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Multimaps (com.google.common.collect.Multimaps)2 Logger (io.airlift.log.Logger)2 ColumnInfo (io.trino.plugin.raptor.legacy.metadata.ColumnInfo)2 ColumnStats (io.trino.plugin.raptor.legacy.metadata.ColumnStats)2 MetadataDao (io.trino.plugin.raptor.legacy.metadata.MetadataDao)2 ShardManager (io.trino.plugin.raptor.legacy.metadata.ShardManager)2 ColumnRangesSystemTable (io.trino.plugin.raptor.legacy.systemtables.ColumnRangesSystemTable)2