From 8b451f1de7d236a50ac840fe0a52787f6ee90d13 Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Thu, 26 Dec 2024 17:37:17 +0800 Subject: [PATCH 1/3] [fix](Nereids) fix cast from string to date different by be and fe --- .../rules/FoldConstantRuleOnFE.java | 2 +- .../expressions/literal/DateLiteral.java | 24 +++++++++++-------- .../correctness_p0/test_cast_date_decimal.out | 3 +++ .../test_cast_date_decimal.groovy | 4 ++++ 4 files changed, 22 insertions(+), 11 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 eb8269d68fd0a8..b0e97bf5b53d73 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/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); + """ } From 78829e45659d236c801d0ce3b7c069480dc4d85b Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Fri, 27 Dec 2024 17:17:18 +0800 Subject: [PATCH 2/3] fix p0 --- .../sql_functions/cast_function/test_cast_function.out | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 3dd27fc549f4776a88a0e9262c416e2df12a6d25 Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Fri, 27 Dec 2024 18:57:36 +0800 Subject: [PATCH 3/3] fix p0 --- .../query_p0/sql_functions/cast_function/test_cast_function.out | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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