Skip to content

Commit 8dfe1d1

Browse files
feat(TableService): AlterTable supports index renaming (#537)
1 parent 3b6d6e6 commit 8dfe1d1

File tree

6 files changed

+108
-6
lines changed

6 files changed

+108
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Table: AlterTable supports index renaming
2+
13
## 2.3.20 ##
24
* Table: Fixed session status updating on stream calls
35
* Core: Added endpoint pessimization when CreateSession returns OVERLOADED

table/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<environmentVariables>
5252
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
5353
<YDB_DOCKER_IMAGE>ydbplatform/local-ydb:trunk</YDB_DOCKER_IMAGE>
54+
<YDB_FEATURE_FLAGS>enable_vector_index</YDB_FEATURE_FLAGS>
5455
</environmentVariables>
5556
</configuration>
5657
</plugin>
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: 50 additions & 6 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();
@@ -163,7 +163,7 @@ public void alterTableWithSerialTest() {
163163
Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess());
164164

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

169169
TableDescription description = describeResult.getValue();
@@ -193,7 +193,7 @@ public void alterTableWithSerialTest() {
193193
Assert.assertTrue("Alter table with column " + alterStatus, alterStatus.isSuccess());
194194

195195
// --------------------- describe table after first altering -----------------------------
196-
describeResult = ctx.supplyResult(session ->session.describeTable(tablePath)).join();
196+
describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
197197
Assert.assertTrue("Describe table after altering " + describeResult.getStatus(), describeResult.isSuccess());
198198

199199
description = describeResult.getValue();
@@ -220,7 +220,7 @@ public void alterTableWithSerialTest() {
220220
Assert.assertTrue("Alter table with indexes " + alterStatus, alterStatus.isSuccess());
221221

222222
// --------------------- describe table after first altering -----------------------------
223-
describeResult = ctx.supplyResult(session ->session.describeTable(tablePath)).join();
223+
describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
224224
Assert.assertTrue("Describe table after altering " + describeResult.getStatus(), describeResult.isSuccess());
225225

226226
description = describeResult.getValue();
@@ -239,6 +239,50 @@ public void alterTableWithSerialTest() {
239239
assertIndexAsync(description.getIndexes().get(0), "idx2", Collections.singletonList("data"), Collections.singletonList("code"));
240240
}
241241

242+
@Test
243+
public void renameIndexTest() {
244+
// --------------------- create table -----------------------------
245+
ctx.supplyStatus(session -> session.executeSchemeQuery(""
246+
+ "CREATE TABLE " + TABLE_NAME + " ("
247+
+ " id Uint64 NOT NULL,"
248+
+ " code Text NOT NULL,"
249+
+ " size Float,"
250+
+ " created Timestamp,"
251+
+ " data Text,"
252+
+ " PRIMARY KEY(id),"
253+
+ " INDEX idx1 GLOBAL ON (id, code),"
254+
+ " INDEX idx2 GLOBAL ASYNC ON (data) COVER (code)"
255+
+ ")"
256+
)).join().expectSuccess();
257+
258+
Status alterStatus = ctx.supplyStatus(
259+
session -> session.alterTable(tablePath, new AlterTableSettings()
260+
.addRenameIndex("idx1", "new_name"))
261+
).join();
262+
Assert.assertTrue("Alter table with rename indexes " + alterStatus, alterStatus.isSuccess());
263+
264+
Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
265+
Assert.assertTrue("Describe table after altering " + describeResult.getStatus(), describeResult.isSuccess());
266+
267+
TableDescription description = describeResult.getValue();
268+
Assert.assertEquals(2, description.getIndexes().size());
269+
assertIndexSync(description.getIndexes().get(1), "new_name", Arrays.asList("id", "code"), Collections.emptyList());
270+
271+
alterStatus = ctx.supplyStatus(
272+
session -> session.alterTable(tablePath, new AlterTableSettings()
273+
.addRenameIndex("new_name", "idx2", true))
274+
).join();
275+
Assert.assertTrue("Alter table with rename indexes " + alterStatus, alterStatus.isSuccess());
276+
277+
describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
278+
Assert.assertTrue("Describe table after altering " + describeResult.getStatus(), describeResult.isSuccess());
279+
280+
description = describeResult.getValue();
281+
282+
Assert.assertEquals(1, description.getIndexes().size());
283+
assertIndexSync(description.getIndexes().get(0), "idx2", Arrays.asList("id", "code"), Collections.emptyList());
284+
}
285+
242286
private void assertColumn(TableColumn column, String name, Type type) {
243287
assertColumn(column, name, type, false, false);
244288
}

0 commit comments

Comments
 (0)