This is an automated email from the ASF dual-hosted git repository.

dataroaring pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new 5de6aa74c06 [branch-2.1] Picks "[opt](autoinc) Forbid some schema 
change when the table has auto-increment column #37186" (#37331)
5de6aa74c06 is described below

commit 5de6aa74c068fb923bfd34c544734d423c07617b
Author: bobhan1 <bh2444151...@outlook.com>
AuthorDate: Fri Jul 5 21:59:30 2024 +0800

    [branch-2.1] Picks "[opt](autoinc) Forbid some schema change when the table 
has auto-increment column #37186" (#37331)
    
    ## Proposed changes
    
    picks https://github.com/apache/doris/pull/37186
---
 .../apache/doris/alter/SchemaChangeHandler.java    | 19 +++++
 .../test_schema_change_auto_inc.out                | 10 +++
 .../test_schema_change_auto_inc.groovy             | 90 ++++++++++++++++++++++
 3 files changed, 119 insertions(+)

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 9d01d2bc77c..ccb99b7ac35 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 @@ public class SchemaChangeHandler extends AlterHandler {
         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 @@ public class SchemaChangeHandler extends AlterHandler {
         } // end for handling other indices
 
         if (typeChanged && !lightSchemaChange) {
+            Optional<Column> 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 @@ public class SchemaChangeHandler extends AlterHandler {
         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 @@ public class SchemaChangeHandler extends AlterHandler {
         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 00000000000..de085c473d0
--- /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 00000000000..eeae07bd247
--- /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};"
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to