[Enhancement] provide alter table xxx set auto_increment (backport #62767) (#62876)

Signed-off-by: srlch <linzichao@starrocks.com>
Co-authored-by: Evgeniy Shishkin <eshishki@gmail.com>
This commit is contained in:
srlch 2025-09-11 13:54:57 +08:00 committed by GitHub
parent 5969a5e8e3
commit 7bfb6c6076
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 237 additions and 24 deletions

View File

@ -66,6 +66,7 @@ import com.starrocks.sql.ast.AddPartitionClause;
import com.starrocks.sql.ast.AddRollupClause;
import com.starrocks.sql.ast.AlterClause;
import com.starrocks.sql.ast.AlterMaterializedViewStmt;
import com.starrocks.sql.ast.AlterTableAutoIncrementClause;
import com.starrocks.sql.ast.AlterTableCommentClause;
import com.starrocks.sql.ast.AlterTableStmt;
import com.starrocks.sql.ast.AlterViewClause;
@ -917,4 +918,21 @@ public class AlterJobExecutor implements AstVisitor<Void, ConnectContext> {
GlobalStateMgr.getCurrentState().getEditLog().logModifyViewDef(alterViewInfo);
return null;
}
@Override
public Void visitAlterTableAutoIncrementClause(AlterTableAutoIncrementClause clause, ConnectContext context) {
Locker locker = new Locker();
locker.lockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
try {
try {
GlobalStateMgr.getCurrentState().getLocalMetastore()
.alterTableAutoIncrement(db.getFullName(), table.getName(), clause.getAutoIncrementValue());
} catch (DdlException e) {
throw new AlterJobException(e.getMessage());
}
} finally {
locker.unLockTablesWithIntensiveDbLock(db.getId(), Lists.newArrayList(table.getId()), LockType.WRITE);
}
return null;
}
}

View File

