diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java index a1a18ddf8dcd25..e007701db4d2da 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java @@ -29,9 +29,6 @@ import org.apache.doris.nereids.util.DateUtils; import org.apache.doris.nereids.util.StandardDateFormat; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - import java.time.LocalDateTime; import java.time.Year; import java.time.temporal.ChronoField; @@ -44,11 +41,9 @@ public class DateLiteral extends Literal { public static final String JAVA_DATE_FORMAT = "yyyy-MM-dd"; // for cast datetime type to date type. - private static final LocalDateTime startOfAD = LocalDateTime.of(0, 1, 1, 0, 0, 0); - private static final LocalDateTime endOfAD = LocalDateTime.of(9999, 12, 31, 23, 59, 59); - private static final Logger LOG = LogManager.getLogger(DateLiteral.class); - - private static final DateLiteral MIN_DATE = new DateLiteral(0000, 1, 1); + private static final LocalDateTime START_OF_A_DAY = LocalDateTime.of(0, 1, 1, 0, 0, 0); + private static final LocalDateTime END_OF_A_DAY = LocalDateTime.of(9999, 12, 31, 23, 59, 59); + private static final DateLiteral MIN_DATE = new DateLiteral(0, 1, 1); private static final DateLiteral MAX_DATE = new DateLiteral(9999, 12, 31); private static final int[] DAYS_IN_MONTH = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; @@ -264,7 +259,7 @@ protected void init(String s) throws AnalysisException { month = DateUtils.getOrDefault(dateTime, ChronoField.MONTH_OF_YEAR); day = DateUtils.getOrDefault(dateTime, ChronoField.DAY_OF_MONTH); - if (checkRange() || checkDate()) { + if (checkDatetime(dateTime) || checkRange() || checkDate()) { throw new AnalysisException("date/datetime literal [" + s + "] is out of range"); } } @@ -284,7 +279,14 @@ protected boolean checkDate() { } protected static boolean isDateOutOfRange(LocalDateTime dateTime) { - return dateTime.isBefore(startOfAD) || dateTime.isAfter(endOfAD); + return dateTime.isBefore(START_OF_A_DAY) || dateTime.isAfter(END_OF_A_DAY); + } + + private boolean checkDatetime(TemporalAccessor dateTime) { + return DateUtils.getOrDefault(dateTime, ChronoField.HOUR_OF_DAY) != 0 + || DateUtils.getOrDefault(dateTime, ChronoField.MINUTE_OF_HOUR) != 0 + || DateUtils.getOrDefault(dateTime, ChronoField.SECOND_OF_MINUTE) != 0 + || DateUtils.getOrDefault(dateTime, ChronoField.MICRO_OF_SECOND) != 0; } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/types/coercion/DateLikeType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/types/coercion/DateLikeType.java index eca9170157b337..9d0cd838a7274a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/types/coercion/DateLikeType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/types/coercion/DateLikeType.java @@ -61,9 +61,11 @@ public double rangeLength(double high, double low) { */ public DateLiteral fromString(String s) { if (this instanceof DateType) { - return new DateLiteral(s); + DateTimeV2Literal l = new DateTimeV2Literal(DateTimeV2Type.MAX, s); + return new DateLiteral(l.getYear(), l.getMonth(), l.getDay()); } else if (this instanceof DateV2Type) { - return new DateV2Literal(s); + DateTimeV2Literal l = new DateTimeV2Literal(DateTimeV2Type.MAX, s); + return new DateV2Literal(l.getYear(), l.getMonth(), l.getDay()); } else if (this instanceof DateTimeType) { return new DateTimeLiteral(s); } else if (this instanceof DateTimeV2Type) { diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java index 63c24b81ceaf31..f766ea80e56a48 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java @@ -488,10 +488,10 @@ void testDateTimeV2TypeDateTimeArithmeticFunctions() { Assertions.assertEquals(DateTimeExtractAndTransform.dateV2(dateLiteral).toSql(), answer[answerIdx]); Assertions.assertEquals("'2021 52 2022 01'", DateTimeExtractAndTransform.dateFormat( - new DateLiteral("2022-01-01 00:12:42"), + new DateTimeLiteral("2022-01-01 00:12:42"), new VarcharLiteral("%x %v %X %V")).toSql()); Assertions.assertEquals("'2023 18 2023 19'", DateTimeExtractAndTransform.dateFormat( - new DateLiteral("2023-05-07 02:41:42"), + new DateTimeLiteral("2023-05-07 02:41:42"), new VarcharLiteral("%x %v %X %V")).toSql()); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteralTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteralTest.java index a03010e583e3e7..7df00adf1d1008 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteralTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteralTest.java @@ -35,6 +35,10 @@ void reject() { // Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01-1")); // Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01+01")); // Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01+1")); + Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01 01:00:00.000000")); + Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01 00:01:00.000000")); + Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01 00:00:01.000000")); + Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01 00:00:00.000001")); } @Test diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteralTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteralTest.java index 950a3c953b95db..3347b423529016 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteralTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteralTest.java @@ -245,25 +245,24 @@ void testDateTime() { @Test void testIrregularDateTime() { - new DateLiteral("2016-07-02 01:01:00"); - - new DateLiteral("2016-7-02 01:01:00"); - new DateLiteral("2016-07-2 01:01:00"); - new DateLiteral("2016-7-2 01:01:00"); - - new DateLiteral("2016-07-02 1:01:00"); - new DateLiteral("2016-07-02 01:1:00"); - new DateLiteral("2016-07-02 01:01:0"); - new DateLiteral("2016-07-02 1:1:00"); - new DateLiteral("2016-07-02 1:01:0"); - new DateLiteral("2016-07-02 10:1:0"); - new DateLiteral("2016-07-02 1:1:0"); - - new DateLiteral("2016-7-2 1:1:0"); - new DateLiteral("2016-7-02 1:01:0"); - new DateLiteral("2016-07-2 1:1:0"); - new DateLiteral("2016-7-02 01:01:0"); - new DateLiteral("2016-7-2 01:1:0"); + + new DateTimeV2Literal("2016-7-02 01:01:00"); + new DateTimeV2Literal("2016-07-2 01:01:00"); + new DateTimeV2Literal("2016-7-2 01:01:00"); + + new DateTimeV2Literal("2016-07-02 1:01:00"); + new DateTimeV2Literal("2016-07-02 01:1:00"); + new DateTimeV2Literal("2016-07-02 01:01:0"); + new DateTimeV2Literal("2016-07-02 1:1:00"); + new DateTimeV2Literal("2016-07-02 1:01:0"); + new DateTimeV2Literal("2016-07-02 10:1:0"); + new DateTimeV2Literal("2016-07-02 1:1:0"); + + new DateTimeV2Literal("2016-7-2 1:1:0"); + new DateTimeV2Literal("2016-7-02 1:01:0"); + new DateTimeV2Literal("2016-07-2 1:1:0"); + new DateTimeV2Literal("2016-7-02 01:01:0"); + new DateTimeV2Literal("2016-7-2 01:1:0"); } @Test