Search in sources :

Example 1 with ColumnInfo

use of com.facebook.presto.raptor.metadata.ColumnInfo in project presto by prestodb.

the class RaptorMetadata method finishCreateTable.

@Override
public Optional<ConnectorOutputMetadata> finishCreateTable(ConnectorSession session, ConnectorOutputTableHandle outputTableHandle, Collection<Slice> fragments) {
    RaptorOutputTableHandle table = (RaptorOutputTableHandle) outputTableHandle;
    long transactionId = table.getTransactionId();
    long updateTime = session.getStartTime();
    long newTableId = runTransaction(dbi, (dbiHandle, status) -> {
        MetadataDao dao = dbiHandle.attach(MetadataDao.class);
        Long distributionId = table.getDistributionId().isPresent() ? table.getDistributionId().getAsLong() : null;
        // TODO: update default value of organization_enabled to true
        long tableId = dao.insertTable(table.getSchemaName(), table.getTableName(), true, table.isOrganized(), distributionId, updateTime);
        List<RaptorColumnHandle> sortColumnHandles = table.getSortColumnHandles();
        List<RaptorColumnHandle> bucketColumnHandles = table.getBucketColumnHandles();
        for (int i = 0; i < table.getColumnTypes().size(); i++) {
            RaptorColumnHandle column = table.getColumnHandles().get(i);
            int columnId = i + 1;
            String type = table.getColumnTypes().get(i).getTypeSignature().toString();
            Integer sortPosition = sortColumnHandles.contains(column) ? sortColumnHandles.indexOf(column) : null;
            Integer bucketPosition = bucketColumnHandles.contains(column) ? bucketColumnHandles.indexOf(column) : null;
            dao.insertColumn(tableId, columnId, column.getColumnName(), i, type, sortPosition, bucketPosition);
            if (table.getTemporalColumnHandle().isPresent() && table.getTemporalColumnHandle().get().equals(column)) {
                dao.updateTemporalColumnId(tableId, columnId);
            }
        }
        return tableId;
    });
    List<ColumnInfo> columns = table.getColumnHandles().stream().map(ColumnInfo::fromHandle).collect(toList());
    OptionalLong temporalColumnId = table.getTemporalColumnHandle().map(RaptorColumnHandle::getColumnId).map(OptionalLong::of).orElse(OptionalLong.empty());
    // TODO: refactor this to avoid creating an empty table on failure
    shardManager.createTable(newTableId, columns, table.getBucketCount().isPresent(), temporalColumnId);
    shardManager.commitShards(transactionId, newTableId, columns, parseFragments(fragments), Optional.empty(), updateTime);
    clearRollback();
    return Optional.empty();
}
Also used : OptionalLong(java.util.OptionalLong) ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) OptionalLong(java.util.OptionalLong) Constraint(com.facebook.presto.spi.Constraint) MetadataDao(com.facebook.presto.raptor.metadata.MetadataDao)

Example 2 with ColumnInfo

use of com.facebook.presto.raptor.metadata.ColumnInfo in project presto by prestodb.

the class OrcStorageManager method getColumnInfoFromOrcColumnTypes.

private List<ColumnInfo> getColumnInfoFromOrcColumnTypes(List<String> orcColumnNames, List<OrcType> orcColumnTypes) {
    Type rowType = getType(orcColumnTypes, 0);
    if (orcColumnNames.size() != rowType.getTypeParameters().size()) {
        throw new PrestoException(RAPTOR_ERROR, "Column names and types do not match");
    }
    ImmutableList.Builder<ColumnInfo> list = ImmutableList.builder();
    for (int i = 0; i < orcColumnNames.size(); i++) {
        list.add(new ColumnInfo(Long.parseLong(orcColumnNames.get(i)), rowType.getTypeParameters().get(i)));
    }
    return list.build();
}
Also used : Type(com.facebook.presto.spi.type.Type) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) DecimalType(com.facebook.presto.spi.type.DecimalType) OrcType(com.facebook.presto.orc.metadata.OrcType) ImmutableList(com.google.common.collect.ImmutableList) ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) PrestoException(com.facebook.presto.spi.PrestoException)

