diff --git a/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java b/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java index 970aa0c2de..bd937b4d0c 100644 --- a/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java +++ b/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java @@ -1151,6 +1151,16 @@ public class ScalarType extends Type { return PrimitiveType.isImplicitCast(type.getPrimitiveType(), targetType.getPrimitiveType()); } + /** + * Decimal default precision is 9 and scale is 0, this method return whether this is + * default decimal v3 or v2 + */ + public boolean isDefaultDecimal() { + return (isDecimalV3() || isDecimalV2()) + && DEFAULT_PRECISION == this.precision + && DEFAULT_SCALE == this.scale; + } + @Override public TColumnType toColumnTypeThrift() { TColumnType thrift = new TColumnType(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java index 3e741dd08e..a85e4ec7d6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java @@ -735,7 +735,7 @@ public class Column implements Writable, GsonPostProcessable { int scale = sType.getScalarScale(); int precision = sType.getScalarPrecision(); // not default - if (scale > 0 && precision != 9) { + if (!sType.isDefaultDecimal()) { sb.append("(").append(precision).append(", ").append(scale) .append(")"); } diff --git a/regression-test/data/ddl_p0/test_create_table_like.out b/regression-test/data/ddl_p0/test_create_table_like.out new file mode 100644 index 0000000000..b1bf31fcaa --- /dev/null +++ b/regression-test/data/ddl_p0/test_create_table_like.out @@ -0,0 +1,22 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !desc_create_table -- +decimal_test DUP_KEYS name VARCHAR(*) VARCHAR(*) Yes true \N true + id SMALLINT SMALLINT Yes false \N NONE true + timestamp0 DECIMAL DECIMALV3(9, 0) Yes false \N NONE true + timestamp1 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true + timestamp2 DECIMAL(10, 1) DECIMALV3(10, 1) Yes false \N NONE true + timestamp3 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true + timestamp4 DECIMAL(10, 1) DECIMALV3(10, 1) Yes false \N NONE true + +-- !desc_create_table_like -- +decimal_test_like DUP_KEYS name VARCHAR(*) VARCHAR(*) Yes true \N true + id SMALLINT SMALLINT Yes false \N NONE true + timestamp0 DECIMAL DECIMALV3(9, 0) Yes false \N NONE true + timestamp1 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true + timestamp2 DECIMAL(10, 1) DECIMALV3(10, 1) Yes false \N NONE true + timestamp3 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true + timestamp4 DECIMAL(10, 1) DECIMALV3(10, 1) Yes false \N NONE true + +-- !select_table_like -- +test1 1 123456789 1234567891 123456789.0 1234567891 123456789.0 + diff --git a/regression-test/suites/ddl_p0/test_create_table_like.groovy b/regression-test/suites/ddl_p0/test_create_table_like.groovy new file mode 100644 index 0000000000..5ee66fba8b --- /dev/null +++ b/regression-test/suites/ddl_p0/test_create_table_like.groovy @@ -0,0 +1,50 @@ +// 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. + +// this suite is for creating table with timestamp datatype in defferent +// case. For example: 'year' and 'Year' datatype should also be valid in definition + + +suite("test_create_table_like") { + + sql """DROP TABLE IF EXISTS decimal_test""" + sql """CREATE TABLE decimal_test + ( + `name` varchar COMMENT "1m size", + `id` SMALLINT COMMENT "[-32768, 32767]", + `timestamp0` decimal null comment "c0", + `timestamp1` decimal(10, 0) null comment "c1", + `timestamp2` decimal(10, 1) null comment "c2", + `timestamp3` decimalv3(10, 0) null comment "c3", + `timestamp4` decimalv3(10, 1) null comment "c4", + ) + DISTRIBUTED BY HASH(`id`) BUCKETS 1 + PROPERTIES ('replication_num' = '1')""" + qt_desc_create_table """desc decimal_test all""" + + sql """DROP TABLE IF EXISTS decimal_test_like""" + sql """CREATE TABLE decimal_test_like LIKE decimal_test""" + + qt_desc_create_table_like """desc decimal_test_like all""" + + + sql """INSERT INTO decimal_test_like + (`name`, `id`, `timestamp0`, `timestamp1`, `timestamp2`, `timestamp3`, `timestamp4`) + VALUES ("test1", 1, 123456789, 1234567891, 123456789, 1234567891, 123456789)""" + + qt_select_table_like """select * from decimal_test_like""" +} \ No newline at end of file