Skip to content

Commit 29e756f

Browse files
feat(TableService): CreateTable with default value options
1 parent 0fadff6 commit 29e756f

File tree

4 files changed

+220
-5
lines changed

4 files changed

+220
-5
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public Boolean getNextUsed() {
6868
}
6969
}
7070

71+
@Nullable
7172
private final String name;
7273
@Nullable
7374
private final Long minValue;
@@ -100,7 +101,7 @@ public static Builder newBuilder() {
100101
}
101102

102103
public static class Builder {
103-
private String name;
104+
private String name = "sequence_default";
104105
private Long minValue;
105106
private Long maxValue;
106107
private Long startValue;

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import tech.ydb.table.settings.AlterTableSettings;
1919
import tech.ydb.table.settings.PartitioningSettings;
2020
import tech.ydb.table.values.OptionalType;
21+
import tech.ydb.table.values.PrimitiveType;
22+
import tech.ydb.table.values.PrimitiveValue;
2123
import tech.ydb.table.values.Type;
2224

2325
/**
@@ -139,14 +141,63 @@ public Builder addColumn(TableColumn column) {
139141
}
140142

141143
public Builder addNonnullColumn(String name, Type type) {
142-
return addNonnullColumn(name, type, null);
144+
return addNonnullColumn(name, type, (String) null);
143145
}
144146

145147
public Builder addNonnullColumn(String name, Type type, String family) {
146148
columns.put(name, new TableColumn(name, type, family));
147149
return this;
148150
}
149151

152+
public Builder addColumn(String name, Type type, PrimitiveValue defaultValue) {
153+
columns.put(name, new TableColumn(name, type, null, defaultValue));
154+
return this;
155+
}
156+
157+
public Builder addColumn(String name, Type type, String family, PrimitiveValue defaultValue) {
158+
columns.put(name, new TableColumn(name, type, family, defaultValue));
159+
return this;
160+
}
161+
162+
public Builder addSmallSerialColumn(String name) {
163+
return addSmallSerialColumn(name, null, SequenceDescription.newBuilder().build());
164+
}
165+
166+
public Builder addSerialColumn(String name) {
167+
return addSerialColumn(name, null, SequenceDescription.newBuilder().build());
168+
}
169+
170+
public Builder addBigSerialColumn(String name) {
171+
return addBigSerialColumn(name, null, SequenceDescription.newBuilder().build());
172+
}
173+
174+
public Builder addSmallSerialColumn(String name, SequenceDescription sequenceDescription) {
175+
return addSmallSerialColumn(name, null, sequenceDescription);
176+
}
177+
178+
public Builder addSerialColumn(String name, SequenceDescription sequenceDescription) {
179+
return addSerialColumn(name, null, sequenceDescription);
180+
}
181+
182+
public Builder addBigSerialColumn(String name, SequenceDescription sequenceDescription) {
183+
return addBigSerialColumn(name, null, sequenceDescription);
184+
}
185+
186+
public Builder addSmallSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
187+
columns.put(name, new TableColumn(name, PrimitiveType.Int16, family, sequenceDescription));
188+
return this;
189+
}
190+
191+
public Builder addSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
192+
columns.put(name, new TableColumn(name, PrimitiveType.Int32, family, sequenceDescription));
193+
return this;
194+
}
195+
196+
public Builder addBigSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
197+
columns.put(name, new TableColumn(name, PrimitiveType.Int64, family, sequenceDescription));
198+
return this;
199+
}
200+
150201
public Builder addKeyRange(KeyRange value) {
151202
keyRanges.add(value);
152203
return this;

table/src/main/java/tech/ydb/table/impl/BaseSession.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@
6363
import tech.ydb.table.query.Params;
6464
import tech.ydb.table.query.ReadRowsResult;
6565
import tech.ydb.table.query.ReadTablePart;
66-
import tech.ydb.table.result.PrimitiveReader;
6766
import tech.ydb.table.result.ResultSetReader;
68-
import tech.ydb.table.result.ValueReader;
6967
import tech.ydb.table.result.impl.ProtoValueReaders;
7068
import tech.ydb.table.rpc.TableRpc;
7169
import tech.ydb.table.settings.AlterTableSettings;
@@ -110,7 +108,6 @@
110108
import tech.ydb.table.values.proto.ProtoType;
111109
import tech.ydb.table.values.proto.ProtoValue;
112110

113-
114111
/**
115112
* @author Sergey Polovko
116113
* @author Alexandr Gorshenin
@@ -255,6 +252,8 @@ private static YdbTable.ColumnMeta buildColumnMeta(TableColumn column) {
255252
if (setVal.getNextUsed() != null) {
256253
setValBuilder.setNextUsed(setVal.getNextUsed());
257254
}
255+
256+
sequenceDescriptionBuilder.setSetVal(setValBuilder.build());
258257
}
259258

260259
if (sequenceDescription.getMinValue() != null) {
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package tech.ydb.table.integration;
2+
3+
import org.junit.Assert;
4+
import org.junit.ClassRule;
5+
6+
import org.junit.Test;
7+
8+
import tech.ydb.core.Result;
9+
import tech.ydb.core.Status;
10+
import tech.ydb.table.SessionRetryContext;
11+
import tech.ydb.table.description.SequenceDescription;
12+
import tech.ydb.table.description.TableColumn;
13+
import tech.ydb.table.description.TableDescription;
14+
import tech.ydb.table.impl.SimpleTableClient;
15+
import tech.ydb.table.rpc.grpc.GrpcTableRpc;
16+
import tech.ydb.table.settings.CreateTableSettings;
17+
import tech.ydb.table.values.PrimitiveType;
18+
import tech.ydb.table.values.PrimitiveValue;
19+
import tech.ydb.test.junit4.GrpcTransportRule;
20+
21+
/**
22+
* @author Kirill Kurdyukov
23+
*/
24+
public class CreateTableTest {
25+
26+
@ClassRule
27+
public final static GrpcTransportRule ydbTransport = new GrpcTransportRule();
28+
29+
private final SimpleTableClient tableClient = SimpleTableClient.newClient(
30+
GrpcTableRpc.useTransport(ydbTransport)
31+
).build();
32+
33+
private final SessionRetryContext ctx = SessionRetryContext.create(tableClient).build();
34+
35+
@Test
36+
public void smallSerialTest() {
37+
String tablePath = ydbTransport.getDatabase() + "/small_serial_table";
38+
39+
TableDescription tableDescription = TableDescription.newBuilder()
40+
.addSmallSerialColumn("id")
41+
.setPrimaryKey("id")
42+
.build();
43+
44+
Status createStatus = ctx.supplyStatus(session ->
45+
session.createTable(tablePath, tableDescription, new CreateTableSettings())).join();
46+
Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess());
47+
48+
Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
49+
Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess());
50+
TableColumn tableColumn = describeResult.getValue().getColumns().get(0);
51+
Assert.assertNotNull(tableColumn.getSequenceDescription());
52+
Assert.assertEquals("sequence_default", tableColumn.getSequenceDescription().getName());
53+
Assert.assertEquals(PrimitiveType.Int16, tableColumn.getType());
54+
}
55+
56+
@Test
57+
public void serialTest() {
58+
String tablePath = ydbTransport.getDatabase() + "/serial_table";
59+
60+
TableDescription tableDescription = TableDescription.newBuilder()
61+
.addSerialColumn("id")
62+
.setPrimaryKey("id")
63+
.build();
64+
65+
Status createStatus = ctx.supplyStatus(session ->
66+
session.createTable(tablePath, tableDescription, new CreateTableSettings())).join();
67+
Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess());
68+
69+
Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
70+
Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess());
71+
TableColumn tableColumn = describeResult.getValue().getColumns().get(0);
72+
Assert.assertNotNull(tableColumn.getSequenceDescription());
73+
Assert.assertEquals("sequence_default", tableColumn.getSequenceDescription().getName());
74+
Assert.assertEquals(PrimitiveType.Int32, tableColumn.getType());
75+
76+
ctx.supplyStatus(session -> session.dropTable(tablePath)).join();
77+
}
78+
79+
@Test
80+
public void bigSerialTest() {
81+
String tablePath = ydbTransport.getDatabase() + "/big_serial_table";
82+
83+
TableDescription tableDescription = TableDescription.newBuilder()
84+
.addBigSerialColumn("id")
85+
.setPrimaryKey("id")
86+
.build();
87+
88+
Status createStatus = ctx.supplyStatus(session ->
89+
session.createTable(tablePath, tableDescription, new CreateTableSettings())).join();
90+
Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess());
91+
92+
Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
93+
Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess());
94+
TableDescription description = describeResult.getValue();
95+
TableColumn tableColumn = description.getColumns().get(0);
96+
Assert.assertNotNull(tableColumn.getSequenceDescription());
97+
Assert.assertEquals("sequence_default", tableColumn.getSequenceDescription().getName());
98+
Assert.assertEquals(PrimitiveType.Int64, tableColumn.getType());
99+
100+
ctx.supplyStatus(session -> session.dropTable(tablePath)).join();
101+
}
102+
103+
@Test
104+
public void defaultValueTest() {
105+
String tablePath = ydbTransport.getDatabase() + "/default_value_table";
106+
107+
TableDescription tableDescription = TableDescription.newBuilder()
108+
.addColumn("id", PrimitiveType.Text, PrimitiveValue.newText("text"))
109+
.setPrimaryKey("id")
110+
.build();
111+
112+
Status createStatus = ctx.supplyStatus(session ->
113+
session.createTable(tablePath, tableDescription, new CreateTableSettings())).join();
114+
Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess());
115+
116+
Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
117+
Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess());
118+
TableColumn tableColumn = describeResult.getValue().getColumns().get(0);
119+
Assert.assertNull(tableColumn.getSequenceDescription());
120+
Assert.assertEquals("text", tableColumn.getLiteralDefaultValue().getText());
121+
Assert.assertEquals(PrimitiveType.Text, tableColumn.getType());
122+
123+
ctx.supplyStatus(session -> session.dropTable(tablePath)).join();
124+
}
125+
126+
@Test
127+
public void customSequenceDescriptionTest() {
128+
String tablePath = ydbTransport.getDatabase() + "/default_value_table";
129+
130+
TableDescription tableDescription = TableDescription.newBuilder()
131+
.addBigSerialColumn("id", SequenceDescription.newBuilder()
132+
.setCache(5L)
133+
.setMaxValue((long) Integer.MAX_VALUE)
134+
.setMinValue(10L)
135+
.setName("custom_sequence_description")
136+
.setCycle(true)
137+
.setSetVal(11L, true)
138+
.setIncrement(2L)
139+
.setStartValue(12L)
140+
.build()
141+
)
142+
.setPrimaryKey("id")
143+
.build();
144+
145+
Status createStatus = ctx.supplyStatus(session ->
146+
session.createTable(tablePath, tableDescription, new CreateTableSettings())).join();
147+
Assert.assertTrue("Create table with indexes " + createStatus, createStatus.isSuccess());
148+
149+
Result<TableDescription> describeResult = ctx.supplyResult(session -> session.describeTable(tablePath)).join();
150+
Assert.assertTrue("Describe table with indexes " + describeResult.getStatus(), describeResult.isSuccess());
151+
TableDescription description = describeResult.getValue();
152+
SequenceDescription sequenceDescription = description.getColumns().get(0).getSequenceDescription();
153+
Assert.assertNotNull(sequenceDescription);
154+
Assert.assertEquals("custom_sequence_description", sequenceDescription.getName());
155+
Assert.assertEquals(Integer.MAX_VALUE, sequenceDescription.getMaxValue().intValue());
156+
Assert.assertEquals(10L, (long) sequenceDescription.getMinValue());
157+
Assert.assertEquals(5L, (long) sequenceDescription.getCache());
158+
Assert.assertEquals(true, sequenceDescription.getCycle());
159+
Assert.assertEquals(2L, (long) sequenceDescription.getIncrement());
160+
Assert.assertEquals(12L, (long) sequenceDescription.getStartValue());
161+
162+
ctx.supplyStatus(session -> session.dropTable(tablePath)).join();
163+
}
164+
}

0 commit comments

Comments
 (0)