[feature](Nereids) fold weeks_sub/add on fe (#25155)

support folding weeks_sub/add on fe
This commit is contained in:
谢健
2023-10-09 21:52:44 +08:00
committed by GitHub
parent 53b46b7e6c
commit 5e8aef4756
6 changed files with 73 additions and 2 deletions

View File

@ -30,7 +30,7 @@ import java.time.temporal.ChronoUnit;
/**
* executable function:
* date_add/sub, years/months/days/hours/minutes/seconds_add/sub, datediff
* date_add/sub, years/months/week/days/hours/minutes/seconds_add/sub, datediff
*/
public class DateTimeArithmetic {
/**
@ -125,6 +125,29 @@ public class DateTimeArithmetic {
return date.plusMonths(month.getValue());
}
/**
* datetime arithmetic function weeks-add.
*/
@ExecFunction(name = "weeks_add", argTypes = {"DATE", "INT"}, returnType = "DATE")
public static Expression weeksAdd(DateLiteral date, IntegerLiteral weeks) {
return date.plusWeeks(weeks.getValue());
}
@ExecFunction(name = "weeks_add", argTypes = {"DATETIME", "INT"}, returnType = "DATETIME")
public static Expression weeksAdd(DateTimeLiteral date, IntegerLiteral weeks) {
return date.plusWeeks(weeks.getValue());
}
@ExecFunction(name = "weeks_add", argTypes = {"DATEV2", "INT"}, returnType = "DATEV2")
public static Expression weeksAdd(DateV2Literal date, IntegerLiteral weeks) {
return date.plusWeeks(weeks.getValue());
}
@ExecFunction(name = "weeks_add", argTypes = {"DATETIMEV2", "INT"}, returnType = "DATETIMEV2")
public static Expression weeksAdd(DateTimeV2Literal date, IntegerLiteral weeks) {
return date.plusWeeks(weeks.getValue());
}
/**
* datetime arithmetic function days-add.
*/
@ -241,6 +264,29 @@ public class DateTimeArithmetic {
return monthsAdd(date, new IntegerLiteral(-month.getValue()));
}
/**
* datetime arithmetic function weeks-sub.
*/
@ExecFunction(name = "weeks_sub", argTypes = {"DATE", "INT"}, returnType = "DATE")
public static Expression weeksSub(DateLiteral date, IntegerLiteral weeks) {
return date.plusWeeks(-weeks.getValue());
}
@ExecFunction(name = "weeks_sub", argTypes = {"DATETIME", "INT"}, returnType = "DATETIME")
public static Expression weeksSub(DateTimeLiteral date, IntegerLiteral weeks) {
return date.plusWeeks(-weeks.getValue());
}
@ExecFunction(name = "weeks_sub", argTypes = {"DATEV2", "INT"}, returnType = "DATEV2")
public static Expression weeksSub(DateV2Literal date, IntegerLiteral weeks) {
return date.plusWeeks(-weeks.getValue());
}
@ExecFunction(name = "weeks_sub", argTypes = {"DATETIMEV2", "INT"}, returnType = "DATETIMEV2")
public static Expression weeksSub(DateTimeV2Literal date, IntegerLiteral weeks) {
return date.plusWeeks(-weeks.getValue());
}
/**
* datetime arithmetic function days-sub
*/

View File

@ -345,6 +345,11 @@ public class DateLiteral extends Literal {
DateUtils.getTime(StandardDateFormat.DATE_FORMATTER, getStringValue()).plusMonths(months));
}
public Expression plusWeeks(long weeks) {
return fromJavaDateType(
DateUtils.getTime(StandardDateFormat.DATE_FORMATTER, getStringValue()).plusWeeks(weeks));
}
public Expression plusYears(long years) {
return fromJavaDateType(
DateUtils.getTime(StandardDateFormat.DATE_FORMATTER, getStringValue()).plusYears(years));

View File

@ -209,6 +209,11 @@ public class DateTimeLiteral extends DateLiteral {
DateUtils.getTime(StandardDateFormat.DATE_TIME_FORMATTER, getStringValue()).plusMonths(months));
}
public Expression plusWeeks(long weeks) {
return fromJavaDateType(
DateUtils.getTime(StandardDateFormat.DATE_TIME_FORMATTER, getStringValue()).plusWeeks(weeks));
}
public Expression plusDays(long days) {
return fromJavaDateType(
DateUtils.getTime(StandardDateFormat.DATE_TIME_FORMATTER, getStringValue()).plusDays(days));

View File

@ -120,6 +120,13 @@ public class DateTimeV2Literal extends DateTimeLiteral {
.plusMonths(months), getDataType().getScale());
}
@Override
public Expression plusWeeks(long weeks) {
return fromJavaDateType(
DateUtils.getTime(StandardDateFormat.DATE_TIME_FORMATTER_TO_MICRO_SECOND, getStringValue())
.plusWeeks(weeks), getDataType().getScale());
}
@Override
public Expression plusDays(long days) {
return fromJavaDateType(

View File

@ -60,6 +60,11 @@ public class DateV2Literal extends DateLiteral {
DateUtils.getTime(StandardDateFormat.DATE_FORMATTER, getStringValue()).plusMonths(months));
}
public Expression plusWeeks(long weeks) {
return fromJavaDateType(
DateUtils.getTime(StandardDateFormat.DATE_FORMATTER, getStringValue()).plusWeeks(weeks));
}
public Expression plusYears(long years) {
return fromJavaDateType(
DateUtils.getTime(StandardDateFormat.DATE_FORMATTER, getStringValue()).plusYears(years));

View File

@ -309,7 +309,8 @@ class FoldConstantTest extends ExpressionRewriteTestHelper {
String[] answer = {
"2000-01-30", "1999-12-01", "2029-12-31", "1969-12-31",
"2002-06-30", "1997-06-30", "2000-01-30", "1999-12-01",
"2002-06-30", "1997-06-30", "2000-07-28", "1999-06-04",
"2000-01-30", "1999-12-01",
"1999", "4", "12", "6", "31", "365", "31",
"'1999-12-31'", "1999-12-27", "1999-12-31"
};
@ -321,6 +322,8 @@ class FoldConstantTest extends ExpressionRewriteTestHelper {
Assertions.assertEquals(DateTimeArithmetic.yearsSub(dateLiteral, integerLiteral).toSql(), answer[answerIdx++]);
Assertions.assertEquals(DateTimeArithmetic.monthsAdd(dateLiteral, integerLiteral).toSql(), answer[answerIdx++]);
Assertions.assertEquals(DateTimeArithmetic.monthsSub(dateLiteral, integerLiteral).toSql(), answer[answerIdx++]);
Assertions.assertEquals(DateTimeArithmetic.weeksAdd(dateLiteral, integerLiteral).toSql(), answer[answerIdx++]);
Assertions.assertEquals(DateTimeArithmetic.weeksSub(dateLiteral, integerLiteral).toSql(), answer[answerIdx++]);
Assertions.assertEquals(DateTimeArithmetic.daysAdd(dateLiteral, integerLiteral).toSql(), answer[answerIdx++]);
Assertions.assertEquals(DateTimeArithmetic.daysSub(dateLiteral, integerLiteral).toSql(), answer[answerIdx++]);