From ff75ac32e8959a2bc5a18fa9963d1cb3c0586acd Mon Sep 17 00:00:00 2001 From: kikyo Date: Wed, 19 Apr 2023 14:58:21 +0800 Subject: [PATCH] fix ctas --- .../doris/datasource/InternalCatalog.java | 8 ++- .../analysis/CreateTableAsSelectStmtTest.java | 2 +- .../suites/ddl_p0/test_ctas.groovy | 49 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 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 dfa6a38c134f6f..06e4fd8a13368d 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 @@ -43,6 +43,7 @@ import org.apache.doris.analysis.DropPartitionClause; import org.apache.doris.analysis.DropTableStmt; import org.apache.doris.analysis.Expr; +import org.apache.doris.analysis.FunctionCallExpr; import org.apache.doris.analysis.HashDistributionDesc; import org.apache.doris.analysis.KeysDesc; import org.apache.doris.analysis.LinkDbStmt; @@ -1221,6 +1222,7 @@ public void createTableAsSelect(CreateTableAsSelectStmt stmt) throws DdlExceptio List columnNames = stmt.getColumnNames(); CreateTableStmt createTableStmt = stmt.getCreateTableStmt(); QueryStmt queryStmt = stmt.getQueryStmt(); + KeysDesc keysDesc = createTableStmt.getKeysDesc(); ArrayList resultExprs = queryStmt.getResultExprs(); ArrayList colLabels = queryStmt.getColLabels(); int size = resultExprs.size(); @@ -1242,7 +1244,11 @@ public void createTableAsSelect(CreateTableAsSelectStmt stmt) throws DdlExceptio TypeDef typeDef; Expr resultExpr = resultExprs.get(i); Type resultType = resultExpr.getType(); - if (resultType.isStringType()) { + if (resultExpr instanceof FunctionCallExpr + && resultExpr.getType().getPrimitiveType().equals(PrimitiveType.VARCHAR)) { + resultType = ScalarType.createVarchar(65533); + } + if (resultType.isStringType() && (keysDesc == null || !keysDesc.containsCol(name))) { // Use String for varchar/char/string type, // to avoid char-length-vs-byte-length issue. typeDef = new TypeDef(ScalarType.createStringType()); 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 58fcff2c2a3a0f..9544dfc7a6a25b 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 @@ -473,7 +473,7 @@ public void testUseKeyType() throws Exception { ShowResultSet showResultSet = showCreateTableByName("test_use_key_type"); Assertions.assertEquals( "CREATE TABLE `test_use_key_type` (\n" - + " `userId` varchar(65533) NOT NULL,\n" + + " `userId` varchar(255) NOT NULL,\n" + " `username` text NOT NULL\n" + ") ENGINE=OLAP\n" + "UNIQUE KEY(`userId`)\n" diff --git a/regression-test/suites/ddl_p0/test_ctas.groovy b/regression-test/suites/ddl_p0/test_ctas.groovy index 1fdb93cc589744..e17b82b2ec5b4c 100644 --- a/regression-test/suites/ddl_p0/test_ctas.groovy +++ b/regression-test/suites/ddl_p0/test_ctas.groovy @@ -107,6 +107,51 @@ suite("test_ctas") { rowNum 6 } + sql """ + create table if not exists test_tbl_81748325 + ( + `col1` varchar(66) not null , + `col2` bigint not null , + `col3` varchar(66) not null , + `col4` varchar(42) not null , + `col5` bigint not null , + `col6` bigint not null , + `col7` datetime not null , + `col8` varchar(66) not null, + `col9` varchar(66) , + `col10` varchar(66) , + `col11` varchar(66) , + `col12` text + ) + UNIQUE KEY (`col1`,`col2`,`col3`,`col4`,`col5`,`col6`) + DISTRIBUTED BY HASH(`col4`) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + """ + + sql """ + create table `test_tbl_3156019` + UNIQUE KEY (col4,col3,from_address,to_address) + DISTRIBUTED BY HASH (col4) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ) + as + select + col4 as col4, + col3 as col3, + concat('0x', substring(col9, 27)) as from_address, + concat('0x', substring(col10, 27)) as to_address, + col7 as date_time, + now() as update_time, + '20230318' as pt, + col8 as amount + from test_tbl_81748325 + where col4 = '43815251' + and substring(col8, 1, 10) = '1451601'; + """ + } finally { sql """ DROP TABLE IF EXISTS test_ctas """ @@ -119,6 +164,10 @@ suite("test_ctas") { sql """ DROP TABLE IF EXISTS test_ctas_json_object1 """ sql """drop table if exists a""" + + sql """DROP TABLE IF EXISTS test_tbl_81748325""" + + sql """DROP TABLE IF EXISTS test_tbl_3156019""" } }