This is an automated email from the ASF dual-hosted git repository.

kxiao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 9cf33427c29 [fix](function) fix date_format function execution error 
on fe #31645 (#31921)
9cf33427c29 is described below

commit 9cf33427c29bf6c4870ce3a88f510fa98f0b4eb3
Author: lw112 <[email protected]>
AuthorDate: Thu Mar 7 20:51:45 2024 +0800

    [fix](function) fix date_format function execution error on fe #31645 
(#31921)
---
 .../src/main/java/org/apache/doris/analysis/DateLiteral.java      | 8 +++++++-
 .../src/main/java/org/apache/doris/nereids/util/DateUtils.java    | 8 +++++++-
 .../apache/doris/nereids/rules/expression/FoldConstantTest.java   | 2 +-
 .../src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java   | 2 +-
 .../sql_functions/datetime_functions/test_date_function.out       | 6 ++++++
 .../sql_functions/datetime_functions/test_date_function.out       | 6 ++++++
 .../sql_functions/datetime_functions/test_date_function.groovy    | 2 ++
 .../sql_functions/datetime_functions/test_date_function.groovy    | 2 ++
 8 files changed, 32 insertions(+), 4 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
index 2816762f943..36691a1e1e3 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/DateLiteral.java
@@ -44,6 +44,7 @@ import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.sql.Timestamp;
+import java.time.DayOfWeek;
 import java.time.LocalDateTime;
 import java.time.Year;
 import java.time.ZoneId;
@@ -52,10 +53,12 @@ import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeFormatterBuilder;
 import java.time.format.DateTimeParseException;
 import java.time.format.ResolverStyle;
+import java.time.format.SignStyle;
 import java.time.format.TextStyle;
 import java.time.temporal.ChronoField;
 import java.time.temporal.IsoFields;
 import java.time.temporal.TemporalAccessor;
+import java.time.temporal.WeekFields;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -109,6 +112,7 @@ public class DateLiteral extends LiteralExpr {
     private static final int ALLOW_SPACE_MASK = 4 | 64;
     private static final int MAX_DATE_PARTS = 8;
     private static final int YY_PART_YEAR = 70;
+    private static final WeekFields weekFields = 
WeekFields.of(DayOfWeek.SUNDAY, 7);
 
     static {
         try {
@@ -1019,7 +1023,7 @@ public class DateLiteral extends LiteralExpr {
                         builder.appendPattern("HH:mm:ss");
                         break;
                     case 'V': // %V Week (01..53), where Sunday is the first 
day of the week; used with %X
-                        builder.appendValue(ChronoField.ALIGNED_WEEK_OF_YEAR, 
2);
+                        builder.appendValue(weekFields.weekOfWeekBasedYear(), 
2);
                         break;
                     case 'v': // %v Week (01..53), where Monday is the first 
day of the week; used with %x
                         builder.appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 
2);
@@ -1031,6 +1035,8 @@ public class DateLiteral extends LiteralExpr {
                         builder.appendValue(IsoFields.WEEK_BASED_YEAR, 4);
                         break;
                     case 'X':
+                        builder.appendValue(weekFields.weekBasedYear(), 4, 10, 
SignStyle.EXCEEDS_PAD);
+                        break;
                     case 'Y': // %Y Year, numeric, four digits
                         // %X Year for the week, where Sunday is the first day 
of the week,
                         // numeric, four digits; used with %v
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 bd09bfa3429..70f25b25fc5 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
@@ -20,19 +20,23 @@ package org.apache.doris.nereids.util;
 import org.apache.doris.nereids.exceptions.AnalysisException;
 import org.apache.doris.qe.ConnectContext;
 
+import java.time.DayOfWeek;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeFormatterBuilder;
+import java.time.format.SignStyle;
 import java.time.format.TextStyle;
 import java.time.temporal.ChronoField;
 import java.time.temporal.IsoFields;
 import java.time.temporal.TemporalAccessor;
+import java.time.temporal.WeekFields;
 
 /**
  * date util tools.
  */
 public class DateUtils {
+    private static final WeekFields weekFields = 
WeekFields.of(DayOfWeek.SUNDAY, 7);
 
     /**
      * format builder.
@@ -103,7 +107,7 @@ public class DateUtils {
                         builder.appendPattern("HH:mm:ss");
                         break;
                     case 'V': // %V Week (01..53), where Sunday is the first 
day of the week; used with %X
-                        builder.appendValue(ChronoField.ALIGNED_WEEK_OF_YEAR, 
2);
+                        builder.appendValue(weekFields.weekOfWeekBasedYear(), 
2);
                         break;
                     case 'v': // %v Week (01..53), where Monday is the first 
day of the week; used with %x
                         builder.appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 
2);
@@ -115,6 +119,8 @@ public class DateUtils {
                         builder.appendValue(IsoFields.WEEK_BASED_YEAR, 4);
                         break;
                     case 'X':
+                        builder.appendValue(weekFields.weekBasedYear(), 4, 10, 
SignStyle.EXCEEDS_PAD);
+                        break;
                     case 'Y': // %Y Year, numeric, four digits
                         // %X Year for the week, where Sunday is the first day 
of the week,
                         // numeric, four digits; used with %v
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java
index f7a3f8deb8b..384bfcecead 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java
@@ -491,7 +491,7 @@ public class FoldConstantTest extends 
ExpressionRewriteTestHelper {
         
Assertions.assertEquals(DateTimeExtractAndTransform.date(dateLiteral).toSql(), 
answer[answerIdx++]);
         
Assertions.assertEquals(DateTimeExtractAndTransform.dateV2(dateLiteral).toSql(),
 answer[answerIdx]);
 
-        Assertions.assertEquals("'2021 52 2022 01'", 
DateTimeExtractAndTransform.dateFormat(
+        Assertions.assertEquals("'2021 52 2021 52'", 
DateTimeExtractAndTransform.dateFormat(
                 new DateTimeLiteral("2022-01-01 00:12:42"),
                 new VarcharLiteral("%x %v %X %V")).toSql());
         Assertions.assertEquals("'2023 18 2023 19'", 
DateTimeExtractAndTransform.dateFormat(
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
index 727a6635620..b405d787219 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
@@ -386,7 +386,7 @@ public class FEFunctionsTest {
                     new StringLiteral("2013-05-17 08:07:05 PM"), new 
StringLiteral("%Y-%m-%d %r")).getStringValue());
             Assert.assertEquals("2013-05-17 08:07:05", 
FEFunctions.dateParse(new StringLiteral("2013-05-17 08:07:05"),
                     new StringLiteral("%Y-%m-%d %T")).getStringValue());
-            Assert.assertEquals("2021 52 2022 01", FEFunctions.dateFormat(new 
DateLiteral("2022-01-01 00:12:42", Type.DATETIMEV2),
+            Assert.assertEquals("2021 52 2021 52", FEFunctions.dateFormat(new 
DateLiteral("2022-01-01 00:12:42", Type.DATETIMEV2),
                     new StringLiteral("%x %v %X %V")).getStringValue());
             Assert.assertEquals("2023 18 2023 19", FEFunctions.dateFormat(new 
DateLiteral("2023-05-07 02:41:42", Type.DATETIMEV2),
                     new StringLiteral("%x %v %X %V")).getStringValue());
diff --git 
a/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out
 
b/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out
index ad3b6bd5893..bf52d2a57c8 100644
--- 
a/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out
+++ 
b/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out
@@ -466,6 +466,12 @@ true
 -- !sql --
 true
 
+-- !sql --
+1998 52
+
+-- !sql --
+2024 52
+
 -- !sql --
 2022 31 4
 
diff --git 
a/regression-test/data/query_p0/sql_functions/datetime_functions/test_date_function.out
 
b/regression-test/data/query_p0/sql_functions/datetime_functions/test_date_function.out
index 40c2dda3ec8..ff0cde4324d 100644
--- 
a/regression-test/data/query_p0/sql_functions/datetime_functions/test_date_function.out
+++ 
b/regression-test/data/query_p0/sql_functions/datetime_functions/test_date_function.out
@@ -490,6 +490,12 @@ true
 -- !sql --
 true
 
+-- !sql --
+1998 52
+
+-- !sql --
+2024 52
+
 -- !sql --
 2022 31 4
 
diff --git 
a/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy
 
b/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy
index 97e8df2bbd0..2c84eba4a43 100644
--- 
a/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy
+++ 
b/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy
@@ -465,6 +465,8 @@ suite("test_date_function") {
 
     sql """ drop table ${tableName} """
 
+    qt_sql """ select date_format('1999-01-01', '%X %V'); """
+    qt_sql """ select date_format('2025-01-01', '%X %V'); """
     qt_sql """ select date_format('2022-08-04', '%X %V %w'); """
     qt_sql """ select STR_TO_DATE('Tue Jul 12 20:00:45 CST 2022', '%a %b %e 
%H:%i:%s %Y'); """
     qt_sql """ select STR_TO_DATE('Tue Jul 12 20:00:45 CST 2022', '%a %b %e %T 
CST %Y'); """
diff --git 
a/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy
 
b/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy
index 3d34c67a938..75142d7d847 100644
--- 
a/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy
+++ 
b/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy
@@ -469,6 +469,8 @@ suite("test_date_function") {
 
     sql """ drop table ${tableName} """
 
+    qt_sql """ select date_format('1999-01-01', '%X %V'); """
+    qt_sql """ select date_format('2025-01-01', '%X %V'); """
     qt_sql """ select date_format('2022-08-04', '%X %V %w'); """
     qt_sql """ select STR_TO_DATE('Tue Jul 12 20:00:45 CST 2022', '%a %b %e 
%H:%i:%s %Y'); """
     qt_sql """ select STR_TO_DATE('Tue Jul 12 20:00:45 CST 2022', '%a %b %e %T 
CST %Y'); """


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to