@ -70,6 +70,9 @@ public enum AlterOpType {
ALTER_TAG,
ALTER_TABLE_OPERATION,
// ALTER AUTO_INCREMENT counter
ALTER_AUTO_INCREMENT,
// dynamic tablet split
SPLIT_TABLET,
@ -96,5 +99,4 @@ public enum AlterOpType {
public boolean needCheckCapacity() {
return this == ADD_ROLLUP || this == SCHEMA_CHANGE || this == ADD_PARTITION || this == OPTIMIZE;
}
}

View File

@ -115,6 +115,8 @@ import com.starrocks.sql.common.PRangeCell;
import com.starrocks.sql.common.SyncPartitionUtils;
import com.starrocks.sql.optimizer.rule.mv.MVUtils;
import com.starrocks.sql.optimizer.statistics.IDictManager;
import com.starrocks.system.Backend;
import com.starrocks.system.ComputeNode;
import com.starrocks.system.SystemInfoService;
import com.starrocks.task.AgentBatchTask;
import com.starrocks.task.AgentTask;
@ -1211,31 +1213,27 @@ public class OlapTable extends Table {
return partitionInfo;
}
public void sendDropAutoIncrementMapTask() {
Set<Long> fullBackendId = Sets.newHashSet();
for (Partition partition : this.getAllPartitions()) {
for (PhysicalPartition physicalPartition : partition.getSubPartitions()) {
List<MaterializedIndex> allIndices = physicalPartition
.getMaterializedIndices(MaterializedIndex.IndexExtState.ALL);
for (MaterializedIndex materializedIndex : allIndices) {
for (Tablet tablet : materializedIndex.getTablets()) {
Set<Long> backendIds = tablet.getBackendIds();
for (long backendId : backendIds) {
fullBackendId.add(backendId);
}
}
}
}
public boolean sendDropAutoIncrementMapTask() {
Set<Long> nodeIds = Sets.newHashSet();
List<Backend> backends = GlobalStateMgr.getCurrentState().getNodeMgr().getClusterInfo().getBackends();
for (Backend backend : backends) {
nodeIds.add(backend.getId());
}
List<ComputeNode> computeNodes = GlobalStateMgr.getCurrentState().getNodeMgr().getClusterInfo().getComputeNodes();
for (ComputeNode cn : computeNodes) {
nodeIds.add(cn.getId());
}
AgentBatchTask batchTask = new AgentBatchTask();
for (long backendId : fullBackendId) {
DropAutoIncrementMapTask dropAutoIncrementMapTask = new DropAutoIncrementMapTask(backendId, this.id,
for (long nodeId : nodeIds) {
DropAutoIncrementMapTask dropAutoIncrementMapTask = new DropAutoIncrementMapTask(nodeId, this.id,
GlobalStateMgr.getCurrentState().getNextId());
batchTask.addTask(dropAutoIncrementMapTask);
}
boolean ok = true;
if (batchTask.getTaskNum() > 0) {
MarkedCountDownLatch<Long, Long> latch = new MarkedCountDownLatch<>(batchTask.getTaskNum());
for (AgentTask task : batchTask.getAllTasks()) {
@ -1247,7 +1245,6 @@ public class OlapTable extends Table {
// estimate timeout, at most 10 min
long timeout = 60L * 1000L;
boolean ok = false;
try {
LOG.info("begin to send drop auto increment map tasks to BE, total {} tasks. timeout: {}",
batchTask.getTaskNum(), timeout);
@ -1259,8 +1256,8 @@ public class OlapTable extends Table {
if (!ok) {
LOG.warn("drop auto increment map tasks failed");
}
}
return ok;
}
/**

View File

@ -5307,4 +5307,27 @@ public class LocalMetastore implements ConnectorMetadata, MVRepairHandler, Memor
}).sum();
return ImmutableMap.of("Partition", totalCount);
}
public void alterTableAutoIncrement(String dbName, String tableName, long newAutoIncrementValue) throws DdlException {
Table table = getTable(dbName, tableName);
Long tableId = table.getId();
Long currentValue = getCurrentAutoIncrementIdByTableId(tableId);
if (currentValue != null && newAutoIncrementValue <= currentValue) {
throw new DdlException("New auto_increment value must be greater than current value: " + currentValue);
}
if (!((OlapTable) table).sendDropAutoIncrementMapTask()) {
throw new DdlException("Failed to drop auto increment cache in CN/BE");
}
addOrReplaceAutoIncrementIdByTableId(tableId, newAutoIncrementValue);
ConcurrentHashMap<Long, Long> idMap = new ConcurrentHashMap<>();
idMap.put(tableId, newAutoIncrementValue);
AutoIncrementInfo info = new AutoIncrementInfo(idMap);
stateMgr.getEditLog().logSaveAutoIncrementId(info);
LOG.info("Set auto_increment value for table {}.{} to {}", dbName, tableName, newAutoIncrementValue);
}
}

View File

@ -74,6 +74,7 @@ import com.starrocks.sql.ast.AddPartitionClause;
import com.starrocks.sql.ast.AddRollupClause;
import com.starrocks.sql.ast.AlterClause;
import com.starrocks.sql.ast.AlterMaterializedViewStatusClause;
import com.starrocks.sql.ast.AlterTableAutoIncrementClause;
import com.starrocks.sql.ast.AlterTableOperationClause;
import com.starrocks.sql.ast.AstVisitor;
import com.starrocks.sql.ast.AsyncRefreshSchemeDesc;
@ -1652,4 +1653,18 @@ public class AlterTableClauseAnalyzer implements AstVisitor<Void, ConnectContext
clause.setArgs(args);
return null;
}
@Override
public Void visitAlterTableAutoIncrementClause(AlterTableAutoIncrementClause clause, ConnectContext context) {
if (!table.isNativeTable()) {
throw new SemanticException("Only native table supports AUTO_INCREMENT clause");
}
long newValue = clause.getAutoIncrementValue();
if (newValue <= 0) {
throw new SemanticException("AUTO_INCREMENT value must be positive");
}
return null;
}
}

View File

@ -0,0 +1,43 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.starrocks.sql.ast;
import com.starrocks.alter.AlterOpType;
import com.starrocks.sql.parser.NodePosition;
/**
* Clause for ALTER TABLE tbl AUTO_INCREMENT = value
*/
public class AlterTableAutoIncrementClause extends AlterTableClause {
private final long autoIncrementValue;
public AlterTableAutoIncrementClause(long autoIncrementValue) {
this(autoIncrementValue, NodePosition.ZERO);
}
public AlterTableAutoIncrementClause(long autoIncrementValue, NodePosition pos) {
super(AlterOpType.ALTER_AUTO_INCREMENT, pos);
this.autoIncrementValue = autoIncrementValue;
}
public long getAutoIncrementValue() {
return autoIncrementValue;
}
@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitAlterTableAutoIncrementClause(this, context);
}
}

View File

@ -1276,6 +1276,10 @@ public interface AstVisitor<R, C> {
return visitNode(clause, context);
}
default R visitAlterTableAutoIncrementClause(AlterTableAutoIncrementClause clause, C context) {
return visitNode(clause, context);
}
default R visitAddRollupClause(AddRollupClause clause, C context) {
return visitNode(clause, context);
}

View File

@ -164,6 +164,7 @@ import com.starrocks.sql.ast.AlterStorageVolumeClause;
import com.starrocks.sql.ast.AlterStorageVolumeCommentClause;
import com.starrocks.sql.ast.AlterStorageVolumeStmt;
import com.starrocks.sql.ast.AlterSystemStmt;
import com.starrocks.sql.ast.AlterTableAutoIncrementClause;
import com.starrocks.sql.ast.AlterTableClause;
import com.starrocks.sql.ast.AlterTableCommentClause;
import com.starrocks.sql.ast.AlterTableOperationClause;
@ -4765,6 +4766,12 @@ public class AstBuilder extends StarRocksBaseVisitor<ParseNode> {
return new SwapTableClause(identifier.getValue(), createPos(context));
}
@Override
public ParseNode visitAlterTableAutoIncrementClause(StarRocksParser.AlterTableAutoIncrementClauseContext context) {
long autoIncrementValue = Long.parseLong(context.INTEGER_VALUE().getText());
return new AlterTableAutoIncrementClause(autoIncrementValue, createPos(context));
}
@Override
public ParseNode visitModifyPropertiesClause(StarRocksParser.ModifyPropertiesClauseContext context) {
Map<String, String> properties = new HashMap<>();
@ -4791,17 +4798,17 @@ public class AstBuilder extends StarRocksBaseVisitor<ParseNode> {
public ParseNode visitOptimizeRange(StarRocksParser.OptimizeRangeContext context) {
StringLiteral start = null;
StringLiteral end = null;
// Extract start value if present
if (context.start != null) {
start = (StringLiteral) visit(context.start);
}
// Extract end value if present
if (context.end != null) {
end = (StringLiteral) visit(context.end);
}
// Create and return OptimizeRange object with position information
return new OptimizeRange(start, end, createPos(context));
}

View File

@ -962,6 +962,7 @@ alterClause
| tableOperationClause
| dropPersistentIndexClause
| splitTabletClause
| alterTableAutoIncrementClause
//Alter partition clause
| addPartitionClause
@ -1213,6 +1214,10 @@ splitTabletClause
properties?
;
alterTableAutoIncrementClause
: AUTO_INCREMENT '=' INTEGER_VALUE
;
// ---------Alter partition clause---------
addPartitionClause

View File

@ -0,0 +1,39 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.starrocks.alter;
import com.starrocks.common.FeConstants;
import com.starrocks.sql.analyzer.AnalyzeTestUtil;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static com.starrocks.sql.analyzer.AnalyzeTestUtil.analyzeFail;
import static com.starrocks.sql.analyzer.AnalyzeTestUtil.analyzeSuccess;
public class AlterTableAutoIncrementTest {
@BeforeAll
public static void beforeClass() throws Exception {
FeConstants.runningUnitTest = true;
AnalyzeTestUtil.init();
}
@Test
public void testAlterTableAutoIncrement() throws Exception {
analyzeFail("ALTER TABLE test_auto_increment AUTO_INCREMENT = 0");
analyzeFail("ALTER TABLE test_auto_increment AUTO_INCREMENT = -1");
analyzeFail("ALTER TABLE xxxxxx AUTO_INCREMENT = 1");
analyzeSuccess("ALTER TABLE test_auto_increment AUTO_INCREMENT = 1");
}
}

View File

@ -348,6 +348,14 @@ public class AnalyzeTestUtil {
" DUPLICATE KEY(id) PROPERTIES ( \n" +
"\"replication_num\" = \"1\"\n" +
");");
starRocksAssert.withTable("CREATE TABLE test_auto_increment (" +
"id BIGINT NOT NULL AUTO_INCREMENT," +
"name VARCHAR(100)" +
") " +
"PRIMARY KEY (`id`) " +
"DISTRIBUTED BY HASH(`id`) BUCKETS 1 " +
"PROPERTIES(\"replication_num\" = \"1\");");
}
public static String getDbName() {

View File

@ -799,4 +799,38 @@ DROP TABLE t_auto_increment_partial_update_column_upsert;
-- !result
DROP DATABASE test_auto_increment_partial_update_column_upsert;
-- result:
-- !result
-- name: test_alter_auto_increment_counter @sequential
CREATE TABLE `t_alter_auto_increment_counter` (
`k` BIGINT NOT NULL COMMENT "",
`v1` BIGINT AUTO_INCREMENT
) ENGINE=OLAP
PRIMARY KEY(`k`)
DISTRIBUTED BY HASH(`k`) BUCKETS 1
PROPERTIES (
"replicated_storage" = "true",
"replication_num" = "1"
);
-- result:
-- !result
INSERT INTO t_alter_auto_increment_counter VALUES (1, 1);
-- result:
-- !result
INSERT INTO t_alter_auto_increment_counter VALUES (2, 2);
-- result:
-- !result
ALTER TABLE t_alter_auto_increment_counter AUTO_INCREMENT = 300;
-- result:
-- !result
INSERT INTO t_alter_auto_increment_counter VALUES (3, DEFAULT);
-- result:
-- !result
SELECT * FROM t_alter_auto_increment_counter ORDER BY k;
-- result:
1 1
2 2
3 300
-- !result
DROP TABLE t_alter_auto_increment_counter;
-- result:
-- !result

View File

@ -354,4 +354,22 @@ INSERT INTO t_auto_increment_partial_update_column_upsert VALUES (1, 300, 20, 30
SELECT * FROM t_auto_increment_partial_update_column_upsert;
DROP TABLE t_auto_increment_partial_update_column_upsert;
DROP DATABASE test_auto_increment_partial_update_column_upsert;
DROP DATABASE test_auto_increment_partial_update_column_upsert;
-- name: test_alter_auto_increment_counter @sequential
CREATE TABLE `t_alter_auto_increment_counter` (
`k` BIGINT NOT NULL COMMENT "",
`v1` BIGINT AUTO_INCREMENT
) ENGINE=OLAP
PRIMARY KEY(`k`)
DISTRIBUTED BY HASH(`k`) BUCKETS 1
PROPERTIES (
"replicated_storage" = "true",
"replication_num" = "1"
);
INSERT INTO t_alter_auto_increment_counter VALUES (1, 1);
INSERT INTO t_alter_auto_increment_counter VALUES (2, 2);
ALTER TABLE t_alter_auto_increment_counter AUTO_INCREMENT = 300;
INSERT INTO t_alter_auto_increment_counter VALUES (3, DEFAULT);
SELECT * FROM t_alter_auto_increment_counter ORDER BY k;
DROP TABLE t_alter_auto_increment_counter;