[feature](planner) add dayofweek for FEFunctions to support fold constant (#16993)

add dayofweek for FEFunctions to support fold constant. use Zellar algorithm
This commit is contained in:
mch_ucchi
2023-02-22 20:27:49 +08:00
committed by GitHub
parent 7aa063c1f3
commit df2f248712
2 changed files with 39 additions and 0 deletions

View File

@ -77,6 +77,27 @@ public class FEFunctions {
return new IntLiteral(datediff, Type.INT);
}
@FEFunction(name = "dayofweek", argTypes = {"DATETIME"}, returnType = "INT")
public static IntLiteral dayOfWeek(LiteralExpr date) throws AnalysisException {
// use zellar algorithm.
long year = ((DateLiteral) date).getYear();
long month = ((DateLiteral) date).getMonth();
long day = ((DateLiteral) date).getDay();
if (month < 3) {
month += 12;
year -= 1;
}
long c = year / 100;
long y = year % 100;
long t;
if (date.compareTo(new DateLiteral(1582, 10, 4)) > 0) {
t = (y + y / 4 + c / 4 - 2 * c + 26 * (month + 1) / 10 + day - 1) % 7;
} else {
t = (y + y / 4 - c + 26 * (month + 1) / 10 + day + 4) % 7;
}
return new IntLiteral(t + 1);
}
@FEFunction(name = "date_add", argTypes = { "DATETIME", "INT" }, returnType = "DATETIME")
public static DateLiteral dateAdd(LiteralExpr date, LiteralExpr day) throws AnalysisException {
return daysAdd(date, day);

View File

@ -214,6 +214,24 @@ public class FEFunctionsTest {
Assert.assertEquals(expectedResult, actualResult);
}
@Test
public void dayOfWeekTest() throws AnalysisException {
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-23", Type.DATE)).getStringValue(), "1");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-24", Type.DATE)).getStringValue(), "2");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-25", Type.DATE)).getStringValue(), "3");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-26", Type.DATE)).getStringValue(), "4");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-27", Type.DATE)).getStringValue(), "5");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-28", Type.DATE)).getStringValue(), "6");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2019-06-29", Type.DATE)).getStringValue(), "7");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-13", Type.DATE)).getStringValue(), "2");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-14", Type.DATE)).getStringValue(), "3");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-15", Type.DATE)).getStringValue(), "4");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-16", Type.DATE)).getStringValue(), "5");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-17", Type.DATE)).getStringValue(), "6");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-18", Type.DATE)).getStringValue(), "7");
Assert.assertEquals(FEFunctions.dayOfWeek(new DateLiteral("2023-02-19", Type.DATE)).getStringValue(), "1");
}
@Test
public void fromUnixTimeTest() throws AnalysisException {
StringLiteral actualResult = FEFunctions.fromUnixTime(new IntLiteral(100000));