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

kxiao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 44de050734 [branch2.0](fix) fix varchar len in ctas (#22813)
44de050734 is described below

commit 44de050734fc1531ee23953fae414c5cbb38d5c2
Author: Mingyu Chen <morning...@163.com>
AuthorDate: Thu Aug 10 15:01:46 2023 +0800

    [branch2.0](fix) fix varchar len in ctas (#22813)
---
 .../java/org/apache/doris/analysis/ColumnDef.java  |  9 ++++---
 .../analysis/CreateTableAsSelectStmtTest.java      | 28 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
index e65d5c4c9a..2ae7a04335 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
@@ -316,11 +316,14 @@ public class ColumnDef {
         if (typeDef.getType().isScalarType()) {
             final ScalarType targetType = (ScalarType) typeDef.getType();
             if (targetType.getPrimitiveType().isStringType() && 
!targetType.isLengthSet()) {
-                if (targetType.getPrimitiveType() != PrimitiveType.STRING) {
-                    targetType.setLength(1);
-                } else {
+                if (targetType.getPrimitiveType() == PrimitiveType.VARCHAR) {
+                    // always set varchar length MAX_VARCHAR_LENGTH
+                    targetType.setLength(ScalarType.MAX_VARCHAR_LENGTH);
+                } else if (targetType.getPrimitiveType() == 
PrimitiveType.STRING) {
                     // always set text length MAX_STRING_LENGTH
                     targetType.setLength(ScalarType.MAX_STRING_LENGTH);
+                } else {
+                    targetType.setLength(1);
                 }
             }
         }
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 2a5ff740d5..1d2bcdb146 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
@@ -576,4 +576,32 @@ public class CreateTableAsSelectStmtTest extends 
TestWithFeService {
         }
         Assert.assertEquals(2, createStmts.size());
     }
+
+    @Test
+    public void testVarcharLength() throws Exception {
+        String createSql =
+                "create table `test`.`varchar_len1` PROPERTIES 
(\"replication_num\" = \"1\")"
+                        + " as select 'abc', concat('xx', userId), userId from 
`test`.`varchar_table`";
+        createTableAsSelect(createSql);
+        ShowResultSet showResultSet = showCreateTableByName("varchar_len1");
+        String showStr = showResultSet.getResultRows().get(0).get(1);
+        Assertions.assertEquals(
+                "CREATE TABLE `varchar_len1` (\n"
+                        + "  `_col0` varchar(65533) NULL,\n"
+                        + "  `_col1` varchar(65533) NULL,\n"
+                        + "  `userId` varchar(255) NOT 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"
+                        + "\"is_being_synced\" = \"false\",\n"
+                        + "\"storage_format\" = \"V2\",\n"
+                        + "\"light_schema_change\" = \"true\",\n"
+                        + "\"disable_auto_compaction\" = \"false\",\n"
+                        + "\"enable_single_replica_compaction\" = \"false\"\n"
+                        + ");",
+                showStr);
+    }
 }


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

Reply via email to