Example 3 with ColumnInfo

use of com.facebook.presto.raptor.metadata.ColumnInfo in project presto by prestodb.

the class TestShardMetadataRecordCursor method testSimple.

@Test
public void testSimple() throws Exception {
    ShardManager shardManager = createShardManager(dbi);
    // Add shards to the table
    long tableId = 1;
    OptionalInt bucketNumber = OptionalInt.empty();
    UUID uuid1 = UUID.randomUUID();
    UUID uuid2 = UUID.randomUUID();
    UUID uuid3 = UUID.randomUUID();
    ShardInfo shardInfo1 = new ShardInfo(uuid1, bucketNumber, ImmutableSet.of("node1"), ImmutableList.of(), 1, 10, 100);
    ShardInfo shardInfo2 = new ShardInfo(uuid2, bucketNumber, ImmutableSet.of("node2"), ImmutableList.of(), 2, 20, 200);
    ShardInfo shardInfo3 = new ShardInfo(uuid3, bucketNumber, ImmutableSet.of("node3"), ImmutableList.of(), 3, 30, 300);
    List<ShardInfo> shards = ImmutableList.of(shardInfo1, shardInfo2, shardInfo3);
    long transactionId = shardManager.beginTransaction();
    shardManager.commitShards(transactionId, tableId, ImmutableList.of(new ColumnInfo(1, BIGINT), new ColumnInfo(2, DATE)), shards, Optional.empty(), 0);
    Slice schema = utf8Slice(DEFAULT_TEST_ORDERS.getSchemaName());
    Slice table = utf8Slice(DEFAULT_TEST_ORDERS.getTableName());
    DateTime date1 = DateTime.parse("2015-01-01T00:00");
    DateTime date2 = DateTime.parse("2015-01-02T00:00");
    TupleDomain<Integer> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.<Integer, Domain>builder().put(0, Domain.singleValue(createVarcharType(10), schema)).put(1, Domain.create(ValueSet.ofRanges(lessThanOrEqual(createVarcharType(10), table)), true)).put(6, Domain.create(ValueSet.ofRanges(lessThanOrEqual(BIGINT, date1.getMillis()), greaterThan(BIGINT, date2.getMillis())), true)).put(7, Domain.create(ValueSet.ofRanges(lessThanOrEqual(BIGINT, date1.getMillis()), greaterThan(BIGINT, date2.getMillis())), true)).build());
    List<MaterializedRow> actual;
    try (RecordCursor cursor = new ShardMetadataSystemTable(dbi).cursor(null, SESSION, tupleDomain)) {
        actual = getMaterializedResults(cursor, SHARD_METADATA.getColumns());
    }
    assertEquals(actual.size(), 3);
    List<MaterializedRow> expected = ImmutableList.of(new MaterializedRow(DEFAULT_PRECISION, schema, table, utf8Slice(uuid1.toString()), null, 100L, 10L, 1L, null, null), new MaterializedRow(DEFAULT_PRECISION, schema, table, utf8Slice(uuid2.toString()), null, 200L, 20L, 2L, null, null), new MaterializedRow(DEFAULT_PRECISION, schema, table, utf8Slice(uuid3.toString()), null, 300L, 30L, 3L, null, null));
    assertEquals(actual, expected);
}
Also used : RecordCursor(com.facebook.presto.spi.RecordCursor) ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) ShardManager(com.facebook.presto.raptor.metadata.ShardManager) TestDatabaseShardManager.createShardManager(com.facebook.presto.raptor.metadata.TestDatabaseShardManager.createShardManager) OptionalInt(java.util.OptionalInt) DateTime(org.joda.time.DateTime) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) UUID(java.util.UUID) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) MaterializedRow(com.facebook.presto.testing.MaterializedRow) ShardInfo(com.facebook.presto.raptor.metadata.ShardInfo) Test(org.testng.annotations.Test)

Example 4 with ColumnInfo

