This is an automated email from the ASF dual-hosted git repository.
morrysnow pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.1 by this push:
new 4f41bad2834 branch-2.1: [fix](Nereids) fix cast string to date #46065
(#46526)
4f41bad2834 is described below
commit 4f41bad28343db332647e4f4286cd92d75f8467a
Author: LiBinfeng <[email protected]>
AuthorDate: Thu Jan 9 17:03:36 2025 +0800
branch-2.1: [fix](Nereids) fix cast string to date #46065 (#46526)
pick: #46065
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 ++++---
.../rules/SimplifyComparisonPredicateSqlTest.java | 74 ----------------------
.../data/correctness_p0/test_cast_date_decimal.out | 9 +++
.../cast_function/test_cast_function.out | 2 +-
.../cast_function/test_cast_function.out | 2 +-
.../correctness_p0/test_cast_date_decimal.groovy | 12 ++++
7 files changed, 38 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 d67c5c4ab61..cb4bdc07e98 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
@@ -456,7 +456,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 e61ffca6be0..74e99160ad6 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 91b9ceb8ebc..1ec0f02f263 100644
--- a/regression-test/data/correctness_p0/test_cast_date_decimal.out
+++ b/regression-test/data/correctness_p0/test_cast_date_decimal.out
@@ -14,3 +14,12 @@ true
-- !sql5 --
2012-03-12
+-- !sql6 --
+\N \N \N \N
+
+-- !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 03a970d2a99..0593116df11 100644
--- a/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy
+++ b/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy
@@ -35,4 +35,16 @@ suite("test_cast_date_decimal") {
qt_sql5 """
select cast('123.123' as date);
"""
+
+ qt_sql6 """
+ select 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_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: [email protected]
For additional commands, e-mail: [email protected]