Search in sources :

Example 1 with TableAlreadyExistsException

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

the class InMemoryHiveMetastore method alterTable.

@Override
public synchronized void alterTable(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 2 with TableAlreadyExistsException

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

the class InMemoryHiveMetastore method createTable.

@Override
public synchronized void createTable(Table table) {
    TableType tableType = TableType.valueOf(table.getTableType());
    checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE, VIRTUAL_VIEW).contains(tableType), "Invalid table type: %s", tableType);
    if (tableType == VIRTUAL_VIEW) {
        checkArgument(table.getSd().getLocation() == null, "Storage location for view must be null");
    } else {
        File directory = new File(new Path(table.getSd().getLocation()).toUri());
        checkArgument(directory.exists(), "Table directory does not exist");
        if (tableType == MANAGED_TABLE) {
            checkArgument(isParentDir(directory, baseDirectory), "Table directory must be inside of the metastore base directory");
        }
    }
    SchemaTableName schemaTableName = new SchemaTableName(table.getDbName(), table.getTableName());
    Table tableCopy = table.deepCopy();
    if (relations.putIfAbsent(schemaTableName, tableCopy) != null) {
        throw new TableAlreadyExistsException(schemaTableName);
    }
    if (tableType == VIRTUAL_VIEW) {
        views.put(schemaTableName, tableCopy);
    }
    PrincipalPrivilegeSet privileges = table.getPrivileges();
    if (privileges != null) {
        for (Entry<String, List<PrivilegeGrantInfo>> entry : privileges.getUserPrivileges().entrySet()) {
            String user = entry.getKey();
            Set<HivePrivilegeInfo> userPrivileges = entry.getValue().stream().map(HivePrivilegeInfo::parsePrivilege).flatMap(Collection::stream).collect(toImmutableSet());
            setTablePrivileges(user, USER, table.getDbName(), table.getTableName(), userPrivileges);
        }
        for (Entry<String, List<PrivilegeGrantInfo>> entry : privileges.getRolePrivileges().entrySet()) {
            String role = entry.getKey();
            Set<HivePrivilegeInfo> rolePrivileges = entry.getValue().stream().map(HivePrivilegeInfo::parsePrivilege).flatMap(Collection::stream).collect(toImmutableSet());
            setTablePrivileges(role, ROLE, table.getDbName(), table.getTableName(), rolePrivileges);
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) TableType(org.apache.hadoop.hive.metastore.TableType) Table(org.apache.hadoop.hive.metastore.api.Table) PrincipalPrivilegeSet(org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) File(java.io.File) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 3 with TableAlreadyExistsException

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

the class TestingHiveMetastore method createTable.

@Override
public synchronized void createTable(Table table, PrincipalPrivileges principalPrivileges) {
    TableType tableType = TableType.valueOf(table.getTableType());
    checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE, VIRTUAL_VIEW).contains(tableType), "Invalid table type: %s", tableType);
    SchemaTableName schemaTableName = new SchemaTableName(table.getDatabaseName(), table.getTableName());
    if (tableType == VIRTUAL_VIEW) {
        checkArgument(table.getStorage().getLocation().isEmpty(), "Storage location for view must be empty");
    } else {
        File directory = new File(URI.create(table.getStorage().getLocation()));
        if (!directory.exists()) {
            throw new PrestoException(HIVE_METASTORE_ERROR, "Table directory does not exist");
        }
        if (tableType == MANAGED_TABLE && !isParentDir(directory, baseDirectory)) {
            throw new PrestoException(HIVE_METASTORE_ERROR, "Table directory must be inside of the metastore base directory");
        }
    }
    if (relations.putIfAbsent(schemaTableName, table) != null) {
        throw new TableAlreadyExistsException(schemaTableName);
    }
    for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getUserPrivileges().asMap().entrySet()) {
        setTablePrivileges(entry.getKey(), USER, table.getDatabaseName(), table.getTableName(), entry.getValue());
    }
    for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getRolePrivileges().asMap().entrySet()) {
        setTablePrivileges(entry.getKey(), ROLE, table.getDatabaseName(), table.getTableName(), entry.getValue());
    }
}
Also used : TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) TableType(org.apache.hadoop.hive.metastore.TableType) Collection(java.util.Collection) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName) File(java.io.File)

Example 4 with TableAlreadyExistsException

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

the class GlueHiveMetastore method createTable.

@Override
public void createTable(MetastoreContext metastoreContext, Table table, PrincipalPrivileges principalPrivileges) {
    try {
        TableInput input = GlueInputConverter.convertTable(table);
        stats.getCreateTable().record(() -> glueClient.createTable(new CreateTableRequest().withCatalogId(catalogId).withDatabaseName(table.getDatabaseName()).withTableInput(input)));
    } catch (AlreadyExistsException e) {
        throw new TableAlreadyExistsException(new SchemaTableName(table.getDatabaseName(), table.getTableName()));
    } catch (EntityNotFoundException e) {
        throw new SchemaNotFoundException(table.getDatabaseName());
    } catch (AmazonServiceException e) {
        throw new PrestoException(HIVE_METASTORE_ERROR, e);
    }
}
Also used : TableInput(com.amazonaws.services.glue.model.TableInput) GlueInputConverter.toTableInput(com.facebook.presto.hive.metastore.glue.converter.GlueInputConverter.toTableInput) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) SchemaAlreadyExistsException(com.facebook.presto.hive.SchemaAlreadyExistsException) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) AlreadyExistsException(com.amazonaws.services.glue.model.AlreadyExistsException) AmazonServiceException(com.amazonaws.AmazonServiceException) PrestoException(com.facebook.presto.spi.PrestoException) EntityNotFoundException(com.amazonaws.services.glue.model.EntityNotFoundException) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) CreateTableRequest(com.amazonaws.services.glue.model.CreateTableRequest) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 5 with TableAlreadyExistsException

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

the class InMemoryHiveMetastore method createTable.

@Override
public synchronized void createTable(MetastoreContext metastoreContext, Table table) {
    TableType tableType = TableType.valueOf(table.getTableType());
    checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE, VIRTUAL_VIEW).contains(tableType), "Invalid table type: %s", tableType);
    if (tableType == VIRTUAL_VIEW) {
        checkArgument(table.getSd().getLocation() == null, "Storage location for view must be null");
    } else {
        File directory = new File(new Path(table.getSd().getLocation()).toUri());
        checkArgument(directory.exists(), "Table directory does not exist: %s", directory);
        if (tableType == MANAGED_TABLE) {
            checkArgument(isParentDir(directory, baseDirectory), "Table directory must be inside of the metastore base directory");
        }
    }
    SchemaTableName schemaTableName = new SchemaTableName(table.getDbName(), table.getTableName());
    Table tableCopy = table.deepCopy();
    if (relations.putIfAbsent(schemaTableName, tableCopy) != null) {
        throw new TableAlreadyExistsException(schemaTableName);
    }
    if (tableType == VIRTUAL_VIEW) {
        views.put(schemaTableName, tableCopy);
    }
    PrincipalPrivilegeSet privileges = table.getPrivileges();
    if (privileges != null) {
        throw new UnsupportedOperationException();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) TableType(org.apache.hadoop.hive.metastore.TableType) Table(org.apache.hadoop.hive.metastore.api.Table) PrincipalPrivilegeSet(org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet) File(java.io.File) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

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