This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch branch-1.2-lts
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-1.2-lts by this push:
     new 91df306418 [feature](planner) add dayofweek for FEFunctions to support 
fold constant (#16993)
91df306418 is described below

commit 91df3064185910d3ed3d87eb3f46ec774fb52fff
Author: mch_ucchi <[email protected]>
AuthorDate: Wed Feb 22 20:27:49 2023 +0800

    [feature](planner) add dayofweek for FEFunctions to support fold constant 
(#16993)
    
    add dayofweek for FEFunctions to support fold constant. use Zellar algorithm
---
 .../java/org/apache/doris/rewrite/FEFunctions.java  | 21 +++++++++++++++++++++
 .../org/apache/doris/rewrite/FEFunctionsTest.java   | 18 ++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java 
b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java
index fc84515fc0..bb62961b9d 100755
--- a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FEFunctions.java
@@ -78,6 +78,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);
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
index 70ec805e64..210a8635bc 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
@@ -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));


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to