[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();