yiguolei commented on code in PR #47096:
URL: https://github.com/apache/doris/pull/47096#discussion_r1991226985


##########
fe/fe-core/src/test/java/org/apache/doris/alter/SchemaChangeHandlerTest.java:
##########
@@ -95,6 +95,206 @@ private void waitAlterJobDone(Map<Long, AlterJobV2> 
alterJobs) throws Exception
         }
     }
 
+    private void executeAlterAndVerify(String alterStmt, OlapTable tbl, String 
expectedStruct, int expectSchemaVersion,
+            String columnName) throws Exception {
+        AlterTableStmt stmt = (AlterTableStmt) parseAndAnalyzeStmt(alterStmt);
+        Env.getCurrentEnv().getAlterInstance().processAlterTable(stmt);
+        
waitAlterJobDone(Env.getCurrentEnv().getSchemaChangeHandler().getAlterJobsV2());
+
+        tbl.readLock();
+        try {
+            Column column = tbl.getColumn(columnName);
+            
Assertions.assertTrue(column.getType().toSql().toLowerCase().contains(expectedStruct.toLowerCase()),
+                    "Actual struct: " + column.getType().toSql());
+            // then check schema version increase
+            MaterializedIndexMeta indexMeta = 
tbl.getIndexMetaByIndexId(tbl.getBaseIndexId());
+            int schemaVersion = indexMeta.getSchemaVersion();
+            LOG.info("schema version: {}", schemaVersion);
+            Assertions.assertEquals(expectSchemaVersion, schemaVersion);
+        } finally {
+            tbl.readUnlock();
+        }
+    }
+
+    private void expectException(String alterStmt, String expectedErrorMsg) {
+        try {
+            AlterTableStmt stmt = (AlterTableStmt) 
parseAndAnalyzeStmt(alterStmt);
+            Env.getCurrentEnv().getAlterInstance().processAlterTable(stmt);
+            
waitAlterJobDone(Env.getCurrentEnv().getSchemaChangeHandler().getAlterJobsV2());
+            Assertions.fail("Expected exception: " + expectedErrorMsg);
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+            Assertions.assertTrue(e.getMessage().contains(expectedErrorMsg),
+                    "Actual error: " + e.getMessage() + "\nExpected: " + 
expectedErrorMsg);
+        }
+    }
+
+    // In this test we should cover this following cases:
+    //  Positive Test Case
+    //    3.1 add sub-column
+    //    3.2 add sub-columns
+    //    3.3 add sub-column + lengthen sub-varchar-column
+    //  Negative Test Case
+    //    3.4 add sub-column + re-order struct-column
+    //    3.5 reduce sub-column
+    //    3.6 reduce sub-columns
+    //    3.7 add sub-column + shorten sub-varchar-column
+    //    3.8 change struct to other type
+    //    3.9 add sub-column + duplicate sub-column name
+    //    3.10 add sub-column + change origin sub-column name
+    //    3.11 add sub-column + change origin sub-column type
+    //    3.12 add sub-column with json/variant
+    // ------------------------- Positive Test Case -------------------------
+    private void testAddSingleSubColumn(OlapTable tbl, String tableName, 
String defaultValue) throws Exception {
+        String alterStmt = "ALTER TABLE test." + tableName + " MODIFY COLUMN 
c_s STRUCT<col:VARCHAR(10), col1:INT> "
+                + defaultValue;
+        executeAlterAndVerify(alterStmt, tbl, 
"STRUCT<col:varchar(10),col1:int>", 3, "c_s");
+    }
+
+    private void testAddNestedStructSubColumn(OlapTable tbl, String tableName, 
String defaultValue) throws Exception {
+        // origin c_s_s : struct<s1:struct<a:int>, 
s2:struct<a:array<struct<a:int>>>>
+        // case1. add s1 sub-column : struct<s1:struct<a:int, b:double>, 
s2:struct<a:array<struct<a:int>>>>
+        // case2. add s2 sub-column : struct<s1:struct<a:int,b:double>, 
s2:struct<a:array<struct<a:int>>, b:double>>
+        // case3. add s2.a sub-column : 
struct<s1:struct<a:int,b:double>,s2:struct<a:array<struct<a:int,b:double>>,b:double>>
+        // case4. add multiple sub-columns : 
struct<s1:struct<a:int,b:double,c:varchar(10)>,s2:struct<a:array<struct<a:int,b:double,c:varchar(10)>>,b:double,c:varchar(10)>,c:varchar(10)>
+        String alterStmt = "ALTER TABLE test." + tableName + " MODIFY COLUMN 
c_s_s "
+                + 
"struct<s1:struct<a:int,b:double>,s2:struct<a:array<struct<a:int>>>> " + 
defaultValue;
+        executeAlterAndVerify(alterStmt, tbl,
+                
"struct<s1:struct<a:int,b:double>,s2:struct<a:array<struct<a:int>>>>", 4, 
"c_s_s");
+        alterStmt = "ALTER TABLE test." + tableName + " MODIFY COLUMN c_s_s "
+                + "struct<s1:struct<a:int,b:double>, 
s2:struct<a:array<struct<a:int>>,b:double>> " + defaultValue;
+        executeAlterAndVerify(alterStmt, tbl,
+                
"struct<s1:struct<a:int,b:double>,s2:struct<a:array<struct<a:int>>,b:double>>", 
5, "c_s_s");
+        alterStmt = "ALTER TABLE test." + tableName + " MODIFY COLUMN c_s_s "
+                + "struct<s1:struct<a:int,b:double>, 
s2:struct<a:array<struct<a:int, b:double>>,b:double>> "
+                + defaultValue;
+        executeAlterAndVerify(alterStmt, tbl,
+                
"struct<s1:struct<a:int,b:double>,s2:struct<a:array<struct<a:int,b:double>>,b:double>>",
 6, "c_s_s");
+        alterStmt = "ALTER TABLE test." + tableName + " MODIFY COLUMN c_s_s "
+                + 
"struct<s1:struct<a:int,b:double,c:varchar(10)>,s2:struct<a:array<struct<a:int,b:double,c:varchar(10)>>,b:double,c:varchar(10)>,c:varchar(10)>
 "
+                + defaultValue;
+        executeAlterAndVerify(alterStmt, tbl,
+                
"struct<s1:struct<a:int,b:double,c:varchar(10)>,s2:struct<a:array<struct<a:int,b:double,c:varchar(10)>>,b:double,c:varchar(10)>,c:varchar(10)>",
+                7, "c_s_s");
+    }
+
+    private void testAddMultipleSubColumns(OlapTable tbl, String tableName, 
String defaultValue) throws Exception {
+        String alterStmt = "ALTER TABLE test." + tableName + " MODIFY COLUMN 
c_s STRUCT<col:VARCHAR(10), "
+                + "col1:INT, col2:DECIMAL(10,2), col3:DATETIME> " + 
defaultValue;
+        executeAlterAndVerify(alterStmt, tbl,
+                
"struct<col:varchar(10),col1:int,col2:decimalv3(10,2),col3:datetimev2(0)>", 8, 
"c_s");
+    }
+
+    private void testLengthenVarcharSubColumn(OlapTable tbl, String tableName, 
String defaultValue) throws Exception {
+        String alterStmt = "ALTER TABLE test." + tableName
+                + " MODIFY COLUMN c_s 
STRUCT<col:VARCHAR(30),col1:int,col2:decimal(10,2),col3:datetime,col4:string> "
+                + defaultValue;
+        executeAlterAndVerify(alterStmt, tbl,
+                
"struct<col:varchar(30),col1:int,col2:decimalv3(10,2),col3:datetimev2(0),col4:text>",
 9, "c_s");
+    }
+
+    // ------------------------- Negative Test Case -------------------------
+    private void testReduceSubColumns(String defaultValue, String tableName) {
+        String alterStmt = "ALTER TABLE test." + tableName + " MODIFY COLUMN 
c_s STRUCT<col:VARCHAR(10)> "
+                + defaultValue;
+        expectException(alterStmt, "Cannot reduce struct fields");
+    }
+
+    private void testShortenVarcharSubColumn(String defaultValue, String 
tableName) {
+        String alterStmt = "ALTER TABLE test." + tableName
+                + " MODIFY COLUMN c_s 
struct<col:varchar(10),col1:int,col2:decimalv3(10,2),col3:datetimev2(0),col4:string>
 "
+                + defaultValue;
+        expectException(alterStmt, "Shorten type length is prohibited");
+    }
+
+    private void testChangeStructToOtherType(String defaultValue, String 
tableName) {
+        String alterStmt = "ALTER TABLE test." + tableName + " MODIFY COLUMN 
c_s VARCHAR(100) " + defaultValue;
+        expectException(alterStmt, "Can not change");
+    }
+
+    private void testDuplicateSubColumnName(String defaultValue, String 
tableName) {
+        String alterStmt = "ALTER TABLE test." + tableName + " MODIFY COLUMN 
c_s STRUCT<col:VARCHAR(10), col:INT> "
+                + defaultValue;
+        expectException(alterStmt, "Duplicate field name");
+    }
+
+    private void testChangeExistingSubColumnName(String defaultValue, String 
tableName) {
+        String alterStmt = "ALTER TABLE test." + tableName
+                + " MODIFY COLUMN c_s struct<col6:varchar(30),"

Review Comment:
   我们要支持这个rename column 的能力吗?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to