Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](Nereids) fix fe fold constant with date time out of range #45237

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ private EvaluateRangeResult computeMonotonicFunctionRange(EvaluateRangeResult re
expressionRewriteContext) : null;
Expression upperValue = upper != null ? FoldConstantRuleOnFE.evaluate(func.withConstantArgs(upper),
expressionRewriteContext) : null;
if (lowerValue instanceof NullLiteral || upperValue instanceof NullLiteral) {
if (!checkFoldConstantValueIsValid(lowerValue, upperValue)) {
return result;
}
if (!func.isPositive()) {
Expand Down Expand Up @@ -858,4 +858,16 @@ private EvaluateRangeResult computeMonotonicFunctionRange(EvaluateRangeResult re
return new EvaluateRangeResult((Expression) func, newRanges, result.childrenResult);
}
}

// only allow literal(except NullLiteral) and null
private boolean checkFoldConstantValueIsValid(Expression lowerValue, Expression upperValue) {
if (lowerValue instanceof NullLiteral || upperValue instanceof NullLiteral) {
return false;
}
if (lowerValue != null && !(lowerValue instanceof Literal)
|| upperValue != null && !(upperValue instanceof Literal)) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,10 @@ private static Expression processDateLikeTypeCoercion(ComparisonPredicate cp, Ex
return BooleanLiteral.FALSE;
} else if (cp instanceof GreaterThanEqual || cp instanceof LessThan) {
// '9999-12-31' + 1 will overflow
Expression tomorrow = ((DateV2Literal) right).plusDays(1);
if (tomorrow.isNullLiteral()) {
if (DateLiteral.isDateOutOfRange(((DateV2Literal) right).toJavaDateType().plusDays(1))) {
return cp;
}
right = tomorrow;
right = ((DateV2Literal) right).plusDays(1);
}
}
if (cast.child().getDataType() instanceof DateV2Type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ protected static boolean checkDate(long year, long month, long day) {
return false;
}

protected static boolean isDateOutOfRange(LocalDateTime dateTime) {
public static boolean isDateOutOfRange(LocalDateTime dateTime) {
return dateTime == null || dateTime.isBefore(START_OF_A_DAY) || dateTime.isAfter(END_OF_A_DAY);
}

Expand Down Expand Up @@ -530,9 +530,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 AnalysisException("datetime out of range: " + dateTime.toString());
}
return new DateLiteral(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,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 AnalysisException("datetime out of range: " + dateTime.toString());
}
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 @@ -281,9 +281,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 AnalysisException("datetime out of range" + dateTime.toString());
}
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 @@ -72,9 +72,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 AnalysisException("datetime out of range: " + dateTime.toString());
}
return new DateV2Literal(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ void testFoldDate() {
hoursAdd = new HoursAdd(DateV2Literal.fromJavaDateType(LocalDateTime.of(9999, 12, 31, 23, 1, 1)),
new IntegerLiteral(24));
rewritten = executor.rewrite(hoursAdd, context);
Assertions.assertEquals(new NullLiteral(hoursAdd.getDataType()), rewritten);
Assertions.assertEquals(hoursAdd, rewritten);
hoursAdd = new HoursAdd(DateV2Literal.fromJavaDateType(LocalDateTime.of(0, 1, 1, 1, 1, 1)),
new IntegerLiteral(-25));
rewritten = executor.rewrite(hoursAdd, context);
Assertions.assertEquals(new NullLiteral(hoursAdd.getDataType()), rewritten);
Assertions.assertEquals(hoursAdd, rewritten);

MinutesAdd minutesAdd = new MinutesAdd(DateLiteral.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
new IntegerLiteral(1));
Expand All @@ -237,11 +237,11 @@ void testFoldDate() {
minutesAdd = new MinutesAdd(DateV2Literal.fromJavaDateType(LocalDateTime.of(9999, 12, 31, 23, 59, 1)),
new IntegerLiteral(1440));
rewritten = executor.rewrite(minutesAdd, context);
Assertions.assertEquals(new NullLiteral(minutesAdd.getDataType()), rewritten);
Assertions.assertEquals(minutesAdd, rewritten);
minutesAdd = new MinutesAdd(DateV2Literal.fromJavaDateType(LocalDateTime.of(0, 1, 1, 0, 1, 1)),
new IntegerLiteral(-2));
rewritten = executor.rewrite(minutesAdd, context);
Assertions.assertEquals(new NullLiteral(minutesAdd.getDataType()), rewritten);
Assertions.assertEquals(minutesAdd, rewritten);

SecondsAdd secondsAdd = new SecondsAdd(DateLiteral.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
new IntegerLiteral(1));
Expand All @@ -254,11 +254,11 @@ void testFoldDate() {
secondsAdd = new SecondsAdd(DateV2Literal.fromJavaDateType(LocalDateTime.of(9999, 12, 31, 23, 59, 59)),
new IntegerLiteral(86400));
rewritten = executor.rewrite(secondsAdd, context);
Assertions.assertEquals(new NullLiteral(secondsAdd.getDataType()), rewritten);
Assertions.assertEquals(secondsAdd, rewritten);
secondsAdd = new SecondsAdd(DateV2Literal.fromJavaDateType(LocalDateTime.of(0, 1, 1, 0, 1, 1)),
new IntegerLiteral(-61));
rewritten = executor.rewrite(secondsAdd, context);
Assertions.assertEquals(new NullLiteral(secondsAdd.getDataType()), rewritten);
Assertions.assertEquals(secondsAdd, rewritten);

ToDays toDays = new ToDays(DateLiteral.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)));
rewritten = executor.rewrite(toDays, context);
Expand Down
Loading
Loading