Skip to content

Commit 8da2dcf

Browse files
authored
Merge branch 'trinodb:master' into hsqldb
2 parents 54542de + 00da374 commit 8da2dcf

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

plugin/trino-memory/src/main/java/io/trino/plugin/memory/MemoryMetadata.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,32 +165,40 @@ public synchronized void renameSchema(ConnectorSession session, String source, S
165165
}
166166
schemas.add(target);
167167

168-
for (Map.Entry<SchemaTableName, Long> table : tableIds.entrySet()) {
168+
Map<SchemaTableName, Long> newTableIds = new HashMap<>();
169+
for (Iterator<Map.Entry<SchemaTableName, Long>> iterator = tableIds.entrySet().iterator(); iterator.hasNext(); ) {
170+
Map.Entry<SchemaTableName, Long> table = iterator.next();
169171
if (table.getKey().getSchemaName().equals(source)) {
170-
tableIds.remove(table.getKey());
171-
tableIds.put(new SchemaTableName(target, table.getKey().getTableName()), table.getValue());
172+
iterator.remove();
173+
newTableIds.put(new SchemaTableName(target, table.getKey().getTableName()), table.getValue());
172174
}
173175
}
176+
tableIds.putAll(newTableIds);
174177

175-
for (TableInfo table : tables.values()) {
176-
if (table.schemaName().equals(source)) {
177-
tables.put(table.id(), new TableInfo(table.id(), target, table.tableName(), table.columns(), false, table.dataFragments(), table.comment()));
178-
}
179-
}
178+
tables.replaceAll((tableId, table) ->
179+
table.schemaName().equals(source)
180+
? new TableInfo(tableId, target, table.tableName(), table.columns(), table.truncated(), table.dataFragments(), table.comment())
181+
: table);
180182

181-
for (Map.Entry<SchemaTableName, ConnectorViewDefinition> view : views.entrySet()) {
183+
Map<SchemaTableName, ConnectorViewDefinition> newViews = new HashMap<>();
184+
for (Iterator<Map.Entry<SchemaTableName, ConnectorViewDefinition>> iterator = views.entrySet().iterator(); iterator.hasNext(); ) {
185+
Map.Entry<SchemaTableName, ConnectorViewDefinition> view = iterator.next();
182186
if (view.getKey().getSchemaName().equals(source)) {
183-
views.remove(view.getKey());
184-
views.put(new SchemaTableName(target, view.getKey().getTableName()), view.getValue());
187+
iterator.remove();
188+
newViews.put(new SchemaTableName(target, view.getKey().getTableName()), view.getValue());
185189
}
186190
}
191+
views.putAll(newViews);
187192

188-
for (Map.Entry<SchemaFunctionName, Map<String, LanguageFunction>> function : functions.entrySet()) {
193+
Map<SchemaFunctionName, Map<String, LanguageFunction>> newFunctions = new HashMap<>();
194+
for (Iterator<Map.Entry<SchemaFunctionName, Map<String, LanguageFunction>>> iterator = functions.entrySet().iterator(); iterator.hasNext(); ) {
195+
Map.Entry<SchemaFunctionName, Map<String, LanguageFunction>> function = iterator.next();
189196
if (function.getKey().getSchemaName().equals(source)) {
190-
functions.remove(function.getKey());
191-
functions.put(new SchemaFunctionName(target, function.getKey().getFunctionName()), function.getValue());
197+
iterator.remove();
198+
newFunctions.put(new SchemaFunctionName(target, function.getKey().getFunctionName()), function.getValue());
192199
}
193200
}
201+
functions.putAll(newFunctions);
194202
}
195203

196204
@GuardedBy("this")

plugin/trino-memory/src/test/java/io/trino/plugin/memory/TestMemoryMetadata.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
import java.util.List;
3333
import java.util.Map;
3434
import java.util.Optional;
35+
import java.util.Set;
36+
import java.util.stream.IntStream;
3537

38+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
3639
import static io.trino.spi.StandardErrorCode.ALREADY_EXISTS;
3740
import static io.trino.spi.StandardErrorCode.NOT_FOUND;
3841
import static io.trino.spi.connector.RetryMode.NO_RETRIES;
@@ -344,6 +347,33 @@ public void testRenameTable()
344347
assertThat(metadata.listTables(SESSION, Optional.of("test_different_schema"))).isEqualTo(ImmutableList.of(differentSchemaTableName));
345348
}
346349

350+
@Test
351+
public void testRenameSchema()
352+
{
353+
Set<SchemaTableName> tableNames = IntStream.range(1, 10)
354+
.mapToObj(idx -> new SchemaTableName("test_schema_to_be_renamed", "test_table_" + idx))
355+
.collect(toImmutableSet());
356+
MemoryMetadata metadata = createMetadata();
357+
metadata.createSchema(SESSION, "test_schema_to_be_renamed", ImmutableMap.of(), new TrinoPrincipal(USER, SESSION.getUser()));
358+
tableNames.forEach(tableName -> {
359+
ConnectorOutputTableHandle table = metadata.beginCreateTable(
360+
SESSION,
361+
new ConnectorTableMetadata(tableName, ImmutableList.of(), ImmutableMap.of()),
362+
Optional.empty(),
363+
NO_RETRIES,
364+
false);
365+
metadata.finishCreateTable(SESSION, table, ImmutableList.of(), ImmutableList.of());
366+
});
367+
368+
// rename schema
369+
Set<SchemaTableName> renamedTableNames = tableNames.stream()
370+
.map(tableName -> new SchemaTableName("test_schema", tableName.getTableName()))
371+
.collect(toImmutableSet());
372+
metadata.renameSchema(SESSION, "test_schema_to_be_renamed", "test_schema");
373+
assertThat(metadata.listTables(SESSION, Optional.of("test_schema_to_be_renamed"))).isEmpty();
374+
assertThat(metadata.listTables(SESSION, Optional.of("test_schema"))).containsAll(renamedTableNames);
375+
}
376+
347377
private static void assertNoTables(MemoryMetadata metadata)
348378
{
349379
assertThat(metadata.listTables(SESSION, Optional.empty()))

0 commit comments

Comments
 (0)