use of io.trino.plugin.hive.metastore.Database in project trino by trinodb.
the class HiveMetadata method createSchema.
@Override
public void createSchema(ConnectorSession session, String schemaName, Map<String, Object> properties, TrinoPrincipal owner) {
Optional<String> location = HiveSchemaProperties.getLocation(properties).map(locationUri -> {
try {
hdfsEnvironment.getFileSystem(new HdfsContext(session), new Path(locationUri));
} catch (IOException e) {
throw new TrinoException(INVALID_SCHEMA_PROPERTY, "Invalid location URI: " + locationUri, e);
}
return locationUri;
});
Database database = Database.builder().setDatabaseName(schemaName).setLocation(location).setOwnerType(accessControlMetadata.isUsingSystemSecurity() ? Optional.empty() : Optional.of(owner.getType())).setOwnerName(accessControlMetadata.isUsingSystemSecurity() ? Optional.empty() : Optional.of(owner.getName())).build();
metastore.createDatabase(database);
}
use of io.trino.plugin.hive.metastore.Database in project trino by trinodb.
the class AlluxioHiveMetastore method getPartitionsByNames.
@Override
public Map<String, Optional<Partition>> getPartitionsByNames(Table table, List<String> partitionNames) {
if (partitionNames.isEmpty()) {
return Collections.emptyMap();
}
String databaseName = table.getDatabaseName();
String tableName = table.getTableName();
try {
// Get all partitions
List<PartitionInfo> partitionInfos = ProtoUtils.toPartitionInfoList(client.readTable(databaseName, tableName, Constraint.getDefaultInstance()));
// Check that table name is correct
// TODO also check for database name equality
partitionInfos = partitionInfos.stream().filter(partition -> partition.getTableName().equals(tableName)).collect(Collectors.toList());
Map<String, Optional<Partition>> result = partitionInfos.stream().filter(partitionName -> partitionNames.stream().anyMatch(partitionName.getPartitionName()::equals)).collect(Collectors.toMap(PartitionInfo::getPartitionName, partitionInfo -> Optional.of(ProtoUtils.fromProto(partitionInfo))));
return Collections.unmodifiableMap(result);
} catch (AlluxioStatusException e) {
throw new TrinoException(HIVE_METASTORE_ERROR, e);
}
}
use of io.trino.plugin.hive.metastore.Database in project trino by trinodb.
the class GlueHiveMetastore method renameDatabase.
@Override
public void renameDatabase(String databaseName, String newDatabaseName) {
try {
Database database = getDatabase(databaseName).orElseThrow(() -> new SchemaNotFoundException(databaseName));
DatabaseInput renamedDatabase = GlueInputConverter.convertDatabase(database).withName(newDatabaseName);
stats.getUpdateDatabase().call(() -> glueClient.updateDatabase(new UpdateDatabaseRequest().withCatalogId(catalogId).withName(databaseName).withDatabaseInput(renamedDatabase)));
} catch (AmazonServiceException e) {
throw new TrinoException(HIVE_METASTORE_ERROR, e);
}
}
use of io.trino.plugin.hive.metastore.Database in project trino by trinodb.
the class FileHiveMetastore method listAllTables.
@GuardedBy("this")
private List<String> listAllTables(String databaseName) {
requireNonNull(databaseName, "databaseName is null");
Optional<Database> database = getDatabase(databaseName);
if (database.isEmpty()) {
return ImmutableList.of();
}
Path databaseMetadataDirectory = getDatabaseMetadataDirectory(databaseName);
List<String> tables = getChildSchemaDirectories(TABLE, databaseMetadataDirectory).stream().map(Path::getName).collect(toImmutableList());
return tables;
}
use of io.trino.plugin.hive.metastore.Database in project trino by trinodb.
the class SqlStandardAccessControl method isDatabaseOwner.
private boolean isDatabaseOwner(ConnectorSecurityContext context, String databaseName) {
// all users are "owners" of the default database
if (DEFAULT_DATABASE_NAME.equalsIgnoreCase(databaseName)) {
return true;
}
if (isAdmin(context)) {
return true;
}
Optional<Database> databaseMetadata = metastore.getDatabase(context, databaseName);
if (databaseMetadata.isEmpty()) {
return false;
}
Database database = databaseMetadata.get();
// a database can be owned by a user or role
ConnectorIdentity identity = context.getIdentity();
if (database.getOwnerName().isPresent()) {
if (database.getOwnerType().orElse(null) == USER && identity.getUser().equals(database.getOwnerName().get())) {
return true;
}
if (database.getOwnerType().orElse(null) == ROLE && isRoleEnabled(identity, hivePrincipal -> metastore.listRoleGrants(context, hivePrincipal), database.getOwnerName().get())) {
return true;
}
}
return false;
}
Aggregations