From f7ae38ad6bf59136f01fc46141cf657a203c9053 Mon Sep 17 00:00:00 2001 From: even986025158 <39552698+even986025158@users.noreply.github.com> Date: Fri, 11 Oct 2024 19:00:57 +0800 Subject: [PATCH] [Enhancement]implement dayofweek in FE (#51453) Signed-off-by: evenhuang <986025158@qq.com> (cherry picked from commit fb410393146ed808d5c15d6ba0ce99f940fb7ec8) Signed-off-by: even986025158 <39552698+even986025158@users.noreply.github.com> --- .../rewrite/ScalarOperatorFunctions.java | 28 ++++++++++++++++++- .../rewrite/ScalarOperatorFunctionsTest.java | 15 ++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctions.java b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctions.java index 82accbf03334e..f336b276a2ecb 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctions.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctions.java @@ -182,7 +182,33 @@ public static ConstantOperator dateDiff(ConstantOperator first, ConstantOperator first.getDatetime().truncatedTo(ChronoUnit.DAYS)).toDays()); } - @ConstantFunction(name = "years_add", argTypes = {DATETIME, INT}, returnType = DATETIME, isMonotonic = true) + @ConstantFunction.List(list = { + @ConstantFunction(name = "to_days", argTypes = {DATETIME}, returnType = INT, isMonotonic = true), + @ConstantFunction(name = "to_days", argTypes = {DATE}, returnType = INT, isMonotonic = true) + }) + public static ConstantOperator to_days(ConstantOperator first) { + ConstantOperator second = ConstantOperator.createDatetime(LocalDateTime.of(0000, 01, 01, 00, 00, 00)); + return ConstantOperator.createInt((int) Duration.between( + second.getDatetime().truncatedTo(ChronoUnit.DAYS), + first.getDatetime().truncatedTo(ChronoUnit.DAYS)).toDays()); + } + + @ConstantFunction.List(list = { + @ConstantFunction(name = "dayofweek", argTypes = {DATETIME}, returnType = INT), + @ConstantFunction(name = "dayofweek", argTypes = {DATE}, returnType = INT), + @ConstantFunction(name = "dayofweek", argTypes = {INT}, returnType = INT) + }) + public static ConstantOperator dayofweek(ConstantOperator date) { + // LocalDateTime.getDayOfWeek is return day of the week, such as monday is 1 and sunday is 7. + // function of dayofweek in starrocks monday is 2 and sunday is 1, so need mod 7 and plus 1. + return ConstantOperator.createInt((date.getDatetime().getDayOfWeek().getValue()) % 7 + 1); + } + + @ConstantFunction.List(list = { + @ConstantFunction(name = "years_add", argTypes = {DATETIME, + INT}, returnType = DATETIME, isMonotonic = true), + @ConstantFunction(name = "years_add", argTypes = {DATE, INT}, returnType = DATE, isMonotonic = true) + }) public static ConstantOperator yearsAdd(ConstantOperator date, ConstantOperator year) { return ConstantOperator.createDatetime(date.getDatetime().plusYears(year.getInt())); } diff --git a/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctionsTest.java b/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctionsTest.java index 68fd04cc90351..223c8052475e0 100644 --- a/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctionsTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctionsTest.java @@ -118,6 +118,21 @@ public void dateDiff() { assertEquals(-1572, ScalarOperatorFunctions.dateDiff(O_DT_20101202_023010, O_DT_20150323_092355).getInt()); } + @Test + public void dayofweek() { + ConstantOperator testDate = ConstantOperator.createDatetime(LocalDateTime.of(2024, 2, 3, 13, 4, 5)); + assertEquals(7, + ScalarOperatorFunctions.dayofweek(testDate).getInt()); + + testDate = ConstantOperator.createDatetime(LocalDateTime.of(2024, 2, 4, 13, 4, 5)); + assertEquals(1, + ScalarOperatorFunctions.dayofweek(testDate).getInt()); + + testDate = ConstantOperator.createDatetime(LocalDateTime.of(2024, 2, 5, 13, 4, 5)); + assertEquals(2, + ScalarOperatorFunctions.dayofweek(testDate).getInt()); + } + @Test public void yearsAdd() { assertEquals("2025-03-23T09:23:55",