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

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


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new aed6d470969 branch-3.0: [fix](sql) Fix error result for column tosql 
#44997 (#45395)
aed6d470969 is described below

commit aed6d470969264945a4bc83d1d0ca04ed9e578e7
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Dec 18 14:59:22 2024 +0800

    branch-3.0: [fix](sql) Fix error result for column tosql #44997 (#45395)
    
    Cherry-picked from #44997
    
    Co-authored-by: Uniqueyou <1520358...@qq.com>
---
 .../java/org/apache/doris/analysis/ColumnDef.java  | 30 ++++----
 .../doris/analysis/AddColumnDefaultValueTest.java  | 89 ++++++++++++++++++++++
 2 files changed, 106 insertions(+), 13 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 3a046f36a17..fc3ea435ca2 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
@@ -30,6 +30,7 @@ import org.apache.doris.catalog.Type;
 import org.apache.doris.common.AnalysisException;
 import org.apache.doris.common.Config;
 import org.apache.doris.common.FeNameFormat;
+import org.apache.doris.common.util.SqlUtils;
 import org.apache.doris.common.util.TimeUtils;
 import org.apache.doris.qe.SessionVariable;
 
@@ -180,17 +181,6 @@ public class ColumnDef {
             }
             return value;
         }
-
-        public String toSql() {
-            StringBuilder sb = new StringBuilder();
-            sb.append("DEFAULT ");
-            if (value != null) {
-                sb.append('"').append(value).append('"');
-            } else {
-                sb.append("NULL");
-            }
-            return sb.toString();
-        }
     }
 
     // parameter initialized in constructor
@@ -433,7 +423,7 @@ public class ColumnDef {
         }
 
         if (type.getPrimitiveType() == PrimitiveType.HLL) {
-            if (defaultValue.isSet) {
+            if (defaultValue != null && defaultValue.isSet) {
                 throw new AnalysisException("Hll type column can not set 
default value");
             }
             defaultValue = DefaultValue.HLL_EMPTY_DEFAULT_VALUE;
@@ -681,7 +671,21 @@ public class ColumnDef {
         }
 
         if (defaultValue.isSet) {
-            sb.append(defaultValue.toSql()).append(" ");
+            if (defaultValue.value != null) {
+                if (typeDef.getType().getPrimitiveType() != 
PrimitiveType.BITMAP
+                        && typeDef.getType().getPrimitiveType() != 
PrimitiveType.HLL) {
+                    if (defaultValue.defaultValueExprDef != null) {
+                        sb.append("DEFAULT 
").append(defaultValue.value).append(" ");
+                    } else {
+                        sb.append("DEFAULT 
").append("\"").append(SqlUtils.escapeQuota(defaultValue.value)).append("\"")
+                                .append(" ");
+                    }
+                } else if (typeDef.getType().getPrimitiveType() == 
PrimitiveType.BITMAP) {
+                    sb.append("DEFAULT 
").append(defaultValue.defaultValueExprDef.getExprName()).append(" ");
+                }
+            } else {
+                sb.append("DEFAULT ").append("NULL").append(" ");
+            }
         }
         sb.append("COMMENT \"").append(comment).append("\"");
 
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/analysis/AddColumnDefaultValueTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/analysis/AddColumnDefaultValueTest.java
new file mode 100644
index 00000000000..4f59ee068db
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/analysis/AddColumnDefaultValueTest.java
@@ -0,0 +1,89 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.analysis;
+
+import org.apache.doris.analysis.ColumnDef.DefaultValue;
+import org.apache.doris.catalog.AggregateType;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.PrimitiveType;
+import org.apache.doris.catalog.ScalarType;
+import org.apache.doris.common.AnalysisException;
+
+import com.google.common.collect.Lists;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.List;
+
+public class AddColumnDefaultValueTest {
+    private static Analyzer analyzer;
+
+    @BeforeClass
+    public static void setUp() {
+        analyzer = AccessTestUtil.fetchAdminAnalyzer(false);
+    }
+
+    @Test
+    public void testNormal() throws AnalysisException {
+        List<ColumnDef> columns = Lists.newArrayList();
+        ColumnDef definition = new ColumnDef("col1", new 
TypeDef(ScalarType.createType(PrimitiveType.DATETIME)),
+                true,
+                null, false,
+                DefaultValue.CURRENT_TIMESTAMP_DEFAULT_VALUE, "");
+        definition.setKeysType(KeysType.DUP_KEYS);
+        columns.add(definition);
+        definition = new ColumnDef("col2", new 
TypeDef(ScalarType.createType(PrimitiveType.INT)),
+                false,
+                null, false,
+                new DefaultValue(true, 1), "");
+        columns.add(definition);
+        definition = new ColumnDef("col3", new 
TypeDef(ScalarType.createType(PrimitiveType.VARCHAR)),
+                false,
+                null, false,
+                new DefaultValue(true, "varchar"), "");
+        columns.add(definition);
+        definition = new ColumnDef("col4", new 
TypeDef(ScalarType.createType(PrimitiveType.BITMAP)),
+                false,
+                null, false,
+                DefaultValue.BITMAP_EMPTY_DEFAULT_VALUE, "");
+        definition.setKeysType(KeysType.DUP_KEYS);
+        columns.add(definition);
+        definition = new ColumnDef("col5", new 
TypeDef(ScalarType.createType(PrimitiveType.VARCHAR)),
+                false,
+                null, false,
+                new DefaultValue(true, "xxx\"\"xxx"), "");
+        columns.add(definition);
+        definition = new ColumnDef("col6", new 
TypeDef(ScalarType.createType(PrimitiveType.HLL)),
+                false,
+                AggregateType.HLL_UNION, false,
+                null, "");
+        columns.add(definition);
+        AddColumnsClause clause = new AddColumnsClause(columns, null, null);
+        clause.analyze(analyzer);
+        System.out.println(clause.toString());
+        Assert.assertEquals(
+                "ADD COLUMN (`col1` datetime NOT NULL DEFAULT 
CURRENT_TIMESTAMP COMMENT \"\", "
+                        + "`col2` int NOT NULL DEFAULT \"1\" COMMENT \"\", "
+                        + "`col3` varchar(65533) NOT NULL DEFAULT \"varchar\" 
COMMENT \"\", "
+                        + "`col4` bitmap NOT NULL DEFAULT BITMAP_EMPTY COMMENT 
\"\", "
+                        + "`col5` varchar(65533) NOT NULL DEFAULT 
\"xxx\\\"\\\"xxx\" COMMENT \"\", "
+                        + "`col6` hll HLL_UNION NOT NULL COMMENT \"\")",
+                clause.toString());
+    }
+}


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

Reply via email to