Skip to content

Commit

Permalink
[fix](Nereids) fix fe fold constant with date time out of range
Browse files Browse the repository at this point in the history
  • Loading branch information
LiBinfeng-01 committed Dec 10, 2024
1 parent ed11252 commit 3c4b7cd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c4b7cd

Please sign in to comment.