Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](planner) Fix decimal precision and scale wrong when create table like #25802

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,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"""
}