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 93127933ea7160..04a0f0120e278f 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 @@ -183,6 +183,10 @@ static String normalize(String s) { } // normalize leading 0 for date and time + // The first part is 1-digit x: 000x-month-day + // The first part is 2-digit xy: 20xy-month-day / 19xy-month-day + // The first part is 3-digit xyz: 20xy-0z-day / 19xy-0z-day + // The first part is 4-digit xyzw: xyzw-month-day while (i < s.length() && partNumber < 6) { char c = s.charAt(i); if (Character.isDigit(c)) { @@ -194,6 +198,20 @@ static String normalize(String s) { int len = j - i; 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"); + } + sb.append(yy).append('-'); + } else { + sb.append(s, i, i + 2).append(' '); + } + 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 5b10e282f28038..91b9ceb8ebc503 100644 --- a/regression-test/data/correctness_p0/test_cast_date_decimal.out +++ b/regression-test/data/correctness_p0/test_cast_date_decimal.out @@ -1,4 +1,16 @@ -- This file is automatically generated. You should know what you did if you want to edit this --- !sql -- +-- !sql1 -- true +-- !sql2 -- +2024-12-12 + +-- !sql3 -- +2024-12-12 + +-- !sql4 -- +2024-12-12 + +-- !sql5 -- +2012-03-12 + 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 0272c0be822108..03a970d2a99063 100644 --- a/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy +++ b/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy @@ -16,7 +16,23 @@ // under the License. suite("test_cast_date_decimal") { - qt_sql """ + qt_sql1 """ select cast('2020-02-02' as date ) between cast('2020-02-02' as date ) and cast('2020-02-02' as date ) + 1.0; """ + + qt_sql2 """ + select cast('2024-12-12' as date); + """ + + qt_sql3 """ + select cast('2024.12.12' as date); + """ + + qt_sql4 """ + select cast('24.12.12' as date); + """ + + qt_sql5 """ + select cast('123.123' as date); + """ }