Skip to content

Commit 1770224

Browse files
authored
[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.
1 parent 4644191 commit 1770224

4 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,16 @@ public static boolean canCastTo(ScalarType type, ScalarType targetType) {
11511151
return PrimitiveType.isImplicitCast(type.getPrimitiveType(), targetType.getPrimitiveType());
11521152
}
11531153

1154+
/**
1155+
* Decimal default precision is 9 and scale is 0, this method return whether this is
1156+
* default decimal v3 or v2
1157+
*/
1158+
public boolean isDefaultDecimal() {
1159+
return (isDecimalV3() || isDecimalV2())
1160+
&& DEFAULT_PRECISION == this.precision
1161+
&& DEFAULT_SCALE == this.scale;
1162+
}
1163+
11541164
@Override
11551165
public TColumnType toColumnTypeThrift() {
11561166
TColumnType thrift = new TColumnType();

‎fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ public String toSql(boolean isUniqueTable, boolean isCompatible) {
735735
int scale = sType.getScalarScale();
736736
int precision = sType.getScalarPrecision();
737737
// not default
738-
if (scale > 0 && precision != 9) {
738+
if (!sType.isDefaultDecimal()) {
739739
sb.append("(").append(precision).append(", ").append(scale)
740740
.append(")");
741741
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !desc_create_table --
3+
decimal_test DUP_KEYS name VARCHAR(*) VARCHAR(*) Yes true \N true
4+
id SMALLINT SMALLINT Yes false \N NONE true
5+
timestamp0 DECIMAL DECIMALV3(9, 0) Yes false \N NONE true
6+
timestamp1 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true
7+
timestamp2 DECIMAL(10, 1) DECIMALV3(10, 1) Yes false \N NONE true
8+
timestamp3 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true
9+
timestamp4 DECIMAL(10, 1) DECIMALV3(10, 1) Yes false \N NONE true
10+
11+
-- !desc_create_table_like --
12+
decimal_test_like DUP_KEYS name VARCHAR(*) VARCHAR(*) Yes true \N true
13+
id SMALLINT SMALLINT Yes false \N NONE true
14+
timestamp0 DECIMAL DECIMALV3(9, 0) Yes false \N NONE true
15+
timestamp1 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true
16+
timestamp2 DECIMAL(10, 1) DECIMALV3(10, 1) Yes false \N NONE true
17+
timestamp3 DECIMAL DECIMALV3(10, 0) Yes false \N NONE true
18+
timestamp4 DECIMAL(10, 1) DECIMALV3(10, 1) Yes false \N NONE true
19+
20+
-- !select_table_like --
21+
test1 1 123456789 1234567891 123456789.0 1234567891 123456789.0
22+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// this suite is for creating table with timestamp datatype in defferent
19+
// case. For example: 'year' and 'Year' datatype should also be valid in definition
20+
21+
22+
suite("test_create_table_like") {
23+
24+
sql """DROP TABLE IF EXISTS decimal_test"""
25+
sql """CREATE TABLE decimal_test
26+
(
27+
`name` varchar COMMENT "1m size",
28+
`id` SMALLINT COMMENT "[-32768, 32767]",
29+
`timestamp0` decimal null comment "c0",
30+
`timestamp1` decimal(10, 0) null comment "c1",
31+
`timestamp2` decimal(10, 1) null comment "c2",
32+
`timestamp3` decimalv3(10, 0) null comment "c3",
33+
`timestamp4` decimalv3(10, 1) null comment "c4",
34+
)
35+
DISTRIBUTED BY HASH(`id`) BUCKETS 1
36+
PROPERTIES ('replication_num' = '1')"""
37+
qt_desc_create_table """desc decimal_test all"""
38+
39+
sql """DROP TABLE IF EXISTS decimal_test_like"""
40+
sql """CREATE TABLE decimal_test_like LIKE decimal_test"""
41+
42+
qt_desc_create_table_like """desc decimal_test_like all"""
43+
44+
45+
sql """INSERT INTO decimal_test_like
46+
(`name`, `id`, `timestamp0`, `timestamp1`, `timestamp2`, `timestamp3`, `timestamp4`)
47+
VALUES ("test1", 1, 123456789, 1234567891, 123456789, 1234567891, 123456789)"""
48+
49+
qt_select_table_like """select * from decimal_test_like"""
50+
}

0 commit comments

Comments
 (0)