Search in sources :

Example 26 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class FileHiveMetastore method alterTable.

private void alterTable(String databaseName, String tableName, Function<TableMetadata, TableMetadata> alterFunction) {
    requireNonNull(databaseName, "databaseName is null");
    requireNonNull(tableName, "tableName is null");
    Path tableMetadataDirectory = getTableMetadataDirectory(databaseName, tableName);
    TableMetadata oldTableSchema = readSchemaFile("table", tableMetadataDirectory, tableCodec).orElseThrow(() -> new TableNotFoundException(new SchemaTableName(databaseName, tableName)));
    TableMetadata newTableSchema = alterFunction.apply(oldTableSchema);
    if (oldTableSchema == newTableSchema) {
        return;
    }
    writeSchemaFile("table", tableMetadataDirectory, tableCodec, newTableSchema, true);
}
Also used : Path(org.apache.hadoop.fs.Path) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 27 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class SemiTransactionalHiveMetastore method commitShared.

@GuardedBy("this")
private void commitShared() {
    checkHoldsLock();
    Committer committer = new Committer();
    try {
        for (Map.Entry<SchemaTableName, Action<TableAndMore>> entry : tableActions.entrySet()) {
            SchemaTableName schemaTableName = entry.getKey();
            Action<TableAndMore> action = entry.getValue();
            switch(action.getType()) {
                case DROP:
                    committer.prepareDropTable(schemaTableName);
                    break;
                case ALTER:
                    committer.prepareAlterTable();
                    break;
                case ADD:
                    committer.prepareAddTable(action.getUser(), action.getData());
                    break;
                case INSERT_EXISTING:
                    committer.prepareInsertExistingTable(action.getUser(), action.getData());
                    break;
                default:
                    throw new IllegalStateException("Unknown action type");
            }
        }
        for (Map.Entry<SchemaTableName, Map<List<String>, Action<PartitionAndMore>>> tableEntry : partitionActions.entrySet()) {
            SchemaTableName schemaTableName = tableEntry.getKey();
            for (Map.Entry<List<String>, Action<PartitionAndMore>> partitionEntry : tableEntry.getValue().entrySet()) {
                List<String> partitionValues = partitionEntry.getKey();
                Action<PartitionAndMore> action = partitionEntry.getValue();
                switch(action.getType()) {
                    case DROP:
                        committer.prepareDropPartition(schemaTableName, partitionValues);
                        break;
                    case ALTER:
                        committer.prepareAlterPartition(action.getQueryId(), action.getUser(), action.getData());
                        break;
                    case ADD:
                        committer.prepareAddPartition(action.getUser(), action.getData());
                        break;
                    case INSERT_EXISTING:
                        committer.prepareInsertExistingPartition(action.getUser(), action.getData());
                        break;
                    default:
                        throw new IllegalStateException("Unknown action type");
                }
            }
        }
        // Wait for all renames submitted for "INSERT_EXISTING" action to finish
        committer.waitForAsyncRenames();
        // At this point, all file system operations, whether asynchronously issued or not, have completed successfully.
        // We are moving on to metastore operations now.
        committer.executeAddTableOperations();
        committer.executeAlterPartitionOperations();
        committer.executeAddPartitionOperations();
    } catch (Throwable t) {
        committer.cancelUnstartedAsyncRenames();
        committer.undoAddPartitionOperations();
        committer.undoAddTableOperations();
        committer.waitForAsyncRenamesSuppressThrowables();
        // fileRenameFutures must all come back before any file system cleanups are carried out.
        // Otherwise, files that should be deleted may be created after cleanup is done.
        committer.executeCleanupTasksForAbort(committer.extractFilePrefixes(declaredIntentionsToWrite));
        committer.executeRenameTasksForAbort();
        // Partition directory must be put back before relevant metastore operation can be undone
        committer.undoAlterPartitionOperations();
        rollbackShared();
        throw t;
    }
    try {
        // After this line, operations are no longer reversible.
        // The next section will deal with "dropping table/partition". Commit may still fail in
        // this section. Even if commit fails, cleanups, instead of rollbacks, will be executed.
        committer.executeIrreversibleMetastoreOperations();
    // If control flow reached this point, this commit is considered successful no matter
    // what happens later. The only kind of operations that haven't been carried out yet
    // are cleanups.
    // The program control flow will go to finally next. And cleanup will run because
    // moveForwardInFinally has been set to false.
    } finally {
        // In this method, all operations are best-effort clean up operations.
        // If any operation fails, the error will be logged and ignored.
        // Additionally, other clean up operations should still be attempted.
        // Execute deletion tasks
        committer.executeDeletionTasksForFinish();
        // Clean up empty staging directories (that may recursively contain empty directories)
        committer.deleteEmptyStagingDirectories(declaredIntentionsToWrite);
    }
}
Also used : SchemaTableName(com.facebook.presto.spi.SchemaTableName) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 28 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class SemiTransactionalHiveMetastore method getTableSource.

