Skip to content

Commit 5800199

Browse files
feat(TableService): AlterTable now supports index renaming
1 parent 3b6d6e6 commit 5800199

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tech.ydb.table.description;
2+
3+
/**
4+
* @author Kirill Kurdyukov
5+
*/
6+
public class RenameIndex {
7+
8+
private final String sourceName;
9+
private final String destinationName;
10+
private final boolean replaceDestination;
11+
12+
public RenameIndex(String sourceName, String destinationName, boolean replaceDestination) {
13+
this.sourceName = sourceName;
14+
this.destinationName = destinationName;
15+
this.replaceDestination = replaceDestination;
16+
}
17+
18+
public String getSourceName() {
19+
return sourceName;
20+
}
21+
22+
public String getDestinationName() {
23+
return destinationName;
24+
}
25+
26+
public boolean isReplaceDestination() {
27+
return replaceDestination;
28+
}
29+
}

table/src/main/java/tech/ydb/table/impl/BaseSession.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import tech.ydb.table.description.ColumnFamily;
5050
import tech.ydb.table.description.KeyBound;
5151
import tech.ydb.table.description.KeyRange;
52+
import tech.ydb.table.description.RenameIndex;
5253
import tech.ydb.table.description.StoragePool;
5354
import tech.ydb.table.description.TableColumn;
5455
import tech.ydb.table.description.TableDescription;
@@ -576,6 +577,13 @@ public CompletableFuture<Status> alterTable(String path, AlterTableSettings sett
576577
builder.addDropIndexes(dropIndex);
577578
}
578579

580+
for (RenameIndex renameIndex : settings.getRenameIndexes()) {
581+
builder.addRenameIndexes(YdbTable.RenameIndexItem.newBuilder()
582+
.setSourceName(renameIndex.getSourceName())
583+
.setDestinationName(renameIndex.getDestinationName())
584+
.setReplaceDestination(renameIndex.isReplaceDestination()).build());
585+
}
586+
579587
return rpc.alterTable(builder.build(), makeOptions(settings).build());
580588
}
581589

table/src/main/java/tech/ydb/table/settings/AlterTableSettings.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tech.ydb.table.settings;
22

3+
import java.util.ArrayList;
34
import java.util.Collection;
45
import java.util.HashMap;
56
import java.util.HashSet;
@@ -11,6 +12,7 @@
1112

1213
import javax.annotation.Nullable;
1314

15+
import tech.ydb.table.description.RenameIndex;
1416
import tech.ydb.table.description.TableColumn;
1517
import tech.ydb.table.description.TableIndex;
1618
import tech.ydb.table.description.TableTtl;
@@ -31,6 +33,8 @@ public class AlterTableSettings extends RequestSettings<AlterTableSettings> {
3133
private final Set<String> dropChangefeeds = new HashSet<>();
3234
private final Set<String> dropIndexes = new HashSet<>();
3335

36+
private final List<RenameIndex> renameIndices = new ArrayList<>();
37+
3438
@Nullable
3539
private TableTtl ttl;
3640
@Nullable
@@ -119,6 +123,16 @@ public AlterTableSettings addGlobalAsyncIndex(String name, List<String> columns,
119123
return this;
120124
}
121125

126+
public AlterTableSettings addRenameIndex(String oldName, String newName) {
127+
renameIndices.add(new RenameIndex(oldName, newName, false));
128+
return this;
129+
}
130+
131+
public AlterTableSettings addRenameIndex(String oldName, String newName, boolean replaceExisting) {
132+
renameIndices.add(new RenameIndex(oldName, newName, replaceExisting));
133+
return this;
134+
}
135+
122136
public AlterTableSettings dropIndex(String index) {
123137
dropIndexes.add(index);
124138
return this;
@@ -168,6 +182,10 @@ public Collection<String> getDropIndexes() {
168182
return dropIndexes;
169183
}
170184

185+
public Collection<RenameIndex> getRenameIndexes() {
186+
return renameIndices;
187+
}
188+
171189
@Nullable
172190
public TableTtl getTableTTL() {
173191
return ttl;

table/src/test/java/tech/ydb/table/integration/AlterTableTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void alterTableTest() {
6969
Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess());
7070

7171
// --------------------- describe table after creating -----------------------------
72-
Result<TableDescription> describeResult = ctx.supplyResult(session ->session.describeTable(tablePath)).join();
72+
Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
7373
Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess());
7474

7575
TableDescription description = describeResult.getValue();
@@ -99,7 +99,7 @@ public void alterTableTest() {
9999
Assert.assertTrue("Alter table with column " + alterStatus, alterStatus.isSuccess());
100100

101101
// --------------------- describe table after first altering -----------------------------
102-
describeResult = ctx.supplyResult(session ->session.describeTable(tablePath)).join();
102+
describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
103103
Assert.assertTrue("Describe table after altering " + describeResult.getStatus(), describeResult.isSuccess());
104104

105105
description = describeResult.getValue();
@@ -126,7 +126,7 @@ public void alterTableTest() {
126126
Assert.assertTrue("Alter table with indexes " + alterStatus, alterStatus.isSuccess());
127127

128128
// --------------------- describe table after first altering -----------------------------
129-
describeResult = ctx.supplyResult(session ->session.describeTable(tablePath)).join();
129+
describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
130130
Assert.assertTrue("Describe table after altering " + describeResult.getStatus(), describeResult.isSuccess());
131131

132132
description = describeResult.getValue();
@@ -143,6 +143,20 @@ public void alterTableTest() {
143143

144144
Assert.assertEquals(1, description.getIndexes().size());
145145
assertIndexAsync(description.getIndexes().get(0), "idx2", Collections.singletonList("data"), Collections.singletonList("code"));
146+
147+
// // --------------------- alter table with rename indexes -----------------------------
148+
// alterStatus = ctx.supplyStatus(
149+
// session -> session.alterTable(tablePath, new AlterTableSettings()
150+
// .addRenameIndex("idx2", "new_name"))
151+
// ).join();
152+
// Assert.assertTrue("Alter table with rename indexes " + alterStatus, alterStatus.isSuccess());
153+
//
154+
// // --------------------- describe table after rename indexes altering -----------------------------
155+
// describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
156+
// Assert.assertTrue("Describe table after altering " + describeResult.getStatus(), describeResult.isSuccess());
157+
//
158+
// Assert.assertEquals(1, description.getIndexes().size());
159+
// assertIndexAsync(description.getIndexes().get(0), "new_name", Arrays.asList("data"), Arrays.asList("code"));
146160
}
147161

148162
@Test

0 commit comments

Comments
 (0)