Skip to content

Commit 90338cb

Browse files
feat(TableService): CreateTable with default value options
1 parent fd55a56 commit 90338cb

File tree

2 files changed

+125
-9
lines changed

2 files changed

+125
-9
lines changed

table/src/main/java/tech/ydb/table/description/TableDescription.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,34 @@ public Builder addColumn(String name, Type type, String family, PrimitiveValue d
159159
return this;
160160
}
161161

162+
public Builder addSequenceColumn(String name, Type type) {
163+
return addSequenceColumn(name, type, null, SequenceDescription.newBuilder().build());
164+
}
165+
166+
public Builder addSequenceColumn(String name, Type type, String family) {
167+
return addSequenceColumn(name, type, family, SequenceDescription.newBuilder().build());
168+
}
169+
170+
public Builder addSequenceColumn(String name, Type type, String family, SequenceDescription sequenceDescription) {
171+
if (type instanceof PrimitiveType) {
172+
PrimitiveType primitiveType = (PrimitiveType) type;
173+
174+
switch (primitiveType) {
175+
case Int16:
176+
columns.put(name, new TableColumn(name, PrimitiveType.Int16, family, sequenceDescription));
177+
return this;
178+
case Int32:
179+
columns.put(name, new TableColumn(name, PrimitiveType.Int32, family, sequenceDescription));
180+
return this;
181+
case Int64:
182+
columns.put(name, new TableColumn(name, PrimitiveType.Int64, family, sequenceDescription));
183+
return this;
184+
}
185+
}
186+
187+
throw new IllegalArgumentException("Type " + type + " cannot be used as a sequence column");
188+
}
189+
162190
public Builder addSmallSerialColumn(String name) {
163191
return addSmallSerialColumn(name, null, SequenceDescription.newBuilder().build());
164192
}
@@ -184,18 +212,15 @@ public Builder addBigSerialColumn(String name, SequenceDescription sequenceDescr
184212
}
185213

186214
public Builder addSmallSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
187-
columns.put(name, new TableColumn(name, PrimitiveType.Int16, family, sequenceDescription));
188-
return this;
215+
return addSequenceColumn(name, PrimitiveType.Int16, family, sequenceDescription);
189216
}
190217

191218
public Builder addSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
192-
columns.put(name, new TableColumn(name, PrimitiveType.Int32, family, sequenceDescription));
193-
return this;
219+
return addSequenceColumn(name, PrimitiveType.Int32, family, sequenceDescription);
194220
}
195221

196222
public Builder addBigSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
197-
columns.put(name, new TableColumn(name, PrimitiveType.Int64, family, sequenceDescription));
198-
return this;
223+
return addSequenceColumn(name, PrimitiveType.Int64, family, sequenceDescription);
199224
}
200225

