[Bug](ctas/ctl): Ctas and ctl default value error (#12015)

This commit is contained in:
Stalary
2022-08-24 08:46:43 +08:00
committed by GitHub
parent 7798309807
commit 5c3367b67d
3 changed files with 87 additions and 55 deletions

View File

@ -550,7 +550,11 @@ public class Column implements Writable {
sb.append(" NOT NULL");
}
if (defaultValue != null && getDataType() != PrimitiveType.HLL && getDataType() != PrimitiveType.BITMAP) {
sb.append(" DEFAULT \"").append(defaultValue).append("\"");
if (defaultValueExprDef != null) {
sb.append(" DEFAULT ").append(defaultValue).append("");
} else {
sb.append(" DEFAULT \"").append(defaultValue).append("\"");
}
}
if (StringUtils.isNotBlank(comment)) {
sb.append(" COMMENT '").append(getComment(true)).append("'");

View File

@ -1146,9 +1146,15 @@ public class InternalCatalog implements CatalogIf<Database> {
} else {
Column column = resultExpr.getSrcSlotRef().getDesc().getColumn();
boolean setDefault = StringUtils.isNotBlank(column.getDefaultValue());
DefaultValue defaultValue;
if (column.getDefaultValueExprDef() != null) {
defaultValue = new DefaultValue(setDefault, column.getDefaultValue(),
column.getDefaultValueExprDef().getExprName());
} else {
defaultValue = new DefaultValue(setDefault, column.getDefaultValue());
}
columnDef = new ColumnDef(name, typeDef, column.isKey(), column.getAggregationType(),
column.isAllowNull(), new DefaultValue(setDefault, column.getDefaultValue()),
column.getComment());
column.isAllowNull(), defaultValue, column.getComment());
}
createTableStmt.addColumnDef(columnDef);
// set first column as default distribution

View File

@ -55,6 +55,13 @@ public class CreateTableAsSelectStmtTest extends TestWithFeService {
+ "\"replication_allocation\" = \"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
+ "\"storage_format\" = \"V2\"\n)";
createTable(joinTable);
String defaultTimestampTable =
"CREATE TABLE `test`.`default_timestamp_table` (`userId` varchar(255) NOT NULL,"
+ " `date` datetime NULL DEFAULT CURRENT_TIMESTAMP)" + " ENGINE = OLAP unique KEY(`userId`)\n"
+ "COMMENT 'default_timestamp_table'\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)";
createTable(defaultTimestampTable);
}
@Test
@ -77,16 +84,18 @@ public class CreateTableAsSelectStmtTest extends TestWithFeService {
if (Config.enable_decimal_conversion && Config.enable_decimalv3) {
Assertions.assertEquals(
"CREATE TABLE `select_decimal_table_1` (\n" + " `_col0` decimal(38, 2) NULL\n" + ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`_col0`)\n" + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY HASH(`_col0`) BUCKETS 10\n"
+ "PROPERTIES (\n" + "\"replication_allocation\" = \"tag.location.default: 1\",\n"
+ "DUPLICATE KEY(`_col0`)\n" + "COMMENT 'OLAP'\n"
+ "DISTRIBUTED BY HASH(`_col0`) 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_1").getResultRows().get(0).get(1));
} else {
Assertions.assertEquals(
"CREATE TABLE `select_decimal_table_1` (\n" + " `_col0` decimal(27, 9) NULL\n" + ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`_col0`)\n" + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY HASH(`_col0`) BUCKETS 10\n"
+ "PROPERTIES (\n" + "\"replication_allocation\" = \"tag.location.default: 1\",\n"
+ "DUPLICATE KEY(`_col0`)\n" + "COMMENT 'OLAP'\n"
+ "DISTRIBUTED BY HASH(`_col0`) 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_1").getResultRows().get(0).get(1));
@ -110,10 +119,11 @@ 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"
+ "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" + ")",
+ " `username` varchar(255) 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" + ")",
showResultSet.getResultRows().get(0).get(1));
}
@ -154,21 +164,20 @@ public class CreateTableAsSelectStmtTest extends TestWithFeService {
createTableAsSelect(selectAlias1);
ShowResultSet showResultSet1 = showCreateTableByName("select_alias_1");
Assertions.assertEquals("CREATE TABLE `select_alias_1` (\n" + " `amount` bigint(20) NULL\n" + ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`amount`)\n" + "COMMENT 'OLAP'\n"
+ "DISTRIBUTED BY HASH(`amount`) 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" + ")",
showResultSet1.getResultRows().get(0).get(1));
+ "DUPLICATE KEY(`amount`)\n" + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY HASH(`amount`) 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" + ")", showResultSet1.getResultRows().get(0).get(1));
String selectAlias2 = "create table `test`.`select_alias_2` PROPERTIES(\"replication_num\" = \"1\") "
+ "as select userId as alias_name, username from `test`.`varchar_table`";
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"
+ "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" + ")",
+ " `username` varchar(255) REPLACE NOT NULL\n"
+ ") ENGINE=OLAP\n" + "AGGREGATE 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" + ")",
showResultSet2.getResultRows().get(0).get(1));
}
@ -180,11 +189,11 @@ 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"
+ "\"storage_format\" = \"V2\",\n\"disable_auto_compaction\" = \"false\"\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"
+ "\"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\") "
+ "as select vt.userId as userId1, jt.userId as userId2, vt.username, jt.status "
@ -192,11 +201,12 @@ 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"
+ "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" + ")",
+ " `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"
+ "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" + ")",
showResultSet1.getResultRows().get(0).get(1));
}
@ -209,11 +219,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"
+ "\"storage_format\" = \"V2\",\n\"disable_auto_compaction\" = \"false\"\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"
+ "\"storage_format\" = \"V2\",\n\"disable_auto_compaction\" = \"false\"\n" + ")",
showResultSet.getResultRows().get(0).get(1));
}
@ -224,12 +234,10 @@ public class CreateTableAsSelectStmtTest extends TestWithFeService {
createTableAsSelect(selectFromName);
ShowResultSet showResultSet = showCreateTableByName("select_union");
Assertions.assertEquals("CREATE TABLE `select_union` (\n" + " `userId` varchar(255) 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));
+ "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
@ -249,13 +257,11 @@ public class CreateTableAsSelectStmtTest extends TestWithFeService {
+ "as with source_data as (select 1 as id union all select 2 as id) select * from source_data;";
createTableAsSelect(selectFromCteAndUnion);
ShowResultSet showResultSet1 = showCreateTableByName("select_cte_union");
Assertions.assertEquals(
"CREATE TABLE `select_cte_union` (\n" + " `id` tinyint(4) NULL\n" + ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`id`)\n" + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY HASH(`id`) 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" + ")",
showResultSet1.getResultRows().get(0).get(1));
Assertions.assertEquals("CREATE TABLE `select_cte_union` (\n" + " `id` tinyint(4) NULL\n" + ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`id`)\n" + "COMMENT 'OLAP'\n" + "DISTRIBUTED BY HASH(`id`) 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" + ")", showResultSet1.getResultRows().get(0).get(1));
}
@Test
@ -266,12 +272,28 @@ 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"
+ "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"
+ "\"replication_allocation\" = \"tag.location.default: 1\",\n" + "\"in_memory\" = \"false\",\n"
+ "\"storage_format\" = \"V2\",\n\"disable_auto_compaction\" = \"false\"\n" + ")",
+ " `username` varchar(255) REPLACE NOT NULL\n"
+ ") ENGINE=OLAP\n" + "AGGREGATE 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"
+ "\"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 testDefaultTimestamp() throws Exception {
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));
}
}