[fix](function) fix date_format function execution error on fe (#31645)
This commit is contained in:
@ -45,6 +45,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;
|
||||
@ -53,10 +54,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;
|
||||
@ -108,6 +111,7 @@ public class DateLiteral extends LiteralExpr {
|
||||
private static Map<String, Integer> WEEK_DAY_NAME_DICT = Maps.newHashMap();
|
||||
private static Set<Character> TIME_PART_SET = Sets.newHashSet();
|
||||
private static final int[] DAYS_IN_MONTH = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
private static final WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 7);
|
||||
|
||||
static {
|
||||
try {
|
||||
@ -1047,7 +1051,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);
|
||||
@ -1059,6 +1063,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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -491,7 +491,7 @@ 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(
|
||||
|
||||
@ -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());
|
||||
|
||||
@ -479,6 +479,12 @@ true
|
||||
-- !sql --
|
||||
true
|
||||
|
||||
-- !sql --
|
||||
1998 52
|
||||
|
||||
-- !sql --
|
||||
2024 52
|
||||
|
||||
-- !sql --
|
||||
2022 31 4
|
||||
|
||||
|
||||
@ -496,6 +496,12 @@ true
|
||||
-- !sql --
|
||||
true
|
||||
|
||||
-- !sql --
|
||||
1998 52
|
||||
|
||||
-- !sql --
|
||||
2024 52
|
||||
|
||||
-- !sql --
|
||||
2022 31 4
|
||||
|
||||
|
||||
@ -470,6 +470,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'); """
|
||||
|
||||
@ -473,6 +473,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'); """
|
||||
|
||||
Reference in New Issue
Block a user