Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Table: CreateTable with default value options
* Table: AlterTable supports index renaming

## 2.3.20 ##
Expand Down
1 change: 0 additions & 1 deletion table/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
<environmentVariables>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
<YDB_DOCKER_IMAGE>ydbplatform/local-ydb:trunk</YDB_DOCKER_IMAGE>
<YDB_FEATURE_FLAGS>enable_vector_index</YDB_FEATURE_FLAGS>
</environmentVariables>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package tech.ydb.table.description;

import javax.annotation.Nullable;

/**
* @author Kirill Kurdyukov
*/
public class SequenceDescription {

private final String name;
@Nullable
private final Long minValue;
@Nullable
private final Long maxValue;
@Nullable
private final Long startValue;
@Nullable
private final Long cache;
@Nullable
private final Long increment;
@Nullable
private final Boolean cycle;

private SequenceDescription(Builder builder) {
this.name = builder.name;
this.minValue = builder.minValue;
this.maxValue = builder.maxValue;
this.startValue = builder.startValue;
this.cache = builder.cache;
this.increment = builder.increment;
this.cycle = builder.cycle;
}

public String getName() {
return name;
}

@Nullable
public Long getMinValue() {
return minValue;
}

@Nullable
public Long getMaxValue() {
return maxValue;
}

@Nullable
public Long getStartValue() {
return startValue;
}

@Nullable
public Long getCache() {
return cache;
}

@Nullable
public Long getIncrement() {
return increment;
}

@Nullable
public Boolean getCycle() {
return cycle;
}

public static Builder newBuilder() {
return new Builder();
}

public static class Builder {
private String name = "sequence_default";
private Long minValue;
private Long maxValue;
private Long startValue;
private Long cache;
private Long increment;
private Boolean cycle;

public Builder setName(String name) {
this.name = name;
return this;
}

public Builder setMinValue(@Nullable Long minValue) {
this.minValue = minValue;
return this;
}

public Builder setMaxValue(@Nullable Long maxValue) {
this.maxValue = maxValue;
return this;
}

public Builder setStartValue(@Nullable Long startValue) {
this.startValue = startValue;
return this;
}

public Builder setCache(@Nullable Long cache) {
this.cache = cache;
return this;
}

public Builder setIncrement(@Nullable Long increment) {
this.increment = increment;
return this;
}

public Builder setCycle(@Nullable Boolean cycle) {
this.cycle = cycle;
return this;
}

public SequenceDescription build() {
if (name == null) {
throw new IllegalStateException("name is required");
}

return new SequenceDescription(this);
}
}
}
32 changes: 27 additions & 5 deletions table/src/main/java/tech/ydb/table/description/TableColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.annotation.Nullable;

import tech.ydb.table.values.PrimitiveValue;
import tech.ydb.table.values.Type;


Expand All @@ -14,18 +15,29 @@ public class TableColumn {
private final Type type;
@Nullable
private final String family;
@Nullable
private final PrimitiveValue literalDefaultValue;
@Nullable
private final SequenceDescription sequenceDescription;

private final boolean hasDefaultValue;
public TableColumn(String name, Type type, @Nullable String family, PrimitiveValue literalDefaultValue) {
this.name = name;
this.type = type;
this.family = family;
this.literalDefaultValue = literalDefaultValue;
this.sequenceDescription = null;
}

public TableColumn(String name, Type type, String family, boolean hasDefaultValue) {
public TableColumn(String name, Type type, @Nullable String family, SequenceDescription sequenceDescription) {
this.name = name;
this.type = type;
this.family = family;
this.hasDefaultValue = hasDefaultValue;
this.literalDefaultValue = null;
this.sequenceDescription = sequenceDescription;
}

public TableColumn(String name, Type type, String family) {
this(name, type, family, false);
this(name, type, family, (PrimitiveValue) null);
}

public TableColumn(String name, Type type) {
Expand All @@ -41,7 +53,7 @@ public Type getType() {
}

public boolean hasDefaultValue() {
return hasDefaultValue;
return literalDefaultValue != null || sequenceDescription != null;
}

@Nullable
Expand All @@ -53,4 +65,14 @@ public String getFamily() {
public String toString() {
return name + ' ' + type;
}

@Nullable
public PrimitiveValue getLiteralDefaultValue() {
return literalDefaultValue;
}

@Nullable
public SequenceDescription getSequenceDescription() {
return sequenceDescription;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import tech.ydb.table.settings.AlterTableSettings;
import tech.ydb.table.settings.PartitioningSettings;
import tech.ydb.table.values.OptionalType;
import tech.ydb.table.values.PrimitiveType;
import tech.ydb.table.values.PrimitiveValue;
import tech.ydb.table.values.Type;

/**
Expand Down Expand Up @@ -139,14 +141,94 @@ public Builder addColumn(TableColumn column) {
}

public Builder addNonnullColumn(String name, Type type) {
return addNonnullColumn(name, type, null);
return addNonnullColumn(name, type, (String) null);
}

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

public Builder addColumn(String name, Type type, PrimitiveValue defaultValue) {
columns.put(name, new TableColumn(name, type, null, defaultValue));
return this;
}

public Builder addColumn(String name, Type type, String family, PrimitiveValue defaultValue) {
columns.put(name, new TableColumn(name, type, family, defaultValue));
return this;
}

public Builder addSequenceColumn(String name, Type type) {
return addSequenceColumn(name, type, null, SequenceDescription.newBuilder().build());
}

public Builder addSequenceColumn(String name, Type type, String family) {
return addSequenceColumn(name, type, family, SequenceDescription.newBuilder().build());
}

public Builder addSequenceColumn(
String name,
Type type,
String family,
SequenceDescription sequenceDescription
) {
if (type instanceof PrimitiveType) {
PrimitiveType primitiveType = (PrimitiveType) type;

switch (primitiveType) {
case Int16:
columns.put(name, new TableColumn(name, PrimitiveType.Int16, family, sequenceDescription));
return this;
case Int32:
columns.put(name, new TableColumn(name, PrimitiveType.Int32, family, sequenceDescription));
return this;
case Int64:
columns.put(name, new TableColumn(name, PrimitiveType.Int64, family, sequenceDescription));
return this;
default:
}
}

throw new IllegalArgumentException("Type " + type + " cannot be used as a sequence column");
}

public Builder addSmallSerialColumn(String name) {
return addSmallSerialColumn(name, null, SequenceDescription.newBuilder().build());
}

public Builder addSerialColumn(String name) {
return addSerialColumn(name, null, SequenceDescription.newBuilder().build());
}

public Builder addBigSerialColumn(String name) {
return addBigSerialColumn(name, null, SequenceDescription.newBuilder().build());
}

public Builder addSmallSerialColumn(String name, SequenceDescription sequenceDescription) {
return addSmallSerialColumn(name, null, sequenceDescription);
}

public Builder addSerialColumn(String name, SequenceDescription sequenceDescription) {
return addSerialColumn(name, null, sequenceDescription);
}

public Builder addBigSerialColumn(String name, SequenceDescription sequenceDescription) {
return addBigSerialColumn(name, null, sequenceDescription);
}

public Builder addSmallSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
return addSequenceColumn(name, PrimitiveType.Int16, family, sequenceDescription);
}

public Builder addSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
return addSequenceColumn(name, PrimitiveType.Int32, family, sequenceDescription);
}

public Builder addBigSerialColumn(String name, String family, SequenceDescription sequenceDescription) {
return addSequenceColumn(name, PrimitiveType.Int64, family, sequenceDescription);
}

public Builder addKeyRange(KeyRange value) {
keyRanges.add(value);
return this;
Expand Down
Loading