From 2f2d0eeac2b109ea8cb5267e726f6caeaf8c67f6 Mon Sep 17 00:00:00 2001 From: bobhan1 Date: Tue, 2 Jul 2024 19:19:23 +0800 Subject: [PATCH] tmp --- .../doris/alter/SchemaChangeHandler.java | 19 ++++ .../test_schema_change_auto_inc.out | 10 +++ .../test_schema_change_auto_inc.groovy | 90 +++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 regression-test/data/schema_change_p0/test_schema_change_auto_inc.out create mode 100644 regression-test/suites/schema_change_p0/test_schema_change_auto_inc.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java b/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java index 9d01d2bc77c6a3..ccb99b7ac35bc6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java +++ b/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java @@ -642,6 +642,11 @@ private boolean processModifyColumn(ModifyColumnClause alterClause, OlapTable ol modColumn.setName(oriColumn.getName()); modColumn.setUniqueId(oriColumn.getUniqueId()); + if (!modColumn.equals(oriColumn) && oriColumn.isAutoInc() != modColumn.isAutoInc()) { + throw new DdlException("Can't modify the column[" + + oriColumn.getName() + "]'s auto-increment attribute."); + } + // handle the move operation in 'indexForFindingColumn' if has if (hasColPos) { // move col @@ -719,6 +724,12 @@ private boolean processModifyColumn(ModifyColumnClause alterClause, OlapTable ol } // end for handling other indices if (typeChanged && !lightSchemaChange) { + Optional autoIncCol = olapTable.getBaseSchema(true).stream() + .filter(col -> col.isAutoInc()).findFirst(); + if (autoIncCol.isPresent()) { + throw new DdlException("Can not modify column " + modColumn.getName() + " becasue table " + + olapTable.getName() + " has auto-increment column " + autoIncCol.get().getName()); + } /* * In new alter table process (AlterJobV2), any modified columns are treated as new columns. * But the modified columns' name does not changed. So in order to distinguish this, we will add @@ -835,6 +846,10 @@ private void addColumnInternal(Column newColumn, ColumnPosition columnPos, List< int posIndex = -1; boolean hasPos = (columnPos != null && !columnPos.isFirst()); + if (newColumn.isAutoInc()) { + throw new DdlException("Can not add auto-increment column " + newColumn.getName()); + } + for (int i = 0; i < modIndexSchema.size(); i++) { Column col = modIndexSchema.get(i); if (col.getName().equalsIgnoreCase(newColName)) { @@ -904,6 +919,10 @@ private boolean addColumnInternal(OlapTable olapTable, Column newColumn, ColumnP boolean lightSchemaChange = olapTable.getEnableLightSchemaChange(); String newColName = newColumn.getName(); + if (newColumn.isAutoInc()) { + throw new DdlException("Can not add auto-increment column " + newColumn.getName()); + } + // check the validation of aggregation method on column. // also fill the default aggregation method if not specified. if (KeysType.AGG_KEYS == olapTable.getKeysType()) { diff --git a/regression-test/data/schema_change_p0/test_schema_change_auto_inc.out b/regression-test/data/schema_change_p0/test_schema_change_auto_inc.out new file mode 100644 index 00000000000000..de085c473d076e --- /dev/null +++ b/regression-test/data/schema_change_p0/test_schema_change_auto_inc.out @@ -0,0 +1,10 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !sql -- +3 + +-- !sql -- +3 + +-- !sql -- +6 + diff --git a/regression-test/suites/schema_change_p0/test_schema_change_auto_inc.groovy b/regression-test/suites/schema_change_p0/test_schema_change_auto_inc.groovy new file mode 100644 index 00000000000000..eeae07bd247606 --- /dev/null +++ b/regression-test/suites/schema_change_p0/test_schema_change_auto_inc.groovy @@ -0,0 +1,90 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 +// +// http://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. + +suite("test_schema_change_auto_inc") { + + def table1 = "test_schema_change_auto_inc" + sql "drop table if exists ${table1}" + sql """ + CREATE TABLE IF NOT EXISTS `${table1}` ( + `name` varchar(65533) NOT NULL, + `value` int(11) NOT NULL DEFAULT "999", + `id` BIGINT NOT NULL AUTO_INCREMENT, + `id2` BIGINT NOT NULL DEFAULT "0" + ) ENGINE=OLAP + UNIQUE KEY(`name`) + COMMENT "OLAP" + DISTRIBUTED BY HASH(`name`) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "enable_unique_key_merge_on_write" = "false", + "light_schema_change" = "false" + )""" + + // add an auto-increment column + test { + sql "alter table ${table1} add column d BIGINT NULL AUTO_INCREMENT" + exception "Can not add auto-increment column d" + } + + // remove auto-increment attribute from a column + test { + sql "alter table ${table1} modify column id BIGINT NOT NULL" + exception "Can't modify the column[id]'s auto-increment attribute." + } + + // add auto-increment attribute to a column + test { + sql "alter table ${table1} modify column id2 BIGINT NOT NULL AUTO_INCREMENT" + exception "Can't modify the column[id2]'s auto-increment attribute." + } + + // schema change that invoke double write on a table which has auto-increment column is forbidden + test { + sql "alter table ${table1} modify column value VARCHAR(20) NOT NULL" + exception "Can not modify column value becasue table ${table1} has auto-increment column id" + } + + sql """ insert into ${table1}(name, value, id2) values("A", 999, 1), ("B", 888, 2), ("C", 777, 3);""" + qt_sql "select count(distinct id) from ${table1};" + + sql "alter table ${table1} modify column id BIGINT NOT NULL AUTO_INCREMENT after id2" + def getJobState = { tbName -> + def jobStateResult = sql """ SHOW ALTER TABLE COLUMN WHERE IndexName='${tbName}' ORDER BY createtime DESC LIMIT 1 """ + return jobStateResult[0][9] + } + + int max_try_time = 1000 + while (max_try_time--){ + String result = getJobState(table1) + if (result == "FINISHED") { + sleep(3000) + break + } else { + sleep(100) + if (max_try_time < 1){ + assertEquals(1,2) + } + } + } + + qt_sql "select count(distinct id) from ${table1};" + sql """ insert into ${table1}(name) values("D"), ("E"), ("F") """ + qt_sql "select count(distinct id) from ${table1};" + + sql "drop table if exists ${table1};" +}