From e7d4dda3c8420bcc0ce751760e65f87c9425b3fd Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 16 Mar 2025 20:16:40 +0800 Subject: [PATCH] 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 --- .../executable/DateTimeExtractAndTransform.java | 8 ++++---- .../java/org/apache/doris/nereids/util/DateUtils.java | 8 +++++--- .../fold_constant/fold_constant_date_arithmatic.groovy | 9 +++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java index c908271531..bd1915c2d8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java @@ -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())))); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java index 354d78d027..c0563de5d1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java @@ -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) diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy index 5e0ed2e5d2..e19a01b2a3 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy @@ -29,4 +29,13 @@ suite("fold_constant_date_arithmatic") { testFoldConst("select substr(current_date(), 1, 10);") testFoldConst("select substr(current_timestamp(), 1, 10);") testFoldConst("select substr(current_timestamp(3), 1, 10);") + + testFoldConst("SELECT date_format('2020-12-01 00:00:30.01', '%h');") + testFoldConst("SELECT date_format('2020-12-01 00:00:30.01', '%I');") + testFoldConst("SELECT date_format('2020-12-01 00:00:30.01', '%l');") + testFoldConst("SELECT date_format('2020-12-01 00:00:30.01', '%r');") + testFoldConst("SELECT date_format('2020-12-01 12:00:30.01', '%h');") + testFoldConst("SELECT date_format('2020-12-01 12:00:30.01', '%I');") + testFoldConst("SELECT date_format('2020-12-01 12:00:30.01', '%l');") + testFoldConst("SELECT date_format('2020-12-01 12:00:30.01', '%r');") }