use of com.facebook.presto.raptor.metadata.ColumnInfo in project presto by prestodb.

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.
    // TODO: This needs to be updated when we support dropping columns.
    List<TableColumn> existingColumns = dao.listTableColumns(table.getSchemaName(), table.getTableName());
    TableColumn lastColumn = existingColumns.get(existingColumns.size() - 1);
    long columnId = lastColumn.getColumnId() + 1;
    int ordinalPosition = existingColumns.size();
    String type = column.getType().getTypeSignature().toString();
    daoTransaction(dbi, MetadataDao.class, dao -> {
        dao.insertColumn(table.getTableId(), columnId, column.getName(), ordinalPosition, type, null, null);
        dao.updateTableVersion(table.getTableId(), session.getStartTime());
    });
    shardManager.addColumn(table.getTableId(), new ColumnInfo(columnId, column.getType()));
}
Also used : ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) TableColumn(com.facebook.presto.raptor.metadata.TableColumn) Constraint(com.facebook.presto.spi.Constraint)

Example 5 with ColumnInfo

use of com.facebook.presto.raptor.metadata.ColumnInfo in project presto by prestodb.

the class RaptorMetadata method finishDelete.

@Override
public void finishDelete(ConnectorSession session, ConnectorTableHandle tableHandle, Collection<Slice> fragments) {
    RaptorTableHandle table = (RaptorTableHandle) tableHandle;
    long transactionId = table.getTransactionId().getAsLong();
    long tableId = table.getTableId();
    List<ColumnInfo> columns = getColumnHandles(session, tableHandle).values().stream().map(RaptorColumnHandle.class::cast).map(ColumnInfo::fromHandle).collect(toList());
    ImmutableSet.Builder<UUID> oldShardUuidsBuilder = ImmutableSet.builder();
    ImmutableList.Builder<ShardInfo> newShardsBuilder = ImmutableList.builder();
    fragments.stream().map(fragment -> SHARD_DELTA_CODEC.fromJson(fragment.getBytes())).forEach(delta -> {
        oldShardUuidsBuilder.addAll(delta.getOldShardUuids());
        newShardsBuilder.addAll(delta.getNewShards());
    });
    Set<UUID> oldShardUuids = oldShardUuidsBuilder.build();
    List<ShardInfo> newShards = newShardsBuilder.build();
    OptionalLong updateTime = OptionalLong.of(session.getStartTime());
    log.info("Finishing delete for tableId %s (removed: %s, rewritten: %s)", tableId, oldShardUuids.size() - newShards.size(), newShards.size());
    shardManager.replaceShardUuids(transactionId, tableId, columns, oldShardUuids, newShards, updateTime);
    clearRollback();
}
Also used : SHARD_UUID_COLUMN_TYPE(com.facebook.presto.raptor.RaptorColumnHandle.SHARD_UUID_COLUMN_TYPE) NOT_FOUND(com.facebook.presto.spi.StandardErrorCode.NOT_FOUND) DatabaseUtil.runIgnoringConstraintViolation(com.facebook.presto.raptor.util.DatabaseUtil.runIgnoringConstraintViolation) Map(java.util.Map) ShardManager(com.facebook.presto.raptor.metadata.ShardManager) INVALID_TABLE_PROPERTY(com.facebook.presto.spi.StandardErrorCode.INVALID_TABLE_PROPERTY) RaptorTableProperties.getSortColumns(com.facebook.presto.raptor.RaptorTableProperties.getSortColumns) ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) ViewResult(com.facebook.presto.raptor.metadata.ViewResult) Set(java.util.Set) ConnectorSession(com.facebook.presto.spi.ConnectorSession) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) RaptorColumnHandle.shardUuidColumnHandle(com.facebook.presto.raptor.RaptorColumnHandle.shardUuidColumnHandle) ConnectorPartitioningHandle(com.facebook.presto.spi.connector.ConnectorPartitioningHandle) Slice(io.airlift.slice.Slice) ConnectorOutputTableHandle(com.facebook.presto.spi.ConnectorOutputTableHandle) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) Type(com.facebook.presto.spi.type.Type) BUCKET_NUMBER_COLUMN_NAME(com.facebook.presto.raptor.RaptorColumnHandle.BUCKET_NUMBER_COLUMN_NAME) RaptorSessionProperties.getExternalBatchId(com.facebook.presto.raptor.RaptorSessionProperties.getExternalBatchId) TIMESTAMP(com.facebook.presto.spi.type.TimestampType.TIMESTAMP) DatabaseUtil.daoTransaction(com.facebook.presto.raptor.util.DatabaseUtil.daoTransaction) Nullable(javax.annotation.Nullable) RaptorTableProperties.isOrganized(com.facebook.presto.raptor.RaptorTableProperties.isOrganized) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Distribution(com.facebook.presto.raptor.metadata.Distribution) BUCKETED_ON_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.BUCKETED_ON_PROPERTY) ORDERING_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.ORDERING_PROPERTY) RaptorSessionProperties.getOneSplitPerBucketThreshold(com.facebook.presto.raptor.RaptorSessionProperties.getOneSplitPerBucketThreshold) ColumnIdentity(com.facebook.presto.spi.ColumnIdentity) Constraint(com.facebook.presto.spi.Constraint) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) RaptorTableProperties.getBucketColumns(com.facebook.presto.raptor.RaptorTableProperties.getBucketColumns) MetadataDao(com.facebook.presto.raptor.metadata.MetadataDao) JsonCodec.jsonCodec(io.airlift.json.JsonCodec.jsonCodec) ConnectorTableLayoutResult(com.facebook.presto.spi.ConnectorTableLayoutResult) RAPTOR_ERROR(com.facebook.presto.raptor.RaptorErrorCode.RAPTOR_ERROR) SchemaTablePrefix(com.facebook.presto.spi.SchemaTablePrefix) TreeMap(java.util.TreeMap) ColumnHandle(com.facebook.presto.spi.ColumnHandle) ConnectorNewTableLayout(com.facebook.presto.spi.ConnectorNewTableLayout) ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition) BUCKET_COUNT_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.BUCKET_COUNT_PROPERTY) ViewNotFoundException(com.facebook.presto.spi.ViewNotFoundException) ShardDelta(com.facebook.presto.raptor.metadata.ShardDelta) IDBI(org.skife.jdbi.v2.IDBI) RaptorBucketFunction.validateBucketType(com.facebook.presto.raptor.RaptorBucketFunction.validateBucketType) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) TableColumn(com.facebook.presto.raptor.metadata.TableColumn) DatabaseUtil.runTransaction(com.facebook.presto.raptor.util.DatabaseUtil.runTransaction) ImmutableSet(com.google.common.collect.ImmutableSet) RaptorTableProperties.getDistributionName(com.facebook.presto.raptor.RaptorTableProperties.getDistributionName) ImmutableMap(com.google.common.collect.ImmutableMap) DISTRIBUTION_NAME_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.DISTRIBUTION_NAME_PROPERTY) Collections.nCopies(java.util.Collections.nCopies) Collection(java.util.Collection) RaptorTableProperties.getTemporalColumn(com.facebook.presto.raptor.RaptorTableProperties.getTemporalColumn) UUID(java.util.UUID) RaptorColumnHandle.isHiddenColumn(com.facebook.presto.raptor.RaptorColumnHandle.isHiddenColumn) RaptorColumnHandle.bucketNumberColumnHandle(com.facebook.presto.raptor.RaptorColumnHandle.bucketNumberColumnHandle) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) List(java.util.List) RaptorTableProperties.getBucketCount(com.facebook.presto.raptor.RaptorTableProperties.getBucketCount) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) TEMPORAL_COLUMN_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.TEMPORAL_COLUMN_PROPERTY) Optional(java.util.Optional) INTEGER(com.facebook.presto.spi.type.IntegerType.INTEGER) SortedMap(java.util.SortedMap) JsonCodec(io.airlift.json.JsonCodec) ConnectorMetadata(com.facebook.presto.spi.connector.ConnectorMetadata) ORGANIZED_PROPERTY(com.facebook.presto.raptor.RaptorTableProperties.ORGANIZED_PROPERTY) Logger(io.airlift.log.Logger) ConnectorTableLayoutHandle(com.facebook.presto.spi.ConnectorTableLayoutHandle) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle) PrestoException(com.facebook.presto.spi.PrestoException) OptionalInt(java.util.OptionalInt) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors.toCollection(java.util.stream.Collectors.toCollection) ImmutableList(com.google.common.collect.ImmutableList) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) ASC_NULLS_FIRST(com.facebook.presto.spi.block.SortOrder.ASC_NULLS_FIRST) SHARD_UUID_COLUMN_NAME(com.facebook.presto.raptor.RaptorColumnHandle.SHARD_UUID_COLUMN_NAME) Objects.requireNonNull(java.util.Objects.requireNonNull) DatabaseUtil.onDemandDao(com.facebook.presto.raptor.util.DatabaseUtil.onDemandDao) ConnectorNodePartitioning(com.facebook.presto.spi.ConnectorNodePartitioning) Table(com.facebook.presto.raptor.metadata.Table) ConnectorInsertTableHandle(com.facebook.presto.spi.ConnectorInsertTableHandle) ConnectorOutputMetadata(com.facebook.presto.spi.connector.ConnectorOutputMetadata) ConnectorTableLayout(com.facebook.presto.spi.ConnectorTableLayout) Maps(com.google.common.collect.Maps) ShardInfo(com.facebook.presto.raptor.metadata.ShardInfo) LongConsumer(java.util.function.LongConsumer) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) DATE(com.facebook.presto.spi.type.DateType.DATE) RaptorColumnHandle.shardRowIdHandle(com.facebook.presto.raptor.RaptorColumnHandle.shardRowIdHandle) ImmutableList(com.google.common.collect.ImmutableList) ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) ImmutableSet(com.google.common.collect.ImmutableSet) OptionalLong(java.util.OptionalLong) UUID(java.util.UUID) ShardInfo(com.facebook.presto.raptor.metadata.ShardInfo)

