Skip to content

Commit

Permalink
[Fix](Nereids) fix date and date time arithmatic (#40745) (#41533)
Browse files Browse the repository at this point in the history
cherry-pick from master #40745
fix date and time arithmatic of: hours_add, minutes_add, seconds_add,
to_days
  • Loading branch information
LiBinfeng-01 authored Oct 21, 2024
1 parent 8d60e57 commit d7032d1
Showing 1 changed file with 40 additions and 0 deletions.
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", argTypes = {"DATE", "INT"}, returnType = "DATE")
public static Expression hoursAdd(DateLiteral date, IntegerLiteral hour) {
return date.toBeginOfTheDay().plusHours(hour.getValue());
}

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

@ExecFunction(name = "hours_add", argTypes = {"DATETIME", "INT"}, returnType = "DATETIME")
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", argTypes = {"DATE", "INT"}, returnType = "DATE")
public static Expression minutesAdd(DateLiteral date, IntegerLiteral minute) {
return date.toBeginOfTheDay().plusMinutes(minute.getValue());
}

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

@ExecFunction(name = "minutes_add", argTypes = {"DATETIME", "INT"}, returnType = "DATETIME")
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", argTypes = {"DATE", "INT"}, returnType = "DATE")
public static Expression secondsAdd(DateLiteral date, IntegerLiteral second) {
return date.toBeginOfTheDay().plusSeconds(second.getValue());
}

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

@ExecFunction(name = "seconds_add", argTypes = {"DATETIME", "INT"}, returnType = "DATETIME")
public static Expression secondsAdd(DateTimeLiteral date, IntegerLiteral second) {
return date.plusSeconds(second.getValue());
Expand Down Expand Up @@ -380,4 +410,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", argTypes = {"DATE"}, returnType = "INT")
public static Expression toDays(DateLiteral date) {
return new IntegerLiteral((int) date.getDay());
}

@ExecFunction(name = "to_days", argTypes = {"DATEV2"}, returnType = "INT")
public static Expression toDays(DateV2Literal date) {
return new IntegerLiteral((int) date.getDay());
}
}

0 comments on commit d7032d1

Please sign in to comment.