From 3c4b7cdc3a4cc8b9c4c8be7318665a1ff43e4d18 Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Tue, 10 Dec 2024 16:25:01 +0800 Subject: [PATCH] [fix](Nereids) fix fe fold constant with date time out of range --- .../nereids/trees/expressions/literal/DateLiteral.java | 8 +++++--- .../trees/expressions/literal/DateTimeLiteral.java | 8 +++++--- .../trees/expressions/literal/DateTimeV2Literal.java | 8 +++++--- .../nereids/trees/expressions/literal/DateV2Literal.java | 8 +++++--- .../doris/nereids/rules/expression/FoldConstantTest.java | 6 ++++++ 5 files changed, 26 insertions(+), 12 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 6ea1d2af725679..1475607641da99 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 @@ -20,6 +20,7 @@ import org.apache.doris.analysis.LiteralExpr; import org.apache.doris.catalog.Type; import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.exceptions.NotSupportedException; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; import org.apache.doris.nereids.types.DataType; @@ -518,9 +519,10 @@ private long calculateDays(long year, long month, long day) { } public static Expression fromJavaDateType(LocalDateTime dateTime) { - return isDateOutOfRange(dateTime) - ? new NullLiteral(DateType.INSTANCE) - : new DateLiteral(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth()); + if (isDateOutOfRange(dateTime)) { + throw new NotSupportedException("date out of range"); + } + return new DateLiteral(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth()); } /** diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java index 27470187eae0d2..a0bc694e04d47e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java @@ -20,6 +20,7 @@ import org.apache.doris.analysis.LiteralExpr; import org.apache.doris.catalog.Type; import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.exceptions.NotSupportedException; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; import org.apache.doris.nereids.types.DateTimeType; @@ -395,9 +396,10 @@ public LocalDateTime toJavaDateType() { } public static Expression fromJavaDateType(LocalDateTime dateTime) { - return isDateOutOfRange(dateTime) - ? new NullLiteral(DateTimeType.INSTANCE) - : new DateTimeLiteral(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth(), + if (isDateOutOfRange(dateTime)) { + throw new NotSupportedException("date out of range"); + } + return new DateTimeLiteral(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth(), dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond()); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java index c6ec4b99d4f4b0..d6b9daa3d2795f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java @@ -19,6 +19,7 @@ import org.apache.doris.analysis.LiteralExpr; import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.exceptions.NotSupportedException; import org.apache.doris.nereids.exceptions.UnboundException; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; @@ -276,9 +277,10 @@ public static Expression fromJavaDateType(LocalDateTime dateTime) { */ public static Expression fromJavaDateType(LocalDateTime dateTime, int precision) { long value = (long) Math.pow(10, DateTimeV2Type.MAX_SCALE - precision); - return isDateOutOfRange(dateTime) - ? new NullLiteral(DateTimeV2Type.of(precision)) - : new DateTimeV2Literal(DateTimeV2Type.of(precision), dateTime.getYear(), + if (isDateOutOfRange(dateTime)) { + throw new NotSupportedException("datetime out of range"); + } + return new DateTimeV2Literal(DateTimeV2Type.of(precision), dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth(), dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(), (dateTime.getNano() / 1000) / value * value); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateV2Literal.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateV2Literal.java index 1534d5518508fa..1c416024cf2cd1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateV2Literal.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateV2Literal.java @@ -19,6 +19,7 @@ import org.apache.doris.catalog.Type; import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.exceptions.NotSupportedException; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; import org.apache.doris.nereids.types.DateTimeV2Type; @@ -72,9 +73,10 @@ public Expression plusYears(long years) { } public static Expression fromJavaDateType(LocalDateTime dateTime) { - return isDateOutOfRange(dateTime) - ? new NullLiteral(DateV2Type.INSTANCE) - : new DateV2Literal(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth()); + if (isDateOutOfRange(dateTime)) { + throw new NotSupportedException("date out of range"); + } + return new DateV2Literal(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth()); } /** 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 0601b3b558d882..ce358b65fe3d57 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 @@ -671,6 +671,12 @@ void testDateTypeDateTimeArithmeticFunctions() { answer[answerIdx++]); Assertions.assertEquals(DateTimeExtractAndTransform.toMonday(dateLiteral).toSql(), answer[answerIdx++]); Assertions.assertEquals(DateTimeExtractAndTransform.lastDay(dateLiteral).toSql(), answer[answerIdx]); + + DateLiteral finalDateLiteral = new DateLiteral("9999-12-31"); + IntegerLiteral finalIntegerLiteral = new IntegerLiteral(1); + Assertions.assertThrows(NotSupportedException.class, () -> { + DateTimeArithmetic.dateAdd(finalDateLiteral, finalIntegerLiteral); + }, "datetime out of range"); } @Test