This is an automated email from the ASF dual-hosted git repository.

englefly pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 2466d08d007 [fix](Nereids) fix cast string to date (#46065)
2466d08d007 is described below

commit 2466d08d00775cf1dc94e3e564c567d1efb91e16
Author: LiBinfeng <libinf...@selectdb.com>
AuthorDate: Mon Dec 30 16:35:44 2024 +0800

    [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
---
 .../expression/rules/FoldConstantRuleOnFE.java     |  2 +-
 .../trees/expressions/literal/DateLiteral.java     | 24 +++++++++++++---------
 .../data/correctness_p0/test_cast_date_decimal.out |  3 +++
 .../cast_function/test_cast_function.out           |  2 +-
 .../cast_function/test_cast_function.out           |  2 +-
 .../correctness_p0/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 c439458ff4c..c2a73f7afec 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 class FoldConstantRuleOnFE extends 
AbstractExpressionRewriteRule
                 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 04300891c45..3bfc4a7dc81 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 @@ public class DateLiteral extends Literal {
                 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 1738d19a49d..1ec0f02f263 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 6b34e73bd2e..c3b73874eaa 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 8b3214cfa75..31736a0624b 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 f533b885a14..0593116df11 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);
+    """
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to