Skip to content

Commit

Permalink
[fix](planner) Fix decimal precision and scale wrong when create tabl…
Browse files Browse the repository at this point in the history
…e like (apache#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.
  • Loading branch information
JingDas authored and seawinde committed Nov 12, 2023
1 parent 03799d6 commit 919d83c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(")");
}
Expand Down
22 changes: 22 additions & 0 deletions regression-test/data/ddl_p0/test_create_table_like.out
Original file line number Diff line number Diff line change
@@ -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

50 changes: 50 additions & 0 deletions regression-test/suites/ddl_p0/test_create_table_like.groovy
Original file line number Diff line number Diff line change
@@ -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"""
}

0 comments on commit 919d83c

Please sign in to comment.