Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Table: AlterTable supports index renaming

## 2.3.20 ##
* Table: Fixed session status updating on stream calls
* Core: Added endpoint pessimization when CreateSession returns OVERLOADED
Expand Down
1 change: 1 addition & 0 deletions table/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<environmentVariables>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
<YDB_DOCKER_IMAGE>ydbplatform/local-ydb:trunk</YDB_DOCKER_IMAGE>
<YDB_FEATURE_FLAGS>enable_vector_index</YDB_FEATURE_FLAGS>
</environmentVariables>
</configuration>
</plugin>
Expand Down
29 changes: 29 additions & 0 deletions table/src/main/java/tech/ydb/table/description/RenameIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tech.ydb.table.description;

/**
* @author Kirill Kurdyukov
*/
public class RenameIndex {

private final String sourceName;
private final String destinationName;
private final boolean replaceDestination;

public RenameIndex(String sourceName, String destinationName, boolean replaceDestination) {
this.sourceName = sourceName;
this.destinationName = destinationName;
this.replaceDestination = replaceDestination;
}

public String getSourceName() {
return sourceName;
}

public String getDestinationName() {
return destinationName;
}

public boolean isReplaceDestination() {
return replaceDestination;
}
}
8 changes: 8 additions & 0 deletions table/src/main/java/tech/ydb/table/impl/BaseSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import tech.ydb.table.description.ColumnFamily;
import tech.ydb.table.description.KeyBound;
import tech.ydb.table.description.KeyRange;
import tech.ydb.table.description.RenameIndex;
import tech.ydb.table.description.StoragePool;
import tech.ydb.table.description.TableColumn;
import tech.ydb.table.description.TableDescription;
Expand Down Expand Up @@ -576,6 +577,13 @@ public CompletableFuture<Status> alterTable(String path, AlterTableSettings sett
builder.addDropIndexes(dropIndex);
}

for (RenameIndex renameIndex : settings.getRenameIndexes()) {
builder.addRenameIndexes(YdbTable.RenameIndexItem.newBuilder()
.setSourceName(renameIndex.getSourceName())
.setDestinationName(renameIndex.getDestinationName())
.setReplaceDestination(renameIndex.isReplaceDestination()).build());
}

return rpc.alterTable(builder.build(), makeOptions(settings).build());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.ydb.table.settings;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -11,6 +12,7 @@

import javax.annotation.Nullable;

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

private final List<RenameIndex> renameIndices = new ArrayList<>();

@Nullable
private TableTtl ttl;
@Nullable
Expand Down Expand Up @@ -119,6 +123,16 @@ public AlterTableSettings addGlobalAsyncIndex(String name, List<String> columns,
return this;
}

public AlterTableSettings addRenameIndex(String oldName, String newName) {
renameIndices.add(new RenameIndex(oldName, newName, false));
return this;
}

public AlterTableSettings addRenameIndex(String oldName, String newName, boolean replaceExisting) {
renameIndices.add(new RenameIndex(oldName, newName, replaceExisting));
return this;
}

public AlterTableSettings dropIndex(String index) {
dropIndexes.add(index);
return this;
Expand Down Expand Up @@ -168,6 +182,10 @@ public Collection<String> getDropIndexes() {
return dropIndexes;
}

public Collection<RenameIndex> getRenameIndexes() {
return renameIndices;
}

@Nullable
public TableTtl getTableTTL() {
return ttl;
Expand Down
56 changes: 50 additions & 6 deletions table/src/test/java/tech/ydb/table/integration/AlterTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void alterTableTest() {
Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess());

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

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

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

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

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

description = describeResult.getValue();
Expand Down Expand Up @@ -163,7 +163,7 @@ public void alterTableWithSerialTest() {
Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess());

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

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

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

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

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

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

@Test
public void renameIndexTest() {
// --------------------- create table -----------------------------
ctx.supplyStatus(session -> session.executeSchemeQuery(""
+ "CREATE TABLE " + TABLE_NAME + " ("
+ " id Uint64 NOT NULL,"
+ " code Text NOT NULL,"
+ " size Float,"
+ " created Timestamp,"
+ " data Text,"
+ " PRIMARY KEY(id),"
+ " INDEX idx1 GLOBAL ON (id, code),"
+ " INDEX idx2 GLOBAL ASYNC ON (data) COVER (code)"
+ ")"
)).join().expectSuccess();

Status alterStatus = ctx.supplyStatus(
session -> session.alterTable(tablePath, new AlterTableSettings()
.addRenameIndex("idx1", "new_name"))
).join();
Assert.assertTrue("Alter table with rename indexes " + alterStatus, alterStatus.isSuccess());

Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
Assert.assertTrue("Describe table after altering " + describeResult.getStatus(), describeResult.isSuccess());

TableDescription description = describeResult.getValue();
Assert.assertEquals(2, description.getIndexes().size());
assertIndexSync(description.getIndexes().get(1), "new_name", Arrays.asList("id", "code"), Collections.emptyList());

alterStatus = ctx.supplyStatus(
session -> session.alterTable(tablePath, new AlterTableSettings()
.addRenameIndex("new_name", "idx2", true))
).join();
Assert.assertTrue("Alter table with rename indexes " + alterStatus, alterStatus.isSuccess());

describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
Assert.assertTrue("Describe table after altering " + describeResult.getStatus(), describeResult.isSuccess());

description = describeResult.getValue();

Assert.assertEquals(1, description.getIndexes().size());
assertIndexSync(description.getIndexes().get(0), "idx2", Arrays.asList("id", "code"), Collections.emptyList());
}

private void assertColumn(TableColumn column, String name, Type type) {
assertColumn(column, name, type, false, false);
}
Expand Down