[Bug](function) Fix function for cast string as date/datetime (#35637) (#37891)

## Proposed changes

Issue Number: close #35635
cherry-pick https://github.com/apache/doris/pull/35637 from master to
branch-2.1

<!--Describe your changes.-->
Cast rules:Consistent with mysql.
String:Date
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

## Proposed changes

Issue Number: close #xxx

<!--Describe your changes.-->
This commit is contained in:
yongjinhou
2024-07-16 17:22:03 +08:00
committed by GitHub
parent 46c06b3d97
commit 638d4b6f27
3 changed files with 48 additions and 2 deletions

View File

@ -172,6 +172,10 @@ public class DateLiteral extends Literal {
}
// 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)) {
@ -183,6 +187,20 @@ public class DateLiteral extends Literal {
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);