Aggregations

ColumnInfo (com.facebook.presto.raptor.metadata.ColumnInfo)8 MetadataDao (com.facebook.presto.raptor.metadata.MetadataDao)3 ShardInfo (com.facebook.presto.raptor.metadata.ShardInfo)3 Constraint (com.facebook.presto.spi.Constraint)3 PrestoException (com.facebook.presto.spi.PrestoException)3 ImmutableList (com.google.common.collect.ImmutableList)3 UUID (java.util.UUID)3 ShardManager (com.facebook.presto.raptor.metadata.ShardManager)2 TableColumn (com.facebook.presto.raptor.metadata.TableColumn)2 FileOrcDataSource (com.facebook.presto.orc.FileOrcDataSource)1 OrcDataSource (com.facebook.presto.orc.OrcDataSource)1 OrcReader (com.facebook.presto.orc.OrcReader)1 OrcMetadataReader (com.facebook.presto.orc.metadata.OrcMetadataReader)1 OrcType (com.facebook.presto.orc.metadata.OrcType)1 RaptorBucketFunction.validateBucketType (com.facebook.presto.raptor.RaptorBucketFunction.validateBucketType)1 BUCKET_NUMBER_COLUMN_NAME (com.facebook.presto.raptor.RaptorColumnHandle.BUCKET_NUMBER_COLUMN_NAME)1 SHARD_UUID_COLUMN_NAME (com.facebook.presto.raptor.RaptorColumnHandle.SHARD_UUID_COLUMN_NAME)1 SHARD_UUID_COLUMN_TYPE (com.facebook.presto.raptor.RaptorColumnHandle.SHARD_UUID_COLUMN_TYPE)1 RaptorColumnHandle.bucketNumberColumnHandle (com.facebook.presto.raptor.RaptorColumnHandle.bucketNumberColumnHandle)1 RaptorColumnHandle.isHiddenColumn (com.facebook.presto.raptor.RaptorColumnHandle.isHiddenColumn)1