branch-2.1: [Fix](function) Fix wrong FE fold constant implementation of function date_format #49032 (#49086)

Cherry-picked from #49032

Co-authored-by: zclllyybb <zhaochangle@selectdb.com>
This commit is contained in:
github-actions[bot]
2025-03-16 20:16:40 +08:00
committed by GitHub
parent be3e2f36d0
commit e7d4dda3c8
3 changed files with 18 additions and 7 deletions

View File

@ -295,14 +295,14 @@ public class DateTimeExtractAndTransform {
@ExecFunction(name = "date_format")
public static Expression dateFormat(DateLiteral date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter(Locale.US).format(
java.time.LocalDate.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()))));
}
@ExecFunction(name = "date_format")
public static Expression dateFormat(DateTimeLiteral date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter(Locale.US).format(
java.time.LocalDateTime.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()),
((int) date.getHour()), ((int) date.getMinute()), ((int) date.getSecond()))));
}
@ -310,14 +310,14 @@ public class DateTimeExtractAndTransform {
@ExecFunction(name = "date_format")
public static Expression dateFormat(DateV2Literal date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter(Locale.US).format(
java.time.LocalDate.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()))));
}
@ExecFunction(name = "date_format")
public static Expression dateFormat(DateTimeV2Literal date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter().format(
return new VarcharLiteral(DateUtils.formatBuilder(format.getValue()).toFormatter(Locale.US).format(
java.time.LocalDateTime.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()),
((int) date.getHour()), ((int) date.getMinute()), ((int) date.getSecond()))));
}

View File

@ -68,7 +68,7 @@ public class DateUtils {
break;
case 'h': // %h Hour (01..12)
case 'I': // %I Hour (01..12)
builder.appendValue(ChronoField.HOUR_OF_AMPM, 2);
builder.appendValue(ChronoField.CLOCK_HOUR_OF_AMPM, 2);
break;
case 'i': // %i Minutes, numeric (00..59)
builder.appendValue(ChronoField.MINUTE_OF_HOUR, 2);
@ -80,7 +80,7 @@ public class DateUtils {
builder.appendValue(ChronoField.HOUR_OF_DAY);
break;
case 'l': // %l Hour (1..12)
builder.appendValue(ChronoField.HOUR_OF_AMPM);
builder.appendValue(ChronoField.CLOCK_HOUR_OF_AMPM);
break;
case 'M': // %M Month name (January..December)
builder.appendText(ChronoField.MONTH_OF_YEAR, TextStyle.FULL);
@ -92,8 +92,10 @@ public class DateUtils {
builder.appendText(ChronoField.AMPM_OF_DAY);
break;
case 'r': // %r Time, 12-hour (hh:mm:ss followed by AM or PM)
builder.appendValue(ChronoField.HOUR_OF_AMPM, 2)
builder.appendValue(ChronoField.CLOCK_HOUR_OF_AMPM, 2)
.appendLiteral(':')
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.appendLiteral(' ')
.appendText(ChronoField.AMPM_OF_DAY, TextStyle.FULL)