[Feature](auto-inc) Add start value for auto increment column (#30512)

This commit is contained in:
abmdocrt
2024-02-06 11:59:37 +08:00
committed by yiguolei
parent 5ca10d95b7
commit 2cb46eed94
27 changed files with 1156 additions and 235 deletions

View File

@ -178,18 +178,19 @@ public class ColumnDef {
private boolean isKey;
private boolean isAllowNull;
private boolean isAutoInc;
private long autoIncInitValue;
private DefaultValue defaultValue;
private String comment;
private boolean visible;
private int clusterKeyId = -1;
public ColumnDef(String name, TypeDef typeDef) {
this(name, typeDef, false, null, false, false, DefaultValue.NOT_SET, "");
this(name, typeDef, false, null, false, -1, DefaultValue.NOT_SET, "");
}
public ColumnDef(String name, TypeDef typeDef, boolean isKey, AggregateType aggregateType,
boolean isAllowNull, boolean isAutoInc, DefaultValue defaultValue, String comment) {
this(name, typeDef, isKey, aggregateType, isAllowNull, isAutoInc, defaultValue, comment, true);
boolean isAllowNull, long autoIncInitValue, DefaultValue defaultValue, String comment) {
this(name, typeDef, isKey, aggregateType, isAllowNull, autoIncInitValue, defaultValue, comment, true);
}
public ColumnDef(String name, TypeDef typeDef, boolean isAllowNull) {
@ -197,18 +198,19 @@ public class ColumnDef {
}
public ColumnDef(String name, TypeDef typeDef, boolean isKey, AggregateType aggregateType,
boolean isAllowNull, DefaultValue defaultValue, String comment) {
this(name, typeDef, isKey, aggregateType, isAllowNull, false, defaultValue, comment, true);
boolean isAllowNull, DefaultValue defaultValue, String comment) {
this(name, typeDef, isKey, aggregateType, isAllowNull, -1, defaultValue, comment, true);
}
public ColumnDef(String name, TypeDef typeDef, boolean isKey, AggregateType aggregateType,
boolean isAllowNull, boolean isAutoInc, DefaultValue defaultValue, String comment, boolean visible) {
boolean isAllowNull, long autoIncInitValue, DefaultValue defaultValue, String comment, boolean visible) {
this.name = name;
this.typeDef = typeDef;
this.isKey = isKey;
this.aggregateType = aggregateType;
this.isAllowNull = isAllowNull;
this.isAutoInc = isAutoInc;
this.isAutoInc = autoIncInitValue != -1;
this.autoIncInitValue = autoIncInitValue;
this.defaultValue = defaultValue;
this.comment = comment;
this.visible = visible;
@ -216,39 +218,39 @@ public class ColumnDef {
public static ColumnDef newDeleteSignColumnDef() {
return new ColumnDef(Column.DELETE_SIGN, TypeDef.create(PrimitiveType.TINYINT), false, null, false,
false, new ColumnDef.DefaultValue(true, "0"), "doris delete flag hidden column", false);
-1, new ColumnDef.DefaultValue(true, "0"), "doris delete flag hidden column", false);
}
public static ColumnDef newDeleteSignColumnDef(AggregateType aggregateType) {
return new ColumnDef(Column.DELETE_SIGN, TypeDef.create(PrimitiveType.TINYINT), false, aggregateType, false,
false, new ColumnDef.DefaultValue(true, "0"), "doris delete flag hidden column", false);
-1, new ColumnDef.DefaultValue(true, "0"), "doris delete flag hidden column", false);
}
public static ColumnDef newSequenceColumnDef(Type type) {
return new ColumnDef(Column.SEQUENCE_COL, new TypeDef(type), false, null, true,
false, DefaultValue.NULL_DEFAULT_VALUE, "sequence column hidden column", false);
-1, DefaultValue.NULL_DEFAULT_VALUE, "sequence column hidden column", false);
}
public static ColumnDef newSequenceColumnDef(Type type, AggregateType aggregateType) {
return new ColumnDef(Column.SEQUENCE_COL, new TypeDef(type), false,
aggregateType, true, false, DefaultValue.NULL_DEFAULT_VALUE,
aggregateType, true, -1, DefaultValue.NULL_DEFAULT_VALUE,
"sequence column hidden column", false);
}
public static ColumnDef newRowStoreColumnDef(AggregateType aggregateType) {
return new ColumnDef(Column.ROW_STORE_COL, TypeDef.create(PrimitiveType.STRING), false,
aggregateType, false, false,
aggregateType, false, -1,
new ColumnDef.DefaultValue(true, ""), "doris row store hidden column", false);
}
public static ColumnDef newVersionColumnDef() {
return new ColumnDef(Column.VERSION_COL, TypeDef.create(PrimitiveType.BIGINT), false, null, false, false,
return new ColumnDef(Column.VERSION_COL, TypeDef.create(PrimitiveType.BIGINT), false, null, false, -1,
new ColumnDef.DefaultValue(true, "0"), "doris version hidden column", false);
}
public static ColumnDef newVersionColumnDef(AggregateType aggregateType) {
return new ColumnDef(Column.VERSION_COL, TypeDef.create(PrimitiveType.BIGINT), false, aggregateType, false,
false, new ColumnDef.DefaultValue(true, "0"), "doris version hidden column", false);
-1, new ColumnDef.DefaultValue(true, "0"), "doris version hidden column", false);
}
public boolean isAllowNull() {
@ -560,6 +562,9 @@ public class ColumnDef {
if (isAutoInc) {
sb.append("AUTO_INCREMENT ");
sb.append("(");
sb.append(autoIncInitValue);
sb.append(")");
}
if (defaultValue.isSet) {
@ -582,7 +587,7 @@ public class ColumnDef {
type = Expr.createAggStateType(genericAggregationName, typeList, nullableList);
}
return new Column(name, type, isKey, aggregateType, isAllowNull, isAutoInc, defaultValue.value, comment,
return new Column(name, type, isKey, aggregateType, isAllowNull, autoIncInitValue, defaultValue.value, comment,
visible, defaultValue.defaultValueExprDef, Column.COLUMN_UNIQUE_ID_INIT_VALUE, defaultValue.getValue(),
clusterKeyId);
}

View File

@ -57,10 +57,11 @@ public class AutoIncrementGenerator implements Writable {
public AutoIncrementGenerator() {
}
public AutoIncrementGenerator(long dbId, long tableId, long columnId) {
public AutoIncrementGenerator(long dbId, long tableId, long columnId, long nextId) {
this.dbId = dbId;
this.tableId = tableId;
this.columnId = columnId;
this.nextId = nextId;
}
public void setEditLog(EditLog editLog) {

View File

@ -69,7 +69,7 @@ public class Column implements Writable, GsonPostProcessable {
private static final String COLUMN_MAP_KEY = "key";
private static final String COLUMN_MAP_VALUE = "value";
public static final Column UNSUPPORTED_COLUMN = new Column("unknown", Type.UNSUPPORTED, true, null, true, false,
public static final Column UNSUPPORTED_COLUMN = new Column("unknown", Type.UNSUPPORTED, true, null, true, -1,
null, "invalid", true, null, -1, null);
@SerializedName(value = "name")
@ -93,6 +93,9 @@ public class Column implements Writable, GsonPostProcessable {
private boolean isAllowNull;
@SerializedName(value = "isAutoInc")
private boolean isAutoInc;
@SerializedName(value = "autoIncInitValue")
private long autoIncInitValue;
@SerializedName(value = "defaultValue")
private String defaultValue;
@SerializedName(value = "comment")
@ -178,32 +181,32 @@ public class Column implements Writable, GsonPostProcessable {
public Column(String name, Type type, boolean isKey, AggregateType aggregateType, boolean isAllowNull,
String defaultValue, String comment) {
this(name, type, isKey, aggregateType, isAllowNull, false, defaultValue, comment, true, null,
this(name, type, isKey, aggregateType, isAllowNull, -1, defaultValue, comment, true, null,
COLUMN_UNIQUE_ID_INIT_VALUE, defaultValue, false, null);
}
public Column(String name, Type type, boolean isKey, AggregateType aggregateType, boolean isAllowNull,
String comment, boolean visible, int colUniqueId) {
this(name, type, isKey, aggregateType, isAllowNull, false, null, comment, visible, null, colUniqueId, null,
this(name, type, isKey, aggregateType, isAllowNull, -1, null, comment, visible, null, colUniqueId, null,
false, null);
}
public Column(String name, Type type, boolean isKey, AggregateType aggregateType, boolean isAllowNull,
String defaultValue, String comment, boolean visible, DefaultValueExprDef defaultValueExprDef,
int colUniqueId, String realDefaultValue) {
this(name, type, isKey, aggregateType, isAllowNull, false, defaultValue, comment, visible, defaultValueExprDef,
this(name, type, isKey, aggregateType, isAllowNull, -1, defaultValue, comment, visible, defaultValueExprDef,
colUniqueId, realDefaultValue, false, null);
}
public Column(String name, Type type, boolean isKey, AggregateType aggregateType, boolean isAllowNull,
boolean isAutoInc, String defaultValue, String comment, boolean visible,
long autoIncInitValue, String defaultValue, String comment, boolean visible,
DefaultValueExprDef defaultValueExprDef, int colUniqueId, String realDefaultValue) {
this(name, type, isKey, aggregateType, isAllowNull, isAutoInc, defaultValue, comment, visible,
this(name, type, isKey, aggregateType, isAllowNull, autoIncInitValue, defaultValue, comment, visible,
defaultValueExprDef, colUniqueId, realDefaultValue, false, null);
}
public Column(String name, Type type, boolean isKey, AggregateType aggregateType, boolean isAllowNull,
boolean isAutoInc, String defaultValue, String comment, boolean visible,
long autoIncInitValue, String defaultValue, String comment, boolean visible,
DefaultValueExprDef defaultValueExprDef, int colUniqueId, String realDefaultValue,
boolean hasOnUpdateDefaultValue, DefaultValueExprDef onUpdateDefaultValueExprDef) {
this.name = name;
@ -220,7 +223,8 @@ public class Column implements Writable, GsonPostProcessable {
this.isAggregationTypeImplicit = false;
this.isKey = isKey;
this.isAllowNull = isAllowNull;
this.isAutoInc = isAutoInc;
this.isAutoInc = autoIncInitValue != -1;
this.autoIncInitValue = autoIncInitValue;
this.defaultValue = defaultValue;
this.realDefaultValue = realDefaultValue;
this.defaultValueExprDef = defaultValueExprDef;
@ -246,20 +250,20 @@ public class Column implements Writable, GsonPostProcessable {
}
public Column(String name, Type type, boolean isKey, AggregateType aggregateType,
boolean isAllowNull, boolean isAutoInc, String defaultValue, String comment,
boolean isAllowNull, long autoIncInitValue, String defaultValue, String comment,
boolean visible, DefaultValueExprDef defaultValueExprDef, int colUniqueId,
String realDefaultValue, boolean hasOnUpdateDefaultValue,
DefaultValueExprDef onUpdateDefaultValueExprDef, int clusterKeyId) {
this(name, type, isKey, aggregateType, isAllowNull, isAutoInc, defaultValue, comment,
this(name, type, isKey, aggregateType, isAllowNull, autoIncInitValue, defaultValue, comment,
visible, defaultValueExprDef, colUniqueId, realDefaultValue,
hasOnUpdateDefaultValue, onUpdateDefaultValueExprDef);
this.clusterKeyId = clusterKeyId;
}
public Column(String name, Type type, boolean isKey, AggregateType aggregateType, boolean isAllowNull,
boolean isAutoInc, String defaultValue, String comment, boolean visible,
long autoIncInitValue, String defaultValue, String comment, boolean visible,
DefaultValueExprDef defaultValueExprDef, int colUniqueId, String realDefaultValue, int clusterKeyId) {
this(name, type, isKey, aggregateType, isAllowNull, isAutoInc, defaultValue, comment, visible,
this(name, type, isKey, aggregateType, isAllowNull, autoIncInitValue, defaultValue, comment, visible,
defaultValueExprDef, colUniqueId, realDefaultValue);
this.clusterKeyId = clusterKeyId;
}
@ -974,6 +978,10 @@ public class Column implements Writable, GsonPostProcessable {
return this.uniqueId;
}
public long getAutoIncInitValue() {
return this.autoIncInitValue;
}
public void setIndexFlag(TColumn tColumn, OlapTable olapTable) {
List<Index> indexes = olapTable.getIndexes();
for (Index index : indexes) {

View File

@ -2434,7 +2434,8 @@ public class OlapTable extends Table implements MTMVRelatedTableIf {
public void initAutoIncrementGenerator(long dbId) {
for (Column column : fullSchema) {
if (column.isAutoInc()) {
autoIncrementGenerator = new AutoIncrementGenerator(dbId, id, column.getUniqueId());
autoIncrementGenerator = new AutoIncrementGenerator(dbId, id, column.getUniqueId(),
column.getAutoIncInitValue());
autoIncrementGenerator.setEditLog(Env.getCurrentEnv().getEditLog());
break;
}

View File

@ -1260,7 +1260,7 @@ public class InternalCatalog implements CatalogIf<Database> {
ColumnDef columnDef;
if (resultExpr.getSrcSlotRef() == null) {
columnDef = new ColumnDef(name, typeDef, false, null,
true, false, new DefaultValue(false, null), "");
true, -1, new DefaultValue(false, null), "");
} else {
Column column = resultExpr.getSrcSlotRef().getDesc().getColumn();
boolean setDefault = StringUtils.isNotBlank(column.getDefaultValue());
@ -1278,7 +1278,7 @@ public class InternalCatalog implements CatalogIf<Database> {
defaultValue = new DefaultValue(setDefault, column.getDefaultValue());
}
columnDef = new ColumnDef(name, typeDef, false, null,
column.isAllowNull(), false, defaultValue, column.getComment());
column.isAllowNull(), -1, defaultValue, column.getComment());
}
createTableStmt.addColumnDef(columnDef);
// set first column as default distribution

View File

@ -517,7 +517,7 @@ public abstract class JdbcClient {
// because for utf8 encoding, a Chinese character takes up 3 bytes
protected int charOctetLength;
protected boolean isAllowNull;
protected boolean isAutoincrement;
protected long autoIncInitValue;
protected String defaultValue;
}

View File

@ -164,7 +164,7 @@ public class JdbcMySQLClient extends JdbcClient {
field.setRemarks(rs.getString("REMARKS"));
field.setCharOctetLength(rs.getInt("CHAR_OCTET_LENGTH"));
String isAutoincrement = rs.getString("IS_AUTOINCREMENT");
field.setAutoincrement("YES".equalsIgnoreCase(isAutoincrement));
field.setAutoIncInitValue("YES".equalsIgnoreCase(isAutoincrement) ? 1 : -1);
field.setDefaultValue(rs.getString("COLUMN_DEF"));
tableSchema.add(field);
}
@ -197,7 +197,7 @@ public class JdbcMySQLClient extends JdbcClient {
}
dorisTableSchema.add(new Column(field.getColumnName(),
jdbcTypeToDoris(field), field.isKey(), null,
field.isAllowNull(), field.isAutoincrement(), field.getDefaultValue(), field.getRemarks(),
field.isAllowNull(), field.getAutoIncInitValue(), field.getDefaultValue(), field.getRemarks(),
true, defaultValueExprDef, -1, null));
}
return dorisTableSchema;

View File

@ -2495,8 +2495,20 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
//comment should remove '\' and '(") at the beginning and end
String comment = ctx.comment != null ? ctx.comment.getText().substring(1, ctx.comment.getText().length() - 1)
.replace("\\", "") : "";
boolean isAutoInc = ctx.AUTO_INCREMENT() != null;
return new ColumnDefinition(colName, colType, isKey, aggType, !isNotNull, isAutoInc, defaultValue,
long autoIncInitValue = -1;
if (ctx.AUTO_INCREMENT() != null) {
if (ctx.autoIncInitValue != null) {
// AUTO_INCREMENT(Value) Value >= 0.
autoIncInitValue = Long.valueOf(ctx.autoIncInitValue.getText());
if (autoIncInitValue < 0) {
throw new AnalysisException("AUTO_INCREMENT start value can not be negative.");
}
} else {
// AUTO_INCREMENT default 1.
autoIncInitValue = Long.valueOf(1);
}
}
return new ColumnDefinition(colName, colType, isKey, aggType, !isNotNull, autoIncInitValue, defaultValue,
onUpdateDefaultValue, comment);
}

View File

@ -62,7 +62,7 @@ public class ColumnDefinition {
private final String comment;
private final boolean isVisible;
private boolean aggTypeImplicit = false;
private boolean isAutoInc = false;
private long autoIncInitValue = -1;
private int clusterKeyId = -1;
public ColumnDefinition(String name, DataType type, boolean isKey, AggregateType aggType, boolean isNullable,
@ -71,9 +71,9 @@ public class ColumnDefinition {
}
public ColumnDefinition(String name, DataType type, boolean isKey, AggregateType aggType,
boolean isNullable, boolean isAutoInc, Optional<DefaultValue> defaultValue,
boolean isNullable, long autoIncInitValue, Optional<DefaultValue> defaultValue,
Optional<DefaultValue> onUpdateDefaultValue, String comment) {
this(name, type, isKey, aggType, isNullable, isAutoInc, defaultValue, onUpdateDefaultValue,
this(name, type, isKey, aggType, isNullable, autoIncInitValue, defaultValue, onUpdateDefaultValue,
comment, true);
}
@ -96,14 +96,14 @@ public class ColumnDefinition {
* constructor
*/
private ColumnDefinition(String name, DataType type, boolean isKey, AggregateType aggType,
boolean isNullable, boolean isAutoInc, Optional<DefaultValue> defaultValue,
boolean isNullable, long autoIncInitValue, Optional<DefaultValue> defaultValue,
Optional<DefaultValue> onUpdateDefaultValue, String comment, boolean isVisible) {
this.name = name;
this.type = type;
this.isKey = isKey;
this.aggType = aggType;
this.isNullable = isNullable;
this.isAutoInc = isAutoInc;
this.autoIncInitValue = autoIncInitValue;
this.defaultValue = defaultValue;
this.onUpdateDefaultValue = onUpdateDefaultValue;
this.comment = comment;
@ -150,10 +150,6 @@ public class ColumnDefinition {
this.clusterKeyId = clusterKeyId;
}
public boolean isAutoInc() {
return isAutoInc;
}
private DataType updateCharacterTypeLength(DataType dataType) {
if (dataType instanceof ArrayType) {
return ArrayType.of(updateCharacterTypeLength(((ArrayType) dataType).getItemType()));
@ -619,7 +615,7 @@ public class ColumnDefinition {
*/
public Column translateToCatalogStyle() {
Column column = new Column(name, type.toCatalogDataType(), isKey, aggType, isNullable,
isAutoInc, defaultValue.map(DefaultValue::getRawValue).orElse(null), comment, isVisible,
autoIncInitValue, defaultValue.map(DefaultValue::getRawValue).orElse(null), comment, isVisible,
defaultValue.map(DefaultValue::getDefaultValueExprDef).orElse(null), Column.COLUMN_UNIQUE_ID_INIT_VALUE,
defaultValue.map(DefaultValue::getValue).orElse(null), onUpdateDefaultValue.isPresent(),
onUpdateDefaultValue.map(DefaultValue::getDefaultValueExprDef).orElse(null), clusterKeyId);