Skip to content

Commit e9f5385

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

File tree

7 files changed

+591
-38
lines changed

7 files changed

+591
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Table: CreateTable with default value options
12
* Table: AlterTable supports index renaming
23

34
## 2.3.20 ##

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: 27 additions & 5 deletions
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,18 +15,29 @@ public class TableColumn {
1415
private final Type type;
1516
@Nullable
1617
private final String family;
18+
@Nullable
19+
private final PrimitiveValue literalDefaultValue;
20+
@Nullable
21+
private final SequenceDescription sequenceDescription;
1722

18-
private final boolean hasDefaultValue;
23+
public TableColumn(String name, Type type, @Nullable String family, PrimitiveValue literalDefaultValue) {
24+
this.name = name;
25+
this.type = type;
26+
this.family = family;
27+
this.literalDefaultValue = literalDefaultValue;
28+
this.sequenceDescription = null;
29+
}
1930

20-
public TableColumn(String name, Type type, String family, boolean hasDefaultValue) {
31+
public TableColumn(String name, Type type, @Nullable String family, SequenceDescription sequenceDescription) {
2132
this.name = name;
2233
this.type = type;
2334
this.family = family;
24-
this.hasDefaultValue = hasDefaultValue;
35+
this.literalDefaultValue = null;
36+
this.sequenceDescription = sequenceDescription;
2537
}
2638

2739
public TableColumn(String name, Type type, String family) {
28-
this(name, type, family, false);
40+
this(name, type, family, (PrimitiveValue) null);
2941
}
3042

3143
public TableColumn(String name, Type type) {
@@ -41,7 +53,7 @@ public Type getType() {
4153
}
4254

4355
public boolean hasDefaultValue() {
44-
return hasDefaultValue;
56+
return literalDefaultValue != null || sequenceDescription != null;
4557
}
4658

4759
@Nullable
@@ -53,4 +65,14 @@ public String getFamily() {
5365
public String toString() {
5466
return name + ' ' + type;
5567
}
68+
69+
@Nullable
70+
public PrimitiveValue getLiteralDefaultValue() {
71+
return literalDefaultValue;
72+
}
73+
74+
@Nullable
75+
public SequenceDescription getSequenceDescription() {
76+
return sequenceDescription;
77+
}
5678
}

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

Lines changed: 83 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,94 @@ 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(
171+
String name,
172+
Type type,
173+
String family,
174+
SequenceDescription sequenceDescription
175+
) {
176+
if (type instanceof PrimitiveType) {
177+
PrimitiveType primitiveType = (PrimitiveType) type;
178+
179+
switch (primitiveType) {
180+
case Int16:
181+
columns.put(name, new TableColumn(name, PrimitiveType.Int16, family, sequenceDescription));
182+
return this;
183+
case Int32:
184+
columns.put(name, new TableColumn(name, PrimitiveType.Int32, family, sequenceDescription));
185+
return this;
186+
case Int64:
187+
columns.put(name, new TableColumn(name, PrimitiveType.Int64, family, sequenceDescription));
188+
return this;
189+
default:
190+
}
191+
}
192+
193+
throw new IllegalArgumentException("Type " + type + " cannot be used as a sequence column");
194+
}
195+
196+
public Builder addSmallSerialColumn(String name) {
197+
return addSmallSerialColumn(name, null, SequenceDescription.newBuilder().build());
198+
}
199+
200+
public Builder addSerialColumn(String name) {
201+
return addSerialColumn(name, null, SequenceDescription.newBuilder().build());
202+
}
203+
204+
public Builder addBigSerialColumn(String name) {
205+
return addBigSerialColumn(name, null, SequenceDescription.newBuilder().build());
206+
}
207+
208+
public Builder addSmallSerialColumn(String name, SequenceDescription sequenceDescription) {
209+
return addSmallSerialColumn(name, null, sequenceDescription);
210+
}
211+
212+
public Builder addSerialColumn(String name, SequenceDescription sequenceDescription) {
213+
return addSerialColumn(name, null, sequenceDescription);
214+
}
215+
216+
public Builder addBigSerialColumn(String name, SequenceDescription sequenceDescription) {
217+
return addBigSerialColumn(name, null, sequenceDescription);
218+
}
219+
220+
public Builder addSmallSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
221+
return addSequenceColumn(name, PrimitiveType.Int16, family, sequenceDescription);
222+
}
223+
224+
public Builder addSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
225+
return addSequenceColumn(name, PrimitiveType.Int32, family, sequenceDescription);
226+
}
227+
228+
public Builder addBigSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
229+
return addSequenceColumn(name, PrimitiveType.Int64, family, sequenceDescription);
230+
}
231+
150232
public Builder addKeyRange(KeyRange value) {
151233
keyRanges.add(value);
152234
return this;

0 commit comments

Comments
 (0)