Skip to content

Commit

Permalink
[fix](Nereids) fix cast string to date (#46065)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
LiBinfeng-01 authored Dec 30, 2024
1 parent b959c66 commit 2466d08
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,23 @@ static Result<String, AnalysisException> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ true
-- !sql7 --
\N \N \N \N

-- !sql8 --
2012-03-12T03:00 0123-01-01T00:00 2012-03-12T12:23:59

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
11

-- !sql --
\N
2000-01-01T03:14:17

-- !sql --
\N
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
11

-- !sql --
\N
2000-01-01T03:14:17

-- !sql --
\N
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
"""
}

0 comments on commit 2466d08

Please sign in to comment.