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

morrysnow pushed a commit to branch branch-3.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new 5a27ebfc6b2 branch-3.0: [fix](Nereids) fix cast string to date #46065 
(#46941)
5a27ebfc6b2 is described below

commit 5a27ebfc6b27da9858ae1662a6698df0ee2a7ce2
Author: LiBinfeng <libinf...@selectdb.com>
AuthorDate: Tue Jan 14 13:57:49 2025 +0800

    branch-3.0: [fix](Nereids) fix cast string to date #46065 (#46941)
    
    pick from master  #46065
---
 .../expression/rules/FoldConstantRuleOnFE.java     |  2 +-
 .../trees/expressions/literal/DateLiteral.java     | 24 ++++---
 .../rules/SimplifyComparisonPredicateSqlTest.java  | 74 ----------------------
 .../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 ++
 7 files changed, 24 insertions(+), 87 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 1e01e544a25..888a757d107 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
@@ -472,7 +472,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 ed99e3025e8..d0364991c1e 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
@@ -188,19 +188,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/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateSqlTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateSqlTest.java
index 29889efdd6c..055eefd3c39 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateSqlTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateSqlTest.java
@@ -17,14 +17,10 @@
 
 package org.apache.doris.nereids.rules.expression.rules;
 
-import org.apache.doris.nereids.exceptions.AnalysisException;
-import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
-import org.apache.doris.nereids.types.DateTimeV2Type;
 import org.apache.doris.nereids.util.MemoPatternMatchSupported;
 import org.apache.doris.nereids.util.PlanChecker;
 import org.apache.doris.utframe.TestWithFeService;
 
-import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 class SimplifyComparisonPredicateSqlTest extends TestWithFeService implements 
MemoPatternMatchSupported {
@@ -96,74 +92,4 @@ class SimplifyComparisonPredicateSqlTest extends 
TestWithFeService implements Me
                                 .when(f -> 
f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(b >= 111.12)")))
                 );
     }
-
-    @Test
-    void dateLikeOverflow() {
-        PlanChecker.from(connectContext)
-                .analyze("select CAST('2021-01-32 00:00:00' AS DATETIME(6))")
-                .rewrite()
-                .matches(
-                        logicalResultSink(
-                                logicalOneRowRelation().when(p -> 
p.getProjects().get(0).child(0).equals(new NullLiteral(DateTimeV2Type.of(6))))
-                        )
-                );
-
-        PlanChecker.from(connectContext)
-                .analyze("select CONVERT('2021-01-32 00:00:00', DATETIME(6))")
-                .rewrite()
-                .matches(
-                        logicalResultSink(
-                                logicalOneRowRelation().when(p -> 
p.getProjects().get(0).child(0).equals(new NullLiteral(DateTimeV2Type.of(6))))
-                        )
-                );
-
-        PlanChecker.from(connectContext)
-                .analyze("select CONVERT_TZ('2021-01-32 00:00:00', '+08:00', 
'America/London') = '2021-01-30'")
-                .rewrite()
-                .matches(
-                        logicalResultSink(
-                                logicalOneRowRelation().when(p -> 
p.getProjects().get(0).child(0) instanceof NullLiteral)
-                        )
-                );
-
-        PlanChecker.from(connectContext)
-                .analyze("select CONVERT_TZ('2021-01-32 00:00:00', '+08:00', 
'America/London')")
-                .rewrite()
-                .matches(
-                        logicalResultSink(
-                                logicalOneRowRelation().when(p -> 
p.getProjects().get(0).child(0) instanceof NullLiteral)
-                        )
-                );
-
-        PlanChecker.from(connectContext)
-                .analyze("select CONVERT_TZ('2021-01-32 00:00:00.0000001', 
'+08:00', 'America/London')")
-                .rewrite()
-                .matches(
-                        logicalResultSink(
-                                logicalOneRowRelation().when(p -> 
p.getProjects().get(0).child(0) instanceof NullLiteral)
-                        )
-                );
-
-        PlanChecker.from(connectContext)
-                .analyze("select CONVERT_TZ('2021-01-32 00:00:00.001', 
'+08:00', 'America/London') = '2021-01-30'")
-                .rewrite()
-                .matches(
-                        logicalResultSink(
-                                logicalOneRowRelation().when(p -> 
p.getProjects().get(0).child(0) instanceof NullLiteral)
-                        )
-                );
-
-        Assertions.assertThrows(AnalysisException.class, () -> 
PlanChecker.from(connectContext)
-                .analyze("select CAST('2021-01-32 00:00:00' AS DATETIME(6)) = 
'2021-01-32 00:00:00'")
-                .rewrite()
-        );
-        Assertions.assertThrows(AnalysisException.class, () -> 
PlanChecker.from(connectContext)
-                .analyze("select CAST('2021-01-32 00:00:00' AS DATETIME(6)) = 
'2021-01-32 23:00:00'")
-                .rewrite()
-        );
-        Assertions.assertThrows(AnalysisException.class, () -> 
PlanChecker.from(connectContext)
-                .analyze("select CAST('2021-01-32 00:00:00' AS DATETIME(6)) = 
'1000'")
-                .rewrite()
-        );
-    }
 }
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