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();
}
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);
}
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());
}
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);
}
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()));
}
Aggregations