Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1791,20 +1791,6 @@ && collectChildReturnTypes()[0].isDecimalV3()) {
this.type = PRECISION_INFER_RULE.getOrDefault(fnName.getFunction(), DEFAULT_PRECISION_INFER_RULE)
.apply(children, this.type);
}

// cast(xx as char(N)/varchar(N)) will be handled as substr(cast(xx as char, varchar), 1, N),
// but type is varchar(*), we change it to varchar(N);
if (fn.getFunctionName().getFunction().equals("substr")
&& children.size() == 3
&& children.get(1) instanceof IntLiteral
&& children.get(2) instanceof IntLiteral) {
long len = ((IntLiteral) children.get(2)).getValue();
if (type.isWildcardChar()) {
this.type = ScalarType.createCharType(((int) (len)));
} else if (type.isWildcardVarchar()) {
this.type = ScalarType.createVarchar(((int) (len)));
}
}
// rewrite return type if is nested type function
analyzeNestedFunction();
for (OrderByElement o : orderByElements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1197,24 +1197,13 @@ public void createTableAsSelect(CreateTableAsSelectStmt stmt) throws DdlExceptio
Expr resultExpr = resultExprs.get(i);
Type resultType = resultExpr.getType();
if (resultExpr instanceof FunctionCallExpr
&& resultExpr.getType().getPrimitiveType().equals(PrimitiveType.VARCHAR)
&& resultExpr.getType().getLength() == -1) {
resultType = ScalarType.createVarchar(ScalarType.MAX_VARCHAR_LENGTH);
&& resultExpr.getType().getPrimitiveType().equals(PrimitiveType.VARCHAR)) {
resultType = ScalarType.createVarchar(65533);
}
if (resultType.isStringType() && (keysDesc == null || !keysDesc.containsCol(name))) {
switch (resultType.getPrimitiveType()) {
case STRING:
typeDef = new TypeDef(ScalarType.createStringType());
break;
case VARCHAR:
typeDef = new TypeDef(ScalarType.createVarchar(resultType.getLength()));
break;
case CHAR:
typeDef = new TypeDef(ScalarType.createCharType(resultType.getLength()));
break;
default:
throw new DdlException("Unsupported string type for ctas");
}
// Use String for varchar/char/string type,
// to avoid char-length-vs-byte-length issue.
typeDef = new TypeDef(ScalarType.createStringType());
} else if (resultType.isDecimalV2() && resultType.equals(ScalarType.DECIMALV2)) {
typeDef = new TypeDef(ScalarType.createDecimalType(27, 9));
} else if (resultType.isDecimalV3()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testDecimal() throws Exception {
+ "as select * from `test`.`decimal_table`";
createTableAsSelect(selectFromDecimal);
Assertions.assertEquals("CREATE TABLE `select_decimal_table` (\n"
+ " `userId` varchar(255) NOT NULL,\n"
+ " `userId` varchar(65533) NOT NULL,\n"
+ " `amount_decimal` "
+ "DECIMAL" + "(10, 2) NOT NULL\n"
+ ") ENGINE=OLAP\n"
Expand Down Expand Up @@ -157,8 +157,8 @@ public void testVarchar() throws Exception {
createTableAsSelect(selectFromVarchar);
ShowResultSet showResultSet = showCreateTableByName("select_varchar");
Assertions.assertEquals("CREATE TABLE `select_varchar` (\n"
+ " `userId` varchar(255) NOT NULL,\n"
+ " `username` varchar(255) NOT NULL\n"
+ " `userId` varchar(65533) NOT NULL,\n"
+ " `username` text NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`userId`)\n"
+ "COMMENT 'OLAP'\n"
Expand Down Expand Up @@ -249,8 +249,8 @@ public void testAlias() throws Exception {
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) NOT NULL\n"
+ " `alias_name` varchar(65533) NOT NULL,\n"
+ " `username` text NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`alias_name`)\n"
+ "COMMENT 'OLAP'\n"
Expand All @@ -274,8 +274,8 @@ public void testJoin() throws Exception {
createTableAsSelect(selectFromJoin);
ShowResultSet showResultSet = showCreateTableByName("select_join");
Assertions.assertEquals("CREATE TABLE `select_join` (\n"
+ " `userId` varchar(255) NOT NULL,\n"
+ " `username` varchar(255) NOT NULL,\n"
+ " `userId` varchar(65533) NOT NULL,\n"
+ " `username` text NOT NULL,\n"
+ " `status` int(11) NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`userId`)\n"
Expand All @@ -296,9 +296,9 @@ public void testJoin() throws Exception {
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) NOT NULL,\n"
+ " `userId1` varchar(65533) NOT NULL,\n"
+ " `userId2` text NOT NULL,\n"
+ " `username` text NOT NULL,\n"
+ " `status` int(11) NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`userId1`)\n"
Expand All @@ -324,8 +324,8 @@ public void testName() throws Exception {
createTableAsSelect(selectFromName);
ShowResultSet showResultSet = showCreateTableByName("select_name");
Assertions.assertEquals("CREATE TABLE `select_name` (\n"
+ " `user` varchar(255) NOT NULL,\n"
+ " `testname` varchar(255) NOT NULL,\n"
+ " `user` varchar(65533) NOT NULL,\n"
+ " `testname` text NOT NULL,\n"
+ " `userstatus` int(11) NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`user`)\n"
Expand All @@ -350,7 +350,7 @@ public void testUnion() throws Exception {
ShowResultSet showResultSet = showCreateTableByName("select_union");
Assertions.assertEquals(
"CREATE TABLE `select_union` (\n"
+ " `userId` varchar(255) NULL\n"
+ " `userId` varchar(65533) NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`userId`)\n"
+ "COMMENT 'OLAP'\n"
Expand All @@ -373,7 +373,7 @@ public void testCte() throws Exception {
ShowResultSet showResultSet = showCreateTableByName("select_cte");
Assertions.assertEquals(
"CREATE TABLE `select_cte` (\n"
+ " `userId` varchar(255) NOT NULL\n"
+ " `userId` varchar(65533) NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`userId`)\n"
+ "COMMENT 'OLAP'\n"
Expand Down Expand Up @@ -415,8 +415,8 @@ public void testPartition() throws Exception {
createTableAsSelect(selectFromPartition);
ShowResultSet showResultSet = showCreateTableByName("selectPartition");
Assertions.assertEquals("CREATE TABLE `selectPartition` (\n"
+ " `userId` varchar(255) NOT NULL,\n"
+ " `username` varchar(255) NOT NULL\n"
+ " `userId` varchar(65533) NOT NULL,\n"
+ " `username` text NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`userId`)\n"
+ "COMMENT 'OLAP'\n"
Expand All @@ -441,7 +441,7 @@ public void testDefaultTimestamp() throws Exception {
createTableAsSelect(createSql);
ShowResultSet showResultSet = showCreateTableByName("test_default_timestamp");
Assertions.assertEquals("CREATE TABLE `test_default_timestamp` (\n"
+ " `userId` varchar(255) NOT NULL,\n"
+ " `userId` varchar(65533) NOT NULL,\n"
+ " `date` datetime"
+ " NULL DEFAULT CURRENT_TIMESTAMP\n"
+ ") ENGINE=OLAP\n"
Expand All @@ -467,7 +467,7 @@ public void testAggValue() throws Exception {
ShowResultSet showResultSet = showCreateTableByName("test_agg_value");
Assertions.assertEquals(
"CREATE TABLE `test_agg_value` (\n"
+ " `username` varchar(255) NOT NULL\n"
+ " `username` varchar(65533) NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`username`)\n"
+ "COMMENT 'OLAP'\n"
Expand All @@ -492,7 +492,7 @@ public void testUseKeyType() throws Exception {
Assertions.assertEquals(
"CREATE TABLE `test_use_key_type` (\n"
+ " `userId` varchar(255) NOT NULL,\n"
+ " `username` varchar(255) NOT NULL\n"
+ " `username` text NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "UNIQUE KEY(`userId`)\n"
+ "COMMENT 'OLAP'\n"
Expand Down