From 2466d08d00775cf1dc94e3e564c567d1efb91e16 Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Mon, 30 Dec 2024 16:35:44 +0800 Subject: [PATCH] [fix](Nereids) fix cast string to date (#46065) ### What problem does this PR solve? Issue Number: close #xxx Related PR: #35637 Problem Summary: When cast("201-01-01" as datetimev2(0)), The result is "2020-01-01" but it is wrong. It should be result in "0201-01-01". 201 would be regarded as 20xy-0z as related pr show, it was a bug. But actually it should not have this trasformation and result in --- .../rules/FoldConstantRuleOnFE.java | 2 +- .../expressions/literal/DateLiteral.java | 24 +++++++++++-------- .../correctness_p0/test_cast_date_decimal.out | 3 +++ .../cast_function/test_cast_function.out | 2 +- .../cast_function/test_cast_function.out | 2 +- .../test_cast_date_decimal.groovy | 4 ++++ 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java index c439458ff4c96e..c2a73f7afec3be 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java @@ -481,7 +481,7 @@ public Expression visitCast(Cast cast, ExpressionRewriteContext context) { return ((DateLikeType) dataType).fromString(((StringLikeLiteral) child).getStringValue()); } catch (AnalysisException t) { if (cast.isExplicitType()) { - return new NullLiteral(dataType); + return cast; } else { // If cast is from type coercion, we don't use NULL literal and will throw exception. throw t; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java index 04300891c45a8d..3bfc4a7dc81436 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java @@ -189,19 +189,23 @@ static Result normalize(String s) { if (len == 4 || len == 2) { sb.append(s, i, j); } else if (len == 3) { - if (partNumber == 0) { - String yy = s.substring(i, i + 2); - int year = Integer.parseInt(yy); - if (year >= 0 && year <= 69) { - sb.append("20"); - } else if (year >= 70 && year <= 99) { - sb.append("19"); + if (s.charAt(j) == '.') { + if (partNumber == 0) { + String yy = s.substring(i, i + 2); + int year = Integer.parseInt(yy); + if (year >= 0 && year <= 69) { + sb.append("20"); + } else if (year >= 70 && year <= 99) { + sb.append("19"); + } + sb.append(yy).append('-'); + } else { + sb.append(s, i, i + 2).append(' '); } - sb.append(yy).append('-'); + j = j - 1; } else { - sb.append(s, i, i + 2).append(' '); + sb.append("0").append(s, i, j); } - j = j - 1; } else if (len == 1) { if (partNumber == 0) { sb.append("000").append(c); diff --git a/regression-test/data/correctness_p0/test_cast_date_decimal.out b/regression-test/data/correctness_p0/test_cast_date_decimal.out index 1738d19a49d58b..1ec0f02f263d6c 100644 --- a/regression-test/data/correctness_p0/test_cast_date_decimal.out +++ b/regression-test/data/correctness_p0/test_cast_date_decimal.out @@ -20,3 +20,6 @@ true -- !sql7 -- \N \N \N \N +-- !sql8 -- +2012-03-12T03:00 0123-01-01T00:00 2012-03-12T12:23:59 + diff --git a/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out b/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out index 6b34e73bd2e82a..c3b73874eaa4e6 100644 --- a/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out +++ b/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out @@ -6,7 +6,7 @@ 11 -- !sql -- -\N +2000-01-01T03:14:17 -- !sql -- \N diff --git a/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out index 8b3214cfa75659..31736a0624b484 100644 --- a/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out +++ b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out @@ -6,7 +6,7 @@ 11 -- !sql -- -\N +2000-01-01T03:14:17 -- !sql -- \N diff --git a/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy b/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy index f533b885a14702..0593116df11494 100644 --- a/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy +++ b/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy @@ -43,4 +43,8 @@ suite("test_cast_date_decimal") { qt_sql7 """ select /*+SET_VAR(debug_skip_fold_constant=true)*/ cast('0000-02-29' as date), cast('0000-02-29' as datetime), cast('00000229' as date), cast('0000-02-29 12:12:12.123' as datetime); """ + + qt_sql8 """ + select cast('123.123' as datetimev2), cast('123-01-01' as datetimev2), cast('123.1212.235959' as datetimev2); + """ }