You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2023/06/13 02:37:31 UTC

[doris] branch branch-1.2-lts updated: [Fix](planner) inplement fold constant for function to_monday() (#20707)

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

morrysnow 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 3bc57ec435 [Fix](planner) inplement fold constant for function to_monday() (#20707)
3bc57ec435 is described below

commit 3bc57ec4357acec669244cacea17935aa97c06aa
Author: mch_ucchi <41...@users.noreply.github.com>
AuthorDate: Tue Jun 13 10:37:25 2023 +0800

    [Fix](planner) inplement fold constant for function to_monday() (#20707)
---
 .../java/org/apache/doris/rewrite/FEFunctions.java | 35 ++++++++++++++++++++++
 1 file changed, 35 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 bb62961b9d..9c2023e77f 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
@@ -362,6 +362,41 @@ public class FEFunctions {
         return null;
     }
 
+    @FEFunction(name = "to_monday", argTypes = {"DATETIME"}, returnType = "DATE")
+    public static DateLiteral toMonday(LiteralExpr arg) {
+        if (arg instanceof DateLiteral && (arg.getType().isDate() || arg.getType().isDatetime())) {
+            DateLiteral dateLiteral = ((DateLiteral) arg);
+            LocalDateTime dateTime = LocalDateTime.of(
+                    ((int) dateLiteral.getYear()), ((int) dateLiteral.getMonth()), ((int) dateLiteral.getDay()),
+                    0, 0, 0);
+            dateTime = toMonday(dateTime);
+            return new DateLiteral(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth(), Type.DATE);
+        }
+        return null;
+    }
+
+    @FEFunction(name = "to_monday", argTypes = {"DATETIMEV2"}, returnType = "DATEV2")
+    public static DateLiteral toMondayV2(LiteralExpr arg) {
+        if (arg instanceof DateLiteral && (arg.getType().isDateV2() || arg.getType().isDatetimeV2())) {
+            DateLiteral dateLiteral = ((DateLiteral) arg);
+            LocalDateTime dateTime = LocalDateTime.of(
+                    ((int) dateLiteral.getYear()), ((int) dateLiteral.getMonth()), ((int) dateLiteral.getDay()),
+                    0, 0, 0);
+            dateTime = toMonday(dateTime);
+            return new DateLiteral(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth(), Type.DATEV2);
+        }
+        return null;
+    }
+
+    private static LocalDateTime toMonday(LocalDateTime dateTime) {
+        LocalDateTime specialUpperBound = LocalDateTime.of(1970, 1, 4, 0, 0, 0);
+        LocalDateTime specialLowerBound = LocalDateTime.of(1970, 1, 1, 0, 0, 0);
+        if (dateTime.isAfter(specialUpperBound) || dateTime.isBefore(specialLowerBound)) {
+            return dateTime.plusDays(-dateTime.getDayOfWeek().getValue() + 1);
+        }
+        return specialLowerBound;
+    }
+
     /**
      ------------------------------------------------------------------------------
      */


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org