Skip to content

Commit 8dd8eb9

Browse files
feat(TableService): CreateTable with default value options
1 parent 8dfe1d1 commit 8dd8eb9

File tree

6 files changed

+591
-34
lines changed

6 files changed

+591
-34
lines changed

table/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
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>
5554
</environmentVariables>
5655
</configuration>
5756
</plugin>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package tech.ydb.table.description;
2+
3+
import javax.annotation.Nullable;
4+
5+
/**
6+
* @author Kirill Kurdyukov
7+
*/
8+
public class SequenceDescription {
9+
10+
private final String name;
11+
@Nullable
12+
private final Long minValue;
13+
@Nullable
14+
private final Long maxValue;
15+
@Nullable
16+
private final Long startValue;
17+
@Nullable
18+
private final Long cache;
19+
@Nullable
20+
private final Long increment;
21+
@Nullable
22+
private final Boolean cycle;
23+
24+
private SequenceDescription(Builder builder) {
25+
this.name = builder.name;
26+
this.minValue = builder.minValue;
27+
this.maxValue = builder.maxValue;
28+
this.startValue = builder.startValue;
29+
this.cache = builder.cache;
30+
this.increment = builder.increment;
31+
this.cycle = builder.cycle;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
@Nullable
39+
public Long getMinValue() {
40+
return minValue;
41+
}
42+
43+
@Nullable
44+
public Long getMaxValue() {
45+
return maxValue;
46+
}
47+
48+
@Nullable
49+
public Long getStartValue() {
50+
return startValue;
51+
}
52+
53+
@Nullable
54+
public Long getCache() {
55+
return cache;
56+
}
57+
58+
@Nullable
59+
public Long getIncrement() {
60+
return increment;
61+
}
62+
63+
@Nullable
64+
public Boolean getCycle() {
65+
return cycle;
66+
}
67+
68+
public static Builder newBuilder() {
69+
return new Builder();
70+
}
71+
72+
public static class Builder {
73+
private String name = "sequence_default";
74+
private Long minValue;
75+
private Long maxValue;
76+
private Long startValue;
77+
private Long cache;
78+
private Long increment;
79+
private Boolean cycle;
80+
81+
public Builder setName(String name) {
82+
this.name = name;
83+
return this;
84+
}
85+
86+
public Builder setMinValue(@Nullable Long minValue) {
87+
this.minValue = minValue;
88+
return this;
89+
}
90+
91+
public Builder setMaxValue(@Nullable Long maxValue) {
92+
this.maxValue = maxValue;
93+
return this;
94+
}
95+
96+
public Builder setStartValue(@Nullable Long startValue) {
97+
this.startValue = startValue;
98+
return this;
99+
}
100+
101+
public Builder setCache(@Nullable Long cache) {
102+
this.cache = cache;
103+
return this;
104+
}
105+
106+
public Builder setIncrement(@Nullable Long increment) {
107+
this.increment = increment;
108+
return this;
109+
}
110+
111+
public Builder setCycle(@Nullable Boolean cycle) {
112+
this.cycle = cycle;
113+
return this;
114+
}
115+
116+
public SequenceDescription build() {
117+
if (name == null) {
118+
throw new IllegalStateException("name is required");
119+
}
120+
121+
return new SequenceDescription(this);
122+
}
123+
}
124+
}

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import javax.annotation.Nullable;
44

5+
import tech.ydb.table.values.PrimitiveValue;
56
import tech.ydb.table.values.Type;
67

78

@@ -14,14 +15,37 @@ public class TableColumn {
1415
private final Type type;
1516
@Nullable
1617
private final String family;
17-
1818
private final boolean hasDefaultValue;
19+
@Nullable
20+
private final PrimitiveValue literalDefaultValue;
21+
@Nullable
22+
private final SequenceDescription sequenceDescription;
1923

2024
public TableColumn(String name, Type type, String family, boolean hasDefaultValue) {
2125
this.name = name;
2226
this.type = type;
2327
this.family = family;
2428
this.hasDefaultValue = hasDefaultValue;
29+
this.literalDefaultValue = null;
30+
this.sequenceDescription = null;
31+
}
32+
33+
public TableColumn(String name, Type type, @Nullable String family, PrimitiveValue literalDefaultValue) {
34+
this.name = name;
35+
this.type = type;
36+
this.family = family;
37+
this.hasDefaultValue = true;
38+
this.literalDefaultValue = literalDefaultValue;
39+
this.sequenceDescription = null;
40+
}
41+
42+
public TableColumn(String name, Type type, @Nullable String family, SequenceDescription sequenceDescription) {
43+
this.name = name;
44+
this.type = type;
45+
this.family = family;
46+
this.hasDefaultValue = true;
47+
this.literalDefaultValue = null;
48+
this.sequenceDescription = sequenceDescription;
2549
}
2650

2751
public TableColumn(String name, Type type, String family) {
@@ -53,4 +77,14 @@ public String getFamily() {
5377
public String toString() {
5478
return name + ' ' + type;
5579
}
80+
81+
@Nullable
82+
public PrimitiveValue getLiteralDefaultValue() {
83+
return literalDefaultValue;
84+
}
85+
86+
@Nullable
87+
public SequenceDescription getSequenceDescription() {
88+
return sequenceDescription;
89+
}
5690
}

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

Lines changed: 77 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,88 @@ 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 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+
190+
public Builder addSmallSerialColumn(String name) {
191+
return addSmallSerialColumn(name, null, SequenceDescription.newBuilder().build());
192+
}
193+
194+
public Builder addSerialColumn(String name) {
195+
return addSerialColumn(name, null, SequenceDescription.newBuilder().build());
196+
}
197+
198+
public Builder addBigSerialColumn(String name) {
199+
return addBigSerialColumn(name, null, SequenceDescription.newBuilder().build());
200+
}
201+
202+
public Builder addSmallSerialColumn(String name, SequenceDescription sequenceDescription) {
203+
return addSmallSerialColumn(name, null, sequenceDescription);
204+
}
205+
206+
public Builder addSerialColumn(String name, SequenceDescription sequenceDescription) {
207+
return addSerialColumn(name, null, sequenceDescription);
208+
}
209+
210+
public Builder addBigSerialColumn(String name, SequenceDescription sequenceDescription) {
211+
return addBigSerialColumn(name, null, sequenceDescription);
212+
}
213+
214+
public Builder addSmallSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
215+
return addSequenceColumn(name, PrimitiveType.Int16, family, sequenceDescription);
216+
}
217+
218+
public Builder addSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
219+
return addSequenceColumn(name, PrimitiveType.Int32, family, sequenceDescription);
220+
}
221+
222+
public Builder addBigSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
223+
return addSequenceColumn(name, PrimitiveType.Int64, family, sequenceDescription);
224+
}
225+
150226
public Builder addKeyRange(KeyRange value) {
151227
keyRanges.add(value);
152228
return this;

0 commit comments

Comments
 (0)