use of io.trino.spi.security.TrinoPrincipal in project trino by trinodb.
the class BaseTrinoCatalogTest method testRenameTable.
@Test
public void testRenameTable() throws IOException {
TrinoCatalog catalog = createTrinoCatalog(false);
Path tmpDirectory = Files.createTempDirectory("iceberg_catalog_test_rename_table_");
tmpDirectory.toFile().deleteOnExit();
String namespace = "test_rename_table_" + randomTableSuffix();
String targetNamespace = "test_rename_table_" + randomTableSuffix();
String table = "tableName";
SchemaTableName sourceSchemaTableName = new SchemaTableName(namespace, table);
try {
catalog.createNamespace(SESSION, namespace, ImmutableMap.of(), new TrinoPrincipal(PrincipalType.USER, SESSION.getUser()));
catalog.createNamespace(SESSION, targetNamespace, ImmutableMap.of(), new TrinoPrincipal(PrincipalType.USER, SESSION.getUser()));
catalog.newCreateTableTransaction(SESSION, sourceSchemaTableName, new Schema(Types.NestedField.of(1, true, "col1", Types.LongType.get())), PartitionSpec.unpartitioned(), tmpDirectory.toAbsolutePath().toString(), ImmutableMap.of()).commitTransaction();
assertThat(catalog.listTables(SESSION, Optional.of(namespace))).contains(sourceSchemaTableName);
// Rename within the same schema
SchemaTableName targetSchemaTableName = new SchemaTableName(sourceSchemaTableName.getSchemaName(), "newTableName");
catalog.renameTable(SESSION, sourceSchemaTableName, targetSchemaTableName);
assertThat(catalog.listTables(SESSION, Optional.of(namespace))).doesNotContain(sourceSchemaTableName);
assertThat(catalog.listTables(SESSION, Optional.of(namespace))).contains(targetSchemaTableName);
// Move to a different schema
sourceSchemaTableName = targetSchemaTableName;
targetSchemaTableName = new SchemaTableName(targetNamespace, sourceSchemaTableName.getTableName());
catalog.renameTable(SESSION, sourceSchemaTableName, targetSchemaTableName);
assertThat(catalog.listTables(SESSION, Optional.of(namespace))).doesNotContain(sourceSchemaTableName);
assertThat(catalog.listTables(SESSION, Optional.of(targetNamespace))).contains(targetSchemaTableName);
catalog.dropTable(SESSION, targetSchemaTableName);
} finally {
try {
catalog.dropNamespace(SESSION, namespace);
catalog.dropNamespace(SESSION, targetNamespace);
} catch (Exception e) {
LOG.warn("Failed to clean up namespaces: %s, %s", namespace, targetNamespace);
}
}
}
use of io.trino.spi.security.TrinoPrincipal in project trino by trinodb.
the class BaseTrinoCatalogTest method testCreateTable.
@Test
public void testCreateTable() throws IOException {
TrinoCatalog catalog = createTrinoCatalog(false);
Path tmpDirectory = Files.createTempDirectory("iceberg_catalog_test_create_table_");
tmpDirectory.toFile().deleteOnExit();
String namespace = "test_create_table_" + randomTableSuffix();
String table = "tableName";
SchemaTableName schemaTableName = new SchemaTableName(namespace, table);
try {
catalog.createNamespace(SESSION, namespace, ImmutableMap.of(), new TrinoPrincipal(PrincipalType.USER, SESSION.getUser()));
catalog.newCreateTableTransaction(SESSION, schemaTableName, new Schema(Types.NestedField.of(1, true, "col1", Types.LongType.get())), PartitionSpec.unpartitioned(), tmpDirectory.toAbsolutePath().toString(), ImmutableMap.of()).commitTransaction();
assertThat(catalog.listTables(SESSION, Optional.of(namespace))).contains(schemaTableName);
assertThat(catalog.listTables(SESSION, Optional.empty())).contains(schemaTableName);
Table icebergTable = catalog.loadTable(SESSION, schemaTableName);
assertEquals(icebergTable.name(), quotedTableName(schemaTableName));
assertEquals(icebergTable.schema().columns().size(), 1);
assertEquals(icebergTable.schema().columns().get(0).name(), "col1");
assertEquals(icebergTable.schema().columns().get(0).type(), Types.LongType.get());
assertEquals(icebergTable.location(), tmpDirectory.toAbsolutePath().toString());
assertEquals(icebergTable.properties(), ImmutableMap.of());
catalog.dropTable(SESSION, schemaTableName);
assertThat(catalog.listTables(SESSION, Optional.of(namespace))).doesNotContain(schemaTableName);
assertThat(catalog.listTables(SESSION, Optional.empty())).doesNotContain(schemaTableName);
} finally {
try {
catalog.dropNamespace(SESSION, namespace);
} catch (Exception e) {
LOG.warn("Failed to clean up namespace: %s", namespace);
}
}
}
use of io.trino.spi.security.TrinoPrincipal in project trino by trinodb.
the class BaseTrinoCatalogTest method testCreateNamespaceWithLocation.
@Test
public void testCreateNamespaceWithLocation() {
TrinoCatalog catalog = createTrinoCatalog(false);
String namespace = "test_create_namespace_with_location_" + randomTableSuffix();
catalog.createNamespace(SESSION, namespace, ImmutableMap.of(LOCATION_PROPERTY, "/a/path/"), new TrinoPrincipal(PrincipalType.USER, SESSION.getUser()));
assertThat(catalog.listNamespaces(SESSION)).contains(namespace);
assertEquals(catalog.loadNamespaceMetadata(SESSION, namespace), ImmutableMap.of(LOCATION_PROPERTY, "/a/path/"));
assertEquals(catalog.defaultTableLocation(SESSION, new SchemaTableName(namespace, "table")), "/a/path/table");
catalog.dropNamespace(SESSION, namespace);
assertThat(catalog.listNamespaces(SESSION)).doesNotContain(namespace);
}
use of io.trino.spi.security.TrinoPrincipal in project trino by trinodb.
the class TestTrinoGlueCatalogTest method testDefaultLocation.
@Test
public void testDefaultLocation() throws IOException {
Path tmpDirectory = Files.createTempDirectory("test_glue_catalog_default_location_");
tmpDirectory.toFile().deleteOnExit();
HdfsEnvironment hdfsEnvironment = new HdfsEnvironment(new HiveHdfsConfiguration(new HdfsConfigurationInitializer(new HdfsConfig(), ImmutableSet.of()), ImmutableSet.of()), new HdfsConfig(), new NoHdfsAuthentication());
TrinoCatalog catalogWithDefaultLocation = new TrinoGlueCatalog(hdfsEnvironment, new GlueIcebergTableOperationsProvider(new HdfsFileIoProvider(hdfsEnvironment), new GlueHiveMetastoreConfig()), AWSGlueAsyncClientBuilder.defaultClient(), new GlueMetastoreStats(), Optional.of(tmpDirectory.toAbsolutePath().toString()), false);
String namespace = "test_default_location_" + randomTableSuffix();
String table = "tableName";
SchemaTableName schemaTableName = new SchemaTableName(namespace, table);
catalogWithDefaultLocation.createNamespace(SESSION, namespace, ImmutableMap.of(), new TrinoPrincipal(PrincipalType.USER, SESSION.getUser()));
try {
File expectedSchemaDirectory = new File(tmpDirectory.toFile(), namespace + ".db");
File expectedTableDirectory = new File(expectedSchemaDirectory, schemaTableName.getTableName());
assertEquals(catalogWithDefaultLocation.defaultTableLocation(SESSION, schemaTableName), expectedTableDirectory.toPath().toAbsolutePath().toString());
} finally {
try {
catalogWithDefaultLocation.dropNamespace(SESSION, namespace);
} catch (Exception e) {
LOG.warn("Failed to clean up namespace: %s", namespace);
}
}
}
use of io.trino.spi.security.TrinoPrincipal in project trino by trinodb.
the class SetRoleTask method execute.
@Override
public ListenableFuture<Void> execute(SetRole statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
Session session = stateMachine.getSession();
Optional<String> catalog = processRoleCommandCatalog(metadata, session, statement, statement.getCatalog().map(Identifier::getValue));
if (statement.getType() == SetRole.Type.ROLE) {
String role = statement.getRole().map(c -> c.getValue().toLowerCase(ENGLISH)).orElseThrow();
if (!metadata.roleExists(session, role, catalog)) {
throw semanticException(ROLE_NOT_FOUND, statement, "Role '%s' does not exist", role);
}
if (catalog.isPresent()) {
accessControl.checkCanSetCatalogRole(SecurityContext.of(session), role, catalog.get());
} else {
Set<RoleGrant> roleGrants = metadata.listApplicableRoles(session, new TrinoPrincipal(USER, session.getUser()), Optional.empty());
if (roleGrants.stream().map(RoleGrant::getRoleName).noneMatch(role::equals)) {
denySetRole(role);
}
}
}
SelectedRole.Type type = toSelectedRoleType(statement.getType());
stateMachine.addSetRole(catalog.orElse("system"), new SelectedRole(type, statement.getRole().map(c -> c.getValue().toLowerCase(ENGLISH))));
return immediateVoidFuture();
}
Aggregations