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

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 0d33c713d1 [Bug](CTAS) Fix CTAS error for use agg column as first. 
(#12299)
0d33c713d1 is described below

commit 0d33c713d1686ad6f5c32af8af5955859c763c5e
Author: Stalary <stal...@163.com>
AuthorDate: Fri Sep 2 20:44:01 2022 +0800

    [Bug](CTAS) Fix CTAS error for use agg column as first. (#12299)
    
    * FIX: ctas default use duplicate key.
---
 .../apache/doris/datasource/InternalCatalog.java   |  7 +-
 .../analysis/CreateTableAsSelectStmtTest.java      | 76 ++++++++++++++--------
 regression-test/data/ddl_p0/test_ctas.out          |  2 +-
 regression-test/suites/ddl_p0/test_ctas.groovy     | 18 ++---
 regression-test/suites/ddl_p0/test_ctl.groovy      | 14 ++--
 5 files changed, 68 insertions(+), 49 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java 
b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
index 456ee1693e..fbfee9d842 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
@@ -59,7 +59,6 @@ import org.apache.doris.analysis.TruncateTableStmt;
 import org.apache.doris.analysis.TypeDef;
 import org.apache.doris.analysis.UserDesc;
 import org.apache.doris.analysis.UserIdentity;
-import org.apache.doris.catalog.AggregateType;
 import org.apache.doris.catalog.BrokerTable;
 import org.apache.doris.catalog.ColocateGroupSchema;
 import org.apache.doris.catalog.ColocateTableIndex;
@@ -1154,11 +1153,7 @@ public class InternalCatalog implements 
CatalogIf<Database> {
                     } else {
                         defaultValue = new DefaultValue(setDefault, 
column.getDefaultValue());
                     }
-                    // AggregateType.NONE cause the table to change to the 
AGGREGATE KEY when analyze is used,
-                    // cause CURRENT_TIMESTAMP to report an error.
-                    columnDef = new ColumnDef(name, typeDef, column.isKey(),
-                            
AggregateType.NONE.equals(column.getAggregationType())
-                                    ? null : column.getAggregationType(),
+                    columnDef = new ColumnDef(name, typeDef, false, null,
                             column.isAllowNull(), defaultValue, 
column.getComment());
                 }
                 createTableStmt.addColumnDef(columnDef);
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateTableAsSelectStmtTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateTableAsSelectStmtTest.java
index 2481ea7109..22e78471b0 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateTableAsSelectStmtTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateTableAsSelectStmtTest.java
@@ -83,11 +83,11 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
                 + "as select * from `test`.`decimal_table`";
         createTableAsSelect(selectFromDecimal);
         Assertions.assertEquals("CREATE TABLE `select_decimal_table` (\n" + "  
`userId` varchar(255) NOT NULL,\n"
-                        + "  `amount_decimal` decimal(10, 2) REPLACE NOT 
NULL\n" + ") ENGINE=OLAP\n"
-                        + "AGGREGATE KEY(`userId`)\n" + "COMMENT 'OLAP'\n" + 
"DISTRIBUTED BY HASH(`userId`) BUCKETS 10\n"
-                        + "PROPERTIES (\n" + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n"
-                        + "\"in_memory\" = \"false\",\n" + "\"storage_format\" 
= \"V2\","
-                        + "\n\"disable_auto_compaction\" = \"false\"\n" + ");",
+                        + "  `amount_decimal` decimal(10, 2) NOT NULL\n"
+                        + ") ENGINE=OLAP\n" + "DUPLICATE KEY(`userId`)\n"
+                        + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY HASH(`userId`) 
BUCKETS 10\n" + "PROPERTIES (\n"
+                        + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
+                        + "\"storage_format\" = \"V2\"," + 
"\n\"disable_auto_compaction\" = \"false\"\n" + ");",
                 
showCreateTableByName("select_decimal_table").getResultRows().get(0).get(1));
         String selectFromDecimal1 =
                 "create table `test`.`select_decimal_table_1` 
PROPERTIES(\"replication_num\" = \"1\") "
@@ -131,7 +131,7 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
         createTableAsSelect(selectFromVarchar);
         ShowResultSet showResultSet = showCreateTableByName("select_varchar");
         Assertions.assertEquals("CREATE TABLE `select_varchar` (\n" + "  
`userId` varchar(255) NOT NULL,\n"
-                        + "  `username` varchar(255) REPLACE NOT NULL\n" + ") 
ENGINE=OLAP\n" + "AGGREGATE KEY(`userId`)\n"
+                        + "  `username` varchar(255) NOT NULL\n" + ") 
ENGINE=OLAP\n" + "DUPLICATE KEY(`userId`)\n"
                         + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY HASH(`userId`) 
BUCKETS 10\n" + "PROPERTIES (\n"
                         + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
                         + "\"storage_format\" = 
\"V2\",\n\"disable_auto_compaction\" = \"false\"\n" + ");",
@@ -184,7 +184,7 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
         createTableAsSelect(selectAlias2);
         ShowResultSet showResultSet2 = showCreateTableByName("select_alias_2");
         Assertions.assertEquals("CREATE TABLE `select_alias_2` (\n" + "  
`alias_name` varchar(255) NOT NULL,\n"
-                        + "  `username` varchar(255) REPLACE NOT NULL\n" + ") 
ENGINE=OLAP\n" + "AGGREGATE KEY(`alias_name`)\n"
+                        + "  `username` varchar(255) NOT NULL\n" + ") 
ENGINE=OLAP\n" + "DUPLICATE KEY(`alias_name`)\n"
                         + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY 
HASH(`alias_name`) BUCKETS 10\n" + "PROPERTIES (\n"
                         + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
                         + "\"storage_format\" = 
\"V2\",\n\"disable_auto_compaction\" = \"false\"\n" + ");",
@@ -199,10 +199,12 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
         createTableAsSelect(selectFromJoin);
         ShowResultSet showResultSet = showCreateTableByName("select_join");
         Assertions.assertEquals("CREATE TABLE `select_join` (\n" + "  `userId` 
varchar(255) NOT NULL,\n"
-                        + "  `username` varchar(255) REPLACE NOT NULL,\n" + "  
`status` int(11) REPLACE NOT NULL\n"
-                        + ") ENGINE=OLAP\n" + "AGGREGATE KEY(`userId`)\n" + 
"COMMENT 'OLAP'\n"
-                        + "DISTRIBUTED BY HASH(`userId`) BUCKETS 10\n" + 
"PROPERTIES (\n"
-                        + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
+                        + "  `username` varchar(255) NOT NULL,\n"
+                        + "  `status` int(11) NOT NULL\n" + ") ENGINE=OLAP\n"
+                        + "DUPLICATE KEY(`userId`)\n" + "COMMENT 'OLAP'\n"
+                        + "DISTRIBUTED BY HASH(`userId`) BUCKETS 10\n"
+                        + "PROPERTIES (\n" + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n"
+                        + "\"in_memory\" = \"false\",\n"
                         + "\"storage_format\" = 
\"V2\",\n\"disable_auto_compaction\" = \"false\"\n" + ");",
                 showResultSet.getResultRows().get(0).get(1));
         String selectFromJoin1 = "create table `test`.`select_join1` 
PROPERTIES(\"replication_num\" = \"1\") "
@@ -211,8 +213,8 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
         createTableAsSelect(selectFromJoin1);
         ShowResultSet showResultSet1 = showCreateTableByName("select_join1");
         Assertions.assertEquals("CREATE TABLE `select_join1` (\n" + "  
`userId1` varchar(255) NOT NULL,\n"
-                        + "  `userId2` varchar(255) NOT NULL,\n" + "  
`username` varchar(255) REPLACE NOT NULL,\n"
-                        + "  `status` int(11) REPLACE NOT NULL\n" + ") 
ENGINE=OLAP\n" + "AGGREGATE KEY(`userId1`, `userId2`)\n"
+                        + "  `userId2` varchar(255) NOT NULL,\n" + "  
`username` varchar(255) NOT NULL,\n"
+                        + "  `status` int(11) NOT NULL\n" + ") ENGINE=OLAP\n" 
+ "DUPLICATE KEY(`userId1`)\n"
                         + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY HASH(`userId1`) 
BUCKETS 10\n" + "PROPERTIES (\n"
                         + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
                         + "\"storage_format\" = 
\"V2\",\n\"disable_auto_compaction\" = \"false\"\n" + ");",
@@ -228,10 +230,11 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
         createTableAsSelect(selectFromName);
         ShowResultSet showResultSet = showCreateTableByName("select_name");
         Assertions.assertEquals("CREATE TABLE `select_name` (\n" + "  `user` 
varchar(255) NOT NULL,\n"
-                        + "  `testname` varchar(255) REPLACE NOT NULL,\n" + "  
`userstatus` int(11) REPLACE NOT NULL\n"
-                        + ") ENGINE=OLAP\n" + "AGGREGATE KEY(`user`)\n" + 
"COMMENT 'OLAP'\n"
-                        + "DISTRIBUTED BY HASH(`user`) BUCKETS 10\n" + 
"PROPERTIES (\n"
-                        + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
+                        + "  `testname` varchar(255) NOT NULL,\n"
+                        + "  `userstatus` int(11) NOT NULL\n" + ") 
ENGINE=OLAP\n"
+                        + "DUPLICATE KEY(`user`)\n" + "COMMENT 'OLAP'\n" + 
"DISTRIBUTED BY HASH(`user`) BUCKETS 10\n"
+                        + "PROPERTIES (\n" + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n"
+                        + "\"in_memory\" = \"false\",\n"
                         + "\"storage_format\" = 
\"V2\",\n\"disable_auto_compaction\" = \"false\"\n" + ");",
                 showResultSet.getResultRows().get(0).get(1));
     }
@@ -281,7 +284,7 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
         createTableAsSelect(selectFromPartition);
         ShowResultSet showResultSet = showCreateTableByName("selectPartition");
         Assertions.assertEquals("CREATE TABLE `selectPartition` (\n" + "  
`userId` varchar(255) NOT NULL,\n"
-                        + "  `username` varchar(255) REPLACE NOT NULL\n" + ") 
ENGINE=OLAP\n" + "AGGREGATE KEY(`userId`)\n"
+                        + "  `username` varchar(255) NOT NULL\n" + ") 
ENGINE=OLAP\n" + "DUPLICATE KEY(`userId`)\n"
                         + "COMMENT 'OLAP'\n" + "PARTITION BY LIST(`userId`)\n"
                         + "(PARTITION p1 VALUES IN 
(\"CA\",\"GB\",\"US\",\"ZH\"))\n"
                         + "DISTRIBUTED BY HASH(`userId`) BUCKETS 10\n" + 
"PROPERTIES (\n"
@@ -292,17 +295,32 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
 
     @Test
     public void testDefaultTimestamp() throws Exception {
-        String createSql
-                = "create table `test`.`test_default_timestamp` PROPERTIES 
(\"replication_num\" = \"1\")"
+        String createSql = "create table `test`.`test_default_timestamp` 
PROPERTIES (\"replication_num\" = \"1\")"
                 + " as select * from `test`.`default_timestamp_table`";
         createTableAsSelect(createSql);
         ShowResultSet showResultSet = 
showCreateTableByName("test_default_timestamp");
         Assertions.assertEquals("CREATE TABLE `test_default_timestamp` (\n" + 
"  `userId` varchar(255) NOT NULL,\n"
-                + "  `date` datetime REPLACE NULL DEFAULT CURRENT_TIMESTAMP\n" 
+ ") ENGINE=OLAP\n"
-                + "AGGREGATE KEY(`userId`)\n" + "COMMENT 'OLAP'\n" + 
"DISTRIBUTED BY HASH(`userId`) BUCKETS 10\n"
-                + "PROPERTIES (\n" + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n"
-                + "\"in_memory\" = \"false\",\n" + "\"storage_format\" = 
\"V2\",\n"
-                + "\"disable_auto_compaction\" = \"false\"\n" + ");", 
showResultSet.getResultRows().get(0).get(1));
+                        + "  `date` datetime NULL DEFAULT CURRENT_TIMESTAMP\n"
+                        + ") ENGINE=OLAP\n" + "DUPLICATE KEY(`userId`)\n"
+                        + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY HASH(`userId`) 
BUCKETS 10\n" + "PROPERTIES (\n"
+                        + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
+                        + "\"storage_format\" = \"V2\",\n" + 
"\"disable_auto_compaction\" = \"false\"\n" + ");",
+                showResultSet.getResultRows().get(0).get(1));
+    }
+
+    @Test
+    public void testAggValue() throws Exception {
+        String createSql = "create table `test`.`test_agg_value` PROPERTIES 
(\"replication_num\" = \"1\")"
+                + " as select username from `test`.`varchar_table`";
+        createTableAsSelect(createSql);
+        ShowResultSet showResultSet = showCreateTableByName("test_agg_value");
+        Assertions.assertEquals(
+                "CREATE TABLE `test_agg_value` (\n" + "  `username` 
varchar(255) NOT NULL\n" + ") ENGINE=OLAP\n"
+                        + "DUPLICATE KEY(`username`)\n" + "COMMENT 'OLAP'\n"
+                        + "DISTRIBUTED BY HASH(`username`) BUCKETS 10\n" + 
"PROPERTIES (\n"
+                        + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
+                        + "\"storage_format\" = \"V2\",\n" + 
"\"disable_auto_compaction\" = \"false\"\n" + ");",
+                showResultSet.getResultRows().get(0).get(1));
     }
 
     @Test
@@ -322,7 +340,7 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
 
         String sql1 = "select * from v1";
 
-        org.apache.doris.analysis.SqlParser parser = new SqlParser(
+        org.apache.doris.analysis.SqlParser parser = new 
org.apache.doris.analysis.SqlParser(
                 new org.apache.doris.analysis.SqlScanner(new 
StringReader(sql1)));
         QueryStmt stmt = (QueryStmt) SqlParserUtils.getStmt(parser, 0);
         Map<Long, TableIf> tableMap = Maps.newHashMap();
@@ -339,14 +357,16 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
                 Assert.assertEquals("CREATE TABLE `qs1` (\n" + "  `k1` int(11) 
NULL,\n" + "  `k2` int(11) NULL\n"
                                 + ") ENGINE=OLAP\n" + "DUPLICATE KEY(`k1`, 
`k2`)\n" + "COMMENT 'OLAP'\n"
                                 + "DISTRIBUTED BY HASH(`k1`) BUCKETS 1\n" + 
"PROPERTIES (\n"
-                                + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
+                                + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n"
+                                + "\"in_memory\" = \"false\",\n"
                                 + "\"storage_format\" = \"V2\",\n" + 
"\"disable_auto_compaction\" = \"false\"\n" + ");",
                         createTableStmts.get(0));
             } else {
                 Assert.assertEquals("CREATE TABLE `qs2` (\n" + "  `k1` int(11) 
NULL,\n" + "  `k2` int(11) NULL\n"
                                 + ") ENGINE=OLAP\n" + "DUPLICATE KEY(`k1`, 
`k2`)\n" + "COMMENT 'OLAP'\n"
                                 + "DISTRIBUTED BY HASH(`k1`) BUCKETS 1\n" + 
"PROPERTIES (\n"
-                                + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
+                                + "\"replication_allocation\" = 
\"tag.location.default: 1\",\n"
+                                + "\"in_memory\" = \"false\",\n"
                                 + "\"storage_format\" = \"V2\",\n" + 
"\"disable_auto_compaction\" = \"false\"\n" + ");",
                         createTableStmts.get(0));
             }
diff --git a/regression-test/data/ddl_p0/test_ctas.out 
b/regression-test/data/ddl_p0/test_ctas.out
index aa20456016..aa41c6a791 100644
--- a/regression-test/data/ddl_p0/test_ctas.out
+++ b/regression-test/data/ddl_p0/test_ctas.out
@@ -1,6 +1,6 @@
 -- This file is automatically generated. You should know what you did if you 
want to edit this
 -- !select --
-test_ctas1     CREATE TABLE `test_ctas1` (\n  `test_varchar` varchar(150) 
NULL,\n  `test_datetime` datetime REPLACE NULL,\n  `test_default_timestamp` 
datetime REPLACE NULL DEFAULT CURRENT_TIMESTAMP\n) ENGINE=OLAP\nAGGREGATE 
KEY(`test_varchar`)\nCOMMENT 'OLAP'\nDISTRIBUTED BY HASH(`test_varchar`) 
BUCKETS 10\nPROPERTIES (\n"replication_allocation" = "tag.location.default: 
1",\n"in_memory" = "false",\n"storage_format" = 
"V2",\n"disable_auto_compaction" = "false"\n);
+test_ctas1     CREATE TABLE `test_ctas1` (\n  `test_varchar` varchar(150) 
NULL,\n  `test_datetime` datetime NULL,\n  `test_default_timestamp` datetime 
NULL DEFAULT CURRENT_TIMESTAMP\n) ENGINE=OLAP\nDUPLICATE 
KEY(`test_varchar`)\nCOMMENT 'OLAP'\nDISTRIBUTED BY HASH(`test_varchar`) 
BUCKETS 10\nPROPERTIES (\n"replication_allocation" = "tag.location.default: 
1",\n"in_memory" = "false",\n"storage_format" = 
"V2",\n"disable_auto_compaction" = "false"\n);
 
 -- !select --
 2
diff --git a/regression-test/suites/ddl_p0/test_ctas.groovy 
b/regression-test/suites/ddl_p0/test_ctas.groovy
index cfd7e825c9..e19a32b997 100644
--- a/regression-test/suites/ddl_p0/test_ctas.groovy
+++ b/regression-test/suites/ddl_p0/test_ctas.groovy
@@ -16,7 +16,8 @@
 // under the License.
 
 suite("test_ctas") {
-    sql """
+    try {
+        sql """
     CREATE TABLE IF NOT EXISTS `test_ctas` (
       `test_varchar` varchar(150) NULL,
       `test_datetime` datetime NULL,
@@ -31,9 +32,9 @@ suite("test_ctas") {
     )
     """
 
-    sql """ INSERT INTO test_ctas(test_varchar, test_datetime) VALUES 
('test1','2022-04-27 16:00:33'),('test2','2022-04-27 16:00:54') """
+        sql """ INSERT INTO test_ctas(test_varchar, test_datetime) VALUES 
('test1','2022-04-27 16:00:33'),('test2','2022-04-27 16:00:54') """
 
-    sql """ 
+        sql """ 
     CREATE TABLE IF NOT EXISTS `test_ctas1` 
     PROPERTIES (
       "replication_allocation" = "tag.location.default: 1",
@@ -42,12 +43,13 @@ suite("test_ctas") {
     ) as select * from test_ctas;
     """
 
-    qt_select """SHOW CREATE TABLE `test_ctas1`"""
+        qt_select """SHOW CREATE TABLE `test_ctas1`"""
 
-    qt_select """select count(*) from test_ctas1"""
+        qt_select """select count(*) from test_ctas1"""
+    } finally {
+        sql """ DROP TABLE IF EXISTS test_ctas """
 
-    sql """ DROP TABLE IF EXISTS test_ctas """
-
-    sql """ DROP TABLE IF EXISTS test_ctas1 """
+        sql """ DROP TABLE IF EXISTS test_ctas1 """
+    }
 
 }
diff --git a/regression-test/suites/ddl_p0/test_ctl.groovy 
b/regression-test/suites/ddl_p0/test_ctl.groovy
index ef1735c167..b0669ca9ef 100644
--- a/regression-test/suites/ddl_p0/test_ctl.groovy
+++ b/regression-test/suites/ddl_p0/test_ctl.groovy
@@ -16,7 +16,8 @@
 // under the License.
 
 suite("test_ctl") {
-    sql """
+    try {
+        sql """
     CREATE TABLE IF NOT EXISTS `test_ctl` (
       `test_varchar` varchar(150) NULL,
       `test_datetime` datetime NULL,
@@ -31,14 +32,15 @@ suite("test_ctl") {
     )
     """
 
-    sql """ 
+        sql """ 
     CREATE TABLE IF NOT EXISTS `test_ctl1` LIKE `test_ctl`
     """
 
-    qt_select """SHOW CREATE TABLE `test_ctl1`"""
+        qt_select """SHOW CREATE TABLE `test_ctl1`"""
+    } finally {
+        sql """ DROP TABLE IF EXISTS test_ctl """
 
-    sql """ DROP TABLE IF EXISTS test_ctl """
-
-    sql """ DROP TABLE IF EXISTS test_ctl1 """
+        sql """ DROP TABLE IF EXISTS test_ctl1 """
+    }
 
 }


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

Reply via email to