zclllyybb commented on code in PR #56945:
URL: https://github.com/apache/doris/pull/56945#discussion_r2428239659


##########
regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy:
##########
@@ -838,4 +838,38 @@ suite("test_date_function") {
             result([[true]])
         }
     }()
+
+    sql """ DROP TABLE IF EXISTS test_period_union; """
+    sql """ CREATE TABLE test_period_union (
+            id INT,
+            period_1 BIGINT,
+            month BIGINT,
+            period_2 BIGINT
+        ) DUPLICATE KEY(id)
+        DISTRIBUTED BY HASH(id) BUCKETS 1
+        PROPERTIES ( 'replication_num' = '1' ); """
+    sql """ INSERT INTO test_period_union VALUES

Review Comment:
   测试覆盖的好像不是很全,mysql支持的合法位数还是比较多的



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java:
##########
@@ -1342,4 +1342,45 @@ public static Expression secToTime(IntegerLiteral sec) {
     public static Expression secToTime(DoubleLiteral sec) {
         return new TimeV2Literal(sec.getValue() * 1000000);
     }
+
+    /**
+     * date transform function period_add
+     */
+    @ExecFunction(name = "period_add")
+    public static Expression periodAdd(BigIntLiteral period, BigIntLiteral 
months) {
+        return new BigIntLiteral(convertMonthToPeriod(
+                checkAndConvertPeriodToMonth(period.getValue()) + 
months.getValue()));
+    }
+
+    /**
+     * date transform function period_diff
+     */
+    @ExecFunction(name = "period_diff")
+    public static Expression periodDiff(BigIntLiteral period1, BigIntLiteral 
period2) {
+        return new BigIntLiteral(checkAndConvertPeriodToMonth(
+                period1.getValue()) - 
checkAndConvertPeriodToMonth(period2.getValue()));
+    }
+
+    private static void validatePeriod(long period) {
+        if (period <= 0 || (period % 100) == 0 || (period % 100) > 12) {
+            throw new AnalysisException("Period function got invalid period: " 
+ period);
+        }
+    }
+
+    private static long checkAndConvertPeriodToMonth(long period) {
+        validatePeriod(period);
+        long year = period / 100;

Review Comment:
   mysql似乎不光支持这些,它语义似乎比较复杂?
   like:
   ```
   mysql> select period_add(20200102030405, 2);
   +-------------------------------+
   | period_add(20200102030405, 2) |
   +-------------------------------+
   |                20200102030407 |
   +-------------------------------+
   1 row in set (0.00 sec)
   
   mysql> select period_add(202001020304, 2);
   +-----------------------------+
   | period_add(202001020304, 2) |
   +-----------------------------+
   |                202001020306 |
   +-----------------------------+
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to