Search in sources :

Example 6 with TableAlreadyExistsException

use of com.facebook.presto.hive.TableAlreadyExistsException in project presto by prestodb.

the class InMemoryHiveMetastore method alterTable.

@Override
public synchronized void alterTable(MetastoreContext metastoreContext, String databaseName, String tableName, Table newTable) {
    SchemaTableName oldName = new SchemaTableName(databaseName, tableName);
    SchemaTableName newName = new SchemaTableName(newTable.getDbName(), newTable.getTableName());
    // if the name did not change, this is a simple schema change
    if (oldName.equals(newName)) {
        if (relations.replace(oldName, newTable) == null) {
            throw new TableNotFoundException(oldName);
        }
        return;
    }
    // remove old table definition and add the new one
    Table table = relations.get(oldName);
    if (table == null) {
        throw new TableNotFoundException(oldName);
    }
    if (relations.putIfAbsent(newName, newTable) != null) {
        throw new TableAlreadyExistsException(newName);
    }
    relations.remove(oldName);
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) Table(org.apache.hadoop.hive.metastore.api.Table) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 7 with TableAlreadyExistsException

use of com.facebook.presto.hive.TableAlreadyExistsException in project presto by prestodb.

the class IcebergHadoopMetadata method beginCreateTable.

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout) {
    SchemaTableName schemaTableName = tableMetadata.getTable();
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    Schema schema = toIcebergSchema(tableMetadata.getColumns());
    PartitionSpec partitionSpec = parsePartitionFields(schema, getPartitioning(tableMetadata.getProperties()));
    ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builder();
    FileFormat fileFormat = getFileFormat(tableMetadata.getProperties());
    propertiesBuilder.put(DEFAULT_FILE_FORMAT, fileFormat.toString());
    if (tableMetadata.getComment().isPresent()) {
        propertiesBuilder.put(TABLE_COMMENT, tableMetadata.getComment().get());
    }
    String formatVersion = getFormatVersion(tableMetadata.getProperties());
    if (formatVersion != null) {
        propertiesBuilder.put(FORMAT_VERSION, formatVersion);
    }
    try {
        transaction = resourceFactory.getCatalog(session).newCreateTableTransaction(toIcebergTableIdentifier(schemaTableName), schema, partitionSpec, propertiesBuilder.build());
    } catch (AlreadyExistsException e) {
        throw new TableAlreadyExistsException(schemaTableName);
    }
    Table icebergTable = transaction.table();
    return new IcebergWritableTableHandle(schemaName, tableName, SchemaParser.toJson(icebergTable.schema()), PartitionSpecParser.toJson(icebergTable.spec()), getColumns(icebergTable.schema(), typeManager), icebergTable.location(), fileFormat, icebergTable.properties());
}
Also used : TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) SystemTable(com.facebook.presto.spi.SystemTable) IcebergUtil.getHadoopIcebergTable(com.facebook.presto.iceberg.IcebergUtil.getHadoopIcebergTable) Table(org.apache.iceberg.Table) AlreadyExistsException(org.apache.iceberg.exceptions.AlreadyExistsException) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) Schema(org.apache.iceberg.Schema) FileFormat(org.apache.iceberg.FileFormat) IcebergTableProperties.getFileFormat(com.facebook.presto.iceberg.IcebergTableProperties.getFileFormat) SchemaTableName(com.facebook.presto.spi.SchemaTableName) PartitionSpec(org.apache.iceberg.PartitionSpec) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 8 with TableAlreadyExistsException

use of com.facebook.presto.hive.TableAlreadyExistsException in project presto by prestodb.

the class TestingHiveMetastore method renameTable.

@Override
public synchronized void renameTable(String databaseName, String tableName, String newDatabaseName, String newTableName) {
    SchemaTableName oldName = new SchemaTableName(databaseName, tableName);
    Table oldTable = getRequiredTable(oldName);
    // todo move data to new location
    Table newTable = Table.builder(oldTable).setDatabaseName(newDatabaseName).setTableName(newTableName).build();
    SchemaTableName newName = new SchemaTableName(newDatabaseName, newTableName);
    if (relations.putIfAbsent(newName, newTable) != null) {
        throw new TableAlreadyExistsException(newName);
    }
    relations.remove(oldName);
    // rename partitions
    for (Entry<PartitionName, Partition> entry : ImmutableList.copyOf(partitions.entrySet())) {
        PartitionName partitionName = entry.getKey();
        Partition partition = entry.getValue();
        if (partitionName.matches(databaseName, tableName)) {
            partitions.remove(partitionName);
            partitions.put(new PartitionName(newDatabaseName, newTableName, partitionName.getValues()), Partition.builder(partition).setDatabaseName(newDatabaseName).setTableName(newTableName).build());
        }
    }
    // rename privileges
    for (Entry<PrincipalTableKey, Set<HivePrivilegeInfo>> entry : ImmutableList.copyOf(tablePrivileges.entrySet())) {
        PrincipalTableKey principalTableKey = entry.getKey();
        Set<HivePrivilegeInfo> privileges = entry.getValue();
        if (principalTableKey.matches(databaseName, tableName)) {
            tablePrivileges.remove(principalTableKey);
            tablePrivileges.put(new PrincipalTableKey(principalTableKey.getPrincipalName(), principalTableKey.getPrincipalType(), newTableName, newDatabaseName), privileges);
        }
    }
}
Also used : TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 9 with TableAlreadyExistsException

use of com.facebook.presto.hive.TableAlreadyExistsException in project presto by prestodb.

the class SemiTransactionalHiveMetastore method createTable.

