Skip to content

Commit

Permalink
[Fix](planner) fix bug of char(255) toSql (#37340) (#37671)
Browse files Browse the repository at this point in the history
cherry-pick #37340 from master
  • Loading branch information
feiniaofeiafei authored Jul 12, 2024
1 parent 4dc933b commit 6214d64
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public static ScalarType createHllType() {
public String toString() {
if (type == PrimitiveType.CHAR) {
if (isWildcardChar()) {
return "CHARACTER";
return "CHARACTER(" + MAX_CHAR_LENGTH + ")";
}
return "CHAR(" + len + ")";
} else if (type == PrimitiveType.DECIMALV2) {
Expand Down Expand Up @@ -617,7 +617,7 @@ public String toSql(int depth) {
switch (type) {
case CHAR:
if (isWildcardChar()) {
stringBuilder.append("CHARACTER");
stringBuilder.append("CHARACTER").append("(").append(MAX_CHAR_LENGTH).append(")");
} else if (Strings.isNullOrEmpty(lenStr)) {
stringBuilder.append("CHAR").append("(").append(len).append(")");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public void testCreateGlobalFunction() throws Exception {

queryStr = "select to_char(k1, 4) from db2.tbl1;";
Assert.assertTrue(containsIgnoreCase(dorisAssert.query(queryStr).explainQuery(),
"CAST(`k1` AS CHARACTER)"));
"CAST(`k1` AS CHARACTER(255))"));
}

private void testFunctionQuery(ConnectContext ctx, String queryStr, Boolean isStringLiteral) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@
6 \N 6
6 7 1

-- !test_char_255 --
0

-- !select --
123 abcdddddd

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mv_tbl_scalar_types_dup_1 DUP_KEYS mv_c_tinyint TINYINT TINYINT Yes true \N tru
mv_c_datetime DATETIME DATETIMEV2(0) Yes false \N NONE true `c_datetime`
mv_c_datev2 DATE DATEV2 Yes false \N NONE true `c_datev2`
mv_c_datetimev2 DATETIME DATETIMEV2(0) Yes false \N NONE true `c_datetimev2`
mv_c_char CHARACTER CHARACTER Yes false \N NONE true `c_char`
mv_c_char CHARACTER(255) CHARACTER(255) Yes false \N NONE true `c_char`
mv_c_varchar VARCHAR(65533) VARCHAR(65533) Yes false \N NONE true `c_varchar`
mv_c_string TEXT TEXT Yes false \N NONE true `c_string`

Expand Down Expand Up @@ -97,7 +97,7 @@ mv_tbl_scalar_types_dup_1 DUP_KEYS mv_c_tinyint TINYINT TINYINT Yes true \N tru
mv_c_datetime DATETIME DATETIMEV2(0) Yes false \N NONE true `c_datetime`
mv_c_datev2 DATE DATEV2 Yes false \N NONE true `c_datev2`
mv_c_datetimev2 DATETIME DATETIMEV2(0) Yes false \N NONE true `c_datetimev2`
mv_c_char CHARACTER CHARACTER Yes false \N NONE true `c_char`
mv_c_char CHARACTER(255) CHARACTER(255) Yes false \N NONE true `c_char`
mv_c_varchar VARCHAR(65533) VARCHAR(65533) Yes false \N NONE true `c_varchar`
mv_c_string TEXT TEXT Yes false \N NONE true `c_string`

30 changes: 30 additions & 0 deletions regression-test/plugins/plugin_must_contains.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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.

import org.apache.doris.regression.suite.Suite

Suite.metaClass.mustContain = {String str1, String str2 ->
try {
assert str1.contains(str2)
logger.info("Assertion passed: '${str1}' contains '${str2}'")
} catch (AssertionError e) {
logger.error("Assertion failed: '${str1}' does not contain '${str2}'")
throw e
}
return true
}
logger.info("Added 'mustContain' function to Suite")
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,28 @@ suite("test_create_table_like_nereids") {
sql "drop table if exists table_like_with_partial_roll_up_exists"
sql """CREATE TABLE if not exists table_like_with_partial_roll_up_exists
LIKE mal_test_create_table_like with rollup (ru1);"""

sql "drop table if exists test_create_table_like_char_255"
sql """
CREATE TABLE test_create_table_like_char_255
(
`id` INT NOT NULL,
`name` CHAR(255)
)
UNIQUE KEY(`id`)
DISTRIBUTED BY HASH(`id`) BUCKETS AUTO
PROPERTIES (
"replication_num" = "1",
"light_schema_change" = "true"
);
"""
sql "drop table if exists new_char_255"
qt_test_char_255 """
create table new_char_255 like test_create_table_like_char_255;
"""
def res1 = sql "show create table new_char_255"
mustContain(res1[0][1], "CHARACTER(255)")

sql "insert into new_char_255 values(123,'abcdddddd')"
qt_select "select * from new_char_255"
}

0 comments on commit 6214d64

Please sign in to comment.