[Fix](functions) Fix bug in makedate and str_to_date functions

This commit is contained in:
yongjinhou
2024-05-10 09:20:14 +08:00
committed by yiguolei
parent d5d6c7f8a4
commit e38801968d
4 changed files with 24 additions and 5 deletions

View File

@ -32,6 +32,7 @@ import org.apache.doris.nereids.trees.expressions.literal.SmallIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
import org.apache.doris.nereids.types.DateTimeV2Type;
import org.apache.doris.nereids.types.DateType;
import org.apache.doris.nereids.types.DecimalV3Type;
import org.apache.doris.nereids.types.VarcharType;
import org.apache.doris.nereids.util.DateUtils;
@ -593,8 +594,9 @@ public class DateTimeExtractAndTransform {
*/
@ExecFunction(name = "makedate", argTypes = {"INT", "INT"}, returnType = "DATE")
public static Expression makeDate(IntegerLiteral year, IntegerLiteral dayOfYear) {
return DateLiteral.fromJavaDateType(LocalDateTime.of(year.getValue(), 1, 1, 0, 0, 0)
.plusDays(dayOfYear.getValue() - 1));
int day = dayOfYear.getValue();
return day > 0 ? DateLiteral.fromJavaDateType(LocalDateTime.of(year.getValue(), 1, 1, 0, 0, 0)
.plusDays(day - 1)) : new NullLiteral(DateType.INSTANCE);
}
/**

View File

@ -163,7 +163,7 @@ public class DateUtils {
getOrDefault(accessor, ChronoField.YEAR),
getOrDefault(accessor, ChronoField.MONTH_OF_YEAR),
getOrDefault(accessor, ChronoField.DAY_OF_MONTH),
getOrDefault(accessor, ChronoField.HOUR_OF_DAY),
getHourOrDefault(accessor),
getOrDefault(accessor, ChronoField.MINUTE_OF_HOUR),
getOrDefault(accessor, ChronoField.SECOND_OF_MINUTE),
getOrDefault(accessor, ChronoField.NANO_OF_SECOND));
@ -173,6 +173,19 @@ public class DateUtils {
return accessor.isSupported(field) ? accessor.get(field) : /* default value */ 0;
}
/**
* get hour from accessor, if not support hour field, return 0
*/
public static int getHourOrDefault(final TemporalAccessor accessor) {
if (accessor.isSupported(ChronoField.HOUR_OF_DAY)) {
return accessor.get(ChronoField.HOUR_OF_DAY);
} else if (accessor.isSupported(ChronoField.HOUR_OF_AMPM)) {
return accessor.get(ChronoField.HOUR_OF_AMPM);
} else {
return 0;
}
}
public static ZoneId getTimeZone() {
if (ConnectContext.get() == null || ConnectContext.get().getSessionVariable() == null) {
return ZoneId.systemDefault();

View File

@ -234,7 +234,7 @@ Saturday
0
-- !sql --
2021-01-01 2021-04-10 2022-02-04
\N 2021-01-01 2021-04-10 2022-02-04
-- !sql --
59
@ -272,6 +272,9 @@ February
-- !sql --
2014-12-21T12:34:56
-- !sql --
2011-11-09 11:09:30
-- !sql --
2014-12-21T12:34:56.789

View File

@ -299,7 +299,7 @@ suite("test_date_function") {
qt_sql """ select hour('2018-12-31') """
// MAKEDATE
qt_sql """ select makedate(2021,1), makedate(2021,100), makedate(2021,400) """
qt_sql """ select makedate(2021,0), makedate(2021,1), makedate(2021,100), makedate(2021,400) """
// MINUTE
qt_sql """ select minute('2018-12-31 23:59:59') """
@ -332,6 +332,7 @@ suite("test_date_function") {
qt_sql """ select str_to_date(test_datetime, '%Y-%m-%d %H:%i:%s') from ${tableName}; """
qt_sql """ select str_to_date("", "%Y-%m-%d %H:%i:%s"); """
qt_sql """ select str_to_date("2014-12-21 12:34%3A56", '%Y-%m-%d %H:%i%%3A%s'); """
qt_sql """ select str_to_date('11.09.2011 11:09:30', '%m.%d.%Y %h:%i:%s'); """
qt_sql """ select str_to_date("2014-12-21 12:34:56.789 PM", '%Y-%m-%d %h:%i:%s.%f %p'); """
qt_sql """ select str_to_date('2023-07-05T02:09:55.880Z','%Y-%m-%dT%H:%i:%s.%fZ') """
qt_sql """ select str_to_date('200442 Monday', '%X%V %W') """