use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.
the class FileHiveMetastore method getTablePrivileges.
@Override
public synchronized Set<HivePrivilegeInfo> getTablePrivileges(String user, String databaseName, String tableName) {
Table table = getRequiredTable(databaseName, tableName);
Set<HivePrivilegeInfo> privileges = new HashSet<>();
if (user.equals(table.getOwner())) {
privileges.add(new HivePrivilegeInfo(OWNERSHIP, true));
}
Path permissionsDirectory = getPermissionsDirectory(table);
privileges.addAll(getTablePrivileges(permissionsDirectory, user, USER));
for (String role : getRoles(user)) {
privileges.addAll(getTablePrivileges(permissionsDirectory, role, ROLE));
}
return privileges;
}
use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.
the class FileHiveMetastore method replaceTable.
@Override
public synchronized void replaceTable(String databaseName, String tableName, Table newTable, PrincipalPrivileges principalPrivileges) {
Table table = getRequiredTable(databaseName, tableName);
if (!table.getTableType().equals(VIRTUAL_VIEW.name()) || !newTable.getTableType().equals(VIRTUAL_VIEW.name())) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Only views can be updated with replaceTable");
}
if (!table.getDatabaseName().equals(databaseName) || !table.getTableName().equals(tableName)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Replacement table must have same name");
}
Path tableMetadataDirectory = getTableMetadataDirectory(table);
writeSchemaFile("table", tableMetadataDirectory, tableCodec, new TableMetadata(newTable), true);
// replace existing permissions
deleteTablePrivileges(table);
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());
}
}
use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.
the class FileHiveMetastore method alterPartition.
@Override
public synchronized void alterPartition(String databaseName, String tableName, Partition partition) {
Table table = getRequiredTable(databaseName, tableName);
verifiedPartition(table, partition);
Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partition.getValues());
writeSchemaFile("partition", partitionMetadataDirectory, partitionCodec, new PartitionMetadata(table, partition), true);
}
use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.
the class FileHiveMetastore method dropTable.
@Override
public synchronized void dropTable(String databaseName, String tableName, boolean deleteData) {
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
Table table = getRequiredTable(databaseName, tableName);
Path tableMetadataDirectory = getTableMetadataDirectory(databaseName, tableName);
// It is safe to delete the whole meta directory for external tables and views
if (!table.getTableType().equals(MANAGED_TABLE.name()) || deleteData) {
deleteMetadataDirectory(tableMetadataDirectory);
} else {
// in this case we only wan to delete the metadata of a managed table
deleteSchemaFile("table", tableMetadataDirectory);
deleteTablePrivileges(table);
}
}
use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.
the class FileHiveMetastore method addPartitions.
@Override
public synchronized void addPartitions(String databaseName, String tableName, List<Partition> partitions) {
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(partitions, "partitions is null");
Table table = getRequiredTable(databaseName, tableName);
TableType tableType = TableType.valueOf(table.getTableType());
checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE).contains(tableType), "Invalid table type: %s", tableType);
try {
Map<Path, byte[]> schemaFiles = new LinkedHashMap<>();
for (Partition partition : partitions) {
verifiedPartition(table, partition);
Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partition.getValues());
Path schemaPath = new Path(partitionMetadataDirectory, PRESTO_SCHEMA_FILE_NAME);
if (metadataFileSystem.exists(schemaPath)) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Partition already exists");
}
byte[] schemaJson = partitionCodec.toJsonBytes(new PartitionMetadata(table, partition));
schemaFiles.put(schemaPath, schemaJson);
}
Set<Path> createdFiles = new LinkedHashSet<>();
try {
for (Entry<Path, byte[]> entry : schemaFiles.entrySet()) {
try (OutputStream outputStream = metadataFileSystem.create(entry.getKey())) {
createdFiles.add(entry.getKey());
outputStream.write(entry.getValue());
} catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Could not write partition schema", e);
}
}
} catch (Throwable e) {
for (Path createdFile : createdFiles) {
try {
metadataFileSystem.delete(createdFile, false);
} catch (IOException ignored) {
}
}
throw e;
}
} catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, e);
}
}
Aggregations