/**
     * This method can only be called when the table is known to exist
     */
@GuardedBy("this")
private TableSource getTableSource(String databaseName, String tableName) {
    checkHoldsLock();
    checkReadable();
    Action<TableAndMore> tableAction = tableActions.get(new SchemaTableName(databaseName, tableName));
    if (tableAction == null) {
        return TableSource.PRE_EXISTING_TABLE;
    }
    switch(tableAction.getType()) {
        case ADD:
            return TableSource.CREATED_IN_THIS_TRANSACTION;
        case ALTER:
            throw new IllegalStateException("Tables are never altered in the current implementation");
        case DROP:
            throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));
        case INSERT_EXISTING:
            return TableSource.PRE_EXISTING_TABLE;
        default:
            throw new IllegalStateException("Unknown action type");
    }
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) SchemaTableName(com.facebook.presto.spi.SchemaTableName) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 29 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class SemiTransactionalHiveMetastore method getTable.

public synchronized Optional<Table> getTable(String databaseName, String tableName) {
    checkReadable();
    Action<TableAndMore> tableAction = tableActions.get(new SchemaTableName(databaseName, tableName));
    if (tableAction == null) {
        return delegate.getTable(databaseName, tableName);
    }
    switch(tableAction.getType()) {
        case ADD:
        case ALTER:
        case INSERT_EXISTING:
            return Optional.of(tableAction.getData().getTable());
        case DROP:
            return Optional.empty();
        default:
            throw new IllegalStateException("Unknown action type");
    }
}
Also used : SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 30 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class SemiTransactionalHiveMetastore method getPartition.

public synchronized Optional<Partition> getPartition(String databaseName, String tableName, List<String> partitionValues) {
    checkReadable();
    TableSource tableSource = getTableSource(databaseName, tableName);
    Map<List<String>, Action<PartitionAndMore>> partitionActionsOfTable = partitionActions.computeIfAbsent(new SchemaTableName(databaseName, tableName), k -> new HashMap<>());
    Action<PartitionAndMore> partitionAction = partitionActionsOfTable.get(partitionValues);
    if (partitionAction != null) {
        return getPartitionFromPartitionAction(partitionAction);
    }
    switch(tableSource) {
        case PRE_EXISTING_TABLE:
            return delegate.getPartition(databaseName, tableName, partitionValues);
        case CREATED_IN_THIS_TRANSACTION:
            return Optional.empty();
        default:
            throw new UnsupportedOperationException("unknown table source");
    }
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Aggregations

SchemaTableName (com.facebook.presto.spi.SchemaTableName)159 ImmutableList (com.google.common.collect.ImmutableList)49 List (java.util.List)42 Test (org.testng.annotations.Test)40 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)35 ImmutableMap (com.google.common.collect.ImmutableMap)35 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)31 PrestoException (com.facebook.presto.spi.PrestoException)31 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)25 ConnectorMetadata (com.facebook.presto.spi.connector.ConnectorMetadata)19 ArrayList (java.util.ArrayList)19 Collectors.toList (java.util.stream.Collectors.toList)17 ConnectorSession (com.facebook.presto.spi.ConnectorSession)16 Map (java.util.Map)16 ColumnHandle (com.facebook.presto.spi.ColumnHandle)15 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)14 SchemaTablePrefix (com.facebook.presto.spi.SchemaTablePrefix)14 Optional (java.util.Optional)14 Table (com.facebook.presto.hive.metastore.Table)13 Path (org.apache.hadoop.fs.Path)12