This is an automated email from the ASF dual-hosted git repository. kxiao pushed a commit to branch branch-2.0 in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.0 by this push: new 4b657d5b3de [fix](Nereids) init Date/DateV2Literal should check non-zero time fields (#25402) 4b657d5b3de is described below commit 4b657d5b3dedced176df8924da2e3def815e8093 Author: morrySnow <101034200+morrys...@users.noreply.github.com> AuthorDate: Fri Oct 13 17:13:14 2023 +0800 [fix](Nereids) init Date/DateV2Literal should check non-zero time fields (#25402) check picked from master PR: #24971 commit id: 63b283a8481148181aeaf68b72af7b7c027b5359 --- .../trees/expressions/literal/DateLiteral.java | 22 +++++++------ .../doris/nereids/types/coercion/DateLikeType.java | 6 ++-- .../nereids/rules/expression/FoldConstantTest.java | 4 +-- .../trees/expressions/literal/DateLiteralTest.java | 4 +++ .../expressions/literal/DateTimeLiteralTest.java | 37 +++++++++++----------- 5 files changed, 40 insertions(+), 33 deletions(-) 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 f522280c4be..9b2d281161b 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.DateTimeFormatterUtils; 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 @@ public class DateLiteral extends Literal { 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 @@ public class DateLiteral extends Literal { } 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 ff728a73ace..acbce88e5e2 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 @@ -59,9 +59,11 @@ public abstract class DateLikeType extends PrimitiveType { */ 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 af23c34c270..e92841f5ead 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 @@ -491,10 +491,10 @@ public class FoldConstantTest extends ExpressionRewriteTestHelper { 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 a03010e583e..7df00adf1d1 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 @@ class DateLiteralTest { // 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 950a3c953b9..3347b423529 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 @@ class DateTimeLiteralTest { @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 --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org