201226
public Builder addKeyRange(KeyRange value) {

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

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import tech.ydb.table.values.PrimitiveValue;
1818
import tech.ydb.test.junit4.GrpcTransportRule;
1919

20+
import java.util.stream.Collectors;
21+
2022
/**
2123
* @author Kirill Kurdyukov
2224
*/
@@ -86,15 +88,13 @@ public void bigSerialTest() {
8688
Status createStatus = ctx.supplyStatus(session ->
8789
session.createTable(tablePath, tableDescription)).join();
8890
Assert.assertTrue("Create table with big serial " + createStatus, createStatus.isSuccess());
89-
9091
Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
9192
Assert.assertTrue("Describe table with big serial " + describeResult.getStatus(), describeResult.isSuccess());
9293
TableDescription description = describeResult.getValue();
9394
TableColumn tableColumn = description.getColumns().get(0);
9495
Assert.assertNotNull(tableColumn.getSequenceDescription());
9596
Assert.assertEquals("sequence_default", tableColumn.getSequenceDescription().getName());
9697
Assert.assertEquals(PrimitiveType.Int64, tableColumn.getType());
97-
9898
ctx.supplyStatus(session -> session.dropTable(tablePath)).join();
9999
}
100100

@@ -115,7 +115,6 @@ public void defaultValueTest() {
115115
Assert.assertNull(tableColumn.getSequenceDescription());
116116
Assert.assertEquals("text", tableColumn.getLiteralDefaultValue().getText());
117117
Assert.assertEquals(PrimitiveType.Text, tableColumn.getType());
118-
119118
ctx.supplyStatus(session -> session.dropTable(tablePath)).join();
120119
}
121120

@@ -154,4 +153,96 @@ public void customSequenceDescriptionTest() {
154153
Assert.assertEquals(12L, (long) sequenceDescription.getStartValue());
155154
ctx.supplyStatus(session -> session.dropTable(tablePath)).join();
156155
}
156+
157+
@Test
158+
public void copyTableWithSequenceAndDefaultValue() {
159+
String tablePath = ydbTransport.getDatabase() + "/copy_table";
160+
161+
TableDescription tableDescription = TableDescription.newBuilder()
162+
.addBigSerialColumn("id", SequenceDescription.newBuilder()
163+
.setCache(5L)
164+
.setMaxValue((long) Integer.MAX_VALUE)
165+
.setMinValue(10L)
166+
.setName("custom_sequence_description")
167+
.setCycle(true)
168+
.setIncrement(2L)
169+
.setStartValue(12L)
170+
.build()
171+
)
172+
.addColumn("name", PrimitiveType.Text, PrimitiveValue.newText("new_name"))
173+
.addNonnullColumn("another_name", PrimitiveType.Bool)
174+
.setPrimaryKey("id")
175+
.build();
176+
Status createStatus = ctx.supplyStatus(session -> session.createTable(tablePath, tableDescription)).join();
177+
Assert.assertTrue("Create table " + createStatus, createStatus.isSuccess());
178+
Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
179+
Assert.assertTrue("Describe table " + describeResult.getStatus(),
180+
describeResult.isSuccess());
181+
TableDescription description = describeResult.getValue();
182+
Assert.assertEquals(3, description.getColumns().size());
183+
SequenceDescription sequenceDescription = description.getColumns().get(0).getSequenceDescription();
184+
Assert.assertNotNull(sequenceDescription);
185+
Assert.assertEquals("custom_sequence_description", sequenceDescription.getName());
186+
Assert.assertEquals(Integer.MAX_VALUE, sequenceDescription.getMaxValue().intValue());
187+
Assert.assertEquals(10L, (long) sequenceDescription.getMinValue());
188+
Assert.assertEquals(5L, (long) sequenceDescription.getCache());
189+
Assert.assertEquals(true, sequenceDescription.getCycle());
190+
Assert.assertEquals(2L, (long) sequenceDescription.getIncrement());
191+
Assert.assertEquals(12L, (long) sequenceDescription.getStartValue());
192+
TableColumn tableColumn = describeResult.getValue().getColumns().get(1);
193+
Assert.assertNull(tableColumn.getSequenceDescription());
194+
Assert.assertEquals("new_name", tableColumn.getLiteralDefaultValue().getText());
195+
Assert.assertEquals(PrimitiveType.Text, tableColumn.getType());
196+
TableColumn anotherTableColumn = describeResult.getValue().getColumns().get(2);
197+
Assert.assertNull(anotherTableColumn.getSequenceDescription());
198+
Assert.assertNull(anotherTableColumn.getLiteralDefaultValue());
199+
Assert.assertEquals("new_name", tableColumn.getLiteralDefaultValue().getText());
200+
Assert.assertEquals(PrimitiveType.Text, tableColumn.getType());
201+
202+
TableDescription.Builder copyTableDescription = TableDescription.newBuilder();
203+
204+
for (TableColumn columnTable : description.getColumns()) {
205+
if (columnTable.getSequenceDescription() != null) {
206+
copyTableDescription.addSequenceColumn("copy_" + columnTable.getName(),
207+
columnTable.getType(), columnTable.getFamily(), columnTable.getSequenceDescription());
208+
209+
continue;
210+
}
211+
212+
copyTableDescription.addColumn(columnTable.getName(), columnTable.getType(), columnTable.getFamily(),
213+
columnTable.getLiteralDefaultValue());
214+
215+
}
216+
217+
copyTableDescription.setPrimaryKeys(description.getPrimaryKeys().stream()
218+
.map(key -> "copy_" + key).collect(Collectors.toList()));
219+
220+
createStatus = ctx.supplyStatus(session -> session.createTable(tablePath + "_copy", tableDescription)).join();
221+
Assert.assertTrue("Create copy table " + createStatus, createStatus.isSuccess());
222+
describeResult = ctx.supplyResult(session -> session.describeTable(tablePath + "_copy")).join();
223+
Assert.assertTrue("Describe copy table " + describeResult.getStatus(),
224+
describeResult.isSuccess());
225+
description = describeResult.getValue();
226+
Assert.assertEquals(3, description.getColumns().size());
227+
sequenceDescription = description.getColumns().get(0).getSequenceDescription();
228+
Assert.assertNotNull(sequenceDescription);
229+
Assert.assertEquals("custom_sequence_description", sequenceDescription.getName());
230+
Assert.assertEquals(Integer.MAX_VALUE, sequenceDescription.getMaxValue().intValue());
231+
Assert.assertEquals(10L, (long) sequenceDescription.getMinValue());
232+
Assert.assertEquals(5L, (long) sequenceDescription.getCache());
233+
Assert.assertEquals(true, sequenceDescription.getCycle());
234+
Assert.assertEquals(2L, (long) sequenceDescription.getIncrement());
235+
Assert.assertEquals(12L, (long) sequenceDescription.getStartValue());
236+
tableColumn = describeResult.getValue().getColumns().get(1);
237+
Assert.assertNull(tableColumn.getSequenceDescription());
238+
Assert.assertEquals("new_name", tableColumn.getLiteralDefaultValue().getText());
239+
Assert.assertEquals(PrimitiveType.Text, tableColumn.getType());
240+
anotherTableColumn = describeResult.getValue().getColumns().get(2);
241+
Assert.assertNull(anotherTableColumn.getSequenceDescription());
242+
Assert.assertNull(anotherTableColumn.getLiteralDefaultValue());
243+
Assert.assertEquals("new_name", tableColumn.getLiteralDefaultValue().getText());
244+
Assert.assertEquals(PrimitiveType.Text, tableColumn.getType());
245+
246+
ctx.supplyStatus(session -> session.dropTable(tablePath)).join();
247+
}
157248
}

0 commit comments

Comments
 (0)