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 93bcb92f58062a4..54053fb17f7cc8d 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 @@ -179,17 +179,6 @@ public String getValue() { } 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 @@ -680,9 +669,23 @@ public String toSql() { } if (defaultValue.isSet) { - sb.append(defaultValue.toSql()).append(" "); + sb.append("DEFAULT "); + if (defaultValue.value != null) { + if (typeDef.getType().getPrimitiveType() != PrimitiveType.BITMAP + && typeDef.getType().getPrimitiveType() != PrimitiveType.HLL) { + if (defaultValue.defaultValueExprDef != null) { + sb.append(defaultValue.value); + } else { + sb.append("\"").append(defaultValue.value).append("\""); + } + } else if (typeDef.getType().getPrimitiveType() == PrimitiveType.BITMAP) { + sb.append(defaultValue.defaultValueExprDef.getExprName()); + } + } else { + sb.append("NULL"); + } } - sb.append("COMMENT \"").append(comment).append("\""); + sb.append(" COMMENT \"").append(comment).append("\""); return sb.toString(); } 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 000000000000000..df5a8c33f9dddb8 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/AddColumnDefaultValueTest.java @@ -0,0 +1,76 @@ +// 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.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 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); + 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 \"\")", + clause.toString()); + } +}