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 date and date time arithmatic #40745

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -174,6 +174,16 @@ public static Expression daysAdd(DateTimeV2Literal date, IntegerLiteral day) {
/**
* datetime arithmetic function hours-add.
*/
@ExecFunction(name = "hours_add")
public static Expression hoursAdd(DateLiteral date, IntegerLiteral hour) {
return date.toBeginOfTheDay().plusHours(hour.getValue());
}

@ExecFunction(name = "hours_add")
public static Expression hoursAdd(DateV2Literal date, IntegerLiteral hour) {
return date.toBeginOfTheDay().plusHours(hour.getValue());
}

@ExecFunction(name = "hours_add")
public static Expression hoursAdd(DateTimeLiteral date, IntegerLiteral hour) {
return date.plusHours(hour.getValue());
Expand All @@ -187,6 +197,16 @@ public static Expression hoursAdd(DateTimeV2Literal date, IntegerLiteral hour) {
/**
* datetime arithmetic function minutes-add.
*/
@ExecFunction(name = "minutes_add")
public static Expression minutesAdd(DateLiteral date, IntegerLiteral minute) {
return date.toBeginOfTheDay().plusMinutes(minute.getValue());
}

@ExecFunction(name = "minutes_add")
public static Expression minutesAdd(DateV2Literal date, IntegerLiteral minute) {
return date.toBeginOfTheDay().plusMinutes(minute.getValue());
}

@ExecFunction(name = "minutes_add")
public static Expression minutesAdd(DateTimeLiteral date, IntegerLiteral minute) {
return date.plusMinutes(minute.getValue());
Expand All @@ -200,6 +220,16 @@ public static Expression minutesAdd(DateTimeV2Literal date, IntegerLiteral minut
/**
* datetime arithmetic function seconds-add.
*/
@ExecFunction(name = "seconds_add")
public static Expression secondsAdd(DateLiteral date, IntegerLiteral second) {
return date.toBeginOfTheDay().plusSeconds(second.getValue());
}

@ExecFunction(name = "seconds_add")
public static Expression secondsAdd(DateV2Literal date, IntegerLiteral second) {
return date.toBeginOfTheDay().plusSeconds(second.getValue());
}

@ExecFunction(name = "seconds_add")
public static Expression secondsAdd(DateTimeLiteral date, IntegerLiteral second) {
return date.plusSeconds(second.getValue());
Expand Down Expand Up @@ -404,4 +434,14 @@ public static Expression dateDiff(DateTimeV2Literal date1, DateTimeV2Literal dat
private static int dateDiff(LocalDateTime date1, LocalDateTime date2) {
return ((int) ChronoUnit.DAYS.between(date2.toLocalDate(), date1.toLocalDate()));
}

@ExecFunction(name = "to_days")
public static Expression toDays(DateLiteral date) {
return new IntegerLiteral((int) date.getDay());
}

@ExecFunction(name = "to_days")
public static Expression toDays(DateV2Literal date) {
return new IntegerLiteral((int) date.getDay());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.DateFormat;
import org.apache.doris.nereids.trees.expressions.functions.scalar.DateTrunc;
import org.apache.doris.nereids.trees.expressions.functions.scalar.FromUnixtime;
import org.apache.doris.nereids.trees.expressions.functions.scalar.HoursAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.MinutesAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.StrToDate;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ToDays;
import org.apache.doris.nereids.trees.expressions.functions.scalar.UnixTimestamp;
import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DateLiteral;
Expand Down Expand Up @@ -179,6 +183,46 @@ void testCastFold() {
Assertions.assertEquals(rewritten, expected);
}

@Test
void testFoldDate() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(FoldConstantRuleOnFE.VISITOR_INSTANCE)
));
HoursAdd hoursAdd = new HoursAdd(DateLiteral.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
new IntegerLiteral(1));
Expression rewritten = executor.rewrite(hoursAdd, context);
Assertions.assertTrue(new DateTimeLiteral("0001-01-01 01:00:00").compareTo((Literal) rewritten) == 0);
hoursAdd = new HoursAdd(DateV2Literal.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
new IntegerLiteral(1));
rewritten = executor.rewrite(hoursAdd, context);
Assertions.assertTrue(new DateTimeV2Literal("0001-01-01 01:00:00").compareTo((Literal) rewritten) == 0);

MinutesAdd minutesAdd = new MinutesAdd(DateLiteral.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
new IntegerLiteral(1));
rewritten = executor.rewrite(minutesAdd, context);
Assertions.assertTrue(new DateTimeLiteral("0001-01-01 00:01:00").compareTo((Literal) rewritten) == 0);
minutesAdd = new MinutesAdd(DateV2Literal.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
new IntegerLiteral(1));
rewritten = executor.rewrite(minutesAdd, context);
Assertions.assertTrue(new DateTimeV2Literal("0001-01-01 00:01:00").compareTo((Literal) rewritten) == 0);

SecondsAdd secondsAdd = new SecondsAdd(DateLiteral.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
new IntegerLiteral(1));
rewritten = executor.rewrite(secondsAdd, context);
Assertions.assertTrue(new DateTimeLiteral("0001-01-01 00:00:01").compareTo((Literal) rewritten) == 0);
secondsAdd = new SecondsAdd(DateV2Literal.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)),
new IntegerLiteral(1));
rewritten = executor.rewrite(secondsAdd, context);
Assertions.assertTrue(new DateTimeV2Literal("0001-01-01 00:00:01").compareTo((Literal) rewritten) == 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use assertEquals


ToDays toDays = new ToDays(DateLiteral.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)));
rewritten = executor.rewrite(toDays, context);
Assertions.assertTrue(new IntegerLiteral(366).compareTo((Literal) rewritten) == 0);
toDays = new ToDays(DateV2Literal.fromJavaDateType(LocalDateTime.of(1, 1, 1, 1, 1, 1)));
rewritten = executor.rewrite(toDays, context);
Assertions.assertTrue(new IntegerLiteral(366).compareTo((Literal) rewritten) == 0);
}

@Test
void testFoldString() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,6 @@ suite("test_fold_constant_by_fe") {
res = sql """explain select "12" like '%123%'"""
assertTrue(res.contains("like"))

testFoldConst("select DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY) + INTERVAL 3600 SECOND")

}
Loading