Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -295,29 +295,29 @@ private static LocalDateTime firstDayOfWeek(LocalDateTime dateTime) {
@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(Locale.US).format(
return new VarcharLiteral(DateUtils.dateTimeFormatter(format.getValue()).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(Locale.US).format(
return new VarcharLiteral(DateUtils.dateTimeFormatter(format.getValue()).format(
java.time.LocalDateTime.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()),
((int) date.getHour()), ((int) date.getMinute()), ((int) date.getSecond()))));
}

@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(Locale.US).format(
return new VarcharLiteral(DateUtils.dateTimeFormatter(format.getValue()).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(Locale.US).format(
return new VarcharLiteral(DateUtils.dateTimeFormatter(format.getValue()).format(
java.time.LocalDateTime.of(((int) date.getYear()), ((int) date.getMonth()), ((int) date.getDay()),
((int) date.getHour()), ((int) date.getMinute()), ((int) date.getSecond()))));
}
Expand Down Expand Up @@ -539,7 +539,7 @@ public static Expression unixTimestamp(DateTimeV2Literal date) {
@ExecFunction(name = "unix_timestamp")
public static Expression unixTimestamp(StringLikeLiteral date, StringLikeLiteral format) {
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
DateTimeFormatter formatter = DateUtils.formatBuilder(format.getValue()).toFormatter();
DateTimeFormatter formatter = DateUtils.dateTimeFormatter(format.getValue());
LocalDateTime dateObj;
try {
dateObj = LocalDateTime.parse(date.getValue(), formatter);
Expand Down Expand Up @@ -630,20 +630,20 @@ public static Expression strToDate(StringLikeLiteral str, StringLikeLiteral form
if (returnType instanceof DateTimeV2Type) {
boolean hasMicroPart = org.apache.doris.analysis.DateLiteral
.hasMicroSecondPart(format.getStringValue());
return DateTimeV2Literal.fromJavaDateType(DateUtils.getTime(DateUtils.formatBuilder(format.getValue())
.toFormatter(), str.getValue()), hasMicroPart ? 6 : 0);
return DateTimeV2Literal.fromJavaDateType(DateUtils.getTime(DateUtils
.dateTimeFormatter(format.getValue()), str.getValue()), hasMicroPart ? 6 : 0);
} else {
return DateTimeLiteral.fromJavaDateType(DateUtils.getTime(DateUtils.formatBuilder(format.getValue())
.toFormatter(), str.getValue()));
return DateTimeLiteral.fromJavaDateType(DateUtils.getTime(DateUtils
.dateTimeFormatter(format.getValue()), str.getValue()));
}
} else {
DataType returnType = DataType.fromCatalogType(ScalarType.getDefaultDateType(Type.DATE));
if (returnType instanceof DateV2Type) {
return DateV2Literal.fromJavaDateType(DateUtils.getTime(DateUtils.formatBuilder(format.getValue())
.toFormatter(), str.getValue()));
return DateV2Literal.fromJavaDateType(DateUtils.getTime(DateUtils.dateTimeFormatter(format.getValue()),
str.getValue()));
} else {
return DateLiteral.fromJavaDateType(DateUtils.getTime(DateUtils.formatBuilder(format.getValue())
.toFormatter(), str.getValue()));
return DateLiteral.fromJavaDateType(DateUtils.getTime(DateUtils.dateTimeFormatter(format.getValue()),
str.getValue()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
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.Locale;
import java.util.Set;

/**
Expand All @@ -47,7 +49,7 @@ public class DateUtils {
/**
* format builder.
*/
public static DateTimeFormatterBuilder formatBuilder(String pattern) throws AnalysisException {
public static DateTimeFormatter dateTimeFormatter(String pattern) throws AnalysisException {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
boolean escaped = false;
for (int i = 0; i < pattern.length(); i++) {
Expand Down Expand Up @@ -159,7 +161,7 @@ public static DateTimeFormatterBuilder formatBuilder(String pattern) throws Anal
builder.appendLiteral(character);
}
}
return builder;
return builder.toFormatter(Locale.US).withResolverStyle(ResolverStyle.STRICT);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ suite("fold_constant_date_arithmatic") {
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');")

testFoldConst("select str_to_date('2023-02-29', '%Y-%m-%d') AS result;")
testFoldConst("select str_to_date('1900-02-29', '%Y-%m-%d') AS result;")
testFoldConst("select str_to_date('2025-04-31', '%Y-%m-%d') AS result;")
testFoldConst("select str_to_date('31-12-2020 23:59:59', '%d-%m-%Y %H:%i:%s');")
testFoldConst("select str_to_date('2020-12-31T23:59:59', '%Y-%m-%dT%H:%i:%s');")
testFoldConst("select str_to_date('20201231235959', '%Y%m%d%H%i%s');")
testFoldConst("select str_to_date('31/12/2020 23:59', '%d/%m/%Y %H:%i');")
testFoldConst("select str_to_date('31/12/2020 11:59 PM', '%d/%m/%Y %h:%i %p');")
testFoldConst("select str_to_date('20201231T235959', '%Y%m%dT%H%i%s');")
}
Loading