Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
wyxxxcat committed Feb 10, 2025
1 parent 612a288 commit 113c6a6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
19 changes: 10 additions & 9 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public DefaultValue(boolean isSet, String value, String exprName, Long precision
public static String NOW = "now";
public static String HLL_EMPTY = "HLL_EMPTY";
public static String BITMAP_EMPTY = "BITMAP_EMPTY";
public static DefaultValue CURRENT_TIMESTAMP_DEFAULT_VALUE = new DefaultValue(true, CURRENT_TIMESTAMP, NOW);
public static DefaultValue CURRENT_TIMESTAMP_DEFAULT_VALUE = new DefaultValue(true, NOW, CURRENT_TIMESTAMP);
// no default value
public static DefaultValue NOT_SET = new DefaultValue(false, null);
// default null
Expand All @@ -127,21 +127,21 @@ public static DefaultValue currentTimeStampDefaultValueWithPrecision(Long precis
+ " precision must be between 0 and 6");
}
if (precision == 0) {
return new DefaultValue(true, CURRENT_TIMESTAMP, NOW);
return new DefaultValue(true, NOW, CURRENT_TIMESTAMP);
}
String value = CURRENT_TIMESTAMP + "(" + precision + ")";
String exprName = NOW;
return new DefaultValue(true, value, exprName, precision);
return new DefaultValue(true, exprName, value, precision);
}

public boolean isCurrentTimeStamp() {
return "CURRENT_TIMESTAMP".equals(value) && defaultValueExprDef != null
&& NOW.equals(defaultValueExprDef.getExprName());
return "NOW".equals(value) && defaultValueExprDef != null
&& CURRENT_TIMESTAMP.equals(defaultValueExprDef.getExprName());
}

public boolean isCurrentTimeStampWithPrecision() {
return defaultValueExprDef != null && value.startsWith(CURRENT_TIMESTAMP + "(")
&& NOW.equals(defaultValueExprDef.getExprName());
&& CURRENT_TIMESTAMP.equals(defaultValueExprDef.getExprName());
}

public long getCurrentTimeStampPrecision() {
Expand Down Expand Up @@ -524,7 +524,8 @@ public static void validateDefaultValue(Type type, String defaultValue, DefaultV
// if not check it first, some literal constructor will throw AnalysisException,
// and it is not intuitive to users.
PrimitiveType primitiveType = scalarType.getPrimitiveType();
if (null != defaultValueExprDef && defaultValueExprDef.getExprName().equalsIgnoreCase("now")) {
if (null != defaultValueExprDef
&& defaultValueExprDef.getExprName().equalsIgnoreCase(DefaultValue.CURRENT_TIMESTAMP)) {
switch (primitiveType) {
case DATETIME:
case DATETIMEV2:
Expand Down Expand Up @@ -607,7 +608,7 @@ public static void validateDefaultValue(Type type, String defaultValue, DefaultV
if (defaultValueExprDef == null) {
new DateLiteral(defaultValue, scalarType);
} else {
if (defaultValueExprDef.getExprName().equals(DefaultValue.NOW)) {
if (defaultValueExprDef.getExprName().equals(DefaultValue.CURRENT_TIMESTAMP)) {
if (defaultValueExprDef.getPrecision() != null) {
Long defaultValuePrecision = defaultValueExprDef.getPrecision();
String typeStr = scalarType.toString();
Expand Down Expand Up @@ -681,7 +682,7 @@ public String toSql() {
if (typeDef.getType().getPrimitiveType() != PrimitiveType.BITMAP
&& typeDef.getType().getPrimitiveType() != PrimitiveType.HLL) {
if (defaultValue.defaultValueExprDef != null) {
sb.append("DEFAULT ").append(defaultValue.value).append(" ");
sb.append("DEFAULT ").append(defaultValue.defaultValueExprDef.getExprName()).append(" ");
} else {
sb.append("DEFAULT ").append("\"").append(SqlUtils.escapeQuota(defaultValue.value)).append("\"")
.append(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ public String toSql(boolean isUniqueTable, boolean isCompatible) {
}
if (defaultValue != null && getDataType() != PrimitiveType.HLL && getDataType() != PrimitiveType.BITMAP) {
if (defaultValueExprDef != null) {
sb.append(" DEFAULT ").append(defaultValue).append("");
sb.append(" DEFAULT ").append(defaultValueExprDef.getExprName()).append("");
} else {
sb.append(" DEFAULT \"").append(defaultValue).append("\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class DefaultValue {
public static String NOW = "now";
public static String HLL_EMPTY = "HLL_EMPTY";
public static String BITMAP_EMPTY = "BITMAP_EMPTY";
public static DefaultValue CURRENT_DATE_DEFAULT_VALUE = new DefaultValue(CURRENT_DATE, CURRENT_DATE.toLowerCase());
public static DefaultValue CURRENT_TIMESTAMP_DEFAULT_VALUE = new DefaultValue(CURRENT_TIMESTAMP, NOW);
public static DefaultValue CURRENT_DATE_DEFAULT_VALUE = new DefaultValue(CURRENT_DATE.toLowerCase(), CURRENT_DATE);
public static DefaultValue CURRENT_TIMESTAMP_DEFAULT_VALUE = new DefaultValue(NOW, CURRENT_TIMESTAMP);
// default null
public static DefaultValue NULL_DEFAULT_VALUE = new DefaultValue(null);
public static String ZERO = new String(new byte[] {0});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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.nereids.trees.plans.commands;

import org.apache.doris.nereids.parser.NereidsParser;
import org.apache.doris.nereids.trees.plans.commands.info.TableNameInfo;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.qe.ShowResultSet;
import org.apache.doris.utframe.TestWithFeService;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;

public class ShowCreateTableCommandTest extends TestWithFeService {
@Override
protected void runBeforeAll() throws Exception {
createDatabase("test");
connectContext.setDatabase("test");
}

@Override
public void createTable(String sql) throws Exception {
LogicalPlan plan = new NereidsParser().parseSingle(sql);
Assertions.assertTrue(plan instanceof CreateTableCommand);
((CreateTableCommand) plan).run(connectContext, null);
}

@Test
public void testCreateTableDefaultValueMath() throws Exception {
// test
String sql = "create table if not exists test.tbl\n" + "(k1 int, k2 double default E, k3 double default PI)\n"
+ "distributed by hash(k1) buckets 1\n"
+ "properties('replication_num' = '1'); ";
createTable(sql);
ShowCreateTableCommand showCreateTableCommand = new ShowCreateTableCommand(new TableNameInfo("test", "tbl"),
false);
ShowResultSet result = showCreateTableCommand.doRun(connectContext, null);
String resultStr = result.getResultRows().stream().flatMap(List::stream).collect(Collectors.joining(" "));
Assertions.assertTrue(resultStr.contains("`k2` double NULL DEFAULT E"));
Assertions.assertTrue(resultStr.contains("`k3` double NULL DEFAULT PI"));
}
}

0 comments on commit 113c6a6

Please sign in to comment.