Skip to content

Commit 0eb4869

Browse files
feat(TableService): AlterTable now supports index renaming
1 parent c91ad03 commit 0eb4869

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
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
@@ -45,6 +45,7 @@
4545
import tech.ydb.table.description.ColumnFamily;
4646
import tech.ydb.table.description.KeyBound;
4747
import tech.ydb.table.description.KeyRange;
48+
import tech.ydb.table.description.RenameIndex;
4849
import tech.ydb.table.description.StoragePool;
4950
import tech.ydb.table.description.TableColumn;
5051
import tech.ydb.table.description.TableDescription;
@@ -567,6 +568,13 @@ public CompletableFuture<Status> alterTable(String path, AlterTableSettings sett
567568
builder.addDropIndexes(dropIndex);
568569
}
569570

571+
for (RenameIndex renameIndex : settings.getRenameIndexes()) {
572+
builder.addRenameIndexes(YdbTable.RenameIndexItem.newBuilder()
573+
.setSourceName(renameIndex.getSourceName())
574+
.setDestinationName(renameIndex.getDestinationName())
575+
.setReplaceDestination(renameIndex.isReplaceDestination()).build());
576+
}
577+
570578
String traceId = getTraceIdOrGenerateNew(settings.getTraceId());
571579
final GrpcRequestSettings grpcRequestSettings = makeGrpcRequestSettings(settings.getTimeoutDuration(), traceId);
572580
return tableRpc.alterTable(builder.build(), grpcRequestSettings);

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
@@ -111,6 +115,16 @@ public AlterTableSettings addGlobalAsyncIndex(String name, List<String> columns,
111115
return this;
112116
}
113117

118+
public AlterTableSettings addRenameIndex(String oldName, String newName) {
119+
renameIndices.add(new RenameIndex(oldName, newName, false));
120+
return this;
121+
}
122+
123+
public AlterTableSettings addRenameIndex(String oldName, String newName, boolean replaceExisting) {
124+
renameIndices.add(new RenameIndex(oldName, newName, replaceExisting));
125+
return this;
126+
}
127+
114128
public AlterTableSettings dropIndex(String index) {
115129
dropIndexes.add(index);
116130
return this;
@@ -160,6 +174,10 @@ public Collection<String> getDropIndexes() {
160174
return dropIndexes;
161175
}
162176

177+
public Collection<RenameIndex> getRenameIndexes() {
178+
return renameIndices;
179+
}
180+
163181
@Nullable
164182
public TableTtl getTableTTL() {
165183
return ttl;

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void dropTable() {
5151

5252
@Test
5353
public void alterTableTest() {
54-
// --------------------- craete table -----------------------------
54+
// --------------------- create table -----------------------------
5555
TableDescription createTableDesc = TableDescription.newBuilder()
5656
.addNonnullColumn("id", PrimitiveType.Uint64)
5757
.addNullableColumn("code", PrimitiveType.Text)
@@ -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", Arrays.asList("data"), Arrays.asList("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
private void assertColumn(TableColumn column, String name, Type type) {

0 commit comments

Comments
 (0)