[fix](function) fix date_format function execution error on fe (#31645)

This commit is contained in:
lw112
2024-03-07 12:04:48 +08:00
committed by yiguolei
parent 21ce85dc14
commit e91d16854b
8 changed files with 32 additions and 4 deletions

View File

@ -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

View File

@ -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