From 919d83c551d39885f20f585a7793557381cf72e7 Mon Sep 17 00:00:00 2001 From: JingDas <114388747+JingDas@users.noreply.github.com> Date: Wed, 1 Nov 2023 16:11:43 +0800 Subject: [PATCH] [fix](planner) Fix decimal precision and scale wrong when create table like (#25802) Use field datatype such as decimal(10, 0) to create table like. Because the scale is 0, the precision and scale will lost when create table like done. this will fix the bug. **Before fix, create table with following SQL**: CREATE TABLE IF NOT EXISTS db_test.table_test ( `name` varchar COMMENT "1m size", `id` SMALLINT COMMENT "[-32768, 32767]", `timestamp0` decimal null comment "c0", `timestamp1` decimal(38, 0) null comment "c1" ) DISTRIBUTED BY HASH(`id`) BUCKETS 1 PROPERTIES ('replication_num' = '1'); **and Then run** CREATE TABLE db_test.table_test_like LIKE db_test.table_test SHOW CREATE TABLE db_test.table_test_like; the field `timestamp1` will be decimal(9, 0), it's wrong. this will fix it. --- .../org/apache/doris/catalog/ScalarType.java | 10 ++++ .../java/org/apache/doris/catalog/Column.java | 2 +- .../data/ddl_p0/test_create_table_like.out | 22 ++++++++ .../ddl_p0/test_create_table_like.groovy | 50 +++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 regression-test/data/ddl_p0/test_create_table_like.out create mode 100644 regression-test/suites/ddl_p0/test_create_table_like.groovy 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 970aa0c2defc88..bd937b4d0c59f1 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 static boolean canCastTo(ScalarType type, ScalarType targetType) { 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 3e741dd08edc00..a85e4ec7d67bb3 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 String toSql(boolean isUniqueTable, boolean isCompatible) { 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 00000000000000..b1bf31fcaa589d --- /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 00000000000000..5ee66fba8b4578 --- /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