/**
     * {@code currentLocation} needs to be supplied if a writePath exists for the table.
     */
public synchronized void createTable(ConnectorSession session, Table table, PrincipalPrivileges principalPrivileges, Optional<Path> currentPath) {
    setShared();
    // When creating a table, it should never have partition actions. This is just a sanity check.
    checkNoPartitionAction(table.getDatabaseName(), table.getTableName());
    SchemaTableName schemaTableName = new SchemaTableName(table.getDatabaseName(), table.getTableName());
    Action<TableAndMore> oldTableAction = tableActions.get(schemaTableName);
    TableAndMore tableAndMore = new TableAndMore(table, Optional.of(principalPrivileges), currentPath, Optional.empty());
    if (oldTableAction == null) {
        tableActions.put(schemaTableName, new Action<>(ActionType.ADD, tableAndMore, session.getUser(), session.getQueryId()));
        return;
    }
    switch(oldTableAction.getType()) {
        case DROP:
            throw new PrestoException(TRANSACTION_CONFLICT, "Dropping and then recreating the same table in a transaction is not supported");
        case ADD:
        case ALTER:
        case INSERT_EXISTING:
            throw new TableAlreadyExistsException(schemaTableName);
        default:
            throw new IllegalStateException("Unknown action type");
    }
}
Also used : TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 10 with TableAlreadyExistsException

use of com.facebook.presto.hive.TableAlreadyExistsException in project presto by prestodb.

the class IcebergHiveMetadata method beginCreateTable.

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout) {
    SchemaTableName schemaTableName = tableMetadata.getTable();
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    Schema schema = toIcebergSchema(tableMetadata.getColumns());
    PartitionSpec partitionSpec = parsePartitionFields(schema, getPartitioning(tableMetadata.getProperties()));
    MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER);
    Database database = metastore.getDatabase(metastoreContext, schemaName).orElseThrow(() -> new SchemaNotFoundException(schemaName));
    HdfsContext hdfsContext = new HdfsContext(session, schemaName, tableName);
    String targetPath = getTableLocation(tableMetadata.getProperties());
    if (targetPath == null) {
        Optional<String> location = database.getLocation();
        if (!location.isPresent() || location.get().isEmpty()) {
            throw new PrestoException(NOT_SUPPORTED, "Database " + schemaName + " location is not set");
        }
        Path databasePath = new Path(location.get());
        Path resultPath = new Path(databasePath, tableName);
        targetPath = resultPath.toString();
    }
    TableOperations operations = new HiveTableOperations(metastore, new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER), hdfsEnvironment, hdfsContext, schemaName, tableName, session.getUser(), targetPath);
    if (operations.current() != null) {
        throw new TableAlreadyExistsException(schemaTableName);
    }
    ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builderWithExpectedSize(2);
    FileFormat fileFormat = getFileFormat(tableMetadata.getProperties());
    propertiesBuilder.put(DEFAULT_FILE_FORMAT, fileFormat.toString());
    if (tableMetadata.getComment().isPresent()) {
        propertiesBuilder.put(TABLE_COMMENT, tableMetadata.getComment().get());
    }
    TableMetadata metadata = newTableMetadata(schema, partitionSpec, targetPath, propertiesBuilder.build());
    transaction = createTableTransaction(tableName, operations, metadata);
    return new IcebergWritableTableHandle(schemaName, tableName, SchemaParser.toJson(metadata.schema()), PartitionSpecParser.toJson(metadata.spec()), getColumns(metadata.schema(), typeManager), targetPath, fileFormat, metadata.properties());
}
Also used : Path(org.apache.hadoop.fs.Path) TableMetadata(org.apache.iceberg.TableMetadata) TableMetadata.newTableMetadata(org.apache.iceberg.TableMetadata.newTableMetadata) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) Schema(org.apache.iceberg.Schema) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) PrestoException(com.facebook.presto.spi.PrestoException) FileFormat(org.apache.iceberg.FileFormat) IcebergTableProperties.getFileFormat(com.facebook.presto.iceberg.IcebergTableProperties.getFileFormat) SchemaTableName(com.facebook.presto.spi.SchemaTableName) PartitionSpec(org.apache.iceberg.PartitionSpec) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) TableOperations(org.apache.iceberg.TableOperations) Database(com.facebook.presto.hive.metastore.Database) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) HdfsContext(com.facebook.presto.hive.HdfsContext)

Aggregations

TableAlreadyExistsException (com.facebook.presto.hive.TableAlreadyExistsException)11 SchemaTableName (com.facebook.presto.spi.SchemaTableName)10 PrestoException (com.facebook.presto.spi.PrestoException)5 Table (org.apache.hadoop.hive.metastore.api.Table)4 File (java.io.File)3 Path (org.apache.hadoop.fs.Path)3 TableType (org.apache.hadoop.hive.metastore.TableType)3 HdfsContext (com.facebook.presto.hive.HdfsContext)2 IcebergTableProperties.getFileFormat (com.facebook.presto.iceberg.IcebergTableProperties.getFileFormat)2 SchemaNotFoundException (com.facebook.presto.spi.SchemaNotFoundException)2 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2 PrincipalPrivilegeSet (org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet)2 FileFormat (org.apache.iceberg.FileFormat)2 PartitionSpec (org.apache.iceberg.PartitionSpec)2 Schema (org.apache.iceberg.Schema)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AlreadyExistsException (com.amazonaws.services.glue.model.AlreadyExistsException)1 CreateTableRequest (com.amazonaws.services.glue.model.CreateTableRequest)1