Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -11,6 +12,7 @@
import javax.annotation.Nullable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

import tech.ydb.table.Session;
Expand Down Expand Up @@ -48,6 +50,8 @@ public enum StoreType {

private final TableTtl tableTtl;

private final Map<String, String> attributes;

private TableDescription(Builder builder) {
this.storeType = builder.storeType;
this.primaryKeys = ImmutableList.copyOf(builder.primaryKeys);
Expand All @@ -61,6 +65,7 @@ private TableDescription(Builder builder) {
this.partitionStats = ImmutableList.copyOf(builder.partitionStats);
this.tableTtl = builder.ttlSettings;
this.changefeeds = builder.changefeeds;
this.attributes = ImmutableMap.copyOf(builder.attributes);
}

public static Builder newBuilder() {
Expand Down Expand Up @@ -113,6 +118,10 @@ public List<ChangefeedDescription> getChangefeeds() {
return changefeeds;
}

public Map<String, String> getAttributes() {
return attributes;
}

/**
* BUILDER
*/
Expand All @@ -129,6 +138,7 @@ public static class Builder {
private final List<PartitionStats> partitionStats = new ArrayList<>();
private TableTtl ttlSettings = TableTtl.notSet();
private final List<ChangefeedDescription> changefeeds = new ArrayList<>();
private final Map<String, String> attributes = new HashMap<>();

public Builder setStoreType(StoreType storeType) {
this.storeType = storeType;
Expand Down Expand Up @@ -363,6 +373,17 @@ public Builder addChangefeed(ChangefeedDescription changefeed) {
return this;
}

public Builder addAttribute(String name, String value) {
this.attributes.put(name, value);
return this;
}

public Builder setAttributes(Map<String, String> attrs) {
this.attributes.clear();
this.attributes.putAll(attrs);
return this;
}

@Deprecated
public Builder setTtlSettings(int ttlModeCase, String columnName, int expireAfterSeconds) {
this.ttlSettings = new TableTtl(TtlMode.forCase(ttlModeCase), columnName, expireAfterSeconds);
Expand Down
12 changes: 12 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 @@ -550,6 +550,10 @@ public CompletableFuture<Status> createTable(
CommonProtos.FeatureFlag.Status.ENABLED : CommonProtos.FeatureFlag.Status.DISABLED);
}

if (!description.getAttributes().isEmpty()) {
request.putAllAttributes(description.getAttributes());
}

return rpc.createTable(request.build(), makeOptions(settings).build());
}

Expand Down Expand Up @@ -628,6 +632,10 @@ public CompletableFuture<Status> alterTable(String path, AlterTableSettings sett
.setReplaceDestination(renameIndex.isReplaceDestination()).build());
}

if (!settings.getAlterAttributes().isEmpty()) {
builder.putAllAlterAttributes(settings.getAlterAttributes());
}

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

Expand Down Expand Up @@ -977,6 +985,10 @@ private static Result<TableDescription> mapDescribeTable(Result<YdbTable.Describ
));
}

if (!desc.getAttributesMap().isEmpty()) {
description.setAttributes(desc.getAttributesMap());
}

return Result.success(description.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class AlterTableSettings extends RequestSettings<AlterTableSettings> {

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

private final Map<String, String> alterAttributes = new HashMap<>();

@Nullable
private TableTtl ttl;
@Nullable
Expand Down Expand Up @@ -200,6 +202,20 @@ public TtlSettings getTtlSettings() {
return new TtlSettings(ttl.getDateTimeColumn(), ttl.getExpireAfterSeconds());
}

public AlterTableSettings alterAttribute(String name, String value) {
alterAttributes.put(name, value);
return this;
}

public AlterTableSettings dropAttribute(String name) {
alterAttributes.put(name, "");
return this;
}

public Map<String, String> getAlterAttributes() {
return alterAttributes;
}

@Nullable
public PartitioningSettings getPartitioningSettings() {
return partitioningSettings;
Expand Down
99 changes: 99 additions & 0 deletions table/src/test/java/tech/ydb/table/integration/AlterTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -283,6 +284,81 @@ public void renameIndexTest() {
assertIndexSync(description.getIndexes().get(0), "idx2", Arrays.asList("id", "code"), Collections.emptyList());
}

/**
* Checks that create table with 3 custom attributes pass
*/
@Test
public void createAttributesTest() {
// --------------------- create table with attributes -----------------------------
Map<String, String> attrs = createTable();

Assert.assertEquals("1", attrs.get("scheme_version"));
Assert.assertEquals("test-service", attrs.get("owner"));
Assert.assertEquals("max", attrs.get("author"));
Assert.assertEquals(3, attrs.size());
}

/**
* Checks that test overwrites an existing attribute
* and adds a new one in a single alter request.
* <p>
* The table is created with three attributes,
* then scheme_version is changed from "code 1" to "code 2"
* and a new {env attribute is added.
* After the alter, expected return four
* attributes with the untouched ones owner, author preserved.
*/
@Test
public void modifyAttributesTest() {
createTable();

// --------------------- alter: modify one attribute, add another -----------------------------
Status alterStatus = ctx.supplyStatus(
session -> session.alterTable(tablePath, new AlterTableSettings()
.alterAttribute("scheme_version", "2")
.alterAttribute("env", "production"))
).join();
Assert.assertTrue("Alter table attributes " + alterStatus, alterStatus.isSuccess());

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

Map<String, String> attrs = describeResult.getValue().getAttributes();
Assert.assertEquals("2", attrs.get("scheme_version"));
Assert.assertEquals("test-service", attrs.get("owner"));
Assert.assertEquals("production", attrs.get("env"));
Assert.assertEquals("max", attrs.get("author"));
Assert.assertEquals(4, attrs.size());
}

/**
* Checks that test removes a single attribute from the table
* and leaves the rest of them intact.
* <p>
* The table is created with three attributes, then owner is dropped.
* After the alter is expected to return only scheme_version and @code author.
*/
@Test
public void dropAttributesTest() {
// --------------------- create table with attributes -----------------------------
createTable();

// --------------------- alter: drop an attribute -----------------------------
Status alterStatus = ctx.supplyStatus(
session -> session.alterTable(tablePath, new AlterTableSettings()
.dropAttribute("owner"))
).join();
Assert.assertTrue("Drop table attribute " + alterStatus, alterStatus.isSuccess());

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

Map<String, String> attrs = describeResult.getValue().getAttributes();
Assert.assertEquals("1", attrs.get("scheme_version"));
Assert.assertEquals("max", attrs.get("author"));
Assert.assertEquals(2, attrs.size());
}

private void assertColumn(TableColumn column, String name, Type type) {
assertColumn(column, name, type, false, false);
}
Expand Down Expand Up @@ -315,4 +391,27 @@ private void assertListEquals(List<String> expected, List<String> values) {
Assert.assertEquals(expected.get(idx), values.get(idx));
}
}

private Map<String, String> createTable() {
// --------------------- create table with attributes -----------------------------
TableDescription createTableDesc = TableDescription.newBuilder()
.addNonnullColumn("id", PrimitiveType.Uint64)
.addNullableColumn("value", PrimitiveType.Text)
.setPrimaryKey("id")
.addAttribute("scheme_version", "1")
.addAttribute("owner", "test-service")
.addAttribute("author", "max")
.build();

Status createStatus = ctx.supplyStatus(
session -> session.createTable(tablePath, createTableDesc, new CreateTableSettings())
).join();
Assert.assertTrue("Create table with attributes " + createStatus, createStatus.isSuccess());

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

return describeResult.getValue().getAttributes();
}
}
Loading